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