Increase default loglevel to 4 (WARN).
[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
35
36 #define INTERACTIVE_COMMAND(name, desc) \
37         static int icom_ ## name (char *line);
38
39 INTERACTIVE_COMMANDS
40
41 #undef INTERACTIVE_COMMAND
42
43 #define INTERACTIVE_COMMAND(_name, _desc) \
44         { \
45         .name = #_name, \
46         .handler = icom_ ## _name, \
47         .desc = _desc \
48         },
49
50 struct interactive_command icmds[] = {
51         INTERACTIVE_COMMANDS
52         {.name  = NULL}
53 };
54
55 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
56
57 static int read_input_line(char *line, size_t size)
58 {
59         return fgets(line, size, stdin)? 1 : -1;
60 }
61
62 static int icom_run(__a_unused char *line)
63 {
64         return run_select_query(admissible_uids, fi);
65 }
66
67 static int icom_help(__a_unused char *line)
68 {
69         struct interactive_command *c;
70
71         FOR_EACH_COMMAND(c)
72                 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
73         return 1;
74 }
75
76 void print_interactive_help(void)
77 {
78         struct interactive_command *c;
79         FOR_EACH_COMMAND(c)
80                 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
81 }
82
83 static int icom_reset(__a_unused char *line)
84 {
85         free_format_info(fi);
86         fi = NULL;
87         free(admissible_uids);
88         admissible_uids = NULL;
89         select_cmdline_parser_init(&select_conf);
90         return 1;
91 }
92
93 static int icom_set(char *line)
94 {
95         struct select_cmdline_parser_params params = {
96                 .override = 1,
97                 .initialize = 0,
98                 .check_required = 1,
99                 .check_ambiguity = 0,
100                 .print_errors = 1
101         };
102         if (!line) {
103                 select_cmdline_parser_dump(stdout, &select_conf);
104                 return 1;
105         }
106
107         free_format_info(fi);
108         fi = NULL;
109         free(admissible_uids);
110         admissible_uids = NULL;
111         return parse_select_options(line, &params, &admissible_uids, &fi);
112 }
113
114 static int exec_interactive_command(char *line)
115 {
116         const char const *delim = "\t\n\f\r\v ";
117         int i;
118         char *cmd, *args;
119         int ret = -E_SYNTAX;
120         size_t len;
121
122         if (!line || !*line)
123                 return 1;
124         len = strlen(line);
125
126         while (len && isspace(line[len - 1])) {
127                 line[len - 1] = '\0';
128                 len--;
129         }
130         if (!len)
131                 return 1;
132         line += strspn(line, delim); /* skip initial whitespace */
133         if (!*line)
134                 return 1;
135         /* OK, we have a non-empty line */
136         if (*line == '#')
137                 return 1;
138         cmd = adu_strdup(line);
139         args = cmd + strcspn(cmd, delim);
140         if (!*args)
141                 args = NULL;
142         else {
143                 *args = '\0';
144                 args++;
145                 /* let p point to the next non-whitespace char */
146                 args += strspn(args, delim);
147                 if (!*args)
148                         args = NULL;
149         }
150         DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
151         for (i = 0; icmds[i].name; i++) {
152                 if (strcmp(icmds[i].name, cmd))
153                         continue;
154                 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
155                 ret = icmds[i].handler(args);
156                 break;
157         }
158         free(cmd);
159         return ret;
160 }
161
162 int com_interactive(void)
163 {
164         char line[255];
165         int ret = 1;
166
167         select_cmdline_parser_init(&select_conf);
168         ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
169         while (read_input_line(line, sizeof(line)) >= 0) {
170                 ret = exec_interactive_command(line);
171                 if (ret < 0)
172                         printf("%s\n", adu_strerror(-ret));
173         }
174         return ret;
175 }