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)
85 NOTICE_LOG("resetting configuration to default\n");
88 free(admissible_uids);
89 admissible_uids = NULL;
90 select_cmdline_parser_init(&select_conf);
91 return parse_select_options(NULL, NULL, &admissible_uids, &fi);
94 static int icom_set(char *line)
97 struct select_cmdline_parser_params params = {
101 .check_ambiguity = 0,
105 select_cmdline_parser_dump(stdout, &select_conf);
109 free_format_info(fi);
111 free(admissible_uids);
112 admissible_uids = NULL;
113 ret = parse_select_options(line, ¶ms, &admissible_uids, &fi);
116 return icom_reset(NULL);
119 static int exec_interactive_command(char *line)
121 const char const *delim = "\t\n\f\r\v ";
131 while (len && isspace(line[len - 1])) {
132 line[len - 1] = '\0';
137 line += strspn(line, delim); /* skip initial whitespace */
140 /* OK, we have a non-empty line */
143 cmd = adu_strdup(line);
144 args = cmd + strcspn(cmd, delim);
150 /* let args point to the next non-whitespace char */
151 args += strspn(args, delim);
155 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
156 for (i = 0; icmds[i].name; i++) {
157 if (strcmp(icmds[i].name, cmd))
159 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
160 ret = icmds[i].handler(args);
167 int com_interactive(void)
172 select_cmdline_parser_init(&select_conf);
173 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
176 ret = read_uid_file();
179 while (read_input_line(line, sizeof(line)) >= 0) {
180 ret = exec_interactive_command(line);
182 printf("%s\n", adu_strerror(-ret));