Fix user summary accounting if pattern is given.
[adu.git] / interactive.c
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file interactive.c \brief Commands for interactive mode. */
8
9 #include <ctype.h> /* isspace() */
10
11 #include "adu.h"
12 #include "format.h"
13 #include "user.h"
14 #include "string.h"
15 #include "cmdline.h"
16 #include "select.cmdline.h"
17 #include "select.h"
18 #include "error.h"
19
20 /**
21 * Describes one valid command for interactive mode.
22 *
23 * When invoked in interactive mode, adu reads commands from stdin. There's a
24 * static array of all such commands.
25 */
26 struct interactive_command {
27 /** The name of the command. */
28 const char *name;
29 /** Pointer to The function that is being executed. */
30 int (*handler)(char *);
31 /** Help text. */
32 const char *desc;
33 };
34
35 static struct uid_range *admissible_uids;
36 static struct format_info *fi;
37
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")
45
46
47 /** \cond doxygen is not smart enough for this */
48 #define INTERACTIVE_COMMAND(name, desc) \
49 static int icom_ ## name (char *line);
50
51 INTERACTIVE_COMMANDS
52
53 #undef INTERACTIVE_COMMAND
54
55 #define INTERACTIVE_COMMAND(_name, _desc) \
56 { \
57 .name = #_name, \
58 .handler = icom_ ## _name, \
59 .desc = _desc \
60 },
61
62 struct interactive_command icmds[] = {
63 INTERACTIVE_COMMANDS
64 {.name = NULL}
65 };
66 /** \endcond */
67
68 /** Iterate over the list of all interactive commands. */
69 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
70
71 static int read_input_line(char *line, size_t size)
72 {
73 return fgets(line, size, stdin)? 1 : -1;
74 }
75
76 static int icom_run(__a_unused char *line)
77 {
78 return run_select_query(admissible_uids, fi);
79 }
80
81 static int icom_help(__a_unused char *line)
82 {
83 struct interactive_command *c;
84
85 FOR_EACH_COMMAND(c)
86 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
87 return 1;
88 }
89
90 /**
91 * Print the list of commands with short descriptions.
92 */
93 void print_interactive_help(void)
94 {
95 struct interactive_command *c;
96 FOR_EACH_COMMAND(c)
97 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
98 }
99
100 static int icom_reset(__a_unused char *line)
101 {
102 NOTICE_LOG("resetting configuration to default\n");
103 free_format_info(fi);
104 fi = NULL;
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);
109 }
110
111 static int icom_set(char *line)
112 {
113 int ret;
114 struct select_cmdline_parser_params params = {
115 .override = 1,
116 .initialize = 0,
117 .check_required = 1,
118 .check_ambiguity = 0,
119 .print_errors = 1
120 };
121 if (!line) {
122 select_cmdline_parser_dump(stdout, &select_conf);
123 return 1;
124 }
125
126 free_format_info(fi);
127 fi = NULL;
128 free(admissible_uids);
129 admissible_uids = NULL;
130 ret = parse_select_options(line, &params, &admissible_uids, &fi);
131 if (ret >= 0)
132 return ret;
133 return icom_reset(NULL);
134 }
135
136 static int exec_interactive_command(char *line)
137 {
138 const char const *delim = "\t\n\f\r\v ";
139 int i;
140 char *cmd, *args;
141 int ret = -E_SYNTAX;
142 size_t len;
143
144 if (!line || !*line)
145 return 1;
146 len = strlen(line);
147
148 while (len && adu_isspace(line[len - 1])) {
149 line[len - 1] = '\0';
150 len--;
151 }
152 if (!len)
153 return 1;
154 line += strspn(line, delim); /* skip initial whitespace */
155 if (!*line)
156 return 1;
157 /* OK, we have a non-empty line */
158 if (*line == '#')
159 return 1;
160 cmd = adu_strdup(line);
161 args = cmd + strcspn(cmd, delim);
162 if (!*args)
163 args = NULL;
164 else {
165 *args = '\0';
166 args++;
167 /* let args point to the next non-whitespace char */
168 args += strspn(args, delim);
169 if (!*args)
170 args = NULL;
171 }
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))
175 continue;
176 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
177 ret = icmds[i].handler(args);
178 break;
179 }
180 free(cmd);
181 return ret;
182 }
183
184 static int icom_source(char *args)
185 {
186 char line[255];
187 FILE *src = fopen(args, "r");
188 int ret;
189
190 if (!src)
191 return -ERRNO_TO_ERROR(errno);
192 while (fgets(line, sizeof(line), src)) {
193 ret = exec_interactive_command(line);
194 if (ret < 0)
195 goto out;
196 }
197 ret = 1;
198 out:
199 fclose(src);
200 return ret;
201 }
202
203 /**
204 * The main function for interactive mode.
205 *
206 * \return Standard.
207 */
208 int com_interactive(void)
209 {
210 char line[255];
211 int ret = 1;
212
213 select_cmdline_parser_init(&select_conf);
214 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
215 if (ret< 0)
216 return ret;
217 ret = read_uid_file();
218 if (ret < 0)
219 return ret;
220 while (read_input_line(line, sizeof(line)) >= 0) {
221 ret = exec_interactive_command(line);
222 if (ret < 0)
223 printf("%s\n", adu_strerror(-ret));
224 fflush(NULL);
225 }
226 return ret;
227 }