Apply patch #5225, assertions.
[pspp] / src / language / data-io / inpt-pgm.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <language/data-io/inpt-pgm.h>
23
24 #include <float.h>
25 #include <stdlib.h>
26
27 #include <data/case-source.h>
28 #include <data/case.h>
29 #include <data/case-source.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <data/transformations.h>
33 #include <data/variable.h>
34 #include <language/command.h>
35 #include <language/data-io/data-reader.h>
36 #include <language/data-io/file-handle.h>
37 #include <language/expressions/public.h>
38 #include <language/lexer/lexer.h>
39 #include <libpspp/alloc.h>
40 #include <libpspp/assertion.h>
41 #include <libpspp/compiler.h>
42 #include <libpspp/message.h>
43 #include <libpspp/message.h>
44 #include <libpspp/misc.h>
45 #include <libpspp/str.h>
46
47 #include "gettext.h"
48 #define _(msgid) gettext (msgid)
49
50 /* Private result codes for use within INPUT PROGRAM. */
51 enum cmd_result_extensions 
52   {
53     CMD_END_INPUT_PROGRAM = CMD_PRIVATE_FIRST,
54     CMD_END_CASE
55   };
56
57 /* Indicates how a `union value' should be initialized. */
58 enum value_init_type
59   {
60     INP_NUMERIC = 01,           /* Numeric. */
61     INP_STRING = 0,             /* String. */
62     
63     INP_INIT_ONCE = 02,         /* Initialize only once. */
64     INP_REINIT = 0,             /* Reinitialize for each iteration. */
65   };
66
67 struct input_program_pgm 
68   {
69     struct trns_chain *trns_chain;
70
71     size_t case_nr;             /* Incremented by END CASE transformation. */
72     write_case_func *write_case;/* Called by END CASE. */
73     write_case_data wc_data;    /* Aux data used by END CASE. */
74
75     enum value_init_type *init; /* How to initialize each `union value'. */
76     size_t init_cnt;            /* Number of elements in inp_init. */
77     size_t case_size;           /* Size of case in bytes. */
78   };
79
80 static void destroy_input_program (struct input_program_pgm *);
81 static trns_proc_func end_case_trns_proc;
82 static trns_proc_func reread_trns_proc;
83 static trns_proc_func end_file_trns_proc;
84 static trns_free_func reread_trns_free;
85
86 static const struct case_source_class input_program_source_class;
87
88 static bool inside_input_program;
89
90 /* Returns true if we're parsing the inside of a INPUT
91    PROGRAM...END INPUT PROGRAM construct, false otherwise. */
92 bool
93 in_input_program (void) 
94 {
95   return inside_input_program;
96 }
97
98 /* Emits an END CASE transformation for INP. */
99 static void
100 emit_END_CASE (struct input_program_pgm *inp) 
101 {
102   add_transformation (end_case_trns_proc, NULL, inp);
103 }
104
105 int
106 cmd_input_program (void)
107 {
108   struct input_program_pgm *inp;
109   size_t i;
110   bool saw_END_CASE = false;
111
112   discard_variables ();
113   if (token != '.')
114     return lex_end_of_command ();
115
116   inp = xmalloc (sizeof *inp);
117   inp->trns_chain = NULL;
118   inp->init = NULL;
119   
120   inside_input_program = true;
121   for (;;) 
122     {
123       enum cmd_result result = cmd_parse (CMD_STATE_INPUT_PROGRAM);
124       if (result == CMD_END_INPUT_PROGRAM)
125         break;
126       else if (result == CMD_END_CASE) 
127         {
128           emit_END_CASE (inp);
129           saw_END_CASE = true; 
130         }
131       else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
132         {
133           if (result == CMD_EOF)
134             msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
135           inside_input_program = false;
136           discard_variables ();
137           destroy_input_program (inp);
138           return result;
139         }
140     }
141   if (!saw_END_CASE)
142     emit_END_CASE (inp);
143   inside_input_program = false;
144
145   if (dict_get_next_value_idx (default_dict) == 0) 
146     {
147       msg (SE, _("Input program did not create any variables."));
148       discard_variables ();
149       destroy_input_program (inp);
150       return CMD_FAILURE;
151     }
152   
153   inp->trns_chain = proc_capture_transformations ();
154   trns_chain_finalize (inp->trns_chain);
155
156   /* Figure out how to initialize each input case. */
157   inp->init_cnt = dict_get_next_value_idx (default_dict);
158   inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
159   for (i = 0; i < inp->init_cnt; i++)
160     inp->init[i] = -1;
161   for (i = 0; i < dict_get_var_cnt (default_dict); i++)
162     {
163       struct variable *var = dict_get_var (default_dict, i);
164       enum value_init_type value_init;
165       size_t j;
166       
167       value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
168       value_init |= var->leave ? INP_INIT_ONCE : INP_REINIT;
169
170       for (j = 0; j < var->nv; j++)
171         inp->init[j + var->fv] = value_init;
172     }
173   for (i = 0; i < inp->init_cnt; i++)
174     assert (inp->init[i] != -1);
175   inp->case_size = dict_get_case_size (default_dict);
176
177   proc_set_source (create_case_source (&input_program_source_class, inp));
178
179   return CMD_SUCCESS;
180 }
181
182 int
183 cmd_end_input_program (void)
184 {
185   assert (in_input_program ());
186   return CMD_END_INPUT_PROGRAM; 
187 }
188
189 /* Initializes case C.  Called before the first case is read. */
190 static void
191 init_case (const struct input_program_pgm *inp, struct ccase *c)
192 {
193   size_t i;
194
195   for (i = 0; i < inp->init_cnt; i++)
196     switch (inp->init[i]) 
197       {
198       case INP_NUMERIC | INP_INIT_ONCE:
199         case_data_rw (c, i)->f = 0.0;
200         break;
201       case INP_NUMERIC | INP_REINIT:
202         case_data_rw (c, i)->f = SYSMIS;
203         break;
204       case INP_STRING | INP_INIT_ONCE:
205       case INP_STRING | INP_REINIT:
206         memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
207         break;
208       default:
209         NOT_REACHED ();
210       }
211 }
212
213 /* Clears case C.  Called between reading successive records. */
214 static void
215 clear_case (const struct input_program_pgm *inp, struct ccase *c)
216 {
217   size_t i;
218
219   for (i = 0; i < inp->init_cnt; i++)
220     switch (inp->init[i]) 
221       {
222       case INP_NUMERIC | INP_INIT_ONCE:
223         break;
224       case INP_NUMERIC | INP_REINIT:
225         case_data_rw (c, i)->f = SYSMIS;
226         break;
227       case INP_STRING | INP_INIT_ONCE:
228         break;
229       case INP_STRING | INP_REINIT:
230         memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
231         break;
232       default:
233         NOT_REACHED ();
234       }
235 }
236
237 /* Executes each transformation in turn on a `blank' case.
238    Returns true if successful, false if an I/O error occurred. */
239 static bool
240 input_program_source_read (struct case_source *source,
241                            struct ccase *c,
242                            write_case_func *write_case,
243                            write_case_data wc_data)
244 {
245   struct input_program_pgm *inp = source->aux;
246
247   inp->case_nr = 1;
248   inp->write_case = write_case;
249   inp->wc_data = wc_data;
250   for (init_case (inp, c); ; clear_case (inp, c))
251     {
252       enum trns_result result = trns_chain_execute (inp->trns_chain, c,
253                                                     &inp->case_nr);
254       if (result == TRNS_ERROR)
255         return false;
256       else if (result == TRNS_END_FILE)
257         return true;
258     }
259 }
260
261 static void
262 destroy_input_program (struct input_program_pgm *pgm) 
263 {
264   if (pgm != NULL) 
265     {
266       trns_chain_destroy (pgm->trns_chain);
267       free (pgm->init);
268       free (pgm);
269     }
270 }
271
272 /* Destroys an INPUT PROGRAM source. */
273 static void
274 input_program_source_destroy (struct case_source *source)
275 {
276   struct input_program_pgm *inp = source->aux;
277
278   destroy_input_program (inp);
279 }
280
281 static const struct case_source_class input_program_source_class =
282   {
283     "INPUT PROGRAM",
284     NULL,
285     input_program_source_read,
286     input_program_source_destroy,
287   };
288 \f
289 int
290 cmd_end_case (void)
291 {
292   assert (in_input_program ());
293   if (token == '.')
294     return CMD_END_CASE;
295   return lex_end_of_command ();
296 }
297
298 /* Sends the current case as the source's output. */
299 int
300 end_case_trns_proc (void *inp_, struct ccase *c, int case_nr UNUSED)
301 {
302   struct input_program_pgm *inp = inp_;
303
304   if (!inp->write_case (inp->wc_data))
305     return TRNS_ERROR;
306
307   inp->case_nr++;
308   clear_case (inp, c);
309   return TRNS_CONTINUE;
310 }
311
312 /* REREAD transformation. */
313 struct reread_trns
314   {
315     struct dfm_reader *reader;  /* File to move file pointer back on. */
316     struct expression *column;  /* Column to reset file pointer to. */
317   };
318
319 /* Parses REREAD command. */
320 int
321 cmd_reread (void)
322 {
323   struct file_handle *fh;       /* File to be re-read. */
324   struct expression *e;         /* Expression for column to set. */
325   struct reread_trns *t;        /* Created transformation. */
326
327   fh = fh_get_default_handle ();
328   e = NULL;
329   while (token != '.')
330     {
331       if (lex_match_id ("COLUMN"))
332         {
333           lex_match ('=');
334           
335           if (e)
336             {
337               msg (SE, _("COLUMN subcommand multiply specified."));
338               expr_free (e);
339               return CMD_CASCADING_FAILURE;
340             }
341           
342           e = expr_parse (default_dict, EXPR_NUMBER);
343           if (!e)
344             return CMD_CASCADING_FAILURE;
345         }
346       else if (lex_match_id ("FILE"))
347         {
348           lex_match ('=');
349           fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
350           if (fh == NULL)
351             {
352               expr_free (e);
353               return CMD_CASCADING_FAILURE;
354             }
355           lex_get ();
356         }
357       else
358         {
359           lex_error (NULL);
360           expr_free (e);
361         }
362     }
363
364   t = xmalloc (sizeof *t);
365   t->reader = dfm_open_reader (fh);
366   t->column = e;
367   add_transformation (reread_trns_proc, reread_trns_free, t);
368
369   return CMD_SUCCESS;
370 }
371
372 /* Executes a REREAD transformation. */
373 static int
374 reread_trns_proc (void *t_, struct ccase *c, int case_num)
375 {
376   struct reread_trns *t = t_;
377
378   if (t->column == NULL)
379     dfm_reread_record (t->reader, 1);
380   else
381     {
382       double column = expr_evaluate_num (t->column, c, case_num);
383       if (!finite (column) || column < 1)
384         {
385           msg (SE, _("REREAD: Column numbers must be positive finite "
386                "numbers.  Column set to 1."));
387           dfm_reread_record (t->reader, 1);
388         }
389       else
390         dfm_reread_record (t->reader, column);
391     }
392   return TRNS_CONTINUE;
393 }
394
395 /* Frees a REREAD transformation.
396    Returns true if successful, false if an I/O error occurred. */
397 static bool
398 reread_trns_free (void *t_)
399 {
400   struct reread_trns *t = t_;
401   expr_free (t->column);
402   dfm_close_reader (t->reader);
403   return true;
404 }
405
406 /* Parses END FILE command. */
407 int
408 cmd_end_file (void)
409 {
410   assert (in_input_program ());
411
412   add_transformation (end_file_trns_proc, NULL, NULL);
413
414   return lex_end_of_command ();
415 }
416
417 /* Executes an END FILE transformation. */
418 static int
419 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
420                     int case_num UNUSED)
421 {
422   return TRNS_END_FILE;
423 }