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/lexer/variable-parser.h>
34 #include <language/line-buffer.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/message.h>
37 #include <libpspp/message.h>
38 #include <libpspp/misc.h>
39 #include <libpspp/pool.h>
40 #include <libpspp/str.h>
41 #include <data/variable.h>
46 #define _(msgid) gettext (msgid)
48 /* Defines a list of lines used by DO REPEAT. */
51 struct line_list *next; /* Next line. */
52 char *file_name; /* File name. */
53 int line_number; /* Line number. */
54 char *line; /* Contents. */
57 /* The type of substitution made for a DO REPEAT macro. */
58 enum repeat_entry_type
64 /* Describes one DO REPEAT macro. */
67 struct repeat_entry *next; /* Next entry. */
68 enum repeat_entry_type type; /* Types of replacements. */
69 char id[LONG_NAME_LEN + 1]; /* Macro identifier. */
70 char **replacement; /* Macro replacement. */
73 /* A DO REPEAT...END REPEAT block. */
76 struct pool *pool; /* Pool used for storage. */
77 struct dataset *ds; /* The dataset for this block */
78 struct line_list *first_line; /* First line in line buffer. */
79 struct line_list *cur_line; /* Current line in line buffer. */
80 int loop_cnt; /* Number of loops. */
81 int loop_idx; /* Number of loops so far. */
82 struct repeat_entry *macros; /* Pointer to macro table. */
83 bool print; /* Print lines as executed? */
86 static bool parse_specification (struct repeat_block *);
87 static bool parse_lines (struct repeat_block *);
88 static void create_vars (struct repeat_block *);
90 static int parse_ids (const struct dictionary *dict, struct repeat_entry *, struct pool *);
91 static int parse_numbers (struct repeat_entry *, struct pool *);
92 static int parse_strings (struct repeat_entry *, struct pool *);
94 static void do_repeat_filter (struct string *line, void *block);
95 static bool do_repeat_read (struct string *line, char **file_name,
96 int *line_number, void *block);
97 static void do_repeat_close (void *block);
100 cmd_do_repeat (struct dataset *ds)
102 struct repeat_block *block;
104 block = pool_create_container (struct repeat_block, pool);
107 if (!parse_specification (block) || !parse_lines (block))
112 block->cur_line = NULL;
113 block->loop_idx = -1;
114 getl_include_filter (do_repeat_filter, do_repeat_close, block);
115 getl_include_function (do_repeat_read, NULL, block);
120 pool_destroy (block->pool);
121 return CMD_CASCADING_FAILURE;
124 /* Parses the whole DO REPEAT command specification.
127 parse_specification (struct repeat_block *block)
129 char first_name[LONG_NAME_LEN + 1];
132 block->macros = NULL;
135 struct repeat_entry *e;
136 struct repeat_entry *iter;
137 struct dictionary *dict = dataset_dict (block->ds);
140 /* Get a stand-in variable name and make sure it's unique. */
141 if (!lex_force_id ())
143 if (dict_lookup_var (dict, tokid))
144 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
147 for (iter = block->macros; iter != NULL; iter = iter->next)
148 if (!strcasecmp (iter->id, tokid))
150 msg (SE, _("Dummy variable name \"%s\" is given twice."), tokid);
154 /* Make a new stand-in variable entry and link it into the
156 e = pool_alloc (block->pool, sizeof *e);
157 e->next = block->macros;
158 strcpy (e->id, tokid);
161 /* Skip equals sign. */
163 if (!lex_force_match ('='))
166 /* Get the details of the variable's possible values. */
168 count = parse_ids (dict, e, block->pool);
169 else if (lex_is_number ())
170 count = parse_numbers (e, block->pool);
171 else if (token == T_STRING)
172 count = parse_strings (e, block->pool);
180 if (token != '/' && token != '.')
186 /* If this is the first variable then it defines how many
187 replacements there must be; otherwise enforce this number of
189 if (block->loop_cnt == 0)
191 block->loop_cnt = count;
192 strcpy (first_name, e->id);
194 else if (block->loop_cnt != count)
196 msg (SE, _("Dummy variable \"%s\" had %d "
197 "substitutions, so \"%s\" must also, but %d "
199 first_name, block->loop_cnt, e->id, count);
205 while (token != '.');
210 /* If KEYWORD appears beginning at CP, possibly preceded by white
211 space, returns a pointer to the character just after the
212 keyword. Otherwise, returns a null pointer. */
214 recognize_keyword (const char *cp, const char *keyword)
218 while (isspace ((unsigned char) *cp))
221 end = lex_skip_identifier (cp);
223 && lex_id_match_len (keyword, strlen (keyword), cp, end - cp))
229 /* Returns CP, advanced past a '+' or '-' if present. */
231 skip_indentor (const char *cp)
233 if (*cp == '+' || *cp == '-')
238 /* Returns true if LINE contains a DO REPEAT command, false
241 recognize_do_repeat (const char *line)
243 const char *cp = recognize_keyword (skip_indentor (line), "do");
244 return cp != NULL && recognize_keyword (cp, "repeat") != NULL;
247 /* Returns true if LINE contains an END REPEAT command, false
248 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
251 recognize_end_repeat (const char *line, bool *print)
253 const char *cp = recognize_keyword (skip_indentor (line), "end");
257 cp = recognize_keyword (cp, "repeat");
261 *print = recognize_keyword (cp, "print");
265 /* Read all the lines we are going to substitute, inside the DO
266 REPEAT...END REPEAT block. */
268 parse_lines (struct repeat_block *block)
270 char *previous_file_name;
271 struct line_list **last_line;
274 previous_file_name = NULL;
275 block->first_line = NULL;
276 last_line = &block->first_line;
281 const char *cur_file_name;
283 struct line_list *line;
284 struct string cur_line_copy;
287 if (! lex_get_line_raw ())
290 /* If the current file has changed then record the fact. */
291 getl_location (&cur_file_name, &cur_line_number);
292 if (previous_file_name == NULL
293 || !strcmp (cur_file_name, previous_file_name))
294 previous_file_name = pool_strdup (block->pool, cur_file_name);
296 ds_init_string (&cur_line_copy, lex_entire_line_ds () );
297 ds_rtrim (&cur_line_copy, ss_cstr (CC_SPACES));
298 dot = ds_chomp (&cur_line_copy, get_endcmd ());
300 if (recognize_do_repeat (ds_cstr (&cur_line_copy)))
302 else if (recognize_end_repeat (ds_cstr (&cur_line_copy), &block->print))
304 if (nesting_level-- == 0)
307 ds_destroy (&cur_line_copy);
312 ds_put_char (&cur_line_copy, get_endcmd ());
314 line = *last_line = pool_alloc (block->pool, sizeof *line);
316 line->file_name = previous_file_name;
317 line->line_number = cur_line_number;
318 line->line = pool_strdup (block->pool, ds_cstr (&cur_line_copy) );
319 last_line = &line->next;
321 ds_destroy (&cur_line_copy);
328 /* Creates variables for the given DO REPEAT. */
330 create_vars (struct repeat_block *block)
332 struct repeat_entry *iter;
334 for (iter = block->macros; iter; iter = iter->next)
335 if (iter->type == VAR_NAMES)
339 for (i = 0; i < block->loop_cnt; i++)
341 /* Ignore return value: if the variable already
342 exists there is no harm done. */
343 dict_create_var (dataset_dict (block->ds), iter->replacement[i], 0);
348 /* Parses a set of ids for DO REPEAT. */
350 parse_ids (const struct dictionary *dict, struct repeat_entry *e, struct pool *pool)
354 return parse_mixed_vars_pool (dict, pool, &e->replacement, &n, PV_NONE) ? n : 0;
357 /* Adds STRING to E's list of replacements, which has *USED
358 elements and has room for *ALLOCATED. Allocates memory from
361 add_replacement (char *string,
362 struct repeat_entry *e, struct pool *pool,
363 size_t *used, size_t *allocated)
365 if (*used == *allocated)
366 e->replacement = pool_2nrealloc (pool, e->replacement, allocated,
367 sizeof *e->replacement);
368 e->replacement[(*used)++] = string;
371 /* Parses a list of numbers for DO REPEAT. */
373 parse_numbers (struct repeat_entry *e, struct pool *pool)
376 size_t allocated = 0;
379 e->replacement = NULL;
385 /* Parse A TO B into a, b. */
386 if (!lex_force_int ())
394 if (!lex_force_int ())
399 msg (SE, _("%ld TO %ld is an invalid range."), a, b);
407 for (i = a; i <= b; i++)
408 add_replacement (pool_asprintf (pool, "%ld", i),
409 e, pool, &used, &allocated);
414 while (token != '/' && token != '.');
419 /* Parses a list of strings for DO REPEAT. */
421 parse_strings (struct repeat_entry *e, struct pool *pool)
424 size_t allocated = 0;
427 e->replacement = NULL;
433 if (token != T_STRING)
435 msg (SE, _("String expected."));
439 string = lex_token_representation ();
440 pool_register (pool, free, string);
441 add_replacement (string, e, pool, &used, &allocated);
446 while (token != '/' && token != '.');
452 cmd_end_repeat (struct dataset *ds UNUSED)
454 msg (SE, _("No matching DO REPEAT."));
455 return CMD_CASCADING_FAILURE;
458 /* Finds a DO REPEAT macro with name MACRO_NAME and returns the
459 appropriate subsitution if found, or NULL if not. */
461 find_substitution (struct repeat_block *block, const char *name, size_t length)
463 struct repeat_entry *e;
465 for (e = block->macros; e; e = e->next)
466 if (!memcasecmp (e->id, name, length) && strlen (e->id) == length)
467 return e->replacement[block->loop_idx];
472 /* Makes appropriate DO REPEAT macro substitutions within the
475 do_repeat_filter (struct string *line, void *block_)
477 struct repeat_block *block = block_;
478 bool in_apos, in_quote;
480 struct string output;
483 ds_init_empty (&output);
485 /* Strip trailing whitespace, check for & remove terminal dot. */
486 while (isspace (ds_last (line)))
487 ds_truncate (line, ds_length (line) - 1);
488 dot = ds_chomp (line, get_endcmd ());
490 in_apos = in_quote = false;
491 for (cp = ds_cstr (line); cp < ds_end (line); )
493 if (*cp == '\'' && !in_quote)
495 else if (*cp == '"' && !in_apos)
496 in_quote = !in_quote;
498 if (in_quote || in_apos || !lex_is_id1 (*cp))
499 ds_put_char (&output, *cp++);
502 const char *start = cp;
503 char *end = lex_skip_identifier (start);
504 const char *substitution = find_substitution (block,
506 if (substitution != NULL)
507 ds_put_cstr (&output, substitution);
509 ds_put_substring (&output, ss_buffer (start, end - start));
514 ds_put_char (&output, get_endcmd ());
516 ds_swap (line, &output);
517 ds_destroy (&output);
520 /* Function called by getl to read a line.
521 Puts the line in OUTPUT, sets the file name in *FILE_NAME and
522 line number in *LINE_NUMBER. Returns true if a line was
523 obtained, false if the source is exhausted. */
525 do_repeat_read (struct string *output, char **file_name, int *line_number,
528 struct repeat_block *block = block_;
529 struct line_list *line;
531 if (block->cur_line == NULL)
534 if (block->loop_idx >= block->loop_cnt)
536 block->cur_line = block->first_line;
538 line = block->cur_line;
540 ds_assign_cstr (output, line->line);
541 *file_name = line->file_name;
542 *line_number = -line->line_number;
543 block->cur_line = line->next;
547 /* Frees a DO REPEAT block.
548 Called by getl to close out the DO REPEAT block. */
550 do_repeat_close (void *block_)
552 struct repeat_block *block = block_;
553 pool_destroy (block->pool);