lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / language / data-io / get-data.c
index 7b38d62233e1d0ff76500c201f773a6141df5625..f65e8ac782547971f63f0bb3210eaae6040340d5 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2007 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <stdlib.h>
 
-#include <data/gnumeric-reader.h>
-
-#include <data/dictionary.h>
-#include <data/format.h>
-#include <data/procedure.h>
-#include <language/command.h>
-#include <language/data-io/data-parser.h>
-#include <language/data-io/data-reader.h>
-#include <language/data-io/file-handle.h>
-#include <language/data-io/placement-parser.h>
-#include <language/lexer/format-parser.h>
-#include <language/lexer/lexer.h>
-#include <libpspp/message.h>
+#include "data/dictionary.h"
+#include "data/format.h"
+#include "data/gnumeric-reader.h"
+#include "data/procedure.h"
+#include "data/psql-reader.h"
+#include "data/settings.h"
+#include "language/command.h"
+#include "language/data-io/data-parser.h"
+#include "language/data-io/data-reader.h"
+#include "language/data-io/file-handle.h"
+#include "language/data-io/placement-parser.h"
+#include "language/lexer/format-parser.h"
+#include "language/lexer/lexer.h"
+#include "libpspp/i18n.h"
+#include "libpspp/message.h"
+
+#include "gl/xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
 static int parse_get_txt (struct lexer *lexer, struct dataset *);
+static int parse_get_psql (struct lexer *lexer, struct dataset *);
 
 int
 cmd_get_data (struct lexer *lexer, struct dataset *ds)
 {
-  lex_force_match (lexer, '/');
+  lex_force_match (lexer, T_SLASH);
 
   if (!lex_force_match_id (lexer, "TYPE"))
     return CMD_FAILURE;
 
-  lex_force_match (lexer, '=');
+  lex_force_match (lexer, T_EQUALS);
 
   if (lex_match_id (lexer, "GNM"))
     return parse_get_gnm (lexer, ds);
   else if (lex_match_id (lexer, "TXT"))
     return parse_get_txt (lexer, ds);
+  else if (lex_match_id (lexer, "PSQL"))
+    return parse_get_psql (lexer, ds);
 
-  msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
+  msg (SE, _("Unsupported TYPE %s."), lex_tokcstr (lexer));
   return CMD_FAILURE;
 }
 
