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