Add more source code documentation.
[adu.git] / interactive.c
1 #include <ctype.h> /* isspace() */
2
3 #include "adu.h"
4 #include "format.h"
5 #include "select.h"
6 #include "string.h"
7 #include "error.h"
8 #include "cmdline.h"
9
10 /**
11 * Describes one valid command for interactive mode.
12 *
13 * When invoked in interactive mode, adu reads commands from stdin. There's a
14 * static array of all such commands.
15 */
16 struct interactive_command {
17 /** The name of the command. */
18 const char *name;
19 /** Pointer to The function that is being executed. */
20 int (*handler)(char *);
21 /** Help text. */
22 const char *desc;
23 };
24
25 static struct uid_range *admissible_uids;
26 static struct format_info *fi;
27
28 #define INTERACTIVE_COMMANDS \
29 INTERACTIVE_COMMAND(set, "change the current configuration") \
30 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
31 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
32 INTERACTIVE_COMMAND(run, "start the query according to the current configuration")
33
34
35 #define INTERACTIVE_COMMAND(name, desc) \
36 static int icom_ ## name (char *line);
37
38 INTERACTIVE_COMMANDS
39
40 #undef INTERACTIVE_COMMAND
41
42 #define INTERACTIVE_COMMAND(_name, _desc) \
43 { \
44 .name = #_name, \
45 .handler = icom_ ## _name, \
46 .desc = _desc \
47 },
48
49 struct interactive_command icmds[] = {
50 INTERACTIVE_COMMANDS
51 {.name = NULL}
52 };
53
54 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
55
56 static int read_input_line(char *line, size_t size)
57 {
58 return fgets(line, size, stdin)? 1 : -1;
59 }
60
61 static int icom_run(__a_unused char *line)
62 {
63 return run_select_query(admissible_uids, fi);
64 }
65
66 static int icom_help(__a_unused char *line)
67 {
68 struct interactive_command *c;
69
70 FOR_EACH_COMMAND(c)
71 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
72 return 1;
73 }
74
75 void print_interactive_help(void)
76 {
77 struct interactive_command *c;
78 FOR_EACH_COMMAND(c)
79 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
80 }
81
82 static int icom_reset(__a_unused char *line)
83 {
84 free_format_info(fi);
85 fi = NULL;
86 free(admissible_uids);
87 admissible_uids = NULL;
88 select_cmdline_parser_init(&select_conf);
89 return 1;
90 }
91
92 static int icom_set(char *line)
93 {
94 struct select_cmdline_parser_params params = {
95 .override = 1,
96 .initialize = 0,
97 .check_required = 1,
98 .check_ambiguity = 0,
99 .print_errors = 1
100 };
101 if (!line) {
102 select_cmdline_parser_dump(stdout, &select_conf);
103 return 1;
104 }
105
106 free_format_info(fi);
107 fi = NULL;
108 free(admissible_uids);
109 admissible_uids = NULL;
110 return parse_select_options(line, &params, &admissible_uids, &fi);
111 }
112
113 static int exec_interactive_command(char *line)
114 {
115 const char const *delim = "\t\n\f\r\v ";
116 int i;
117 char *cmd, *args;
118 int ret = -E_SYNTAX;
119 size_t len;
120
121 if (!line || !*line)
122 return 1;
123 len = strlen(line);
124
125 while (len && isspace(line[len - 1])) {
126 line[len - 1] = '\0';
127 len--;
128 }
129 if (!len)
130 return 1;
131 line += strspn(line, delim); /* skip initial whitespace */
132 if (!*line)
133 return 1;
134 /* OK, we have a non-empty line */
135 if (*line == '#')
136 return 1;
137 cmd = adu_strdup(line);
138 args = cmd + strcspn(cmd, delim);
139 if (!*args)
140 args = NULL;
141 else {
142 *args = '\0';
143 args++;
144 /* let p point to the next non-whitespace char */
145 args += strspn(args, delim);
146 if (!*args)
147 args = NULL;
148 }
149 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
150 for (i = 0; icmds[i].name; i++) {
151 if (strcmp(icmds[i].name, cmd))
152 continue;
153 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
154 ret = icmds[i].handler(args);
155 break;
156 }
157 free(cmd);
158 return ret;
159 }
160
161 int com_interactive(void)
162 {
163 char line[255];
164 int ret = 1;
165
166 select_cmdline_parser_init(&select_conf);
167 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
168 while (read_input_line(line, sizeof(line)) >= 0) {
169 ret = exec_interactive_command(line);
170 if (ret < 0)
171 printf("%s\n", adu_strerror(-ret));
172 }
173 return ret;
174 }