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/session.h"
28 #include "data/transformations.h"
29 #include "data/variable.h"
30 #include "language/command.h"
31 #include "language/data-io/data-reader.h"
32 #include "language/data-io/file-handle.h"
33 #include "language/data-io/inpt-pgm.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/misc.h"
40 #include "libpspp/str.h"
42 #include "gl/xalloc.h"
45 #define _(msgid) gettext (msgid)
47 /* Indicates how a `union value' should be initialized. */
48 struct input_program_pgm
50 struct session *session;
52 struct trns_chain *trns_chain;
53 enum trns_result restart;
55 casenumber case_nr; /* Incremented by END CASE transformation. */
57 struct caseinit *init;
58 struct caseproto *proto;
61 static void destroy_input_program (struct input_program_pgm *);
62 static trns_proc_func end_case_trns_proc;
63 static trns_proc_func reread_trns_proc;
64 static trns_proc_func end_file_trns_proc;
65 static trns_free_func reread_trns_free;
67 static const struct casereader_class input_program_casereader_class;
69 static bool inside_input_program;
71 /* Returns true if we're parsing the inside of a INPUT
72 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
74 in_input_program (void)
76 return inside_input_program;
79 /* Emits an END CASE transformation for INP. */
81 emit_END_CASE (struct dataset *ds, struct input_program_pgm *inp)
83 add_transformation (ds, end_case_trns_proc, NULL, inp);
87 cmd_input_program (struct lexer *lexer, struct dataset *ds)
89 struct input_program_pgm *inp;
90 bool saw_END_CASE = false;
91 bool saw_END_FILE = false;
92 bool saw_DATA_LIST = false;
94 if (!lex_match (lexer, T_ENDCMD))
95 return lex_end_of_command (lexer);
97 inp = xmalloc (sizeof *inp);
98 inp->session = session_create (dataset_session (ds));
99 inp->ds = dataset_create (inp->session, "INPUT PROGRAM");
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, inp->ds, CMD_STATE_INPUT_PROGRAM);
113 saw_DATA_LIST = true;
117 emit_END_CASE (inp->ds, inp);
129 if (cmd_result_is_failure (result)
130 && lex_get_error_mode (lexer) != LEX_ERROR_INTERACTIVE)
132 if (result == CMD_EOF)
133 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
134 inside_input_program = false;
135 destroy_input_program (inp);
141 emit_END_CASE (inp->ds, inp);
142 inside_input_program = false;
144 if (!saw_DATA_LIST && !saw_END_FILE)
146 msg (SE, _("Input program must contain DATA LIST or END FILE."));
147 destroy_input_program (inp);
150 if (dict_get_next_value_idx (dataset_dict (inp->ds)) == 0)
152 msg (SE, _("Input program did not create any variables."));
153 destroy_input_program (inp);
157 inp->trns_chain = proc_capture_transformations (inp->ds);
158 trns_chain_finalize (inp->trns_chain);
160 inp->restart = TRNS_CONTINUE;
162 /* Figure out how to initialize each input case. */
163 inp->init = caseinit_create ();
164 caseinit_mark_for_init (inp->init, dataset_dict (inp->ds));
165 inp->proto = caseproto_ref (dict_get_proto (dataset_dict (inp->ds)));
167 dataset_set_dict (ds, dict_clone (dataset_dict (inp->ds)));
169 ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
170 &input_program_casereader_class, inp));
176 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
178 /* Inside INPUT PROGRAM, this should get caught at the top of the loop in
181 Outside of INPUT PROGRAM, the command parser should reject this
186 /* Returns true if STATE is valid given the transformations that
187 are allowed within INPUT PROGRAM. */
189 is_valid_state (enum trns_result state)
191 return (state == TRNS_CONTINUE
192 || state == TRNS_ERROR
193 || state == TRNS_END_FILE
197 /* Reads and returns one case.
198 Returns the case if successful, null at end of file or if an
199 I/O error occurred. */
200 static struct ccase *
201 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
203 struct input_program_pgm *inp = inp_;
204 struct ccase *c = case_create (inp->proto);
208 assert (is_valid_state (inp->restart));
209 if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
215 c = case_unshare (c);
216 caseinit_init_vars (inp->init, c);
217 inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
219 assert (is_valid_state (inp->restart));
220 caseinit_update_left_vars (inp->init, c);
222 while (inp->restart < 0);
228 destroy_input_program (struct input_program_pgm *pgm)
232 session_destroy (pgm->session);
233 trns_chain_destroy (pgm->trns_chain);
234 caseinit_destroy (pgm->init);
235 caseproto_unref (pgm->proto);
240 /* Destroys the casereader. */
242 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
244 struct input_program_pgm *inp = inp_;
245 if (inp->restart == TRNS_ERROR)
246 casereader_force_error (reader);
247 destroy_input_program (inp);
250 static const struct casereader_class input_program_casereader_class =
252 input_program_casereader_read,
253 input_program_casereader_destroy,
259 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
261 assert (in_input_program ());
262 if (lex_token (lexer) == T_ENDCMD)
267 /* Outputs the current case */
269 end_case_trns_proc (void *inp_, struct ccase **c UNUSED,
270 casenumber case_nr UNUSED)
272 struct input_program_pgm *inp = inp_;
274 return TRNS_END_CASE;
277 /* REREAD transformation. */
280 struct dfm_reader *reader; /* File to move file pointer back on. */
281 struct expression *column; /* Column to reset file pointer to. */
284 /* Parses REREAD command. */
286 cmd_reread (struct lexer *lexer, struct dataset *ds)
288 struct file_handle *fh; /* File to be re-read. */
289 struct expression *e; /* Expression for column to set. */
290 struct reread_trns *t; /* Created transformation. */
291 char *encoding = NULL;
293 fh = fh_get_default_handle ();
295 while (lex_token (lexer) != T_ENDCMD)
297 if (lex_match_id (lexer, "COLUMN"))
299 lex_match (lexer, T_EQUALS);
303 lex_sbc_only_once ("COLUMN");
307 e = expr_parse (lexer, ds, EXPR_NUMBER);
311 else if (lex_match_id (lexer, "FILE"))
313 lex_match (lexer, T_EQUALS);
315 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
319 else if (lex_match_id (lexer, "ENCODING"))
321 lex_match (lexer, T_EQUALS);
322 if (!lex_force_string (lexer))
326 encoding = ss_xstrdup (lex_tokss (lexer));
332 lex_error (lexer, NULL);
337 t = xmalloc (sizeof *t);
338 t->reader = dfm_open_reader (fh, lexer, encoding);
340 add_transformation (ds, reread_trns_proc, reread_trns_free, t);
349 return CMD_CASCADING_FAILURE;
352 /* Executes a REREAD transformation. */
354 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
356 struct reread_trns *t = t_;
358 if (t->column == NULL)
359 dfm_reread_record (t->reader, 1);
362 double column = expr_evaluate_num (t->column, *c, case_num);
363 if (!isfinite (column) || column < 1)
365 msg (SE, _("REREAD: Column numbers must be positive finite "
366 "numbers. Column set to 1."));
367 dfm_reread_record (t->reader, 1);
370 dfm_reread_record (t->reader, column);
372 return TRNS_CONTINUE;
375 /* Frees a REREAD transformation.
376 Returns true if successful, false if an I/O error occurred. */
378 reread_trns_free (void *t_)
380 struct reread_trns *t = t_;
381 expr_free (t->column);
382 dfm_close_reader (t->reader);
386 /* Parses END FILE command. */
388 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
390 assert (in_input_program ());
392 add_transformation (ds, end_file_trns_proc, NULL, NULL);
397 /* Executes an END FILE transformation. */
399 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
400 casenumber case_num UNUSED)
402 return TRNS_END_FILE;