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 <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/settings.h>
31 #include <language/command.h>
32 #include <language/lexer/lexer.h>
33 #include <language/line-buffer.h>
34 #include <libpspp/alloc.h>
35 #include <libpspp/message.h>
36 #include <libpspp/message.h>
37 #include <libpspp/misc.h>
38 #include <libpspp/pool.h>
39 #include <libpspp/str.h>
40 #include <data/variable.h>
45 #define _(msgid) gettext (msgid)
47 /* Defines a list of lines used by DO REPEAT. */
50 struct line_list *next; /* Next line. */
51 char *file_name; /* File name. */
52 int line_number; /* Line number. */
53 char *line; /* Contents. */
56 /* The type of substitution made for a DO REPEAT macro. */
57 enum repeat_entry_type
63 /* Describes one DO REPEAT macro. */
66 struct repeat_entry *next; /* Next entry. */
67 enum repeat_entry_type type; /* Types of replacements. */
68 char id[LONG_NAME_LEN + 1]; /* Macro identifier. */
69 char **replacement; /* Macro replacement. */
72 /* A DO REPEAT...END REPEAT block. */
75 struct pool *pool; /* Pool used for storage. */
76 struct line_list *first_line; /* First line in line buffer. */
77 struct line_list *cur_line; /* Current line in line buffer. */
78 int loop_cnt; /* Number of loops. */
79 int loop_idx; /* Number of loops so far. */
80 struct repeat_entry *macros; /* Pointer to macro table. */
81 bool print; /* Print lines as executed? */
84 static bool parse_specification (struct repeat_block *);
85 static bool parse_lines (struct repeat_block *);
86 static void create_vars (struct repeat_block *);
88 static int parse_ids (struct repeat_entry *, struct pool *);
89 static int parse_numbers (struct repeat_entry *, struct pool *);
90 static int parse_strings (struct repeat_entry *, struct pool *);
92 static void do_repeat_filter (struct string *line, void *block);
93 static bool do_repeat_read (struct string *line, char **file_name,
94 int *line_number, void *block);
95 static void do_repeat_close (void *block);
100 struct repeat_block *block;
102 block = pool_create_container (struct repeat_block, pool);
104 if (!parse_specification (block) || !parse_lines (block))
109 block->cur_line = NULL;
110 block->loop_idx = -1;
111 getl_include_filter (do_repeat_filter, do_repeat_close, block);
112 getl_include_function (do_repeat_read, NULL, block);
117 pool_destroy (block->pool);
118 return CMD_CASCADING_FAILURE;
121 /* Parses the whole DO REPEAT command specification.
124 parse_specification (struct repeat_block *block)
126 char first_name[LONG_NAME_LEN + 1];
129 block->macros = NULL;
132 struct repeat_entry *e;
133 struct repeat_entry *iter;
136 /* Get a stand-in variable name and make sure it's unique. */
137 if (!lex_force_id ())
139 if (dict_lookup_var (default_dict, tokid))
140 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
143 for (iter = block->macros; iter != NULL; iter = iter->next)
144 if (!strcasecmp (iter->id, tokid))
146 msg (SE, _("Dummy variable name \"%s\" is given twice."), tokid);
150 /* Make a new stand-in variable entry and link it into the
152 e = pool_alloc (block->pool, sizeof *e);
153 e->next = block->macros;
154 strcpy (e->id, tokid);
157 /* Skip equals sign. */
159 if (!lex_force_match ('='))
162 /* Get the details of the variable's possible values. */
164 count = parse_ids (e, block->pool);
165 else if (lex_is_number ())
166 count = parse_numbers (e, block->pool);
167 else if (token == T_STRING)
168 count = parse_strings (e, block->pool);
176 if (token != '/' && token != '.')
182 /* If this is the first variable then it defines how many
183 replacements there must be; otherwise enforce this number of
185 if (block->loop_cnt == 0)
187 block->loop_cnt = count;
188 strcpy (first_name, e->id);
190 else if (block->loop_cnt != count)
192 msg (SE, _("Dummy variable \"%s\" had %d "
193 "substitutions, so \"%s\" must also, but %d "
195 first_name, block->loop_cnt, e->id, count);
201 while (token != '.');
206 /* If KEYWORD appears beginning at CP, possibly preceded by white
207 space, returns a pointer to the character just after the
208 keyword. Otherwise, returns a null pointer. */
210 recognize_keyword (const char *cp, const char *keyword)
214 while (isspace ((unsigned char) *cp))
217 end = lex_skip_identifier (cp);
219 && lex_id_match_len (keyword, strlen (keyword), cp, end - cp))
225 /* Returns CP, advanced past a '+' or '-' if present. */
227 skip_indentor (const char *cp)
229 if (*cp == '+' || *cp == '-')
234 /* Returns true if LINE contains a DO REPEAT command, false
237 recognize_do_repeat (const char *line)
239 const char *cp = recognize_keyword (skip_indentor (line), "do");
240 return cp != NULL && recognize_keyword (cp, "repeat") != NULL;
243 /* Returns true if LINE contains an END REPEAT command, false
244 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
247 recognize_end_repeat (const char *line, bool *print)
249 const char *cp = recognize_keyword (skip_indentor (line), "end");
253 cp = recognize_keyword (cp, "repeat");
257 *print = recognize_keyword (cp, "print");
261 /* Read all the lines we are going to substitute, inside the DO
262 REPEAT...END REPEAT block. */
264 parse_lines (struct repeat_block *block)
266 char *previous_file_name;
267 struct line_list **last_line;
270 previous_file_name = NULL;
271 block->first_line = NULL;
272 last_line = &block->first_line;
277 const char *cur_file_name;
279 struct line_list *line;
282 if (!getl_read_line (NULL))
285 /* If the current file has changed then record the fact. */
286 getl_location (&cur_file_name, &cur_line_number);
287 if (previous_file_name == NULL
288 || !strcmp (cur_file_name, previous_file_name))
289 previous_file_name = pool_strdup (block->pool, cur_file_name);
291 ds_rtrim_spaces (&getl_buf);
292 dot = ds_chomp (&getl_buf, get_endcmd ());
293 if (recognize_do_repeat (ds_c_str (&getl_buf)))
295 else if (recognize_end_repeat (ds_c_str (&getl_buf), &block->print))
297 if (nesting_level-- == 0)
304 ds_putc (&getl_buf, get_endcmd ());
306 line = *last_line = pool_alloc (block->pool, sizeof *line);
308 line->file_name = previous_file_name;
309 line->line_number = cur_line_number;
310 line->line = pool_strdup (block->pool, ds_c_str (&getl_buf));
311 last_line = &line->next;
318 /* Creates variables for the given DO REPEAT. */
320 create_vars (struct repeat_block *block)
322 struct repeat_entry *iter;
324 for (iter = block->macros; iter; iter = iter->next)
325 if (iter->type == VAR_NAMES)
329 for (i = 0; i < block->loop_cnt; i++)
331 /* Ignore return value: if the variable already
332 exists there is no harm done. */
333 dict_create_var (default_dict, iter->replacement[i], 0);
338 /* Parses a set of ids for DO REPEAT. */
340 parse_ids (struct repeat_entry *e, struct pool *pool)
344 return parse_mixed_vars_pool (pool, &e->replacement, &n, PV_NONE) ? n : 0;
347 /* Adds STRING to E's list of replacements, which has *USED
348 elements and has room for *ALLOCATED. Allocates memory from
351 add_replacement (char *string,
352 struct repeat_entry *e, struct pool *pool,
353 size_t *used, size_t *allocated)
355 if (*used == *allocated)
356 e->replacement = pool_2nrealloc (pool, e->replacement, allocated,
357 sizeof *e->replacement);
358 e->replacement[(*used)++] = string;
361 /* Parses a list of numbers for DO REPEAT. */
363 parse_numbers (struct repeat_entry *e, struct pool *pool)
366 size_t allocated = 0;
369 e->replacement = NULL;
375 /* Parse A TO B into a, b. */
376 if (!lex_force_int ())
384 if (!lex_force_int ())
389 msg (SE, _("%ld TO %ld is an invalid range."), a, b);
397 for (i = a; i <= b; i++)
398 add_replacement (pool_asprintf (pool, "%ld", i),
399 e, pool, &used, &allocated);
404 while (token != '/' && token != '.');
409 /* Parses a list of strings for DO REPEAT. */
411 parse_strings (struct repeat_entry *e, struct pool *pool)
414 size_t allocated = 0;
417 e->replacement = NULL;
423 if (token != T_STRING)
425 msg (SE, _("String expected."));
429 string = lex_token_representation ();
430 pool_register (pool, free, string);
431 add_replacement (string, e, pool, &used, &allocated);
436 while (token != '/' && token != '.');
442 cmd_end_repeat (void)
444 msg (SE, _("No matching DO REPEAT."));
445 return CMD_CASCADING_FAILURE;
448 /* Finds a DO REPEAT macro with name MACRO_NAME and returns the
449 appropriate subsitution if found, or NULL if not. */
451 find_substitution (struct repeat_block *block, const char *name, size_t length)
453 struct repeat_entry *e;
455 for (e = block->macros; e; e = e->next)
456 if (!memcasecmp (e->id, name, length) && strlen (e->id) == length)
457 return e->replacement[block->loop_idx];
462 /* Makes appropriate DO REPEAT macro substitutions within getl_buf. */
464 do_repeat_filter (struct string *line, void *block_)
466 struct repeat_block *block = block_;
467 bool in_apos, in_quote;
469 struct string output;
472 ds_init (&output, ds_capacity (line));
474 /* Strip trailing whitespace, check for & remove terminal dot. */
475 while (isspace (ds_last (line)))
476 ds_truncate (line, ds_length (line) - 1);
477 dot = ds_chomp (line, get_endcmd ());
479 in_apos = in_quote = false;
480 for (cp = ds_c_str (line); cp < ds_end (line); )
482 if (*cp == '\'' && !in_quote)
484 else if (*cp == '"' && !in_apos)
485 in_quote = !in_quote;
487 if (in_quote || in_apos || !lex_is_id1 (*cp))
488 ds_putc (&output, *cp++);
491 const char *start = cp;
492 char *end = lex_skip_identifier (start);
493 const char *substitution = find_substitution (block,
495 if (substitution != NULL)
496 ds_puts (&output, substitution);
498 ds_concat (&output, start, end - start);
503 ds_putc (&output, get_endcmd ());
505 ds_swap (line, &output);
506 ds_destroy (&output);
509 /* Function called by getl to read a line.
510 Puts the line in OUTPUT, sets the file name in *FILE_NAME and
511 line number in *LINE_NUMBER. Returns true if a line was
512 obtained, false if the source is exhausted. */
514 do_repeat_read (struct string *output, char **file_name, int *line_number,
517 struct repeat_block *block = block_;
518 struct line_list *line;
520 if (block->cur_line == NULL)
523 if (block->loop_idx >= block->loop_cnt)
525 block->cur_line = block->first_line;
527 line = block->cur_line;
529 ds_assign_c_str (output, line->line);
530 *file_name = line->file_name;
531 *line_number = -line->line_number;
532 block->cur_line = line->next;
536 /* Frees a DO REPEAT block.
537 Called by getl to close out the DO REPEAT block. */
539 do_repeat_close (void *block_)
541 struct repeat_block *block = block_;
542 pool_destroy (block->pool);