starting to look like it works
[pspp] / src / language / data-io / inpt-pgm.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013 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 <float.h>
20 #include <stdlib.h>
21
22 #include "data/case.h"
23 #include "data/caseinit.h"
24 #include "data/casereader-provider.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/session.h"
28 #include "data/transformations.h"
29 #include "data/variable.h"
30 #include "language/command.h"
31 #include "language/data-io/data-reader.h"
32 #include "language/data-io/file-handle.h"
33 #include "language/data-io/inpt-pgm.h"
34 #include "language/expressions/public.h"
35 #include "language/lexer/lexer.h"
36 #include "libpspp/assertion.h"
37 #include "libpspp/compiler.h"
38 #include "libpspp/message.h"
39 #include "libpspp/misc.h"
40 #include "libpspp/str.h"
41
42 #include "gl/xalloc.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46
47 /* Indicates how a `union value' should be initialized. */
48 struct input_program_pgm
49   {
50     struct session *session;
51     struct dataset *ds;
52
53     struct trns_chain xforms;
54     size_t idx;
55     bool eof;
56
57     casenumber case_nr;             /* Incremented by END CASE transformation. */
58
59     struct caseinit *init;
60     struct caseproto *proto;
61   };
62
63 static void destroy_input_program (struct input_program_pgm *);
64 static const struct trns_class end_case_trns_class;
65 static const struct trns_class reread_trns_class;
66 static const struct trns_class end_file_trns_class;
67
68 static const struct casereader_class input_program_casereader_class;
69
70 static bool inside_input_program;
71 static bool saw_END_CASE;
72 static bool saw_END_FILE;
73 static bool saw_DATA_LIST;
74
75 /* Returns true if we're parsing the inside of a INPUT
76    PROGRAM...END INPUT PROGRAM construct, false otherwise. */
77 bool
78 in_input_program (void)
79 {
80   return inside_input_program;
81 }
82
83 /* Emits an END CASE transformation for INP. */
84 static void
85 emit_END_CASE (struct dataset *ds)
86 {
87   add_transformation (ds, &end_case_trns_class, NULL);
88 }
89
90 int
91 cmd_input_program (struct lexer *lexer, struct dataset *ds)
92 {
93   if (!lex_match (lexer, T_ENDCMD))
94     return lex_end_of_command (lexer);
95
96   struct session *session = session_create (dataset_session (ds));
97   struct dataset *inp_ds = dataset_create (session, "INPUT PROGRAM");
98
99   struct input_program_pgm *inp = xmalloc (sizeof *inp);
100   *inp = (struct input_program_pgm) { .session = session, .ds = inp_ds };
101
102   proc_push_transformations (inp->ds);
103   inside_input_program = true;
104   saw_END_CASE = saw_END_FILE = saw_DATA_LIST = false;
105   while (!lex_match_phrase (lexer, "END INPUT PROGRAM"))
106     {
107       enum cmd_result result;
108
109       result = cmd_parse_in_state (lexer, inp->ds, CMD_STATE_INPUT_PROGRAM);
110       if (result != CMD_FAILURE
111           && cmd_result_is_failure (result)
112           && lex_get_error_mode (lexer) != LEX_ERROR_TERMINAL)
113         {
114           proc_pop_transformations (ds, &inp->xforms);
115
116           if (result == CMD_EOF)
117             msg (SE, _("Unexpected end-of-file within %s."), "INPUT PROGRAM");
118           inside_input_program = false;
119           destroy_input_program (inp);
120           return result;
121         }
122     }
123   if (!saw_END_CASE)
124     emit_END_CASE (inp->ds);
125   inside_input_program = false;
126   proc_pop_transformations (inp->ds, &inp->xforms);
127
128   if (!saw_DATA_LIST && !saw_END_FILE)
129     {
130       msg (SE, _("Input program must contain %s or %s."), "DATA LIST", "END FILE");
131       destroy_input_program (inp);
132       return CMD_FAILURE;
133     }
134   if (dict_get_next_value_idx (dataset_dict (inp->ds)) == 0)
135     {
136       msg (SE, _("Input program did not create any variables."));
137       destroy_input_program (inp);
138       return CMD_FAILURE;
139     }
140
141   /* Figure out how to initialize each input case. */
142   inp->init = caseinit_create ();
143   caseinit_mark_for_init (inp->init, dataset_dict (inp->ds));
144   inp->proto = caseproto_ref (dict_get_proto (dataset_dict (inp->ds)));
145
146   dataset_set_dict (ds, dict_clone (dataset_dict (inp->ds)));
147   dataset_set_source (
148     ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
149                                       &input_program_casereader_class, inp));
150
151   return CMD_SUCCESS;
152 }
153
154 /* Reads and returns one case.
155    Returns the case if successful, null at end of file or if an
156    I/O error occurred. */
157 static struct ccase *
158 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
159 {
160   struct input_program_pgm *inp = inp_;
161
162   if (inp->eof || !inp->xforms.n)
163     return NULL;
164
165   struct ccase *c = case_create (inp->proto);
166   caseinit_init_vars (inp->init, c);
167
168   printf ("%s:%d\n", __FILE__, __LINE__);
169   for (size_t i = inp->idx < inp->xforms.n ? inp->idx : 0; ; i++)
170     {
171       printf ("%s:%d %zu\n", __FILE__, __LINE__, i);
172       if (i >= inp->xforms.n)
173         {
174           i = 0;
175           c = case_unshare (c);
176           caseinit_update_left_vars (inp->init, c);
177           caseinit_init_vars (inp->init, c);
178         }
179
180       const struct transformation *trns = &inp->xforms.xforms[i];
181       switch (trns->class->execute (trns->aux, &c, inp->case_nr))
182         {
183         case TRNS_END_CASE:
184           printf ("END CASE\n");
185           inp->case_nr++;
186           inp->idx = i + 1;
187           return c;
188
189         case TRNS_ERROR:
190           printf ("ERROR\n");
191           casereader_force_error (reader);
192           /* Fall through. */
193         case TRNS_END_FILE:
194           printf ("END FILE\n");
195           inp->eof = true;
196           case_unref (c);
197           return NULL;
198
199         case TRNS_CONTINUE:
200           printf ("CONTINUE\n");
201           break;
202
203         default:
204           NOT_REACHED ();
205         }
206     }
207 }
208
209 static void
210 destroy_input_program (struct input_program_pgm *pgm)
211 {
212   if (pgm != NULL)
213     {
214       session_destroy (pgm->session);
215       trns_chain_uninit (&pgm->xforms);
216       caseinit_destroy (pgm->init);
217       caseproto_unref (pgm->proto);
218       free (pgm);
219     }
220 }
221
222 /* Destroys the casereader. */
223 static void
224 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
225 {
226   struct input_program_pgm *inp = inp_;
227   destroy_input_program (inp);
228 }
229
230 static const struct casereader_class input_program_casereader_class =
231   {
232     input_program_casereader_read,
233     input_program_casereader_destroy,
234     NULL,
235     NULL,
236   };
237 \f
238 int
239 cmd_end_case (struct lexer *lexer UNUSED, struct dataset *ds)
240 {
241   assert (in_input_program ());
242   emit_END_CASE (ds);
243   saw_END_CASE = true;
244   return CMD_SUCCESS;
245 }
246
247 /* Outputs the current case */
248 static enum trns_result
249 end_case_trns_proc (void *aux UNUSED, struct ccase **c UNUSED,
250                     casenumber case_nr UNUSED)
251 {
252   printf ("%s:%d\n", __FILE__, __LINE__);
253   return TRNS_END_CASE;
254 }
255
256 static const struct trns_class end_case_trns_class = {
257   .name = "END CASE",
258   .execute = end_case_trns_proc,
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   char *encoding = NULL;
276
277   fh = fh_get_default_handle ();
278   e = NULL;
279   while (lex_token (lexer) != T_ENDCMD)
280     {
281       if (lex_match_id (lexer, "COLUMN"))
282         {
283           lex_match (lexer, T_EQUALS);
284
285           if (e)
286             {
287               lex_sbc_only_once ("COLUMN");
288               goto error;
289             }
290
291           e = expr_parse (lexer, ds, VAL_NUMERIC);
292           if (!e)
293             goto error;
294         }
295       else if (lex_match_id (lexer, "FILE"))
296         {
297           lex_match (lexer, T_EQUALS);
298           fh_unref (fh);
299           fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
300           if (fh == NULL)
301             goto error;
302         }
303       else if (lex_match_id (lexer, "ENCODING"))
304         {
305           lex_match (lexer, T_EQUALS);
306           if (!lex_force_string (lexer))
307             goto error;
308
309           free (encoding);
310           encoding = ss_xstrdup (lex_tokss (lexer));
311
312           lex_get (lexer);
313         }
314       else
315         {
316           lex_error (lexer, NULL);
317           goto error;
318         }
319     }
320
321   t = xmalloc (sizeof *t);
322   t->reader = dfm_open_reader (fh, lexer, encoding);
323   t->column = e;
324   add_transformation (ds, &reread_trns_class, t);
325
326   fh_unref (fh);
327   free (encoding);
328   return CMD_SUCCESS;
329
330 error:
331   expr_free (e);
332   free (encoding);
333   return CMD_CASCADING_FAILURE;
334 }
335
336 /* Executes a REREAD transformation. */
337 static enum trns_result
338 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
339 {
340   struct reread_trns *t = t_;
341
342   if (t->column == NULL)
343     dfm_reread_record (t->reader, 1);
344   else
345     {
346       double column = expr_evaluate_num (t->column, *c, case_num);
347       if (!isfinite (column) || column < 1)
348         {
349           msg (SE, _("REREAD: Column numbers must be positive finite "
350                "numbers.  Column set to 1."));
351           dfm_reread_record (t->reader, 1);
352         }
353       else
354         dfm_reread_record (t->reader, column);
355     }
356   return TRNS_CONTINUE;
357 }
358
359 /* Frees a REREAD transformation.
360    Returns true if successful, false if an I/O error occurred. */
361 static bool
362 reread_trns_free (void *t_)
363 {
364   struct reread_trns *t = t_;
365   expr_free (t->column);
366   dfm_close_reader (t->reader);
367   return true;
368 }
369
370 static const struct trns_class reread_trns_class = {
371   .name = "REREAD",
372   .execute = reread_trns_proc,
373   .destroy = reread_trns_free,
374 };
375
376 /* Parses END FILE command. */
377 int
378 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
379 {
380   assert (in_input_program ());
381
382   add_transformation (ds, &end_file_trns_class, NULL);
383   saw_END_FILE = true;
384
385   return CMD_SUCCESS;
386 }
387
388 /* Executes an END FILE transformation. */
389 static enum trns_result
390 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
391                     casenumber case_num UNUSED)
392 {
393   return TRNS_END_FILE;
394 }
395
396 static const struct trns_class end_file_trns_class = {
397   .name = "END FILE",
398   .execute = end_file_trns_proc,
399 };