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") \
34 INTERACTIVE_COMMAND(source, "read and execute interactive commands from a file")
37 #define INTERACTIVE_COMMAND(name, desc) \
38 static int icom_ ## name (char *line);
42 #undef INTERACTIVE_COMMAND
44 #define INTERACTIVE_COMMAND(_name, _desc) \
47 .handler = icom_ ## _name, \
51 struct interactive_command icmds[] = {
56 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
58 static int read_input_line(char *line, size_t size)
60 return fgets(line, size, stdin)? 1 : -1;
63 static int icom_run(__a_unused char *line)
65 return run_select_query(admissible_uids, fi);
68 static int icom_help(__a_unused char *line)
70 struct interactive_command *c;
73 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
77 void print_interactive_help(void)
79 struct interactive_command *c;
81 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
84 static int icom_reset(__a_unused char *line)
86 NOTICE_LOG("resetting configuration to default\n");
89 free(admissible_uids);
90 admissible_uids = NULL;
91 select_cmdline_parser_init(&select_conf);
92 return parse_select_options(NULL, NULL, &admissible_uids, &fi);
95 static int icom_set(char *line)
98 struct select_cmdline_parser_params params = {
102 .check_ambiguity = 0,
106 select_cmdline_parser_dump(stdout, &select_conf);
110 free_format_info(fi);
112 free(admissible_uids);
113 admissible_uids = NULL;
114 ret = parse_select_options(line, ¶ms, &admissible_uids, &fi);
117 return icom_reset(NULL);
120 static int exec_interactive_command(char *line)
122 const char const *delim = "\t\n\f\r\v ";
132 while (len && isspace(line[len - 1])) {
133 line[len - 1] = '\0';
138 line += strspn(line, delim); /* skip initial whitespace */
141 /* OK, we have a non-empty line */
144 cmd = adu_strdup(line);
145 args = cmd + strcspn(cmd, delim);
151 /* let args point to the next non-whitespace char */
152 args += strspn(args, delim);
156 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
157 for (i = 0; icmds[i].name; i++) {
158 if (strcmp(icmds[i].name, cmd))
160 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
161 ret = icmds[i].handler(args);
168 static int icom_source(char *args)
171 FILE *src = fopen(args, "r");
175 return -ERRNO_TO_ERROR(errno);
176 while (fgets(line, sizeof(line), src)) {
177 ret = exec_interactive_command(line);
187 int com_interactive(void)
192 select_cmdline_parser_init(&select_conf);
193 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
196 ret = read_uid_file();
199 while (read_input_line(line, sizeof(line)) >= 0) {
200 ret = exec_interactive_command(line);
202 printf("%s\n", adu_strerror(-ret));