Index: modules/modgen/grammar.y
===================================================================
--- modules/modgen/grammar.y	(revision 172cdfc80c3ad17a576a439c847fa830ffac37e6)
+++ modules/modgen/grammar.y	(revision d76127c24c920542ed71575d5303af18eb70c1c3)
@@ -1,4 +1,4 @@
 /*
- * $Id: grammar.y,v 1.12 2000-04-17 07:21:24 krueger Exp $
+ * $Id: grammar.y,v 1.13 2000-05-01 19:14:48 krueger Exp $
  */
 
@@ -23,4 +23,5 @@
 extern int yylineno;
 extern int do_create_makefile;
+extern char *sectname[];
  
 extern int init_modgen(moddef *module_def, char *filename);
@@ -57,6 +58,14 @@
 
 %token SECTEND
+/* SECT2: Singular procedure declaration */
+%token SECT2START
 %token SECT2END
+/* SECT3: procedure declaration */
+%token SECT3START
 %token SECT3END
+/* SECT4: C/C++ text */
+%token SECT4START
+%token SECT4END
+
 /*%token PROCEND*/
 %token PROCDECLTOK
@@ -74,4 +83,5 @@
 %token <name> MCODETOK
 %token <name> CODEPART
+%token <name> CMTPART
 %token <name> PROCCMD
 %token <name> ANYTOK
@@ -85,9 +95,24 @@
 %nonassoc '='
 %%
-goal: part1 sect2 sect2end code
-        {
-          if(trace)printf("Finish modules\n");
+goal: part1 sect3 sect3end sect4
+      {
+          if(trace)printf("Finish modules 1\n");
           return 0;
-        }
+      }
+      | part1 sect3full sect4
+      {
+          if(trace)printf("Finish modules 2\n");
+          return 0;
+      }
+      | part1 sect2full sect3full sect4
+      {
+          if(trace)printf("Finish modules 3\n");
+          return 0;
+      }
+      | part1 sect3full sect2full sect4
+      {
+          if(trace)printf("Finish modules 4\n");
+          return 0;
+      }
 ;
 
@@ -134,5 +159,5 @@
         {
           memset(&procedure_decl, 0, sizeof(procdef));
-          if(debug>2)printf("End of section %d\n", sectnum-1);
+          if(debug>2)printf("End of section 1 (new=%d)\n", sectnum);
         }
         ;
@@ -158,5 +183,5 @@
                 }
                 break;
