72d5a2e7b88e302df07e4ca54e59c206cbdebae5
[pspp-builds.git] / src / language / data-io / inpt-pgm.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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/dictionary.h"
26 #include "data/procedure.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   proc_discard_active_file (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           proc_discard_active_file (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       proc_discard_active_file (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   proc_set_active_file_data (
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
270   fh = fh_get_default_handle ();
271   e = NULL;
272   while (lex_token (lexer) != T_ENDCMD)
273     {
274       if (lex_match_id (lexer, "COLUMN"))
275         {
276           lex_match (lexer, T_EQUALS);
277
278           if (e)
279             {
280               msg (SE, _("%s subcommand may be given at most once."), "COLUMN");
281               expr_free (e);
282               return CMD_CASCADING_FAILURE;
283             }
284
285           e = expr_parse (lexer, ds, EXPR_NUMBER);
286           if (!e)
287             return CMD_CASCADING_FAILURE;
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);
294           if (fh == NULL)
295             {
296               expr_free (e);
297               return CMD_CASCADING_FAILURE;
298             }
299         }
300       else
301         {
302           lex_error (lexer, NULL);
303           expr_free (e);
304           return CMD_CASCADING_FAILURE;
305         }
306     }
307
308   t = xmalloc (sizeof *t);
309   t->reader = dfm_open_reader (fh, lexer);
310   t->column = e;
311   add_transformation (ds, reread_trns_proc, reread_trns_free, t);
312
313   fh_unref (fh);
314   return CMD_SUCCESS;
315 }
316
317 /* Executes a REREAD transformation. */
318 static int
319 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
320 {
321   struct reread_trns *t = t_;
322
323   if (t->column == NULL)
324     dfm_reread_record (t->reader, 1);
325   else
326     {
327       double column = expr_evaluate_num (t->column, *c, case_num);
328       if (!isfinite (column) || column < 1)
329         {
330           msg (SE, _("REREAD: Column numbers must be positive finite "
331                "numbers.  Column set to 1."));
332           dfm_reread_record (t->reader, 1);
333         }
334       else
335         dfm_reread_record (t->reader, column);
336     }
337   return TRNS_CONTINUE;
338 }
339
340 /* Frees a REREAD transformation.
341    Returns true if successful, false if an I/O error occurred. */
342 static bool
343 reread_trns_free (void *t_)
344 {
345   struct reread_trns *t = t_;
346   expr_free (t->column);
347   dfm_close_reader (t->reader);
348   return true;
349 }
350
351 /* Parses END FILE command. */
352 int
353 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
354 {
355   assert (in_input_program ());
356
357   add_transformation (ds, end_file_trns_proc, NULL, NULL);
358
359   return CMD_SUCCESS;
360 }
361
362 /* Executes an END FILE transformation. */
363 static int
364 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
365                     casenumber case_num UNUSED)
366 {
367   return TRNS_END_FILE;
368 }