Eliminated global variable current_dataset.
[pspp-builds.git] / src / language / lexer / q2c.c
index 9e8af0a246998d55babd943e2a6de952b8de52be..70a4d780db1187014d41cfa4f40a21daf7680e8d 100644 (file)
 #include <stdarg.h>
 #include <time.h>
 #include <errno.h>
-#if HAVE_UNISTD_H
 #include <unistd.h>
-#endif
+#include <libpspp/assertion.h>
 #include <libpspp/compiler.h>
 #include <libpspp/str.h>
-
-
-/* Brokenness. */
-#ifndef EXIT_SUCCESS
-#define EXIT_SUCCESS 0
-#endif
-
-#ifndef EXIT_FAILURE
-#define EXIT_FAILURE 1
-#endif
+#include "exit.h"
 
      
-#include <libpspp/debug-print.h>
-
 /* Max length of an input line. */
 #define MAX_LINE_LEN 1024
 
@@ -55,7 +43,7 @@
 char *program_name;
 
 /* Have the input and output files been opened yet? */
-int is_open;
+bool is_open;
 
 /* Input, output files. */
 FILE *in, *out;
@@ -92,7 +80,7 @@ finish_up (void)
 {
   if (!is_open)
     return;
-  is_open = 0;
+  is_open = false;
   fclose (in);
   fclose (out);
   if (remove (ofn) == -1)
@@ -237,7 +225,7 @@ skip_ws (const char *s)
 
 /* Read one line from the input file into buf.  Lines having special
    formats are handled specially. */
-static int
+static bool
 get_line (void)
 {
   ln++;
@@ -245,7 +233,7 @@ get_line (void)
     {
       if (ferror (in))
        fail ("%s: fgets: %s", ifn, strerror (errno));
-      return 0;
+      return false;
     }
 
   cp = strchr (buf, '\n');
@@ -253,7 +241,7 @@ get_line (void)
     *cp = '\0';
 
   cp = buf;
-  return 1;
+  return true;
 }
 \f
 /* Symbol table manager. */
@@ -338,7 +326,7 @@ find_symbol (int x)
   return iter;
 }
 
-#if DEBUGGING 
+#if DUMP_TOKENS 
 /* Writes a printable representation of the current token to
    stdout. */
 static void
@@ -356,7 +344,7 @@ dump_token (void)
       printf ("PUNCT\t%c\n", token);
     }
 }
-#endif /* DEBUGGING */
+#endif /* DUMP_TOKENS */
 
 /* Reads a token from the input file. */
 static int
@@ -406,7 +394,7 @@ lex_get (void)
   else
     token = *cp++;
   
-#if DEBUGGING
+#if DUMP_TOKENS
   dump_token ();
 #endif
   
@@ -430,29 +418,29 @@ force_string (void)
 }
 
 /* Checks whether the current token is the identifier S; if so, skips
-   the token and returns 1; otherwise, returns 0. */
-static int
+   the token and returns true; otherwise, returns false. */
+static bool
 match_id (const char *s)
 {
   if (token == T_ID && !strcmp (tokstr, s))
     {
       lex_get ();
-      return 1;
+      return true;
     }
-  return 0;
+  return false;
 }
 
 /* Checks whether the current token is T.  If so, skips the token and
-   returns 1; otherwise, returns 0. */
-static int
+   returns true; otherwise, returns false. */
+static bool
 match_token (int t)
 {
   if (token == t)
     {
       lex_get ();
-      return 1;
+      return true;
     }
-  return 0;
+  return false;
 }
 
 /* Force the current token to be T, and skip it. */
@@ -752,8 +740,6 @@ parse_specifiers (subcommand *sbc)
 static void
 parse_subcommand (subcommand *sbc)
 {
-  sbc->arity = ARITY_MANY;
-
   if (match_token ('*'))
     {
       if (def)
@@ -761,8 +747,9 @@ parse_subcommand (subcommand *sbc)
       def = sbc;
     }
 
+  sbc->arity = ARITY_ONCE_ONLY;
   if ( match_token('+'))
-    sbc->arity = ARITY_ONCE_ONLY ;
+    sbc->arity = ARITY_MANY;
   else if (match_token('^'))
     sbc->arity = ARITY_ONCE_EXACTLY ;
 
@@ -961,8 +948,8 @@ dump_specifier_vars (const specifier *spec, const subcommand *sbc)
   }
 }
 
