2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file interactive.c \brief Commands for interactive mode. */
9 #include <ctype.h> /* isspace() */
16 #include "select.cmdline.h"
21 * Describes one valid command for interactive mode.
23 * When invoked in interactive mode, adu reads commands from stdin. There's a
24 * static array of all such commands.
26 struct interactive_command {
27 /** The name of the command. */
29 /** Pointer to The function that is being executed. */
30 int (*handler)(char *);
35 static struct uid_range *admissible_uids;
36 static struct format_info *fi;
38 /** The set of supported interactive commands. */
39 #define INTERACTIVE_COMMANDS \
40 INTERACTIVE_COMMAND(set, "change the current configuration") \
41 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
42 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
43 INTERACTIVE_COMMAND(run, "start the query according to the current configuration") \
44 INTERACTIVE_COMMAND(source, "read and execute interactive commands from a file")
47 /** \cond doxygen is not smart enough for this */
48 #define INTERACTIVE_COMMAND(name, desc) \
49 static int icom_ ## name (char *line);
53 #undef INTERACTIVE_COMMAND
55 #define INTERACTIVE_COMMAND(_name, _desc) \
58 .handler = icom_ ## _name, \
62 struct interactive_command icmds[] = {
68 /** Iterate over the list of all interactive commands. */
69 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
71 static int read_input_line(char *line, size_t size)
73 return fgets(line, size, stdin)? 1 : -1;
76 static int icom_run(__a_unused char *line)
78 return run_select_query(admissible_uids, fi);
81 static int icom_help(__a_unused char *line)
83 struct interactive_command *c;
86 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
91 * Print the list of commands with short descriptions.
93 void print_interactive_help(void)
95 struct interactive_command *c;
97 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
100 static int icom_reset(__a_unused char *line)
102 NOTICE_LOG("resetting configuration to default\n");
103 free_format_info(fi);
105 free(admissible_uids);
106 admissible_uids = NULL;
107 select_cmdline_parser_init(&select_conf);
108 return parse_select_options(NULL, NULL, &admissible_uids, &fi);
111 static int icom_set(char *line)
113 struct select_cmdline_parser_params params = {
117 .check_ambiguity = 0,
121 select_cmdline_parser_dump(stdout, &select_conf);
125 free_format_info(fi);
127 free(admissible_uids);
128 admissible_uids = NULL;
129 return parse_select_options(line, ¶ms, &admissible_uids, &fi);
132 static int exec_interactive_command(char *line)
134 const char const *delim = "\t\n\f\r\v ";
144 while (len && adu_isspace(line[len - 1])) {
145 line[len - 1] = '\0';
150 line += strspn(line, delim); /* skip initial whitespace */
153 /* OK, we have a non-empty line */
156 cmd = adu_strdup(line);
157 args = cmd + strcspn(cmd, delim);
163 /* let args point to the next non-whitespace char */
164 args += strspn(args, delim);
168 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
169 for (i = 0; icmds[i].name; i++) {
170 if (strcmp(icmds[i].name, cmd))
172 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
173 ret = icmds[i].handler(args);
180 static int icom_source(char *args)
183 FILE *src = fopen(args, "r");
187 return -ERRNO_TO_ERROR(errno);
188 while (fgets(line, sizeof(line), src)) {
189 ret = exec_interactive_command(line);
200 * The main function for interactive mode.
204 int com_interactive(void)
209 select_cmdline_parser_init(&select_conf);
210 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
213 ret = read_uid_file();
216 while (read_input_line(line, sizeof(line)) >= 0) {
217 ret = exec_interactive_command(line);
219 printf("%s\n", adu_strerror(-ret));