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