1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <libpspp/message.h>
26 #include <libpspp/alloc.h>
27 #include <language/command.h>
28 #include <data/dictionary.h>
30 #include <libpspp/message.h>
31 #include <language/line-buffer.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/misc.h>
34 #include <libpspp/pool.h>
35 #include <data/settings.h>
36 #include <libpspp/str.h>
37 #include <data/variable.h>
40 #define _(msgid) gettext (msgid)
42 /* Defines a list of lines used by DO REPEAT. */
45 struct line_list *next; /* Next line. */
46 char *file_name; /* File name. */
47 int line_number; /* Line number. */
48 char *line; /* Contents. */
51 /* The type of substitution made for a DO REPEAT macro. */
52 enum repeat_entry_type
58 /* Describes one DO REPEAT macro. */
61 struct repeat_entry *next; /* Next entry. */
62 enum repeat_entry_type type; /* Types of replacements. */
63 char id[LONG_NAME_LEN + 1]; /* Macro identifier. */
64 char **replacement; /* Macro replacement. */
67 /* A DO REPEAT...END REPEAT block. */
70 struct pool *pool; /* Pool used for storage. */
71 struct line_list *first_line; /* First line in line buffer. */
72 struct line_list *cur_line; /* Current line in line buffer. */
73 int loop_cnt; /* Number of loops. */
74 int loop_idx; /* Number of loops so far. */
75 struct repeat_entry *macros; /* Pointer to macro table. */
76 bool print; /* Print lines as executed? */
79 static bool parse_specification (struct repeat_block *);
80 static bool parse_lines (struct repeat_block *);
81 static void create_vars (struct repeat_block *);
83 static int parse_ids (struct repeat_entry *);
84 static int parse_numbers (struct repeat_entry *);
85 static int parse_strings (struct repeat_entry *);
87 static void do_repeat_filter (struct string *line, void *block);
88 static bool do_repeat_read (struct string *line, char **file_name,
89 int *line_number, void *block);
90 static void do_repeat_close (void *block);
95 struct repeat_block *block;
97 block = pool_create_container (struct repeat_block, pool);
99 if (!parse_specification (block) || !parse_lines (block))
104 block->cur_line = NULL;
105 block->loop_idx = -1;
106 getl_include_filter (do_repeat_filter, do_repeat_close, block);
107 getl_include_function (do_repeat_read, NULL, block);
112 pool_destroy (block->pool);
113 return CMD_CASCADING_FAILURE;
116 /* Parses the whole DO REPEAT command specification.
119 parse_specification (struct repeat_block *block)
121 char first_name[LONG_NAME_LEN + 1];
124 block->macros = NULL;
127 struct repeat_entry *e;
128 struct repeat_entry *iter;
131 /* Get a stand-in variable name and make sure it's unique. */
132 if (!lex_force_id ())
134 if (dict_lookup_var (default_dict, tokid))
135 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
138 for (iter = block->macros; iter != NULL; iter = iter->next)
139 if (!strcasecmp (iter->id, tokid))
141 msg (SE, _("Dummy variable name \"%s\" is given twice."), tokid);
145 /* Make a new stand-in variable entry and link it into the
147 e = pool_alloc (block->pool, sizeof *e);
148 e->next = block->macros;
149 strcpy (e->id, tokid);
152 /* Skip equals sign. */
154 if (!lex_force_match ('='))
157 /* Get the details of the variable's possible values. */
159 count = parse_ids (e);
160 else if (lex_is_number ())
161 count = parse_numbers (e);
162 else if (token == T_STRING)
163 count = parse_strings (e);
172 /* If this is the first variable then it defines how many
173 replacements there must be; otherwise enforce this number of
175 if (block->loop_cnt == 0)
177 block->loop_cnt = count;
178 strcpy (first_name, e->id);
180 else if (block->loop_cnt != count)
182 msg (SE, _("Dummy variable \"%s\" had %d "
183 "substitutions, so \"%s\" must also, but %d "
185 first_name, block->loop_cnt, e->id, count);
191 while (token != '.');
196 /* If KEYWORD appears beginning at CP, possibly preceded by white
197 space, returns a pointer to the character just after the
198 keyword. Otherwise, returns a null pointer. */
200 recognize_keyword (const char *cp, const char *keyword)
204 while (isspace ((unsigned char) *cp))
207 end = lex_skip_identifier (cp);
209 && lex_id_match_len (keyword, strlen (keyword), cp, end - cp))
215 /* Returns CP, advanced past a '+' or '-' if present. */
217 skip_indentor (const char *cp)
219 if (*cp == '+' || *cp == '-')
224 /* Returns true if LINE contains a DO REPEAT command, false
227 recognize_do_repeat (const char *line)
229 const char *cp = recognize_keyword (skip_indentor (line), "do");
230 return cp != NULL && recognize_keyword (cp, "repeat") != NULL;
233 /* Returns true if LINE contains an END REPEAT command, false
234 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
237 recognize_end_repeat (const char *line, bool *print)
239 const char *cp = recognize_keyword (skip_indentor (line), "end");
243 cp = recognize_keyword (cp, "repeat");
247 *print = recognize_keyword (cp, "print");
251 /* Read all the lines we are going to substitute, inside the DO
252 REPEAT...END REPEAT block. */
254 parse_lines (struct repeat_block *block)
256 char *previous_file_name;
257 struct line_list **last_line;
260 previous_file_name = NULL;
261 block->first_line = NULL;
262 last_line = &block->first_line;
267 const char *cur_file_name;
269 struct line_list *line;
272 if (!getl_read_line (NULL))
275 /* If the current file has changed then record the fact. */
276 getl_location (&cur_file_name, &cur_line_number);
277 if (previous_file_name == NULL
278 || !strcmp (cur_file_name, previous_file_name))
279 previous_file_name = pool_strdup (block->pool, cur_file_name);
281 ds_rtrim_spaces (&getl_buf);
282 dot = ds_chomp (&getl_buf, get_endcmd ());
283 if (recognize_do_repeat (ds_c_str (&getl_buf)))
285 else if (recognize_end_repeat (ds_c_str (&getl_buf), &block->print))
287 if (nesting_level-- == 0)
294 ds_putc (&getl_buf, get_endcmd ());
296 line = *last_line = pool_alloc (block->pool, sizeof *line);
298 line->file_name = previous_file_name;
299 line->line_number = cur_line_number;
300 line->line = pool_strdup (block->pool, ds_c_str (&getl_buf));
301 last_line = &line->next;
308 /* Creates variables for the given DO REPEAT. */
310 create_vars (struct repeat_block *block)
312 struct repeat_entry *iter;
314 for (iter = block->macros; iter; iter = iter->next)
315 if (iter->type == VAR_NAMES)
319 for (i = 0; i < block->loop_cnt; i++)
321 /* Ignore return value: if the variable already
322 exists there is no harm done. */
323 dict_create_var (default_dict, iter->replacement[i], 0);
328 /* Parses a set of ids for DO REPEAT. */
330 parse_ids (struct repeat_entry *e)
336 e->replacement = NULL;
343 if (!parse_mixed_vars (&names, &nnames, PV_NONE))
346 e->replacement = xnrealloc (e->replacement,
347 nnames + n, sizeof *e->replacement);
348 for (i = 0; i < nnames; i++)
350 e->replacement[n + i] = xstrdup (names[i]);
356 while (token != '/' && token != '.');
361 /* Stores VALUE into *REPL. */
363 store_numeric (char **repl, long value)
365 *repl = xmalloc (INT_STRLEN_BOUND (value) + 1);
366 sprintf (*repl, "%ld", value);
369 /* Parses a list of numbers for DO REPEAT. */
371 parse_numbers (struct repeat_entry *e)
373 /* First and last numbers for TO, plus the step factor. */
376 /* Alias to e->replacement. */
379 /* Number of entries in array; maximum number for this allocation
385 e->replacement = array = NULL;
389 /* Parse A TO B into a, b. */
390 if (!lex_force_int ())
398 if (!lex_force_int ())
406 if (n + (abs (b - a) + 1) > m)
408 m = n + (abs (b - a) + 1) + 16;
409 e->replacement = array = xnrealloc (array,
410 m, sizeof *e->replacement);
414 store_numeric (&array[n++], a);
420 for (iter = a; iter <= b; iter++)
421 store_numeric (&array[n++], iter);
423 for (iter = a; iter >= b; iter--)
424 store_numeric (&array[n++], iter);
429 while (token != '/' && token != '.');
430 e->replacement = xrealloc (array, n * sizeof *e->replacement);
435 /* Parses a list of strings for DO REPEAT. */
437 parse_strings (struct repeat_entry *e)
443 string = e->replacement = NULL;
448 if (token != T_STRING)
451 msg (SE, _("String expected."));
452 for (i = 0; i < n; i++)
461 e->replacement = string = xnrealloc (string,
462 m, sizeof *e->replacement);
464 string[n++] = lex_token_representation ();
469 while (token != '/' && token != '.');
470 e->replacement = xnrealloc (string, n, sizeof *e->replacement);
476 cmd_end_repeat (void)
478 msg (SE, _("No matching DO REPEAT."));
479 return CMD_CASCADING_FAILURE;
482 /* Finds a DO REPEAT macro with name MACRO_NAME and returns the
483 appropriate subsitution if found, or NULL if not. */
485 find_substitution (struct repeat_block *block, const char *name, size_t length)
487 struct repeat_entry *e;
489 for (e = block->macros; e; e = e->next)
490 if (!memcasecmp (e->id, name, length) && strlen (e->id) == length)
491 return e->replacement[block->loop_idx];
496 /* Makes appropriate DO REPEAT macro substitutions within getl_buf. */
498 do_repeat_filter (struct string *line, void *block_)
500 struct repeat_block *block = block_;
501 bool in_apos, in_quote;
503 struct string output;
506 ds_init (&output, ds_capacity (line));
508 /* Strip trailing whitespace, check for & remove terminal dot. */
509 while (isspace (ds_last (line)))
510 ds_truncate (line, ds_length (line) - 1);
511 dot = ds_chomp (line, get_endcmd ());
513 in_apos = in_quote = false;
514 for (cp = ds_c_str (line); cp < ds_end (line); )
516 if (*cp == '\'' && !in_quote)
518 else if (*cp == '"' && !in_apos)
519 in_quote = !in_quote;
521 if (in_quote || in_apos || !lex_is_id1 (*cp))
522 ds_putc (&output, *cp++);
525 const char *start = cp;
526 char *end = lex_skip_identifier (start);
527 const char *substitution = find_substitution (block,
529 if (substitution != NULL)
530 ds_puts (&output, substitution);
532 ds_concat (&output, start, end - start);
537 ds_putc (&output, get_endcmd ());
539 ds_swap (line, &output);
540 ds_destroy (&output);
543 /* Function called by getl to read a line.
544 Puts the line in OUTPUT, sets the file name in *FILE_NAME and
545 line number in *LINE_NUMBER. Returns true if a line was
546 obtained, false if the source is exhausted. */
548 do_repeat_read (struct string *output, char **file_name, int *line_number,
551 struct repeat_block *block = block_;
552 struct line_list *line;
554 if (block->cur_line == NULL)
557 if (block->loop_idx >= block->loop_cnt)
559 block->cur_line = block->first_line;
561 line = block->cur_line;
563 ds_assign_c_str (output, line->line);
564 *file_name = line->file_name;
565 *line_number = -line->line_number;
566 block->cur_line = line->next;
570 /* Frees a DO REPEAT block.
571 Called by getl to close out the DO REPEAT block. */
573 do_repeat_close (void *block_)
575 struct repeat_block *block = block_;
576 pool_destroy (block->pool);