-              case 2: /* pass 2: procedure declaration */
+              case 3: /* pass 3: procedure declaration */
                 if( (vt=checkvar($1, VAR_STRING, &write_cmd)) ) {
                   proc_set_var(&procedure_decl, VAR_STRING, vt, $1, $3.string);
@@ -182,5 +207,5 @@
                 Add2files(&module_def, $3);
                 break;
-              case 2: /* pass 2: procedure declaration */
+              case 3: /* pass 3: procedure declaration */
                 if( (vt=checkvar($1, VAR_FILE, &write_cmd)) ) {
                   proc_set_var(&procedure_decl, VAR_FILE, vt, $1, $3);
@@ -200,5 +225,5 @@
               case 1: /* pass 1: */
                 break;
-              case 2: /* pass 2: procedure declaration */
+              case 3: /* pass 3: procedure declaration */
                 if( (vt=checkvar($1, VAR_FILES, &write_cmd)) ) {
                   proc_set_var(&procedure_decl, VAR_FILES, vt, $1, &$3);
@@ -218,5 +243,5 @@
               case 1: /* pass 1: */
                 break;
-              case 2: /* pass 2: procedure declaration */
+              case 3: /* pass 3: procedure declaration */
                 if( (vt=checkvar($1, VAR_NUM, &write_cmd)) ) {
                   proc_set_var(&procedure_decl, VAR_NUM, vt, $1, &$3);
@@ -244,5 +269,5 @@
                 }
                 break;
-              case 2: /* pass 2: procedure declaration */
+              case 3: /* pass 3: procedure declaration */
                 if( (vt=checkvar($1, VAR_BOOL, &write_cmd)) ) {
                   proc_set_var(&procedure_decl, VAR_BOOL, vt, $1, &$3);
@@ -271,12 +296,28 @@
 ;
 
-sect2:  procdef 
-        | sect2 procdef
-        ;
+sect2full: SECT2START sect2 sect2end;
+
+sect2: procdefsg
+       | sect2 procdefsg
+       ;
 
 sect2end: SECT2END
+{
+          if(debug>2)printf("End of section 'Singular' 2 (%d)\n",
+                            sectnum);
+};
+
+
+sect3full: SECT3START sect3 sect3end;
+
+sect3:  procdef 
+        | sect3 procdef
+        ;
+
+sect3end: SECT3END
         {
           write_finish_functions(&module_def, &procedure_decl);
-          if(debug>2)printf("End of section %d\n", sectnum-1);
+          if(debug>2)printf("End of section 'procedures' 3 (%d)\n",
+                            sectnum);
         }
         ;
@@ -284,20 +325,20 @@
 /*
  */
-procdef: procdecl proccode
-        {
-          if(debug>2)printf("PROCDEF:\n");
+procdefsg: procdeclsg proccode
+        {
+          if(debug>2)printf("SG-PROCDEF:\n");
         }
         | procdecl proccode procdeclexample
         {
-          if(debug>2)printf("PROCDEF mit example:\n");
+          if(debug>2)printf("SG-PROCDEF mit example:\n");
           fflush(module_def.fmtfp);
         }
-        ;
-
-procdecl: procdecl2 '{'
+;
+
+procdeclsg: procdeclsg2 '{'
         {
           setup_proc(&module_def, &procedure_decl);
         }
-        | procdecl2 procdeclhelp '{'
+        | procdeclsg2 procdeclhelp '{'
         {
           setup_proc(&module_def, &procedure_decl);
@@ -305,24 +346,9 @@
         ;
 
-procdecl1: PROCDECLTOK NAME
-        {
-          init_proc(&procedure_decl, $2, NULL, yylineno, LANG_SINGULAR);
-          free($2); 
-          if(write_singular_procedures(&module_def, &procedure_decl))
-            return(myyyerror("Error while creating bin-file\n"));
-        }
-        | STATICTOK PROCDECLTOK NAME
-        {
-          init_proc(&procedure_decl, $3, NULL, yylineno, LANG_SINGULAR);
-          procedure_decl.is_static = TRUE;
-          free($3);
-          if(write_singular_procedures(&module_def, &procedure_decl))
-            return(myyyerror("Error while creating bin-file\n"));
-        };
-
-procdecl2: procdecl1 '(' sgtypelist ')'
-        | funcdecl1
-        | funcdecl1 '(' ')'
-        | funcdecl1 '(' typelist ')'
+procdeclsg2: procdecl1 '(' sgtypelist ')'
+         {
+           write_singular_parameter(&module_def, yylineno, "list", "#");
+           procedure_decl.lineno_other = yylineno;
+         }
         | procdecl1
          {
@@ -337,22 +363,50 @@
         ;
 
-funcdecl1: NAME
-        {
-          if(debug>2)printf("funcdecl1-1\n");
-          init_proc(&procedure_decl, $1, NULL, yylineno);
-          free($1);
-        }
-        | VARTYPETOK NAME
+procdecl1: PROCDECLTOK NAME
+        {
+          init_proc(&procedure_decl, $2, NULL, yylineno, LANG_SINGULAR);
+          free($2); 
+          if(write_singular_procedures(&module_def, &procedure_decl))
+            return(myyyerror("Error while creating bin-file\n"));
+        }
+        | STATICTOK PROCDECLTOK NAME
+        {
+          init_proc(&procedure_decl, $3, NULL, yylineno, LANG_SINGULAR);
+          procedure_decl.is_static = TRUE;
+          free($3);
+          if(write_singular_procedures(&module_def, &procedure_decl))
+            return(myyyerror("Error while creating bin-file\n"));
+        };
+
+procdef: procdecl proccode
+        {
+          if(debug>2)printf("PROCDEF:\n");
+        }
+        | procdecl proccode procdeclexample
+        {
+          if(debug>2)printf("PROCDEF mit example:\n");
+          fflush(module_def.fmtfp);
+        }
+        ;
+
+procdecl: procdecl2 '{'
+        {
+          setup_proc(&module_def, &procedure_decl);
+        }
+        | procdecl2 procdeclhelp '{'
+        {
+          setup_proc(&module_def, &procedure_decl);
+        }
+        ;
+
+procdecl2: funcdecl1
+        | funcdecl1 '(' ')'
+        | funcdecl1 '(' typelist ')';
+
+funcdecl1: VARTYPETOK NAME
         {
           if(debug>2)printf("funcdecl1-2\n");
           init_proc(&procedure_decl, $2, &$1, yylineno);
           free($2);
-        }
-        | STATICTOK NAME
-        {
-          if(debug>2)printf("funcdecl1-3\n");
-          init_proc(&procedure_decl, $2, NULL, yylineno);
-          free($2);
-          procedure_decl.is_static = TRUE;
         }
         | STATICTOK VARTYPETOK NAME
@@ -382,8 +436,15 @@
 proccodeline: CODEPART
         {
+          printf(">>>>(%d) %s<<<<\n", procedure_decl.flags.start_of_code, $1);
           write_codeline(&module_def, &procedure_decl, $1, yylineno-1);
         }
         | proccodeline CODEPART
         {
+          printf(">>>>(%d) %s<<<<\n", procedure_decl.flags.start_of_code, $2);
+          write_codeline(&module_def, &procedure_decl, $2);
+        }
+        | proccodeline CMTPART
+        {
+          printf(">>>>(%d) %s<<<<\n", procedure_decl.flags.start_of_code, $2);
           write_codeline(&module_def, &procedure_decl, $2);
         }
@@ -445,5 +506,14 @@
                           yylineno, $2, sectnum));
                 break;
+
+              case CMD_DECL:
+              case CMD_CHECK:
+                procedure_decl.flags.auto_header = 0;
+              case CMD_NODECL:
+                write_cmd(&module_def, &procedure_decl);
+                break;
+
               default:
+                write_function_header(&module_def, &procedure_decl);
                 write_cmd(&module_def, &procedure_decl);
           }
@@ -465,6 +535,8 @@
                 break;
               default:
-                write_cmd(&module_def, &procedure_decl,
-                          procedure_decl.procname);
+                write_function_header(&module_def, &procedure_decl);
+              case CMD_DECL:
+              case CMD_CHECK:
+                write_cmd(&module_def, &procedure_decl);
           }
           free($2);
@@ -485,4 +557,7 @@
                 break;
               default:
+                write_function_header(&module_def, &procedure_decl);
+              case CMD_DECL:
+              case CMD_CHECK:
                 write_cmd(&module_def, &procedure_decl, $4);
           }
@@ -504,4 +579,7 @@
                 break;
               default:
+                write_function_header(&module_def, &procedure_decl);
+              case CMD_DECL:
+              case CMD_CHECK:
                 write_cmd(&module_def, &procedure_decl, $4);
           }
@@ -523,4 +601,7 @@
                 break;
               default:
+                write_function_header(&module_def, &procedure_decl);
+              case CMD_DECL:
+              case CMD_CHECK:
                 write_cmd(&module_def, &procedure_decl, $4);
           }
