8 struct interactive_command {
10 int (*handler)(char *);
14 static struct uid_range *admissible_uids;
15 static struct format_info *fi;
17 #define INTERACTIVE_COMMANDS \
18 INTERACTIVE_COMMAND(dump, "dump the current configuration") \
19 INTERACTIVE_COMMAND(set, "change the current configuration") \
20 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
21 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
22 INTERACTIVE_COMMAND(run, "start the query according to the current options")
25 #define INTERACTIVE_COMMAND(name, desc) \
26 static int icom_ ## name (char *line);
30 #undef INTERACTIVE_COMMAND
32 #define INTERACTIVE_COMMAND(_name, _desc) \
35 .handler = icom_ ## _name, \
39 struct interactive_command icmds[] = {
44 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
46 static int read_input_line(char *line, size_t size)
48 return fgets(line, size, stdin)? 1 : -1;
51 static int icom_run(__a_unused char *line)
53 return run_select_query(admissible_uids, fi);
56 static int icom_help(__a_unused char *line)
58 struct interactive_command *c;
61 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
65 void print_interactive_help(void)
70 static int icom_reset(__a_unused char *line)
72 select_cmdline_parser_init(&select_conf);
76 static int icom_set(char *line)
78 struct select_cmdline_parser_params params = {
85 return parse_select_options(line, ¶ms, &admissible_uids, &fi);
88 static int icom_dump(__a_unused char *line)
90 select_cmdline_parser_dump(stdout, &select_conf);
94 static int exec_interactive_command(char *line)
96 const char const *delim = "\t\n ";
98 char *cmd = adu_strdup(line + strspn(line, delim));
99 char *p = cmd + strcspn(cmd, delim);
104 for (i = 0; icmds[i].name; i++) {
105 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
106 if (strcmp(icmds[i].name, cmd))
108 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
109 ret = icmds[i].handler(p);
116 int com_interactive(void)
121 select_cmdline_parser_init(&select_conf);
122 while (read_input_line(line, sizeof(line)) >= 0) {
123 size_t len = strlen(line);
126 if (line[len - 1] == '\n')
127 line[len - 1] = '\0';
128 ret = exec_interactive_command(line);
130 printf("%s\n", adu_strerror(-ret));