1 #include <ctype.h> /* isspace() */
7 #include "select.cmdline.h"
12 * Describes one valid command for interactive mode.
14 * When invoked in interactive mode, adu reads commands from stdin. There's a
15 * static array of all such commands.
17 struct interactive_command {
18 /** The name of the command. */
20 /** Pointer to The function that is being executed. */
21 int (*handler)(char *);
26 static struct uid_range *admissible_uids;
27 static struct format_info *fi;
29 #define INTERACTIVE_COMMANDS \
30 INTERACTIVE_COMMAND(set, "change the current configuration") \
31 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
32 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
33 INTERACTIVE_COMMAND(run, "start the query according to the current configuration")
36 #define INTERACTIVE_COMMAND(name, desc) \
37 static int icom_ ## name (char *line);
41 #undef INTERACTIVE_COMMAND
43 #define INTERACTIVE_COMMAND(_name, _desc) \
46 .handler = icom_ ## _name, \
50 struct interactive_command icmds[] = {
55 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
57 static int read_input_line(char *line, size_t size)
59 return fgets(line, size, stdin)? 1 : -1;
62 static int icom_run(__a_unused char *line)
64 return run_select_query(admissible_uids, fi);
67 static int icom_help(__a_unused char *line)
69 struct interactive_command *c;
72 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
76 void print_interactive_help(void)
78 struct interactive_command *c;
80 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
83 static int icom_reset(__a_unused char *line)
87 free(admissible_uids);
88 admissible_uids = NULL;
89 select_cmdline_parser_init(&select_conf);
93 static int icom_set(char *line)
95 struct select_cmdline_parser_params params = {
103 select_cmdline_parser_dump(stdout, &select_conf);
107 free_format_info(fi);
109 free(admissible_uids);
110 admissible_uids = NULL;
111 return parse_select_options(line, ¶ms, &admissible_uids, &fi);
114 static int exec_interactive_command(char *line)
116 const char const *delim = "\t\n\f\r\v ";
126 while (len && isspace(line[len - 1])) {
127 line[len - 1] = '\0';
132 line += strspn(line, delim); /* skip initial whitespace */
135 /* OK, we have a non-empty line */
138 cmd = adu_strdup(line);
139 args = cmd + strcspn(cmd, delim);
145 /* let p point to the next non-whitespace char */
146 args += strspn(args, delim);
150 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
151 for (i = 0; icmds[i].name; i++) {
152 if (strcmp(icmds[i].name, cmd))
154 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
155 ret = icmds[i].handler(args);
162 int com_interactive(void)
167 select_cmdline_parser_init(&select_conf);
168 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
169 while (read_input_line(line, sizeof(line)) >= 0) {
170 ret = exec_interactive_command(line);
172 printf("%s\n", adu_strerror(-ret));