@@ -607,9 +688,8 @@
         ;
 
-code:  codeline SECT3END
-        {
-          fprintf(module_def.modfp, "%s", $1);
-        }
-;
+sect4:  SECT4START codeline SECT4END
+        {
+          fprintf(module_def.modfp, "'%s'", $2);
+        };
 
 codeline: CODEPART
Index: modules/modgen/misc.cc
===================================================================
--- modules/modgen/misc.cc	(revision 172cdfc80c3ad17a576a439c847fa830ffac37e6)
+++ modules/modgen/misc.cc	(revision d76127c24c920542ed71575d5303af18eb70c1c3)
@@ -2,5 +2,5 @@
 *  Computer Algebra System SINGULAR     *
 ****************************************/
-/* $Id: misc.cc,v 1.9 2000-03-30 06:35:45 krueger Exp $ */
+/* $Id: misc.cc,v 1.10 2000-05-01 19:17:32 krueger Exp $ */
 /*
 * ABSTRACT: lib parsing
@@ -187,7 +187,9 @@
 } valid_cmds[] = {
   { "declaration",  write_function_declaration, CMD_DECL,   CMDT_SINGLE, 0 },
+  { "error",        write_function_error,       CMD_ERROR,  CMDT_ANY,    0 },
+  { "nodeclaration",write_function_nodecl,      CMD_NODECL, CMDT_SINGLE, 0 },
   { "typecheck",    write_function_typecheck,   CMD_CHECK,  CMDT_SINGLE, 0 },
-  { "return",       write_function_result,      CMD_RETURN, CMDT_EQ,     0 },
-  { "return",       write_function_return,      CMD_RETURN, CMDT_SINGLE, 1 },
+//{ "return",       write_function_result,      CMD_RETURN, CMDT_EQ,     0 },
+//{ "return",       write_function_return,      CMD_RETURN, CMDT_SINGLE, 1 },
   { "return",       write_function_return,      CMD_RETURN, CMDT_0,     1 },
   { "return",       write_function_return,      CMD_RETURN, CMDT_ANY,   1 },
@@ -225,11 +227,8 @@
                     idtyp t, void *arg1 = NULL, void *arg2 = NULL);
 } valid_vars[] = {
-  { "module",       VAR_STRING,  VAR_MODULE,  0 },
+  { "help",         VAR_STRING,  VAR_HELP,    write_main_variable },
+  { "info",         VAR_STRING,  VAR_INFO,    write_main_variable },
+  { "package",      VAR_STRING,  VAR_MODULE,  0 },
   { "version",      VAR_STRING,  VAR_VERSION, write_main_variable },
-  { "info",         VAR_STRING,  VAR_INFO,    write_main_variable },
-  { "help",         VAR_STRING,  VAR_HELP,    write_main_variable },
-#if 0
-  { "do_typecheck", VAR_BOOL,    VAR_TYPECHECK },
-#endif
   { NULL,           VAR_UNKNOWN, VAR_NONE, 0 }
 };
@@ -280,5 +279,6 @@
   printf(")\n");
   if(pi->return_val.typ!=0)
-    printf("\treturn = %s (%s)\n", pi->return_val.name, pi->return_val.typname);
+    printf("\treturn = %s (%s)\n", pi->return_val.name,
+           pi->return_val.typname);
   printf("{%s}\n", pi->c_code);
 }
@@ -443,6 +443,8 @@
             
         default:
-          fprintf(module->fmtfp, "  res->rtyp = %s;\n", pi->return_val.typname);
-          fprintf(module->fmtfp, "  res->data = (void *)%s();\n", pi->funcname);
+          fprintf(module->fmtfp, "  res->rtyp = %s;\n", 
+                  pi->return_val.typname);
+          fprintf(module->fmtfp, "  res->data = (void *)%s();\n", 
+                  pi->funcname);
           fprintf(module->fmtfp, "  return FALSE;");
     }
@@ -521,5 +523,5 @@
 {
   strcpy(type_conv[NONE], "none");
-  strcpy(type_conv[NONE], "void");
+//  strcpy(type_conv[NONE], "void");
   strcpy(type_conv[INT_CMD], "int");
   strcpy(type_conv[RING_CMD], "ring");
Index: modules/modgen/modgen.h
===================================================================
--- modules/modgen/modgen.h	(revision 172cdfc80c3ad17a576a439c847fa830ffac37e6)
+++ modules/modgen/modgen.h	(revision d76127c24c920542ed71575d5303af18eb70c1c3)
@@ -1,4 +1,4 @@
 /*
- *  $Id: modgen.h,v 1.11 2000-03-30 06:36:10 krueger Exp $
+ *  $Id: modgen.h,v 1.12 2000-05-01 19:14:48 krueger Exp $
  *
  */
