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