1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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"
26 #include "data/casereader.h"
27 #include "data/dataset.h"
28 #include "data/dictionary.h"
29 #include "data/settings.h"
30 #include "data/variable.h"
31 #include "language/lexer/command-name.h"
32 #include "language/lexer/lexer.h"
33 #include "libpspp/assertion.h"
34 #include "libpspp/compiler.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/message.h"
37 #include "libpspp/str.h"
38 #include "output/text-item.h"
44 #define _(msgid) gettext (msgid)
45 #define N_(msgid) msgid
47 /* Returns true if RESULT is a valid "enum cmd_result",
50 cmd_result_is_valid (enum cmd_result result)
52 return (result == CMD_SUCCESS || result == CMD_EOF || result == CMD_FINISH
53 || (result >= CMD_PRIVATE_FIRST && result <= CMD_PRIVATE_LAST)
54 || result == CMD_FAILURE || result == CMD_NOT_IMPLEMENTED
55 || result == CMD_CASCADING_FAILURE);
58 /* Returns true if RESULT indicates success,
61 cmd_result_is_success (enum cmd_result result)
63 assert (cmd_result_is_valid (result));
67 /* Returns true if RESULT indicates failure,
70 cmd_result_is_failure (enum cmd_result result)
72 assert (cmd_result_is_valid (result));
76 /* Command processing states. */
79 S_INITIAL = 0x01, /* Allowed before active dataset defined. */
80 S_DATA = 0x02, /* Allowed after active dataset defined. */
81 S_INPUT_PROGRAM = 0x04, /* Allowed in INPUT PROGRAM. */
82 S_FILE_TYPE = 0x08, /* Allowed in FILE TYPE. */
83 S_ANY = 0x0f /* Allowed anywhere. */
86 /* Other command requirements. */
89 F_ENHANCED = 0x10, /* Allowed only in enhanced syntax mode. */
90 F_TESTING = 0x20, /* Allowed only in testing mode. */
91 F_ABBREV = 0x80 /* Not a candidate for name completion. */
94 /* A single command. */
97 enum states states; /* States in which command is allowed. */
98 enum flags flags; /* Other command requirements. */
99 const char *name; /* Command name. */
100 int (*function) (struct lexer *, struct dataset *); /* Function to call. */
103 /* Define the command array. */
104 #define DEF_CMD(STATES, FLAGS, NAME, FUNCTION) {STATES, FLAGS, NAME, FUNCTION},
105 #define UNIMPL_CMD(NAME, DESCRIPTION) {S_ANY, 0, NAME, NULL},
106 static const struct command commands[] =
108 #include "command.def"
113 static const size_t command_cnt = sizeof commands / sizeof *commands;
115 static bool in_correct_state (const struct command *, enum cmd_state);
116 static bool report_state_mismatch (const struct command *, enum cmd_state);
117 static void set_completion_state (enum cmd_state);
119 /* Command parser. */
121 static const struct command *parse_command_name (struct lexer *,
123 static enum cmd_result do_parse_command (struct lexer *, struct dataset *, enum cmd_state);
125 /* Parses an entire command, from command name to terminating
126 dot. On failure, skips to the terminating dot.
127 Returns the command's success or failure result. */
129 cmd_parse_in_state (struct lexer *lexer, struct dataset *ds,
130 enum cmd_state state)
134 result = do_parse_command (lexer, ds, state);
136 assert (!proc_is_open (ds));
137 unset_cmd_algorithm ();
138 dict_clear_aux (dataset_dict (ds));
139 if (!dataset_end_of_command (ds))
140 result = CMD_CASCADING_FAILURE;
146 cmd_parse (struct lexer *lexer, struct dataset *ds)
148 const struct dictionary *dict = dataset_dict (ds);
149 return cmd_parse_in_state (lexer, ds,
150 dataset_has_source (ds) &&
151 dict_get_var_cnt (dict) > 0 ?
152 CMD_STATE_DATA : CMD_STATE_INITIAL);
156 /* Parses an entire command, from command name to terminating
158 static enum cmd_result
159 do_parse_command (struct lexer *lexer,
160 struct dataset *ds, enum cmd_state state)
162 const struct command *command = NULL;
163 enum cmd_result result;
167 /* Read the command's first token. */
168 set_completion_state (state);
169 if (lex_token (lexer) == T_STOP)
174 else if (lex_token (lexer) == T_ENDCMD)
176 /* Null commands can result from extra empty lines. */
177 result = CMD_SUCCESS;
181 /* Parse the command name. */
182 command = parse_command_name (lexer, &n_tokens);
185 result = CMD_FAILURE;
188 text_item_submit (text_item_create (TEXT_ITEM_COMMAND_OPEN, command->name));
191 if (command->function == NULL)
193 msg (SE, _("%s is not yet implemented."), command->name);
194 result = CMD_NOT_IMPLEMENTED;
196 else if ((command->flags & F_TESTING) && !settings_get_testing_mode ())
198 msg (SE, _("%s may be used only in testing mode."), command->name);
199 result = CMD_FAILURE;
201 else if ((command->flags & F_ENHANCED) && settings_get_syntax () != ENHANCED)
203 msg (SE, _("%s may be used only in enhanced syntax mode."),
205 result = CMD_FAILURE;
207 else if (!in_correct_state (command, state))
209 report_state_mismatch (command, state);
210 result = CMD_FAILURE;
214 /* Execute command. */
217 for (i = 0; i < n_tokens; i++)
219 result = command->function (lexer, ds);
222 assert (cmd_result_is_valid (result));
225 if (cmd_result_is_failure (result))
226 lex_interactive_reset (lexer);
227 else if (result == CMD_SUCCESS)
228 result = lex_end_of_command (lexer);
230 lex_discard_rest_of_command (lexer);
231 while (lex_token (lexer) == T_ENDCMD)
235 text_item_submit (text_item_create (TEXT_ITEM_COMMAND_CLOSE,
242 find_best_match (struct substring s, const struct command **matchp)
244 const struct command *cmd;
245 struct command_matcher cm;
248 command_matcher_init (&cm, s);
249 for (cmd = commands; cmd < &commands[command_cnt]; cmd++)
250 command_matcher_add (&cm, ss_cstr (cmd->name), CONST_CAST (void *, cmd));
252 *matchp = command_matcher_get_match (&cm);
253 missing_words = command_matcher_get_missing_words (&cm);
255 command_matcher_destroy (&cm);
257 return missing_words;
261 parse_command_word (struct lexer *lexer, struct string *s, int n)
263 bool need_space = ds_last (s) != EOF && ds_last (s) != '-';
265 switch (lex_next_token (lexer, n))
268 ds_put_byte (s, '-');
273 ds_put_byte (s, ' ');
274 ds_put_cstr (s, lex_next_tokcstr (lexer, n));
278 if (lex_next_is_integer (lexer, n))
280 int integer = lex_next_integer (lexer, n);
284 ds_put_byte (s, ' ');
285 ds_put_format (s, "%ld", lex_next_integer (lexer, n));
296 /* Parses the command name. On success returns a pointer to the corresponding
297 struct command and stores the number of tokens in the command name into
298 *N_TOKENS. On failure, returns a null pointer and stores the number of
299 tokens required to determine that no command name was present into
301 static const struct command *
302 parse_command_name (struct lexer *lexer, int *n_tokens)
304 const struct command *command;
313 while (parse_command_word (lexer, &s, word))
315 missing_words = find_best_match (ds_ss (&s), &command);
316 if (missing_words <= 0)
321 if (command == NULL && missing_words > 0)
323 ds_put_cstr (&s, " .");
324 missing_words = find_best_match (ds_ss (&s), &command);
325 ds_truncate (&s, ds_length (&s) - 2);
330 if (ds_is_empty (&s))
331 lex_error (lexer, _("expecting command name"));
333 msg (SE, _("Unknown command `%s'."), ds_cstr (&s));
338 *n_tokens = (word + 1) + missing_words;
342 /* Returns true if COMMAND is allowed in STATE,
345 in_correct_state (const struct command *command, enum cmd_state state)
347 return ((state == CMD_STATE_INITIAL && command->states & S_INITIAL)
348 || (state == CMD_STATE_DATA && command->states & S_DATA)
349 || (state == CMD_STATE_INPUT_PROGRAM
350 && command->states & S_INPUT_PROGRAM)
351 || (state == CMD_STATE_FILE_TYPE && command->states & S_FILE_TYPE));
354 /* Emits an appropriate error message for trying to invoke
357 report_state_mismatch (const struct command *command, enum cmd_state state)
359 assert (!in_correct_state (command, state));
360 if (state == CMD_STATE_INITIAL || state == CMD_STATE_DATA)
362 switch (command->states)
364 /* One allowed state. */
366 msg (SE, _("%s is allowed only before the active dataset has "
367 "been defined."), command->name);
370 msg (SE, _("%s is allowed only after the active dataset has "
371 "been defined."), command->name);
373 case S_INPUT_PROGRAM:
374 msg (SE, _("%s is allowed only inside INPUT PROGRAM."),
378 msg (SE, _("%s is allowed only inside FILE TYPE."), command->name);
381 /* Two allowed states. */
382 case S_INITIAL | S_DATA:
384 case S_INITIAL | S_INPUT_PROGRAM:
385 msg (SE, _("%s is allowed only before the active dataset has "
386 "been defined or inside INPUT PROGRAM."), command->name);
388 case S_INITIAL | S_FILE_TYPE:
389 msg (SE, _("%s is allowed only before the active dataset has "
390 "been defined or inside FILE TYPE."), command->name);
392 case S_DATA | S_INPUT_PROGRAM:
393 msg (SE, _("%s is allowed only after the active dataset has "
394 "been defined or inside INPUT PROGRAM."), command->name);
396 case S_DATA | S_FILE_TYPE:
397 msg (SE, _("%s is allowed only after the active dataset has "
398 "been defined or inside FILE TYPE."), command->name);
400 case S_INPUT_PROGRAM | S_FILE_TYPE:
401 msg (SE, _("%s is allowed only inside INPUT PROGRAM "
402 "or inside FILE TYPE."), command->name);
405 /* Three allowed states. */
406 case S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
407 msg (SE, _("%s is allowed only after the active dataset has "
408 "been defined, inside INPUT PROGRAM, or inside "
409 "FILE TYPE."), command->name);
411 case S_INITIAL | S_INPUT_PROGRAM | S_FILE_TYPE:
412 msg (SE, _("%s is allowed only before the active dataset has "
413 "been defined, inside INPUT PROGRAM, or inside "
414 "FILE TYPE."), command->name);
416 case S_INITIAL | S_DATA | S_FILE_TYPE:
418 case S_INITIAL | S_DATA | S_INPUT_PROGRAM:
421 /* Four allowed states. */
422 case S_INITIAL | S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
429 else if (state == CMD_STATE_INPUT_PROGRAM)
430 msg (SE, _("%s is not allowed inside %s."),
431 command->name, "INPUT PROGRAM" );
432 else if (state == CMD_STATE_FILE_TYPE)
433 msg (SE, _("%s is not allowed inside %s."), command->name, "FILE TYPE");
438 /* Command name completion. */
440 static enum cmd_state completion_state = CMD_STATE_INITIAL;
443 set_completion_state (enum cmd_state state)
445 completion_state = state;
448 /* Returns the next possible completion of a command name that
449 begins with PREFIX, in the current command state, or a null
450 pointer if no completions remain.
451 Before calling the first time, set *CMD to a null pointer. */
453 cmd_complete (const char *prefix, const struct command **cmd)
458 for (; *cmd < commands + command_cnt; (*cmd)++)
459 if (!memcasecmp ((*cmd)->name, prefix, strlen (prefix))
460 && (!((*cmd)->flags & F_TESTING) || settings_get_testing_mode ())
461 && (!((*cmd)->flags & F_ENHANCED) || settings_get_syntax () == ENHANCED)
462 && !((*cmd)->flags & F_ABBREV)
463 && ((*cmd)->function != NULL)
464 && in_correct_state (*cmd, completion_state))
465 return (*cmd)++->name;
470 /* Simple commands. */
472 /* Parse and execute FINISH command. */
474 cmd_finish (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
479 /* Parses the N command. */
481 cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
486 if (!lex_force_int (lexer))
488 x = lex_integer (lexer);
490 if (!lex_match_id (lexer, "ESTIMATED"))
491 dict_set_case_limit (dataset_dict (ds), x);
496 /* Parses, performs the EXECUTE procedure. */
498 cmd_execute (struct lexer *lexer UNUSED, struct dataset *ds)
500 bool ok = casereader_destroy (proc_open (ds));
501 if (!proc_commit (ds) || !ok)
502 return CMD_CASCADING_FAILURE;
506 /* Parses, performs the ERASE command. */
508 cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
513 if (settings_get_safer_mode ())
515 msg (SE, _("This command not allowed when the SAFER option is set."));
519 if (!lex_force_match_id (lexer, "FILE"))
521 lex_match (lexer, T_EQUALS);
522 if (!lex_force_string (lexer))
525 filename = utf8_to_filename (lex_tokcstr (lexer));
526 retval = remove (filename);
531 msg (SW, _("Error removing `%s': %s."),
532 lex_tokcstr (lexer), strerror (errno));
540 /* Parses, performs the NEW FILE command. */
542 cmd_new_file (struct lexer *lexer UNUSED, struct dataset *ds)