1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 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/>. */
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 /* Private result codes for use within INPUT PROGRAM. */
47 enum cmd_result_extensions
49 CMD_END_CASE = CMD_PRIVATE_FIRST
52 /* Indicates how a `union value' should be initialized. */
53 struct input_program_pgm
55 struct trns_chain *trns_chain;
56 enum trns_result restart;
58 casenumber case_nr; /* Incremented by END CASE transformation. */
60 struct caseinit *init;
61 struct caseproto *proto;
64 static void destroy_input_program (struct input_program_pgm *);
65 static trns_proc_func end_case_trns_proc;
66 static trns_proc_func reread_trns_proc;
67 static trns_proc_func end_file_trns_proc;
68 static trns_free_func reread_trns_free;
70 static const struct casereader_class input_program_casereader_class;
72 static bool inside_input_program;
74 /* Returns true if we're parsing the inside of a INPUT
75 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
77 in_input_program (void)
79 return inside_input_program;
82 /* Emits an END CASE transformation for INP. */
84 emit_END_CASE (struct dataset *ds, struct input_program_pgm *inp)
86 add_transformation (ds, end_case_trns_proc, NULL, inp);
90 cmd_input_program (struct lexer *lexer, struct dataset *ds)
92 struct input_program_pgm *inp;
93 bool saw_END_CASE = false;
96 if (!lex_match (lexer, T_ENDCMD))
97 return lex_end_of_command (lexer);
99 inp = xmalloc (sizeof *inp);
100 inp->trns_chain = NULL;
104 inside_input_program = true;
105 while (!lex_match_phrase (lexer, "END INPUT PROGRAM"))
107 enum cmd_result result;
109 result = cmd_parse_in_state (lexer, ds, CMD_STATE_INPUT_PROGRAM);
110 if (result == CMD_END_CASE)
112 emit_END_CASE (ds, inp);
115 else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
117 if (result == CMD_EOF)
118 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
119 inside_input_program = false;
121 destroy_input_program (inp);
126 emit_END_CASE (ds, inp);
127 inside_input_program = false;
129 if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
131 msg (SE, _("Input program did not create any variables."));
133 destroy_input_program (inp);
137 inp->trns_chain = proc_capture_transformations (ds);
138 trns_chain_finalize (inp->trns_chain);
140 inp->restart = TRNS_CONTINUE;
142 /* Figure out how to initialize each input case. */
143 inp->init = caseinit_create ();
144 caseinit_mark_for_init (inp->init, dataset_dict (ds));
145 inp->proto = caseproto_ref (dict_get_proto (dataset_dict (ds)));
148 ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
149 &input_program_casereader_class, inp));
155 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
157 /* Inside INPUT PROGRAM, this should get caught at the top of the loop in
160 Outside of INPUT PROGRAM, the command parser should reject this
165 /* Returns true if STATE is valid given the transformations that
166 are allowed within INPUT PROGRAM. */
168 is_valid_state (enum trns_result state)
170 return (state == TRNS_CONTINUE
171 || state == TRNS_ERROR
172 || state == TRNS_END_FILE
176 /* Reads and returns one case.
177 Returns the case if successful, null at end of file or if an
178 I/O error occurred. */
179 static struct ccase *
180 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
182 struct input_program_pgm *inp = inp_;
183 struct ccase *c = case_create (inp->proto);
187 assert (is_valid_state (inp->restart));
188 if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
194 c = case_unshare (c);
195 caseinit_init_vars (inp->init, c);
196 inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
198 assert (is_valid_state (inp->restart));
199 caseinit_update_left_vars (inp->init, c);
201 while (inp->restart < 0);
207 destroy_input_program (struct input_program_pgm *pgm)
211 trns_chain_destroy (pgm->trns_chain);
212 caseinit_destroy (pgm->init);
213 caseproto_unref (pgm->proto);
218 /* Destroys the casereader. */
220 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
222 struct input_program_pgm *inp = inp_;
223 if (inp->restart == TRNS_ERROR)
224 casereader_force_error (reader);
225 destroy_input_program (inp);
228 static const struct casereader_class input_program_casereader_class =
230 input_program_casereader_read,
231 input_program_casereader_destroy,
237 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
239 assert (in_input_program ());
240 if (lex_token (lexer) == T_ENDCMD)
245 /* Outputs the current case */
247 end_case_trns_proc (void *inp_, struct ccase **c UNUSED,
248 casenumber case_nr UNUSED)
250 struct input_program_pgm *inp = inp_;
252 return TRNS_END_CASE;
255 /* REREAD transformation. */
258 struct dfm_reader *reader; /* File to move file pointer back on. */
259 struct expression *column; /* Column to reset file pointer to. */
262 /* Parses REREAD command. */
264 cmd_reread (struct lexer *lexer, struct dataset *ds)
266 struct file_handle *fh; /* File to be re-read. */
267 struct expression *e; /* Expression for column to set. */
268 struct reread_trns *t; /* Created transformation. */
270 fh = fh_get_default_handle ();
272 while (lex_token (lexer) != T_ENDCMD)
274 if (lex_match_id (lexer, "COLUMN"))
276 lex_match (lexer, T_EQUALS);
280 lex_sbc_only_once ("COLUMN");
282 return CMD_CASCADING_FAILURE;
285 e = expr_parse (lexer, ds, EXPR_NUMBER);
287 return CMD_CASCADING_FAILURE;
289 else if (lex_match_id (lexer, "FILE"))
291 lex_match (lexer, T_EQUALS);
293 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
297 return CMD_CASCADING_FAILURE;
302 lex_error (lexer, NULL);
304 return CMD_CASCADING_FAILURE;
308 t = xmalloc (sizeof *t);
309 t->reader = dfm_open_reader (fh, lexer);
311 add_transformation (ds, reread_trns_proc, reread_trns_free, t);
317 /* Executes a REREAD transformation. */
319 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
321 struct reread_trns *t = t_;
323 if (t->column == NULL)
324 dfm_reread_record (t->reader, 1);
327 double column = expr_evaluate_num (t->column, *c, case_num);
328 if (!isfinite (column) || column < 1)
330 msg (SE, _("REREAD: Column numbers must be positive finite "
331 "numbers. Column set to 1."));
332 dfm_reread_record (t->reader, 1);
335 dfm_reread_record (t->reader, column);
337 return TRNS_CONTINUE;
340 /* Frees a REREAD transformation.
341 Returns true if successful, false if an I/O error occurred. */
343 reread_trns_free (void *t_)
345 struct reread_trns *t = t_;
346 expr_free (t->column);
347 dfm_close_reader (t->reader);
351 /* Parses END FILE command. */
353 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
355 assert (in_input_program ());
357 add_transformation (ds, end_file_trns_proc, NULL, NULL);
362 /* Executes an END FILE transformation. */
364 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
365 casenumber case_num UNUSED)
367 return TRNS_END_FILE;