1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
21 #include "data/dictionary.h"
22 #include "data/format.h"
23 #include "data/gnumeric-reader.h"
24 #include "data/procedure.h"
25 #include "data/psql-reader.h"
26 #include "data/settings.h"
27 #include "language/command.h"
28 #include "language/data-io/data-parser.h"
29 #include "language/data-io/data-reader.h"
30 #include "language/data-io/file-handle.h"
31 #include "language/data-io/placement-parser.h"
32 #include "language/lexer/format-parser.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/message.h"
36 #include "gl/xalloc.h"
39 #define _(msgid) gettext (msgid)
40 #define N_(msgid) (msgid)
42 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
43 static int parse_get_txt (struct lexer *lexer, struct dataset *);
44 static int parse_get_psql (struct lexer *lexer, struct dataset *);
47 cmd_get_data (struct lexer *lexer, struct dataset *ds)
49 lex_force_match (lexer, T_SLASH);
51 if (!lex_force_match_id (lexer, "TYPE"))
54 lex_force_match (lexer, T_EQUALS);
56 if (lex_match_id (lexer, "GNM"))
57 return parse_get_gnm (lexer, ds);
58 else if (lex_match_id (lexer, "TXT"))
59 return parse_get_txt (lexer, ds);
60 else if (lex_match_id (lexer, "PSQL"))
61 return parse_get_psql (lexer, ds);
63 msg (SE, _("Unsupported TYPE %s."), lex_tokcstr (lexer));
68 parse_get_psql (struct lexer *lexer, struct dataset *ds)
70 struct psql_read_info psql;
71 psql.allow_clear = false;
75 ds_init_empty (&psql.sql);
77 lex_force_match (lexer, T_SLASH);
79 if (!lex_force_match_id (lexer, "CONNECT"))
82 lex_force_match (lexer, T_EQUALS);
84 if (!lex_force_string (lexer))
87 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
91 while (lex_match (lexer, T_SLASH) )
93 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
95 lex_match (lexer, T_EQUALS);
96 psql.str_width = lex_integer (lexer);
99 else if ( lex_match_id (lexer, "BSIZE"))
101 lex_match (lexer, T_EQUALS);
102 psql.bsize = lex_integer (lexer);
105 else if ( lex_match_id (lexer, "UNENCRYPTED"))
107 psql.allow_clear = true;
109 else if (lex_match_id (lexer, "SQL"))
111 lex_match (lexer, T_EQUALS);
112 if ( ! lex_force_string (lexer) )
115 ds_put_substring (&psql.sql, lex_tokss (lexer));
120 struct dictionary *dict = NULL;
121 struct casereader *reader = psql_open_reader (&psql, &dict);
124 proc_set_active_file (ds, reader, dict);
127 ds_destroy (&psql.sql);
128 free (psql.conninfo);
134 ds_destroy (&psql.sql);
135 free (psql.conninfo);
141 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
143 struct gnumeric_read_info gri = {NULL, NULL, NULL, 1, true, -1};
145 lex_force_match (lexer, T_SLASH);
147 if (!lex_force_match_id (lexer, "FILE"))
150 lex_force_match (lexer, T_EQUALS);
152 if (!lex_force_string (lexer))
155 gri.file_name = ss_xstrdup (lex_tokss (lexer));
159 while (lex_match (lexer, T_SLASH) )
161 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
163 lex_match (lexer, T_EQUALS);
164 gri.asw = lex_integer (lexer);
167 else if (lex_match_id (lexer, "SHEET"))
169 lex_match (lexer, T_EQUALS);
170 if (lex_match_id (lexer, "NAME"))
172 if ( ! lex_force_string (lexer) )
175 gri.sheet_name = ss_xstrdup (lex_tokss (lexer));
176 gri.sheet_index = -1;
180 else if (lex_match_id (lexer, "INDEX"))
182 gri.sheet_index = lex_integer (lexer);
188 else if (lex_match_id (lexer, "CELLRANGE"))
190 lex_match (lexer, T_EQUALS);
192 if (lex_match_id (lexer, "FULL"))
194 gri.cell_range = NULL;
196 else if (lex_match_id (lexer, "RANGE"))
198 if ( ! lex_force_string (lexer) )
201 gri.cell_range = ss_xstrdup (lex_tokss (lexer));
207 else if (lex_match_id (lexer, "READNAMES"))
209 lex_match (lexer, T_EQUALS);
211 if ( lex_match_id (lexer, "ON"))
213 gri.read_names = true;
215 else if (lex_match_id (lexer, "OFF"))
217 gri.read_names = false;
224 lex_error (lexer, NULL);
230 struct dictionary *dict = NULL;
231 struct casereader *reader = gnumeric_open_reader (&gri, &dict);
234 proc_set_active_file (ds, reader, dict);
237 free (gri.file_name);
238 free (gri.sheet_name);
239 free (gri.cell_range);
244 free (gri.file_name);
245 free (gri.sheet_name);
246 free (gri.cell_range);
251 set_type (struct data_parser *parser, const char *subcommand,
252 enum data_parser_type type, bool *has_type)
256 data_parser_set_type (parser, type);
259 else if (type != data_parser_get_type (parser))
261 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
262 "was stated or implied earlier in this command."),
264 type == DP_FIXED ? "FIXED" : "DELIMITED",
265 type == DP_FIXED ? "DELIMITED" : "FIXED");
272 parse_get_txt (struct lexer *lexer, struct dataset *ds)
274 struct data_parser *parser = NULL;
275 struct dictionary *dict = dict_create ();
276 struct file_handle *fh = NULL;
277 struct dfm_reader *reader = NULL;
281 enum data_parser_type type;
284 lex_force_match (lexer, T_SLASH);
286 if (!lex_force_match_id (lexer, "FILE"))
288 lex_force_match (lexer, T_EQUALS);
289 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
293 parser = data_parser_create (dict);
295 data_parser_set_type (parser, DP_DELIMITED);
296 data_parser_set_span (parser, false);
297 data_parser_set_quotes (parser, ss_empty ());
298 data_parser_set_empty_line_has_field (parser, true);
302 if (!lex_force_match (lexer, T_SLASH))
305 if (lex_match_id (lexer, "ARRANGEMENT"))
309 lex_match (lexer, T_EQUALS);
310 if (lex_match_id (lexer, "FIXED"))
311 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
312 else if (lex_match_id (lexer, "DELIMITED"))
313 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
314 DP_DELIMITED, &has_type);
317 lex_error (lexer, _("expecting %s or %s"), "FIXED", "DELIMITED");
323 else if (lex_match_id (lexer, "FIRSTCASE"))
325 lex_match (lexer, T_EQUALS);
326 if (!lex_force_int (lexer))
328 if (lex_integer (lexer) < 1)
330 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
333 data_parser_set_skip (parser, lex_integer (lexer) - 1);
336 else if (lex_match_id_n (lexer, "DELCASE", 4))
338 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
340 lex_match (lexer, T_EQUALS);
341 if (lex_match_id (lexer, "LINE"))
342 data_parser_set_span (parser, false);
343 else if (lex_match_id (lexer, "VARIABLES"))
345 data_parser_set_span (parser, true);
347 /* VARIABLES takes an integer argument, but for no
348 good reason. We just ignore it. */
349 if (!lex_force_int (lexer))
355 lex_error (lexer, _("expecting %s or %s"), "LINE", "VARIABLES");
359 else if (lex_match_id (lexer, "FIXCASE"))
361 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
363 lex_match (lexer, T_EQUALS);
364 if (!lex_force_int (lexer))
366 if (lex_integer (lexer) < 1)
368 msg (SE, _("Value of FIXCASE must be at least 1."));
371 data_parser_set_records (parser, lex_integer (lexer));
374 else if (lex_match_id (lexer, "IMPORTCASES"))
376 lex_match (lexer, T_EQUALS);
377 if (lex_match (lexer, T_ALL))
379 data_parser_set_case_limit (parser, -1);
380 data_parser_set_case_percent (parser, 100);
382 else if (lex_match_id (lexer, "FIRST"))
384 if (!lex_force_int (lexer))
386 if (lex_integer (lexer) < 1)
388 msg (SE, _("Value of FIRST must be at least 1."));
391 data_parser_set_case_limit (parser, lex_integer (lexer));
394 else if (lex_match_id (lexer, "PERCENT"))
396 if (!lex_force_int (lexer))
398 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
400 msg (SE, _("Value of PERCENT must be between 1 and 100."));
403 data_parser_set_case_percent (parser, lex_integer (lexer));
407 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
409 struct string hard_seps = DS_EMPTY_INITIALIZER;
410 const char *soft_seps = "";
414 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
416 lex_match (lexer, T_EQUALS);
418 if (!lex_force_string (lexer))
421 s = lex_tokss (lexer);
422 if (ss_match_string (&s, ss_cstr ("\\t")))
423 ds_put_cstr (&hard_seps, "\t");
424 if (ss_match_string (&s, ss_cstr ("\\\\")))
425 ds_put_cstr (&hard_seps, "\\");
426 while ((c = ss_get_byte (&s)) != EOF)
430 ds_put_byte (&hard_seps, c);
431 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
432 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
433 ds_destroy (&hard_seps);
437 else if (lex_match_id (lexer, "QUALIFIERS"))
439 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
441 lex_match (lexer, T_EQUALS);
443 if (!lex_force_string (lexer))
446 if (settings_get_syntax () == COMPATIBLE
447 && ss_length (lex_tokss (lexer)) != 1)
449 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
450 "must contain exactly one character."));
454 data_parser_set_quotes (parser, lex_tokss (lexer));
457 else if (settings_get_syntax () == ENHANCED
458 && lex_match_id (lexer, "ESCAPE"))
459 data_parser_set_quote_escape (parser, true);
460 else if (lex_match_id (lexer, "VARIABLES"))
464 lex_error (lexer, _("expecting %s"), "VARIABLES");
468 lex_match (lexer, T_EQUALS);
471 type = data_parser_get_type (parser);
474 struct fmt_spec input, output;
478 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
480 if (!lex_force_int (lexer))
482 if (lex_integer (lexer) < record)
484 msg (SE, _("The record number specified, %ld, is at or "
485 "before the previous record, %d. Data "
486 "fields must be listed in order of "
487 "increasing record number."),
488 lex_integer (lexer), record);
491 if (lex_integer (lexer) > data_parser_get_records (parser))
493 msg (SE, _("The record number specified, %ld, exceeds "
494 "the number of records per case specified "
496 lex_integer (lexer), data_parser_get_records (parser));
499 record = lex_integer (lexer);
503 if (!lex_force_id (lexer))
505 name = xstrdup (lex_tokcstr (lexer));
508 if (type == DP_DELIMITED)
510 if (!parse_format_specifier (lexer, &input)
511 || !fmt_check_input (&input))
516 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
518 if (!parse_format_specifier_name (lexer, &input.type))
520 input.w = lc - fc + 1;
522 if (!fmt_check_input (&input))
525 output = fmt_for_output_from_input (&input);
527 v = dict_create_var (dict, name, fmt_var_width (&input));
530 msg (SE, _("%s is a duplicate variable name."), name);
533 var_set_both_formats (v, &output);
535 if (type == DP_DELIMITED)
536 data_parser_add_delimited_field (parser, &input,
537 var_get_case_index (v),
540 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
545 while (lex_token (lexer) != T_ENDCMD);
547 reader = dfm_open_reader (fh, lexer);
551 data_parser_make_active_file (parser, ds, reader, dict);
556 data_parser_destroy (parser);
560 return CMD_CASCADING_FAILURE;