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