6 struct interactive_command {
8 int (*handler)(char *);
12 static struct uid_range *admissible_uids;
14 #define INTERACTIVE_COMMANDS \
15 INTERACTIVE_COMMAND(dump, "dump the current configuration") \
16 INTERACTIVE_COMMAND(set, "change the current configuration") \
17 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
18 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
19 INTERACTIVE_COMMAND(run, "start the query according to the current options")
22 #define INTERACTIVE_COMMAND(name, desc) \
23 static int icom_ ## name (char *line);
27 #undef INTERACTIVE_COMMAND
29 #define INTERACTIVE_COMMAND(_name, _desc) \
32 .handler = icom_ ## _name, \
36 struct interactive_command icmds[] = {
41 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
43 static int read_input_line(char *line, size_t size)
45 return fgets(line, size, stdin)? 1 : -1;
48 static int icom_run(__a_unused char *line)
50 return run_select_query(admissible_uids);
53 static int icom_help(__a_unused char *line)
55 struct interactive_command *c;
58 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
62 static int icom_reset(__a_unused char *line)
64 select_cmdline_parser_init(&select_conf);
68 static int icom_set(char *line)
70 struct select_cmdline_parser_params params = {
77 return parse_select_options(line, ¶ms, &admissible_uids);
80 static int icom_dump(__a_unused char *line)
82 ERROR_LOG("dump: %s\n", select_conf.format_arg);
83 select_cmdline_parser_dump(stdout, &select_conf);
87 static int exec_interactive_command(char *line)
89 const char const *delim = "\t\n ";
91 char *cmd = adu_strdup(line + strspn(line, delim));
92 char *p = cmd + strcspn(cmd, delim);
97 for (i = 0; icmds[i].name; i++) {
98 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
99 if (strcmp(icmds[i].name, cmd))
101 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
102 ret = icmds[i].handler(p);
109 int com_interactive(void)
114 select_cmdline_parser_init(&select_conf);
115 while (read_input_line(line, sizeof(line)) >= 0) {
116 size_t len = strlen(line);
119 if (line[len - 1] == '\n')
120 line[len - 1] = '\0';
121 ret = exec_interactive_command(line);
123 printf("%s\n", adu_strerror(-ret));