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
cmd_do_repeat(struct lexer * lexer,struct dataset * ds)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
hash_dummy(const char * name,size_t name_len)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 *
find_dummy_var(struct hmap * hmap,const char * name,size_t name_len)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
parse_specification(struct lexer * lexer,struct dictionary * dict,struct hmap * dummies)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
count_values(struct hmap * dummies)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
do_parse_commands(struct substring s,enum segmenter_mode mode,struct hmap * dummies,struct string * outputs,size_t n_outputs)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, true, &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 true, &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
parse_commands(struct lexer * lexer,struct hmap * dummies)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
279 n_values = count_values (dummies);
280 outputs = xmalloc (n_values * sizeof *outputs);
281 for (i = 0; i < n_values; i++)
282 ds_init_empty (&outputs[i]);
283
284 syntax_mode = lex_get_syntax_mode (lexer);
285 if (syntax_mode == LEX_SYNTAX_AUTO)
286 mode = SEG_MODE_AUTO;
287 else if (syntax_mode == LEX_SYNTAX_INTERACTIVE)
288 mode = SEG_MODE_INTERACTIVE;
289 else if (syntax_mode == LEX_SYNTAX_BATCH)
290 mode = SEG_MODE_BATCH;
291 else
292 NOT_REACHED ();
293 do_parse_commands (ds_ss (&input), mode, dummies, outputs, n_values);
294
295 ds_destroy (&input);
296
297 while (lex_match (lexer, T_ENDCMD))
298 continue;
299
300 ok = (lex_force_match_id (lexer, "END")
301 && lex_force_match_id (lexer, "REPEAT"));
302 if (ok)
303 lex_match_id (lexer, "PRINT"); /* XXX */
304
305 lex_discard_rest_of_command (lexer);
306
307 for (i = 0; i < n_values; i++)
308 {
309 struct string *output = &outputs[n_values - i - 1];
310 const char *encoding = lex_get_encoding (lexer);
311 struct lex_reader *reader = lex_reader_for_substring_nocopy (ds_ss (output), encoding);
312 lex_reader_set_file_name (reader, file_name);
313 reader->line_number = line_number;
314 lex_include (lexer, reader);
315 }
316 free (file_name);
317 free (outputs);
318
319 return ok;
320 }
321
322 static void
destroy_dummies(struct hmap * dummies)323 destroy_dummies (struct hmap *dummies)
324 {
325 struct dummy_var *dv, *next;
326
327 HMAP_FOR_EACH_SAFE (dv, next, struct dummy_var, hmap_node, dummies)
328 {
329 size_t i;
330
331 hmap_delete (dummies, &dv->hmap_node);
332
333 free (dv->name);
334 for (i = 0; i < dv->n_values; i++)
335 free (dv->values[i]);
336 free (dv->values);
337 free (dv);
338 }
339 hmap_destroy (dummies);
340 }
341
342 /* Parses a set of ids for DO REPEAT. */
343 static bool
parse_ids(struct lexer * lexer,const struct dictionary * dict,struct dummy_var * dv)344 parse_ids (struct lexer *lexer, const struct dictionary *dict,
345 struct dummy_var *dv)
346 {
347 return parse_mixed_vars (lexer, dict, &dv->values, &dv->n_values, PV_NONE);
348 }
349
350 /* Adds REPLACEMENT to MACRO's list of replacements, which has
351 *USED elements and has room for *ALLOCATED. Allocates memory
352 from POOL. */
353 static void
add_replacement(struct dummy_var * dv,char * value,size_t * allocated)354 add_replacement (struct dummy_var *dv, char *value, size_t *allocated)
355 {
356 if (dv->n_values == *allocated)
357 dv->values = x2nrealloc (dv->values, allocated, sizeof *dv->values);
358 dv->values[dv->n_values++] = value;
359 }
360
361 /* Parses a list or range of numbers for DO REPEAT. */
362 static bool
parse_numbers(struct lexer * lexer,struct dummy_var * dv)363 parse_numbers (struct lexer *lexer, struct dummy_var *dv)
364 {
365 size_t allocated = 0;
366
367 do
368 {
369 if (!lex_force_num (lexer))
370 return false;
371
372 if (lex_next_token (lexer, 1) == T_TO)
373 {
374 long int a, b;
375 long int i;
376
377 if (!lex_is_integer (lexer))
378 {
379 msg (SE, _("Ranges may only have integer bounds."));
380 return false;
381 }
382
383 a = lex_integer (lexer);
384 lex_get (lexer);
385 lex_get (lexer);
386
387 if (!lex_force_int (lexer))
388 return false;
389
390 b = lex_integer (lexer);
391 if (b < a)
392 {
393 msg (SE, _("%ld TO %ld is an invalid range."), a, b);
394 return false;
395 }
396 lex_get (lexer);
397
398 for (i = a; i <= b; i++)
399 add_replacement (dv, xasprintf ("%ld", i), &allocated);
400 }
401 else
402 {
403 char s[DBL_BUFSIZE_BOUND];
404
405 c_dtoastr (s, sizeof s, 0, 0, lex_number (lexer));
406 add_replacement (dv, xstrdup (s), &allocated);
407 lex_get (lexer);
408 }
409
410 lex_match (lexer, T_COMMA);
411 }
412 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
413
414 return true;
415 }
416
417 /* Parses a list of strings for DO REPEAT. */
418 static bool
parse_strings(struct lexer * lexer,struct dummy_var * dv)419 parse_strings (struct lexer *lexer, struct dummy_var *dv)
420 {
421 size_t allocated = 0;
422
423 do
424 {
425 if (!lex_force_string (lexer))
426 {
427 return false;
428 }
429
430 add_replacement (dv, token_to_string (lex_next (lexer, 0)), &allocated);
431
432 lex_get (lexer);
433 lex_match (lexer, T_COMMA);
434 }
435 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
436
437 return true;
438 }
439
440 int
cmd_end_repeat(struct lexer * lexer UNUSED,struct dataset * ds UNUSED)441 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
442 {
443 msg (SE, _("No matching %s."), "DO REPEAT");
444 return CMD_CASCADING_FAILURE;
445 }
446