1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013 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/>. */
22 #include "data/case.h"
23 #include "data/caseinit.h"
24 #include "data/casereader-provider.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/transformations.h"
28 #include "data/variable.h"
29 #include "language/command.h"
30 #include "language/data-io/data-reader.h"
31 #include "language/data-io/file-handle.h"
32 #include "language/data-io/inpt-pgm.h"
33 #include "language/expressions/public.h"
34 #include "language/lexer/lexer.h"
35 #include "libpspp/assertion.h"
36 #include "libpspp/compiler.h"
37 #include "libpspp/message.h"
38 #include "libpspp/misc.h"
39 #include "libpspp/str.h"
41 #include "gl/xalloc.h"
44 #define _(msgid) gettext (msgid)
46 /* Indicates how a `union value' should be initialized. */
47 struct input_program_pgm
49 struct trns_chain *trns_chain;
50 enum trns_result restart;
52 casenumber case_nr; /* Incremented by END CASE transformation. */
54 struct caseinit *init;
55 struct caseproto *proto;
58 static void destroy_input_program (struct input_program_pgm *);
59 static trns_proc_func end_case_trns_proc;
60 static trns_proc_func reread_trns_proc;
61 static trns_proc_func end_file_trns_proc;
62 static trns_free_func reread_trns_free;
64 static const struct casereader_class input_program_casereader_class;
66 static bool inside_input_program;
68 /* Returns true if we're parsing the inside of a INPUT
69 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
71 in_input_program (void)
73 return inside_input_program;
76 /* Emits an END CASE transformation for INP. */
78 emit_END_CASE (struct dataset *ds, struct input_program_pgm *inp)
80 add_transformation (ds, end_case_trns_proc, NULL, inp);
84 cmd_input_program (struct lexer *lexer, struct dataset *ds)
86 struct input_program_pgm *inp;
87 bool saw_END_CASE = false;
88 bool saw_END_FILE = false;
89 bool saw_DATA_LIST = false;
92 if (!lex_match (lexer, T_ENDCMD))
93 return lex_end_of_command (lexer);
95 inp = xmalloc (sizeof *inp);
96 inp->trns_chain = NULL;
100 inside_input_program = true;
101 while (!lex_match_phrase (lexer, "END INPUT PROGRAM"))
103 enum cmd_result result;
105 result = cmd_parse_in_state (lexer, ds, CMD_STATE_INPUT_PROGRAM);
109 saw_DATA_LIST = true;
113 emit_END_CASE (ds, inp);
125 if (cmd_result_is_failure (result)
126 && lex_get_error_mode (lexer) != LEX_ERROR_INTERACTIVE)
128 if (result == CMD_EOF)
129 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
130 inside_input_program = false;
132 destroy_input_program (inp);
138 emit_END_CASE (ds, inp);
139 inside_input_program = false;
141 if (!saw_DATA_LIST && !saw_END_FILE)
143 msg (SE, _("Input program must contain DATA LIST or END FILE."));
145 destroy_input_program (inp);
148 if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
150 msg (SE, _("Input program did not create any variables."));
152 destroy_input_program (inp);
156 inp->trns_chain = proc_capture_transformations (ds);
157 trns_chain_finalize (inp->trns_chain);
159 inp->restart = TRNS_CONTINUE;
161 /* Figure out how to initialize each input case. */
162 inp->init = caseinit_create ();
163 caseinit_mark_for_init (inp->init, dataset_dict (ds));
164 inp->proto = caseproto_ref (dict_get_proto (dataset_dict (ds)));
167 ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
168 &input_program_casereader_class, inp));
174 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
176 /* Inside INPUT PROGRAM, this should get caught at the top of the loop in
179 Outside of INPUT PROGRAM, the command parser should reject this
184 /* Returns true if STATE is valid given the transformations that
185 are allowed within INPUT PROGRAM. */
187 is_valid_state (enum trns_result state)
189 return (state == TRNS_CONTINUE
190 || state == TRNS_ERROR
191 || state == TRNS_END_FILE
195 /* Reads and returns one case.
196 Returns the case if successful, null at end of file or if an
197 I/O error occurred. */
198 static struct ccase *
199 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
201 struct input_program_pgm *inp = inp_;
202 struct ccase *c = case_create (inp->proto);
206 assert (is_valid_state (inp->restart));
207 if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
213 c = case_unshare (c);
214 caseinit_init_vars (inp->init, c);
215 inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
217 assert (is_valid_state (inp->restart));
218 caseinit_update_left_vars (inp->init, c);
220 while (inp->restart < 0);
226 destroy_input_program (struct input_program_pgm *pgm)
230 trns_chain_destroy (pgm->trns_chain);
231 caseinit_destroy (pgm->init);
232 caseproto_unref (pgm->proto);
237 /* Destroys the casereader. */
239 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
241 struct input_program_pgm *inp = inp_;
242 if (inp->restart == TRNS_ERROR)
243 casereader_force_error (reader);
244 destroy_input_program (inp);
247 static const struct casereader_class input_program_casereader_class =
249 input_program_casereader_read,
250 input_program_casereader_destroy,
256 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
258 assert (in_input_program ());
259 if (lex_token (lexer) == T_ENDCMD)
264 /* Outputs the current case */
266 end_case_trns_proc (void *inp_, struct ccase **c UNUSED,
267 casenumber case_nr UNUSED)
269 struct input_program_pgm *inp = inp_;
271 return TRNS_END_CASE;
274 /* REREAD transformation. */
277 struct dfm_reader *reader; /* File to move file pointer back on. */
278 struct expression *column; /* Column to reset file pointer to. */
281 /* Parses REREAD command. */
283 cmd_reread (struct lexer *lexer, struct dataset *ds)
285 struct file_handle *fh; /* File to be re-read. */
286 struct expression *e; /* Expression for column to set. */
287 struct reread_trns *t; /* Created transformation. */
288 char *encoding = NULL;
290 fh = fh_get_default_handle ();
292 while (lex_token (lexer) != T_ENDCMD)
294 if (lex_match_id (lexer, "COLUMN"))
296 lex_match (lexer, T_EQUALS);
300 lex_sbc_only_once ("COLUMN");
304 e = expr_parse (lexer, ds, EXPR_NUMBER);
308 else if (lex_match_id (lexer, "FILE"))
310 lex_match (lexer, T_EQUALS);
312 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
316 else if (lex_match_id (lexer, "ENCODING"))
318 lex_match (lexer, T_EQUALS);
319 if (!lex_force_string (lexer))
323 encoding = ss_xstrdup (lex_tokss (lexer));
329 lex_error (lexer, NULL);
334 t = xmalloc (sizeof *t);
335 t->reader = dfm_open_reader (fh, lexer, encoding);
337 add_transformation (ds, reread_trns_proc, reread_trns_free, t);
346 return CMD_CASCADING_FAILURE;
349 /* Executes a REREAD transformation. */
351 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
353 struct reread_trns *t = t_;
355 if (t->column == NULL)
356 dfm_reread_record (t->reader, 1);
359 double column = expr_evaluate_num (t->column, *c, case_num);
360 if (!isfinite (column) || column < 1)
362 msg (SE, _("REREAD: Column numbers must be positive finite "
363 "numbers. Column set to 1."));
364 dfm_reread_record (t->reader, 1);
367 dfm_reread_record (t->reader, column);
369 return TRNS_CONTINUE;
372 /* Frees a REREAD transformation.
373 Returns true if successful, false if an I/O error occurred. */
375 reread_trns_free (void *t_)
377 struct reread_trns *t = t_;
378 expr_free (t->column);
379 dfm_close_reader (t->reader);
383 /* Parses END FILE command. */
385 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
387 assert (in_input_program ());
389 add_transformation (ds, end_file_trns_proc, NULL, NULL);
394 /* Executes an END FILE transformation. */
396 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
397 casenumber case_num UNUSED)
399 return TRNS_END_FILE;