GET DATA /TYPE=TXT: Remove obsolete IMPORTCASES subcommand.
[pspp] / src / language / data-io / get-data.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012,
3                  2013, 2015, 2016 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include <stdlib.h>
21
22 #include <string.h>
23
24 #include "data/dataset.h"
25 #include "data/dictionary.h"
26 #include "data/format.h"
27 #include "data/gnumeric-reader.h"
28 #include "data/ods-reader.h"
29 #include "data/spreadsheet-reader.h"
30 #include "data/psql-reader.h"
31 #include "data/settings.h"
32 #include "language/command.h"
33 #include "language/data-io/data-parser.h"
34 #include "language/data-io/data-reader.h"
35 #include "language/data-io/file-handle.h"
36 #include "language/data-io/placement-parser.h"
37 #include "language/lexer/format-parser.h"
38 #include "language/lexer/lexer.h"
39 #include "libpspp/cast.h"
40 #include "libpspp/i18n.h"
41 #include "libpspp/message.h"
42
43 #include "gl/xalloc.h"
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47 #define N_(msgid) (msgid)
48
49
50 #ifdef ODF_READ_SUPPORT
51 static const bool odf_read_support = true;
52 #else
53 static const bool odf_read_support = false;
54 struct spreadsheet *ods_probe (const char *filename, bool report_errors){}
55 struct casereader * ods_make_reader (struct spreadsheet *spreadsheet,
56                                           const struct spreadsheet_read_options *opts){}
57 void ods_unref (struct spreadsheet *r){}
58 #endif
59
60 #ifdef GNM_READ_SUPPORT
61 static const bool gnm_read_support = true;
62 #else
63 static const bool gnm_read_support = false;
64 struct spreadsheet *gnumeric_probe (const char *filename, bool report_errors){}
65 struct casereader * gnumeric_make_reader (struct spreadsheet *spreadsheet,
66                                           const struct spreadsheet_read_options *opts){}
67 void gnumeric_unref (struct spreadsheet *r){}
68
69 #endif
70
71 static bool parse_spreadsheet (struct lexer *lexer, char **filename,
72                                struct spreadsheet_read_options *opts);
73
74 static void destroy_spreadsheet_read_info (struct spreadsheet_read_options *);
75
76 static int parse_get_txt (struct lexer *lexer, struct dataset *);
77 static int parse_get_psql (struct lexer *lexer, struct dataset *);
78
79 int
80 cmd_get_data (struct lexer *lexer, struct dataset *ds)
81 {
82   char *tok = NULL;
83   struct spreadsheet_read_options opts;
84   
85   opts.sheet_name = NULL;
86   opts.sheet_index = -1;
87   opts.cell_range = NULL;
88   opts.read_names = false;
89   opts.asw = -1;
90
91   lex_force_match (lexer, T_SLASH);
92
93   if (!lex_force_match_id (lexer, "TYPE"))
94     goto error;
95
96   lex_force_match (lexer, T_EQUALS);
97
98   tok = strdup (lex_tokcstr (lexer));
99   if (lex_match_id (lexer, "TXT"))
100     {
101       free (tok);
102       return parse_get_txt (lexer, ds);
103     }
104   else if (lex_match_id (lexer, "PSQL"))
105     {
106       free (tok);
107       return parse_get_psql (lexer, ds);
108     }
109   else if (lex_match_id (lexer, "GNM") || 
110       lex_match_id (lexer, "ODS"))
111     {
112       char *filename = NULL;
113       struct casereader *reader = NULL;
114       struct dictionary *dict = NULL;
115
116       if (!parse_spreadsheet (lexer, &filename, &opts))
117         goto error;
118
119       if ( gnm_read_support && 0 == strncasecmp (tok, "GNM", 3))
120         {
121           struct spreadsheet *spreadsheet = gnumeric_probe (filename, true);
122           if (spreadsheet == NULL)
123             goto error;
124           reader = gnumeric_make_reader (spreadsheet, &opts);
125           dict = spreadsheet->dict;
126           gnumeric_unref (spreadsheet);
127         }
128       else if ( odf_read_support && 0 == strncasecmp (tok, "ODS", 3))
129         {
130           struct spreadsheet *spreadsheet = ods_probe (filename, true);
131           if (spreadsheet == NULL)
132             goto error;
133           reader = ods_make_reader (spreadsheet, &opts);
134           dict = spreadsheet->dict;
135           ods_unref (spreadsheet);
136         }
137
138       free (filename);
139
140       if (reader)
141         {
142           dataset_set_dict (ds, dict);
143           dataset_set_source (ds, reader);
144           free (tok);
145           destroy_spreadsheet_read_info (&opts);
146           return CMD_SUCCESS;
147         }
148     }
149   else
150     msg (SE, _("Unsupported TYPE %s."), tok);
151
152
153  error:
154   destroy_spreadsheet_read_info (&opts);
155   free (tok);
156   return CMD_FAILURE;
157 }
158
159 static int
160 parse_get_psql (struct lexer *lexer, struct dataset *ds)
161 {
162   struct psql_read_info psql;
163   psql.allow_clear = false;
164   psql.conninfo = NULL;
165   psql.str_width = -1;
166   psql.bsize = -1;
167   ds_init_empty (&psql.sql);
168
169   lex_force_match (lexer, T_SLASH);
170
171   if (!lex_force_match_id (lexer, "CONNECT"))
172     goto error;
173
174   lex_force_match (lexer, T_EQUALS);
175
176   if (!lex_force_string (lexer))
177     goto error;
178
179   psql.conninfo = ss_xstrdup (lex_tokss (lexer));
180
181   lex_get (lexer);
182
183   while (lex_match (lexer, T_SLASH) )
184     {
185       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
186         {
187           lex_match (lexer, T_EQUALS);
188           psql.str_width = lex_integer (lexer);
189           lex_get (lexer);
190         }
191       else if ( lex_match_id (lexer, "BSIZE"))
192         {
193           lex_match (lexer, T_EQUALS);
194           psql.bsize = lex_integer (lexer);
195           lex_get (lexer);
196         }
197       else if ( lex_match_id (lexer, "UNENCRYPTED"))
198         {
199           psql.allow_clear = true;
200         }
201       else if (lex_match_id (lexer, "SQL"))
202         {
203           lex_match (lexer, T_EQUALS);
204           if ( ! lex_force_string (lexer) )
205             goto error;
206
207           ds_put_substring (&psql.sql, lex_tokss (lexer));
208           lex_get (lexer);
209         }
210      }
211   {
212     struct dictionary *dict = NULL;
213     struct casereader *reader = psql_open_reader (&psql, &dict);
214
215     if ( reader )
216       {
217         dataset_set_dict (ds, dict);
218         dataset_set_source (ds, reader);
219       }
220   }
221
222   ds_destroy (&psql.sql);
223   free (psql.conninfo);
224
225   return CMD_SUCCESS;
226
227  error:
228
229   ds_destroy (&psql.sql);
230   free (psql.conninfo);
231
232   return CMD_FAILURE;
233 }
234
235 static bool
236 parse_spreadsheet (struct lexer *lexer, char **filename, 
237                    struct spreadsheet_read_options *opts)
238 {
239   opts->sheet_index = 1;
240   opts->sheet_name = NULL;
241   opts->cell_range = NULL;
242   opts->read_names = true;
243   opts->asw = -1;
244
245   lex_force_match (lexer, T_SLASH);
246
247   if (!lex_force_match_id (lexer, "FILE"))
248     goto error;
249
250   lex_force_match (lexer, T_EQUALS);
251
252   if (!lex_force_string (lexer))
253     goto error;
254
255   *filename  = utf8_to_filename (lex_tokcstr (lexer));
256
257   lex_get (lexer);
258
259   while (lex_match (lexer, T_SLASH) )
260     {
261       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
262         {
263           lex_match (lexer, T_EQUALS);
264           opts->asw = lex_integer (lexer);
265           lex_get (lexer);
266         }
267       else if (lex_match_id (lexer, "SHEET"))
268         {
269           lex_match (lexer, T_EQUALS);
270           if (lex_match_id (lexer, "NAME"))
271             {
272               if ( ! lex_force_string (lexer) )
273                 goto error;
274
275               opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
276               opts->sheet_index = -1;
277
278               lex_get (lexer);
279             }
280           else if (lex_match_id (lexer, "INDEX"))
281             {
282               opts->sheet_index = lex_integer (lexer);
283               if (opts->sheet_index <= 0)
284                 {
285                   msg (SE, _("The sheet index must be greater than or equal to 1"));
286                   goto error;
287                 }
288               lex_get (lexer);
289             }
290           else
291             {
292               msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
293                    "/SHEET", "NAME", "INDEX");
294               goto error;
295             }
296         }
297       else if (lex_match_id (lexer, "CELLRANGE"))
298         {
299           lex_match (lexer, T_EQUALS);
300
301           if (lex_match_id (lexer, "FULL"))
302             {
303               opts->cell_range = NULL;
304             }
305           else if (lex_match_id (lexer, "RANGE"))
306             {
307               if ( ! lex_force_string (lexer) )
308                 goto error;
309
310               opts->cell_range = ss_xstrdup (lex_tokss (lexer));
311               lex_get (lexer);
312             }
313           else
314             {
315               msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
316                    "/CELLRANGE", "FULL", "RANGE");
317               goto error;
318             }
319         }
320       else if (lex_match_id (lexer, "READNAMES"))
321         {
322           lex_match (lexer, T_EQUALS);
323
324           if ( lex_match_id (lexer, "ON"))
325             {
326               opts->read_names = true;
327             }
328           else if (lex_match_id (lexer, "OFF"))
329             {
330               opts->read_names = false;
331             }
332           else
333             {
334               msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
335                    "/READNAMES", "ON", "OFF");
336               goto error;
337             }
338         }
339       else
340         {
341           lex_error (lexer, NULL);
342           goto error;
343         }
344     }
345
346   return true;
347
348  error:
349   return false;
350 }
351
352
353 static bool
354 set_type (struct data_parser *parser, const char *subcommand,
355           enum data_parser_type type, bool *has_type)
356 {
357   if (!*has_type)
358     {
359       data_parser_set_type (parser, type);
360       *has_type = true;
361     }
362   else if (type != data_parser_get_type (parser))
363     {
364       msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
365                  "was stated or implied earlier in this command."),
366            subcommand,
367            type == DP_FIXED ? "FIXED" : "DELIMITED",
368            type == DP_FIXED ? "DELIMITED" : "FIXED");
369       return false;
370     }
371   return true;
372 }
373
374 static int
375 parse_get_txt (struct lexer *lexer, struct dataset *ds)
376 {
377   struct data_parser *parser = NULL;
378   struct dictionary *dict = dict_create (get_default_encoding ());
379   struct file_handle *fh = NULL;
380   struct dfm_reader *reader = NULL;
381   char *encoding = NULL;
382   char *name = NULL;
383
384   int record;
385   enum data_parser_type type;
386   bool has_type;
387
388   lex_force_match (lexer, T_SLASH);
389
390   if (!lex_force_match_id (lexer, "FILE"))
391     goto error;
392   lex_force_match (lexer, T_EQUALS);
393   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
394   if (fh == NULL)
395     goto error;
396
397   parser = data_parser_create (dict);
398   has_type = false;
399   data_parser_set_type (parser, DP_DELIMITED);
400   data_parser_set_span (parser, false);
401   data_parser_set_quotes (parser, ss_empty ());
402   data_parser_set_quote_escape (parser, true);
403   data_parser_set_empty_line_has_field (parser, true);
404
405   for (;;)
406     {
407       if (!lex_force_match (lexer, T_SLASH))
408         goto error;
409
410       if (lex_match_id (lexer, "ENCODING"))
411         {
412           lex_match (lexer, T_EQUALS);
413           if (!lex_force_string (lexer))
414             goto error;
415
416           free (encoding);
417           encoding = ss_xstrdup (lex_tokss (lexer));
418
419           lex_get (lexer);
420         }
421       else if (lex_match_id (lexer, "ARRANGEMENT"))
422         {
423           bool ok;
424
425           lex_match (lexer, T_EQUALS);
426           if (lex_match_id (lexer, "FIXED"))
427             ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
428           else if (lex_match_id (lexer, "DELIMITED"))
429             ok = set_type (parser, "ARRANGEMENT=DELIMITED",
430                            DP_DELIMITED, &has_type);
431           else
432             {
433               lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
434               goto error;
435             }
436           if (!ok)
437             goto error;
438         }
439       else if (lex_match_id (lexer, "FIRSTCASE"))
440         {
441           lex_match (lexer, T_EQUALS);
442           if (!lex_force_int (lexer))
443             goto error;
444           if (lex_integer (lexer) < 1)
445             {
446               msg (SE, _("Value of %s must be 1 or greater."), "FIRSTCASE");
447               goto error;
448             }
449           data_parser_set_skip (parser, lex_integer (lexer) - 1);
450           lex_get (lexer);
451         }
452       else if (lex_match_id_n (lexer, "DELCASE", 4))
453         {
454           if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
455             goto error;
456           lex_match (lexer, T_EQUALS);
457           if (lex_match_id (lexer, "LINE"))
458             data_parser_set_span (parser, false);
459           else if (lex_match_id (lexer, "VARIABLES"))
460             {
461               data_parser_set_span (parser, true);
462
463               /* VARIABLES takes an integer argument, but for no
464                  good reason.  We just ignore it. */
465               if (!lex_force_int (lexer))
466                 goto error;
467               lex_get (lexer);
468             }
469           else
470             {
471               lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
472               goto error;
473             }
474         }
475       else if (lex_match_id (lexer, "FIXCASE"))
476         {
477           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
478             goto error;
479           lex_match (lexer, T_EQUALS);
480           if (!lex_force_int (lexer))
481             goto error;
482           if (lex_integer (lexer) < 1)
483             {
484               msg (SE, _("Value of %s must be 1 or greater."), "FIXCASE");
485               goto error;
486             }
487           data_parser_set_records (parser, lex_integer (lexer));
488           lex_get (lexer);
489         }
490       else if (lex_match_id (lexer, "IMPORTCASES"))
491         {
492           lex_match (lexer, T_EQUALS);
493           if (lex_match (lexer, T_ALL))
494             {
495               /* Nothing to do. */
496             }
497           else if (lex_match_id (lexer, "FIRST"))
498             {
499               if (!lex_force_int (lexer))
500                 goto error;
501               lex_get (lexer);
502             }
503           else if (lex_match_id (lexer, "PERCENT"))
504             {
505               if (!lex_force_int (lexer))
506                 goto error;
507               lex_get (lexer);
508             }
509           msg (SW, _("Ignoring obsolete IMPORTCASES subcommand.  (N OF CASES "
510                      "or SAMPLE may be used to substitute.)"));
511         }
512       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
513         {
514           struct string hard_seps = DS_EMPTY_INITIALIZER;
515           const char *soft_seps = "";
516           struct substring s;
517           int c;
518
519           if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
520             goto error;
521           lex_match (lexer, T_EQUALS);
522
523           if (!lex_force_string (lexer))
524             goto error;
525
526           /* XXX should support multibyte UTF-8 characters */
527           s = lex_tokss (lexer);
528           if (ss_match_string (&s, ss_cstr ("\\t")))
529             ds_put_cstr (&hard_seps, "\t");
530           if (ss_match_string (&s, ss_cstr ("\\\\")))
531             ds_put_cstr (&hard_seps, "\\");
532           while ((c = ss_get_byte (&s)) != EOF)
533             if (c == ' ')
534               soft_seps = " ";
535             else
536               ds_put_byte (&hard_seps, c);
537           data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
538           data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
539           ds_destroy (&hard_seps);
540
541           lex_get (lexer);
542         }
543       else if (lex_match_id (lexer, "QUALIFIERS"))
544         {
545           if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
546             goto error;
547           lex_match (lexer, T_EQUALS);
548
549           if (!lex_force_string (lexer))
550             goto error;
551
552           /* XXX should support multibyte UTF-8 characters */
553           if (settings_get_syntax () == COMPATIBLE
554               && ss_length (lex_tokss (lexer)) != 1)
555             {
556               msg (SE, _("In compatible syntax mode, the QUALIFIER string "
557                          "must contain exactly one character."));
558               goto error;
559             }
560
561           data_parser_set_quotes (parser, lex_tokss (lexer));
562           lex_get (lexer);
563         }
564       else if (lex_match_id (lexer, "VARIABLES"))
565         break;
566       else
567         {
568           lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
569           goto error;
570         }
571     }
572   lex_match (lexer, T_EQUALS);
573
574   record = 1;
575   type = data_parser_get_type (parser);
576   do
577     {
578       struct fmt_spec input, output;
579       struct variable *v;
580       int fc, lc;
581
582       while (type == DP_FIXED && lex_match (lexer, T_SLASH))
583         {
584           if (!lex_force_int (lexer))
585             goto error;
586           if (lex_integer (lexer) < record)
587             {
588               msg (SE, _("The record number specified, %ld, is at or "
589                          "before the previous record, %d.  Data "
590                          "fields must be listed in order of "
591                          "increasing record number."),
592                    lex_integer (lexer), record);
593               goto error;
594             }
595           if (lex_integer (lexer) > data_parser_get_records (parser))
596             {
597               msg (SE, _("The record number specified, %ld, exceeds "
598                          "the number of records per case specified "
599                          "on FIXCASE, %d."),
600                    lex_integer (lexer), data_parser_get_records (parser));
601               goto error;
602             }
603           record = lex_integer (lexer);
604           lex_get (lexer);
605         }
606
607       if (!lex_force_id (lexer)
608           || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
609         goto error;
610       name = xstrdup (lex_tokcstr (lexer));
611       lex_get (lexer);
612
613       if (type == DP_DELIMITED)
614         {
615           if (!parse_format_specifier (lexer, &input)
616               || !fmt_check_input (&input))
617             goto error;
618
619           output = fmt_for_output_from_input (&input);
620         }
621       else
622         {
623           char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
624           enum fmt_type fmt_type;
625           int w, d;
626
627           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
628             goto error;
629
630           /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR).  */
631           if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
632             goto error;
633           if (!fmt_from_name (fmt_type_name, &fmt_type))
634             {
635               msg (SE, _("Unknown format type `%s'."), fmt_type_name);
636               goto error;
637             }
638
639           /* Compose input format. */
640           input.type = fmt_type;
641           input.w = lc - fc + 1;
642           input.d = 0;
643           if (!fmt_check_input (&input))
644             goto error;
645
646           /* Compose output format. */
647           if (w != 0)
648             {
649               output.type = fmt_type;
650               output.w = w;
651               output.d = d;
652               if (!fmt_check_output (&output))
653                 goto error;
654             }
655           else
656             output = fmt_for_output_from_input (&input);
657         }
658
659       v = dict_create_var (dict, name, fmt_var_width (&input));
660       if (v == NULL)
661         {
662           msg (SE, _("%s is a duplicate variable name."), name);
663           goto error;
664         }
665       var_set_both_formats (v, &output);
666
667       if (type == DP_DELIMITED)
668         data_parser_add_delimited_field (parser, &input,
669                                          var_get_case_index (v),
670                                          name);
671       else
672         data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
673                                      name, record, fc);
674       free (name);
675       name = NULL;
676     }
677   while (lex_token (lexer) != T_ENDCMD);
678
679   reader = dfm_open_reader (fh, lexer, encoding);
680   if (reader == NULL)
681     goto error;
682
683   data_parser_make_active_file (parser, ds, reader, dict);
684   fh_unref (fh);
685   free (encoding);
686   return CMD_SUCCESS;
687
688  error:
689   data_parser_destroy (parser);
690   dict_destroy (dict);
691   fh_unref (fh);
692   free (name);
693   free (encoding);
694   return CMD_CASCADING_FAILURE;
695 }
696
697
698 static void 
699 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
700 {
701   free (opts->cell_range);
702   free (opts->sheet_name);
703 }