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