X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=interactive.c;h=9460877e344e8f8452b0f0d20f1d6ceef83dd39e;hp=036f7a84d1a861eb0a48b21583854cb6fe0a068a;hb=e584cd6d5a3782ddeb6640f69534ab1664da2605;hpb=de525e7c5b6592b41f8681f919d5d53406801861 diff --git a/interactive.c b/interactive.c index 036f7a8..9460877 100644 --- a/interactive.c +++ b/interactive.c @@ -1,13 +1,25 @@ +#include /* isspace() */ + #include "adu.h" #include "format.h" -#include "select.h" +#include "user.h" #include "string.h" +#include "select.cmdline.h" +#include "select.h" #include "error.h" -#include "cmdline.h" +/** + * Describes one valid command for interactive mode. + * + * When invoked in interactive mode, adu reads commands from stdin. There's a + * static array of all such commands. + */ struct interactive_command { + /** The name of the command. */ const char *name; + /** Pointer to The function that is being executed. */ int (*handler)(char *); + /** Help text. */ const char *desc; }; @@ -15,11 +27,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) \ @@ -62,8 +73,19 @@ static int icom_help(__a_unused char *line) return 1; } +void print_interactive_help(void) +{ + struct interactive_command *c; + FOR_EACH_COMMAND(c) + fprintf(stdout, "\t%s\t%s\n", c->name, c->desc); +} + static int icom_reset(__a_unused char *line) { + free_format_info(fi); + fi = NULL; + free(admissible_uids); + admissible_uids = NULL; select_cmdline_parser_init(&select_conf); return 1; } @@ -77,31 +99,60 @@ 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; + free_format_info(fi); + fi = NULL; + free(admissible_uids); + admissible_uids = NULL; + 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; - *p = '\0'; - p++; + if (!line || !*line) + return 1; + len = strlen(line); + + 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 */ + if (*line == '#') + return 1; + cmd = adu_strdup(line); + args = cmd + strcspn(cmd, delim); + if (!*args) + args = NULL; + else { + *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); @@ -114,12 +165,8 @@ int com_interactive(void) int ret = 1; select_cmdline_parser_init(&select_conf); + ret = parse_select_options(NULL, NULL, &admissible_uids, &fi); 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));