1 #include <ctype.h> /* isspace() */
10 struct interactive_command {
12 int (*handler)(char *);
16 static struct uid_range *admissible_uids;
17 static struct format_info *fi;
19 #define INTERACTIVE_COMMANDS \
20 INTERACTIVE_COMMAND(set, "change the current configuration") \
21 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
22 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
23 INTERACTIVE_COMMAND(run, "start the query according to the current configuration")
26 #define INTERACTIVE_COMMAND(name, desc) \
27 static int icom_ ## name (char *line);
31 #undef INTERACTIVE_COMMAND
33 #define INTERACTIVE_COMMAND(_name, _desc) \
36 .handler = icom_ ## _name, \
40 struct interactive_command icmds[] = {
45 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
47 static int read_input_line(char *line, size_t size)
49 return fgets(line, size, stdin)? 1 : -1;
52 static int icom_run(__a_unused char *line)
54 return run_select_query(admissible_uids, fi);
57 static int icom_help(__a_unused char *line)
59 struct interactive_command *c;
62 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
66 void print_interactive_help(void)
71 static int icom_reset(__a_unused char *line)
75 free(admissible_uids);
76 admissible_uids = NULL;
77 select_cmdline_parser_init(&select_conf);
81 static int icom_set(char *line)
83 struct select_cmdline_parser_params params = {
91 select_cmdline_parser_dump(stdout, &select_conf);
97 free(admissible_uids);
98 admissible_uids = NULL;
99 return parse_select_options(line, ¶ms, &admissible_uids, &fi);
102 static int exec_interactive_command(char *line)
104 const char const *delim = "\t\n\f\r\v ";
114 while (len && isspace(line[len - 1])) {
115 line[len - 1] = '\0';
120 line += strspn(line, delim); /* skip initial whitespace */
123 /* OK, we have a non-empty line */
126 cmd = adu_strdup(line);
127 args = cmd + strcspn(cmd, delim);
133 /* let p point to the next non-whitespace char */
134 args += strspn(args, delim);
138 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
139 for (i = 0; icmds[i].name; i++) {
140 if (strcmp(icmds[i].name, cmd))
142 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
143 ret = icmds[i].handler(args);
150 int com_interactive(void)
155 select_cmdline_parser_init(&select_conf);
156 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
157 while (read_input_line(line, sizeof(line)) >= 0) {
158 ret = exec_interactive_command(line);
160 printf("%s\n", adu_strerror(-ret));