@@ -33,7 +33,10 @@
 class procflags {
   public:
+  char start_of_code;
   char declaration_done;
   char typecheck_done;
-  
+
+  char auto_header;
+  char do_declaration;
   char do_typecheck;
   char do_return;
@@ -110,5 +113,5 @@
 
 typedef enum { CMD_NONE, CMD_BADSYNTAX, CMD_DECL, CMD_CHECK, CMD_RETURN,
-               CMD_SINGULAR
+               CMD_SINGULAR, CMD_ERROR, CMD_NODECL
 } cmd_token;
 
@@ -127,4 +130,5 @@
 extern int debug;
 extern int trace;
+extern int xyz;
 
 extern int IsCmd(char *n, int & tok);
@@ -173,7 +177,10 @@
 
 /* from proc.cc */
+extern void write_function_header(moddefv module, procdefv proc);
 extern void write_procedure_typecheck(moddefv module, procdefv pi, FILE *fmtfp);
 extern void write_procedure_return(moddefv module, procdefv pi, FILE *fmtfp);
 extern void write_function_declaration(moddefv module, procdefv pi, void *arg = NULL);
+extern void write_function_nodecl(moddefv module, procdefv pi, void *arg = NULL);
+extern void write_function_error(moddefv module, procdefv pi, void *arg = NULL);
 extern void write_function_typecheck(moddefv module, procdefv pi, void *arg = NULL);
 extern void write_function_result(moddefv module, procdefv pi, void *arg = NULL);
Index: modules/modgen/proc.cc
===================================================================
--- modules/modgen/proc.cc	(revision 172cdfc80c3ad17a576a439c847fa830ffac37e6)
+++ modules/modgen/proc.cc	(revision d76127c24c920542ed71575d5303af18eb70c1c3)
@@ -1,4 +1,4 @@
 /*
- * $Id: proc.cc,v 1.12 2000-03-30 06:35:45 krueger Exp $
+ * $Id: proc.cc,v 1.13 2000-05-01 19:14:48 krueger Exp $
  */
 
@@ -80,4 +80,30 @@
 
 /*========================================================================*/
+void write_function_header(
+  moddefv module,
+  procdefv proc
+  )
+{
+  if(!proc->flags.auto_header) return;
+  if(!proc->flags.start_of_code) return;
+
+  if(debug>4) {
+    printf("write_function_header: auto=%d\n", proc->flags.auto_header);
+    printf("write_function_header: declaration: do=%d, done=%d\n",
+           proc->flags.do_declaration, 
+           proc->flags.declaration_done);
+    printf("write_function_header: typecheck: do=%d, done=%d\n",
+           proc->flags.do_typecheck, 
+           proc->flags.typecheck_done);
+  }
+  
+  if(proc->flags.do_declaration && !proc->flags.declaration_done)
+    write_function_declaration(module, proc, module->fmtfp);
+
+  if(proc->flags.do_typecheck && !proc->flags.typecheck_done)
+    write_function_typecheck(module, proc, module->fmtfp);
+}
+
+/*========================================================================*/
 /*
  * write declaration of function to file pointed by 'fp', usualy the
@@ -116,12 +142,16 @@
 {
   int i;
+  if(debug>4) printf("write_function_declaration: do=%d, done=%d\n",
+                     pi->flags.do_declaration, pi->flags.declaration_done);
+  if(pi->flags.declaration_done) return;
+  if(!pi->flags.do_declaration) return;
   if(pi->paramcnt>0) {
     fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename);
     fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name);
-    fprintf(module->fmtfp, "  leftv v = h, v_save;\n");
-    fprintf(module->fmtfp, "  int tok = NONE, index = 0;\n");
+    fprintf(module->fmtfp, "  leftv __v = __h, __v_save;\n");
+    fprintf(module->fmtfp, "  int __tok = NONE, __index = 0;\n");
     for (i=0;i<pi->paramcnt; i++) {
       fprintf(module->fmtfp, 
-              "  sleftv s%s; leftv z%s = &s%s;\n", pi->param[i].varname,
+              "  sleftv __s%s; leftv __z%s = &__s%s;\n", pi->param[i].varname,
               pi->param[i].varname, pi->param[i].varname);
       fprintf(module->fmtfp, "  %s %s;\n", type_conv[pi->param[i].typ],
@@ -131,6 +161,33 @@
     fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename);
     fprintf(module->fmtfp, "\n");
-    pi->flags.declaration_done = 1;
-  }
+  }
+  pi->flags.declaration_done = 1;
+}
+
+/*========================================================================*/
+void write_function_nodecl(
+  moddefv module,
+  procdefv pi,
+  void *arg
+  )
+{
+  pi->flags.do_declaration = 0;
+  pi->flags.do_typecheck = 0;
+  pi->flags.auto_header = 0;
+}
+
+/*========================================================================*/
+void write_function_error(
+  moddefv module,
+  procdefv pi,
+  void *arg
+  )
+{
+  int i;
+
+  fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename);
+  fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name);
+  fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename);
+  fprintf(module->fmtfp, "\n");
 }
 
