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