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