448fae7ea803463f11a54edbec50a23c816875a4
[pspp] / src / language / command.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <language/command.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #if HAVE_SYS_WAIT_H
27 #include <sys/wait.h>
28 #endif
29 #if HAVE_READLINE
30 #include <readline/readline.h>
31 #endif
32
33 #include <data/casereader.h>
34 #include <data/dictionary.h>
35 #include <data/procedure.h>
36 #include <data/settings.h>
37 #include <data/variable.h>
38 #include <language/lexer/lexer.h>
39 #include <language/prompt.h>
40 #include <libpspp/assertion.h>
41 #include <libpspp/compiler.h>
42 #include <libpspp/message.h>
43 #include <libpspp/message.h>
44 #include <libpspp/str.h>
45 #include <libpspp/getl.h>
46 #include <output/text-item.h>
47
48 #include "xalloc.h"
49 #include "xmalloca.h"
50
51 #include "gettext.h"
52 #define _(msgid) gettext (msgid)
53 #define N_(msgid) msgid
54 \f
55 /* Returns true if RESULT is a valid "enum cmd_result",
56    false otherwise. */
57 static inline bool
58 cmd_result_is_valid (enum cmd_result result)
59 {
60   return (result == CMD_SUCCESS || result == CMD_EOF || result == CMD_FINISH
61           || (result >= CMD_PRIVATE_FIRST && result <= CMD_PRIVATE_LAST)
62           || result == CMD_FAILURE || result == CMD_NOT_IMPLEMENTED
63           || result == CMD_CASCADING_FAILURE);
64 }
65
66 /* Returns true if RESULT indicates success,
67    false otherwise. */
68 bool
69 cmd_result_is_success (enum cmd_result result)
70 {
71   assert (cmd_result_is_valid (result));
72   return result > 0;
73 }
74
75 /* Returns true if RESULT indicates failure,
76    false otherwise. */
77 bool
78 cmd_result_is_failure (enum cmd_result result)
79 {
80   assert (cmd_result_is_valid (result));
81   return result < 0;
82 }
83 \f
84 /* Command processing states. */
85 enum states
86   {
87     S_INITIAL = 0x01,         /* Allowed before active file defined. */
88     S_DATA = 0x02,            /* Allowed after active file defined. */
89     S_INPUT_PROGRAM = 0x04,   /* Allowed in INPUT PROGRAM. */
90     S_FILE_TYPE = 0x08,       /* Allowed in FILE TYPE. */
91     S_ANY = 0x0f              /* Allowed anywhere. */
92   };
93
94 /* Other command requirements. */
95 enum flags
96   {
97     F_ENHANCED = 0x10,        /* Allowed only in enhanced syntax mode. */
98     F_TESTING = 0x20,         /* Allowed only in testing mode. */
99     F_KEEP_FINAL_TOKEN = 0x40,/* Don't skip final token in command name. */
100     F_ABBREV = 0x80           /* Not a candidate for name completion. */
101   };
102
103 /* A single command. */
104 struct command
105   {
106     enum states states;         /* States in which command is allowed. */
107     enum flags flags;           /* Other command requirements. */
108     const char *name;           /* Command name. */
109     int (*function) (struct lexer *, struct dataset *); /* Function to call. */
110   };
111
112 /* Define the command array. */
113 #define DEF_CMD(STATES, FLAGS, NAME, FUNCTION) {STATES, FLAGS, NAME, FUNCTION},
114 #define UNIMPL_CMD(NAME, DESCRIPTION) {S_ANY, 0, NAME, NULL},
115 static const struct command commands[] =
116   {
117 #include "command.def"
118   };
119 #undef DEF_CMD
120 #undef UNIMPL_CMD
121
122 static const size_t command_cnt = sizeof commands / sizeof *commands;
123
124 static bool in_correct_state (const struct command *, enum cmd_state);
125 static bool report_state_mismatch (const struct command *, enum cmd_state);
126 static const struct command *find_command (const char *name);
127 static void set_completion_state (enum cmd_state);
128 \f
129 /* Command parser. */
130
131 static const struct command *parse_command_name (struct lexer *lexer);
132 static enum cmd_result do_parse_command (struct lexer *, struct dataset *, enum cmd_state);
133
134 /* Parses an entire command, from command name to terminating
135    dot.  On failure, skips to the terminating dot.
136    Returns the command's success or failure result. */
137 enum cmd_result
138 cmd_parse_in_state (struct lexer *lexer, struct dataset *ds,
139                     enum cmd_state state)
140 {
141   int result;
142
143   result = do_parse_command (lexer, ds, state);
144
145   assert (!proc_is_open (ds));
146   unset_cmd_algorithm ();
147   dict_clear_aux (dataset_dict (ds));
148   if (!dataset_end_of_command (ds))
149     result = CMD_CASCADING_FAILURE;
150
151   return result;
152 }
153
154 enum cmd_result
155 cmd_parse (struct lexer *lexer, struct dataset *ds)
156 {
157   const struct dictionary *dict = dataset_dict (ds);
158   return cmd_parse_in_state (lexer, ds,
159                              proc_has_active_file (ds) &&
160                              dict_get_var_cnt (dict) > 0 ?
161                              CMD_STATE_DATA : CMD_STATE_INITIAL);
162 }
163
164
165 /* Parses an entire command, from command name to terminating
166    dot. */
167 static enum cmd_result
168 do_parse_command (struct lexer *lexer,
169                   struct dataset *ds, enum cmd_state state)
170 {
171   const struct command *command = NULL;
172   enum cmd_result result;
173   bool opened = false;
174
175   /* Read the command's first token. */
176   prompt_set_style (PROMPT_FIRST);
177   set_completion_state (state);
178   lex_get (lexer);
179   if (lex_token (lexer) == T_STOP)
180     {
181       result = CMD_EOF;
182       goto finish;
183     }
184   else if (lex_token (lexer) == '.')
185     {
186       /* Null commands can result from extra empty lines. */
187       result = CMD_SUCCESS;
188       goto finish;
189     }
190
191   prompt_set_style (PROMPT_LATER);
192
193   /* Parse the command name. */
194   command = parse_command_name (lexer);
195   if (command == NULL)
196     {
197       result = CMD_FAILURE;
198       goto finish;
199     }
200   text_item_submit (text_item_create (TEXT_ITEM_COMMAND_OPEN, command->name));
201   opened = true;
202
203   if (command->function == NULL)
204     {
205       msg (SE, _("%s is not yet implemented."), command->name);
206       result = CMD_NOT_IMPLEMENTED;
207     }
208   else if ((command->flags & F_TESTING) && !settings_get_testing_mode ())
209     {
210       msg (SE, _("%s may be used only in testing mode."), command->name);
211       result = CMD_FAILURE;
212     }
213   else if ((command->flags & F_ENHANCED) && settings_get_syntax () != ENHANCED)
214     {
215       msg (SE, _("%s may be used only in enhanced syntax mode."),
216            command->name);
217       result = CMD_FAILURE;
218     }
219   else if (!in_correct_state (command, state))
220     {
221       report_state_mismatch (command, state);
222       result = CMD_FAILURE;
223     }
224   else
225     {
226       /* Execute command. */
227       result = command->function (lexer, ds);
228     }
229
230   assert (cmd_result_is_valid (result));
231
232  finish:
233   if (cmd_result_is_failure (result))
234     {
235       lex_discard_rest_of_command (lexer);
236       if (source_stream_current_error_mode (
237             lex_get_source_stream (lexer)) == ERRMODE_STOP )
238         {
239           msg (MW, _("Error encountered while ERROR=STOP is effective."));
240           result = CMD_CASCADING_FAILURE;
241         }
242     }
243
244   if (opened)
245     text_item_submit (text_item_create (TEXT_ITEM_COMMAND_CLOSE,
246                                         command->name));
247
248   return result;
249 }
250
251 static size_t
252 match_strings (const char *a, size_t a_len,
253                const char *b, size_t b_len)
254 {
255   size_t match_len = 0;
256
257   while (a_len > 0 && b_len > 0)
258     {
259       /* Mismatch always returns zero. */
260       if (toupper ((unsigned char) *a++) != toupper ((unsigned char) *b++))
261         return 0;
262
263       /* Advance. */
264       a_len--;
265       b_len--;
266       match_len++;
267     }
268
269   return match_len;
270 }
271
272 /* Returns the first character in the first word in STRING,
273    storing the word's length in *WORD_LEN.  If no words remain,
274    returns a null pointer and stores 0 in *WORD_LEN.  Words are
275    sequences of alphanumeric characters or single
276    non-alphanumeric characters.  Words are delimited by
277    spaces. */
278 static const char *
279 find_word (const char *string, size_t *word_len)
280 {
281   /* Skip whitespace and asterisks. */
282   while (isspace ((unsigned char) *string))
283     string++;
284
285   /* End of string? */
286   if (*string == '\0')
287     {
288       *word_len = 0;
289       return NULL;
290     }
291
292   /* Special one-character word? */
293   if (!isalnum ((unsigned char) *string))
294     {
295       *word_len = 1;
296       return string;
297     }
298
299   /* Alphanumeric word. */
300   *word_len = 1;
301   while (isalnum ((unsigned char) string[*word_len]))
302     (*word_len)++;
303
304   return string;
305 }
306
307 /* Returns true if strings A and B can be confused based on
308    their first three letters. */
309 static bool
310 conflicting_3char_prefixes (const char *a, const char *b)
311 {
312   size_t aw_len, bw_len;
313   const char *aw, *bw;
314
315   aw = find_word (a, &aw_len);
316   bw = find_word (b, &bw_len);
317   assert (aw != NULL && bw != NULL);
318
319   /* Words that are the same don't conflict. */
320   if (aw_len == bw_len && !buf_compare_case (aw, bw, aw_len))
321     return false;
322
323   /* Words that are otherwise the same in the first three letters
324      do conflict. */
325   return ((aw_len > 3 && bw_len > 3)
326           || (aw_len == 3 && bw_len > 3)
327           || (bw_len == 3 && aw_len > 3)) && !buf_compare_case (aw, bw, 3);
328 }
329
330 /* Returns true if CMD can be confused with another command
331    based on the first three letters of its first word. */
332 static bool
333 conflicting_3char_prefix_command (const struct command *cmd)
334 {
335   assert (cmd >= commands && cmd < commands + command_cnt);
336
337   return ((cmd > commands
338            && conflicting_3char_prefixes (cmd[-1].name, cmd[0].name))
339           || (cmd < commands + command_cnt
340               && conflicting_3char_prefixes (cmd[0].name, cmd[1].name)));
341 }
342
343 /* Ways that a set of words can match a command name. */
344 enum command_match
345   {
346     MISMATCH,           /* Not a match. */
347     PARTIAL_MATCH,      /* The words begin the command name. */
348     COMPLETE_MATCH      /* The words are the command name. */
349   };
350
351 /* Figures out how well the WORD_CNT words in WORDS match CMD,
352    and returns the appropriate enum value.  If WORDS are a
353    partial match for CMD and the next word in CMD is a dash, then
354    *DASH_POSSIBLE is set to 1 if DASH_POSSIBLE is non-null;
355    otherwise, *DASH_POSSIBLE is unchanged. */
356 static enum command_match
357 cmd_match_words (const struct command *cmd,
358                  char *const words[], size_t word_cnt,
359                  int *dash_possible)
360 {
361   const char *word;
362   size_t word_len;
363   size_t word_idx;
364
365   for (word = find_word (cmd->name, &word_len), word_idx = 0;
366        word != NULL && word_idx < word_cnt;
367        word = find_word (word + word_len, &word_len), word_idx++)
368     if (word_len != strlen (words[word_idx])
369         || buf_compare_case (word, words[word_idx], word_len))
370       {
371         size_t match_chars = match_strings (word, word_len,
372                                             words[word_idx],
373                                             strlen (words[word_idx]));
374         if (match_chars == 0)
375           {
376             /* Mismatch. */
377             return MISMATCH;
378           }
379         else if (match_chars == 1 || match_chars == 2)
380           {
381             /* One- and two-character abbreviations are not
382                acceptable. */
383             return MISMATCH;
384           }
385         else if (match_chars == 3)
386           {
387             /* Three-character abbreviations are acceptable
388                in the first word of a command if there are
389                no name conflicts.  They are always
390                acceptable after the first word. */
391             if (word_idx == 0 && conflicting_3char_prefix_command (cmd))
392               return MISMATCH;
393           }
394         else /* match_chars > 3 */
395           {
396             /* Four-character and longer abbreviations are
397                always acceptable.  */
398           }
399       }
400
401   if (word == NULL && word_idx == word_cnt)
402     {
403       /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR"}. */
404       return COMPLETE_MATCH;
405     }
406   else if (word == NULL)
407     {
408       /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR", "BAZ"}. */
409       return MISMATCH;
410     }
411   else
412     {
413       /* cmd->name = "FOO BAR BAZ", words[] = {"FOO", "BAR"}. */
414       if (word[0] == '-' && dash_possible != NULL)
415         *dash_possible = 1;
416       return PARTIAL_MATCH;
417     }
418 }
419
420 /* Returns the number of commands for which the WORD_CNT words in
421    WORDS are a partial or complete match.  If some partial match
422    has a dash as the next word, then *DASH_POSSIBLE is set to 1,
423    otherwise it is set to 0. */
424 static int
425 count_matching_commands (char *const words[], size_t word_cnt,
426                          int *dash_possible)
427 {
428   const struct command *cmd;
429   int cmd_match_count;
430
431   cmd_match_count = 0;
432   *dash_possible = 0;
433   for (cmd = commands; cmd < commands + command_cnt; cmd++)
434     if (cmd_match_words (cmd, words, word_cnt, dash_possible) != MISMATCH)
435       cmd_match_count++;
436
437   return cmd_match_count;
438 }
439
440 /* Returns the command for which the WORD_CNT words in WORDS are
441    a complete match.  Returns a null pointer if no such command
442    exists. */
443 static const struct command *
444 get_complete_match (char *const words[], size_t word_cnt)
445 {
446   const struct command *cmd;
447
448   for (cmd = commands; cmd < commands + command_cnt; cmd++)
449     if (cmd_match_words (cmd, words, word_cnt, NULL) == COMPLETE_MATCH)
450       return cmd;
451
452   return NULL;
453 }
454
455 /* Returns the command with the given exact NAME.
456    Aborts if no such command exists. */
457 static const struct command *
458 find_command (const char *name)
459 {
460   const struct command *cmd;
461
462   for (cmd = commands; cmd < commands + command_cnt; cmd++)
463     if (!strcmp (cmd->name, name))
464       return cmd;
465   NOT_REACHED ();
466 }
467
468 /* Frees the WORD_CNT words in WORDS. */
469 static void
470 free_words (char *words[], size_t word_cnt)
471 {
472   size_t idx;
473
474   for (idx = 0; idx < word_cnt; idx++)
475     free (words[idx]);
476 }
477
478 /* Flags an error that the command whose name is given by the
479    WORD_CNT words in WORDS is unknown. */
480 static void
481 unknown_command_error (struct lexer *lexer, char *const words[], size_t word_cnt)
482 {
483   if (word_cnt == 0)
484     lex_error (lexer, _("expecting command name"));
485   else
486     {
487       struct string s;
488       size_t i;
489
490       ds_init_empty (&s);
491       for (i = 0; i < word_cnt; i++)
492         {
493           if (i != 0)
494             ds_put_char (&s, ' ');
495           ds_put_cstr (&s, words[i]);
496         }
497
498       msg (SE, _("Unknown command %s."), ds_cstr (&s));
499
500       ds_destroy (&s);
501     }
502 }
503
504 /* Parse the command name and return a pointer to the corresponding
505    struct command if successful.
506    If not successful, return a null pointer. */
507 static const struct command *
508 parse_command_name (struct lexer *lexer)
509 {
510   char *words[16];
511   int word_cnt;
512   int complete_word_cnt;
513   int dash_possible;
514
515   if (lex_token (lexer) == T_EXP ||
516                   lex_token (lexer) == '*' || lex_token (lexer) == '[')
517     return find_command ("COMMENT");
518
519   dash_possible = 0;
520   word_cnt = complete_word_cnt = 0;
521   while (lex_token (lexer) == T_ID || (dash_possible && lex_token (lexer) == '-'))
522     {
523       int cmd_match_cnt;
524
525       assert (word_cnt < sizeof words / sizeof *words);
526       if (lex_token (lexer) == T_ID)
527         {
528           words[word_cnt] = ds_xstrdup (lex_tokstr (lexer));
529           str_uppercase (words[word_cnt]);
530         }
531       else if (lex_token (lexer) == '-')
532         words[word_cnt] = xstrdup ("-");
533       word_cnt++;
534
535       cmd_match_cnt = count_matching_commands (words, word_cnt,
536                                                &dash_possible);
537       if (cmd_match_cnt == 0)
538         break;
539       else if (cmd_match_cnt == 1)
540         {
541           const struct command *command = get_complete_match (words, word_cnt);
542           if (command != NULL)
543             {
544               if (!(command->flags & F_KEEP_FINAL_TOKEN))
545                 lex_get (lexer);
546               free_words (words, word_cnt);
547               return command;
548             }
549         }
550       else /* cmd_match_cnt > 1 */
551         {
552           /* Do we have a complete command name so far? */
553           if (get_complete_match (words, word_cnt) != NULL)
554             complete_word_cnt = word_cnt;
555         }
556       lex_get (lexer);
557     }
558
559   /* If we saw a complete command name earlier, drop back to
560      it. */
561   if (complete_word_cnt)
562     {
563       int pushback_word_cnt;
564       const struct command *command;
565
566       /* Get the command. */
567       command = get_complete_match (words, complete_word_cnt);
568       assert (command != NULL);
569
570       /* Figure out how many words we want to keep.
571          We normally want to swallow the entire command. */
572       pushback_word_cnt = complete_word_cnt + 1;
573       if (command->flags & F_KEEP_FINAL_TOKEN)
574         pushback_word_cnt--;
575
576       /* FIXME: We only support one-token pushback. */
577       assert (pushback_word_cnt + 1 >= word_cnt);
578
579       while (word_cnt > pushback_word_cnt)
580         {
581           word_cnt--;
582           if (strcmp (words[word_cnt], "-"))
583             lex_put_back_id (lexer, words[word_cnt]);
584           else
585             lex_put_back (lexer, '-');
586           free (words[word_cnt]);
587         }
588
589       free_words (words, word_cnt);
590       return command;
591     }
592
593   /* We didn't get a valid command name. */
594   unknown_command_error (lexer, words, word_cnt);
595   free_words (words, word_cnt);
596   return NULL;
597 }
598
599 /* Returns true if COMMAND is allowed in STATE,
600    false otherwise. */
601 static bool
602 in_correct_state (const struct command *command, enum cmd_state state)
603 {
604   return ((state == CMD_STATE_INITIAL && command->states & S_INITIAL)
605           || (state == CMD_STATE_DATA && command->states & S_DATA)
606           || (state == CMD_STATE_INPUT_PROGRAM
607               && command->states & S_INPUT_PROGRAM)
608           || (state == CMD_STATE_FILE_TYPE && command->states & S_FILE_TYPE));
609 }
610
611 /* Emits an appropriate error message for trying to invoke
612    COMMAND in STATE. */
613 static bool
614 report_state_mismatch (const struct command *command, enum cmd_state state)
615 {
616   assert (!in_correct_state (command, state));
617   if (state == CMD_STATE_INITIAL || state == CMD_STATE_DATA)
618     {
619       switch (command->states)
620         {
621           /* One allowed state. */
622         case S_INITIAL:
623           msg (SE, _("%s is allowed only before the active file has "
624                      "been defined."), command->name);
625           break;
626         case S_DATA:
627           msg (SE, _("%s is allowed only after the active file has "
628                      "been defined."), command->name);
629           break;
630         case S_INPUT_PROGRAM:
631           msg (SE, _("%s is allowed only inside INPUT PROGRAM."),
632                command->name);
633           break;
634         case S_FILE_TYPE:
635           msg (SE, _("%s is allowed only inside FILE TYPE."), command->name);
636           break;
637
638           /* Two allowed states. */
639         case S_INITIAL | S_DATA:
640           NOT_REACHED ();
641         case S_INITIAL | S_INPUT_PROGRAM:
642           msg (SE, _("%s is allowed only before the active file has "
643                      "been defined or inside INPUT PROGRAM."), command->name);
644           break;
645         case S_INITIAL | S_FILE_TYPE:
646           msg (SE, _("%s is allowed only before the active file has "
647                      "been defined or inside FILE TYPE."), command->name);
648           break;
649         case S_DATA | S_INPUT_PROGRAM:
650           msg (SE, _("%s is allowed only after the active file has "
651                      "been defined or inside INPUT PROGRAM."), command->name);
652           break;
653         case S_DATA | S_FILE_TYPE:
654           msg (SE, _("%s is allowed only after the active file has "
655                      "been defined or inside FILE TYPE."), command->name);
656           break;
657         case S_INPUT_PROGRAM | S_FILE_TYPE:
658           msg (SE, _("%s is allowed only inside INPUT PROGRAM "
659                      "or inside FILE TYPE."), command->name);
660           break;
661
662           /* Three allowed states. */
663         case S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
664           msg (SE, _("%s is allowed only after the active file has "
665                      "been defined, inside INPUT PROGRAM, or inside "
666                      "FILE TYPE."), command->name);
667           break;
668         case S_INITIAL | S_INPUT_PROGRAM | S_FILE_TYPE:
669           msg (SE, _("%s is allowed only before the active file has "
670                      "been defined, inside INPUT PROGRAM, or inside "
671                      "FILE TYPE."), command->name);
672           break;
673         case S_INITIAL | S_DATA | S_FILE_TYPE:
674           NOT_REACHED ();
675         case S_INITIAL | S_DATA | S_INPUT_PROGRAM:
676           NOT_REACHED ();
677
678           /* Four allowed states. */
679         case S_INITIAL | S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
680           NOT_REACHED ();
681
682         default:
683           NOT_REACHED ();
684         }
685     }
686   else if (state == CMD_STATE_INPUT_PROGRAM)
687     msg (SE, _("%s is not allowed inside %s."), command->name, "INPUT PROGRAM" );
688   else if (state == CMD_STATE_FILE_TYPE)
689     msg (SE, _("%s is not allowed inside %s."), command->name, "FILE TYPE");
690
691   return false;
692 }
693 \f
694 /* Command name completion. */
695
696 static enum cmd_state completion_state = CMD_STATE_INITIAL;
697
698 static void
699 set_completion_state (enum cmd_state state)
700 {
701   completion_state = state;
702 }
703
704 /* Returns the next possible completion of a command name that
705    begins with PREFIX, in the current command state, or a null
706    pointer if no completions remain.
707    Before calling the first time, set *CMD to a null pointer. */
708 const char *
709 cmd_complete (const char *prefix, const struct command **cmd)
710 {
711   if (*cmd == NULL)
712     *cmd = commands;
713
714   for (; *cmd < commands + command_cnt; (*cmd)++)
715     if (!memcasecmp ((*cmd)->name, prefix, strlen (prefix))
716         && (!((*cmd)->flags & F_TESTING) || settings_get_testing_mode ())
717         && (!((*cmd)->flags & F_ENHANCED) || settings_get_syntax () == ENHANCED)
718         && !((*cmd)->flags & F_ABBREV)
719         && ((*cmd)->function != NULL)
720         && in_correct_state (*cmd, completion_state))
721       return (*cmd)++->name;
722
723   return NULL;
724 }
725 \f
726 /* Simple commands. */
727
728 /* Parse and execute FINISH command. */
729 int
730 cmd_finish (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
731 {
732   return CMD_FINISH;
733 }
734
735 /* Parses the N command. */
736 int
737 cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
738 {
739   /* Value for N. */
740   int x;
741
742   if (!lex_force_int (lexer))
743     return CMD_FAILURE;
744   x = lex_integer (lexer);
745   lex_get (lexer);
746   if (!lex_match_id (lexer, "ESTIMATED"))
747     dict_set_case_limit (dataset_dict (ds), x);
748
749   return lex_end_of_command (lexer);
750 }
751
752 /* Parses, performs the EXECUTE procedure. */
753 int
754 cmd_execute (struct lexer *lexer, struct dataset *ds)
755 {
756   bool ok = casereader_destroy (proc_open (ds));
757   if (!proc_commit (ds) || !ok)
758     return CMD_CASCADING_FAILURE;
759   return lex_end_of_command (lexer);
760 }
761
762 /* Parses, performs the ERASE command. */
763 int
764 cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
765 {
766   if (settings_get_safer_mode ())
767     {
768       msg (SE, _("This command not allowed when the SAFER option is set."));
769       return CMD_FAILURE;
770     }
771
772   if (!lex_force_match_id (lexer, "FILE"))
773     return CMD_FAILURE;
774   lex_match (lexer, '=');
775   if (!lex_force_string (lexer))
776     return CMD_FAILURE;
777
778   if (remove (ds_cstr (lex_tokstr (lexer))) == -1)
779     {
780       msg (SW, _("Error removing `%s': %s."),
781            ds_cstr (lex_tokstr (lexer)), strerror (errno));
782       return CMD_FAILURE;
783     }
784
785   return CMD_SUCCESS;
786 }
787
788 #if HAVE_FORK && HAVE_EXECL
789 /* Spawn an interactive shell process. */
790 static bool
791 shell (void)
792 {
793   int pid;
794
795   pid = fork ();
796   switch (pid)
797     {
798     case 0:
799       {
800         const char *shell_fn;
801         char *shell_process;
802
803         {
804           int i;
805
806           for (i = 3; i < 20; i++)
807             close (i);
808         }
809
810         shell_fn = getenv ("SHELL");
811         if (shell_fn == NULL)
812           shell_fn = "/bin/sh";
813
814         {
815           const char *cp = strrchr (shell_fn, '/');
816           cp = cp ? &cp[1] : shell_fn;
817           shell_process = xmalloca (strlen (cp) + 8);
818           strcpy (shell_process, "-");
819           strcat (shell_process, cp);
820           if (strcmp (cp, "sh"))
821             shell_process[0] = '+';
822         }
823
824         execl (shell_fn, shell_process, NULL);
825
826         _exit (1);
827       }
828
829     case -1:
830       msg (SE, _("Couldn't fork: %s."), strerror (errno));
831       return false;
832
833     default:
834       assert (pid > 0);
835       while (wait (NULL) != pid)
836         ;
837       return true;
838     }
839 }
840 #else /* !(HAVE_FORK && HAVE_EXECL) */
841 /* Don't know how to spawn an interactive shell. */
842 static bool
843 shell (void)
844 {
845   msg (SE, _("Interactive shell not supported on this platform."));
846   return false;
847 }
848 #endif
849
850 /* Executes the specified COMMAND in a subshell.  Returns true if
851    successful, false otherwise. */
852 static bool
853 run_command (const char *command)
854 {
855   if (system (NULL) == 0)
856     {
857       msg (SE, _("Command shell not supported on this platform."));
858       return false;
859     }
860
861   /* Execute the command. */
862   if (system (command) == -1)
863     msg (SE, _("Error executing command: %s."), strerror (errno));
864
865   return true;
866 }
867
868 /* Parses, performs the HOST command. */
869 int
870 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
871 {
872   int look_ahead;
873
874   if (settings_get_safer_mode ())
875     {
876       msg (SE, _("This command not allowed when the SAFER option is set."));
877       return CMD_FAILURE;
878     }
879
880   look_ahead = lex_look_ahead (lexer);
881   if (look_ahead == '.')
882     {
883       lex_get (lexer);
884       return shell () ? CMD_SUCCESS : CMD_FAILURE;
885     }
886   else if (look_ahead == '\'' || look_ahead == '"')
887     {
888       bool ok;
889
890       lex_get (lexer);
891       if (!lex_force_string (lexer))
892         NOT_REACHED ();
893       ok = run_command (ds_cstr (lex_tokstr (lexer)));
894
895       lex_get (lexer);
896       return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
897     }
898   else
899     {
900       bool ok = run_command (lex_rest_of_line (lexer));
901       lex_discard_line (lexer);
902       return ok ? CMD_SUCCESS : CMD_FAILURE;
903     }
904 }
905
906 /* Parses, performs the NEW FILE command. */
907 int
908 cmd_new_file (struct lexer *lexer, struct dataset *ds)
909 {
910   proc_discard_active_file (ds);
911
912   return lex_end_of_command (lexer);
913 }
914
915 /* Parses a comment. */
916 int
917 cmd_comment (struct lexer *lexer, struct dataset *ds UNUSED)
918 {
919   lex_skip_comment (lexer);
920   return CMD_SUCCESS;
921 }