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
28 #include "dictionary.h"
30 #include "line-buffer.h"
39 #define _(msgid) gettext (msgid)
41 #include "debug-print.h"
43 /* Defines a list of lines used by DO REPEAT. */
46 struct line_list *next; /* Next line. */
47 char *file_name; /* File name. */
48 int line_number; /* Line number. */
49 char *line; /* Contents. */
52 /* The type of substitution made for a DO REPEAT macro. */
53 enum repeat_entry_type
59 /* Describes one DO REPEAT macro. */
62 struct repeat_entry *next; /* Next entry. */
63 enum repeat_entry_type type; /* Types of replacements. */
64 char id[LONG_NAME_LEN + 1]; /* Macro identifier. */
65 char **replacement; /* Macro replacement. */
68 /* A DO REPEAT...END REPEAT block. */
71 struct pool *pool; /* Pool used for storage. */
72 struct line_list *first_line; /* First line in line buffer. */
73 struct line_list *cur_line; /* Current line in line buffer. */
74 int loop_cnt; /* Number of loops. */
75 int loop_idx; /* Number of loops so far. */
76 struct repeat_entry *macros; /* Pointer to macro table. */
77 bool print; /* Print lines as executed? */
80 static bool parse_specification (struct repeat_block *);
81 static bool parse_lines (struct repeat_block *);
82 static void create_vars (struct repeat_block *);
84 static int parse_ids (struct repeat_entry *);
85 static int parse_numbers (struct repeat_entry *);
86 static int parse_strings (struct repeat_entry *);
88 static void do_repeat_filter (struct string *line, void *block);
89 static bool do_repeat_read (struct string *line, char **file_name,
90 int *line_number, void *block);
91 static void do_repeat_close (void *block);
96 struct repeat_block *block;
98 block = pool_create_container (struct repeat_block, pool);
100 if (!parse_specification (block) || !parse_lines (block))
105 block->cur_line = NULL;
106 block->loop_idx = -1;
107 getl_include_filter (do_repeat_filter, do_repeat_close, block);
108 getl_include_function (do_repeat_read, NULL, block);
113 pool_destroy (block->pool);
114 return CMD_CASCADING_FAILURE;
117 /* Parses the whole DO REPEAT command specification.
120 parse_specification (struct repeat_block *block)
122 char first_name[LONG_NAME_LEN + 1];
125 block->macros = NULL;
128 struct repeat_entry *e;
129 struct repeat_entry *iter;
132 /* Get a stand-in variable name and make sure it's unique. */
133 if (!lex_force_id ())
135 if (dict_lookup_var (default_dict, tokid))
136 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
139 for (iter = block->macros; iter != NULL; iter = iter->next)
140 if (!strcasecmp (iter->id, tokid))
142 msg (SE, _("Dummy variable name \"%s\" is given twice."), tokid);
146 /* Make a new stand-in variable entry and link it into the
148 e = pool_alloc (block->pool, sizeof *e);
149 e->next = block->macros;
150 strcpy (e->id, tokid);
153 /* Skip equals sign. */
155 if (!lex_force_match ('='))
158 /* Get the details of the variable's possible values. */
160 count = parse_ids (e);
161 else if (lex_is_number ())
162 count = parse_numbers (e);
163 else if (token == T_STRING)
164 count = parse_strings (e);
173 /* If this is the first variable then it defines how many
174 replacements there must be; otherwise enforce this number of
176 if (block->loop_cnt == 0)
178 block->loop_cnt = count;
179 strcpy (first_name, e->id);
181 else if (block->loop_cnt != count)
183 msg (SE, _("Dummy variable \"%s\" had %d "
184 "substitutions, so \"%s\" must also, but %d "
186 first_name, block->loop_cnt, e->id, count);
192 while (token != '.');
197 /* If KEYWORD appears beginning at CP, possibly preceded by white
198 space, returns a pointer to the character just after the
199 keyword. Otherwise, returns a null pointer. */
201 recognize_keyword (const char *cp, const char *keyword)
205 while (isspace ((unsigned char) *cp))
208 end = lex_skip_identifier (cp);
210 && lex_id_match_len (keyword, strlen (keyword), cp, end - cp))
216 /* Returns CP, advanced past a '+' or '-' if present. */
218 skip_indentor (const char *cp)
220 if (*cp == '+' || *cp == '-')
225 /* Returns true if LINE contains a DO REPEAT command, false
228 recognize_do_repeat (const char *line)
230 const char *cp = recognize_keyword (skip_indentor (line), "do");
231 return cp != NULL && recognize_keyword (cp, "repeat") != NULL;
234 /* Returns true if LINE contains an END REPEAT command, false
235 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
238 recognize_end_repeat (const char *line, bool *print)
240 const char *cp = recognize_keyword (skip_indentor (line), "end");
244 cp = recognize_keyword (cp, "repeat");
248 *print = recognize_keyword (cp, "print");
252 /* Read all the lines we are going to substitute, inside the DO
253 REPEAT...END REPEAT block. */
255 parse_lines (struct repeat_block *block)
257 char *previous_file_name;
258 struct line_list **last_line;
261 previous_file_name = NULL;
262 block->first_line = NULL;
263 last_line = &block->first_line;
268 const char *cur_file_name;
270 struct line_list *line;
273 if (!getl_read_line (NULL))
276 /* If the current file has changed then record the fact. */
277 getl_location (&cur_file_name, &cur_line_number);
278 if (previous_file_name == NULL
279 || !strcmp (cur_file_name, previous_file_name))
280 previous_file_name = pool_strdup (block->pool, cur_file_name);
282 ds_rtrim_spaces (&getl_buf);
283 dot = ds_chomp (&getl_buf, get_endcmd ());
284 if (recognize_do_repeat (ds_c_str (&getl_buf)))
286 else if (recognize_end_repeat (ds_c_str (&getl_buf), &block->print))
288 if (nesting_level-- == 0)
295 ds_putc (&getl_buf, get_endcmd ());
297 line = *last_line = pool_alloc (block->pool, sizeof *line);
299 line->file_name = previous_file_name;
300 line->line_number = cur_line_number;
301 line->line = pool_strdup (block->pool, ds_c_str (&getl_buf));
302 last_line = &line->next;
309 /* Creates variables for the given DO REPEAT. */
311 create_vars (struct repeat_block *block)
313 struct repeat_entry *iter;
315 for (iter = block->macros; iter; iter = iter->next)
316 if (iter->type == VAR_NAMES)
320 for (i = 0; i < block->loop_cnt; i++)
322 /* Ignore return value: if the variable already
323 exists there is no harm done. */
324 dict_create_var (default_dict, iter->replacement[i], 0);
329 /* Parses a set of ids for DO REPEAT. */
331 parse_ids (struct repeat_entry *e)
337 e->replacement = NULL;
344 if (!parse_mixed_vars (&names, &nnames, PV_NONE))
347 e->replacement = xnrealloc (e->replacement,
348 nnames + n, sizeof *e->replacement);
349 for (i = 0; i < nnames; i++)
351 e->replacement[n + i] = xstrdup (names[i]);
357 while (token != '/' && token != '.');
362 /* Stores VALUE into *REPL. */
364 store_numeric (char **repl, long value)
366 *repl = xmalloc (INT_DIGITS + 1);
367 sprintf (*repl, "%ld", value);
370 /* Parses a list of numbers for DO REPEAT. */
372 parse_numbers (struct repeat_entry *e)
374 /* First and last numbers for TO, plus the step factor. */
377 /* Alias to e->replacement. */
380 /* Number of entries in array; maximum number for this allocation
386 e->replacement = array = NULL;
390 /* Parse A TO B into a, b. */
391 if (!lex_force_int ())
399 if (!lex_force_int ())
407 if (n + (abs (b - a) + 1) > m)
409 m = n + (abs (b - a) + 1) + 16;
410 e->replacement = array = xnrealloc (array,
411 m, sizeof *e->replacement);
415 store_numeric (&array[n++], a);
421 for (iter = a; iter <= b; iter++)
422 store_numeric (&array[n++], iter);
424 for (iter = a; iter >= b; iter--)
425 store_numeric (&array[n++], iter);
430 while (token != '/' && token != '.');
431 e->replacement = xrealloc (array, n * sizeof *e->replacement);
436 /* Parses a list of strings for DO REPEAT. */
438 parse_strings (struct repeat_entry *e)
444 string = e->replacement = NULL;
449 if (token != T_STRING)
452 msg (SE, _("String expected."));
453 for (i = 0; i < n; i++)
462 e->replacement = string = xnrealloc (string,
463 m, sizeof *e->replacement);
465 string[n++] = lex_token_representation ();
470 while (token != '/' && token != '.');
471 e->replacement = xnrealloc (string, n, sizeof *e->replacement);
477 cmd_end_repeat (void)
479 msg (SE, _("No matching DO REPEAT."));
480 return CMD_CASCADING_FAILURE;
483 /* Finds a DO REPEAT macro with name MACRO_NAME and returns the
484 appropriate subsitution if found, or NULL if not. */
486 find_substitution (struct repeat_block *block, const char *name, size_t length)
488 struct repeat_entry *e;
490 for (e = block->macros; e; e = e->next)
491 if (!memcasecmp (e->id, name, length) && strlen (e->id) == length)
492 return e->replacement[block->loop_idx];
497 /* Makes appropriate DO REPEAT macro substitutions within getl_buf. */
499 do_repeat_filter (struct string *line, void *block_)
501 struct repeat_block *block = block_;
502 bool in_apos, in_quote;
504 struct string output;
507 ds_init (&output, ds_capacity (line));
509 /* Strip trailing whitespace, check for & remove terminal dot. */
510 while (isspace (ds_last (line)))
511 ds_truncate (line, ds_length (line) - 1);
512 dot = ds_chomp (line, get_endcmd ());
514 in_apos = in_quote = false;
515 for (cp = ds_c_str (line); cp < ds_end (line); )
517 if (*cp == '\'' && !in_quote)
519 else if (*cp == '"' && !in_apos)
520 in_quote = !in_quote;
522 if (in_quote || in_apos || !lex_is_id1 (*cp))
523 ds_putc (&output, *cp++);
526 const char *start = cp;
527 char *end = lex_skip_identifier (start);
528 const char *substitution = find_substitution (block,
530 if (substitution != NULL)
531 ds_puts (&output, substitution);
533 ds_concat (&output, start, end - start);
538 ds_putc (&output, get_endcmd ());
540 ds_swap (line, &output);
541 ds_destroy (&output);
544 /* Function called by getl to read a line.
545 Puts the line in OUTPUT, sets the file name in *FILE_NAME and
546 line number in *LINE_NUMBER. Returns true if a line was
547 obtained, false if the source is exhausted. */
549 do_repeat_read (struct string *output, char **file_name, int *line_number,
552 struct repeat_block *block = block_;
553 struct line_list *line;
555 if (block->cur_line == NULL)
558 if (block->loop_idx >= block->loop_cnt)
560 block->cur_line = block->first_line;
562 line = block->cur_line;
564 ds_replace (output, line->line);
565 *file_name = line->file_name;
566 *line_number = -line->line_number;
567 block->cur_line = line->next;
571 /* Frees a DO REPEAT block.
572 Called by getl to close out the DO REPEAT block. */
574 do_repeat_close (void *block_)
576 struct repeat_block *block = block_;
577 pool_destroy (block->pool);