DATA LIST: Improve error messages and coding style.
[pspp] / src / language / data-io / get-data.c
index 061fde1c03a0a4628873d58be10041a8c7cce38f..44741181667d36c38f6955c9896f49de7011e0c3 100644 (file)
@@ -59,7 +59,7 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds)
 {
   char *tok = NULL;
   struct spreadsheet_read_options opts;
-  
+
   opts.sheet_name = NULL;
   opts.sheet_index = -1;
   opts.cell_range = NULL;
@@ -79,7 +79,7 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds)
 
   if (s)
     tok = strdup (s);
-  
+
   if (lex_match_id (lexer, "TXT"))
     {
       free (tok);
@@ -90,7 +90,7 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds)
       free (tok);
       return parse_get_psql (lexer, ds);
     }
-  else if (lex_match_id (lexer, "GNM") || 
+  else if (lex_match_id (lexer, "GNM") ||
       lex_match_id (lexer, "ODS"))
     {
       char *filename = NULL;
@@ -98,9 +98,9 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds)
        goto error;
 
       struct spreadsheet *spreadsheet = NULL;
-      if ( 0 == strncasecmp (tok, "GNM", 3))
+      if (0 == strncasecmp (tok, "GNM", 3))
         spreadsheet = gnumeric_probe (filename, true);
-      else if ( 0 == strncasecmp (tok, "ODS", 3))
+      else if (0 == strncasecmp (tok, "ODS", 3))
         spreadsheet = ods_probe (filename, true);
 
       if (spreadsheet == NULL)
@@ -124,8 +124,7 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds)
       spreadsheet_unref (spreadsheet);
     }
   else
-    msg (SE, _("Unsupported TYPE %s."), tok);
-
+    lex_error_expecting (lexer, "TXT", "PSQL", "GNM", "ODS");
 
  error:
   destroy_spreadsheet_read_info (&opts);
@@ -159,34 +158,34 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds)
 
   lex_get (lexer);
 
-  while (lex_match (lexer, T_SLASH) )
+  while (lex_match (lexer, T_SLASH))
     {
-      if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
+      if (lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
        {
          lex_match (lexer, T_EQUALS);
-          if (lex_force_int (lexer))
+          if (lex_force_int_range (lexer, "ASSUMEDSTRWIDTH", 1, 32767))
             {
               psql.str_width = lex_integer (lexer);
               lex_get (lexer);
             }
        }
-      else if ( lex_match_id (lexer, "BSIZE"))
+      else if (lex_match_id (lexer, "BSIZE"))
        {
          lex_match (lexer, T_EQUALS);
-          if (lex_force_int (lexer))
+          if (lex_force_int_range (lexer, "BSIZE", 1, INT_MAX))
             {
               psql.bsize = lex_integer (lexer);
               lex_get (lexer);
             }
        }
-      else if ( lex_match_id (lexer, "UNENCRYPTED"))
+      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) )
+         if (! lex_force_string (lexer))
            goto error;
 
          ds_put_substring (&psql.sql, lex_tokss (lexer));
@@ -197,7 +196,7 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds)
     struct dictionary *dict = NULL;
     struct casereader *reader = psql_open_reader (&psql, &dict);
 
-    if ( reader )
+    if (reader)
       {
         dataset_set_dict (ds, dict);
         dataset_set_source (ds, reader);
@@ -218,7 +217,7 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds)
 }
 
 static bool