-/* Returns 1 if string T is a PSPP keyword, 0 otherwise. */
-static int
+/* Returns true if string T is a PSPP keyword, false otherwise. */
+static bool
 is_keyword (const char *t)
 {
   static const char *kw[] =
@@ -974,8 +961,8 @@ is_keyword (const char *t)
 
   for (cp = kw; *cp; cp++)
     if (!strcmp (t, *cp))
-      return 1;
-  return 0;
+      return true;
+  return false;
 }
 
 /* Transforms a string NAME into a valid C identifier: makes
@@ -1178,7 +1165,7 @@ dump_declarations (void)
 
   /* Write out prototypes for custom_*() functions as necessary. */
   {
-    int seen = 0;
+    bool seen = false;
     subcommand *sbc;
 
     for (sbc = subcommands; sbc; sbc = sbc->next)
@@ -1186,11 +1173,11 @@ dump_declarations (void)
        {
          if (!seen)
            {
-             seen = 1;
+             seen = true;
              dump (0, "/* Prototype for custom subcommands of %s. */",
                    cmdname);
            }
-         dump (0, "static int %scustom_%s (struct cmd_%s *);",
+         dump (0, "static int %scustom_%s (struct dataset *, struct cmd_%s *, void *);",
                st_lower (prefix), st_lower (sbc->name),
                make_identifier (cmdname));
        }
@@ -1202,7 +1189,7 @@ dump_declarations (void)
   /* Prototypes for parsing and freeing functions. */
   {
     dump (0, "/* Command parsing functions. */");
-    dump (0, "static int parse_%s (struct cmd_%s *);",
+    dump (0, "static int parse_%s (struct dataset *, struct cmd_%s *, void *);",
          make_identifier (cmdname), make_identifier (cmdname));
     dump (0, "static void free_%s (struct cmd_%s *);",
          make_identifier (cmdname), make_identifier (cmdname));
@@ -1335,7 +1322,7 @@ dump_vars_init (int persistent)
                break;
 
              default:
-               assert (0);
+               NOT_REACHED ();
              }
          }
       }
@@ -1597,7 +1584,7 @@ dump_subcommand (const subcommand *sbc)
     }
   else if (sbc->type == SBC_VARLIST)
     {
-      dump (1, "if (!parse_variables (default_dict, &p->%sv_%s, &p->%sn_%s, "
+      dump (1, "if (!parse_variables (dataset_dict (ds), &p->%sv_%s, &p->%sn_%s, "
            "PV_APPEND%s%s))",
            st_lower (sbc->prefix), st_lower (sbc->name),
            st_lower (sbc->prefix), st_lower (sbc->name),
@@ -1608,7 +1595,7 @@ dump_subcommand (const subcommand *sbc)
     }
   else if (sbc->type == SBC_VAR)
     {
-      dump (0, "p->%sv_%s = parse_variable ();",
+      dump (0, "p->%sv_%s = parse_variable (dataset_dict (ds));",
            st_lower (sbc->prefix), st_lower (sbc->name));
       dump (1, "if (!p->%sv_%s)",
            st_lower (sbc->prefix), st_lower (sbc->name));
@@ -1623,7 +1610,7 @@ dump_subcommand (const subcommand *sbc)
          dump (0, "int x;");
        }
       dump (1, "if (!lex_force_string ())");
-      dump (0, "return 0;");
+      dump (0, "return false;");
       outdent ();
       if (sbc->restriction)
        {
@@ -1637,7 +1624,7 @@ dump_subcommand (const subcommand *sbc)
          outdent ();
        }
       dump (0, "free(p->s_%s);", st_lower(sbc->name) );
-      dump (0, "p->s_%s = xstrdup (ds_c_str (&tokstr));",
+      dump (0, "p->s_%s = ds_xstrdup (&tokstr);",
            st_lower (sbc->name));
       dump (0, "lex_get ();");
       if (sbc->restriction)
@@ -1709,7 +1696,7 @@ dump_subcommand (const subcommand *sbc)
     }
   else if (sbc->type == SBC_CUSTOM)
     {
-      dump (1, "switch (%scustom_%s (p))",
+      dump (1, "switch (%scustom_%s (ds, p, aux))",
            st_lower (prefix), st_lower (sbc->name));
       dump (0, "{");
       dump (1, "case 0:");
@@ -1723,7 +1710,7 @@ dump_subcommand (const subcommand *sbc)
       dump (0, "goto lossage;");
       dump (-1, "default:");
       indent ();
-      dump (0, "assert (0);");
+      dump (0, "NOT_REACHED ();");
       dump (-1, "}");
       outdent ();
     }
@@ -1738,7 +1725,9 @@ dump_parser (int persistent)
   indent = 0;
 
   dump (0, "static int");
-  dump (0, "parse_%s (struct cmd_%s *p)", make_identifier (cmdname),
+  dump (0, "parse_%s (struct dataset *ds%s, struct cmd_%s *p, void *aux UNUSED)",
+        make_identifier (cmdname),
+       (def && ( def->type == SBC_VARLIST && def->type == SBC_CUSTOM))?"":" UNUSED",
        make_identifier (cmdname));
   dump (1, "{");
 
@@ -1752,18 +1741,18 @@ dump_parser (int persistent)
     {
       if (def->type == SBC_VARLIST)
        dump (1, "if (token == T_ID "
-              "&& dict_lookup_var (default_dict, tokid) != NULL "
+              "&& dict_lookup_var (dataset_dict (ds), tokid) != NULL "
              "&& lex_look_ahead () != '=')");
       else
        {
          dump (0, "if ((token == T_ID "
-                "&& dict_lookup_var (default_dict, tokid) "
+                "&& dict_lookup_var (dataset_dict (ds), tokid) "
                "&& lex_look_ahead () != '=')");
          dump (1, "     || token == T_ALL)");
        }
       dump (1, "{");
       dump (0, "p->sbc_%s++;", st_lower (def->name));
-      dump (1, "if (!parse_variables (default_dict, &p->%sv_%s, &p->%sn_%s, "
+      dump (1, "if (!parse_variables (dataset_dict (ds), &p->%sv_%s, &p->%sn_%s, "
            "PV_APPEND))",
            st_lower (def->prefix), st_lower (def->name),
            st_lower (def->prefix), st_lower (def->name));
@@ -1774,7 +1763,7 @@ dump_parser (int persistent)
     }
   else if (def && def->type == SBC_CUSTOM)
     {
-      dump (1, "switch (%scustom_%s (p))",
+      dump (1, "switch (%scustom_%s (ds, p, aux))",
            st_lower (prefix), st_lower (def->name));
       dump (0, "{");
       dump (1, "case 0:");
@@ -1788,7 +1777,7 @@ dump_parser (int persistent)
       dump (0, "break;");
       dump (-1, "default:");
       indent ();
-      dump (0, "assert (0);");
+      dump (0, "NOT_REACHED ();");
       dump (-1, "}");
       outdent ();
     }
@@ -1872,12 +1861,12 @@ dump_parser (int persistent)
       }
   }
 
-  dump (-1, "return 1;");
+  dump (-1, "return true;");
   dump (0, nullstr);
   dump (-1, "lossage:");
   indent ();
   dump (0, "free_%s (p);", make_identifier (cmdname));
-  dump (0, "return 0;");
+  dump (0, "return false;");
   dump (-1, "}");
   dump (0, nullstr);
 }
@@ -1944,9 +1933,11 @@ dump_free (int persistent)
              dump (0, "free (p->s_%s);", st_lower (sbc->name));
              break;
            case SBC_DBL_LIST:
-             dump (0, "int i;");
-             dump (1, "for(i = 0; i < MAXLISTS ; ++i)");
-             dump (0, "subc_list_double_destroy(&p->dl_%s[i]);", st_lower (sbc->name));
+              dump (0, "{");
+             dump (1, "int i;");
+             dump (2, "for(i = 0; i < MAXLISTS ; ++i)");
+             dump (1, "subc_list_double_destroy(&p->dl_%s[i]);", st_lower (sbc->name));
+              dump (0, "}");
              outdent();
              break;
            default:
@@ -2005,7 +1996,7 @@ main (int argc, char *argv[])
   if (!out)
     fail ("%s: open: %s.", ofn, strerror (errno));
 
-  is_open = 1;
+  is_open = true;
   buf = xmalloc (MAX_LINE_LEN);
   tokstr = xmalloc (MAX_TOK_LEN);
 
@@ -2041,8 +2032,10 @@ main (int argc, char *argv[])
 
          dump (0, "#include <stdlib.h>");
          dump (0, "#include <libpspp/alloc.h>");
+         dump (0, "#include <libpspp/assertion.h>");
          dump (0, "#include <libpspp/message.h>");
          dump (0, "#include <language/lexer/lexer.h>");
+         dump (0, "#include <language/lexer/variable-parser.h>");
           dump (0, "#include <data/settings.h>");
          dump (0, "#include <libpspp/str.h>");
           dump (0, "#include <language/lexer/subcommand-list.h>");
@@ -2071,7 +2064,5 @@ main (int argc, char *argv[])
       dump (0, "#line %d \"%s\"", ln + 1, ifn);
     }
 
-
-
   return EXIT_SUCCESS;
 }