42481dfa29756f92e02790fc3d54ca3bdb4e4fa4
[pspp-builds.git] / src / language / control / repeat.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "repeat.h"
23
24 #include <ctype.h>
25 #include <math.h>
26 #include <stdlib.h>
27
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>
42
43 #include "intprops.h"
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47
48 /* Defines a list of lines used by DO REPEAT. */
49 struct line_list
50   {
51     struct line_list *next;     /* Next line. */
52     char *file_name;            /* File name. */
53     int line_number;            /* Line number. */
54     char *line;                 /* Contents. */
55   };
56
57 /* The type of substitution made for a DO REPEAT macro. */
58 enum repeat_entry_type 
59   {
60     VAR_NAMES,
61     OTHER
62   };
63
64 /* Describes one DO REPEAT macro. */
65 struct repeat_entry
66   {
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. */
71   };
72
73 /* A DO REPEAT...END REPEAT block. */
74 struct repeat_block 
75   {
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? */
83   };
84
85 static bool parse_specification (struct repeat_block *);
86 static bool parse_lines (struct repeat_block *);
87 static void create_vars (struct repeat_block *);
88
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 *);
92
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);
97
98 int
99 cmd_do_repeat (void)
100 {
101   struct repeat_block *block;
102
103   block = pool_create_container (struct repeat_block, pool);
104
105   if (!parse_specification (block) || !parse_lines (block))
106     goto error;
107   
108   create_vars (block);
109   
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);
114
115   return CMD_SUCCESS;
116
117  error:
118   pool_destroy (block->pool);
119   return CMD_CASCADING_FAILURE;
120 }
121
122 /* Parses the whole DO REPEAT command specification.
123    Returns success. */
124 static bool
125 parse_specification (struct repeat_block *block) 
126 {
127   char first_name[LONG_NAME_LEN + 1];
128
129   block->loop_cnt = 0;
130   block->macros = NULL;
131   do
132     {
133       struct repeat_entry *e;
134       struct repeat_entry *iter;
135       int count;
136
137       /* Get a stand-in variable name and make sure it's unique. */
138       if (!lex_force_id ())
139         return false;
140       if (dict_lookup_var (default_dict, tokid))
141         msg (SW, _("Dummy variable name \"%s\" hides dictionary "
142                    "variable \"%s\"."),
143              tokid, tokid);
144       for (iter = block->macros; iter != NULL; iter = iter->next)
145         if (!strcasecmp (iter->id, tokid))
146           {
147             msg (SE, _("Dummy variable name \"%s\" is given twice."), tokid);
148             return false;
149           }
150
151       /* Make a new stand-in variable entry and link it into the
152          list. */
153       e = pool_alloc (block->pool, sizeof *e);
154       e->next = block->macros;
155       strcpy (e->id, tokid);
156       block->macros = e;
157
158       /* Skip equals sign. */
159       lex_get ();
160       if (!lex_force_match ('='))
161         return false;
162
163       /* Get the details of the variable's possible values. */
164       if (token == T_ID)
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);
170       else
171         {
172           lex_error (NULL);
173           return false;
174         }
175       if (count == 0)
176         return false;
177       if (token != '/' && token != '.') 
178         {
179           lex_error (NULL);
180           return false;
181         }
182
183       /* If this is the first variable then it defines how many
184          replacements there must be; otherwise enforce this number of
185          replacements. */
186       if (block->loop_cnt == 0)
187         {
188           block->loop_cnt = count;
189           strcpy (first_name, e->id);
190         }
191       else if (block->loop_cnt != count)
192         {
193           msg (SE, _("Dummy variable \"%s\" had %d "
194                      "substitutions, so \"%s\" must also, but %d "
195                      "were specified."),
196                first_name, block->loop_cnt, e->id, count);
197           return false;
198         }
199
200       lex_match ('/');
201     }
202   while (token != '.');
203
204   return true;
205 }
206
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. */
210 static const char *
211 recognize_keyword (const char *cp, const char *keyword)
212 {
213   const char *end;
214
215   while (isspace ((unsigned char) *cp))
216     cp++;
217
218   end = lex_skip_identifier (cp);
219   if (end != cp
220       && lex_id_match_len (keyword, strlen (keyword), cp, end - cp))
221     return end;
222   else
223     return NULL;
224 }
225
226 /* Returns CP, advanced past a '+' or '-' if present. */
227 static const char *
228 skip_indentor (const char *cp) 
229 {
230   if (*cp == '+' || *cp == '-')
231     cp++;
232   return cp;
233 }
234
235 /* Returns true if LINE contains a DO REPEAT command, false
236    otherwise. */
237 static bool
238 recognize_do_repeat (const char *line) 
239 {
240   const char *cp = recognize_keyword (skip_indentor (line), "do");
241   return cp != NULL && recognize_keyword (cp, "repeat") != NULL;
242 }
243
244 /* Returns true if LINE contains an END REPEAT command, false
245    otherwise.  Sets *PRINT to true for END REPEAT PRINT, false
246    otherwise. */
247 static bool
248 recognize_end_repeat (const char *line, bool *print)
249 {
250   const char *cp = recognize_keyword (skip_indentor (line), "end");
251   if (cp == NULL)
252     return false;
253
254   cp = recognize_keyword (cp, "repeat");
255   if (cp == NULL) 
256     return false; 
257
258   *print = recognize_keyword (cp, "print");
259   return true; 
260 }
261
262 /* Read all the lines we are going to substitute, inside the DO
263    REPEAT...END REPEAT block. */
264 static bool
265 parse_lines (struct repeat_block *block) 
266 {
267   char *previous_file_name;
268   struct line_list **last_line;
269   int nesting_level;
270
271   previous_file_name = NULL;
272   block->first_line = NULL;
273   last_line = &block->first_line;
274   nesting_level = 0;
275
276   for (;;)
277     {
278       const char *cur_file_name;
279       int cur_line_number;
280       struct line_list *line;
281       bool dot;
282
283       if (!getl_read_line (NULL))
284         return false;
285
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);
291
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))) 
295         nesting_level++; 
296       else if (recognize_end_repeat (ds_cstr (&getl_buf), &block->print)) 
297         {
298         if (nesting_level-- == 0)
299           {
300             lex_discard_line ();
301             return true;
302           } 
303         }
304       if (dot)
305         ds_put_char (&getl_buf, get_endcmd ());
306       
307       line = *last_line = pool_alloc (block->pool, sizeof *line);
308       line->next = NULL;
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;
313     }
314
315   lex_discard_line ();
316   return true;
317 }
318
319 /* Creates variables for the given DO REPEAT. */
320 static void
321 create_vars (struct repeat_block *block)
322 {
323   struct repeat_entry *iter;
324  
325   for (iter = block->macros; iter; iter = iter->next)
326     if (iter->type == VAR_NAMES)
327       {
328         int i;
329
330         for (i = 0; i < block->loop_cnt; i++)
331           {
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);
335           }
336       }
337 }
338
339 /* Parses a set of ids for DO REPEAT. */
340 static int
341 parse_ids (struct repeat_entry *e, struct pool *pool)
342 {
343   size_t n = 0;
344   e->type = VAR_NAMES;
345   return parse_mixed_vars_pool (pool, &e->replacement, &n, PV_NONE) ? n : 0;
346 }
347
348 /* Adds STRING to E's list of replacements, which has *USED
349    elements and has room for *ALLOCATED.  Allocates memory from
350    POOL. */
351 static void
352 add_replacement (char *string,
353                  struct repeat_entry *e, struct pool *pool,
354                  size_t *used, size_t *allocated) 
355 {
356   if (*used == *allocated)
357     e->replacement = pool_2nrealloc (pool, e->replacement, allocated,
358                                      sizeof *e->replacement);
359   e->replacement[(*used)++] = string;
360 }
361
362 /* Parses a list of numbers for DO REPEAT. */
363 static int
364 parse_numbers (struct repeat_entry *e, struct pool *pool)
365 {
366   size_t used = 0;
367   size_t allocated = 0;
368   
369   e->type = OTHER;
370   e->replacement = NULL;
371
372   do
373     {
374       long a, b, i;
375
376       /* Parse A TO B into a, b. */
377       if (!lex_force_int ())
378         return 0;
379       a = lex_integer ();
380
381       lex_get ();
382       if (token == T_TO)
383         {
384           lex_get ();
385           if (!lex_force_int ())
386             return 0;
387           b = lex_integer ();
388           if (b < a) 
389             {
390               msg (SE, _("%ld TO %ld is an invalid range."), a, b);
391               return 0;
392             }
393           lex_get ();
394         }
395       else
396         b = a;
397
398       for (i = a; i <= b; i++)
399         add_replacement (pool_asprintf (pool, "%ld", i),
400                          e, pool, &used, &allocated);
401
402
403       lex_match (',');
404     }
405   while (token != '/' && token != '.');
406
407   return used;
408 }
409
410 /* Parses a list of strings for DO REPEAT. */
411 int
412 parse_strings (struct repeat_entry *e, struct pool *pool)
413 {
414   size_t used = 0;
415   size_t allocated = 0;
416   
417   e->type = OTHER;
418   e->replacement = NULL;
419
420   do
421     {
422       char *string;
423       
424       if (token != T_STRING)
425         {
426           msg (SE, _("String expected."));
427           return 0;
428         }
429
430       string = lex_token_representation ();
431       pool_register (pool, free, string);
432       add_replacement (string, e, pool, &used, &allocated);
433
434       lex_get ();
435       lex_match (',');
436     }
437   while (token != '/' && token != '.');
438
439   return used;
440 }
441 \f
442 int
443 cmd_end_repeat (void)
444 {
445   msg (SE, _("No matching DO REPEAT."));
446   return CMD_CASCADING_FAILURE;
447 }
448 \f
449 /* Finds a DO REPEAT macro with name MACRO_NAME and returns the
450    appropriate subsitution if found, or NULL if not. */
451 static char *
452 find_substitution (struct repeat_block *block, const char *name, size_t length)
453 {
454   struct repeat_entry *e;
455
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];
459   
460   return NULL;
461 }
462
463 /* Makes appropriate DO REPEAT macro substitutions within getl_buf. */
464 static void
465 do_repeat_filter (struct string *line, void *block_)
466 {
467   struct repeat_block *block = block_;
468   bool in_apos, in_quote;
469   char *cp;
470   struct string output;
471   bool dot;
472
473   ds_init_empty (&output);
474
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 ());
479
480   in_apos = in_quote = false;
481   for (cp = ds_cstr (line); cp < ds_end (line); )
482     {
483       if (*cp == '\'' && !in_quote)
484         in_apos = !in_apos;
485       else if (*cp == '"' && !in_apos)
486         in_quote = !in_quote;
487       
488       if (in_quote || in_apos || !lex_is_id1 (*cp))
489         ds_put_char (&output, *cp++);
490       else 
491         {
492           const char *start = cp;
493           char *end = lex_skip_identifier (start);
494           const char *substitution = find_substitution (block,
495                                                         start, end - start);
496           if (substitution != NULL) 
497             ds_put_cstr (&output, substitution);
498           else
499             ds_put_substring (&output, ss_buffer (start, end - start));
500           cp = end;
501         }
502     }
503   if (dot)
504     ds_put_char (&output, get_endcmd ());
505
506   ds_swap (line, &output);
507   ds_destroy (&output);
508 }
509
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. */
514 static bool
515 do_repeat_read (struct string *output, char **file_name, int *line_number,
516                 void *block_) 
517 {
518   struct repeat_block *block = block_;
519   struct line_list *line;
520
521   if (block->cur_line == NULL) 
522     {
523       block->loop_idx++;
524       if (block->loop_idx >= block->loop_cnt)
525         return false;
526       block->cur_line = block->first_line;
527     }
528   line = block->cur_line;
529
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;
534   return true;
535 }
536
537 /* Frees a DO REPEAT block.
538    Called by getl to close out the DO REPEAT block. */
539 static void
540 do_repeat_close (void *block_)
541 {
542   struct repeat_block *block = block_;
543   pool_destroy (block->pool);
544 }