From 99eaf79ebdb45f935eb9d8b60cc0889e09d5b6df Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 1 Nov 2008 23:45:47 +0100 Subject: [PATCH] Improve interactive feeling, kill dump command. It's no good to spam the user with syntax errors on empty lines. In popular shells, the set command prints the current environment if called without arguments. So do the same and kill the dump command. --- interactive.c | 61 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/interactive.c b/interactive.c index 191e633..a35a82b 100644 --- a/interactive.c +++ b/interactive.c @@ -1,3 +1,5 @@ +#include /* isspace() */ + #include "adu.h" #include "format.h" #include "select.h" @@ -15,11 +17,10 @@ static struct uid_range *admissible_uids; static struct format_info *fi; #define INTERACTIVE_COMMANDS \ - INTERACTIVE_COMMAND(dump, "dump the current configuration") \ INTERACTIVE_COMMAND(set, "change the current configuration") \ INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \ INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \ - INTERACTIVE_COMMAND(run, "start the query according to the current options") + INTERACTIVE_COMMAND(run, "start the query according to the current configuration") #define INTERACTIVE_COMMAND(name, desc) \ @@ -82,35 +83,54 @@ static int icom_set(char *line) .check_ambiguity = 0, .print_errors = 1 }; - return parse_select_options(line, ¶ms, &admissible_uids, &fi); -} + if (!line) { + select_cmdline_parser_dump(stdout, &select_conf); + return 1; + } -static int icom_dump(__a_unused char *line) -{ - select_cmdline_parser_dump(stdout, &select_conf); - return 1; + return parse_select_options(line, ¶ms, &admissible_uids, &fi); } static int exec_interactive_command(char *line) { - const char const *delim = "\t\n "; + const char const *delim = "\t\n\f\r\v "; int i; - char *cmd = adu_strdup(line + strspn(line, delim)); - char *p = cmd + strcspn(cmd, delim); + char *cmd, *args; int ret = -E_SYNTAX; + size_t len; + + if (!line || !*line) + return 1; + len = strlen(line); - if (*p == '\0') - p = NULL; + while (len && isspace(line[len - 1])) { + line[len - 1] = '\0'; + len--; + } + if (!len) + return 1; + line += strspn(line, delim); /* skip initial whitespace */ + if (!*line) + return 1; + /* OK, we have a non-empty line */ + cmd = adu_strdup(line); + args = cmd + strcspn(cmd, delim); + if (!*args) + args = NULL; else { - *p = '\0'; - p++; + *args = '\0'; + args++; + /* let p point to the next non-whitespace char */ + args += strspn(args, delim); + if (!*args) + args = NULL; } + DEBUG_LOG("name: %s, args: %s.\n", cmd, args); for (i = 0; icmds[i].name; i++) { - ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd); if (strcmp(icmds[i].name, cmd)) continue; - ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p); - ret = icmds[i].handler(p); + INFO_LOG("exec cmd: %s, args: %s\n", cmd, args); + ret = icmds[i].handler(args); break; } free(cmd); @@ -124,11 +144,6 @@ int com_interactive(void) select_cmdline_parser_init(&select_conf); while (read_input_line(line, sizeof(line)) >= 0) { - size_t len = strlen(line); - if (!len) - continue; - if (line[len - 1] == '\n') - line[len - 1] = '\0'; ret = exec_interactive_command(line); if (ret < 0) printf("%s\n", adu_strerror(-ret)); -- 2.39.2