Replace syntactical keywords in error/warning messages by printf directives.
[pspp] / src / language / control / repeat.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009-2012 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 <stdlib.h>
20
21 #include "data/dataset.h"
22 #include "data/dictionary.h"
23 #include "data/settings.h"
24 #include "language/command.h"
25 #include "language/lexer/lexer.h"
26 #include "language/lexer/segment.h"
27 #include "language/lexer/token.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/cast.h"
31 #include "libpspp/hash-functions.h"
32 #include "libpspp/hmap.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/message.h"
35 #include "libpspp/str.h"
36 #include "libpspp/misc.h"
37
38 #include "gl/ftoastr.h"
39 #include "gl/minmax.h"
40 #include "gl/xalloc.h"
41
42 #include "gettext.h"
43 #define _(msgid) gettext (msgid)
44
45 struct dummy_var
46   {
47     struct hmap_node hmap_node;
48     char *name;
49     char **values;
50     size_t n_values;
51   };
52
53 static bool parse_specification (struct lexer *, struct dictionary *,
54                                  struct hmap *dummies);
55 static bool parse_commands (struct lexer *, struct hmap *dummies);
56 static void destroy_dummies (struct hmap *dummies);
57
58 static bool parse_ids (struct lexer *, const struct dictionary *,
59                        struct dummy_var *);
60 static bool parse_numbers (struct lexer *, struct dummy_var *);
61 static bool parse_strings (struct lexer *, struct dummy_var *);
62
63 int
64 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
65 {
66   struct hmap dummies;
67   bool ok;
68
69   if (!parse_specification (lexer, dataset_dict (ds), &dummies))
70     return CMD_CASCADING_FAILURE;
71
72   ok = parse_commands (lexer, &dummies);
73
74   destroy_dummies (&dummies);
75
76   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
77 }
78
79 static unsigned int
80 hash_dummy (const char *name, size_t name_len)
81 {
82   return utf8_hash_case_bytes (name, name_len, 0);
83 }
84
85 static const struct dummy_var *
86 find_dummy_var (struct hmap *hmap, const char *name, size_t name_len)
87 {
88   const struct dummy_var *dv;
89
90   HMAP_FOR_EACH_WITH_HASH (dv, struct dummy_var, hmap_node,
91                            hash_dummy (name, name_len), hmap)
92     if (utf8_strcasecmp (dv->name, name))
93       return dv;
94
95   return NULL;
96 }
97
98 /* Parses the whole DO REPEAT command specification.
99    Returns success. */
100 static bool
101 parse_specification (struct lexer *lexer, struct dictionary *dict,
102                      struct hmap *dummies)
103 {
104   struct dummy_var *first_dv = NULL;
105
106   hmap_init (dummies);
107   do
108     {
109       struct dummy_var *dv;
110       const char *name;
111       bool ok;
112
113       /* Get a stand-in variable name and make sure it's unique. */
114       if (!lex_force_id (lexer))
115         goto error;
116       name = lex_tokcstr (lexer);
117       if (dict_lookup_var (dict, name))
118         msg (SW, _("Dummy variable name `%s' hides dictionary variable `%s'."),
119              name, name);
120       if (find_dummy_var (dummies, name, strlen (name)))
121         {
122           msg (SE, _("Dummy variable name `%s' is given twice."), name);
123           goto error;
124         }
125
126       /* Make a new macro. */
127       dv = xmalloc (sizeof *dv);
128       dv->name = xstrdup (name);
129       dv->values = NULL;
130       dv->n_values = 0;
131       hmap_insert (dummies, &dv->hmap_node, hash_dummy (name, strlen (name)));
132
133       /* Skip equals sign. */
134       lex_get (lexer);
135       if (!lex_force_match (lexer, T_EQUALS))
136         goto error;
137
138       /* Get the details of the variable's possible values. */
139       if (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
140         ok = parse_ids (lexer, dict, dv);
141       else if (lex_is_number (lexer))
142         ok = parse_numbers (lexer, dv);
143       else if (lex_is_string (lexer))
144         ok = parse_strings (lexer, dv);
145       else
146         {
147           lex_error (lexer, NULL);
148           goto error;
149         }
150       if (!ok)
151         goto error;
152       assert (dv->n_values > 0);
153       if (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
154         {
155           lex_error (lexer, NULL);
156           goto error;
157         }
158
159       /* If this is the first variable then it defines how many replacements
160          there must be; otherwise enforce this number of replacements. */
161       if (first_dv == NULL)
162         first_dv = dv;
163       else if (first_dv->n_values != dv->n_values)
164         {
165           msg (SE, _("Dummy variable `%s' had %zu substitutions, so `%s' must "
166                      "also, but %zu were specified."),
167                first_dv->name, first_dv->n_values,
168                dv->name, dv->n_values);
169           goto error;
170         }
171
172       lex_match (lexer, T_SLASH);
173     }
174   while (!lex_match (lexer, T_ENDCMD));
175
176   while (lex_match (lexer, T_ENDCMD))
177     continue;
178
179   return true;
180
181 error:
182   destroy_dummies (dummies);
183   return false;
184 }
185
186 static size_t
187 count_values (struct hmap *dummies)
188 {
189   const struct dummy_var *dv;
190   dv = HMAP_FIRST (struct dummy_var, hmap_node, dummies);
191   return dv->n_values;
192 }
193
194 static void
195 do_parse_commands (struct substring s, enum segmenter_mode mode,
196                    struct hmap *dummies,
197                    struct string *outputs, size_t n_outputs)
198 {
199   struct segmenter segmenter;
200
201   segmenter_init (&segmenter, mode);
202
203   while (!ss_is_empty (s))
204     {
205       enum segment_type type;
206       int n;
207
208       n = segmenter_push (&segmenter, s.string, s.length, &type);
209       assert (n >= 0);
210
211       if (type == SEG_DO_REPEAT_COMMAND)
212         {
213           for (;;)
214             {
215               int k;
216
217               k = segmenter_push (&segmenter, s.string + n, s.length - n,
218                                   &type);
219               if (type != SEG_NEWLINE && type != SEG_DO_REPEAT_COMMAND)
220                 break;
221
222               n += k;
223             }
224
225           do_parse_commands (ss_head (s, n), mode, dummies,
226                              outputs, n_outputs);
227         }
228       else if (type != SEG_END)
229         {
230           const struct dummy_var *dv;
231           size_t i;
232
233           dv = (type == SEG_IDENTIFIER
234                 ? find_dummy_var (dummies, s.string, n)
235                 : NULL);
236           for (i = 0; i < n_outputs; i++)
237             if (dv != NULL)
238               ds_put_cstr (&outputs[i], dv->values[i]);
239             else
240               ds_put_substring (&outputs[i], ss_head (s, n));
241         }
242
243       ss_advance (&s, n);
244     }
245 }
246
247 static bool
248 parse_commands (struct lexer *lexer, struct hmap *dummies)
249 {
250   enum lex_syntax_mode syntax_mode;
251   enum segmenter_mode mode;
252   struct string *outputs;
253   struct string input;
254   size_t n_values;
255   char *file_name;
256   int line_number;
257   bool ok;
258   size_t i;
259
260   if (lex_get_file_name (lexer) != NULL)
261     file_name = xstrdup (lex_get_file_name (lexer));
262   else
263     file_name = NULL;
264   line_number = lex_get_first_line_number (lexer, 0);
265
266   ds_init_empty (&input);
267   while (lex_is_string (lexer))
268     {
269       ds_put_substring (&input, lex_tokss (lexer));
270       ds_put_byte (&input, '\n');
271       lex_get (lexer);
272     }
273   if (ds_is_empty (&input))
274     ds_put_byte (&input, '\n');
275   ds_put_byte (&input, '\0');
276
277   n_values = count_values (dummies);
278   outputs = xmalloc (n_values * sizeof *outputs);
279   for (i = 0; i < n_values; i++)
280     ds_init_empty (&outputs[i]);
281
282   syntax_mode = lex_get_syntax_mode (lexer);
283   if (syntax_mode == LEX_SYNTAX_AUTO)
284     mode = SEG_MODE_AUTO;
285   else if (syntax_mode == LEX_SYNTAX_INTERACTIVE)
286     mode = SEG_MODE_INTERACTIVE;
287   else if (syntax_mode == LEX_SYNTAX_BATCH)
288     mode = SEG_MODE_BATCH;
289   else
290     NOT_REACHED ();
291   do_parse_commands (ds_ss (&input), mode, dummies, outputs, n_values);
292
293   ds_destroy (&input);
294
295   while (lex_match (lexer, T_ENDCMD))
296     continue;
297
298   ok = (lex_force_match_id (lexer, "END")
299         && lex_force_match_id (lexer, "REPEAT"));
300   if (ok)
301     lex_match_id (lexer, "PRINT"); /* XXX */
302
303   lex_discard_rest_of_command (lexer);
304
305   for (i = 0; i < n_values; i++)
306     {
307       struct string *output = &outputs[n_values - i - 1];
308       struct lex_reader *reader;
309
310       reader = lex_reader_for_substring_nocopy (ds_ss (output));
311       lex_reader_set_file_name (reader, file_name);
312       reader->line_number = line_number;
313       lex_include (lexer, reader);
314     }
315   free (file_name);
316   free (outputs);
317
318   return ok;
319 }
320
321 static void
322 destroy_dummies (struct hmap *dummies)
323 {
324   struct dummy_var *dv, *next;
325
326   HMAP_FOR_EACH_SAFE (dv, next, struct dummy_var, hmap_node, dummies)
327     {
328       size_t i;
329
330       hmap_delete (dummies, &dv->hmap_node);
331
332       free (dv->name);
333       for (i = 0; i < dv->n_values; i++)
334         free (dv->values[i]);
335       free (dv->values);
336       free (dv);
337     }
338   hmap_destroy (dummies);
339 }
340
341 /* Parses a set of ids for DO REPEAT. */
342 static bool
343 parse_ids (struct lexer *lexer, const struct dictionary *dict,
344            struct dummy_var *dv)
345 {
346   return parse_mixed_vars (lexer, dict, &dv->values, &dv->n_values, PV_NONE);
347 }
348
349 /* Adds REPLACEMENT to MACRO's list of replacements, which has
350    *USED elements and has room for *ALLOCATED.  Allocates memory
351    from POOL. */
352 static void
353 add_replacement (struct dummy_var *dv, char *value, size_t *allocated)
354 {
355   if (dv->n_values == *allocated)
356     dv->values = x2nrealloc (dv->values, allocated, sizeof *dv->values);
357   dv->values[dv->n_values++] = value;
358 }
359
360 /* Parses a list or range of numbers for DO REPEAT. */
361 static bool
362 parse_numbers (struct lexer *lexer, struct dummy_var *dv)
363 {
364   size_t allocated = 0;
365
366   do
367     {
368       if (!lex_force_num (lexer))
369         return false;
370
371       if (lex_next_token (lexer, 1) == T_TO)
372         {
373           long int a, b;
374           long int i;
375
376           if (!lex_is_integer (lexer))
377             {
378               msg (SE, _("Ranges may only have integer bounds."));
379               return false;
380             }
381
382           a = lex_integer (lexer);
383           lex_get (lexer);
384           lex_get (lexer);
385
386           if (!lex_force_int (lexer))
387             return false;
388
389           b = lex_integer (lexer);
390           if (b < a)
391             {
392               msg (SE, _("%ld TO %ld is an invalid range."), a, b);
393               return false;
394             }
395           lex_get (lexer);
396
397           for (i = a; i <= b; i++)
398             add_replacement (dv, xasprintf ("%ld", i), &allocated);
399         }
400       else
401         {
402           char s[DBL_BUFSIZE_BOUND];
403
404           c_dtoastr (s, sizeof s, 0, 0, lex_number (lexer));
405           add_replacement (dv, xstrdup (s), &allocated);
406           lex_get (lexer);
407         }
408
409       lex_match (lexer, T_COMMA);
410     }
411   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
412
413   return true;
414 }
415
416 /* Parses a list of strings for DO REPEAT. */
417 static bool
418 parse_strings (struct lexer *lexer, struct dummy_var *dv)
419 {
420   size_t allocated = 0;
421
422   do
423     {
424       if (!lex_force_string (lexer))
425         {
426           return false;
427         }
428
429       add_replacement (dv, token_to_string (lex_next (lexer, 0)), &allocated);
430
431       lex_get (lexer);
432       lex_match (lexer, T_COMMA);
433     }
434   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
435
436   return true;
437 }
438 \f
439 int
440 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
441 {
442   msg (SE, _("No matching %s."), "DO REPEAT");
443   return CMD_CASCADING_FAILURE;
444 }