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 <libpspp/getl.h>
32 #include <language/command.h>
33 #include <language/lexer/lexer.h>
34 #include <language/lexer/variable-parser.h>
35 #include <libpspp/alloc.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 getl_interface parent ;
77 struct pool *pool; /* Pool used for storage. */
78 struct dataset *ds; /* The dataset for this block */
79 struct line_list *first_line; /* First line in line buffer. */
80 struct line_list *cur_line; /* Current line in line buffer. */
81 int loop_cnt; /* Number of loops. */
82 int loop_idx; /* Number of loops so far. */
83 struct repeat_entry *macros; /* Pointer to macro table. */
84 bool print; /* Print lines as executed? */
87 static bool parse_specification (struct lexer *, struct repeat_block *);
88 static bool parse_lines (struct lexer *, struct repeat_block *);
89 static void create_vars (struct repeat_block *);
91 static int parse_ids (struct lexer *, const struct dictionary *dict,
92 struct repeat_entry *, struct pool *);
94 static int parse_numbers (struct lexer *, struct repeat_entry *,
97 static int parse_strings (struct lexer *, struct repeat_entry *,
100 static void do_repeat_filter (struct getl_interface *, struct string *);
101 static bool do_repeat_read (struct getl_interface *, struct string *);
102 static void do_repeat_close (struct getl_interface *);
103 static bool always_false (const struct getl_interface *i UNUSED);
107 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
109 struct repeat_block *block;
111 block = pool_create_container (struct repeat_block, pool);
114 if (!parse_specification (lexer, block) || !parse_lines (lexer, block))
119 block->cur_line = NULL;
120 block->loop_idx = -1;
122 block->parent.read = do_repeat_read;
123 block->parent.close = do_repeat_close;
124 block->parent.filter = do_repeat_filter;
125 block->parent.interactive = always_false;
127 getl_include_source ( (struct getl_interface *) block);
132 pool_destroy (block->pool);
133 return CMD_CASCADING_FAILURE;
136 /* Parses the whole DO REPEAT command specification.
139 parse_specification (struct lexer *lexer, struct repeat_block *block)
141 char first_name[LONG_NAME_LEN + 1];
144 block->macros = NULL;
147 struct repeat_entry *e;
148 struct repeat_entry *iter;
149 struct dictionary *dict = dataset_dict (block->ds);
152 /* Get a stand-in variable name and make sure it's unique. */
153 if (!lex_force_id (lexer))
155 if (dict_lookup_var (dict, lex_tokid (lexer)))
156 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
158 lex_tokid (lexer), lex_tokid (lexer));
159 for (iter = block->macros; iter != NULL; iter = iter->next)
160 if (!strcasecmp (iter->id, lex_tokid (lexer)))
162 msg (SE, _("Dummy variable name \"%s\" is given twice."),
167 /* Make a new stand-in variable entry and link it into the
169 e = pool_alloc (block->pool, sizeof *e);
170 e->next = block->macros;
171 strcpy (e->id, lex_tokid (lexer));
174 /* Skip equals sign. */
176 if (!lex_force_match (lexer, '='))
179 /* Get the details of the variable's possible values. */
180 if (lex_token (lexer) == T_ID)
181 count = parse_ids (lexer, dict, e, block->pool);
182 else if (lex_is_number (lexer))
183 count = parse_numbers (lexer, e, block->pool);
184 else if (lex_token (lexer) == T_STRING)
185 count = parse_strings (lexer, e, block->pool);
188 lex_error (lexer, NULL);
193 if (lex_token (lexer) != '/' && lex_token (lexer) != '.')
195 lex_error (lexer, NULL);
199 /* If this is the first variable then it defines how many
200 replacements there must be; otherwise enforce this number of
202 if (block->loop_cnt == 0)
204 block->loop_cnt = count;
205 strcpy (first_name, e->id);
207 else if (block->loop_cnt != count)
209 msg (SE, _("Dummy variable \"%s\" had %d "
210 "substitutions, so \"%s\" must also, but %d "
212 first_name, block->loop_cnt, e->id, count);
216 lex_match (lexer, '/');
218 while (lex_token (lexer) != '.');
223 /* If KEYWORD appears beginning at CP, possibly preceded by white
224 space, returns a pointer to the character just after the
225 keyword. Otherwise, returns a null pointer. */
227 recognize_keyword (const char *cp, const char *keyword)
231 while (isspace ((unsigned char) *cp))
234 end = lex_skip_identifier (cp);
236 && lex_id_match_len (keyword, strlen (keyword), cp, end - cp))
242 /* Returns CP, advanced past a '+' or '-' if present. */
244 skip_indentor (const char *cp)
246 if (*cp == '+' || *cp == '-')
251 /* Returns true if LINE contains a DO REPEAT command, false
254 recognize_do_repeat (const char *line)
256 const char *cp = recognize_keyword (skip_indentor (line), "do");
257 return cp != NULL && recognize_keyword (cp, "repeat") != NULL;
260 /* Returns true if LINE contains an END REPEAT command, false
261 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
264 recognize_end_repeat (const char *line, bool *print)
266 const char *cp = recognize_keyword (skip_indentor (line), "end");
270 cp = recognize_keyword (cp, "repeat");
274 *print = recognize_keyword (cp, "print");
278 /* Read all the lines we are going to substitute, inside the DO
279 REPEAT...END REPEAT block. */
281 parse_lines (struct lexer *lexer, struct repeat_block *block)
283 char *previous_file_name;
284 struct line_list **last_line;
287 previous_file_name = NULL;
288 block->first_line = NULL;
289 last_line = &block->first_line;
294 const char *cur_file_name = getl_source_name ();
295 int cur_line_number = getl_source_location ();
296 struct line_list *line;
297 struct string cur_line_copy;
300 if (! lex_get_line_raw (lexer))
303 /* If the current file has changed then record the fact. */
305 (previous_file_name == NULL
306 || !strcmp (cur_file_name, previous_file_name))
308 previous_file_name = pool_strdup (block->pool, cur_file_name);
310 ds_init_string (&cur_line_copy, lex_entire_line_ds (lexer) );
311 ds_rtrim (&cur_line_copy, ss_cstr (CC_SPACES));
312 dot = ds_chomp (&cur_line_copy, get_endcmd ());
314 if (recognize_do_repeat (ds_cstr (&cur_line_copy)))
316 else if (recognize_end_repeat (ds_cstr (&cur_line_copy), &block->print))
318 if (nesting_level-- == 0)
320 lex_discard_line (lexer);
321 ds_destroy (&cur_line_copy);
326 ds_put_char (&cur_line_copy, get_endcmd ());
328 line = *last_line = pool_alloc (block->pool, sizeof *line);
330 line->file_name = previous_file_name;
331 line->line_number = cur_line_number;
332 line->line = pool_strdup (block->pool, ds_cstr (&cur_line_copy) );
333 last_line = &line->next;
335 ds_destroy (&cur_line_copy);
338 lex_discard_line (lexer);
342 /* Creates variables for the given DO REPEAT. */
344 create_vars (struct repeat_block *block)
346 struct repeat_entry *iter;
348 for (iter = block->macros; iter; iter = iter->next)
349 if (iter->type == VAR_NAMES)
353 for (i = 0; i < block->loop_cnt; i++)
355 /* Ignore return value: if the variable already
356 exists there is no harm done. */
357 dict_create_var (dataset_dict (block->ds), iter->replacement[i], 0);
362 /* Parses a set of ids for DO REPEAT. */
364 parse_ids (struct lexer *lexer, const struct dictionary *dict,
365 struct repeat_entry *e, struct pool *pool)
369 return parse_mixed_vars_pool (lexer, dict, pool,
370 &e->replacement, &n, PV_NONE) ? n : 0;
373 /* Adds STRING to E's list of replacements, which has *USED
374 elements and has room for *ALLOCATED. Allocates memory from
377 add_replacement (char *string,
378 struct repeat_entry *e, struct pool *pool,
379 size_t *used, size_t *allocated)
381 if (*used == *allocated)
382 e->replacement = pool_2nrealloc (pool, e->replacement, allocated,
383 sizeof *e->replacement);
384 e->replacement[(*used)++] = string;
387 /* Parses a list of numbers for DO REPEAT. */
389 parse_numbers (struct lexer *lexer, struct repeat_entry *e, struct pool *pool)
392 size_t allocated = 0;
395 e->replacement = NULL;
401 /* Parse A TO B into a, b. */
402 if (!lex_force_int (lexer))
404 a = lex_integer (lexer);
407 if (lex_token (lexer) == T_TO)
410 if (!lex_force_int (lexer))
412 b = lex_integer (lexer);
415 msg (SE, _("%ld TO %ld is an invalid range."), a, b);
423 for (i = a; i <= b; i++)
424 add_replacement (pool_asprintf (pool, "%ld", i),
425 e, pool, &used, &allocated);
428 lex_match (lexer, ',');
430 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
435 /* Parses a list of strings for DO REPEAT. */
437 parse_strings (struct lexer *lexer, struct repeat_entry *e, struct pool *pool)
440 size_t allocated = 0;
443 e->replacement = NULL;
449 if (lex_token (lexer) != T_STRING)
451 msg (SE, _("String expected."));
455 string = lex_token_representation (lexer);
456 pool_register (pool, free, string);
457 add_replacement (string, e, pool, &used, &allocated);
460 lex_match (lexer, ',');
462 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
468 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
470 msg (SE, _("No matching DO REPEAT."));
471 return CMD_CASCADING_FAILURE;
474 /* Finds a DO REPEAT macro with name MACRO_NAME and returns the
475 appropriate subsitution if found, or NULL if not. */
477 find_substitution (struct repeat_block *block, const char *name, size_t length)
479 struct repeat_entry *e;
481 for (e = block->macros; e; e = e->next)
482 if (!memcasecmp (e->id, name, length) && strlen (e->id) == length)
483 return e->replacement[block->loop_idx];
488 /* Makes appropriate DO REPEAT macro substitutions within the
491 do_repeat_filter (struct getl_interface *block_, struct string *line)
493 struct repeat_block *block = (struct repeat_block *) block_;
494 bool in_apos, in_quote;
496 struct string output;
499 ds_init_empty (&output);
501 /* Strip trailing whitespace, check for & remove terminal dot. */
502 while (isspace (ds_last (line)))
503 ds_truncate (line, ds_length (line) - 1);
504 dot = ds_chomp (line, get_endcmd ());
506 in_apos = in_quote = false;
507 for (cp = ds_cstr (line); cp < ds_end (line); )
509 if (*cp == '\'' && !in_quote)
511 else if (*cp == '"' && !in_apos)
512 in_quote = !in_quote;
514 if (in_quote || in_apos || !lex_is_id1 (*cp))
515 ds_put_char (&output, *cp++);
518 const char *start = cp;
519 char *end = lex_skip_identifier (start);
520 const char *substitution = find_substitution (block,
522 if (substitution != NULL)
523 ds_put_cstr (&output, substitution);
525 ds_put_substring (&output, ss_buffer (start, end - start));
530 ds_put_char (&output, get_endcmd ());
532 ds_swap (line, &output);
533 ds_destroy (&output);
536 /* Function called by getl to read a line.
537 Puts the line in OUTPUT, sets the file name in *FILE_NAME and
538 line number in *LINE_NUMBER. Returns true if a line was
539 obtained, false if the source is exhausted. */
541 do_repeat_read (struct getl_interface *b, struct string *output)
543 struct repeat_block *block = (struct repeat_block *) b;
544 struct line_list *line;
546 if (block->cur_line == NULL)
549 if (block->loop_idx >= block->loop_cnt)
551 block->cur_line = block->first_line;
552 if (block->cur_line == NULL)
555 line = block->cur_line;
557 ds_assign_cstr (output, line->line);
558 block->cur_line = line->next;
562 /* Frees a DO REPEAT block.
563 Called by getl to close out the DO REPEAT block. */
565 do_repeat_close (struct getl_interface *block_)
567 struct repeat_block *block = (struct repeat_block *) block_;
568 pool_destroy (block->pool);
573 always_false (const struct getl_interface *i UNUSED)