]> git.tue.mpg.de Git - adu.git/commitdiff
Improve interactive feeling, kill dump command.
authorAndre Noll <maan@systemlinux.org>
Sat, 1 Nov 2008 22:45:47 +0000 (23:45 +0100)
committerAndre Noll <maan@systemlinux.org>
Sat, 1 Nov 2008 22:45:47 +0000 (23:45 +0100)
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

index 191e6330f3989995ab93c42252f8a7f1d78ad2dd..a35a82b3867b75bff62ec44fb6da5592954b02cf 100644 (file)
@@ -1,3 +1,5 @@
+#include <ctype.h> /* 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, &params, &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, &params, &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));