@@ -142,4 +199,9 @@
   )
 {
+  if(debug>4) printf("write_function_typecheck: do=%d, done=%d\n",
+                     pi->flags.do_typecheck, pi->flags.typecheck_done);
+  if(pi->flags.typecheck_done) return;
+  if(!pi->flags.do_typecheck) return;
+
   fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename);
   fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name);
@@ -178,5 +240,5 @@
     switch( pi->return_val.typ) {
         case SELF_CMD:
-          fprintf(module->modfp_h, "BOOLEAN %s(res, ", pi->funcname);
+          fprintf(module->modfp_h, "BOOLEAN %s(__res, ", pi->funcname);
           break;
 
@@ -192,6 +254,8 @@
     fprintf(module->modfp_h, ");\n\n");
   }
-  else  if(debug>2)printf("CMD: return()\n");
-
+  else {
+    if(debug>2)printf("CMD: return()\n");
+  }
+  
   fprintf(module->fmtfp, "#line %d \"%s\"\n", yylineno, module->filename);
   write_procedure_return(module, pi, module->fmtfp);
@@ -243,5 +307,5 @@
   
   fprintf(fmtfp, "#line %d \"%s\"\n", pi->lineno, module->filename);
-  fprintf(fmtfp, "BOOLEAN mod_%s(leftv res, leftv h)\n{\n", pi->procname);
+  fprintf(fmtfp, "BOOLEAN mod_%s(leftv __res, leftv __h)\n{\n", pi->procname);
 }
 
@@ -274,5 +338,6 @@
     for (i=0;i<pi->paramcnt; i++) gen_func_param_check(fmtfp, pi, i);
 
-    fprintf(fmtfp, "  if(v!=NULL) { tok = v->Typ(); goto mod_%s_error; }\n",
+    fprintf(fmtfp, 
+            "  if(__v!=NULL) { __tok = __v->Typ(); goto mod_%s_error; }\n",
             pi->procname);
 
@@ -315,7 +380,7 @@
   fprintf(module->modfp, "#line %d \"%s.cc\"\n", modlineno++, module->name);
   if(trace)printf("  done.\n");fflush(stdout);
-  fclose(module->fmtfp);
-  fclose(module->fmtfp2);
-  fclose(module->fmtfp3);
+  fclose(module->fmtfp); module->fmtfp = NULL;
+  fclose(module->fmtfp2); module->fmtfp2 = NULL;
+  fclose(module->fmtfp3); module->fmtfp3 = NULL;
   return;
 }
