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