1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012 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 == (enum cmd_result) CMD_END_CASE)
112 emit_END_CASE (ds, inp);
115 else if (cmd_result_is_failure (result)
116 && result != CMD_FAILURE
117 && lex_get_error_mode (lexer) != LEX_ERROR_INTERACTIVE)
119 if (result == CMD_EOF)
120 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
121 inside_input_program = false;
123 destroy_input_program (inp);
128 emit_END_CASE (ds, inp);
129 inside_input_program = false;
131 if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
133 msg (SE, _("Input program did not create any variables."));
135 destroy_input_program (inp);
139 inp->trns_chain = proc_capture_transformations (ds);
140 trns_chain_finalize (inp->trns_chain);
142 inp->restart = TRNS_CONTINUE;
144 /* Figure out how to initialize each input case. */
145 inp->init = caseinit_create ();
146 caseinit_mark_for_init (inp->init, dataset_dict (ds));
147 inp->proto = caseproto_ref (dict_get_proto (dataset_dict (ds)));
150 ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
151 &input_program_casereader_class, inp));
157 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
159 /* Inside INPUT PROGRAM, this should get caught at the top of the loop in
162 Outside of INPUT PROGRAM, the command parser should reject this
167 /* Returns true if STATE is valid given the transformations that
168 are allowed within INPUT PROGRAM. */
170 is_valid_state (enum trns_result state)
172 return (state == TRNS_CONTINUE
173 || state == TRNS_ERROR
174 || state == TRNS_END_FILE
178 /* Reads and returns one case.
179 Returns the case if successful, null at end of file or if an
180 I/O error occurred. */
181 static struct ccase *
182 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
184 struct input_program_pgm *inp = inp_;
185 struct ccase *c = case_create (inp->proto);
189 assert (is_valid_state (inp->restart));
190 if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
196 c = case_unshare (c);
197 caseinit_init_vars (inp->init, c);
198 inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
200 assert (is_valid_state (inp->restart));
201 caseinit_update_left_vars (inp->init, c);
203 while (inp->restart < 0);
209 destroy_input_program (struct input_program_pgm *pgm)
213 trns_chain_destroy (pgm->trns_chain);
214 caseinit_destroy (pgm->init);
215 caseproto_unref (pgm->proto);
220 /* Destroys the casereader. */
222 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
224 struct input_program_pgm *inp = inp_;
225 if (inp->restart == TRNS_ERROR)
226 casereader_force_error (reader);
227 destroy_input_program (inp);
230 static const struct casereader_class input_program_casereader_class =
232 input_program_casereader_read,
233 input_program_casereader_destroy,
239 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
241 assert (in_input_program ());
242 if (lex_token (lexer) == T_ENDCMD)
247 /* Outputs the current case */
249 end_case_trns_proc (void *inp_, struct ccase **c UNUSED,
250 casenumber case_nr UNUSED)
252 struct input_program_pgm *inp = inp_;
254 return TRNS_END_CASE;
257 /* REREAD transformation. */
260 struct dfm_reader *reader; /* File to move file pointer back on. */
261 struct expression *column; /* Column to reset file pointer to. */
264 /* Parses REREAD command. */
266 cmd_reread (struct lexer *lexer, struct dataset *ds)
268 struct file_handle *fh; /* File to be re-read. */
269 struct expression *e; /* Expression for column to set. */
270 struct reread_trns *t; /* Created transformation. */
271 char *encoding = NULL;
273 fh = fh_get_default_handle ();
275 while (lex_token (lexer) != T_ENDCMD)
277 if (lex_match_id (lexer, "COLUMN"))
279 lex_match (lexer, T_EQUALS);
283 lex_sbc_only_once ("COLUMN");
287 e = expr_parse (lexer, ds, EXPR_NUMBER);
291 else if (lex_match_id (lexer, "FILE"))
293 lex_match (lexer, T_EQUALS);
295 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
299 else if (lex_match_id (lexer, "ENCODING"))
301 lex_match (lexer, T_EQUALS);
302 if (!lex_force_string (lexer))
306 encoding = ss_xstrdup (lex_tokss (lexer));
312 lex_error (lexer, NULL);
317 t = xmalloc (sizeof *t);
318 t->reader = dfm_open_reader (fh, lexer, encoding);
320 add_transformation (ds, reread_trns_proc, reread_trns_free, t);
329 return CMD_CASCADING_FAILURE;
332 /* Executes a REREAD transformation. */
334 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
336 struct reread_trns *t = t_;
338 if (t->column == NULL)
339 dfm_reread_record (t->reader, 1);
342 double column = expr_evaluate_num (t->column, *c, case_num);
343 if (!isfinite (column) || column < 1)
345 msg (SE, _("REREAD: Column numbers must be positive finite "
346 "numbers. Column set to 1."));
347 dfm_reread_record (t->reader, 1);
350 dfm_reread_record (t->reader, column);
352 return TRNS_CONTINUE;
355 /* Frees a REREAD transformation.
356 Returns true if successful, false if an I/O error occurred. */
358 reread_trns_free (void *t_)
360 struct reread_trns *t = t_;
361 expr_free (t->column);
362 dfm_close_reader (t->reader);
366 /* Parses END FILE command. */
368 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
370 assert (in_input_program ());
372 add_transformation (ds, end_file_trns_proc, NULL, NULL);
377 /* Executes an END FILE transformation. */
379 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
380 casenumber case_num UNUSED)
382 return TRNS_END_FILE;