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>
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 <libpspp/getl.h>
47 #include <readline/readline.h>
54 #define _(msgid) gettext (msgid)
55 #define N_(msgid) msgid
57 /* Returns true if RESULT is a valid "enum cmd_result",
60 cmd_result_is_valid (enum cmd_result result)
62 return (result == CMD_SUCCESS || result == CMD_EOF || result == CMD_FINISH
63 || (result >= CMD_PRIVATE_FIRST && result <= CMD_PRIVATE_LAST)
64 || result == CMD_FAILURE || result == CMD_NOT_IMPLEMENTED
65 || result == CMD_CASCADING_FAILURE);
68 /* Returns true if RESULT indicates success,
71 cmd_result_is_success (enum cmd_result result)
73 assert (cmd_result_is_valid (result));
77 /* Returns true if RESULT indicates failure,
80 cmd_result_is_failure (enum cmd_result result)
82 assert (cmd_result_is_valid (result));
86 /* Command processing states. */
89 S_INITIAL = 0x01, /* Allowed before active file defined. */
90 S_DATA = 0x02, /* Allowed after active file defined. */
91 S_INPUT_PROGRAM = 0x04, /* Allowed in INPUT PROGRAM. */
92 S_FILE_TYPE = 0x08, /* Allowed in FILE TYPE. */
93 S_ANY = 0x0f /* Allowed anywhere. */
96 /* Other command requirements. */
99 F_ENHANCED = 0x10, /* Allowed only in enhanced syntax mode. */
100 F_TESTING = 0x20, /* Allowed only in testing mode. */
101 F_KEEP_FINAL_TOKEN = 0x40,/* Don't skip final token in command name. */
102 F_ABBREV = 0x80 /* Not a candidate for name completion. */
105 /* A single command. */
108 enum states states; /* States in which command is allowed. */
109 enum flags flags; /* Other command requirements. */
110 const char *name; /* Command name. */
111 int (*function) (struct lexer *, struct dataset *); /* Function to call. */
114 /* Define the command array. */
115 #define DEF_CMD(STATES, FLAGS, NAME, FUNCTION) {STATES, FLAGS, NAME, FUNCTION},
116 #define UNIMPL_CMD(NAME, DESCRIPTION) {S_ANY, 0, NAME, NULL},
117 static const struct command commands[] =
119 #include "command.def"
124 static const size_t command_cnt = sizeof commands / sizeof *commands;
126 static bool in_correct_state (const struct command *, enum cmd_state);
127 static bool report_state_mismatch (const struct command *, enum cmd_state);
128 static const struct command *find_command (const char *name);
129 static void set_completion_state (enum cmd_state);
131 /* Command parser. */
133 static const struct command *parse_command_name (struct lexer *lexer);
134 static enum cmd_result do_parse_command (struct lexer *, struct dataset *, enum cmd_state);
136 /* Parses an entire command, from command name to terminating
137 dot. On failure, skips to the terminating dot.
138 Returns the command's success or failure result. */
140 cmd_parse_in_state (struct lexer *lexer, struct dataset *ds,
141 enum cmd_state state)
147 result = do_parse_command (lexer, ds, state);
148 if (cmd_result_is_failure (result))
149 lex_discard_rest_of_command (lexer);
151 assert (!proc_is_open (ds));
152 unset_cmd_algorithm ();
153 dict_clear_aux (dataset_dict (ds));
154 if (!dataset_end_of_command (ds))
155 result = CMD_CASCADING_FAILURE;
161 cmd_parse (struct lexer *lexer, struct dataset *ds)
163 const struct dictionary *dict = dataset_dict (ds);
164 return cmd_parse_in_state (lexer, ds,
165 proc_has_active_file (ds) &&
166 dict_get_var_cnt (dict) > 0 ?
167 CMD_STATE_DATA : CMD_STATE_INITIAL);
171 /* Parses an entire command, from command name to terminating
173 static enum cmd_result
174 do_parse_command (struct lexer *lexer,
175 struct dataset *ds, enum cmd_state state)
177 const struct command *command;
178 enum cmd_result result;
180 /* Read the command's first token. */
181 prompt_set_style (PROMPT_FIRST);
182 set_completion_state (state);
184 if (lex_token (lexer) == T_STOP)
189 else if (lex_token (lexer) == '.')
191 /* Null commands can result from extra empty lines. */
192 result = CMD_SUCCESS;
196 prompt_set_style (PROMPT_LATER);
198 /* Parse the command name. */
199 command = parse_command_name (lexer);
202 result = CMD_FAILURE;
205 else if (command->function == NULL)
207 msg (SE, _("%s is not yet implemented."), command->name);
208 result = CMD_NOT_IMPLEMENTED;
211 else if ((command->flags & F_TESTING) && !settings_get_testing_mode ())
213 msg (SE, _("%s may be used only in testing mode."), command->name);
214 result = CMD_FAILURE;
217 else if ((command->flags & F_ENHANCED) && settings_get_syntax () != ENHANCED)
219 msg (SE, _("%s may be used only in enhanced syntax mode."),
221 result = CMD_FAILURE;
224 else if (!in_correct_state (command, state))
226 report_state_mismatch (command, state);
227 result = CMD_FAILURE;
231 /* Execute command. */
232 msg_set_command_name (command->name);
233 som_set_command_name (command->name);
234 result = command->function (lexer, ds);
235 som_set_command_name (NULL);
236 msg_set_command_name (NULL);
238 assert (cmd_result_is_valid (result));
241 if ( cmd_result_is_failure (result))
243 const struct source_stream *cs = lex_get_source_stream (lexer);
245 if ( source_stream_current_error_mode (cs) == ERRMODE_STOP )
247 msg (MW, _("Error encountered while ERROR=STOP is effective."));
248 result = CMD_CASCADING_FAILURE;
256 match_strings (const char *a, size_t a_len,
257 const char *b, size_t b_len)
259 size_t match_len = 0;
261 while (a_len > 0 && b_len > 0)
263 /* Mismatch always returns zero. */
264 if (toupper ((unsigned char) *a++) != toupper ((unsigned char) *b++))
276 /* Returns the first character in the first word in STRING,
277 storing the word's length in *WORD_LEN. If no words remain,
278 returns a null pointer and stores 0 in *WORD_LEN. Words are
279 sequences of alphanumeric characters or single
280 non-alphanumeric characters. Words are delimited by
283 find_word (const char *string, size_t *word_len)
285 /* Skip whitespace and asterisks. */
286 while (isspace ((unsigned char) *string))
296 /* Special one-character word? */
297 if (!isalnum ((unsigned char) *string))
303 /* Alphanumeric word. */
305 while (isalnum ((unsigned char) string[*word_len]))
311 /* Returns true if strings A and B can be confused based on
312 their first three letters. */
314 conflicting_3char_prefixes (const char *a, const char *b)
316 size_t aw_len, bw_len;
319 aw = find_word (a, &aw_len);
320 bw = find_word (b, &bw_len);
321 assert (aw != NULL && bw != NULL);
323 /* Words that are the same don't conflict. */
324 if (aw_len == bw_len && !buf_compare_case (aw, bw, aw_len))
327 /* Words that are otherwise the same in the first three letters
329 return ((aw_len > 3 && bw_len > 3)
330 || (aw_len == 3 && bw_len > 3)
331 || (bw_len == 3 && aw_len > 3)) && !buf_compare_case (aw, bw, 3);
334 /* Returns true if CMD can be confused with another command
335 based on the first three letters of its first word. */
337 conflicting_3char_prefix_command (const struct command *cmd)
339 assert (cmd >= commands && cmd < commands + command_cnt);
341 return ((cmd > commands
342 && conflicting_3char_prefixes (cmd[-1].name, cmd[0].name))
343 || (cmd < commands + command_cnt
344 && conflicting_3char_prefixes (cmd[0].name, cmd[1].name)));
347 /* Ways that a set of words can match a command name. */
350 MISMATCH, /* Not a match. */
351 PARTIAL_MATCH, /* The words begin the command name. */
352 COMPLETE_MATCH /* The words are the command name. */
355 /* Figures out how well the WORD_CNT words in WORDS match CMD,
356 and returns the appropriate enum value. If WORDS are a
357 partial match for CMD and the next word in CMD is a dash, then
358 *DASH_POSSIBLE is set to 1 if DASH_POSSIBLE is non-null;
359 otherwise, *DASH_POSSIBLE is unchanged. */
360 static enum command_match
361 cmd_match_words (const struct command *cmd,
362 char *const words[], size_t word_cnt,
369 for (word = find_word (cmd->name, &word_len), word_idx = 0;
370 word != NULL && word_idx < word_cnt;
371 word = find_word (word + word_len, &word_len), word_idx++)
372 if (word_len != strlen (words[word_idx])
373 || buf_compare_case (word, words[word_idx], word_len))
375 size_t match_chars = match_strings (word, word_len,
377 strlen (words[word_idx]));
378 if (match_chars == 0)
383 else if (match_chars == 1 || match_chars == 2)
385 /* One- and two-character abbreviations are not
389 else if (match_chars == 3)
391 /* Three-character abbreviations are acceptable
392 in the first word of a command if there are
393 no name conflicts. They are always
394 acceptable after the first word. */
395 if (word_idx == 0 && conflicting_3char_prefix_command (cmd))
398 else /* match_chars > 3 */
400 /* Four-character and longer abbreviations are
401 always acceptable. */
405 if (word == NULL && word_idx == word_cnt)
407 /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR"}. */
408 return COMPLETE_MATCH;
410 else if (word == NULL)
412 /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR", "BAZ"}. */
417 /* cmd->name = "FOO BAR BAZ", words[] = {"FOO", "BAR"}. */
418 if (word[0] == '-' && dash_possible != NULL)
420 return PARTIAL_MATCH;
424 /* Returns the number of commands for which the WORD_CNT words in
425 WORDS are a partial or complete match. If some partial match
426 has a dash as the next word, then *DASH_POSSIBLE is set to 1,
427 otherwise it is set to 0. */
429 count_matching_commands (char *const words[], size_t word_cnt,
432 const struct command *cmd;
437 for (cmd = commands; cmd < commands + command_cnt; cmd++)
438 if (cmd_match_words (cmd, words, word_cnt, dash_possible) != MISMATCH)
441 return cmd_match_count;
444 /* Returns the command for which the WORD_CNT words in WORDS are
445 a complete match. Returns a null pointer if no such command
447 static const struct command *
448 get_complete_match (char *const words[], size_t word_cnt)
450 const struct command *cmd;
452 for (cmd = commands; cmd < commands + command_cnt; cmd++)
453 if (cmd_match_words (cmd, words, word_cnt, NULL) == COMPLETE_MATCH)
459 /* Returns the command with the given exact NAME.
460 Aborts if no such command exists. */
461 static const struct command *
462 find_command (const char *name)
464 const struct command *cmd;
466 for (cmd = commands; cmd < commands + command_cnt; cmd++)
467 if (!strcmp (cmd->name, name))
472 /* Frees the WORD_CNT words in WORDS. */
474 free_words (char *words[], size_t word_cnt)
478 for (idx = 0; idx < word_cnt; idx++)
482 /* Flags an error that the command whose name is given by the
483 WORD_CNT words in WORDS is unknown. */
485 unknown_command_error (struct lexer *lexer, char *const words[], size_t word_cnt)
488 lex_error (lexer, _("expecting command name"));
495 for (i = 0; i < word_cnt; i++)
498 ds_put_char (&s, ' ');
499 ds_put_cstr (&s, words[i]);
502 msg (SE, _("Unknown command %s."), ds_cstr (&s));
508 /* Parse the command name and return a pointer to the corresponding
509 struct command if successful.
510 If not successful, return a null pointer. */
511 static const struct command *
512 parse_command_name (struct lexer *lexer)
516 int complete_word_cnt;
519 if (lex_token (lexer) == T_EXP ||
520 lex_token (lexer) == '*' || lex_token (lexer) == '[')
521 return find_command ("COMMENT");
524 word_cnt = complete_word_cnt = 0;
525 while (lex_token (lexer) == T_ID || (dash_possible && lex_token (lexer) == '-'))
529 assert (word_cnt < sizeof words / sizeof *words);
530 if (lex_token (lexer) == T_ID)
532 words[word_cnt] = ds_xstrdup (lex_tokstr (lexer));
533 str_uppercase (words[word_cnt]);
535 else if (lex_token (lexer) == '-')
536 words[word_cnt] = xstrdup ("-");
539 cmd_match_cnt = count_matching_commands (words, word_cnt,
541 if (cmd_match_cnt == 0)
543 else if (cmd_match_cnt == 1)
545 const struct command *command = get_complete_match (words, word_cnt);
548 if (!(command->flags & F_KEEP_FINAL_TOKEN))
550 free_words (words, word_cnt);
554 else /* cmd_match_cnt > 1 */
556 /* Do we have a complete command name so far? */
557 if (get_complete_match (words, word_cnt) != NULL)
558 complete_word_cnt = word_cnt;
563 /* If we saw a complete command name earlier, drop back to
565 if (complete_word_cnt)
567 int pushback_word_cnt;
568 const struct command *command;
570 /* Get the command. */
571 command = get_complete_match (words, complete_word_cnt);
572 assert (command != NULL);
574 /* Figure out how many words we want to keep.
575 We normally want to swallow the entire command. */
576 pushback_word_cnt = complete_word_cnt + 1;
577 if (command->flags & F_KEEP_FINAL_TOKEN)
580 /* FIXME: We only support one-token pushback. */
581 assert (pushback_word_cnt + 1 >= word_cnt);
583 while (word_cnt > pushback_word_cnt)
586 if (strcmp (words[word_cnt], "-"))
587 lex_put_back_id (lexer, words[word_cnt]);
589 lex_put_back (lexer, '-');
590 free (words[word_cnt]);
593 free_words (words, word_cnt);
597 /* We didn't get a valid command name. */
598 unknown_command_error (lexer, words, word_cnt);
599 free_words (words, word_cnt);
603 /* Returns true if COMMAND is allowed in STATE,
606 in_correct_state (const struct command *command, enum cmd_state state)
608 return ((state == CMD_STATE_INITIAL && command->states & S_INITIAL)
609 || (state == CMD_STATE_DATA && command->states & S_DATA)
610 || (state == CMD_STATE_INPUT_PROGRAM
611 && command->states & S_INPUT_PROGRAM)
612 || (state == CMD_STATE_FILE_TYPE && command->states & S_FILE_TYPE));
615 /* Emits an appropriate error message for trying to invoke
618 report_state_mismatch (const struct command *command, enum cmd_state state)
620 assert (!in_correct_state (command, state));
621 if (state == CMD_STATE_INITIAL || state == CMD_STATE_DATA)
623 switch (command->states)
625 /* One allowed state. */
627 msg (SE, _("%s is allowed only before the active file has "
628 "been defined."), command->name);
631 msg (SE, _("%s is allowed only after the active file has "
632 "been defined."), command->name);
634 case S_INPUT_PROGRAM:
635 msg (SE, _("%s is allowed only inside INPUT PROGRAM."),
639 msg (SE, _("%s is allowed only inside FILE TYPE."), command->name);
642 /* Two allowed states. */
643 case S_INITIAL | S_DATA:
645 case S_INITIAL | S_INPUT_PROGRAM:
646 msg (SE, _("%s is allowed only before the active file has "
647 "been defined or inside INPUT PROGRAM."), command->name);
649 case S_INITIAL | S_FILE_TYPE:
650 msg (SE, _("%s is allowed only before the active file has "
651 "been defined or inside FILE TYPE."), command->name);
653 case S_DATA | S_INPUT_PROGRAM:
654 msg (SE, _("%s is allowed only after the active file has "
655 "been defined or inside INPUT PROGRAM."), command->name);
657 case S_DATA | S_FILE_TYPE:
658 msg (SE, _("%s is allowed only after the active file has "
659 "been defined or inside FILE TYPE."), command->name);
661 case S_INPUT_PROGRAM | S_FILE_TYPE:
662 msg (SE, _("%s is allowed only inside INPUT PROGRAM "
663 "or inside FILE TYPE."), command->name);
666 /* Three allowed states. */
667 case S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
668 msg (SE, _("%s is allowed only after the active file has "
669 "been defined, inside INPUT PROGRAM, or inside "
670 "FILE TYPE."), command->name);
672 case S_INITIAL | S_INPUT_PROGRAM | S_FILE_TYPE:
673 msg (SE, _("%s is allowed only before the active file has "
674 "been defined, inside INPUT PROGRAM, or inside "
675 "FILE TYPE."), command->name);
677 case S_INITIAL | S_DATA | S_FILE_TYPE:
679 case S_INITIAL | S_DATA | S_INPUT_PROGRAM:
682 /* Four allowed states. */
683 case S_INITIAL | S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
690 else if (state == CMD_STATE_INPUT_PROGRAM)
691 msg (SE, _("%s is not allowed inside %s."), command->name, "INPUT PROGRAM" );
692 else if (state == CMD_STATE_FILE_TYPE)
693 msg (SE, _("%s is not allowed inside %s."), command->name, "FILE TYPE");
698 /* Command name completion. */
700 static enum cmd_state completion_state = CMD_STATE_INITIAL;
703 set_completion_state (enum cmd_state state)
705 completion_state = state;
708 /* Returns the next possible completion of a command name that
709 begins with PREFIX, in the current command state, or a null
710 pointer if no completions remain.
711 Before calling the first time, set *CMD to a null pointer. */
713 cmd_complete (const char *prefix, const struct command **cmd)
718 for (; *cmd < commands + command_cnt; (*cmd)++)
719 if (!memcasecmp ((*cmd)->name, prefix, strlen (prefix))
720 && (!((*cmd)->flags & F_TESTING) || settings_get_testing_mode ())
721 && (!((*cmd)->flags & F_ENHANCED) || settings_get_syntax () == ENHANCED)
722 && !((*cmd)->flags & F_ABBREV)
723 && ((*cmd)->function != NULL)
724 && in_correct_state (*cmd, completion_state))
725 return (*cmd)++->name;
730 /* Simple commands. */
732 /* Parse and execute FINISH command. */
734 cmd_finish (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
739 /* Parses the N command. */
741 cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
746 if (!lex_force_int (lexer))
748 x = lex_integer (lexer);
750 if (!lex_match_id (lexer, "ESTIMATED"))
751 dict_set_case_limit (dataset_dict (ds), x);
753 return lex_end_of_command (lexer);
756 /* Parses, performs the EXECUTE procedure. */
758 cmd_execute (struct lexer *lexer, struct dataset *ds)
760 bool ok = casereader_destroy (proc_open (ds));
761 if (!proc_commit (ds) || !ok)
762 return CMD_CASCADING_FAILURE;
763 return lex_end_of_command (lexer);
766 /* Parses, performs the ERASE command. */
768 cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
770 if (settings_get_safer_mode ())
772 msg (SE, _("This command not allowed when the SAFER option is set."));
776 if (!lex_force_match_id (lexer, "FILE"))
778 lex_match (lexer, '=');
779 if (!lex_force_string (lexer))
782 if (remove (ds_cstr (lex_tokstr (lexer))) == -1)
784 msg (SW, _("Error removing `%s': %s."),
785 ds_cstr (lex_tokstr (lexer)), strerror (errno));
792 #if HAVE_FORK && HAVE_EXECL
793 /* Spawn an interactive shell process. */
804 const char *shell_fn;
810 for (i = 3; i < 20; i++)
814 shell_fn = getenv ("SHELL");
815 if (shell_fn == NULL)
816 shell_fn = "/bin/sh";
819 const char *cp = strrchr (shell_fn, '/');
820 cp = cp ? &cp[1] : shell_fn;
821 shell_process = xmalloca (strlen (cp) + 8);
822 strcpy (shell_process, "-");
823 strcat (shell_process, cp);
824 if (strcmp (cp, "sh"))
825 shell_process[0] = '+';
828 execl (shell_fn, shell_process, NULL);
834 msg (SE, _("Couldn't fork: %s."), strerror (errno));
839 while (wait (NULL) != pid)
844 #else /* !(HAVE_FORK && HAVE_EXECL) */
845 /* Don't know how to spawn an interactive shell. */
849 msg (SE, _("Interactive shell not supported on this platform."));
854 /* Executes the specified COMMAND in a subshell. Returns true if
855 successful, false otherwise. */
857 run_command (const char *command)
859 if (system (NULL) == 0)
861 msg (SE, _("Command shell not supported on this platform."));
865 /* Execute the command. */
866 if (system (command) == -1)
867 msg (SE, _("Error executing command: %s."), strerror (errno));
872 /* Parses, performs the HOST command. */
874 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
878 if (settings_get_safer_mode ())
880 msg (SE, _("This command not allowed when the SAFER option is set."));
884 look_ahead = lex_look_ahead (lexer);
885 if (look_ahead == '.')
888 return shell () ? CMD_SUCCESS : CMD_FAILURE;
890 else if (look_ahead == '\'' || look_ahead == '"')
895 if (!lex_force_string (lexer))
897 ok = run_command (ds_cstr (lex_tokstr (lexer)));
900 return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
904 bool ok = run_command (lex_rest_of_line (lexer));
905 lex_discard_line (lexer);
906 return ok ? CMD_SUCCESS : CMD_FAILURE;
910 /* Parses, performs the NEW FILE command. */
912 cmd_new_file (struct lexer *lexer, struct dataset *ds)
914 proc_discard_active_file (ds);
916 return lex_end_of_command (lexer);
919 /* Parses a comment. */
921 cmd_comment (struct lexer *lexer, struct dataset *ds UNUSED)
923 lex_skip_comment (lexer);