@@ -329,16 +394,16 @@
   )
 {
-  fprintf(fp, "  if(v==NULL) goto mod_%s_error;\n", pi->procname);
-  fprintf(fp, "  tok = v->Typ();\n");
-  fprintf(fp, "  if((index=iiTestConvert(tok, %s))==0)\n",
+  fprintf(fp, "  if(__v==NULL) goto mod_%s_error;\n", pi->procname);
+  fprintf(fp, "  __tok = __v->Typ();\n");
+  fprintf(fp, "  if((__index=iiTestConvert(__tok, %s))==0)\n",
           pi->param[i].typname);
   fprintf(fp, "     goto mod_%s_error;\n", pi->procname);
-  fprintf(fp, "  v_save = v->next;\n");
-  fprintf(fp, "  v->next = NULL;\n");
-  fprintf(fp, "  if(iiConvert(tok, %s, index, v, z%s))\n",
+  fprintf(fp, "  __v_save = __v->next;\n");
+  fprintf(fp, "  __v->next = NULL;\n");
+  fprintf(fp, "  if(iiConvert(__tok, %s, __index, __v, __z%s))\n",
           pi->param[i].typname, pi->param[i].varname);
   fprintf(fp, "     goto mod_%s_error;\n", pi->procname);
-  fprintf(fp, "  v = v_save;\n");
-  fprintf(fp, "  %s = (%s)z%s->Data();\n", pi->param[i].varname,
+  fprintf(fp, "  __v = __v_save;\n");
+  fprintf(fp, "  %s = (%s)__z%s->Data();\n", pi->param[i].varname,
           type_conv[pi->param[i].typ], pi->param[i].varname);
   
@@ -368,6 +433,6 @@
   fprintf(module->fmtfp, "#line @d \"%s.cc\"\n", module->name);
   if(pi->funcname == NULL) {
-    if(!pi->flags.result_done) fprintf(fmtfp, "  res->data = NULL;\n");
-    fprintf(fmtfp, "  res->rtyp = %s;\n", pi->return_val.typname);
+    if(!pi->flags.result_done) fprintf(fmtfp, "  __res->data = NULL;\n");
+    fprintf(fmtfp, "  __res->rtyp = %s;\n", pi->return_val.typname);
     //fprintf(fmtfp, "  res->rtyp = NONE;\n");
     //fprintf(fmtfp, "  res->data = NULL;\n");
@@ -377,7 +442,7 @@
     switch( pi->return_val.typ) {
         case SELF_CMD:
-          fprintf(fmtfp, "    return(%s(res", pi->funcname);
+          fprintf(fmtfp, "    return(%s(__res", pi->funcname);
           for (i=0;i<pi->paramcnt; i++)
-            fprintf(fmtfp, ", (%s) %s->Data()",
+            fprintf(fmtfp, ", (%s) __%s->Data()",
                     type_conv[pi->param[i].typ], pi->param[i].varname);
           fprintf(fmtfp, "));\n\n");
@@ -389,5 +454,5 @@
           fprintf(fmtfp, "  return(%s(", pi->funcname);
           for (i=0;i<pi->paramcnt; i++) {
-            fprintf(fmtfp, "(%s) %s->Data()",
+            fprintf(fmtfp, "(%s) __%s->Data()",
                     type_conv[pi->param[i].typ], pi->param[i].varname);
             if(i<pi->paramcnt-1) fprintf(fmtfp, ", ");
@@ -551,4 +616,5 @@
 
       case LANG_C: 
+        write_function_header(module, proc);
         if(lineno>=0) {
           fprintf(module->fmtfp, "#line %d \"%s\"\n",
Index: modules/modgen/proc_setup.cc
===================================================================
--- modules/modgen/proc_setup.cc	(revision 172cdfc80c3ad17a576a439c847fa830ffac37e6)
+++ modules/modgen/proc_setup.cc	(revision d76127c24c920542ed71575d5303af18eb70c1c3)
@@ -1,4 +1,4 @@
 /*
- * $Id: proc_setup.cc,v 1.5 2000-03-30 06:35:45 krueger Exp $
+ * $Id: proc_setup.cc,v 1.6 2000-05-01 19:14:49 krueger Exp $
  */
 
@@ -63,8 +63,11 @@
   p->language = language;
   p->lineno = lineno;
+  p->flags.auto_header = 1;
+  
+  p->flags.do_declaration = 1;
+  p->flags.declaration_done = 0;
   p->flags.do_typecheck = default_do_typecheck;
+  p->flags.typecheck_done = 0;
   p->flags.do_return = default_do_return;
-  p->flags.declaration_done = 0;
-  p->flags.typecheck_done = 0;
   
   return 0;
Index: modules/modgen/scanner.l
===================================================================
--- modules/modgen/scanner.l	(revision 172cdfc80c3ad17a576a439c847fa830ffac37e6)
+++ modules/modgen/scanner.l	(revision d76127c24c920542ed71575d5303af18eb70c1c3)
@@ -3,5 +3,5 @@
 *  Computer Algebra System SINGULAR     *
 ****************************************/
-/* $Id: scanner.l,v 1.11 2000-04-17 07:21:24 krueger Exp $ */
+/* $Id: scanner.l,v 1.12 2000-05-01 19:14:49 krueger Exp $ */
 #include <stdio.h>
 #include <string.h>
@@ -13,5 +13,5 @@
 #include "grammar.h"
 
-/* #define DEBUG 3 */
+#define DEBUG 3
  
 #  define YYLP_ERR_NONE    0
@@ -40,6 +40,12 @@
 
   extern int sectnum;
+  static int oldsectnum = 1;
   extern moddef module_def;
-
+  extern procdef procedure_decl;
+
+  char *sectname[] = { "sect -0", "section 1",
+                       "Singular", "procedures", "C-part" 
+  };
+  
   struct _states {
     char name[32];
@@ -56,4 +62,5 @@
   void switch_state(int state, int new_state, char *name);
   void pop_state();
+  int return_sect_token(int passed, int old_sect, int new_sect);
   extern int init_modgen(moddef *module_def, char *filename);
  
@@ -123,7 +130,9 @@
 %x module
 %x COMMENT
+%x COMMENTB
 %x STRING
 %x SECT2
 %x SECT3
+%x SECT4
 %x PROCDECL
 %x EXAMPLE
@@ -142,8 +151,72 @@
        static int quote  = 0;  /* " */
 
-<COMMENT,STRING,INITIAL><<EOF>>   {
+<COMMENT,COMMENTB,STRING,INITIAL><<EOF>>   {
           printf( "EOF encountered inside an action\n");
 	  printf("ERRRRROOOOORRR\n");
                  }
+
+<INITIAL,SECT2,SECT3,SECT4>{
+        ^"%Singular".* {
+                  int passed = (oldsectnum == sectnum) ? 0 : 1;
+                  int old_sect = sectnum;
+
+                  oldsectnum = sectnum;
+                  sectnum = 2;
+                  if(!passed) yyless(0);
+                  else push_state(YYSTATE, SECT2, "SECT2");
+                  return(return_sect_token(passed, old_sect, sectnum));
+                }
+        "%Singular".* {
+                  int passed = (oldsectnum == sectnum) ? 0 : 1;
+                  int old_sect = sectnum;
+
+                  oldsectnum = sectnum;
+                  sectnum = 2;
+                  if(!passed) yyless(0);
+                  else push_state(YYSTATE, SECT2, "SECT2");
+                  return(return_sect_token(passed, old_sect, sectnum));
+                }
+        ^"%procedures".* { 
+                  int passed = (oldsectnum == sectnum) ? 0 : 1;
+                  int old_sect = sectnum;
+
+                  oldsectnum = sectnum;
+                  sectnum = 3;
+                  if(!passed) yyless(0);
+                  else push_state(YYSTATE, SECT3, "SECT3");
+                  return(return_sect_token(passed, old_sect, sectnum));
+                }
+        "%procedures".* { 
+                  int passed = (oldsectnum == sectnum) ? 0 : 1;
+                  int old_sect = sectnum;
+
+                  oldsectnum = sectnum;
+                  sectnum = 3;
+                  if(!passed) yyless(0);
+                  else push_state(YYSTATE, SECT3, "SECT3");
+                  return(return_sect_token(passed, old_sect, sectnum));
+                }
+        ^"%C".* { 
+                  int passed = (oldsectnum == sectnum) ? 0 : 1;
+                  int old_sect = sectnum;
+
+                  oldsectnum = sectnum;
+                  sectnum = 4;
+                  if(!passed) yyless(0);
+                  else push_state(YYSTATE, SECT4, "SECT4");
+                  return(return_sect_token(passed, old_sect, sectnum));
+                }
+        "%C".* { 
+                  int passed = (oldsectnum == sectnum) ? 0 : 1;
+                  int old_sect = sectnum;
+
+                  oldsectnum = sectnum;
+                  sectnum = 4;
+                  if(!passed) yyless(0);
+                  else push_state(YYSTATE, SECT4, "SECT4-2");
+                  return(return_sect_token(passed, old_sect, sectnum));
+                }
+              }
+
 
 <INITIAL>{
@@ -174,13 +247,5 @@
                   push_state(YYSTATE, STRING, "string");
                 }
-        ^"%%".* { 
-		  sectnum = 2;
-#if DEBUG
-                  printf("Goto section %d\n", sectnum);
-#endif
-                  push_state(YYSTATE, SECT2, "SECT2");
-                  /*line_directive_out( (FILE *) 0, 1 );*/
-                  return SECTEND;
-                }
+
 	^{NAME} {
 	          lvalp->name = strdup(yytext);
@@ -192,4 +257,5 @@
                 }
 	   }
+
 <CODEBLOCK>{
             ^"%}".*{NL} {
@@ -244,14 +310,4 @@
 
 <SECT2>{
-        ^"%%".* {
-	          sectnum = 3;
-#if DEBUG
-                  printf("Goto section %d\n", sectnum);
-#endif
-                  switch_state(YYSTATE, SECT3, "SECT3");
-		  /*yyterminate();*/
-                  /*line_directive_out( (FILE *) 0, 1 );*/
-                  return SECT2END;
-                }
         {NL}    { yylineno++; }
         {WS}    /* ignore */
@@ -266,4 +322,24 @@
 		  return PROCDECLTOK;
                 }
+        example {
+#if DEBUG > 1
+	          printf(">>>EXAMPLE\n");
+#endif
+                  brace1 = 0; /* { */
+                  brace2 = 0; /* ( */
+                  brace3 = 0; /* [ */
+	            push_state(YYSTATE, EXAMPLE, "EXAMPLE");
+		        return EXAMPLETOK;
+                }
+ 	    static  { return STATICTOK; }
+        .       { printf("SG(%d) <<<'%s' ", yylineno, yytext); }
+       }
+
+<SECT3>{
+        {NL}    { yylineno++; }
+        {WS}    /* ignore */
+        ;       /* ignore */
+        "/*"      push_state(YYSTATE, COMMENT, "COMMENT"); yymore();
+        "//".*{NL}  ++yylineno; ACTION_ECHO; 
         example {
 #if DEBUG > 1
@@ -300,5 +376,5 @@
 		   }
                  }
-        .       { printf("<<<'%s' ", yytext); }
+        .       { printf("PR(%d) <<<'%s' ", yylineno, yytext); }
        }
 
@@ -313,5 +389,5 @@
 }
 
-<SECT3>{
+<SECT4>{
         {NL}    { yylineno++; }
         .*(\n?) { lvalp->name = yytext; return CODEPART; }
@@ -319,5 +395,5 @@
         <<EOF>>  { sectnum = 0;
 	           iseof=1;
-	           return SECT3END;
+	           return SECT4END;
                    /*yyterminate();*/
                  }
@@ -374,4 +450,16 @@
        }
 
+<COMMENTB>{
+        "*/"          { ACTION_ECHO;
+                        pop_state();
+                        lvalp->name = yytext;
+                        return CMTPART;
+                	yymore();
+                      }
+        "*"           yymore();
+        {NL}          { yylineno++; yymore(); }
+        .             { yymore(); }
+	  }
+
 <CODEBLOCK2>{
          "/*"      push_state(YYSTATE, COMMENT, "COMMENT"); yymore();
@@ -406,6 +494,9 @@
                  }
          {NL}    { yylineno++; lvalp->name = yytext; return CODEPART; }
-	 "%" { push_state(YYSTATE, PROCCMDBLOCK, "PROCCMDBLOCK"); return '%'; }
-         .       { yymore(); }
+	 "%" { push_state(YYSTATE, PROCCMDBLOCK, "PROCCMDBLOCK");
+               procedure_decl.flags.start_of_code = 1;
+               return '%'; }
+         " " { yymore(); }
+         .   { yymore(); procedure_decl.flags.start_of_code = 1; }
 
        }
@@ -505,4 +596,33 @@
 }
 
+int return_sect_token(
+  int passed,
+  int old_sect,
+  int new_sect
+  )
+{
+  if(passed) {
+#if DEBUG
+    printf("Go to section '%s' (%d)\n", sectname[new_sect], new_sect);
+#endif
+    switch(new_sect) {
+        case 2: return SECT2START;
+        case 3: return SECT3START;
+        case 4: return SECT4START;
+        default: return SECTEND;
+    }
+  } else {
+#if DEBUG
+    printf("End of section '%s' (%d)\n", sectname[old_sect], old_sect);
+#endif
+    switch(old_sect) {
+        case 2: return SECT2END;
+        case 3: return SECT3END;
+        case 4: return SECT4END;
+        default: return SECTEND;
+    }
+  }
+}
+
 void add_action(char *new_text)
 {
@@ -518,4 +638,7 @@
       printf("3>%s<#\n", new_text);
       break;
+    case 4:
+      printf("4>%s<#\n", new_text);
+      break;
   }
 }
Index: modules/modgen/utils.cc
===================================================================
--- modules/modgen/utils.cc	(revision 172cdfc80c3ad17a576a439c847fa830ffac37e6)
+++ modules/modgen/utils.cc	(revision d76127c24c920542ed71575d5303af18eb70c1c3)
@@ -1,4 +1,4 @@
 /*
- * $Id: utils.cc,v 1.8 2000-03-30 06:35:46 krueger Exp $
+ * $Id: utils.cc,v 1.9 2000-05-01 19:14:49 krueger Exp $
  */
 
@@ -123,4 +123,4 @@
   vprintf(fmt, ap);
   va_end(ap);
-  return 1;
+  return 2;
 }
