1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009 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>
30 #include <readline/readline.h>
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>
52 #define _(msgid) gettext (msgid)
53 #define N_(msgid) msgid
55 /* Returns true if RESULT is a valid "enum cmd_result",
58 cmd_result_is_valid (enum cmd_result result)
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);
66 /* Returns true if RESULT indicates success,
69 cmd_result_is_success (enum cmd_result result)
71 assert (cmd_result_is_valid (result));
75 /* Returns true if RESULT indicates failure,
78 cmd_result_is_failure (enum cmd_result result)
80 assert (cmd_result_is_valid (result));
84 /* Command processing states. */
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. */
94 /* Other command requirements. */
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. */
103 /* A single command. */
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. */
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[] =
117 #include "command.def"
122 static const size_t command_cnt = sizeof commands / sizeof *commands;
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);
129 /* Command parser. */
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);
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. */
138 cmd_parse_in_state (struct lexer *lexer, struct dataset *ds,
139 enum cmd_state state)
143 result = do_parse_command (lexer, ds, state);
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;
155 cmd_parse (struct lexer *lexer, struct dataset *ds)
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);
165 /* Parses an entire command, from command name to terminating
167 static enum cmd_result
168 do_parse_command (struct lexer *lexer,
169 struct dataset *ds, enum cmd_state state)
171 const struct command *command = NULL;
172 enum cmd_result result;
175 /* Read the command's first token. */
176 prompt_set_style (PROMPT_FIRST);
177 set_completion_state (state);
179 if (lex_token (lexer) == T_STOP)
184 else if (lex_token (lexer) == '.')
186 /* Null commands can result from extra empty lines. */
187 result = CMD_SUCCESS;
191 prompt_set_style (PROMPT_LATER);
193 /* Parse the command name. */
194 command = parse_command_name (lexer);
197 result = CMD_FAILURE;
200 text_item_submit (text_item_create (TEXT_ITEM_COMMAND_OPEN, command->name));
203 if (command->function == NULL)
205 msg (SE, _("%s is not yet implemented."), command->name);
206 result = CMD_NOT_IMPLEMENTED;
208 else if ((command->flags & F_TESTING) && !settings_get_testing_mode ())
210 msg (SE, _("%s may be used only in testing mode."), command->name);
211 result = CMD_FAILURE;
213 else if ((command->flags & F_ENHANCED) && settings_get_syntax () != ENHANCED)
215 msg (SE, _("%s may be used only in enhanced syntax mode."),
217 result = CMD_FAILURE;
219 else if (!in_correct_state (command, state))
221 report_state_mismatch (command, state);
222 result = CMD_FAILURE;
226 /* Execute command. */
227 result = command->function (lexer, ds);
230 assert (cmd_result_is_valid (result));
233 if (cmd_result_is_failure (result))
235 lex_discard_rest_of_command (lexer);
236 if (source_stream_current_error_mode (
237 lex_get_source_stream (lexer)) == ERRMODE_STOP )
239 msg (MW, _("Error encountered while ERROR=STOP is effective."));
240 result = CMD_CASCADING_FAILURE;
245 text_item_submit (text_item_create (TEXT_ITEM_COMMAND_CLOSE,
252 match_strings (const char *a, size_t a_len,
253 const char *b, size_t b_len)
255 size_t match_len = 0;
257 while (a_len > 0 && b_len > 0)
259 /* Mismatch always returns zero. */
260 if (toupper ((unsigned char) *a++) != toupper ((unsigned char) *b++))
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
279 find_word (const char *string, size_t *word_len)
281 /* Skip whitespace and asterisks. */
282 while (isspace ((unsigned char) *string))
292 /* Special one-character word? */
293 if (!isalnum ((unsigned char) *string))
299 /* Alphanumeric word. */
301 while (isalnum ((unsigned char) string[*word_len]))
307 /* Returns true if strings A and B can be confused based on
308 their first three letters. */
310 conflicting_3char_prefixes (const char *a, const char *b)
312 size_t aw_len, bw_len;
315 aw = find_word (a, &aw_len);
316 bw = find_word (b, &bw_len);
317 assert (aw != NULL && bw != NULL);
319 /* Words that are the same don't conflict. */
320 if (aw_len == bw_len && !buf_compare_case (aw, bw, aw_len))
323 /* Words that are otherwise the same in the first three letters
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);
330 /* Returns true if CMD can be confused with another command
331 based on the first three letters of its first word. */
333 conflicting_3char_prefix_command (const struct command *cmd)
335 assert (cmd >= commands && cmd < commands + command_cnt);
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)));
343 /* Ways that a set of words can match a command name. */
346 MISMATCH, /* Not a match. */
347 PARTIAL_MATCH, /* The words begin the command name. */
348 COMPLETE_MATCH /* The words are the command name. */
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,
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))
371 size_t match_chars = match_strings (word, word_len,
373 strlen (words[word_idx]));
374 if (match_chars == 0)
379 else if (match_chars == 1 || match_chars == 2)
381 /* One- and two-character abbreviations are not
385 else if (match_chars == 3)
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))
394 else /* match_chars > 3 */
396 /* Four-character and longer abbreviations are
397 always acceptable. */
401 if (word == NULL && word_idx == word_cnt)
403 /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR"}. */
404 return COMPLETE_MATCH;
406 else if (word == NULL)
408 /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR", "BAZ"}. */
413 /* cmd->name = "FOO BAR BAZ", words[] = {"FOO", "BAR"}. */
414 if (word[0] == '-' && dash_possible != NULL)
416 return PARTIAL_MATCH;
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. */
425 count_matching_commands (char *const words[], size_t word_cnt,
428 const struct command *cmd;
433 for (cmd = commands; cmd < commands + command_cnt; cmd++)
434 if (cmd_match_words (cmd, words, word_cnt, dash_possible) != MISMATCH)
437 return cmd_match_count;
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
443 static const struct command *
444 get_complete_match (char *const words[], size_t word_cnt)
446 const struct command *cmd;
448 for (cmd = commands; cmd < commands + command_cnt; cmd++)
449 if (cmd_match_words (cmd, words, word_cnt, NULL) == COMPLETE_MATCH)
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)
460 const struct command *cmd;
462 for (cmd = commands; cmd < commands + command_cnt; cmd++)
463 if (!strcmp (cmd->name, name))
468 /* Frees the WORD_CNT words in WORDS. */
470 free_words (char *words[], size_t word_cnt)
474 for (idx = 0; idx < word_cnt; idx++)
478 /* Flags an error that the command whose name is given by the
479 WORD_CNT words in WORDS is unknown. */
481 unknown_command_error (struct lexer *lexer, char *const words[], size_t word_cnt)
484 lex_error (lexer, _("expecting command name"));
491 for (i = 0; i < word_cnt; i++)
494 ds_put_char (&s, ' ');
495 ds_put_cstr (&s, words[i]);
498 msg (SE, _("Unknown command %s."), ds_cstr (&s));
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)
512 int complete_word_cnt;
515 if (lex_token (lexer) == T_EXP ||
516 lex_token (lexer) == '*' || lex_token (lexer) == '[')
517 return find_command ("COMMENT");
520 word_cnt = complete_word_cnt = 0;
521 while (lex_token (lexer) == T_ID || (dash_possible && lex_token (lexer) == '-'))
525 assert (word_cnt < sizeof words / sizeof *words);
526 if (lex_token (lexer) == T_ID)
528 words[word_cnt] = ds_xstrdup (lex_tokstr (lexer));
529 str_uppercase (words[word_cnt]);
531 else if (lex_token (lexer) == '-')
532 words[word_cnt] = xstrdup ("-");
535 cmd_match_cnt = count_matching_commands (words, word_cnt,
537 if (cmd_match_cnt == 0)
539 else if (cmd_match_cnt == 1)
541 const struct command *command = get_complete_match (words, word_cnt);
544 if (!(command->flags & F_KEEP_FINAL_TOKEN))
546 free_words (words, word_cnt);
550 else /* cmd_match_cnt > 1 */
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;
559 /* If we saw a complete command name earlier, drop back to
561 if (complete_word_cnt)
563 int pushback_word_cnt;
564 const struct command *command;
566 /* Get the command. */
567 command = get_complete_match (words, complete_word_cnt);
568 assert (command != NULL);
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)
576 /* FIXME: We only support one-token pushback. */
577 assert (pushback_word_cnt + 1 >= word_cnt);
579 while (word_cnt > pushback_word_cnt)
582 if (strcmp (words[word_cnt], "-"))
583 lex_put_back_id (lexer, words[word_cnt]);
585 lex_put_back (lexer, '-');
586 free (words[word_cnt]);
589 free_words (words, word_cnt);
593 /* We didn't get a valid command name. */
594 unknown_command_error (lexer, words, word_cnt);
595 free_words (words, word_cnt);
599 /* Returns true if COMMAND is allowed in STATE,
602 in_correct_state (const struct command *command, enum cmd_state state)
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));
611 /* Emits an appropriate error message for trying to invoke
614 report_state_mismatch (const struct command *command, enum cmd_state state)
616 assert (!in_correct_state (command, state));
617 if (state == CMD_STATE_INITIAL || state == CMD_STATE_DATA)
619 switch (command->states)
621 /* One allowed state. */
623 msg (SE, _("%s is allowed only before the active file has "
624 "been defined."), command->name);
627 msg (SE, _("%s is allowed only after the active file has "
628 "been defined."), command->name);
630 case S_INPUT_PROGRAM:
631 msg (SE, _("%s is allowed only inside INPUT PROGRAM."),
635 msg (SE, _("%s is allowed only inside FILE TYPE."), command->name);
638 /* Two allowed states. */
639 case S_INITIAL | S_DATA:
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);
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);
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);
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);
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);
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);
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);
673 case S_INITIAL | S_DATA | S_FILE_TYPE:
675 case S_INITIAL | S_DATA | S_INPUT_PROGRAM:
678 /* Four allowed states. */
679 case S_INITIAL | S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
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");
694 /* Command name completion. */
696 static enum cmd_state completion_state = CMD_STATE_INITIAL;
699 set_completion_state (enum cmd_state state)
701 completion_state = state;
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. */
709 cmd_complete (const char *prefix, const struct command **cmd)
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;
726 /* Simple commands. */
728 /* Parse and execute FINISH command. */
730 cmd_finish (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
735 /* Parses the N command. */
737 cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
742 if (!lex_force_int (lexer))
744 x = lex_integer (lexer);
746 if (!lex_match_id (lexer, "ESTIMATED"))
747 dict_set_case_limit (dataset_dict (ds), x);
749 return lex_end_of_command (lexer);
752 /* Parses, performs the EXECUTE procedure. */
754 cmd_execute (struct lexer *lexer, struct dataset *ds)
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);
762 /* Parses, performs the ERASE command. */
764 cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
766 if (settings_get_safer_mode ())
768 msg (SE, _("This command not allowed when the SAFER option is set."));
772 if (!lex_force_match_id (lexer, "FILE"))
774 lex_match (lexer, '=');
775 if (!lex_force_string (lexer))
778 if (remove (ds_cstr (lex_tokstr (lexer))) == -1)
780 msg (SW, _("Error removing `%s': %s."),
781 ds_cstr (lex_tokstr (lexer)), strerror (errno));
788 #if HAVE_FORK && HAVE_EXECL
789 /* Spawn an interactive shell process. */
800 const char *shell_fn;
806 for (i = 3; i < 20; i++)
810 shell_fn = getenv ("SHELL");
811 if (shell_fn == NULL)
812 shell_fn = "/bin/sh";
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] = '+';
824 execl (shell_fn, shell_process, NULL);
830 msg (SE, _("Couldn't fork: %s."), strerror (errno));
835 while (wait (NULL) != pid)
840 #else /* !(HAVE_FORK && HAVE_EXECL) */
841 /* Don't know how to spawn an interactive shell. */
845 msg (SE, _("Interactive shell not supported on this platform."));
850 /* Executes the specified COMMAND in a subshell. Returns true if
851 successful, false otherwise. */
853 run_command (const char *command)
855 if (system (NULL) == 0)
857 msg (SE, _("Command shell not supported on this platform."));
861 /* Execute the command. */
862 if (system (command) == -1)
863 msg (SE, _("Error executing command: %s."), strerror (errno));
868 /* Parses, performs the HOST command. */
870 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
874 if (settings_get_safer_mode ())
876 msg (SE, _("This command not allowed when the SAFER option is set."));
880 look_ahead = lex_look_ahead (lexer);
881 if (look_ahead == '.')
884 return shell () ? CMD_SUCCESS : CMD_FAILURE;
886 else if (look_ahead == '\'' || look_ahead == '"')
891 if (!lex_force_string (lexer))
893 ok = run_command (ds_cstr (lex_tokstr (lexer)));
896 return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
900 bool ok = run_command (lex_rest_of_line (lexer));
901 lex_discard_line (lexer);
902 return ok ? CMD_SUCCESS : CMD_FAILURE;
906 /* Parses, performs the NEW FILE command. */
908 cmd_new_file (struct lexer *lexer, struct dataset *ds)
910 proc_discard_active_file (ds);
912 return lex_end_of_command (lexer);
915 /* Parses a comment. */
917 cmd_comment (struct lexer *lexer, struct dataset *ds UNUSED)
919 lex_skip_comment (lexer);