1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <language/command.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/settings.h>
32 #include <data/variable.h>
33 #include <language/lexer/lexer.h>
34 #include <language/prompt.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/assertion.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/message.h>
39 #include <libpspp/message.h>
40 #include <libpspp/str.h>
41 #include <output/manager.h>
42 #include <output/table.h>
49 #include <readline/readline.h>
53 #define _(msgid) gettext (msgid)
54 #define N_(msgid) msgid
56 /* Returns true if RESULT is a valid "enum cmd_result",
59 cmd_result_is_valid (enum cmd_result result)
61 return (result == CMD_SUCCESS || result == CMD_EOF || result == CMD_FINISH
62 || (result >= CMD_PRIVATE_FIRST && result <= CMD_PRIVATE_LAST)
63 || result == CMD_FAILURE || result == CMD_NOT_IMPLEMENTED
64 || result == CMD_CASCADING_FAILURE);
67 /* Returns true if RESULT indicates success,
70 cmd_result_is_success (enum cmd_result result)
72 assert (cmd_result_is_valid (result));
76 /* Returns true if RESULT indicates failure,
79 cmd_result_is_failure (enum cmd_result result)
81 assert (cmd_result_is_valid (result));
85 /* Command processing states. */
88 S_INITIAL = 0x01, /* Allowed before active file defined. */
89 S_DATA = 0x02, /* Allowed after active file defined. */
90 S_INPUT_PROGRAM = 0x04, /* Allowed in INPUT PROGRAM. */
91 S_FILE_TYPE = 0x08, /* Allowed in FILE TYPE. */
92 S_ANY = 0x0f /* Allowed anywhere. */
95 /* Other command requirements. */
98 F_ENHANCED = 0x10, /* Allowed only in enhanced syntax mode. */
99 F_TESTING = 0x20, /* Allowed only in testing mode. */
100 F_KEEP_FINAL_TOKEN = 0x40,/* Don't skip final token in command name. */
101 F_ABBREV = 0x80 /* Not a candidate for name completion. */
104 /* A single command. */
107 enum states states; /* States in which command is allowed. */
108 enum flags flags; /* Other command requirements. */
109 const char *name; /* Command name. */
110 int (*function) (struct lexer *, struct dataset *); /* Function to call. */
113 /* Define the command array. */
114 #define DEF_CMD(STATES, FLAGS, NAME, FUNCTION) {STATES, FLAGS, NAME, FUNCTION},
115 #define UNIMPL_CMD(NAME, DESCRIPTION) {S_ANY, 0, NAME, NULL},
116 static const struct command commands[] =
118 #include "command.def"
123 static const size_t command_cnt = sizeof commands / sizeof *commands;
125 static bool in_correct_state (const struct command *, enum cmd_state);
126 static bool report_state_mismatch (const struct command *, enum cmd_state);
127 static const struct command *find_command (const char *name);
128 static void set_completion_state (enum cmd_state);
130 /* Command parser. */
132 static const struct command *parse_command_name (struct lexer *lexer);
133 static enum cmd_result do_parse_command (struct lexer *, struct dataset *, enum cmd_state);
135 /* Parses an entire command, from command name to terminating
136 dot. On failure, skips to the terminating dot.
137 Returns the command's success or failure result. */
139 cmd_parse_in_state (struct lexer *lexer, struct dataset *ds,
140 enum cmd_state state)
146 result = do_parse_command (lexer, ds, state);
147 if (cmd_result_is_failure (result))
148 lex_discard_rest_of_command (lexer);
150 unset_cmd_algorithm ();
151 dict_clear_aux (dataset_dict (ds));
157 cmd_parse (struct lexer *lexer, struct dataset *ds)
159 const struct dictionary *dict = dataset_dict (ds);
160 return cmd_parse_in_state (lexer, ds,
161 proc_has_source (ds) &&
162 dict_get_var_cnt (dict) > 0 ?
163 CMD_STATE_DATA : CMD_STATE_INITIAL);
167 /* Parses an entire command, from command name to terminating
169 static enum cmd_result
170 do_parse_command (struct lexer *lexer, struct dataset *ds, enum cmd_state state)
172 const struct command *command;
173 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)
181 else if (lex_token (lexer) == '.')
183 /* Null commands can result from extra empty lines. */
186 prompt_set_style (PROMPT_LATER);
188 /* Parse the command name. */
189 command = parse_command_name (lexer);
192 else if (command->function == NULL)
194 msg (SE, _("%s is unimplemented."), command->name);
195 return CMD_NOT_IMPLEMENTED;
197 else if ((command->flags & F_TESTING) && !get_testing_mode ())
199 msg (SE, _("%s may be used only in testing mode."), command->name);
202 else if ((command->flags & F_ENHANCED) && get_syntax () != ENHANCED)
204 msg (SE, _("%s may be used only in enhanced syntax mode."),
208 else if (!in_correct_state (command, state))
210 report_state_mismatch (command, state);
214 /* Execute command. */
215 msg_set_command_name (command->name);
216 tab_set_command_name (command->name);
217 result = command->function (lexer, ds);
218 tab_set_command_name (NULL);
219 msg_set_command_name (NULL);
221 assert (cmd_result_is_valid (result));
226 match_strings (const char *a, size_t a_len,
227 const char *b, size_t b_len)
229 size_t match_len = 0;
231 while (a_len > 0 && b_len > 0)
233 /* Mismatch always returns zero. */
234 if (toupper ((unsigned char) *a++) != toupper ((unsigned char) *b++))
246 /* Returns the first character in the first word in STRING,
247 storing the word's length in *WORD_LEN. If no words remain,
248 returns a null pointer and stores 0 in *WORD_LEN. Words are
249 sequences of alphanumeric characters or single
250 non-alphanumeric characters. Words are delimited by
253 find_word (const char *string, size_t *word_len)
255 /* Skip whitespace and asterisks. */
256 while (isspace ((unsigned char) *string))
266 /* Special one-character word? */
267 if (!isalnum ((unsigned char) *string))
273 /* Alphanumeric word. */
275 while (isalnum ((unsigned char) string[*word_len]))
281 /* Returns true if strings A and B can be confused based on
282 their first three letters. */
284 conflicting_3char_prefixes (const char *a, const char *b)
286 size_t aw_len, bw_len;
289 aw = find_word (a, &aw_len);
290 bw = find_word (b, &bw_len);
291 assert (aw != NULL && bw != NULL);
293 /* Words that are the same don't conflict. */
294 if (aw_len == bw_len && !buf_compare_case (aw, bw, aw_len))
297 /* Words that are otherwise the same in the first three letters
299 return ((aw_len > 3 && bw_len > 3)
300 || (aw_len == 3 && bw_len > 3)
301 || (bw_len == 3 && aw_len > 3)) && !buf_compare_case (aw, bw, 3);
304 /* Returns true if CMD can be confused with another command
305 based on the first three letters of its first word. */
307 conflicting_3char_prefix_command (const struct command *cmd)
309 assert (cmd >= commands && cmd < commands + command_cnt);
311 return ((cmd > commands
312 && conflicting_3char_prefixes (cmd[-1].name, cmd[0].name))
313 || (cmd < commands + command_cnt
314 && conflicting_3char_prefixes (cmd[0].name, cmd[1].name)));
317 /* Ways that a set of words can match a command name. */
320 MISMATCH, /* Not a match. */
321 PARTIAL_MATCH, /* The words begin the command name. */
322 COMPLETE_MATCH /* The words are the command name. */
325 /* Figures out how well the WORD_CNT words in WORDS match CMD,
326 and returns the appropriate enum value. If WORDS are a
327 partial match for CMD and the next word in CMD is a dash, then
328 *DASH_POSSIBLE is set to 1 if DASH_POSSIBLE is non-null;
329 otherwise, *DASH_POSSIBLE is unchanged. */
330 static enum command_match
331 cmd_match_words (const struct command *cmd,
332 char *const words[], size_t word_cnt,
339 for (word = find_word (cmd->name, &word_len), word_idx = 0;
340 word != NULL && word_idx < word_cnt;
341 word = find_word (word + word_len, &word_len), word_idx++)
342 if (word_len != strlen (words[word_idx])
343 || buf_compare_case (word, words[word_idx], word_len))
345 size_t match_chars = match_strings (word, word_len,
347 strlen (words[word_idx]));
348 if (match_chars == 0)
353 else if (match_chars == 1 || match_chars == 2)
355 /* One- and two-character abbreviations are not
359 else if (match_chars == 3)
361 /* Three-character abbreviations are acceptable
362 in the first word of a command if there are
363 no name conflicts. They are always
364 acceptable after the first word. */
365 if (word_idx == 0 && conflicting_3char_prefix_command (cmd))
368 else /* match_chars > 3 */
370 /* Four-character and longer abbreviations are
371 always acceptable. */
375 if (word == NULL && word_idx == word_cnt)
377 /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR"}. */
378 return COMPLETE_MATCH;
380 else if (word == NULL)
382 /* cmd->name = "FOO BAR", words[] = {"FOO", "BAR", "BAZ"}. */
387 /* cmd->name = "FOO BAR BAZ", words[] = {"FOO", "BAR"}. */
388 if (word[0] == '-' && dash_possible != NULL)
390 return PARTIAL_MATCH;
394 /* Returns the number of commands for which the WORD_CNT words in
395 WORDS are a partial or complete match. If some partial match
396 has a dash as the next word, then *DASH_POSSIBLE is set to 1,
397 otherwise it is set to 0. */
399 count_matching_commands (char *const words[], size_t word_cnt,
402 const struct command *cmd;
407 for (cmd = commands; cmd < commands + command_cnt; cmd++)
408 if (cmd_match_words (cmd, words, word_cnt, dash_possible) != MISMATCH)
411 return cmd_match_count;
414 /* Returns the command for which the WORD_CNT words in WORDS are
415 a complete match. Returns a null pointer if no such command
417 static const struct command *
418 get_complete_match (char *const words[], size_t word_cnt)
420 const struct command *cmd;
422 for (cmd = commands; cmd < commands + command_cnt; cmd++)
423 if (cmd_match_words (cmd, words, word_cnt, NULL) == COMPLETE_MATCH)
429 /* Returns the command with the given exact NAME.
430 Aborts if no such command exists. */
431 static const struct command *
432 find_command (const char *name)
434 const struct command *cmd;
436 for (cmd = commands; cmd < commands + command_cnt; cmd++)
437 if (!strcmp (cmd->name, name))
442 /* Frees the WORD_CNT words in WORDS. */
444 free_words (char *words[], size_t word_cnt)
448 for (idx = 0; idx < word_cnt; idx++)
452 /* Flags an error that the command whose name is given by the
453 WORD_CNT words in WORDS is unknown. */
455 unknown_command_error (struct lexer *lexer, char *const words[], size_t word_cnt)
458 lex_error (lexer, _("expecting command name"));
465 for (i = 0; i < word_cnt; i++)
468 ds_put_char (&s, ' ');
469 ds_put_cstr (&s, words[i]);
472 msg (SE, _("Unknown command %s."), ds_cstr (&s));
478 /* Parse the command name and return a pointer to the corresponding
479 struct command if successful.
480 If not successful, return a null pointer. */
481 static const struct command *
482 parse_command_name (struct lexer *lexer)
486 int complete_word_cnt;
489 if (lex_token (lexer) == T_EXP ||
490 lex_token (lexer) == '*' || lex_token (lexer) == '[')
491 return find_command ("COMMENT");
494 word_cnt = complete_word_cnt = 0;
495 while (lex_token (lexer) == T_ID || (dash_possible && lex_token (lexer) == '-'))
499 assert (word_cnt < sizeof words / sizeof *words);
500 if (lex_token (lexer) == T_ID)
502 words[word_cnt] = ds_xstrdup (lex_tokstr (lexer));
503 str_uppercase (words[word_cnt]);
505 else if (lex_token (lexer) == '-')
506 words[word_cnt] = xstrdup ("-");
509 cmd_match_cnt = count_matching_commands (words, word_cnt,
511 if (cmd_match_cnt == 0)
513 else if (cmd_match_cnt == 1)
515 const struct command *command = get_complete_match (words, word_cnt);
518 if (!(command->flags & F_KEEP_FINAL_TOKEN))
520 free_words (words, word_cnt);
524 else /* cmd_match_cnt > 1 */
526 /* Do we have a complete command name so far? */
527 if (get_complete_match (words, word_cnt) != NULL)
528 complete_word_cnt = word_cnt;
533 /* If we saw a complete command name earlier, drop back to
535 if (complete_word_cnt)
537 int pushback_word_cnt;
538 const struct command *command;
540 /* Get the command. */
541 command = get_complete_match (words, complete_word_cnt);
542 assert (command != NULL);
544 /* Figure out how many words we want to keep.
545 We normally want to swallow the entire command. */
546 pushback_word_cnt = complete_word_cnt + 1;
547 if (command->flags & F_KEEP_FINAL_TOKEN)
550 /* FIXME: We only support one-token pushback. */
551 assert (pushback_word_cnt + 1 >= word_cnt);
553 while (word_cnt > pushback_word_cnt)
556 if (strcmp (words[word_cnt], "-"))
557 lex_put_back_id (lexer, words[word_cnt]);
559 lex_put_back (lexer, '-');
560 free (words[word_cnt]);
563 free_words (words, word_cnt);
567 /* We didn't get a valid command name. */
568 unknown_command_error (lexer, words, word_cnt);
569 free_words (words, word_cnt);
573 /* Returns true if COMMAND is allowed in STATE,
576 in_correct_state (const struct command *command, enum cmd_state state)
578 return ((state == CMD_STATE_INITIAL && command->states & S_INITIAL)
579 || (state == CMD_STATE_DATA && command->states & S_DATA)
580 || (state == CMD_STATE_INPUT_PROGRAM
581 && command->states & S_INPUT_PROGRAM)
582 || (state == CMD_STATE_FILE_TYPE && command->states & S_FILE_TYPE));
585 /* Emits an appropriate error message for trying to invoke
588 report_state_mismatch (const struct command *command, enum cmd_state state)
590 assert (!in_correct_state (command, state));
591 if (state == CMD_STATE_INITIAL || state == CMD_STATE_DATA)
593 const char *allowed[3];
598 if (command->states & S_INITIAL)
599 allowed[allowed_cnt++] = _("before the active file has been defined");
600 else if (command->states & S_DATA)
601 allowed[allowed_cnt++] = _("after the active file has been defined");
602 if (command->states & S_INPUT_PROGRAM)
603 allowed[allowed_cnt++] = _("inside INPUT PROGRAM");
604 if (command->states & S_FILE_TYPE)
605 allowed[allowed_cnt++] = _("inside FILE TYPE");
607 if (allowed_cnt == 1)
608 s = xstrdup (allowed[0]);
609 else if (allowed_cnt == 2)
610 s = xasprintf (_("%s or %s"), allowed[0], allowed[1]);
611 else if (allowed_cnt == 3)
612 s = xasprintf (_("%s, %s, or %s"), allowed[0], allowed[1], allowed[2]);
616 msg (SE, _("%s is allowed only %s."), command->name, s);
620 else if (state == CMD_STATE_INPUT_PROGRAM)
621 msg (SE, _("%s is not allowed inside INPUT PROGRAM."), command->name);
622 else if (state == CMD_STATE_FILE_TYPE)
623 msg (SE, _("%s is not allowed inside FILE TYPE."), command->name);
628 /* Command name completion. */
630 static enum cmd_state completion_state = CMD_STATE_INITIAL;
633 set_completion_state (enum cmd_state state)
635 completion_state = state;
638 /* Returns the next possible completion of a command name that
639 begins with PREFIX, in the current command state, or a null
640 pointer if no completions remain.
641 Before calling the first time, set *CMD to a null pointer. */
643 cmd_complete (const char *prefix, const struct command **cmd)
648 for (; *cmd < commands + command_cnt; (*cmd)++)
649 if (!memcasecmp ((*cmd)->name, prefix, strlen (prefix))
650 && (!((*cmd)->flags & F_TESTING) || get_testing_mode ())
651 && (!((*cmd)->flags & F_ENHANCED) || get_syntax () == ENHANCED)
652 && !((*cmd)->flags & F_ABBREV)
653 && ((*cmd)->function != NULL)
654 && in_correct_state (*cmd, completion_state))
655 return (*cmd)++->name;
660 /* Simple commands. */
662 /* Parse and execute FINISH command. */
664 cmd_finish (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
669 /* Parses the N command. */
671 cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
676 if (!lex_force_int (lexer))
678 x = lex_integer (lexer);
680 if (!lex_match_id (lexer, "ESTIMATED"))
681 dict_set_case_limit (dataset_dict (ds), x);
683 return lex_end_of_command (lexer);
686 /* Parses, performs the EXECUTE procedure. */
688 cmd_execute (struct lexer *lexer, struct dataset *ds)
690 if (!procedure (ds, NULL, NULL))
691 return CMD_CASCADING_FAILURE;
692 return lex_end_of_command (lexer);
695 /* Parses, performs the ERASE command. */
697 cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
699 if (get_safer_mode ())
701 msg (SE, _("This command not allowed when the SAFER option is set."));
705 if (!lex_force_match_id (lexer, "FILE"))
707 lex_match (lexer, '=');
708 if (!lex_force_string (lexer))
711 if (remove (ds_cstr (lex_tokstr (lexer))) == -1)
713 msg (SW, _("Error removing `%s': %s."),
714 ds_cstr (lex_tokstr (lexer)), strerror (errno));
722 /* Spawn a shell process. */
733 const char *shell_fn;
739 for (i = 3; i < 20; i++)
743 shell_fn = getenv ("SHELL");
744 if (shell_fn == NULL)
745 shell_fn = "/bin/sh";
748 const char *cp = strrchr (shell_fn, '/');
749 cp = cp ? &cp[1] : shell_fn;
750 shell_process = local_alloc (strlen (cp) + 8);
751 strcpy (shell_process, "-");
752 strcat (shell_process, cp);
753 if (strcmp (cp, "sh"))
754 shell_process[0] = '+';
757 execl (shell_fn, shell_process, NULL);
763 msg (SE, _("Couldn't fork: %s."), strerror (errno));
768 while (wait (NULL) != pid)
775 /* Parses the HOST command argument and executes the specified
776 command. Returns a suitable command return code. */
778 run_command (struct lexer *lexer)
783 /* Handle either a string argument or a full-line argument. */
785 int c = lex_look_ahead (lexer);
787 if (c == '\'' || c == '"')
790 if (!lex_force_string (lexer))
792 cmd = ds_cstr (lex_tokstr (lexer));
797 cmd = lex_rest_of_line (lexer, NULL);
798 lex_discard_line (lexer);
803 /* Execute the command. */
804 if (system (cmd) == -1)
805 msg (SE, _("Error executing command: %s."), strerror (errno));
807 /* Finish parsing. */
812 if (lex_token (lexer) != '.')
814 lex_error (lexer, _("expecting end of command"));
822 /* Parses, performs the HOST command. */
824 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
828 if (get_safer_mode ())
830 msg (SE, _("This command not allowed when the SAFER option is set."));
835 /* Figure out whether to invoke an interactive shell or to execute a
836 single shell command. */
837 if (lex_look_ahead (lexer) == '.')
840 code = shell () ? CMD_FAILURE : CMD_SUCCESS;
843 code = run_command (lexer);
845 /* Make sure that the system has a command interpreter, then run a
847 if (system (NULL) != 0)
848 code = run_command (lexer);
851 msg (SE, _("No operating system support for this command."));
859 /* Parses, performs the NEW FILE command. */
861 cmd_new_file (struct lexer *lexer, struct dataset *ds)
863 discard_variables (ds);
865 return lex_end_of_command (lexer);
868 /* Parses a comment. */
870 cmd_comment (struct lexer *lexer, struct dataset *ds UNUSED)
872 lex_skip_comment (lexer);