MATRIX: Fix multiplication of empty matrices.
[pspp] / src / language / commands / 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/commands/data-reader.h"
32 #include "language/commands/file-handle.h"
33 #include "language/commands/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 void
84 data_list_seen (void)
85 {
86   saw_DATA_LIST = true;
87 }
88
89 /* Emits an END CASE transformation for INP. */
90 static void
91 emit_END_CASE (struct dataset *ds)
92 {
93   add_transformation (ds, &end_case_trns_class, xzalloc (sizeof (bool)));
94 }
95
96 int
97 cmd_input_program (struct lexer *lexer, struct dataset *ds)
98 {
99   struct msg_location *location = lex_ofs_location (lexer, 0, 1);
100   if (!lex_match (lexer, T_ENDCMD))
101     {
102       msg_location_destroy (location);
103       return lex_end_of_command (lexer);
104     }
105
106   struct session *session = session_create (dataset_session (ds));
107   struct dataset *inp_ds = dataset_create (session, "INPUT PROGRAM");
108
109   struct input_program_pgm *inp = xmalloc (sizeof *inp);
110   *inp = (struct input_program_pgm) { .session = session, .ds = inp_ds };
111
112   proc_push_transformations (inp->ds);
113   inside_input_program = true;
114   saw_END_CASE = saw_END_FILE = saw_DATA_LIST = false;
115   while (!lex_match_phrase (lexer, "END INPUT PROGRAM"))
116     {
117       enum cmd_result result;
118
119       result = cmd_parse_in_state (lexer, inp->ds, CMD_STATE_INPUT_PROGRAM);
120       if (result == CMD_EOF
121           || result == CMD_FINISH
122           || result == CMD_CASCADING_FAILURE)
123         {
124           proc_pop_transformations (inp->ds, &inp->xforms);
125
126           if (result == CMD_EOF)
127             msg (SE, _("Unexpected end-of-file within %s."), "INPUT PROGRAM");
128           inside_input_program = false;
129           destroy_input_program (inp);
130           msg_location_destroy (location);
131           return result;
132         }
133     }
134   if (!saw_END_CASE)
135     emit_END_CASE (inp->ds);
136   inside_input_program = false;
137   proc_pop_transformations (inp->ds, &inp->xforms);
138
139   struct msg_location *end = lex_ofs_location (lexer, 0, 2);
140   msg_location_merge (&location, end);
141   location->omit_underlines = true;
142   msg_location_destroy (end);
143
144   if (!saw_DATA_LIST && !saw_END_FILE)
145     {
146       msg_at (SE, location, _("Input program does not contain %s or %s."),
147               "DATA LIST", "END FILE");
148       destroy_input_program (inp);
149       msg_location_destroy (location);
150       return CMD_FAILURE;
151     }
152   if (dict_get_n_vars (dataset_dict (inp->ds)) == 0)
153     {
154       msg_at (SE, location, _("Input program did not create any variables."));
155       destroy_input_program (inp);
156       msg_location_destroy (location);
157       return CMD_FAILURE;
158     }
159   msg_location_destroy (location);
160
161   /* Figure out how to initialize each input case. */
162   inp->init = caseinit_create ();
163   caseinit_mark_for_init (inp->init, dataset_dict (inp->ds));
164   inp->proto = caseproto_ref (dict_get_proto (dataset_dict (inp->ds)));
165
166   dataset_set_dict (ds, dict_clone (dataset_dict (inp->ds)));
167   dataset_set_source (
168     ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
169                                       &input_program_casereader_class, inp));
170
171   return CMD_SUCCESS;
172 }
173
174 /* Reads and returns one case.
175    Returns the case if successful, null at end of file or if an
176    I/O error occurred. */
177 static struct ccase *
178 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_)
179 {
180   struct input_program_pgm *inp = inp_;
181
182   if (inp->eof || !inp->xforms.n)
183     return NULL;
184
185   struct ccase *c = case_create (inp->proto);
186   caseinit_init_vars (inp->init, c);
187   caseinit_restore_left_vars (inp->init, c);
188
189   for (size_t i = inp->idx < inp->xforms.n ? inp->idx : 0; ; i++)
190     {
191       if (i >= inp->xforms.n)
192         {
193           i = 0;
194           c = case_unshare (c);
195           caseinit_save_left_vars (inp->init, c);
196           caseinit_init_vars (inp->init, c);
197         }
198
199       const struct transformation *trns = &inp->xforms.xforms[i];
200       switch (trns->class->execute (trns->aux, &c, inp->case_nr))
201         {
202         case TRNS_END_CASE:
203           inp->case_nr++;
204           inp->idx = i;
205           return c;
206
207         case TRNS_ERROR:
208           casereader_force_error (reader);
209           /* Fall through. */
210         case TRNS_END_FILE:
211           inp->eof = true;
212           case_unref (c);
213           return NULL;
214
215         case TRNS_CONTINUE:
216           break;
217
218         default:
219           NOT_REACHED ();
220         }
221     }
222 }
223
224 static void
225 destroy_input_program (struct input_program_pgm *pgm)
226 {
227   if (pgm != NULL)
228     {
229       session_destroy (pgm->session);
230       trns_chain_uninit (&pgm->xforms);
231       caseinit_destroy (pgm->init);
232       caseproto_unref (pgm->proto);
233       free (pgm);
234     }
235 }
236
237 /* Destroys the casereader. */
238 static void
239 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
240 {
241   struct input_program_pgm *inp = inp_;
242   destroy_input_program (inp);
243 }
244
245 static const struct casereader_class input_program_casereader_class =
246   {
247     input_program_casereader_read,
248     input_program_casereader_destroy,
249     NULL,
250     NULL,
251   };
252 \f
253 int
254 cmd_end_case (struct lexer *lexer UNUSED, struct dataset *ds)
255 {
256   assert (in_input_program ());
257   emit_END_CASE (ds);
258   saw_END_CASE = true;
259   return CMD_SUCCESS;
260 }
261
262 /* Outputs the current case */
263 static enum trns_result
264 end_case_trns_proc (void *resume_, struct ccase **c UNUSED,
265                     casenumber case_nr UNUSED)
266 {
267   bool *resume = resume_;
268   enum trns_result retval = *resume ? TRNS_CONTINUE : TRNS_END_CASE;
269   *resume = !*resume;
270   return retval;
271 }
272
273 static bool
274 end_case_trns_free (void *resume)
275 {
276   free (resume);
277   return true;
278 }
279
280 static const struct trns_class end_case_trns_class = {
281   .name = "END CASE",
282   .execute = end_case_trns_proc,
283   .destroy = end_case_trns_free,
284 };
285
286 /* REREAD transformation. */
287 struct reread_trns
288   {
289     struct dfm_reader *reader;  /* File to move file pointer back on. */
290     struct expression *column;  /* Column to reset file pointer to. */
291   };
292
293 /* Parses REREAD command. */
294 int
295 cmd_reread (struct lexer *lexer, struct dataset *ds)
296 {
297   char *encoding = NULL;
298   struct file_handle *fh = fh_get_default_handle ();
299   struct expression *e = NULL;
300   while (lex_token (lexer) != T_ENDCMD)
301     {
302       if (lex_match_id (lexer, "COLUMN"))
303         {
304           lex_match (lexer, T_EQUALS);
305
306           if (e)
307             {
308               lex_sbc_only_once (lexer, "COLUMN");
309               goto error;
310             }
311
312           e = expr_parse (lexer, ds, VAL_NUMERIC);
313           if (!e)
314             goto error;
315         }
316       else if (lex_match_id (lexer, "FILE"))
317         {
318           lex_match (lexer, T_EQUALS);
319           fh_unref (fh);
320           fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
321           if (fh == NULL)
322             goto error;
323         }
324       else if (lex_match_id (lexer, "ENCODING"))
325         {
326           lex_match (lexer, T_EQUALS);
327           if (!lex_force_string (lexer))
328             goto error;
329
330           free (encoding);
331           encoding = ss_xstrdup (lex_tokss (lexer));
332
333           lex_get (lexer);
334         }
335       else
336         {
337           lex_error_expecting (lexer, "COLUMN", "FILE", "ENCODING");
338           goto error;
339         }
340     }
341
342   struct reread_trns *t = xmalloc (sizeof *t);
343   *t = (struct reread_trns) {
344     .reader = dfm_open_reader (fh, lexer, encoding),
345     .column = e,
346   };
347   add_transformation (ds, &reread_trns_class, t);
348
349   fh_unref (fh);
350   free (encoding);
351   return CMD_SUCCESS;
352
353 error:
354   expr_free (e);
355   free (encoding);
356   return CMD_CASCADING_FAILURE;
357 }
358
359 /* Executes a REREAD transformation. */
360 static enum trns_result
361 reread_trns_proc (void *t_, struct ccase **c, casenumber case_num)
362 {
363   struct reread_trns *t = t_;
364
365   if (t->column == NULL)
366     dfm_reread_record (t->reader, 1);
367   else
368     {
369       double column = expr_evaluate_num (t->column, *c, case_num);
370       if (!isfinite (column) || column < 1)
371         {
372           msg (SE, _("REREAD: Column numbers must be positive finite "
373                "numbers.  Column set to 1."));
374           dfm_reread_record (t->reader, 1);
375         }
376       else
377         dfm_reread_record (t->reader, column);
378     }
379   return TRNS_CONTINUE;
380 }
381
382 /* Frees a REREAD transformation.
383    Returns true if successful, false if an I/O error occurred. */
384 static bool
385 reread_trns_free (void *t_)
386 {
387   struct reread_trns *t = t_;
388   expr_free (t->column);
389   dfm_close_reader (t->reader);
390   return true;
391 }
392
393 static const struct trns_class reread_trns_class = {
394   .name = "REREAD",
395   .execute = reread_trns_proc,
396   .destroy = reread_trns_free,
397 };
398
399 /* Parses END FILE command. */
400 int
401 cmd_end_file (struct lexer *lexer UNUSED, struct dataset *ds)
402 {
403   assert (in_input_program ());
404
405   add_transformation (ds, &end_file_trns_class, NULL);
406   saw_END_FILE = true;
407
408   return CMD_SUCCESS;
409 }
410
411 /* Executes an END FILE transformation. */
412 static enum trns_result
413 end_file_trns_proc (void *trns_ UNUSED, struct ccase **c UNUSED,
414                     casenumber case_num UNUSED)
415 {
416   return TRNS_END_FILE;
417 }
418
419 static const struct trns_class end_file_trns_class = {
420   .name = "END FILE",
421   .execute = end_file_trns_proc,
422 };