1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include <language/command.h>
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/assertion.h>
35 #include <libpspp/compiler.h>
36 #include <libpspp/message.h>
37 #include <libpspp/message.h>
38 #include <libpspp/str.h>
39 #include <output/manager.h>
40 #include <output/table.h>
41 #include <libpspp/getl.h>
48 #include <readline/readline.h>
55 #define _(msgid) gettext (msgid)
56 #define N_(msgid) msgid
58 /* Returns true if RESULT is a valid "enum cmd_result",
61 cmd_result_is_valid (enum cmd_result result)
63 return (result == CMD_SUCCESS || result == CMD_EOF || result == CMD_FINISH
64 || (result >= CMD_PRIVATE_FIRST && result <= CMD_PRIVATE_LAST)
65 || result == CMD_FAILURE || result == CMD_NOT_IMPLEMENTED
66 || result == CMD_CASCADING_FAILURE);
69 /* Returns true if RESULT indicates success,
72 cmd_result_is_success (enum cmd_result result)
74 assert (cmd_result_is_valid (result));
78 /* Returns true if RESULT indicates failure,
81 cmd_result_is_failure (enum cmd_result result)
83 assert (cmd_result_is_valid (result));
87 /* Command processing states. */
90 S_INITIAL = 0x01, /* Allowed before active file defined. */
91 S_DATA = 0x02, /* Allowed after active file defined. */
92 S_INPUT_PROGRAM = 0x04, /* Allowed in INPUT PROGRAM. */
93 S_FILE_TYPE = 0x08, /* Allowed in FILE TYPE. */
94 S_ANY = 0x0f /* Allowed anywhere. */
97 /* Other command requirements. */
100 F_ENHANCED = 0x10, /* Allowed only in enhanced syntax mode. */
101 F_TESTING = 0x20, /* Allowed only in testing mode. */
102 F_KEEP_FINAL_TOKEN = 0x40,/* Don't skip final token in command name. */
103 F_ABBREV = 0x80 /* Not a candidate for name completion. */
106 /* A single command. */
109 enum states states; /* States in which command is allowed. */
110 enum flags flags; /* Other command requirements. */
111 const char *name; /* Command name. */
112 int (*function) (struct lexer *, struct dataset *); /* Function to call. */
115 /* Define the command array. */
116 #define DEF_CMD(STATES, FLAGS, NAME, FUNCTION) {STATES, FLAGS, NAME, FUNCTION},
117 #define UNIMPL_CMD(NAME, DESCRIPTION) {S_ANY, 0, NAME, NULL},
118 static const struct command commands[] =
120 #include "command.def"
125 static const size_t command_cnt = sizeof commands / sizeof *commands;
127 static bool in_correct_state (const struct command *, enum cmd_state);
128 static bool report_state_mismatch (const struct command *, enum cmd_state);
129 static const struct command *find_command (const char *name);
130 static void set_completion_state (enum cmd_state);
132 /* Command parser. */
134 static const struct command *parse_command_name (struct lexer *lexer);
135 static enum cmd_result do_parse_command (struct lexer *, struct dataset *, enum cmd_state);
137 /* Parses an entire command, from command name to terminating
138 dot. On failure, skips to the terminating dot.
139 Returns the command's success or failure result. */
141 cmd_parse_in_state (struct lexer *lexer, struct dataset *ds,
142 enum cmd_state state)
148 result = do_parse_command (lexer, ds, state);
149 if (cmd_result_is_failure (result))
150 lex_discard_rest_of_command (lexer);
152 assert (!proc_is_open (ds));
153 unset_cmd_algorithm ();
154 dict_clear_aux (dataset_dict (ds));
155 if (!dataset_end_of_command (ds))
156 result = CMD_CASCADING_FAILURE;
162 cmd_parse (struct lexer *lexer, struct dataset *ds)
164 const struct dictionary *dict = dataset_dict (ds);
165 return cmd_parse_in_state (lexer, ds,
166 proc_has_active_file (ds) &&
167 dict_get_var_cnt (dict) > 0 ?
168 CMD_STATE_DATA : CMD_STATE_INITIAL);
172 /* Parses an entire command, from command name to terminating
174 static enum cmd_result
175 do_parse_command (struct lexer *lexer,
176 struct dataset *ds, enum cmd_state state)
178 const struct command *command;
179 enum cmd_result result;
181 /* Read the command's first token. */
182 prompt_set_style (PROMPT_FIRST);
183 set_completion_state (state);
185 if (lex_token (lexer) == T_STOP)
190 else if (lex_token (lexer) == '.')
192 /* Null commands can result from extra empty lines. */
193 result = CMD_SUCCESS;
197 prompt_set_style (PROMPT_LATER);
199 /* Parse the command name. */
200 command = parse_command_name (lexer);
203 result = CMD_FAILURE;
206 else if (command->function == NULL)
208 msg (SE, _("%s is unimplemented."), command->name);
209 result = CMD_NOT_IMPLEMENTED;
212 else if ((command->flags & F_TESTING) && !settings_get_testing_mode ())
214 msg (SE, _("%s may be used only in testing mode."), command->name);
215 result = CMD_FAILURE;
218 else if ((command->flags & F_ENHANCED) && settings_get_syntax () != ENHANCED)
220 msg (SE, _("%s may be used only in enhanced syntax mode."),
222 result = CMD_FAILURE;
225 else if (!in_correct_state (command, state))
227 report_state_mismatch (command, state);
228 result = CMD_FAILURE;
232 /* Execute command. */
233 msg_set_command_name (command->name);
234 tab_set_command_name (command->name);
235 result = command->function (lexer, ds);
236 tab_set_command_name (NULL);
237 msg_set_command_name (NULL);
239 assert (cmd_result_is_valid (result));
242 if ( cmd_result_is_failure (result))
244 const struct source_stream *cs = lex_get_source_stream (lexer);
246 if ( source_stream_current_error_mode (cs) == ERRMODE_STOP )
248 msg (MW, _("Error encountered while ERROR=STOP is effective."));
249 result = CMD_CASCADING_FAILURE;
257 match_strings (const char *a, size_t a_len,
258 const char *b, size_t b_len)
260 size_t match_len = 0;
262 while (a_len > 0 && b_len > 0)
264 /* Mismatch always returns zero. */
265 if (toupper ((unsigned char) *a++) != toupper ((unsigned char) *b++))
277 /* Returns the first character in the first word in STRING,
278 storing the word's length in *WORD_LEN. If no words remain,
279 returns a null pointer and stores 0 in *WORD_LEN. Words are
280 sequences of alphanumeric characters or single
281 non-alphanumeric characters. Words are delimited by
284 find_word (const char *string, size_t *word_len)
286 /* Skip whitespace and asterisks. */
287 while (isspace ((unsigned char) *string))
297 /* Special one-character word? */
298 if (!isalnum ((unsigned char) *string))
304 /* Alphanumeric word. */
306 while (isalnum ((unsigned char) string[*word_len]))
312 /* Returns true if strings A and B can be confused based on
313 their first three letters. */
315 conflicting_3char_prefixes (const char *a, const char *b)
317 size_t aw_len, bw_len;
320 aw = find_word (a, &aw_len);
321 bw = find_word (b, &bw_len);
322 assert (aw != NULL && bw != NULL);
324 /* Words that are the same don't conflict. */
325 if (aw_len == bw_len && !buf_compare_case (aw, bw, aw_len))
328 /* Words that are otherwise the same in the first three letters
330 return ((aw_len > 3 && bw_len > 3)
331 || (aw_len == 3 && bw_len > 3)
332 || (bw_len == 3 && aw_len > 3)) && !buf_compare_case (aw, bw, 3);
335 /* Returns true if CMD can be confused with another command
336 based on the first three letters of its first word. */
338 conflicting_3char_prefix_command (const struct command *cmd)
340 assert (cmd >= commands && cmd < commands + command_cnt);
342 return ((cmd > commands
343 && conflicting_3char_prefixes (cmd[-1].name, cmd[0].name))
344 || (cmd < commands + command_cnt
345 && conflicting_3char_prefixes (cmd[0].name, cmd[1].name)));
348 /* Ways that a set of words can match a command name. */
351 MISMATCH, /* Not a match. */
352 PARTIAL_MATCH, /* The words begin the command name. */
353 COMPLETE_MATCH /* The words are the command name. */
356 /* Figures out how well the WORD_CNT words in WORDS match CMD,
357 and returns the appropriate enum value. If WORDS are a
358 partial match for CMD and the next word in CMD is a dash, then
359 *DASH_POSSIBLE is set to 1 if DASH_POSSIBLE is non-null;
360 otherwise, *DASH_POSSIBLE is unchanged. */
361 static enum command_match
362 cmd_match_words (const struct command *cmd,
363 char *const words[], size_t word_cnt,
370 for (word = find_word (cmd->name, &word_len), word_idx = 0;
371 word != NULL && word_idx < word_cnt;
372 word = find_word (word + word_len, &word_len), word_idx++)
373 if (word_len != strlen (words[word_idx])
374 || buf_compare_case (word, words[word_idx], word_len))
376 size_t match_chars = match_strings (word, word_len,
378 strlen (words[word_idx]));
379 if (match_chars == 0)
384 else if (match_chars == 1 || match_chars == 2)
386 /* One- and two-character abbreviations are not
390 else if (match_chars == 3)
392 /* Three-character abbreviations are acceptable
393 in the first word of a command if there are
394 no name conflicts. They are always
395 acceptable after the first word. */
396 if (word_idx == 0 && conflicting_3char_prefix_command (cmd))
399 else /* match_chars > 3 */
401 /* Four-character and longer abbreviations are
402 always acceptable. */
406 if (word == NULL && word_idx == word_cnt)
408 /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR"}. */
409 return COMPLETE_MATCH;
411 else if (word == NULL)
413 /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR", "BAZ"}. */
418 /* cmd->name = "FOO BAR BAZ", words[] = {"FOO", "BAR"}. */
419 if (word[0] == '-' && dash_possible != NULL)
421 return PARTIAL_MATCH;
425 /* Returns the number of commands for which the WORD_CNT words in
426 WORDS are a partial or complete match. If some partial match
427 has a dash as the next word, then *DASH_POSSIBLE is set to 1,
428 otherwise it is set to 0. */
430 count_matching_commands (char *const words[], size_t word_cnt,
433 const struct command *cmd;
438 for (cmd = commands; cmd < commands + command_cnt; cmd++)
439 if (cmd_match_words (cmd, words, word_cnt, dash_possible) != MISMATCH)
442 return cmd_match_count;
445 /* Returns the command for which the WORD_CNT words in WORDS are
446 a complete match. Returns a null pointer if no such command
448 static const struct command *
449 get_complete_match (char *const words[], size_t word_cnt)
451 const struct command *cmd;
453 for (cmd = commands; cmd < commands + command_cnt; cmd++)
454 if (cmd_match_words (cmd, words, word_cnt, NULL) == COMPLETE_MATCH)
460 /* Returns the command with the given exact NAME.
461 Aborts if no such command exists. */
462 static const struct command *
463 find_command (const char *name)
465 const struct command *cmd;
467 for (cmd = commands; cmd < commands + command_cnt; cmd++)
468 if (!strcmp (cmd->name, name))
473 /* Frees the WORD_CNT words in WORDS. */
475 free_words (char *words[], size_t word_cnt)
479 for (idx = 0; idx < word_cnt; idx++)
483 /* Flags an error that the command whose name is given by the
484 WORD_CNT words in WORDS is unknown. */
486 unknown_command_error (struct lexer *lexer, char *const words[], size_t word_cnt)
489 lex_error (lexer, _("expecting command name"));
496 for (i = 0; i < word_cnt; i++)
499 ds_put_char (&s, ' ');
500 ds_put_cstr (&s, words[i]);
503 msg (SE, _("Unknown command %s."), ds_cstr (&s));
509 /* Parse the command name and return a pointer to the corresponding
510 struct command if successful.
511 If not successful, return a null pointer. */
512 static const struct command *
513 parse_command_name (struct lexer *lexer)
517 int complete_word_cnt;
520 if (lex_token (lexer) == T_EXP ||
521 lex_token (lexer) == '*' || lex_token (lexer) == '[')
522 return find_command ("COMMENT");
525 word_cnt = complete_word_cnt = 0;
526 while (lex_token (lexer) == T_ID || (dash_possible && lex_token (lexer) == '-'))
530 assert (word_cnt < sizeof words / sizeof *words);
531 if (lex_token (lexer) == T_ID)
533 words[word_cnt] = ds_xstrdup (lex_tokstr (lexer));
534 str_uppercase (words[word_cnt]);
536 else if (lex_token (lexer) == '-')
537 words[word_cnt] = xstrdup ("-");
540 cmd_match_cnt = count_matching_commands (words, word_cnt,
542 if (cmd_match_cnt == 0)
544 else if (cmd_match_cnt == 1)
546 const struct command *command = get_complete_match (words, word_cnt);
549 if (!(command->flags & F_KEEP_FINAL_TOKEN))
551 free_words (words, word_cnt);
555 else /* cmd_match_cnt > 1 */
557 /* Do we have a complete command name so far? */
558 if (get_complete_match (words, word_cnt) != NULL)
559 complete_word_cnt = word_cnt;
564 /* If we saw a complete command name earlier, drop back to
566 if (complete_word_cnt)
568 int pushback_word_cnt;
569 const struct command *command;
571 /* Get the command. */
572 command = get_complete_match (words, complete_word_cnt);
573 assert (command != NULL);
575 /* Figure out how many words we want to keep.
576 We normally want to swallow the entire command. */
577 pushback_word_cnt = complete_word_cnt + 1;
578 if (command->flags & F_KEEP_FINAL_TOKEN)
581 /* FIXME: We only support one-token pushback. */
582 assert (pushback_word_cnt + 1 >= word_cnt);
584 while (word_cnt > pushback_word_cnt)
587 if (strcmp (words[word_cnt], "-"))
588 lex_put_back_id (lexer, words[word_cnt]);
590 lex_put_back (lexer, '-');
591 free (words[word_cnt]);
594 free_words (words, word_cnt);
598 /* We didn't get a valid command name. */
599 unknown_command_error (lexer, words, word_cnt);
600 free_words (words, word_cnt);
604 /* Returns true if COMMAND is allowed in STATE,
607 in_correct_state (const struct command *command, enum cmd_state state)
609 return ((state == CMD_STATE_INITIAL && command->states & S_INITIAL)
610 || (state == CMD_STATE_DATA && command->states & S_DATA)
611 || (state == CMD_STATE_INPUT_PROGRAM
612 && command->states & S_INPUT_PROGRAM)
613 || (state == CMD_STATE_FILE_TYPE && command->states & S_FILE_TYPE));
616 /* Emits an appropriate error message for trying to invoke
619 report_state_mismatch (const struct command *command, enum cmd_state state)
621 assert (!in_correct_state (command, state));
622 if (state == CMD_STATE_INITIAL || state == CMD_STATE_DATA)
624 switch (command->states)
626 /* One allowed state. */
628 msg (SE, _("%s is allowed only before the active file has "
629 "been defined."), command->name);
632 msg (SE, _("%s is allowed only after the active file has "
633 "been defined."), command->name);
635 case S_INPUT_PROGRAM:
636 msg (SE, _("%s is allowed only inside INPUT PROGRAM."),
640 msg (SE, _("%s is allowed only inside FILE TYPE."), command->name);
643 /* Two allowed states. */
644 case S_INITIAL | S_DATA:
646 case S_INITIAL | S_INPUT_PROGRAM:
647 msg (SE, _("%s is allowed only before the active file has "
648 "been defined or inside INPUT PROGRAM."), command->name);
650 case S_INITIAL | S_FILE_TYPE:
651 msg (SE, _("%s is allowed only before the active file has "
652 "been defined or inside FILE TYPE."), command->name);
654 case S_DATA | S_INPUT_PROGRAM:
655 msg (SE, _("%s is allowed only after the active file has "
656 "been defined or inside INPUT PROGRAM."), command->name);
658 case S_DATA | S_FILE_TYPE:
659 msg (SE, _("%s is allowed only after the active file has "
660 "been defined or inside FILE TYPE."), command->name);
662 case S_INPUT_PROGRAM | S_FILE_TYPE:
663 msg (SE, _("%s is allowed only inside INPUT PROGRAM "
664 "or inside FILE TYPE."), command->name);
667 /* Three allowed states. */
668 case S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
669 msg (SE, _("%s is allowed only after the active file has "
670 "been defined, inside INPUT PROGRAM, or inside "
671 "FILE TYPE."), command->name);
673 case S_INITIAL | S_INPUT_PROGRAM | S_FILE_TYPE:
674 msg (SE, _("%s is allowed only before the active file has "
675 "been defined, inside INPUT PROGRAM, or inside "
676 "FILE TYPE."), command->name);
678 case S_INITIAL | S_DATA | S_FILE_TYPE:
680 case S_INITIAL | S_DATA | S_INPUT_PROGRAM:
683 /* Four allowed states. */
684 case S_INITIAL | S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
691 else if (state == CMD_STATE_INPUT_PROGRAM)
692 msg (SE, _("%s is not allowed inside INPUT PROGRAM."), command->name);
693 else if (state == CMD_STATE_FILE_TYPE)
694 msg (SE, _("%s is not allowed inside FILE TYPE."), command->name);
699 /* Command name completion. */
701 static enum cmd_state completion_state = CMD_STATE_INITIAL;
704 set_completion_state (enum cmd_state state)
706 completion_state = state;
709 /* Returns the next possible completion of a command name that
710 begins with PREFIX, in the current command state, or a null
711 pointer if no completions remain.
712 Before calling the first time, set *CMD to a null pointer. */
714 cmd_complete (const char *prefix, const struct command **cmd)
719 for (; *cmd < commands + command_cnt; (*cmd)++)
720 if (!memcasecmp ((*cmd)->name, prefix, strlen (prefix))
721 && (!((*cmd)->flags & F_TESTING) || settings_get_testing_mode ())
722 && (!((*cmd)->flags & F_ENHANCED) || settings_get_syntax () == ENHANCED)
723 && !((*cmd)->flags & F_ABBREV)
724 && ((*cmd)->function != NULL)
725 && in_correct_state (*cmd, completion_state))
726 return (*cmd)++->name;
731 /* Simple commands. */
733 /* Parse and execute FINISH command. */
735 cmd_finish (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
740 /* Parses the N command. */
742 cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
747 if (!lex_force_int (lexer))
749 x = lex_integer (lexer);
751 if (!lex_match_id (lexer, "ESTIMATED"))
752 dict_set_case_limit (dataset_dict (ds), x);
754 return lex_end_of_command (lexer);
757 /* Parses, performs the EXECUTE procedure. */
759 cmd_execute (struct lexer *lexer, struct dataset *ds)
761 bool ok = casereader_destroy (proc_open (ds));
762 if (!proc_commit (ds) || !ok)
763 return CMD_CASCADING_FAILURE;
764 return lex_end_of_command (lexer);
767 /* Parses, performs the ERASE command. */
769 cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
771 if (settings_get_safer_mode ())
773 msg (SE, _("This command not allowed when the SAFER option is set."));
777 if (!lex_force_match_id (lexer, "FILE"))
779 lex_match (lexer, '=');
780 if (!lex_force_string (lexer))
783 if (remove (ds_cstr (lex_tokstr (lexer))) == -1)
785 msg (SW, _("Error removing `%s': %s."),
786 ds_cstr (lex_tokstr (lexer)), strerror (errno));
793 #if HAVE_FORK && HAVE_EXECL
794 /* Spawn an interactive shell process. */
805 const char *shell_fn;
811 for (i = 3; i < 20; i++)
815 shell_fn = getenv ("SHELL");
816 if (shell_fn == NULL)
817 shell_fn = "/bin/sh";
820 const char *cp = strrchr (shell_fn, '/');
821 cp = cp ? &cp[1] : shell_fn;
822 shell_process = xmalloca (strlen (cp) + 8);
823 strcpy (shell_process, "-");
824 strcat (shell_process, cp);
825 if (strcmp (cp, "sh"))
826 shell_process[0] = '+';
829 execl (shell_fn, shell_process, NULL);
835 msg (SE, _("Couldn't fork: %s."), strerror (errno));
840 while (wait (NULL) != pid)
845 #else /* !(HAVE_FORK && HAVE_EXECL) */
846 /* Don't know how to spawn an interactive shell. */
850 msg (SE, _("Interactive shell not supported on this platform."));
855 /* Executes the specified COMMAND in a subshell. Returns true if
856 successful, false otherwise. */
858 run_command (const char *command)
860 if (system (NULL) == 0)
862 msg (SE, _("Command shell not supported on this platform."));
866 /* Execute the command. */
867 if (system (command) == -1)
868 msg (SE, _("Error executing command: %s."), strerror (errno));
873 /* Parses, performs the HOST command. */
875 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
879 if (settings_get_safer_mode ())
881 msg (SE, _("This command not allowed when the SAFER option is set."));
885 look_ahead = lex_look_ahead (lexer);
886 if (look_ahead == '.')
889 return shell () ? CMD_SUCCESS : CMD_FAILURE;
891 else if (look_ahead == '\'' || look_ahead == '"')
896 if (!lex_force_string (lexer))
898 ok = run_command (ds_cstr (lex_tokstr (lexer)));
901 return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
905 bool ok = run_command (lex_rest_of_line (lexer));
906 lex_discard_line (lexer);
907 return ok ? CMD_SUCCESS : CMD_FAILURE;
911 /* Parses, performs the NEW FILE command. */
913 cmd_new_file (struct lexer *lexer, struct dataset *ds)
915 proc_discard_active_file (ds);
917 return lex_end_of_command (lexer);
920 /* Parses a comment. */
922 cmd_comment (struct lexer *lexer, struct dataset *ds UNUSED)
924 lex_skip_comment (lexer);