2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 #include <ctype.h> /* isspace() */
13 #include "select.cmdline.h"
18 * Describes one valid command for interactive mode.
20 * When invoked in interactive mode, adu reads commands from stdin. There's a
21 * static array of all such commands.
23 struct interactive_command {
24 /** The name of the command. */
26 /** Pointer to The function that is being executed. */
27 int (*handler)(char *);
32 static struct uid_range *admissible_uids;
33 static struct format_info *fi;
35 #define INTERACTIVE_COMMANDS \
36 INTERACTIVE_COMMAND(set, "change the current configuration") \
37 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
38 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
39 INTERACTIVE_COMMAND(run, "start the query according to the current configuration") \
40 INTERACTIVE_COMMAND(source, "read and execute interactive commands from a file")
43 #define INTERACTIVE_COMMAND(name, desc) \
44 static int icom_ ## name (char *line);
48 #undef INTERACTIVE_COMMAND
50 #define INTERACTIVE_COMMAND(_name, _desc) \
53 .handler = icom_ ## _name, \
57 struct interactive_command icmds[] = {
62 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
64 static int read_input_line(char *line, size_t size)
66 return fgets(line, size, stdin)? 1 : -1;
69 static int icom_run(__a_unused char *line)
71 return run_select_query(admissible_uids, fi);
74 static int icom_help(__a_unused char *line)
76 struct interactive_command *c;
79 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
83 void print_interactive_help(void)
85 struct interactive_command *c;
87 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
90 static int icom_reset(__a_unused char *line)
92 NOTICE_LOG("resetting configuration to default\n");
95 free(admissible_uids);
96 admissible_uids = NULL;
97 select_cmdline_parser_init(&select_conf);
98 return parse_select_options(NULL, NULL, &admissible_uids, &fi);
101 static int icom_set(char *line)
104 struct select_cmdline_parser_params params = {
108 .check_ambiguity = 0,
112 select_cmdline_parser_dump(stdout, &select_conf);
116 free_format_info(fi);
118 free(admissible_uids);
119 admissible_uids = NULL;
120 ret = parse_select_options(line, ¶ms, &admissible_uids, &fi);
123 return icom_reset(NULL);
126 static int exec_interactive_command(char *line)
128 const char const *delim = "\t\n\f\r\v ";
138 while (len && isspace(line[len - 1])) {
139 line[len - 1] = '\0';
144 line += strspn(line, delim); /* skip initial whitespace */
147 /* OK, we have a non-empty line */
150 cmd = adu_strdup(line);
151 args = cmd + strcspn(cmd, delim);
157 /* let args point to the next non-whitespace char */
158 args += strspn(args, delim);
162 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
163 for (i = 0; icmds[i].name; i++) {
164 if (strcmp(icmds[i].name, cmd))
166 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
167 ret = icmds[i].handler(args);
174 static int icom_source(char *args)
177 FILE *src = fopen(args, "r");
181 return -ERRNO_TO_ERROR(errno);
182 while (fgets(line, sizeof(line), src)) {
183 ret = exec_interactive_command(line);
193 int com_interactive(void)
198 select_cmdline_parser_init(&select_conf);
199 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
202 ret = read_uid_file();
205 while (read_input_line(line, sizeof(line)) >= 0) {
206 ret = exec_interactive_command(line);
208 printf("%s\n", adu_strerror(-ret));