2 * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
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);
133 * Wrapper for isspace.
137 * The values should be cast to an unsigned char first, then to int.
138 * Why? Because the isdigit (as do all other is/to functions/macros)
139 * expect a number from 0 upto and including 255 as their (int) argument.
140 * Because char is signed on most systems, casting it to int immediately
141 * gives the functions an argument between -128 and 127 (inclusive),
142 * which they will use as an array index, and which will thus fail
143 * horribly for characters which have their most significant bit set.
145 #define adu_isspace(c) isspace((int)(unsigned char)(c))
147 static int exec_interactive_command(char *line)
149 const char const *delim = "\t\n\f\r\v ";
159 while (len && adu_isspace(line[len - 1])) {
160 line[len - 1] = '\0';
165 line += strspn(line, delim); /* skip initial whitespace */
168 /* OK, we have a non-empty line */
171 cmd = adu_strdup(line);
172 args = cmd + strcspn(cmd, delim);
178 /* let args point to the next non-whitespace char */
179 args += strspn(args, delim);
183 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
184 for (i = 0; icmds[i].name; i++) {
185 if (strcmp(icmds[i].name, cmd))
187 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
188 ret = icmds[i].handler(args);
195 static int icom_source(char *args)
198 FILE *src = fopen(args, "r");
202 return -ERRNO_TO_ERROR(errno);
203 while (fgets(line, sizeof(line), src)) {
204 ret = exec_interactive_command(line);
215 * The main function for interactive mode.
219 int com_interactive(void)
224 select_cmdline_parser_init(&select_conf);
225 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
228 ret = read_uid_file();
231 while (read_input_line(line, sizeof(line)) >= 0) {
232 ret = exec_interactive_command(line);
234 printf("%s\n", adu_strerror(-ret));