03345003b701d9d6ba6fd339ce49c44d1fe611b8
[pspp] / src / language / data-io / inpt-pgm.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <float.h>
20 #include <stdlib.h>
21
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"
41
42 #include "gl/xalloc.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46
47 /* Indicates how a `union value' should be initialized. */
48 struct input_program_pgm
49   {
50     struct session *session;
51     struct dataset *ds;
52
53     struct trns_chain xforms;
54     size_t idx;
55     bool eof;
56
57     casenumber case_nr;             /* Incremented by END CASE transformation. */
58
59     struct caseinit *init;
60     struct caseproto *proto;
61   };
62
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;
67
68 static const struct casereader_class input_program_casereader_class;
69
70 static bool inside_input_program;
71 static bool saw_END_CASE;
72 static bool saw_END_FILE;
73 static bool saw_DATA_LIST;
74
75 /* Returns true if we're parsing the inside of a INPUT
76    PROGRAM...END INPUT PROGRAM construct, false otherwise. */
77 bool
78 in_input_program (void)
79 {
80   return inside_input_program;
81 }
82
83 /* Emits an END CASE transformation for INP. */
84 static void
85 emit_END_CASE (struct dataset *ds)
86 {
87   add_transformation (ds, &end_case_trns_class, xzalloc (sizeof (bool)));
88 }
89
90 int
91 cmd_input_program (struct lexer *lexer, struct dataset *ds)
92 {
93   if (!lex_match (lexer, T_ENDCMD))
94     return lex_end_of_command (lexer);
95
96   struct session *session = session_create (dataset_session (ds));
97   struct dataset *inp_ds = dataset_create (session, "INPUT PROGRAM");
98
99   struct input_program_pgm *inp = xmalloc (sizeof *inp);
100   *inp = (struct input_program_pgm) { .session = session, .ds = inp_ds };
101
102   proc_push_transformations (inp->ds);
103   inside_input_program = true;
104   saw_END_CASE = saw_END_FILE = saw_DATA_LIST = false;
105   while (!lex_match_phrase (lexer, "END INPUT PROGRAM"))
106     {
107       enum cmd_result result;
108
109       result = cmd_parse_in_state (lexer, inp->ds, CMD_STATE_INPUT_PROGRAM);
110       if (result != CMD_FAILURE
111           && cmd_result_is_failure (result)
112           && lex_get_error_mode (lexer) != LEX_ERROR_TERMINAL)
113         {
114           proc_pop_transformations (ds, &inp->xforms);
115
116           if (result == CMD_EOF)
117             msg (SE, _("Unexpected end-of-file within %s."), "INPUT PROGRAM");
118           inside_input_program = false;
119           destroy_input_program (inp);
120           return result;
121         }
122     }
123   if (!saw_END_CASE)
124     emit_END_CASE (inp->ds);
125   inside_input_program = false;
126   proc_pop_transformations (inp->ds, &inp->xforms);
127
128   if (!saw_DATA_LIST && !saw_END_FILE)
129     {
130       msg (SE, _("Input program must contain %s or %s."), "DATA LIST", "END FILE");
131       destroy_input_program (inp);
132       return CMD_FAILURE;
133     }
134   if (dict_get_next_value_idx (dataset_dict (inp->ds)) == 0)
135     {
136       msg (SE, _("Input program did not create any variables."));
137       destroy_input_program (inp);
138       return CMD_FAILURE;
139     }
140
141   /* Figure out how to initialize each input case. */
142   inp->init = caseinit_create ();
143   caseinit_mark_for_init (inp->init, dataset_dict (inp->ds));
144   inp->proto = caseproto_ref (dict_get_proto (dataset_dict (inp->ds)));
145
146   dataset_set_dict (ds, dict_clone (dataset_dict (inp->ds)));
147   dataset_set_source (
148     ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
149                                       &input_program_casereader_class, inp));
150
151   return CMD_SUCCESS;
152 }
153
154 /* Reads and returns one case.
155    Returns the case if successful, null at end of file or if an
156    I/O error occurred. */
157 static struct ccase *
158 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
159 {
160   struct input_program_pgm *inp = inp_;
161
162   if (inp->eof || !inp->xforms.n)
163     return NULL;
164
165   struct ccase *c = case_create (inp->proto);
166   caseinit_init_vars (inp->init, c);
167
168   for (size_t i = inp->idx < inp->xforms.n ? inp->idx : 0; ; i++)
169     {
170       if (i >= inp->xforms.n)
171         {
172           i = 0;
173           c = case_unshare (c);
174           caseinit_update_left_vars (inp->init, c);
175           caseinit_init_vars (inp->init, c);
176         }
177
178       const struct transformation *trns = &inp->xforms.xforms[i];
179       switch (trns->class->execute (trns->aux, &c, inp->case_nr))
180         {
181         case TRNS_END_CASE:
182           inp->case_nr++;
183           inp->idx = i;
184           return c;
185
186         case TRNS_ERROR:
187           casereader_force_error (reader);
188           /* Fall through. */
189         case TRNS_END_FILE:
190           inp->eof = true;
191           case_unref (c);
192           return NULL;
193
194         case TRNS_CONTINUE:
195           break;
196
197         default:
198           NOT_REACHED ();
199         }
200     }
201 }
202
203 static void
204 destroy_input_program (struct input_program_pgm *pgm)
205 {
206   if (pgm != NULL)
207     {
208       session_destroy (pgm->session);
209       trns_chain_uninit (&pgm->xforms);
210       caseinit_destroy (pgm->init);
211       caseproto_unref (pgm->proto);
212       free (pgm);
213     }
214 }
215
216 /* Destroys the casereader. */
217 static void
218 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
219 {
220   struct input_program_pgm *inp = inp_;
221   destroy_input_program (inp);
222 }
223
224 static const struct casereader_class input_program_casereader_class =
225   {
226     input_program_casereader_read,
227     input_program_casereader_destroy,
228     NULL,
229     NULL,
230   };
231 \f
232 int
233 cmd_end_case (struct lexer *lexer UNUSED, struct dataset *ds)
234 {
235   assert (in_input_program ());
236   emit_END_CASE (ds);
237   saw_END_CASE = true;
238   return CMD_SUCCESS;
239 }
240
241 /* Outputs the current case */
242 static enum trns_result
243 end_case_trns_proc (void *resume_, struct ccase **c UNUSED,
244                     casenumber case_nr UNUSED)
245 {
246   bool *resume = resume_;
247   enum trns_result retval = *resume ? TRNS_CONTINUE : TRNS_END_CASE;
248   *resume = !*resume;
249   return retval;
250 }
251
252 static bool
253 end_case_trns_free (void *resume)
254 {
255   free (resume);
256   return true;
257 }
258
259 static const struct trns_class end_case_trns_class = {
260   .name = "END CASE",
261   .execute = end_case_trns_proc,
262   .destroy = end_case_trns_free,
263 };
264
265 /* REREAD transformation. */
266 struct reread_trns
267   {
268     struct dfm_reader *reader;  /* File to move file pointer back on. */
269     struct expression *column;  /* Column to reset file pointer to. */
270   };
271
272 /* Parses REREAD command. */
273 int
274 cmd_reread (struct lexer *lexer, struct dataset *ds)
275 {
276   struct file_handle *fh;       /* File to be re-read. */
277   struct expression *e;         /* Expression for column to set. */
278   struct reread_trns *t;        /* Created transformation. */
279   char *encoding = NULL;
280
281   fh = fh_get_default_handle ();
282   e = NULL;
283   while (lex_token (lexer) != T_ENDCMD)
284     {
285       if (lex_match_id (lexer, "COLUMN"))
286         {
287           lex_match (lexer, T_EQUALS);
288
289           if (e)
290             {
291               lex_sbc_only_once ("COLUMN");
292               goto error;
293             }
294
295           e = expr_parse (lexer, ds, VAL_NUMERIC);
296           if (!e)
297             goto error;
298         }
299       else if (lex_match_id (lexer, "FILE"))
300         {
301           lex_match (lexer, T_EQUALS);
302           fh_unref (fh);
303           fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
304           if (fh == NULL)
305             goto error;
306         }
307       else if (lex_match_id (lexer, "ENCODING"))
308         {
309           lex_match (lexer, T_EQUALS);
310           if (!lex_force_string (lexer))
311             goto error;
312
313           free (encoding);
314           encoding = ss_xstrdup (lex_tokss (lexer));
315
316           lex_get (lexer);
317         }
318       else
319         {
320           lex_error (lexer, NULL);
321           goto error;
322         }
323     }
324
325   t = xmalloc (sizeof *t);
326   t->reader = dfm_open_reader (fh, lexer, encoding);
327   t->column = e;
328   add_transformation (ds, &reread_trns_class, t);
329
330   fh_unref (fh);
331   free (encoding);
332   return CMD_SUCCESS;
333
334 error:
335   expr_free (e);
336   free (encoding);
337   return CMD_CASCADING_FAILURE;
338 }
339
340 /* Executes a REREAD transformation. */
341 static enum trns_result
342 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
343 {
344   struct reread_trns *t = t_;
345
346   if (t->column == NULL)
347     dfm_reread_record (t->reader, 1);
348   else
349     {
350       double column = expr_evaluate_num (t->column, *c, case_num);
351       if (!isfinite (column) || column < 1)
352         {
353           msg (SE, _("REREAD: Column numbers must be positive finite "
354                "numbers.  Column set to 1."));
355           dfm_reread_record (t->reader, 1);
356         }
357       else
358         dfm_reread_record (t->reader, column);
359     }
360   return TRNS_CONTINUE;
361 }
362
363 /* Frees a REREAD transformation.
364    Returns true if successful, false if an I/O error occurred. */
365 static bool
366 reread_trns_free (void *t_)
367 {
368   struct reread_trns *t = t_;
369   expr_free (t->column);
370   dfm_close_reader (t->reader);
371   return true;
372 }
373
374 static const struct trns_class reread_trns_class = {
375   .name = "REREAD",
376   .execute = reread_trns_proc,
377   .destroy = reread_trns_free,
378 };
379
380 /* Parses END FILE command. */
381 int
382 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
383 {
384   assert (in_input_program ());
385
386   add_transformation (ds, &end_file_trns_class, NULL);
387   saw_END_FILE = true;
388
389   return CMD_SUCCESS;
390 }
391
392 /* Executes an END FILE transformation. */
393 static enum trns_result
394 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
395                     casenumber case_num UNUSED)
396 {
397   return TRNS_END_FILE;
398 }
399
400 static const struct trns_class end_file_trns_class = {
401   .name = "END FILE",
402   .execute = end_file_trns_proc,
403 };