1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013, 2014 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/session.h"
30 #include "data/settings.h"
31 #include "data/variable.h"
32 #include "language/lexer/command-name.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/compiler.h"
36 #include "libpspp/i18n.h"
37 #include "libpspp/message.h"
38 #include "libpspp/str.h"
39 #include "output/group-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)
61 case CMD_NOT_IMPLEMENTED:
62 case CMD_CASCADING_FAILURE:
70 /* Returns true if RESULT indicates success,
73 cmd_result_is_success (enum cmd_result result)
75 assert (cmd_result_is_valid (result));
79 /* Returns true if RESULT indicates failure,
82 cmd_result_is_failure (enum cmd_result result)
84 assert (cmd_result_is_valid (result));
88 /* Command processing states. */
91 S_INITIAL = 0x01, /* Allowed before active dataset defined. */
92 S_DATA = 0x02, /* Allowed after active dataset defined. */
93 S_INPUT_PROGRAM = 0x04, /* Allowed in INPUT PROGRAM. */
94 S_FILE_TYPE = 0x08, /* Allowed in FILE TYPE. */
95 S_ANY = 0x0f /* Allowed anywhere. */
98 /* Other command requirements. */
101 F_ENHANCED = 0x10, /* Allowed only in enhanced syntax mode. */
102 F_TESTING = 0x20, /* Allowed only in testing mode. */
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 void set_completion_state (enum cmd_state);
131 /* Command parser. */
133 static const struct command *parse_command_name (struct 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)
144 struct session *session = dataset_session (ds);
147 result = do_parse_command (lexer, ds, state);
149 ds = session_active_dataset (session);
150 assert (!proc_is_open (ds));
151 unset_cmd_algorithm ();
152 if (!dataset_end_of_command (ds))
153 result = CMD_CASCADING_FAILURE;
159 cmd_parse (struct lexer *lexer, struct dataset *ds)
161 const struct dictionary *dict = dataset_dict (ds);
162 return cmd_parse_in_state (lexer, ds,
163 dataset_has_source (ds) &&
164 dict_get_var_cnt (dict) > 0 ?
165 CMD_STATE_DATA : CMD_STATE_INITIAL);
169 /* Parses an entire command, from command name to terminating
171 static enum cmd_result
172 do_parse_command (struct lexer *lexer,
173 struct dataset *ds, enum cmd_state state)
175 const struct command *command = NULL;
176 enum cmd_result result;
180 /* Read the command's first token. */
181 set_completion_state (state);
182 if (lex_token (lexer) == T_STOP)
187 else if (lex_token (lexer) == T_ENDCMD)
189 /* Null commands can result from extra empty lines. */
190 result = CMD_SUCCESS;
194 /* Parse the command name. */
195 command = parse_command_name (lexer, &n_tokens);
198 result = CMD_FAILURE;
201 group_open_item_submit (group_open_item_create_nocopy (
202 utf8_to_title (command->name)));
205 if (command->function == NULL)
207 msg (SE, _("%s is not yet implemented."), command->name);
208 result = CMD_NOT_IMPLEMENTED;
210 else if ((command->flags & F_TESTING) && !settings_get_testing_mode ())
212 msg (SE, _("%s may be used only in testing mode."), command->name);
213 result = CMD_FAILURE;
215 else if ((command->flags & F_ENHANCED) && settings_get_syntax () != ENHANCED)
217 msg (SE, _("%s may be used only in enhanced syntax mode."),
219 result = CMD_FAILURE;
221 else if (!in_correct_state (command, state))
223 report_state_mismatch (command, state);
224 result = CMD_FAILURE;
228 /* Execute command. */
231 for (i = 0; i < n_tokens; i++)
233 result = command->function (lexer, ds);
236 assert (cmd_result_is_valid (result));
239 if (cmd_result_is_failure (result))
240 lex_interactive_reset (lexer);
241 else if (result == CMD_SUCCESS)
242 result = lex_end_of_command (lexer);
244 lex_discard_rest_of_command (lexer);
245 if (result != CMD_EOF && result != CMD_FINISH)
246 while (lex_token (lexer) == T_ENDCMD)
250 group_close_item_submit (group_close_item_create ());
256 find_best_match (struct substring s, const struct command **matchp)
258 const struct command *cmd;
259 struct command_matcher cm;
262 command_matcher_init (&cm, s);
263 for (cmd = commands; cmd < &commands[command_cnt]; cmd++)
264 command_matcher_add (&cm, ss_cstr (cmd->name), CONST_CAST (void *, cmd));
266 *matchp = command_matcher_get_match (&cm);
267 missing_words = command_matcher_get_missing_words (&cm);
269 command_matcher_destroy (&cm);
271 return missing_words;
275 parse_command_word (struct lexer *lexer, struct string *s, int n)
277 bool need_space = ds_last (s) != EOF && ds_last (s) != '-';
279 switch (lex_next_token (lexer, n))
282 ds_put_byte (s, '-');
287 ds_put_byte (s, ' ');
288 ds_put_cstr (s, lex_next_tokcstr (lexer, n));
292 if (lex_next_is_integer (lexer, n))
294 int integer = lex_next_integer (lexer, n);
298 ds_put_byte (s, ' ');
299 ds_put_format (s, "%ld", lex_next_integer (lexer, n));
310 /* Parses the command name. On success returns a pointer to the corresponding
311 struct command and stores the number of tokens in the command name into
312 *N_TOKENS. On failure, returns a null pointer and stores the number of
313 tokens required to determine that no command name was present into
315 static const struct command *
316 parse_command_name (struct lexer *lexer, int *n_tokens)
318 const struct command *command;
327 while (parse_command_word (lexer, &s, word))
329 missing_words = find_best_match (ds_ss (&s), &command);
330 if (missing_words <= 0)
335 if (command == NULL && missing_words > 0)
337 ds_put_cstr (&s, " .");
338 missing_words = find_best_match (ds_ss (&s), &command);
339 ds_truncate (&s, ds_length (&s) - 2);
344 if (ds_is_empty (&s))
345 lex_error (lexer, _("expecting command name"));
347 msg (SE, _("Unknown command `%s'."), ds_cstr (&s));
352 *n_tokens = (word + 1) + missing_words;
356 /* Returns true if COMMAND is allowed in STATE,
359 in_correct_state (const struct command *command, enum cmd_state state)
361 return ((state == CMD_STATE_INITIAL && command->states & S_INITIAL)
362 || (state == CMD_STATE_DATA && command->states & S_DATA)
363 || (state == CMD_STATE_INPUT_PROGRAM
364 && command->states & S_INPUT_PROGRAM)
365 || (state == CMD_STATE_FILE_TYPE && command->states & S_FILE_TYPE));
368 /* Emits an appropriate error message for trying to invoke
371 report_state_mismatch (const struct command *command, enum cmd_state state)
373 assert (!in_correct_state (command, state));
374 if (state == CMD_STATE_INITIAL || state == CMD_STATE_DATA)
376 switch ((int) command->states)
378 /* One allowed state. */
380 msg (SE, _("%s is allowed only before the active dataset has "
381 "been defined."), command->name);
384 msg (SE, _("%s is allowed only after the active dataset has "
385 "been defined."), command->name);
387 case S_INPUT_PROGRAM:
388 msg (SE, _("%s is allowed only inside %s."),
389 command->name, "INPUT PROGRAM");
392 msg (SE, _("%s is allowed only inside %s."), command->name, "FILE TYPE");
395 /* Two allowed states. */
396 case S_INITIAL | S_DATA:
398 case S_INITIAL | S_INPUT_PROGRAM:
399 msg (SE, _("%s is allowed only before the active dataset has been defined or inside %s."),
400 command->name, "INPUT PROGRAM");
402 case S_INITIAL | S_FILE_TYPE:
403 msg (SE, _("%s is allowed only before the active dataset has been defined or inside %s."),
404 command->name, "FILE TYPE");
406 case S_DATA | S_INPUT_PROGRAM:
407 msg (SE, _("%s is allowed only after the active dataset has been defined or inside %s."),
408 command->name, "INPUT PROGRAM");
410 case S_DATA | S_FILE_TYPE:
411 msg (SE, _("%s is allowed only after the active dataset has been defined or inside %s."),
412 command->name, "FILE TYPE");
414 case S_INPUT_PROGRAM | S_FILE_TYPE:
415 msg (SE, _("%s is allowed only inside %s or inside %s."), command->name,
416 "INPUT PROGRAM", "FILE TYPE");
419 /* Three allowed states. */
420 case S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
421 msg (SE, _("%s is allowed only after the active dataset has "
422 "been defined, inside INPUT PROGRAM, or inside "
423 "FILE TYPE."), command->name);
425 case S_INITIAL | S_INPUT_PROGRAM | S_FILE_TYPE:
426 msg (SE, _("%s is allowed only before the active dataset has "
427 "been defined, inside INPUT PROGRAM, or inside "
428 "FILE TYPE."), command->name);
430 case S_INITIAL | S_DATA | S_FILE_TYPE:
432 case S_INITIAL | S_DATA | S_INPUT_PROGRAM:
435 /* Four allowed states. */
436 case S_INITIAL | S_DATA | S_INPUT_PROGRAM | S_FILE_TYPE:
443 else if (state == CMD_STATE_INPUT_PROGRAM)
444 msg (SE, _("%s is not allowed inside %s."),
445 command->name, "INPUT PROGRAM");
446 else if (state == CMD_STATE_FILE_TYPE)
447 msg (SE, _("%s is not allowed inside %s."), command->name, "FILE TYPE");
452 /* Command name completion. */
454 static enum cmd_state completion_state = CMD_STATE_INITIAL;
457 set_completion_state (enum cmd_state state)
459 completion_state = state;
462 /* Returns the next possible completion of a command name that
463 begins with PREFIX, in the current command state, or a null
464 pointer if no completions remain.
465 Before calling the first time, set *CMD to a null pointer. */
467 cmd_complete (const char *prefix, const struct command **cmd)
472 for (; *cmd < commands + command_cnt; (*cmd)++)
473 if (!memcasecmp ((*cmd)->name, prefix, strlen (prefix))
474 && (!((*cmd)->flags & F_TESTING) || settings_get_testing_mode ())
475 && (!((*cmd)->flags & F_ENHANCED) || settings_get_syntax () == ENHANCED)
476 && !((*cmd)->flags & F_ABBREV)
477 && ((*cmd)->function != NULL)
478 && in_correct_state (*cmd, completion_state))
479 return (*cmd)++->name;
484 /* Simple commands. */
486 /* Parse and execute FINISH command. */
488 cmd_finish (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
493 /* Parses the N command. */
495 cmd_n_of_cases (struct lexer *lexer, struct dataset *ds)
500 if (!lex_force_int (lexer))
502 x = lex_integer (lexer);
504 if (!lex_match_id (lexer, "ESTIMATED"))
505 dict_set_case_limit (dataset_dict (ds), x);
510 /* Parses, performs the EXECUTE procedure. */
512 cmd_execute (struct lexer *lexer UNUSED, struct dataset *ds)
514 bool ok = casereader_destroy (proc_open (ds));
515 if (!proc_commit (ds) || !ok)
516 return CMD_CASCADING_FAILURE;
520 /* Parses, performs the ERASE command. */
522 cmd_erase (struct lexer *lexer, struct dataset *ds UNUSED)
527 if (settings_get_safer_mode ())
529 msg (SE, _("This command not allowed when the %s option is set."), "SAFER");
533 if (!lex_force_match_id (lexer, "FILE"))
535 lex_match (lexer, T_EQUALS);
536 if (!lex_force_string (lexer))
539 filename = utf8_to_filename (lex_tokcstr (lexer));
540 retval = remove (filename);
545 msg (SW, _("Error removing `%s': %s."),
546 lex_tokcstr (lexer), strerror (errno));
554 /* Parses, performs the NEW FILE command. */
556 cmd_new_file (struct lexer *lexer UNUSED, struct dataset *ds)