1c2625a24383d193dca4d2f000d273b0e06232b1
[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
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <language/data-io/inpt-pgm.h>
22
23 #include <float.h>
24 #include <stdlib.h>
25
26 #include <data/case.h>
27 #include <data/caseinit.h>
28 #include <data/casereader-provider.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/transformations.h>
32 #include <data/variable.h>
33 #include <language/command.h>
34 #include <language/data-io/data-reader.h>
35 #include <language/data-io/file-handle.h>
36 #include <language/expressions/public.h>
37 #include <language/lexer/lexer.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/assertion.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     enum trns_result restart;
70
71     casenumber case_nr;             /* Incremented by END CASE transformation. */
72
73     struct caseinit *init;
74     size_t value_cnt;
75   };
76
77 static void destroy_input_program (struct input_program_pgm *);
78 static trns_proc_func end_case_trns_proc;
79 static trns_proc_func reread_trns_proc;
80 static trns_proc_func end_file_trns_proc;
81 static trns_free_func reread_trns_free;
82
83 static const struct casereader_class input_program_casereader_class;
84
85 static bool inside_input_program;
86
87 /* Returns true if we're parsing the inside of a INPUT
88    PROGRAM...END INPUT PROGRAM construct, false otherwise. */
89 bool
90 in_input_program (void)
91 {
92   return inside_input_program;
93 }
94
95 /* Emits an END CASE transformation for INP. */
96 static void
97 emit_END_CASE (struct dataset *ds, struct input_program_pgm *inp)
98 {
99   add_transformation (ds, end_case_trns_proc, NULL, inp);
100 }
101
102 int
103 cmd_input_program (struct lexer *lexer, struct dataset *ds)
104 {
105   struct input_program_pgm *inp;
106   bool saw_END_CASE = false;
107
108   proc_discard_active_file (ds);
109   if (lex_token (lexer) != '.')
110     return lex_end_of_command (lexer);
111
112   inp = xmalloc (sizeof *inp);
113   inp->trns_chain = NULL;
114   inp->init = NULL;
115
116   inside_input_program = true;
117   for (;;)
118     {
119       enum cmd_result result = cmd_parse_in_state (lexer, ds, CMD_STATE_INPUT_PROGRAM);
120       if (result == CMD_END_INPUT_PROGRAM)
121         break;
122       else if (result == CMD_END_CASE)
123         {
124           emit_END_CASE (ds, inp);
125           saw_END_CASE = true;
126         }
127       else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
128         {
129           if (result == CMD_EOF)
130             msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
131           inside_input_program = false;
132           proc_discard_active_file (ds);
133           destroy_input_program (inp);
134           return result;
135         }
136     }
137   if (!saw_END_CASE)
138     emit_END_CASE (ds, inp);
139   inside_input_program = false;
140
141   if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
142     {
143       msg (SE, _("Input program did not create any variables."));
144       proc_discard_active_file (ds);
145       destroy_input_program (inp);
146       return CMD_FAILURE;
147     }
148
149   inp->trns_chain = proc_capture_transformations (ds);
150   trns_chain_finalize (inp->trns_chain);
151
152   inp->restart = TRNS_CONTINUE;
153
154   /* Figure out how to initialize each input case. */
155   inp->init = caseinit_create ();
156   caseinit_mark_for_init (inp->init, dataset_dict (ds));
157   inp->value_cnt = dict_get_next_value_idx (dataset_dict (ds));
158
159   proc_set_active_file_data (
160     ds, casereader_create_sequential (NULL, inp->value_cnt, CASENUMBER_MAX,
161                                       &input_program_casereader_class, inp));
162
163   return CMD_SUCCESS;
164 }
165
166 int
167 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
168 {
169   assert (in_input_program ());
170   return CMD_END_INPUT_PROGRAM;
171 }
172
173 /* Returns true if STATE is valid given the transformations that
174    are allowed within INPUT PROGRAM. */
175 static bool
176 is_valid_state (enum trns_result state)
177 {
178   return (state == TRNS_CONTINUE
179           || state == TRNS_ERROR
180           || state == TRNS_END_FILE
181           || state >= 0);
182 }
183
184 /* Reads one case into C.
185    Returns true if successful, false at end of file or if an
186    I/O error occurred. */
187 static bool
188 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_,
189                                struct ccase *c)
190 {
191   struct input_program_pgm *inp = inp_;
192
193   case_create (c, inp->value_cnt);
194
195   do
196     {
197       assert (is_valid_state (inp->restart));
198       if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
199         {
200           case_destroy (c);
201           return false;
202         }
203
204       caseinit_init_vars (inp->init, c);
205       inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
206                                          c, inp->case_nr);
207       assert (is_valid_state (inp->restart));
208       caseinit_update_left_vars (inp->init, c);
209     }
210   while (inp->restart < 0);
211
212   return true;
213 }
214
215 static void
216 destroy_input_program (struct input_program_pgm *pgm)
217 {
218   if (pgm != NULL)
219     {
220       trns_chain_destroy (pgm->trns_chain);
221       caseinit_destroy (pgm->init);
222       free (pgm);
223     }
224 }
225
226 /* Destroys the casereader. */
227 static void
228 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
229 {
230   struct input_program_pgm *inp = inp_;
231   if (inp->restart == TRNS_ERROR)
232     casereader_force_error (reader);
233   destroy_input_program (inp);
234 }
235
236 static const struct casereader_class input_program_casereader_class =
237   {
238     input_program_casereader_read,
239     input_program_casereader_destroy,
240     NULL,
241     NULL,
242   };
243 \f
244 int
245 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
246 {
247   assert (in_input_program ());
248   if (lex_token (lexer) == '.')
249     return CMD_END_CASE;
250   return lex_end_of_command (lexer);
251 }
252
253 /* Outputs the current case */
254 int
255 end_case_trns_proc (void *inp_, struct ccase *c UNUSED,
256                     casenumber case_nr UNUSED)
257 {
258   struct input_program_pgm *inp = inp_;
259   inp->case_nr++;
260   return TRNS_END_CASE;
261 }
262
263 /* REREAD transformation. */
264 struct reread_trns
265   {
266     struct dfm_reader *reader;  /* File to move file pointer back on. */
267     struct expression *column;  /* Column to reset file pointer to. */
268   };
269
270 /* Parses REREAD command. */
271 int
272 cmd_reread (struct lexer *lexer, struct dataset *ds)
273 {
274   struct file_handle *fh;       /* File to be re-read. */
275   struct expression *e;         /* Expression for column to set. */
276   struct reread_trns *t;        /* Created transformation. */
277
278   fh = fh_get_default_handle ();
279   e = NULL;
280   while (lex_token (lexer) != '.')
281     {
282       if (lex_match_id (lexer, "COLUMN"))
283         {
284           lex_match (lexer, '=');
285
286           if (e)
287             {
288               msg (SE, _("COLUMN subcommand multiply specified."));
289               expr_free (e);
290               return CMD_CASCADING_FAILURE;
291             }
292
293           e = expr_parse (lexer, ds, EXPR_NUMBER);
294           if (!e)
295             return CMD_CASCADING_FAILURE;
296         }
297       else if (lex_match_id (lexer, "FILE"))
298         {
299           lex_match (lexer, '=');
300           fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
301           if (fh == NULL)
302             {
303               expr_free (e);
304               return CMD_CASCADING_FAILURE;
305             }
306         }
307       else
308         {
309           lex_error (lexer, NULL);
310           expr_free (e);
311           return CMD_CASCADING_FAILURE;
312         }
313     }
314
315   t = xmalloc (sizeof *t);
316   t->reader = dfm_open_reader (fh, lexer);
317   t->column = e;
318   add_transformation (ds, reread_trns_proc, reread_trns_free, t);
319
320   return CMD_SUCCESS;
321 }
322
323 /* Executes a REREAD transformation. */
324 static int
325 reread_trns_proc (void *t_, struct ccase *c, casenumber case_num)
326 {
327   struct reread_trns *t = t_;
328
329   if (t->column == NULL)
330     dfm_reread_record (t->reader, 1);
331   else
332     {
333       double column = expr_evaluate_num (t->column, c, case_num);
334       if (!finite (column) || column < 1)
335         {
336           msg (SE, _("REREAD: Column numbers must be positive finite "
337                "numbers.  Column set to 1."));
338           dfm_reread_record (t->reader, 1);
339         }
340       else
341         dfm_reread_record (t->reader, column);
342     }
343   return TRNS_CONTINUE;
344 }
345
346 /* Frees a REREAD transformation.
347    Returns true if successful, false if an I/O error occurred. */
348 static bool
349 reread_trns_free (void *t_)
350 {
351   struct reread_trns *t = t_;
352   expr_free (t->column);
353   dfm_close_reader (t->reader);
354   return true;
355 }
356
357 /* Parses END FILE command. */
358 int
359 cmd_end_file (struct lexer *lexer, struct dataset *ds)
360 {
361   assert (in_input_program ());
362
363   add_transformation (ds, end_file_trns_proc, NULL, NULL);
364
365   return lex_end_of_command (lexer);
366 }
367
368 /* Executes an END FILE transformation. */
369 static int
370 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
371                     casenumber case_num UNUSED)
372 {
373   return TRNS_END_FILE;
374 }