-parse_spreadsheet (struct lexer *lexer, char **filename, 
+parse_spreadsheet (struct lexer *lexer, char **filename,
                   struct spreadsheet_read_options *opts)
 {
   opts->sheet_index = 1;
@@ -243,12 +242,12 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
 
   lex_get (lexer);
 
-  while (lex_match (lexer, T_SLASH) )
+  while (lex_match (lexer, T_SLASH))
     {
-      if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
+      if (lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
        {
          lex_match (lexer, T_EQUALS);
-          if (lex_force_int (lexer))
+          if (lex_force_int_range (lexer, "ASSUMEDSTRWIDTH", 1, 32767))
             {
               opts->asw = lex_integer (lexer);
               lex_get (lexer);
@@ -259,7 +258,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
          lex_match (lexer, T_EQUALS);
          if (lex_match_id (lexer, "NAME"))
            {
-             if ( ! lex_force_string (lexer) )
+             if (! lex_force_string (lexer))
                goto error;
 
              opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
@@ -269,21 +268,14 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
            }
          else if (lex_match_id (lexer, "INDEX"))
            {
-              if (!lex_force_int (lexer))
+              if (!lex_force_int_range (lexer, "INDEX", 1, INT_MAX))
                 goto error;
-
              opts->sheet_index = lex_integer (lexer);
-             if (opts->sheet_index <= 0)
-               {
-                 msg (SE, _("The sheet index must be greater than or equal to 1"));
-                 goto error;
-               }
              lex_get (lexer);
            }
          else
            {
-             msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
-                  "/SHEET", "NAME", "INDEX");
+              lex_error_expecting (lexer, "NAME", "INDEX");
              goto error;
            }
        }
@@ -297,7 +289,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
            }
          else if (lex_match_id (lexer, "RANGE"))
            {
-             if ( ! lex_force_string (lexer) )
+             if (! lex_force_string (lexer))
                goto error;
 
              opts->cell_range = ss_xstrdup (lex_tokss (lexer));
@@ -305,8 +297,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
            }
          else
            {
-             msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
-                  "/CELLRANGE", "FULL", "RANGE");
+              lex_error_expecting (lexer, "FULL", "RANGE");
              goto error;
            }
        }
@@ -314,7 +305,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
        {
          lex_match (lexer, T_EQUALS);
 
-         if ( lex_match_id (lexer, "ON"))
+         if (lex_match_id (lexer, "ON"))
            {
              opts->read_names = true;
            }
@@ -324,8 +315,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
            }
          else
            {
-             msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
-                  "/READNAMES", "ON", "OFF");
+              lex_error_expecting (lexer, "ON", "OFF");
              goto error;
            }
        }
@@ -389,7 +379,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
   if (fh == NULL)
     goto error;
 
-  parser = data_parser_create (dict);
+  parser = data_parser_create ();
   has_type = false;
   data_parser_set_type (parser, DP_DELIMITED);
   data_parser_set_span (parser, false);
@@ -425,7 +415,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
                            DP_DELIMITED, &has_type);
           else
             {
-              lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
+              lex_error_expecting (lexer, "FIXED", "DELIMITED");
               goto error;
             }
           if (!ok)
@@ -434,13 +424,8 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
       else if (lex_match_id (lexer, "FIRSTCASE"))
         {
          lex_match (lexer, T_EQUALS);
-          if (!lex_force_int (lexer))
+          if (!lex_force_int_range (lexer, "FIRSTCASE", 1, INT_MAX))
             goto error;
-          if (lex_integer (lexer) < 1)
-            {
-              msg (SE, _("Value of %s must be 1 or greater."), "FIRSTCASE");
-              goto error;
-            }
           data_parser_set_skip (parser, lex_integer (lexer) - 1);
           lex_get (lexer);
         }
@@ -463,7 +448,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
             }
           else
             {
-              lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
+              lex_error_expecting (lexer, "LINE", "VARIABLES");
               goto error;
             }
         }
@@ -472,18 +457,14 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
             goto error;
           lex_match (lexer, T_EQUALS);
-          if (!lex_force_int (lexer))
+          if (!lex_force_int_range (lexer, "FIXCASE", 1, INT_MAX))
             goto error;
-          if (lex_integer (lexer) < 1)
-            {
-              msg (SE, _("Value of %s must be 1 or greater."), "FIXCASE");
-              goto error;
-            }
           data_parser_set_records (parser, lex_integer (lexer));
           lex_get (lexer);
         }
       else if (lex_match_id (lexer, "IMPORTCASES"))
         {
+          int start_ofs = lex_ofs (lexer) - 1;
           lex_match (lexer, T_EQUALS);
           if (lex_match (lexer, T_ALL))
             {
@@ -501,8 +482,9 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
                 goto error;
               lex_get (lexer);
             }
-          msg (SW, _("Ignoring obsolete IMPORTCASES subcommand.  (N OF CASES "
-                     "or SAMPLE may be used to substitute.)"));
+          lex_ofs_msg (lexer, SW, start_ofs, lex_ofs (lexer) - 1,
+                       _("Ignoring obsolete IMPORTCASES subcommand.  (N OF "
+                         "CASES or SAMPLE may be used to substitute.)"));
         }
       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
         {
@@ -548,8 +530,8 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
           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."));
+              lex_error (lexer, _("In compatible syntax mode, the QUALIFIER "
+                                  "string must contain exactly one character."));
               goto error;
             }
 
@@ -560,7 +542,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
         break;
       else
         {
-          lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
+          lex_error_expecting (lexer, "VARIABLES");
           goto error;
         }
     }
