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 line_list *first_line; /* First line in line buffer. */
78 struct line_list *cur_line; /* Current line in line buffer. */
79 int loop_cnt; /* Number of loops. */
80 int loop_idx; /* Number of loops so far. */
81 struct repeat_entry *macros; /* Pointer to macro table. */
82 bool print; /* Print lines as executed? */
85 static bool parse_specification (struct repeat_block *);
86 static bool parse_lines (struct repeat_block *);
87 static void create_vars (struct repeat_block *);
89 static int parse_ids (struct repeat_entry *, struct pool *);
90 static int parse_numbers (struct repeat_entry *, struct pool *);
91 static int parse_strings (struct repeat_entry *, struct pool *);
93 static void do_repeat_filter (struct string *line, void *block);
94 static bool do_repeat_read (struct string *line, char **file_name,
95 int *line_number, void *block);
96 static void do_repeat_close (void *block);
101 struct repeat_block *block;
103 block = pool_create_container (struct repeat_block, pool);
105 if (!parse_specification (block) || !parse_lines (block))
110 block->cur_line = NULL;
111 block->loop_idx = -1;
112 getl_include_filter (do_repeat_filter, do_repeat_close, block);
113 getl_include_function (do_repeat_read, NULL, block);
118 pool_destroy (block->pool);
119 return CMD_CASCADING_FAILURE;
122 /* Parses the whole DO REPEAT command specification.
125 parse_specification (struct repeat_block *block)
127 char first_name[LONG_NAME_LEN + 1];
130 block->macros = NULL;
133 struct repeat_entry *e;
134 struct repeat_entry *iter;
137 /* Get a stand-in variable name and make sure it's unique. */
138 if (!lex_force_id ())
140 if (dict_lookup_var (default_dict, tokid))
141 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
144 for (iter = block->macros; iter != NULL; iter = iter->next)
145 if (!strcasecmp (iter->id, tokid))
147 msg (SE, _("Dummy variable name \"%s\" is given twice."), tokid);
151 /* Make a new stand-in variable entry and link it into the
153 e = pool_alloc (block->pool, sizeof *e);
154 e->next = block->macros;
155 strcpy (e->id, tokid);
158 /* Skip equals sign. */
160 if (!lex_force_match ('='))
163 /* Get the details of the variable's possible values. */
165 count = parse_ids (e, block->pool);
166 else if (lex_is_number ())
167 count = parse_numbers (e, block->pool);
168 else if (token == T_STRING)
169 count = parse_strings (e, block->pool);
177 if (token != '/' && token != '.')
183 /* If this is the first variable then it defines how many
184 replacements there must be; otherwise enforce this number of
186 if (block->loop_cnt == 0)
188 block->loop_cnt = count;
189 strcpy (first_name, e->id);
191 else if (block->loop_cnt != count)
193 msg (SE, _("Dummy variable \"%s\" had %d "
194 "substitutions, so \"%s\" must also, but %d "
196 first_name, block->loop_cnt, e->id, count);
202 while (token != '.');
207 /* If KEYWORD appears beginning at CP, possibly preceded by white
208 space, returns a pointer to the character just after the
209 keyword. Otherwise, returns a null pointer. */
211 recognize_keyword (const char *cp, const char *keyword)
215 while (isspace ((unsigned char) *cp))
218 end = lex_skip_identifier (cp);
220 && lex_id_match_len (keyword, strlen (keyword), cp, end - cp))
226 /* Returns CP, advanced past a '+' or '-' if present. */
228 skip_indentor (const char *cp)
230 if (*cp == '+' || *cp == '-')
235 /* Returns true if LINE contains a DO REPEAT command, false
238 recognize_do_repeat (const char *line)
240 const char *cp = recognize_keyword (skip_indentor (line), "do");
241 return cp != NULL && recognize_keyword (cp, "repeat") != NULL;
244 /* Returns true if LINE contains an END REPEAT command, false
245 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
248 recognize_end_repeat (const char *line, bool *print)
250 const char *cp = recognize_keyword (skip_indentor (line), "end");
254 cp = recognize_keyword (cp, "repeat");
258 *print = recognize_keyword (cp, "print");
262 /* Read all the lines we are going to substitute, inside the DO
263 REPEAT...END REPEAT block. */
265 parse_lines (struct repeat_block *block)
267 char *previous_file_name;
268 struct line_list **last_line;
271 previous_file_name = NULL;
272 block->first_line = NULL;
273 last_line = &block->first_line;
278 const char *cur_file_name;
280 struct line_list *line;
283 if (!getl_read_line (NULL))
286 /* If the current file has changed then record the fact. */
287 getl_location (&cur_file_name, &cur_line_number);
288 if (previous_file_name == NULL
289 || !strcmp (cur_file_name, previous_file_name))
290 previous_file_name = pool_strdup (block->pool, cur_file_name);
292 ds_rtrim (&getl_buf, ss_cstr (CC_SPACES));
293 dot = ds_chomp (&getl_buf, get_endcmd ());
294 if (recognize_do_repeat (ds_cstr (&getl_buf)))
296 else if (recognize_end_repeat (ds_cstr (&getl_buf), &block->print))
298 if (nesting_level-- == 0)
305 ds_put_char (&getl_buf, get_endcmd ());
307 line = *last_line = pool_alloc (block->pool, sizeof *line);
309 line->file_name = previous_file_name;
310 line->line_number = cur_line_number;
311 line->line = pool_strdup (block->pool, ds_cstr (&getl_buf));
312 last_line = &line->next;
319 /* Creates variables for the given DO REPEAT. */
321 create_vars (struct repeat_block *block)
323 struct repeat_entry *iter;
325 for (iter = block->macros; iter; iter = iter->next)
326 if (iter->type == VAR_NAMES)
330 for (i = 0; i < block->loop_cnt; i++)
332 /* Ignore return value: if the variable already
333 exists there is no harm done. */
334 dict_create_var (default_dict, iter->replacement[i], 0);
339 /* Parses a set of ids for DO REPEAT. */
341 parse_ids (struct repeat_entry *e, struct pool *pool)
345 return parse_mixed_vars_pool (pool, &e->replacement, &n, PV_NONE) ? n : 0;
348 /* Adds STRING to E's list of replacements, which has *USED
349 elements and has room for *ALLOCATED. Allocates memory from
352 add_replacement (char *string,
353 struct repeat_entry *e, struct pool *pool,
354 size_t *used, size_t *allocated)
356 if (*used == *allocated)
357 e->replacement = pool_2nrealloc (pool, e->replacement, allocated,
358 sizeof *e->replacement);
359 e->replacement[(*used)++] = string;
362 /* Parses a list of numbers for DO REPEAT. */
364 parse_numbers (struct repeat_entry *e, struct pool *pool)
367 size_t allocated = 0;
370 e->replacement = NULL;
376 /* Parse A TO B into a, b. */
377 if (!lex_force_int ())
385 if (!lex_force_int ())
390 msg (SE, _("%ld TO %ld is an invalid range."), a, b);
398 for (i = a; i <= b; i++)
399 add_replacement (pool_asprintf (pool, "%ld", i),
400 e, pool, &used, &allocated);
405 while (token != '/' && token != '.');
410 /* Parses a list of strings for DO REPEAT. */
412 parse_strings (struct repeat_entry *e, struct pool *pool)
415 size_t allocated = 0;
418 e->replacement = NULL;
424 if (token != T_STRING)
426 msg (SE, _("String expected."));
430 string = lex_token_representation ();
431 pool_register (pool, free, string);
432 add_replacement (string, e, pool, &used, &allocated);
437 while (token != '/' && token != '.');
443 cmd_end_repeat (void)
445 msg (SE, _("No matching DO REPEAT."));
446 return CMD_CASCADING_FAILURE;
449 /* Finds a DO REPEAT macro with name MACRO_NAME and returns the
450 appropriate subsitution if found, or NULL if not. */
452 find_substitution (struct repeat_block *block, const char *name, size_t length)
454 struct repeat_entry *e;
456 for (e = block->macros; e; e = e->next)
457 if (!memcasecmp (e->id, name, length) && strlen (e->id) == length)
458 return e->replacement[block->loop_idx];
463 /* Makes appropriate DO REPEAT macro substitutions within getl_buf. */
465 do_repeat_filter (struct string *line, void *block_)
467 struct repeat_block *block = block_;
468 bool in_apos, in_quote;
470 struct string output;
473 ds_init_empty (&output);
475 /* Strip trailing whitespace, check for & remove terminal dot. */
476 while (isspace (ds_last (line)))
477 ds_truncate (line, ds_length (line) - 1);
478 dot = ds_chomp (line, get_endcmd ());
480 in_apos = in_quote = false;
481 for (cp = ds_cstr (line); cp < ds_end (line); )
483 if (*cp == '\'' && !in_quote)
485 else if (*cp == '"' && !in_apos)
486 in_quote = !in_quote;
488 if (in_quote || in_apos || !lex_is_id1 (*cp))
489 ds_put_char (&output, *cp++);
492 const char *start = cp;
493 char *end = lex_skip_identifier (start);
494 const char *substitution = find_substitution (block,
496 if (substitution != NULL)
497 ds_put_cstr (&output, substitution);
499 ds_put_substring (&output, ss_buffer (start, end - start));
504 ds_put_char (&output, get_endcmd ());
506 ds_swap (line, &output);
507 ds_destroy (&output);
510 /* Function called by getl to read a line.
511 Puts the line in OUTPUT, sets the file name in *FILE_NAME and
512 line number in *LINE_NUMBER. Returns true if a line was
513 obtained, false if the source is exhausted. */
515 do_repeat_read (struct string *output, char **file_name, int *line_number,
518 struct repeat_block *block = block_;
519 struct line_list *line;
521 if (block->cur_line == NULL)
524 if (block->loop_idx >= block->loop_cnt)
526 block->cur_line = block->first_line;
528 line = block->cur_line;
530 ds_assign_cstr (output, line->line);
531 *file_name = line->file_name;
532 *line_number = -line->line_number;
533 block->cur_line = line->next;
537 /* Frees a DO REPEAT block.
538 Called by getl to close out the DO REPEAT block. */
540 do_repeat_close (void *block_)
542 struct repeat_block *block = block_;
543 pool_destroy (block->pool);