GET DATA /TYPE=TXT: Get rid of VAR_NAME_LEN limit on variable names.
[pspp-builds.git] / src / language / data-io / get-data.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include <data/gnumeric-reader.h>
22 #include <data/psql-reader.h>
23
24 #include <data/dictionary.h>
25 #include <data/format.h>
26 #include <data/procedure.h>
27 #include <data/settings.h>
28 #include <language/command.h>
29 #include <language/data-io/data-parser.h>
30 #include <language/data-io/data-reader.h>
31 #include <language/data-io/file-handle.h>
32 #include <language/data-io/placement-parser.h>
33 #include <language/lexer/format-parser.h>
34 #include <language/lexer/lexer.h>
35 #include <libpspp/message.h>
36
37 #include "xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) (msgid)
42
43 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
44 static int parse_get_txt (struct lexer *lexer, struct dataset *);
45 static int parse_get_psql (struct lexer *lexer, struct dataset *);
46
47 int
48 cmd_get_data (struct lexer *lexer, struct dataset *ds)
49 {
50   lex_force_match (lexer, T_SLASH);
51
52   if (!lex_force_match_id (lexer, "TYPE"))
53     return CMD_FAILURE;
54
55   lex_force_match (lexer, T_EQUALS);
56
57   if (lex_match_id (lexer, "GNM"))
58     return parse_get_gnm (lexer, ds);
59   else if (lex_match_id (lexer, "TXT"))
60     return parse_get_txt (lexer, ds);
61   else if (lex_match_id (lexer, "PSQL"))
62     return parse_get_psql (lexer, ds);
63
64   msg (SE, _("Unsupported TYPE %s."), lex_tokcstr (lexer));
65   return CMD_FAILURE;
66 }
67
68 static int
69 parse_get_psql (struct lexer *lexer, struct dataset *ds)
70 {
71   struct psql_read_info psql;
72   psql.allow_clear = false;
73   psql.conninfo = NULL;
74   psql.str_width = -1;
75   psql.bsize = -1;
76   ds_init_empty (&psql.sql);
77
78   lex_force_match (lexer, T_SLASH);
79
80   if (!lex_force_match_id (lexer, "CONNECT"))
81     goto error;
82
83   lex_force_match (lexer, T_EQUALS);
84
85   if (!lex_force_string (lexer))
86     goto error;
87
88   psql.conninfo = ss_xstrdup (lex_tokss (lexer));
89
90   lex_get (lexer);
91
92   while (lex_match (lexer, T_SLASH) )
93     {
94       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
95         {
96           lex_match (lexer, T_EQUALS);
97           psql.str_width = lex_integer (lexer);
98           lex_get (lexer);
99         }
100       else if ( lex_match_id (lexer, "BSIZE"))
101         {
102           lex_match (lexer, T_EQUALS);
103           psql.bsize = lex_integer (lexer);
104           lex_get (lexer);
105         }
106       else if ( lex_match_id (lexer, "UNENCRYPTED"))
107         {
108           psql.allow_clear = true;
109         }
110       else if (lex_match_id (lexer, "SQL"))
111         {
112           lex_match (lexer, T_EQUALS);
113           if ( ! lex_force_string (lexer) )
114             goto error;
115
116           ds_put_substring (&psql.sql, lex_tokss (lexer));
117           lex_get (lexer);
118         }
119      }
120   {
121     struct dictionary *dict = NULL;
122     struct casereader *reader = psql_open_reader (&psql, &dict);
123
124     if ( reader )
125       proc_set_active_file (ds, reader, dict);
126   }
127
128   ds_destroy (&psql.sql);
129   free (psql.conninfo);
130
131   return CMD_SUCCESS;
132
133  error:
134
135   ds_destroy (&psql.sql);
136   free (psql.conninfo);
137
138   return CMD_FAILURE;
139 }
140
141 static int
142 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
143 {
144   struct gnumeric_read_info gri  = {NULL, NULL, NULL, 1, true, -1};
145
146   lex_force_match (lexer, T_SLASH);
147
148   if (!lex_force_match_id (lexer, "FILE"))
149     goto error;
150
151   lex_force_match (lexer, T_EQUALS);
152
153   if (!lex_force_string (lexer))
154     goto error;
155
156   gri.file_name = ss_xstrdup (lex_tokss (lexer));
157
158   lex_get (lexer);
159
160   while (lex_match (lexer, T_SLASH) )
161     {
162       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
163         {
164           lex_match (lexer, T_EQUALS);
165           gri.asw = lex_integer (lexer);
166         }
167       else if (lex_match_id (lexer, "SHEET"))
168         {
169           lex_match (lexer, T_EQUALS);
170           if (lex_match_id (lexer, "NAME"))
171             {
172               if ( ! lex_force_string (lexer) )
173                 goto error;
174
175               gri.sheet_name = ss_xstrdup (lex_tokss (lexer));
176               gri.sheet_index = -1;
177             }
178           else if (lex_match_id (lexer, "INDEX"))
179             {
180               gri.sheet_index = lex_integer (lexer);
181             }
182           else
183             goto error;
184         }
185       else if (lex_match_id (lexer, "CELLRANGE"))
186         {
187           lex_match (lexer, T_EQUALS);
188
189           if (lex_match_id (lexer, "FULL"))
190             {
191               gri.cell_range = NULL;
192               lex_put_back (lexer, T_ID);
193             }
194           else if (lex_match_id (lexer, "RANGE"))
195             {
196               if ( ! lex_force_string (lexer) )
197                 goto error;
198
199               gri.cell_range = ss_xstrdup (lex_tokss (lexer));
200             }
201           else
202             goto error;
203         }
204       else if (lex_match_id (lexer, "READNAMES"))
205         {
206           lex_match (lexer, T_EQUALS);
207
208           if ( lex_match_id (lexer, "ON"))
209             {
210               gri.read_names = true;
211             }
212           else if (lex_match_id (lexer, "OFF"))
213             {
214               gri.read_names = false;
215             }
216           else
217             goto error;
218           lex_put_back (lexer, T_ID);
219         }
220       else
221         {
222           printf ("Unknown data file type `%s'\n", lex_tokcstr (lexer));
223           goto error;
224         }
225       lex_get (lexer);
226     }
227
228   {
229     struct dictionary *dict = NULL;
230     struct casereader *reader = gnumeric_open_reader (&gri, &dict);
231
232     if ( reader )
233       proc_set_active_file (ds, reader, dict);
234   }
235
236   free (gri.file_name);
237   free (gri.sheet_name);
238   free (gri.cell_range);
239   return CMD_SUCCESS;
240
241  error:
242
243   free (gri.file_name);
244   free (gri.sheet_name);
245   free (gri.cell_range);
246   return CMD_FAILURE;
247 }
248
249 static bool
250 set_type (struct data_parser *parser, const char *subcommand,
251           enum data_parser_type type, bool *has_type)
252 {
253   if (!*has_type)
254     {
255       data_parser_set_type (parser, type);
256       *has_type = true;
257     }
258   else if (type != data_parser_get_type (parser))
259     {
260       msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
261                  "was stated or implied earlier in this command."),
262            subcommand,
263            type == DP_FIXED ? "FIXED" : "DELIMITED",
264            type == DP_FIXED ? "DELIMITED" : "FIXED");
265       return false;
266     }
267   return true;
268 }
269
270 static int
271 parse_get_txt (struct lexer *lexer, struct dataset *ds)
272 {
273   struct data_parser *parser = NULL;
274   struct dictionary *dict = dict_create ();
275   struct file_handle *fh = NULL;
276   struct dfm_reader *reader = NULL;
277   char *name = NULL;
278
279   int record;
280   enum data_parser_type type;
281   bool has_type;
282
283   lex_force_match (lexer, T_SLASH);
284
285   if (!lex_force_match_id (lexer, "FILE"))
286     goto error;
287   lex_force_match (lexer, T_EQUALS);
288   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
289   if (fh == NULL)
290     goto error;
291
292   parser = data_parser_create (dict);
293   has_type = false;
294   data_parser_set_type (parser, DP_DELIMITED);
295   data_parser_set_span (parser, false);
296   data_parser_set_quotes (parser, ss_empty ());
297   data_parser_set_empty_line_has_field (parser, true);
298
299   for (;;)
300     {
301       if (!lex_force_match (lexer, T_SLASH))
302         goto error;
303
304       if (lex_match_id (lexer, "ARRANGEMENT"))
305         {
306           bool ok;
307
308           lex_match (lexer, T_EQUALS);
309           if (lex_match_id (lexer, "FIXED"))
310             ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
311           else if (lex_match_id (lexer, "DELIMITED"))
312             ok = set_type (parser, "ARRANGEMENT=DELIMITED",
313                            DP_DELIMITED, &has_type);
314           else
315             {
316               lex_error (lexer, _("expecting %s or %s"), "FIXED", "DELIMITED");
317               goto error;
318             }
319           if (!ok)
320             goto error;
321         }
322       else if (lex_match_id (lexer, "FIRSTCASE"))
323         {
324           lex_match (lexer, T_EQUALS);
325           if (!lex_force_int (lexer))
326             goto error;
327           if (lex_integer (lexer) < 1)
328             {
329               msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
330               goto error;
331             }
332           data_parser_set_skip (parser, lex_integer (lexer) - 1);
333           lex_get (lexer);
334         }
335       else if (lex_match_id_n (lexer, "DELCASE", 4))
336         {
337           if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
338             goto error;
339           lex_match (lexer, T_EQUALS);
340           if (lex_match_id (lexer, "LINE"))
341             data_parser_set_span (parser, false);
342           else if (lex_match_id (lexer, "VARIABLES"))
343             {
344               data_parser_set_span (parser, true);
345
346               /* VARIABLES takes an integer argument, but for no
347                  good reason.  We just ignore it. */
348               if (!lex_force_int (lexer))
349                 goto error;
350               lex_get (lexer);
351             }
352           else
353             {
354               lex_error (lexer, _("expecting %s or %s"), "LINE", "VARIABLES");
355               goto error;
356             }
357         }
358       else if (lex_match_id (lexer, "FIXCASE"))
359         {
360           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
361             goto error;
362           lex_match (lexer, T_EQUALS);
363           if (!lex_force_int (lexer))
364             goto error;
365           if (lex_integer (lexer) < 1)
366             {
367               msg (SE, _("Value of FIXCASE must be at least 1."));
368               goto error;
369             }
370           data_parser_set_records (parser, lex_integer (lexer));
371           lex_get (lexer);
372         }
373       else if (lex_match_id (lexer, "IMPORTCASES"))
374         {
375           lex_match (lexer, T_EQUALS);
376           if (lex_match (lexer, T_ALL))
377             {
378               data_parser_set_case_limit (parser, -1);
379               data_parser_set_case_percent (parser, 100);
380             }
381           else if (lex_match_id (lexer, "FIRST"))
382             {
383               if (!lex_force_int (lexer))
384                 goto error;
385               if (lex_integer (lexer) < 1)
386                 {
387                   msg (SE, _("Value of FIRST must be at least 1."));
388                   goto error;
389                 }
390               data_parser_set_case_limit (parser, lex_integer (lexer));
391               lex_get (lexer);
392             }
393           else if (lex_match_id (lexer, "PERCENT"))
394             {
395               if (!lex_force_int (lexer))
396                 goto error;
397               if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
398                 {
399                   msg (SE, _("Value of PERCENT must be between 1 and 100."));
400                   goto error;
401                 }
402               data_parser_set_case_percent (parser, lex_integer (lexer));
403               lex_get (lexer);
404             }
405         }
406       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
407         {
408           struct string hard_seps = DS_EMPTY_INITIALIZER;
409           const char *soft_seps = "";
410           struct substring s;
411           int c;
412
413           if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
414             goto error;
415           lex_match (lexer, T_EQUALS);
416
417           if (!lex_force_string (lexer))
418             goto error;
419
420           s = lex_tokss (lexer);
421           if (ss_match_string (&s, ss_cstr ("\\t")))
422             ds_put_cstr (&hard_seps, "\t");
423           if (ss_match_string (&s, ss_cstr ("\\\\")))
424             ds_put_cstr (&hard_seps, "\\");
425           while ((c = ss_get_byte (&s)) != EOF)
426             if (c == ' ')
427               soft_seps = " ";
428             else
429               ds_put_byte (&hard_seps, c);
430           data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
431           data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
432           ds_destroy (&hard_seps);
433
434           lex_get (lexer);
435         }
436       else if (lex_match_id (lexer, "QUALIFIERS"))
437         {
438           if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
439             goto error;
440           lex_match (lexer, T_EQUALS);
441
442           if (!lex_force_string (lexer))
443             goto error;
444
445           if (settings_get_syntax () == COMPATIBLE
446               && ss_length (lex_tokss (lexer)) != 1)
447             {
448               msg (SE, _("In compatible syntax mode, the QUALIFIER string "
449                          "must contain exactly one character."));
450               goto error;
451             }
452
453           data_parser_set_quotes (parser, lex_tokss (lexer));
454           lex_get (lexer);
455         }
456       else if (settings_get_syntax () == ENHANCED
457                && lex_match_id (lexer, "ESCAPE"))
458         data_parser_set_quote_escape (parser, true);
459       else if (lex_match_id (lexer, "VARIABLES"))
460         break;
461       else
462         {
463           lex_error (lexer, _("expecting %s"), "VARIABLES");
464           goto error;
465         }
466     }
467   lex_match (lexer, T_EQUALS);
468
469   record = 1;
470   type = data_parser_get_type (parser);
471   do
472     {
473       struct fmt_spec input, output;
474       struct variable *v;
475       int fc, lc;
476
477       while (type == DP_FIXED && lex_match (lexer, T_SLASH))
478         {
479           if (!lex_force_int (lexer))
480             goto error;
481           if (lex_integer (lexer) < record)
482             {
483               msg (SE, _("The record number specified, %ld, is at or "
484                          "before the previous record, %d.  Data "
485                          "fields must be listed in order of "
486                          "increasing record number."),
487                    lex_integer (lexer), record);
488               goto error;
489             }
490           if (lex_integer (lexer) > data_parser_get_records (parser))
491             {
492               msg (SE, _("The record number specified, %ld, exceeds "
493                          "the number of records per case specified "
494                          "on FIXCASE, %d."),
495                    lex_integer (lexer), data_parser_get_records (parser));
496               goto error;
497             }
498           record = lex_integer (lexer);
499           lex_get (lexer);
500         }
501
502       if (!lex_force_id (lexer))
503         goto error;
504       name = xstrdup (lex_tokcstr (lexer));
505       lex_get (lexer);
506
507       if (type == DP_DELIMITED)
508         {
509           if (!parse_format_specifier (lexer, &input)
510               || !fmt_check_input (&input))
511             goto error;
512         }
513       else
514         {
515           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
516             goto error;
517           if (!parse_format_specifier_name (lexer, &input.type))
518             goto error;
519           input.w = lc - fc + 1;
520           input.d = 0;
521           if (!fmt_check_input (&input))
522             goto error;
523         }
524       output = fmt_for_output_from_input (&input);
525
526       v = dict_create_var (dict, name, fmt_var_width (&input));
527       if (v == NULL)
528         {
529           msg (SE, _("%s is a duplicate variable name."), name);
530           goto error;
531         }
532       var_set_both_formats (v, &output);
533
534       if (type == DP_DELIMITED)
535         data_parser_add_delimited_field (parser, &input,
536                                          var_get_case_index (v),
537                                          name);
538       else
539         data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
540                                      name, record, fc);
541       free (name);
542       name = NULL;
543     }
544   while (lex_token (lexer) != T_ENDCMD);
545
546   reader = dfm_open_reader (fh, lexer);
547   if (reader == NULL)
548     goto error;
549
550   data_parser_make_active_file (parser, ds, reader, dict);
551   fh_unref (fh);
552   return CMD_SUCCESS;
553
554  error:
555   data_parser_destroy (parser);
556   dict_destroy (dict);
557   fh_unref (fh);
558   free (name);
559   return CMD_CASCADING_FAILURE;
560 }