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