7 struct interactive_command {
9 int (*handler)(char *);
13 static struct uid_range *admissible_uids;
14 static struct select_format_info sfi;
16 #define INTERACTIVE_COMMANDS \
17 INTERACTIVE_COMMAND(dump, "dump the current configuration") \
18 INTERACTIVE_COMMAND(set, "change the current configuration") \
19 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
20 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
21 INTERACTIVE_COMMAND(run, "start the query according to the current options")
24 #define INTERACTIVE_COMMAND(name, desc) \
25 static int icom_ ## name (char *line);
29 #undef INTERACTIVE_COMMAND
31 #define INTERACTIVE_COMMAND(_name, _desc) \
34 .handler = icom_ ## _name, \
38 struct interactive_command icmds[] = {
43 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
45 static int read_input_line(char *line, size_t size)
47 return fgets(line, size, stdin)? 1 : -1;
50 static int icom_run(__a_unused char *line)
52 return run_select_query(admissible_uids, &sfi);
55 static int icom_help(__a_unused char *line)
57 struct interactive_command *c;
60 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
64 static int icom_reset(__a_unused char *line)
66 select_cmdline_parser_init(&select_conf);
70 static int icom_set(char *line)
72 struct select_cmdline_parser_params params = {
79 return parse_select_options(line, ¶ms, &admissible_uids, &sfi);
82 static int icom_dump(__a_unused char *line)
84 ERROR_LOG("dump: %s\n", select_conf.format_arg);
85 select_cmdline_parser_dump(stdout, &select_conf);
89 static int exec_interactive_command(char *line)
91 const char const *delim = "\t\n ";
93 char *cmd = adu_strdup(line + strspn(line, delim));
94 char *p = cmd + strcspn(cmd, delim);
99 for (i = 0; icmds[i].name; i++) {
100 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
101 if (strcmp(icmds[i].name, cmd))
103 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
104 ret = icmds[i].handler(p);
111 int com_interactive(void)
116 select_cmdline_parser_init(&select_conf);
117 while (read_input_line(line, sizeof(line)) >= 0) {
118 size_t len = strlen(line);
121 if (line[len - 1] == '\n')
122 line[len - 1] = '\0';
123 ret = exec_interactive_command(line);
125 printf("%s\n", adu_strerror(-ret));