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/commands/data-reader.h"
32 #include "language/commands/file-handle.h"
33 #include "language/commands/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;
53 struct trns_chain xforms;
57 casenumber case_nr; /* Incremented by END CASE transformation. */
59 struct caseinit *init;
60 struct caseproto *proto;
63 static void destroy_input_program (struct input_program_pgm *);
64 static const struct trns_class end_case_trns_class;
65 static const struct trns_class reread_trns_class;
66 static const struct trns_class end_file_trns_class;
68 static const struct casereader_class input_program_casereader_class;
70 static bool inside_input_program;
71 static bool saw_END_CASE;
72 static bool saw_END_FILE;
73 static bool saw_DATA_LIST;
75 /* Returns true if we're parsing the inside of a INPUT
76 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
78 in_input_program (void)
80 return inside_input_program;
89 /* Emits an END CASE transformation for INP. */
91 emit_END_CASE (struct dataset *ds)
93 add_transformation (ds, &end_case_trns_class, xzalloc (sizeof (bool)));
97 cmd_input_program (struct lexer *lexer, struct dataset *ds)
99 struct msg_location *location = lex_ofs_location (lexer, 0, 1);
100 if (!lex_match (lexer, T_ENDCMD))
102 msg_location_destroy (location);
103 return lex_end_of_command (lexer);
106 struct session *session = session_create (dataset_session (ds));
107 struct dataset *inp_ds = dataset_create (session, "INPUT PROGRAM");
109 struct input_program_pgm *inp = xmalloc (sizeof *inp);
110 *inp = (struct input_program_pgm) { .session = session, .ds = inp_ds };
112 proc_push_transformations (inp->ds);
113 inside_input_program = true;
114 saw_END_CASE = saw_END_FILE = saw_DATA_LIST = false;
115 while (!lex_match_phrase (lexer, "END INPUT PROGRAM"))
117 enum cmd_result result;
119 result = cmd_parse_in_state (lexer, inp->ds, CMD_STATE_INPUT_PROGRAM);
120 if (result == CMD_EOF
121 || result == CMD_FINISH
122 || result == CMD_CASCADING_FAILURE)
124 proc_pop_transformations (inp->ds, &inp->xforms);
126 if (result == CMD_EOF)
127 msg (SE, _("Unexpected end-of-file within %s."), "INPUT PROGRAM");
128 inside_input_program = false;
129 destroy_input_program (inp);
130 msg_location_destroy (location);
135 emit_END_CASE (inp->ds);
136 inside_input_program = false;
137 proc_pop_transformations (inp->ds, &inp->xforms);
139 struct msg_location *end = lex_ofs_location (lexer, 0, 2);
140 msg_location_merge (&location, end);
141 location->omit_underlines = true;
142 msg_location_destroy (end);
144 if (!saw_DATA_LIST && !saw_END_FILE)
146 msg_at (SE, location, _("Input program does not contain %s or %s."),
147 "DATA LIST", "END FILE");
148 destroy_input_program (inp);
149 msg_location_destroy (location);
152 if (dict_get_next_value_idx (dataset_dict (inp->ds)) == 0)
154 msg_at (SE, location, _("Input program did not create any variables."));
155 destroy_input_program (inp);
156 msg_location_destroy (location);
159 msg_location_destroy (location);
161 /* Figure out how to initialize each input case. */
162 inp->init = caseinit_create ();
163 caseinit_mark_for_init (inp->init, dataset_dict (inp->ds));
164 inp->proto = caseproto_ref (dict_get_proto (dataset_dict (inp->ds)));
166 dataset_set_dict (ds, dict_clone (dataset_dict (inp->ds)));
168 ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
169 &input_program_casereader_class, inp));
174 /* Reads and returns one case.
175 Returns the case if successful, null at end of file or if an
176 I/O error occurred. */
177 static struct ccase *
178 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
180 struct input_program_pgm *inp = inp_;
182 if (inp->eof || !inp->xforms.n)
185 struct ccase *c = case_create (inp->proto);
186 caseinit_init_vars (inp->init, c);
188 for (size_t i = inp->idx < inp->xforms.n ? inp->idx : 0; ; i++)
190 if (i >= inp->xforms.n)
193 c = case_unshare (c);
194 caseinit_update_left_vars (inp->init, c);
195 caseinit_init_vars (inp->init, c);
198 const struct transformation *trns = &inp->xforms.xforms[i];
199 switch (trns->class->execute (trns->aux, &c, inp->case_nr))
207 casereader_force_error (reader);
224 destroy_input_program (struct input_program_pgm *pgm)
228 session_destroy (pgm->session);
229 trns_chain_uninit (&pgm->xforms);
230 caseinit_destroy (pgm->init);
231 caseproto_unref (pgm->proto);
236 /* Destroys the casereader. */
238 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
240 struct input_program_pgm *inp = inp_;
241 destroy_input_program (inp);
244 static const struct casereader_class input_program_casereader_class =
246 input_program_casereader_read,
247 input_program_casereader_destroy,
253 cmd_end_case (struct lexer *lexer UNUSED, struct dataset *ds)
255 assert (in_input_program ());
261 /* Outputs the current case */
262 static enum trns_result
263 end_case_trns_proc (void *resume_, struct ccase **c UNUSED,
264 casenumber case_nr UNUSED)
266 bool *resume = resume_;
267 enum trns_result retval = *resume ? TRNS_CONTINUE : TRNS_END_CASE;
273 end_case_trns_free (void *resume)
279 static const struct trns_class end_case_trns_class = {
281 .execute = end_case_trns_proc,
282 .destroy = end_case_trns_free,
285 /* REREAD transformation. */
288 struct dfm_reader *reader; /* File to move file pointer back on. */
289 struct expression *column; /* Column to reset file pointer to. */
292 /* Parses REREAD command. */
294 cmd_reread (struct lexer *lexer, struct dataset *ds)
296 char *encoding = NULL;
297 struct file_handle *fh = fh_get_default_handle ();
298 struct expression *e = NULL;
299 while (lex_token (lexer) != T_ENDCMD)
301 if (lex_match_id (lexer, "COLUMN"))
303 lex_match (lexer, T_EQUALS);
307 lex_sbc_only_once (lexer, "COLUMN");
311 e = expr_parse (lexer, ds, VAL_NUMERIC);
315 else if (lex_match_id (lexer, "FILE"))
317 lex_match (lexer, T_EQUALS);
319 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
323 else if (lex_match_id (lexer, "ENCODING"))
325 lex_match (lexer, T_EQUALS);
326 if (!lex_force_string (lexer))
330 encoding = ss_xstrdup (lex_tokss (lexer));
336 lex_error_expecting (lexer, "COLUMN", "FILE", "ENCODING");
341 struct reread_trns *t = xmalloc (sizeof *t);
342 *t = (struct reread_trns) {
343 .reader = dfm_open_reader (fh, lexer, encoding),
346 add_transformation (ds, &reread_trns_class, t);
355 return CMD_CASCADING_FAILURE;
358 /* Executes a REREAD transformation. */
359 static enum trns_result
360 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
362 struct reread_trns *t = t_;
364 if (t->column == NULL)
365 dfm_reread_record (t->reader, 1);
368 double column = expr_evaluate_num (t->column, *c, case_num);
369 if (!isfinite (column) || column < 1)
371 msg (SE, _("REREAD: Column numbers must be positive finite "
372 "numbers. Column set to 1."));
373 dfm_reread_record (t->reader, 1);
376 dfm_reread_record (t->reader, column);
378 return TRNS_CONTINUE;
381 /* Frees a REREAD transformation.
382 Returns true if successful, false if an I/O error occurred. */
384 reread_trns_free (void *t_)
386 struct reread_trns *t = t_;
387 expr_free (t->column);
388 dfm_close_reader (t->reader);
392 static const struct trns_class reread_trns_class = {
394 .execute = reread_trns_proc,
395 .destroy = reread_trns_free,
398 /* Parses END FILE command. */
400 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
402 assert (in_input_program ());
404 add_transformation (ds, &end_file_trns_class, NULL);
410 /* Executes an END FILE transformation. */
411 static enum trns_result
412 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
413 casenumber case_num UNUSED)
415 return TRNS_END_FILE;
418 static const struct trns_class end_file_trns_class = {
420 .execute = end_file_trns_proc,