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"
31 #include "line-buffer.h"
40 #define _(msgid) gettext (msgid)
42 #include "debug-print.h"
44 /* Defines a list of lines used by DO REPEAT. */
47 struct line_list *next; /* Next line. */
48 char *file_name; /* File name. */
49 int line_number; /* Line number. */
50 char *line; /* Contents. */
53 /* The type of substitution made for a DO REPEAT macro. */
54 enum repeat_entry_type
60 /* Describes one DO REPEAT macro. */
63 struct repeat_entry *next; /* Next entry. */
64 enum repeat_entry_type type; /* Types of replacements. */
65 char id[LONG_NAME_LEN + 1]; /* Macro identifier. */
66 char **replacement; /* Macro replacement. */
69 /* A DO REPEAT...END REPEAT block. */
72 struct pool *pool; /* Pool used for storage. */
73 struct line_list *first_line; /* First line in line buffer. */
74 struct line_list *cur_line; /* Current line in line buffer. */
75 int loop_cnt; /* Number of loops. */
76 int loop_idx; /* Number of loops so far. */
77 struct repeat_entry *macros; /* Pointer to macro table. */
78 bool print; /* Print lines as executed? */
81 static bool parse_specification (struct repeat_block *);
82 static bool parse_lines (struct repeat_block *);
83 static void create_vars (struct repeat_block *);
85 static int parse_ids (struct repeat_entry *);
86 static int parse_numbers (struct repeat_entry *);
87 static int parse_strings (struct repeat_entry *);
89 static void do_repeat_filter (struct string *line, void *block);
90 static bool do_repeat_read (struct string *line, char **file_name,
91 int *line_number, void *block);
92 static void do_repeat_close (void *block);
97 struct repeat_block *block;
99 block = pool_create_container (struct repeat_block, pool);
101 if (!parse_specification (block) || !parse_lines (block))
106 block->cur_line = NULL;
107 block->loop_idx = -1;
108 getl_include_filter (do_repeat_filter, do_repeat_close, block);
109 getl_include_function (do_repeat_read, NULL, block);
114 pool_destroy (block->pool);
115 return CMD_CASCADING_FAILURE;
118 /* Parses the whole DO REPEAT command specification.
121 parse_specification (struct repeat_block *block)
123 char first_name[LONG_NAME_LEN + 1];
126 block->macros = NULL;
129 struct repeat_entry *e;
130 struct repeat_entry *iter;
133 /* Get a stand-in variable name and make sure it's unique. */
134 if (!lex_force_id ())
136 if (dict_lookup_var (default_dict, tokid))
137 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
140 for (iter = block->macros; iter != NULL; iter = iter->next)
141 if (!strcasecmp (iter->id, tokid))
143 msg (SE, _("Dummy variable name \"%s\" is given twice."), tokid);
147 /* Make a new stand-in variable entry and link it into the
149 e = pool_alloc (block->pool, sizeof *e);
150 e->next = block->macros;
151 strcpy (e->id, tokid);
154 /* Skip equals sign. */
156 if (!lex_force_match ('='))
159 /* Get the details of the variable's possible values. */
161 count = parse_ids (e);
162 else if (lex_is_number ())
163 count = parse_numbers (e);
164 else if (token == T_STRING)
165 count = parse_strings (e);
174 /* If this is the first variable then it defines how many
175 replacements there must be; otherwise enforce this number of
177 if (block->loop_cnt == 0)
179 block->loop_cnt = count;
180 strcpy (first_name, e->id);
182 else if (block->loop_cnt != count)
184 msg (SE, _("Dummy variable \"%s\" had %d "
185 "substitutions, so \"%s\" must also, but %d "
187 first_name, block->loop_cnt, e->id, count);
193 while (token != '.');
198 /* If KEYWORD appears beginning at CP, possibly preceded by white
199 space, returns a pointer to the character just after the
200 keyword. Otherwise, returns a null pointer. */
202 recognize_keyword (const char *cp, const char *keyword)
206 while (isspace ((unsigned char) *cp))
209 end = lex_skip_identifier (cp);
211 && lex_id_match_len (keyword, strlen (keyword), cp, end - cp))
217 /* Returns CP, advanced past a '+' or '-' if present. */
219 skip_indentor (const char *cp)
221 if (*cp == '+' || *cp == '-')
226 /* Returns true if LINE contains a DO REPEAT command, false
229 recognize_do_repeat (const char *line)
231 const char *cp = recognize_keyword (skip_indentor (line), "do");
232 return cp != NULL && recognize_keyword (cp, "repeat") != NULL;
235 /* Returns true if LINE contains an END REPEAT command, false
236 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
239 recognize_end_repeat (const char *line, bool *print)
241 const char *cp = recognize_keyword (skip_indentor (line), "end");
245 cp = recognize_keyword (cp, "repeat");
249 *print = recognize_keyword (cp, "print");
253 /* Read all the lines we are going to substitute, inside the DO
254 REPEAT...END REPEAT block. */
256 parse_lines (struct repeat_block *block)
258 char *previous_file_name;
259 struct line_list **last_line;
262 previous_file_name = NULL;
263 block->first_line = NULL;
264 last_line = &block->first_line;
269 const char *cur_file_name;
271 struct line_list *line;
274 if (!getl_read_line (NULL))
277 /* If the current file has changed then record the fact. */
278 getl_location (&cur_file_name, &cur_line_number);
279 if (previous_file_name == NULL
280 || !strcmp (cur_file_name, previous_file_name))
281 previous_file_name = pool_strdup (block->pool, cur_file_name);
283 ds_rtrim_spaces (&getl_buf);
284 dot = ds_chomp (&getl_buf, get_endcmd ());
285 if (recognize_do_repeat (ds_c_str (&getl_buf)))
287 else if (recognize_end_repeat (ds_c_str (&getl_buf), &block->print))
289 if (nesting_level-- == 0)
296 ds_putc (&getl_buf, get_endcmd ());
298 line = *last_line = pool_alloc (block->pool, sizeof *line);
300 line->file_name = previous_file_name;
301 line->line_number = cur_line_number;
302 line->line = pool_strdup (block->pool, ds_c_str (&getl_buf));
303 last_line = &line->next;
310 /* Creates variables for the given DO REPEAT. */
312 create_vars (struct repeat_block *block)
314 struct repeat_entry *iter;
316 for (iter = block->macros; iter; iter = iter->next)
317 if (iter->type == VAR_NAMES)
321 for (i = 0; i < block->loop_cnt; i++)
323 /* Ignore return value: if the variable already
324 exists there is no harm done. */
325 dict_create_var (default_dict, iter->replacement[i], 0);
330 /* Parses a set of ids for DO REPEAT. */
332 parse_ids (struct repeat_entry *e)
338 e->replacement = NULL;
345 if (!parse_mixed_vars (&names, &nnames, PV_NONE))
348 e->replacement = xnrealloc (e->replacement,
349 nnames + n, sizeof *e->replacement);
350 for (i = 0; i < nnames; i++)
352 e->replacement[n + i] = xstrdup (names[i]);
358 while (token != '/' && token != '.');
363 /* Stores VALUE into *REPL. */
365 store_numeric (char **repl, long value)
367 *repl = xmalloc (INT_STRLEN_BOUND (value) + 1);
368 sprintf (*repl, "%ld", value);
371 /* Parses a list of numbers for DO REPEAT. */
373 parse_numbers (struct repeat_entry *e)
375 /* First and last numbers for TO, plus the step factor. */
378 /* Alias to e->replacement. */
381 /* Number of entries in array; maximum number for this allocation
387 e->replacement = array = NULL;
391 /* Parse A TO B into a, b. */
392 if (!lex_force_int ())
400 if (!lex_force_int ())
408 if (n + (abs (b - a) + 1) > m)
410 m = n + (abs (b - a) + 1) + 16;
411 e->replacement = array = xnrealloc (array,
412 m, sizeof *e->replacement);
416 store_numeric (&array[n++], a);
422 for (iter = a; iter <= b; iter++)
423 store_numeric (&array[n++], iter);
425 for (iter = a; iter >= b; iter--)
426 store_numeric (&array[n++], iter);
431 while (token != '/' && token != '.');
432 e->replacement = xrealloc (array, n * sizeof *e->replacement);
437 /* Parses a list of strings for DO REPEAT. */
439 parse_strings (struct repeat_entry *e)
445 string = e->replacement = NULL;
450 if (token != T_STRING)
453 msg (SE, _("String expected."));
454 for (i = 0; i < n; i++)
463 e->replacement = string = xnrealloc (string,
464 m, sizeof *e->replacement);
466 string[n++] = lex_token_representation ();
471 while (token != '/' && token != '.');
472 e->replacement = xnrealloc (string, n, sizeof *e->replacement);
478 cmd_end_repeat (void)
480 msg (SE, _("No matching DO REPEAT."));
481 return CMD_CASCADING_FAILURE;
484 /* Finds a DO REPEAT macro with name MACRO_NAME and returns the
485 appropriate subsitution if found, or NULL if not. */
487 find_substitution (struct repeat_block *block, const char *name, size_t length)
489 struct repeat_entry *e;
491 for (e = block->macros; e; e = e->next)
492 if (!memcasecmp (e->id, name, length) && strlen (e->id) == length)
493 return e->replacement[block->loop_idx];
498 /* Makes appropriate DO REPEAT macro substitutions within getl_buf. */
500 do_repeat_filter (struct string *line, void *block_)
502 struct repeat_block *block = block_;
503 bool in_apos, in_quote;
505 struct string output;
508 ds_init (&output, ds_capacity (line));
510 /* Strip trailing whitespace, check for & remove terminal dot. */
511 while (isspace (ds_last (line)))
512 ds_truncate (line, ds_length (line) - 1);
513 dot = ds_chomp (line, get_endcmd ());
515 in_apos = in_quote = false;
516 for (cp = ds_c_str (line); cp < ds_end (line); )
518 if (*cp == '\'' && !in_quote)
520 else if (*cp == '"' && !in_apos)
521 in_quote = !in_quote;
523 if (in_quote || in_apos || !lex_is_id1 (*cp))
524 ds_putc (&output, *cp++);
527 const char *start = cp;
528 char *end = lex_skip_identifier (start);
529 const char *substitution = find_substitution (block,
531 if (substitution != NULL)
532 ds_puts (&output, substitution);
534 ds_concat (&output, start, end - start);
539 ds_putc (&output, get_endcmd ());
541 ds_swap (line, &output);
542 ds_destroy (&output);
545 /* Function called by getl to read a line.
546 Puts the line in OUTPUT, sets the file name in *FILE_NAME and
547 line number in *LINE_NUMBER. Returns true if a line was
548 obtained, false if the source is exhausted. */
550 do_repeat_read (struct string *output, char **file_name, int *line_number,
553 struct repeat_block *block = block_;
554 struct line_list *line;
556 if (block->cur_line == NULL)
559 if (block->loop_idx >= block->loop_cnt)
561 block->cur_line = block->first_line;
563 line = block->cur_line;
565 ds_replace (output, line->line);
566 *file_name = line->file_name;
567 *line_number = -line->line_number;
568 block->cur_line = line->next;
572 /* Frees a DO REPEAT block.
573 Called by getl to close out the DO REPEAT block. */
575 do_repeat_close (void *block_)
577 struct repeat_block *block = block_;
578 pool_destroy (block->pool);