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)
114 struct select_cmdline_parser_params params = {
118 .check_ambiguity = 0,
122 select_cmdline_parser_dump(stdout, &select_conf);
126 free_format_info(fi);
128 free(admissible_uids);
129 admissible_uids = NULL;
130 ret = parse_select_options(line, ¶ms, &admissible_uids, &fi);
133 return icom_reset(NULL);
136 static int exec_interactive_command(char *line)
138 const char const *delim = "\t\n\f\r\v ";
148 while (len && isspace(line[len - 1])) {
149 line[len - 1] = '\0';
154 line += strspn(line, delim); /* skip initial whitespace */
157 /* OK, we have a non-empty line */
160 cmd = adu_strdup(line);
161 args = cmd + strcspn(cmd, delim);
167 /* let args point to the next non-whitespace char */
168 args += strspn(args, delim);
172 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
173 for (i = 0; icmds[i].name; i++) {
174 if (strcmp(icmds[i].name, cmd))
176 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
177 ret = icmds[i].handler(args);
184 static int icom_source(char *args)
187 FILE *src = fopen(args, "r");
191 return -ERRNO_TO_ERROR(errno);
192 while (fgets(line, sizeof(line), src)) {
193 ret = exec_interactive_command(line);
204 * The main function for interactive mode.
208 int com_interactive(void)
213 select_cmdline_parser_init(&select_conf);
214 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
217 ret = read_uid_file(conf.database_dir_arg);
220 while (read_input_line(line, sizeof(line)) >= 0) {
221 ret = exec_interactive_command(line);
223 printf("%s\n", adu_strerror(-ret));