1 #include <ctype.h> /* isspace() */
11 * Describes one valid command for interactive mode.
13 * When invoked in interactive mode, adu reads commands from stdin. There's a
14 * static array of all such commands.
16 struct interactive_command {
17 /** The name of the command. */
19 /** Pointer to The function that is being executed. */
20 int (*handler)(char *);
25 static struct uid_range *admissible_uids;
26 static struct format_info *fi;
28 #define INTERACTIVE_COMMANDS \
29 INTERACTIVE_COMMAND(set, "change the current configuration") \
30 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
31 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
32 INTERACTIVE_COMMAND(run, "start the query according to the current configuration")
35 #define INTERACTIVE_COMMAND(name, desc) \
36 static int icom_ ## name (char *line);
40 #undef INTERACTIVE_COMMAND
42 #define INTERACTIVE_COMMAND(_name, _desc) \
45 .handler = icom_ ## _name, \
49 struct interactive_command icmds[] = {
54 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
56 static int read_input_line(char *line, size_t size)
58 return fgets(line, size, stdin)? 1 : -1;
61 static int icom_run(__a_unused char *line)
63 return run_select_query(admissible_uids, fi);
66 static int icom_help(__a_unused char *line)
68 struct interactive_command *c;
71 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
75 void print_interactive_help(void)
77 struct interactive_command *c;
79 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
82 static int icom_reset(__a_unused char *line)
86 free(admissible_uids);
87 admissible_uids = NULL;
88 select_cmdline_parser_init(&select_conf);
92 static int icom_set(char *line)
94 struct select_cmdline_parser_params params = {
102 select_cmdline_parser_dump(stdout, &select_conf);
106 free_format_info(fi);
108 free(admissible_uids);
109 admissible_uids = NULL;
110 return parse_select_options(line, ¶ms, &admissible_uids, &fi);
113 static int exec_interactive_command(char *line)
115 const char const *delim = "\t\n\f\r\v ";
125 while (len && isspace(line[len - 1])) {
126 line[len - 1] = '\0';
131 line += strspn(line, delim); /* skip initial whitespace */
134 /* OK, we have a non-empty line */
137 cmd = adu_strdup(line);
138 args = cmd + strcspn(cmd, delim);
144 /* let p point to the next non-whitespace char */
145 args += strspn(args, delim);
149 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
150 for (i = 0; icmds[i].name; i++) {
151 if (strcmp(icmds[i].name, cmd))
153 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
154 ret = icmds[i].handler(args);
161 int com_interactive(void)
166 select_cmdline_parser_init(&select_conf);
167 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
168 while (read_input_line(line, sizeof(line)) >= 0) {
169 ret = exec_interactive_command(line);
171 printf("%s\n", adu_strerror(-ret));