Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / command.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 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
27 #include <data/casereader.h>
28 #include <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/settings.h>
31 #include <data/variable.h>
32 #include <language/lexer/lexer.h>
33 #include <language/prompt.h>
34 #include <libpspp/alloc.h>
35 #include <libpspp/assertion.h>
36 #include <libpspp/compiler.h>
37 #include <libpspp/message.h>
38 #include <libpspp/message.h>
39 #include <libpspp/str.h>
40 #include <output/manager.h>
41 #include <output/table.h>
42
43 #if HAVE_SYS_WAIT_H
44 #include <sys/wait.h>
45 #endif
46
47 #if HAVE_READLINE
48 #include <readline/readline.h>
49 #endif
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   som_new_series ();
144
145   result = do_parse_command (lexer, ds, state);
146   if (cmd_result_is_failure (result))
147     lex_discard_rest_of_command (lexer);
148
149   assert (!proc_is_open (ds));
150   unset_cmd_algorithm ();
151   dict_clear_aux (dataset_dict (ds));
152   if (!dataset_end_of_command (ds))
153     result = CMD_CASCADING_FAILURE;
154
155   return result;
156 }
157
158 enum cmd_result
159 cmd_parse (struct lexer *lexer, struct dataset *ds)
160 {
161   const struct dictionary *dict = dataset_dict (ds);
162   return cmd_parse_in_state (lexer, ds,
163                              proc_has_active_file (ds) &&
164                              dict_get_var_cnt (dict) > 0 ?
165                              CMD_STATE_DATA : CMD_STATE_INITIAL);
166 }
167
168
169 /* Parses an entire command, from command name to terminating
170    dot. */
171 static enum cmd_result
172 do_parse_command (struct lexer *lexer, struct dataset *ds, enum cmd_state state)
173 {
174   const struct command *command;
175   enum cmd_result result;
176
177   /* Read the command's first token. */
178   prompt_set_style (PROMPT_FIRST);
179   set_completion_state (state);
180   lex_get (lexer);
181   if (lex_token (lexer) == T_STOP)
182     return CMD_EOF;
183   else if (lex_token (lexer) == '.')
184     {
185       /* Null commands can result from extra empty lines. */
186       return CMD_SUCCESS;
187     }
188   prompt_set_style (PROMPT_LATER);
189
190   /* Parse the command name. */
191   command = parse_command_name (lexer);
192   if (command == NULL)
193     return CMD_FAILURE;
194   else if (command->function == NULL)
195     {
196       msg (SE, _("%s is unimplemented."), command->name);
197       return CMD_NOT_IMPLEMENTED;
198     }
199   else if ((command->flags & F_TESTING) && !get_testing_mode ())
200     {
201       msg (SE, _("%s may be used only in testing mode."), command->name);
202       return CMD_FAILURE;
203     }
204   else if ((command->flags & F_ENHANCED) && get_syntax () != ENHANCED)
205     {
206       msg (SE, _("%s may be used only in enhanced syntax mode."),
207            command->name);
208       return CMD_FAILURE;
209     }
210   else if (!in_correct_state (command, state))
211     {
212       report_state_mismatch (command, state);
213       return CMD_FAILURE;
214     }
215
216   /* Execute command. */
217   msg_set_command_name (command->name);
218   tab_set_command_name (command->name);
219   result = command->function (lexer, ds);
220   tab_set_command_name (NULL);
221   msg_set_command_name (NULL);
222
223   assert (cmd_result_is_valid (result));
224   return result;
225 }
226
227 static size_t
228 match_strings (const char *a, size_t a_len,
229                const char *b, size_t b_len)
230 {
231   size_t match_len = 0;
232
233   while (a_len > 0 && b_len > 0)
234     {
235       /* Mismatch always returns zero. */
236       if (toupper ((unsigned char) *a++) != toupper ((unsigned char) *b++))
237         return 0;
238
239       /* Advance. */
240       a_len--;
241       b_len--;
242       match_len++;
243     }
244
245   return match_len;
246 }
247
248 /* Returns the first character in the first word in STRING,
249    storing the word's length in *WORD_LEN.  If no words remain,
250    returns a null pointer and stores 0 in *WORD_LEN.  Words are
251    sequences of alphanumeric characters or single
252    non-alphanumeric characters.  Words are delimited by
253    spaces. */
254 static const char *
255 find_word (const char *string, size_t *word_len)
256 {
257   /* Skip whitespace and asterisks. */
258   while (isspace ((unsigned char) *string))
259     string++;
260
261   /* End of string? */
262   if (*string == '\0')
263     {
264       *word_len = 0;
265       return NULL;
266     }
267
268   /* Special one-character word? */
269   if (!isalnum ((unsigned char) *string))
270     {
271       *word_len = 1;
272       return string;
273     }
274
275   /* Alphanumeric word. */
276   *word_len = 1;
277   while (isalnum ((unsigned char) string[*word_len]))
278     (*word_len)++;
279
280   return string;
281 }
282
283 /* Returns true if strings A and B can be confused based on
284    their first three letters. */
285 static bool
286 conflicting_3char_prefixes (const char *a, const char *b)
287 {
288   size_t aw_len, bw_len;
289   const char *aw, *bw;
290
291   aw = find_word (a, &aw_len);
292   bw = find_word (b, &bw_len);
293   assert (aw != NULL && bw != NULL);
294
295   /* Words that are the same don't conflict. */
296   if (aw_len == bw_len && !buf_compare_case (aw, bw, aw_len))
297     return false;
298
299   /* Words that are otherwise the same in the first three letters
300      do conflict. */
301   return ((aw_len > 3 && bw_len > 3)
302           || (aw_len == 3 && bw_len > 3)
303           || (bw_len == 3 && aw_len > 3)) && !buf_compare_case (aw, bw, 3);
304 }
305
306 /* Returns true if CMD can be confused with another command
307    based on the first three letters of its first word. */
308 static bool
309 conflicting_3char_prefix_command (const struct command *cmd)
310 {
311   assert (cmd >= commands && cmd < commands + command_cnt);
312
313   return ((cmd > commands
314            && conflicting_3char_prefixes (cmd[-1].name, cmd[0].name))
315           || (cmd < commands + command_cnt
316               && conflicting_3char_prefixes (cmd[0].name, cmd[1].name)));
317 }
318
319 /* Ways that a set of words can match a command name. */
320 enum command_match
321   {
322     MISMATCH,           /* Not a match. */
323     PARTIAL_MATCH,      /* The words begin the command name. */
324     COMPLETE_MATCH      /* The words are the command name. */
325   };
326
327 /* Figures out how well the WORD_CNT words in WORDS match CMD,
328    and returns the appropriate enum value.  If WORDS are a
329    partial match for CMD and the next word in CMD is a dash, then
330    *DASH_POSSIBLE is set to 1 if DASH_POSSIBLE is non-null;
331    otherwise, *DASH_POSSIBLE is unchanged. */
332 static enum command_match
333 cmd_match_words (const struct command *cmd,
334                  char *const words[], size_t word_cnt,
335                  int *dash_possible)
336 {
337   const char *word;
338   size_t word_len;
339   size_t word_idx;
340
341   for (word = find_word (cmd->name, &word_len), word_idx = 0;
342        word != NULL && word_idx < word_cnt;
343        word = find_word (word + word_len, &word_len), word_idx++)
344     if (word_len != strlen (words[word_idx])
345         || buf_compare_case (word, words[word_idx], word_len))
346       {
347         size_t match_chars = match_strings (word, word_len,
348                                             words[word_idx],
349                                             strlen (words[word_idx]));
350         if (match_chars == 0)
351           {
352             /* Mismatch. */
353             return MISMATCH;
354           }
355         else if (match_chars == 1 || match_chars == 2)
356           {
357             /* One- and two-character abbreviations are not
358                acceptable. */
359             return MISMATCH;
360           }
361         else if (match_chars == 3)
362           {
363             /* Three-character abbreviations are acceptable
364                in the first word of a command if there are
365                no name conflicts.  They are always
366                acceptable after the first word. */
367             if (word_idx == 0 && conflicting_3char_prefix_command (cmd))
368               return MISMATCH;
369           }
370         else /* match_chars > 3 */
371           {
372             /* Four-character and longer abbreviations are
373                always acceptable.  */
374           }
375       }
376
377   if (word == NULL && word_idx == word_cnt)
378     {
379       /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR"}. */
380       return COMPLETE_MATCH;
381     }
382   else if (word == NULL)
383     {
384       /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR", "BAZ"}. */
385       return MISMATCH;
386     }
387   else
388     {
389       /* cmd->name = "FOO BAR BAZ", words[] = {"FOO", "BAR"}. */
390       if (word[0] == '-' && dash_possible != NULL)
391         *dash_possible = 1;
392       return PARTIAL_MATCH;
393     }
394 }
395
396 /* Returns the number of commands for which the WORD_CNT words in
397    WORDS are a partial or complete match.  If some partial match
398    has a dash as the next word, then *DASH_POSSIBLE is set to 1,
399    otherwise it is set to 0. */
400 static int
401 count_matching_commands (char *const words[], size_t word_cnt,
402                          int *dash_possible)
403 {
404   const struct command *cmd;
405   int cmd_match_count;
406
407   cmd_match_count = 0;
408   *dash_possible = 0;
409   for (cmd = commands; cmd < commands + command_cnt; cmd++)
410     if (cmd_match_words (cmd, words, word_cnt, dash_possible) != MISMATCH)
411       cmd_match_count++;
412
413   return cmd_match_count;
414 }
415
416 /* Returns the command for which the WORD_CNT words in WORDS are
417    a complete match.  Returns a null pointer if no such command
418    exists. */
419 static const struct command *
420 get_complete_match (char *const words[], size_t word_cnt)
421 {
422   const struct command *cmd;
423
424   for (cmd = commands; cmd < commands + command_cnt; cmd++)
425     if (cmd_match_words (cmd, words, word_cnt, NULL) == COMPLETE_MATCH)
426       return cmd;
427
428   return NULL;
429 }
430
431 /* Returns the command with the given exact NAME.
432    Aborts if no such command exists. */
433 static const struct command *
434 find_command (const char *name)
435 {
436   const struct command *cmd;
437
438   for (cmd = commands; cmd < commands + command_cnt; cmd++)
439     if (!strcmp (cmd->name, name))
440       return cmd;
441   NOT_REACHED ();
442 }
443
444 /* Frees the WORD_CNT words in WORDS. */
445 static void
446 free_words (char *words[], size_t word_cnt)
447 {
448   size_t idx;
449
450   for (idx = 0; idx < word_cnt; idx++)
451     free (words[idx]);
452 }
453
454 /* Flags an error that the command whose name is given by the
455    WORD_CNT words in WORDS is unknown. */
456 static void
457 unknown_command_error (struct lexer *lexer, char *const words[], size_t word_cnt)
458 {
459   if (word_cnt == 0)
460     lex_error (lexer, _("expecting command name"));
461   else
462     {
463       struct string s;
464       size_t i;
465
466       ds_init_empty (&s);
467       for (i = 0; i < word_cnt; i++)
468         {
469           if (i != 0)
470             ds_put_char (&s, ' ');
471           ds_put_cstr (&s, words[i]);
472         }
473
474       msg (SE, _("Unknown command %s."), ds_cstr (&s));
475
476       ds_destroy (&s);
477     }
478 }
479
480 /* Parse the command name and return a pointer to the corresponding
481    struct command if successful.
482    If not successful, return a null pointer. */
483 static const struct command *
484 parse_command_name (struct lexer *lexer)
485 {
486   char *words[16];
487   int word_cnt;
488   int complete_word_cnt;
489   int dash_possible;
490
491   if (lex_token (lexer) == T_EXP ||
492                   lex_token (lexer) == '*' || lex_token (lexer) == '[')
493     return find_command ("COMMENT");
494
495   dash_possible = 0;
496   word_cnt = complete_word_cnt = 0;
497   while (lex_token (lexer) == T_ID || (dash_possible && lex_token (lexer) == '-'))
498     {
499       int cmd_match_cnt;
500
501       assert (word_cnt < sizeof words / sizeof *words);
502       if (lex_token (lexer) == T_ID)
503         {
504           words[word_cnt] = ds_xstrdup (lex_tokstr (lexer));
505           str_uppercase (words[word_cnt]);
506         }
507       else if (lex_token (lexer) == '-')
508         words[word_cnt] = xstrdup ("-");
509       word_cnt++;
510
511       cmd_match_cnt = count_matching_commands (words, word_cnt,
512                                                &dash_possible);
513       if (cmd_match_cnt == 0)
514         break;
515       else if (cmd_match_cnt == 1)
516         {
517           const struct command *command = get_complete_match (words, word_cnt);
518           if (command != NULL)
519             {
520               if (!(command->flags & F_KEEP_FINAL_TOKEN))
521                 lex_get (lexer);
522               free_words (words, word_cnt);
523               return command;
524             }
525         }
526       else /* cmd_match_cnt > 1 */
527         {
528           /* Do we have a complete command name so far? */
529           if (get_complete_match (words, word_cnt) != NULL)
530             complete_word_cnt = word_cnt;
531         }
532       lex_get (lexer);
533     }
534
535   /* If we saw a complete command name earlier, drop back to
536      it. */
537   if (complete_word_cnt)
538     {
539       int pushback_word_cnt;
540       const struct command *command;
541
542       /* Get the command. */
543       command = get_complete_match (words, complete_word_cnt);
544       assert (command != NULL);
545
546       /* Figure out how many words we want to keep.
547          We normally want to swallow the entire command. */
548       pushback_word_cnt = complete_word_cnt + 1;
549       if (command->flags & F_KEEP_FINAL_TOKEN)
550         pushback_word_cnt--;
551
552       /* FIXME: We only support one-token pushback. */
553       assert (pushback_word_cnt + 1 >= word_cnt);
554
555       while (word_cnt > pushback_word_cnt)
556         {
557           word_cnt--;
558           if (strcmp (words[word_cnt], "-"))
559             lex_put_back_id (lexer, words[word_cnt]);
560           else
561             lex_put_back (lexer, '-');
562           free (words[word_cnt]);
563         }
564
565       free_words (words, word_cnt);
566       return command;
567     }
568
569   /* We didn't get a valid command name. */
570   unknown_command_error (lexer, words, word_cnt);
571   free_words (words, word_cnt);
572   return NULL;
573 }
574
575 /* Returns true if COMMAND is allowed in STATE,
576    false otherwise. */
577 static bool
578 in_correct_state (const struct command *command, enum cmd_state state)
579 {
580   return ((state == CMD_STATE_INITIAL && command->states & S_INITIAL)
581           || (state == CMD_STATE_DATA && command->states & S_DATA)
582           || (state == CMD_STATE_INPUT_PROGRAM
583               && command->states & S_INPUT_PROGRAM)
584           || (state == CMD_STATE_FILE_TYPE && command->states & S_FILE_TYPE));
585 }
586
587 /* Emits an appropriate error message for trying to invoke
588    COMMAND in STATE. */
589 static bool
590 report_state_mismatch (const struct command *command, enum cmd_state state)
591 {
592   assert (!in_correct_state (command, state));
593   if (state == CMD_STATE_INITIAL || state == CMD_STATE_DATA)
594     {
595       const char *allowed[3];
596       int allowed_cnt;
597       char *s;
598
599       allowed_cnt = 0;
600       if (command->states & S_INITIAL)
601         allowed[allowed_cnt++] = _("before the active file has been defined");
602       else if (command->states & S_DATA)
603         allowed[allowed_cnt++] = _("after the active file has been defined");
604       if (command->states & S_INPUT_PROGRAM)
605         allowed[allowed_cnt++] = _("inside INPUT PROGRAM");
606       if (command->states & S_FILE_TYPE)
607         allowed[allowed_cnt++] = _("inside FILE TYPE");
608
609       if (allowed_cnt == 1)
610         s = xstrdup (allowed[0]);
611       else if (allowed_cnt == 2)
612         s = xasprintf (_("%s or %s"), allowed[0], allowed[1]);
613       else if (allowed_cnt == 3)
614         s = xasprintf (_("%s, %s, or %s"), allowed[0], allowed[1], allowed[2]);
615       else
616         NOT_REACHED ();
617
618       msg (SE, _("%s is allowed only %s."), command->name, s);
619
620       free (s);
621     }
622   else if (state == CMD_STATE_INPUT_PROGRAM)
623     msg (SE, _("%s is not allowed inside INPUT PROGRAM."), command->name);
624   else if (state == CMD_STATE_FILE_TYPE)
625     msg (SE, _("%s is not allowed inside FILE TYPE."), command->name);
626
627   return false;
628 }
629 \f
630 /* Command name completion. */
631
632 static enum cmd_state completion_state = CMD_STATE_INITIAL;
633
634 static void
635 set_completion_state (enum cmd_state state)
636 {
637   completion_state = state;
638 }
639
640 /* Returns the next possible completion of a command name that
641    begins with PREFIX, in the current command state, or a null
642    pointer if no completions remain.
643    Before calling the first time, set *CMD to a null pointer. */
644 const char *
645 cmd_complete (const char *prefix, const struct command **cmd)
646 {
647   if (*cmd == NULL)
648     *cmd = commands;
649
650   for (; *cmd < commands + command_cnt; (*cmd)++)
651     if (!memcasecmp ((*cmd)->name, prefix, strlen (prefix))
652         && (!((*cmd)->flags & F_TESTING) || get_testing_mode ())
653         && (!((*cmd)->flags & F_ENHANCED) || get_syntax () == ENHANCED)
654         && !((*cmd)->flags & F_ABBREV)
655         && ((*cmd)->function != NULL)
656         && in_correct_state (*cmd, completion_state))
657       return (*cmd)++->name;
658
659   return NULL;
660 }
661 \f
662 /* Simple commands. */
663
664 /* Parse and execute FINISH command. */
665 int
666 cmd_finish (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
667 {
668   return CMD_FINISH;
669 }
670
671 /* Parses the N command. */
672 int
673 cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
674 {
675   /* Value for N. */
676   int x;
677
678   if (!lex_force_int (lexer))
679     return CMD_FAILURE;
680   x = lex_integer (lexer);
681   lex_get (lexer);
682   if (!lex_match_id (lexer, "ESTIMATED"))
683     dict_set_case_limit (dataset_dict (ds), x);
684
685   return lex_end_of_command (lexer);
686 }
687
688 /* Parses, performs the EXECUTE procedure. */
689 int
690 cmd_execute (struct lexer *lexer, struct dataset *ds)
691 {
692   bool ok = casereader_destroy (proc_open (ds));
693   if (!proc_commit (ds) || !ok)
694     return CMD_CASCADING_FAILURE;
695   return lex_end_of_command (lexer);
696 }
697
698 /* Parses, performs the ERASE command. */
699 int
700 cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
701 {
702   if (get_safer_mode ())
703     {
704       msg (SE, _("This command not allowed when the SAFER option is set."));
705       return CMD_FAILURE;
706     }
707
708   if (!lex_force_match_id (lexer, "FILE"))
709     return CMD_FAILURE;
710   lex_match (lexer, '=');
711   if (!lex_force_string (lexer))
712     return CMD_FAILURE;
713
714   if (remove (ds_cstr (lex_tokstr (lexer))) == -1)
715     {
716       msg (SW, _("Error removing `%s': %s."),
717            ds_cstr (lex_tokstr (lexer)), strerror (errno));
718       return CMD_FAILURE;
719     }
720
721   return CMD_SUCCESS;
722 }
723
724 #if HAVE_FORK && HAVE_EXECL
725 /* Spawn an interactive shell process. */
726 static bool
727 shell (void)
728 {
729   int pid;
730
731   pid = fork ();
732   switch (pid)
733     {
734     case 0:
735       {
736         const char *shell_fn;
737         char *shell_process;
738
739         {
740           int i;
741
742           for (i = 3; i < 20; i++)
743             close (i);
744         }
745
746         shell_fn = getenv ("SHELL");
747         if (shell_fn == NULL)
748           shell_fn = "/bin/sh";
749
750         {
751           const char *cp = strrchr (shell_fn, '/');
752           cp = cp ? &cp[1] : shell_fn;
753           shell_process = local_alloc (strlen (cp) + 8);
754           strcpy (shell_process, "-");
755           strcat (shell_process, cp);
756           if (strcmp (cp, "sh"))
757             shell_process[0] = '+';
758         }
759
760         execl (shell_fn, shell_process, NULL);
761
762         _exit (1);
763       }
764
765     case -1:
766       msg (SE, _("Couldn't fork: %s."), strerror (errno));
767       return false;
768
769     default:
770       assert (pid > 0);
771       while (wait (NULL) != pid)
772         ;
773       return true;
774     }
775 }
776 #else /* !(HAVE_FORK && HAVE_EXECL) */
777 /* Don't know how to spawn an interactive shell. */
778 static bool
779 shell (void)
780 {
781   msg (SE, _("Interactive shell not supported on this platform."));
782   return false;
783 }
784 #endif
785
786 /* Executes the specified COMMAND in a subshell.  Returns true if
787    successful, false otherwise. */
788 static bool
789 run_command (const char *command)
790 {
791   if (system (NULL) == 0)
792     {
793       msg (SE, _("Command shell not supported on this platform."));
794       return false;
795     }
796
797   /* Execute the command. */
798   if (system (command) == -1)
799     msg (SE, _("Error executing command: %s."), strerror (errno));
800
801   return true;
802 }
803
804 /* Parses, performs the HOST command. */
805 int
806 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
807 {
808   int look_ahead;
809
810   if (get_safer_mode ())
811     {
812       msg (SE, _("This command not allowed when the SAFER option is set."));
813       return CMD_FAILURE;
814     }
815
816   look_ahead = lex_look_ahead (lexer);
817   if (look_ahead == '.')
818     {
819       lex_get (lexer);
820       return shell () ? CMD_SUCCESS : CMD_FAILURE;
821     }
822   else if (look_ahead == '\'' || look_ahead == '"')
823     {
824       bool ok;
825
826       lex_get (lexer);
827       if (!lex_force_string (lexer))
828         NOT_REACHED ();
829       ok = run_command (ds_cstr (lex_tokstr (lexer)));
830
831       lex_get (lexer);
832       return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
833     }
834   else
835     {
836       bool ok = run_command (lex_rest_of_line (lexer));
837       lex_discard_line (lexer);
838       return ok ? CMD_SUCCESS : CMD_FAILURE;
839     }
840 }
841
842 /* Parses, performs the NEW FILE command. */
843 int
844 cmd_new_file (struct lexer *lexer, struct dataset *ds)
845 {
846   proc_discard_active_file (ds);
847
848   return lex_end_of_command (lexer);
849 }
850
851 /* Parses a comment. */
852 int
853 cmd_comment (struct lexer *lexer, struct dataset *ds UNUSED)
854 {
855   lex_skip_comment (lexer);
856   return CMD_SUCCESS;
857 }