Fix memory leaks.
[pspp-builds.git] / src / 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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include "error.h"
22 #include <float.h>
23 #include <stdlib.h>
24 #include "alloc.h"
25 #include "case.h"
26 #include "command.h"
27 #include "data-list.h"
28 #include "dfm.h"
29 #include "error.h"
30 #include "expr.h"
31 #include "file-handle.h"
32 #include "lexer.h"
33 #include "misc.h"
34 #include "str.h"
35 #include "var.h"
36 #include "vfm.h"
37
38 #include "debug-print.h"
39
40 /* Indicates how a `union value' should be initialized. */
41 enum value_init_type
42   {
43     INP_NUMERIC = 01,           /* Numeric. */
44     INP_STRING = 0,             /* String. */
45     
46     INP_INIT_ONCE = 02,         /* Initialize only once. */
47     INP_REINIT = 0,             /* Reinitialize for each iteration. */
48   };
49
50 struct input_program_pgm 
51   {
52     enum value_init_type *init; /* How to initialize each `union value'. */
53     size_t init_cnt;            /* Number of elements in inp_init. */
54     size_t case_size;           /* Size of case in bytes. */
55   };
56
57 static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
58 static trns_free_func reread_trns_free;
59
60 int
61 cmd_input_program (void)
62 {
63   discard_variables ();
64
65   /* FIXME: we shouldn't do this here, but I'm afraid that other
66      code will check the class of vfm_source. */
67   vfm_source = create_case_source (&input_program_source_class,
68                                    default_dict, NULL);
69
70   return lex_end_of_command ();
71 }
72
73 int
74 cmd_end_input_program (void)
75 {
76   struct input_program_pgm *inp;
77   size_t i;
78
79   if (!case_source_is_class (vfm_source, &input_program_source_class))
80     {
81       msg (SE, _("No matching INPUT PROGRAM command."));
82       return CMD_FAILURE;
83     }
84   
85   if (dict_get_next_value_idx (default_dict) == 0)
86     msg (SW, _("No data-input or transformation commands specified "
87          "between INPUT PROGRAM and END INPUT PROGRAM."));
88
89   /* Mark the boundary between INPUT PROGRAM transformations and
90      ordinary transformations. */
91   f_trns = n_trns;
92
93   /* Figure out how to initialize each input case. */
94   inp = xmalloc (sizeof *inp);
95   inp->init_cnt = dict_get_next_value_idx (default_dict);
96   inp->init = xmalloc (inp->init_cnt * sizeof *inp->init);
97   for (i = 0; i < inp->init_cnt; i++)
98     inp->init[i] = -1;
99   for (i = 0; i < dict_get_var_cnt (default_dict); i++)
100     {
101       struct variable *var = dict_get_var (default_dict, i);
102       enum value_init_type value_init;
103       size_t j;
104       
105       value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
106       value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
107
108       for (j = 0; j < var->nv; j++)
109         inp->init[j + var->fv] = value_init;
110     }
111   for (i = 0; i < inp->init_cnt; i++)
112     assert (inp->init[i] != -1);
113   inp->case_size = dict_get_case_size (default_dict);
114
115   /* Put inp into vfm_source for later use. */
116   vfm_source->aux = inp;
117
118   /* FIXME: we should use create_case_source() here. */
119   vfm_source->value_cnt = dict_get_next_value_idx (default_dict);
120
121   return lex_end_of_command ();
122 }
123
124 /* Initializes case C.  Called before the first case is read. */
125 static void
126 init_case (const struct input_program_pgm *inp, struct ccase *c)
127 {
128   size_t i;
129
130   for (i = 0; i < inp->init_cnt; i++)
131     switch (inp->init[i]) 
132       {
133       case INP_NUMERIC | INP_INIT_ONCE:
134         case_data_rw (c, i)->f = 0.0;
135         break;
136       case INP_NUMERIC | INP_REINIT:
137         case_data_rw (c, i)->f = SYSMIS;
138         break;
139       case INP_STRING | INP_INIT_ONCE:
140       case INP_STRING | INP_REINIT:
141         memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
142         break;
143       default:
144         assert (0);
145       }
146 }
147
148 /* Clears case C.  Called between reading successive records. */
149 static void
150 clear_case (const struct input_program_pgm *inp, struct ccase *c)
151 {
152   size_t i;
153
154   for (i = 0; i < inp->init_cnt; i++)
155     switch (inp->init[i]) 
156       {
157       case INP_NUMERIC | INP_INIT_ONCE:
158         break;
159       case INP_NUMERIC | INP_REINIT:
160         case_data_rw (c, i)->f = SYSMIS;
161         break;
162       case INP_STRING | INP_INIT_ONCE:
163         break;
164       case INP_STRING | INP_REINIT:
165         memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
166         break;
167       default:
168         assert (0);
169       }
170 }
171
172 /* Executes each transformation in turn on a `blank' case.  When a
173    transformation fails, returning -2, then that's the end of the
174    file.  -1 means go on to the next transformation.  Otherwise the
175    return value is the index of the transformation to go to next. */
176 static void
177 input_program_source_read (struct case_source *source,
178                            struct ccase *c,
179                            write_case_func *write_case,
180                            write_case_data wc_data)
181 {
182   struct input_program_pgm *inp = source->aux;
183   int i;
184
185   /* Nonzero if there were any END CASE commands in the set of
186      transformations.  If so, we don't automatically write out
187      cases. */
188   int end_case = 0;
189
190   /* FIXME?  This is the number of cases sent out of the input
191      program, not the number of cases written to the procedure.
192      The difference should only show up in $CASENUM in COMPUTE.
193      We should check behavior against SPSS. */
194   int cases_written = 0;
195
196   assert (inp != NULL);
197
198   /* Figure end_case. */
199   for (i = 0; i < f_trns; i++)
200     if (t_trns[i]->proc == end_case_trns_proc)
201       end_case = 1;
202
203   /* FIXME: This is an ugly kluge. */
204   for (i = 0; i < f_trns; i++)
205     if (t_trns[i]->proc == repeating_data_trns_proc)
206       repeating_data_set_write_case (t_trns[i], write_case, wc_data);
207
208   init_case (inp, c);
209   for (;;)
210     {
211       /* Perform transformations on `blank' case. */
212       for (i = 0; i < f_trns; )
213         {
214           int code;     /* Return value of last-called transformation. */
215
216           if (t_trns[i]->proc == end_case_trns_proc) 
217             {
218               cases_written++;
219               if (!write_case (wc_data))
220                 goto done;
221               clear_case (inp, c);
222               i++;
223               continue;
224             }
225
226           code = t_trns[i]->proc (t_trns[i], c, cases_written + 1);
227           switch (code)
228             {
229             case -1:
230               i++;
231               break;
232             case -2:
233               goto done;
234             case -3:
235               goto next_case;
236             default:
237               i = code;
238               break;
239             }
240         }
241
242       /* Write the case if appropriate. */
243       if (!end_case) 
244         {
245           cases_written++;
246           if (!write_case (wc_data))
247             break;
248         }
249
250       /* Blank out the case for the next iteration. */
251     next_case:
252       clear_case (inp, c);
253     }
254  done: ;
255 }
256
257 /* Destroys an INPUT PROGRAM source. */
258 static void
259 input_program_source_destroy (struct case_source *source)
260 {
261   struct input_program_pgm *inp = source->aux;
262
263   cancel_transformations ();
264
265   if (inp != NULL) 
266     {
267       free (inp->init);
268       free (inp);
269     }
270 }
271
272 const struct case_source_class input_program_source_class =
273   {
274     "INPUT PROGRAM",
275     NULL,
276     input_program_source_read,
277     input_program_source_destroy,
278   };
279 \f
280 int
281 cmd_end_case (void)
282 {
283   struct trns_header *t;
284
285   if (!case_source_is_class (vfm_source, &input_program_source_class))
286     {
287       msg (SE, _("This command may only be executed between INPUT PROGRAM "
288                  "and END INPUT PROGRAM."));
289       return CMD_FAILURE;
290     }
291
292   t = xmalloc (sizeof *t);
293   t->proc = end_case_trns_proc;
294   t->free = NULL;
295   add_transformation ((struct trns_header *) t);
296
297   return lex_end_of_command ();
298 }
299
300 /* Should never be called, because this is handled in
301    input_program_source_read(). */
302 int
303 end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED,
304                     int case_num UNUSED)
305 {
306   assert (0);
307   abort ();
308 }
309
310 /* REREAD transformation. */
311 struct reread_trns
312   {
313     struct trns_header h;
314
315     struct file_handle *handle; /* 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   /* File to be re-read. */
324   struct file_handle *h;
325   
326   /* Expression for column to set file pointer to. */
327   struct expression *e;
328
329   /* Created transformation. */
330   struct reread_trns *t;
331
332   h = default_handle;
333   e = NULL;
334   while (token != '.')
335     {
336       if (lex_match_id ("COLUMN"))
337         {
338           lex_match ('=');
339           
340           if (e)
341             {
342               msg (SE, _("COLUMN subcommand multiply specified."));
343               expr_free (e);
344               return CMD_FAILURE;
345             }
346           
347           e = expr_parse (EXPR_NUMERIC);
348           if (!e)
349             return CMD_FAILURE;
350         }
351       else if (lex_match_id ("FILE"))
352         {
353           lex_match ('=');
354           h = fh_parse_file_handle ();
355           if (h == NULL)
356             {
357               expr_free (e);
358               return CMD_FAILURE;
359             }
360           lex_get ();
361         }
362       else
363         {
364           lex_error (NULL);
365           expr_free (e);
366         }
367     }
368
369   t = xmalloc (sizeof *t);
370   t->h.proc = reread_trns_proc;
371   t->h.free = reread_trns_free;
372   t->handle = h;
373   t->column = e;
374   add_transformation ((struct trns_header *) t);
375
376   return CMD_SUCCESS;
377 }
378
379 /* Executes a REREAD transformation. */
380 static int
381 reread_trns_proc (struct trns_header * pt, struct ccase * c,
382                   int case_num)
383 {
384   struct reread_trns *t = (struct reread_trns *) pt;
385
386   if (t->column == NULL)
387     dfm_reread_record (t->handle, 1);
388   else
389     {
390       union value column;
391
392       expr_evaluate (t->column, c, case_num, &column);
393       if (!finite (column.f) || column.f < 1)
394         {
395           msg (SE, _("REREAD: Column numbers must be positive finite "
396                "numbers.  Column set to 1."));
397           dfm_reread_record (t->handle, 1);
398         }
399       else
400         dfm_reread_record (t->handle, column.f);
401     }
402   return -1;
403 }
404
405 /* Frees a REREAD transformation. */
406 static void
407 reread_trns_free (struct trns_header * t)
408 {
409   expr_free (((struct reread_trns *) t)->column);
410 }
411
412 /* Parses END FILE command. */
413 int
414 cmd_end_file (void)
415 {
416   struct trns_header *t;
417
418   if (!case_source_is_class (vfm_source, &input_program_source_class))
419     {
420       msg (SE, _("This command may only be executed between INPUT PROGRAM "
421                  "and END INPUT PROGRAM."));
422       return CMD_FAILURE;
423     }
424
425   t = xmalloc (sizeof *t);
426   t->proc = end_file_trns_proc;
427   t->free = NULL;
428   add_transformation ((struct trns_header *) t);
429
430   return lex_end_of_command ();
431 }
432
433 /* Executes an END FILE transformation. */
434 static int
435 end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED,
436                     int case_num UNUSED)
437 {
438   return -2;
439 }