@@ -576,84 +558,92 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
 
       while (type == DP_FIXED && lex_match (lexer, T_SLASH))
         {
-          if (!lex_force_int (lexer))
+          if (!lex_force_int_range (lexer, NULL, record,
+                                    data_parser_get_records (parser)))
             goto error;
-          if (lex_integer (lexer) < record)
-            {
-              msg (SE, _("The record number specified, %ld, is at or "
-                         "before the previous record, %d.  Data "
-                         "fields must be listed in order of "
-                         "increasing record number."),
-                   lex_integer (lexer), record);
-              goto error;
-            }
-          if (lex_integer (lexer) > data_parser_get_records (parser))
-            {
-              msg (SE, _("The record number specified, %ld, exceeds "
-                         "the number of records per case specified "
-                         "on FIXCASE, %d."),
-                   lex_integer (lexer), data_parser_get_records (parser));
-              goto error;
-            }
           record = lex_integer (lexer);
           lex_get (lexer);
         }
 
+      int name_ofs = lex_ofs (lexer);
+      if (!lex_force_id (lexer))
+        goto error;
       name = xstrdup (lex_tokcstr (lexer));
-      if (!lex_force_id (lexer)
-          || !dict_id_is_valid (dict, name, true))
-       {
+      char *error = dict_id_is_valid__ (dict, name);
+      if (error)
+        {
+          lex_error (lexer, "%s", error);
+          free (error);
          goto error;
        }
       lex_get (lexer);
       if (type == DP_DELIMITED)
         {
-          if (!parse_format_specifier (lexer, &input)
-             || !fmt_check_input (&input))
+          if (!parse_format_specifier (lexer, &input))
+            goto error;
+          error = fmt_check_input__ (&input);
+          if (error)
            {
+              lex_next_error (lexer, -1, -1, "%s", error);
+              free (error);
              goto error;
            }
-          output = fmt_for_output_from_input (&input);
+          output = fmt_for_output_from_input (&input,
+                                              settings_get_fmt_settings ());
         }
       else
         {
-          char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
-          enum fmt_type fmt_type;
-          int w, d;
-
+          int start_ofs = lex_ofs (lexer);
           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
             goto error;
 
           /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR).  */
+          char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
+          uint16_t w;
+          uint8_t d;
           if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
             goto error;
+
+          enum fmt_type fmt_type;
           if (!fmt_from_name (fmt_type_name, &fmt_type))
             {
-              msg (SE, _("Unknown format type `%s'."), fmt_type_name);
+              lex_next_error (lexer, -1, -1,
+                              _("Unknown format type `%s'."), fmt_type_name);
               goto error;
             }
+          int end_ofs = lex_ofs (lexer) - 1;
+
           /* Compose input format. */
-          input.type = fmt_type;
-          input.w = lc - fc + 1;
-          input.d = 0;
-          if (!fmt_check_input (&input))
-            goto error;
+          input = (struct fmt_spec) { .type = fmt_type, .w = lc - fc + 1 };
+          error = fmt_check_input__ (&input);
+          if (error)
+            {
+              lex_next_error (lexer, start_ofs, end_ofs, "%s", error);
+              free (error);
+              goto error;
+            }
+
           /* Compose output format. */
           if (w != 0)
             {
-              output.type = fmt_type;
-              output.w = w;
-              output.d = d;
-              if (!fmt_check_output (&output))
-                goto error;
+              output = (struct fmt_spec) { .type = fmt_type, .w = w, .d = d };
+              error = fmt_check_output__ (&output);
+              if (error)
+                {
+                  lex_next_error (lexer, start_ofs, end_ofs, "%s", error);
+                  free (error);
+                  goto error;
+                }
             }
           else
-            output = fmt_for_output_from_input (&input);
+            output = fmt_for_output_from_input (&input,
+                                                settings_get_fmt_settings ());
         }
       v = dict_create_var (dict, name, fmt_var_width (&input));
       if (v == NULL)
         {
-          msg (SE, _("%s is a duplicate variable name."), name);
+          lex_ofs_error (lexer, name_ofs, name_ofs,
+                         _("%s is a duplicate variable name."), name);
           goto error;
         }
       var_set_both_formats (v, &output);
@@ -673,14 +663,14 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
   if (reader == NULL)
     goto error;
 
-  data_parser_make_active_file (parser, ds, reader, dict);
+  data_parser_make_active_file (parser, ds, reader, dict, NULL, NULL);
   fh_unref (fh);
   free (encoding);
   return CMD_SUCCESS;
 
  error:
   data_parser_destroy (parser);
-  dict_destroy (dict);
+  dict_unref (dict);
   fh_unref (fh);
   free (name);
   free (encoding);
@@ -688,7 +678,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
 }
 
 
-static void 
+static void
 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
 {
   free (opts->cell_range);