+static int
+parse_get_psql (struct lexer *lexer, struct dataset *ds)
+{
+  struct psql_read_info psql;
+  psql.allow_clear = false;
+  psql.conninfo = NULL;
+  psql.str_width = -1;
+  psql.bsize = -1;
+  ds_init_empty (&psql.sql);
+
+  lex_force_match (lexer, T_SLASH);
+
+  if (!lex_force_match_id (lexer, "CONNECT"))
+    goto error;
+
+  lex_force_match (lexer, T_EQUALS);
+
+  if (!lex_force_string (lexer))
+    goto error;
+
+  psql.conninfo = ss_xstrdup (lex_tokss (lexer));
+
+  lex_get (lexer);
+
+  while (lex_match (lexer, T_SLASH) )
+    {
+      if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
+       {
+         lex_match (lexer, T_EQUALS);
+         psql.str_width = lex_integer (lexer);
+         lex_get (lexer);
+       }
+      else if ( lex_match_id (lexer, "BSIZE"))
+       {
+         lex_match (lexer, T_EQUALS);
+         psql.bsize = lex_integer (lexer);
+         lex_get (lexer);
+       }
+      else if ( lex_match_id (lexer, "UNENCRYPTED"))
+       {
+         psql.allow_clear = true;
+       }
+      else if (lex_match_id (lexer, "SQL"))
+       {
+         lex_match (lexer, T_EQUALS);
+         if ( ! lex_force_string (lexer) )
+           goto error;
+
+         ds_put_substring (&psql.sql, lex_tokss (lexer));
+         lex_get (lexer);
+       }
+     }
+  {
+    struct dictionary *dict = NULL;
+    struct casereader *reader = psql_open_reader (&psql, &dict);
+
+    if ( reader )
+      proc_set_active_file (ds, reader, dict);
+  }
+
+  ds_destroy (&psql.sql);
+  free (psql.conninfo);
+
+  return CMD_SUCCESS;
+
+ error:
+
+  ds_destroy (&psql.sql);
+  free (psql.conninfo);
+
+  return CMD_FAILURE;
+}
 
 static int
 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
 {
   struct gnumeric_read_info gri  = {NULL, NULL, NULL, 1, true, -1};
 
-  lex_force_match (lexer, '/');
+  lex_force_match (lexer, T_SLASH);
 
   if (!lex_force_match_id (lexer, "FILE"))
     goto error;
 
-  lex_force_match (lexer, '=');
+  lex_force_match (lexer, T_EQUALS);
 
   if (!lex_force_string (lexer))
     goto error;
 
-  gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
+  gri.file_name = utf8_to_filename (lex_tokcstr (lexer));
 
   lex_get (lexer);
 
-  while (lex_match (lexer, '/') )
+  while (lex_match (lexer, T_SLASH) )
     {
       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
        {
-         lex_match (lexer, '=');
+         lex_match (lexer, T_EQUALS);
          gri.asw = lex_integer (lexer);
+         lex_get (lexer);
        }
       else if (lex_match_id (lexer, "SHEET"))
        {
-         lex_match (lexer, '=');
+         lex_match (lexer, T_EQUALS);
          if (lex_match_id (lexer, "NAME"))
            {
              if ( ! lex_force_string (lexer) )
                goto error;
 
-             gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
+             gri.sheet_name = ss_xstrdup (lex_tokss (lexer));
              gri.sheet_index = -1;
+
+             lex_get (lexer);
            }
          else if (lex_match_id (lexer, "INDEX"))
            {
              gri.sheet_index = lex_integer (lexer);
+             lex_get (lexer);
            }
          else
            goto error;
        }
       else if (lex_match_id (lexer, "CELLRANGE"))
        {
-         lex_match (lexer, '=');
+         lex_match (lexer, T_EQUALS);
 
          if (lex_match_id (lexer, "FULL"))
            {
              gri.cell_range = NULL;
-             lex_put_back (lexer, T_ID);
            }
          else if (lex_match_id (lexer, "RANGE"))
            {
              if ( ! lex_force_string (lexer) )
                goto error;
 
-             gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
+             gri.cell_range = ss_xstrdup (lex_tokss (lexer));
+             lex_get (lexer);
            }
          else
            goto error;
        }
       else if (lex_match_id (lexer, "READNAMES"))
        {
-         lex_match (lexer, '=');
+         lex_match (lexer, T_EQUALS);
 
          if ( lex_match_id (lexer, "ON"))
            {
@@ -136,14 +219,12 @@ parse_get_gnm (struct lexer *lexer, struct dataset *ds)
            }
          else
            goto error;
-         lex_put_back (lexer, T_ID);
        }
       else
        {
-         printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
+         lex_error (lexer, NULL);
          goto error;
        }
-      lex_get (lexer);
     }
 
   {
@@ -192,24 +273,25 @@ static int
 parse_get_txt (struct lexer *lexer, struct dataset *ds)
 {
   struct data_parser *parser = NULL;
-  struct dictionary *dict = NULL;
+  struct dictionary *dict = dict_create ();
   struct file_handle *fh = NULL;
   struct dfm_reader *reader = NULL;
+  char *name = NULL;
 
   int record;
   enum data_parser_type type;
   bool has_type;
 
-  lex_force_match (lexer, '/');
+  lex_force_match (lexer, T_SLASH);
 
   if (!lex_force_match_id (lexer, "FILE"))
     goto error;
-  lex_force_match (lexer, '=');
+  lex_force_match (lexer, T_EQUALS);
   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
   if (fh == NULL)
     goto error;
 
-  parser = data_parser_create ();
+  parser = data_parser_create (dict);
   has_type = false;
   data_parser_set_type (parser, DP_DELIMITED);
   data_parser_set_span (parser, false);
@@ -218,14 +300,14 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
 
   for (;;)
     {
-      if (!lex_force_match (lexer, '/'))
+      if (!lex_force_match (lexer, T_SLASH))
         goto error;
 
       if (lex_match_id (lexer, "ARRANGEMENT"))
         {
           bool ok;
 
-         lex_match (lexer, '=');
+         lex_match (lexer, T_EQUALS);
           if (lex_match_id (lexer, "FIXED"))
             ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
           else if (lex_match_id (lexer, "DELIMITED"))
@@ -233,7 +315,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
                            DP_DELIMITED, &has_type);
           else
             {
-              lex_error (lexer, _("expecting FIXED or DELIMITED"));
+              lex_error (lexer, _("expecting %s or %s"), "FIXED", "DELIMITED");
               goto error;
             }
           if (!ok)
@@ -241,7 +323,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
         }
       else if (lex_match_id (lexer, "FIRSTCASE"))
         {
-         lex_match (lexer, '=');
+         lex_match (lexer, T_EQUALS);
           if (!lex_force_int (lexer))
             goto error;
           if (lex_integer (lexer) < 1)
@@ -256,7 +338,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
         {
           if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
             goto error;
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
           if (lex_match_id (lexer, "LINE"))
             data_parser_set_span (parser, false);
           else if (lex_match_id (lexer, "VARIABLES"))
@@ -271,7 +353,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
             }
           else
             {
-              lex_error (lexer, _("expecting LINE or VARIABLES"));
+              lex_error (lexer, _("expecting %s or %s"), "LINE", "VARIABLES");
               goto error;
             }
         }
@@ -279,7 +361,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
         {
           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
             goto error;
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
           if (!lex_force_int (lexer))
             goto error;
           if (lex_integer (lexer) < 1)
@@ -292,7 +374,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
         }
       else if (lex_match_id (lexer, "IMPORTCASES"))
         {
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
           if (lex_match (lexer, T_ALL))
             {
               data_parser_set_case_limit (parser, -1);
@@ -332,60 +414,71 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
 
           if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
             goto error;
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
 
           if (!lex_force_string (lexer))
             goto error;
 
-          s = ds_ss (lex_tokstr (lexer));
+          /* XXX should support multibyte UTF-8 characters */
+          s = lex_tokss (lexer);
           if (ss_match_string (&s, ss_cstr ("\\t")))
             ds_put_cstr (&hard_seps, "\t");
           if (ss_match_string (&s, ss_cstr ("\\\\")))
             ds_put_cstr (&hard_seps, "\\");
-          while ((c = ss_get_char (&s)) != EOF)
+          while ((c = ss_get_byte (&s)) != EOF)
             if (c == ' ')
               soft_seps = " ";
             else
-              ds_put_char (&hard_seps, c);
+              ds_put_byte (&hard_seps, c);
           data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
           data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
           ds_destroy (&hard_seps);
 
           lex_get (lexer);
         }
-      else if (lex_match_id (lexer, "QUALIFIER"))
+      else if (lex_match_id (lexer, "QUALIFIERS"))
         {
-          if (!set_type (parser, "QUALIFIER", DP_DELIMITED, &has_type))
+          if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
             goto error;
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
 
           if (!lex_force_string (lexer))
             goto error;
 
-          data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
+          /* XXX should support multibyte UTF-8 characters */
+          if (settings_get_syntax () == COMPATIBLE
+              && ss_length (lex_tokss (lexer)) != 1)
+            {
+              msg (SE, _("In compatible syntax mode, the QUALIFIER string "
+                         "must contain exactly one character."));
+              goto error;
+            }
+
+          data_parser_set_quotes (parser, lex_tokss (lexer));
           lex_get (lexer);
         }
+      else if (settings_get_syntax () == ENHANCED
+               && lex_match_id (lexer, "ESCAPE"))
+        data_parser_set_quote_escape (parser, true);
       else if (lex_match_id (lexer, "VARIABLES"))
         break;
       else
         {
-          lex_error (lexer, _("expecting VARIABLES"));
+          lex_error (lexer, _("expecting %s"), "VARIABLES");
           goto error;
         }
     }
-  lex_match (lexer, '=');
+  lex_match (lexer, T_EQUALS);
 
-  dict = dict_create ();
   record = 1;
   type = data_parser_get_type (parser);
   do
     {
-      char name[VAR_NAME_LEN + 1];
       struct fmt_spec input, output;
-      int fc, lc;
       struct variable *v;
+      int fc, lc;
 
-      while (type == DP_FIXED && lex_match (lexer, '/'))
+      while (type == DP_FIXED && lex_match (lexer, T_SLASH))
         {
           if (!lex_force_int (lexer))
             goto error;
@@ -410,9 +503,10 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
           lex_get (lexer);
         }
 
-      if (!lex_force_id (lexer))
+      if (!lex_force_id (lexer)
+          || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
         goto error;
-      strcpy (name, lex_tokid (lexer));
+      name = xstrdup (lex_tokcstr (lexer));
       lex_get (lexer);
 
       if (type == DP_DELIMITED)
@@ -449,8 +543,10 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
       else
         data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
                                      name, record, fc);
+      free (name);
+      name = NULL;
     }
-  while (lex_token (lexer) != '.');
+  while (lex_token (lexer) != T_ENDCMD);
 
   reader = dfm_open_reader (fh, lexer);
   if (reader == NULL)
@@ -464,5 +560,6 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
   data_parser_destroy (parser);
   dict_destroy (dict);
   fh_unref (fh);
+  free (name);
   return CMD_CASCADING_FAILURE;
 }