5 #include "select.cmdline.h"
7 struct interactive_command {
9 int (*handler)(char *);
13 static struct uid_range *admissible_uids;
15 #define INTERACTIVE_COMMANDS \
16 INTERACTIVE_COMMAND(dump, "dump the current configuration") \
17 INTERACTIVE_COMMAND(set, "change the current configuration") \
18 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
19 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
20 INTERACTIVE_COMMAND(run, "start the query according to the current options")
23 #define INTERACTIVE_COMMAND(name, desc) \
24 static int icom_ ## name (char *line);
28 #undef INTERACTIVE_COMMAND
30 #define INTERACTIVE_COMMAND(_name, _desc) \
33 .handler = icom_ ## _name, \
37 struct interactive_command icmds[] = {
42 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
44 static int read_input_line(char *line, size_t size)
46 return fgets(line, size, stdin)? 1 : -1;
49 static int icom_run(__a_unused char *line)
51 return run_select_query(admissible_uids);
54 static int icom_help(__a_unused char *line)
56 struct interactive_command *c;
59 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
63 static int icom_reset(__a_unused char *line)
65 select_cmdline_parser_init(&select_conf);
69 static int icom_set(char *line)
71 struct select_cmdline_parser_params params = {
78 if (select_cmdline_parser_string_ext(line, &select_conf, "select",
81 return parse_uid_arg(select_conf.uid_arg, &admissible_uids);
84 static int icom_dump(__a_unused char *line)
86 ERROR_LOG("dump: %s\n", select_conf.format_arg);
87 select_cmdline_parser_dump(stdout, &select_conf);
91 static int exec_interactive_command(char *line)
93 const char const *delim = "\t\n ";
95 char *cmd = adu_strdup(line + strspn(line, delim));
96 char *p = cmd + strcspn(cmd, delim);
101 for (i = 0; icmds[i].name; i++) {
102 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
103 if (strcmp(icmds[i].name, cmd))
105 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
106 ret = icmds[i].handler(p);
113 int com_interactive(void)
118 select_cmdline_parser_init(&select_conf);
119 while (read_input_line(line, sizeof(line)) >= 0) {
120 size_t len = strlen(line);
123 if (line[len - 1] == '\n')
124 line[len - 1] = '\0';
125 ret = exec_interactive_command(line);
127 printf("%s\n", adu_strerror(-ret));