1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009 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/>. */
19 #include <language/data-io/inpt-pgm.h>
24 #include <data/case.h>
25 #include <data/caseinit.h>
26 #include <data/casereader-provider.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/transformations.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/data-io/data-reader.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/expressions/public.h>
35 #include <language/lexer/lexer.h>
36 #include <libpspp/assertion.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/message.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <libpspp/str.h>
46 #define _(msgid) gettext (msgid)
48 /* Private result codes for use within INPUT PROGRAM. */
49 enum cmd_result_extensions
51 CMD_END_INPUT_PROGRAM = CMD_PRIVATE_FIRST,
55 /* Indicates how a `union value' should be initialized. */
58 INP_NUMERIC = 01, /* Numeric. */
59 INP_STRING = 0, /* String. */
61 INP_INIT_ONCE = 02, /* Initialize only once. */
62 INP_REINIT = 0, /* Reinitialize for each iteration. */
65 struct input_program_pgm
67 struct trns_chain *trns_chain;
68 enum trns_result restart;
70 casenumber case_nr; /* Incremented by END CASE transformation. */
72 struct caseinit *init;
73 struct caseproto *proto;
76 static void destroy_input_program (struct input_program_pgm *);
77 static trns_proc_func end_case_trns_proc;
78 static trns_proc_func reread_trns_proc;
79 static trns_proc_func end_file_trns_proc;
80 static trns_free_func reread_trns_free;
82 static const struct casereader_class input_program_casereader_class;
84 static bool inside_input_program;
86 /* Returns true if we're parsing the inside of a INPUT
87 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
89 in_input_program (void)
91 return inside_input_program;
94 /* Emits an END CASE transformation for INP. */
96 emit_END_CASE (struct dataset *ds, struct input_program_pgm *inp)
98 add_transformation (ds, end_case_trns_proc, NULL, inp);
102 cmd_input_program (struct lexer *lexer, struct dataset *ds)
104 struct input_program_pgm *inp;
105 bool saw_END_CASE = false;
107 proc_discard_active_file (ds);
108 if (lex_token (lexer) != '.')
109 return lex_end_of_command (lexer);
111 inp = xmalloc (sizeof *inp);
112 inp->trns_chain = NULL;
116 inside_input_program = true;
119 enum cmd_result result = cmd_parse_in_state (lexer, ds, CMD_STATE_INPUT_PROGRAM);
120 if (result == CMD_END_INPUT_PROGRAM)
122 else if (result == CMD_END_CASE)
124 emit_END_CASE (ds, inp);
127 else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
129 if (result == CMD_EOF)
130 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
131 inside_input_program = false;
132 proc_discard_active_file (ds);
133 destroy_input_program (inp);
138 emit_END_CASE (ds, inp);
139 inside_input_program = false;
141 if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
143 msg (SE, _("Input program did not create any variables."));
144 proc_discard_active_file (ds);
145 destroy_input_program (inp);
149 inp->trns_chain = proc_capture_transformations (ds);
150 trns_chain_finalize (inp->trns_chain);
152 inp->restart = TRNS_CONTINUE;
154 /* Figure out how to initialize each input case. */
155 inp->init = caseinit_create ();
156 caseinit_mark_for_init (inp->init, dataset_dict (ds));
157 inp->proto = caseproto_ref (dict_get_proto (dataset_dict (ds)));
159 proc_set_active_file_data (
160 ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
161 &input_program_casereader_class, inp));
167 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
169 assert (in_input_program ());
170 return CMD_END_INPUT_PROGRAM;
173 /* Returns true if STATE is valid given the transformations that
174 are allowed within INPUT PROGRAM. */
176 is_valid_state (enum trns_result state)
178 return (state == TRNS_CONTINUE
179 || state == TRNS_ERROR
180 || state == TRNS_END_FILE
184 /* Reads and returns one case.
185 Returns the case if successful, null at end of file or if an
186 I/O error occurred. */
187 static struct ccase *
188 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
190 struct input_program_pgm *inp = inp_;
191 struct ccase *c = case_create (inp->proto);
195 assert (is_valid_state (inp->restart));
196 if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
202 c = case_unshare (c);
203 caseinit_init_vars (inp->init, c);
204 inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
206 assert (is_valid_state (inp->restart));
207 caseinit_update_left_vars (inp->init, c);
209 while (inp->restart < 0);
215 destroy_input_program (struct input_program_pgm *pgm)
219 trns_chain_destroy (pgm->trns_chain);
220 caseinit_destroy (pgm->init);
221 caseproto_unref (pgm->proto);
226 /* Destroys the casereader. */
228 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
230 struct input_program_pgm *inp = inp_;
231 if (inp->restart == TRNS_ERROR)
232 casereader_force_error (reader);
233 destroy_input_program (inp);
236 static const struct casereader_class input_program_casereader_class =
238 input_program_casereader_read,
239 input_program_casereader_destroy,
245 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
247 assert (in_input_program ());
248 if (lex_token (lexer) == '.')
250 return lex_end_of_command (lexer);
253 /* Outputs the current case */
255 end_case_trns_proc (void *inp_, struct ccase **c UNUSED,
256 casenumber case_nr UNUSED)
258 struct input_program_pgm *inp = inp_;
260 return TRNS_END_CASE;
263 /* REREAD transformation. */
266 struct dfm_reader *reader; /* File to move file pointer back on. */
267 struct expression *column; /* Column to reset file pointer to. */
270 /* Parses REREAD command. */
272 cmd_reread (struct lexer *lexer, struct dataset *ds)
274 struct file_handle *fh; /* File to be re-read. */
275 struct expression *e; /* Expression for column to set. */
276 struct reread_trns *t; /* Created transformation. */
278 fh = fh_get_default_handle ();
280 while (lex_token (lexer) != '.')
282 if (lex_match_id (lexer, "COLUMN"))
284 lex_match (lexer, '=');
288 msg (SE, _("COLUMN subcommand multiply specified."));
290 return CMD_CASCADING_FAILURE;
293 e = expr_parse (lexer, ds, EXPR_NUMBER);
295 return CMD_CASCADING_FAILURE;
297 else if (lex_match_id (lexer, "FILE"))
299 lex_match (lexer, '=');
301 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
305 return CMD_CASCADING_FAILURE;
310 lex_error (lexer, NULL);
312 return CMD_CASCADING_FAILURE;
316 t = xmalloc (sizeof *t);
317 t->reader = dfm_open_reader (fh, lexer);
319 add_transformation (ds, reread_trns_proc, reread_trns_free, t);
325 /* Executes a REREAD transformation. */
327 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
329 struct reread_trns *t = t_;
331 if (t->column == NULL)
332 dfm_reread_record (t->reader, 1);
335 double column = expr_evaluate_num (t->column, *c, case_num);
336 if (!isfinite (column) || column < 1)
338 msg (SE, _("REREAD: Column numbers must be positive finite "
339 "numbers. Column set to 1."));
340 dfm_reread_record (t->reader, 1);
343 dfm_reread_record (t->reader, column);
345 return TRNS_CONTINUE;
348 /* Frees a REREAD transformation.
349 Returns true if successful, false if an I/O error occurred. */
351 reread_trns_free (void *t_)
353 struct reread_trns *t = t_;
354 expr_free (t->column);
355 dfm_close_reader (t->reader);
359 /* Parses END FILE command. */
361 cmd_end_file (struct lexer *lexer, struct dataset *ds)
363 assert (in_input_program ());
365 add_transformation (ds, end_file_trns_proc, NULL, NULL);
367 return lex_end_of_command (lexer);
370 /* Executes an END FILE transformation. */
372 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
373 casenumber case_num UNUSED)
375 return TRNS_END_FILE;