Fix a bug in exec_interactive_command().
[adu.git] / interactive.c
1 #include "adu.h"
2 #include "format.h"
3 #include "select.h"
4 #include "string.h"
5 #include "error.h"
6 #include "cmdline.h"
7
8 struct interactive_command {
9 const char *name;
10 int (*handler)(char *);
11 const char *desc;
12 };
13
14 static struct uid_range *admissible_uids;
15 static struct format_info *fi;
16
17 #define INTERACTIVE_COMMANDS \
18 INTERACTIVE_COMMAND(dump, "dump the current configuration") \
19 INTERACTIVE_COMMAND(set, "change the current configuration") \
20 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
21 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
22 INTERACTIVE_COMMAND(run, "start the query according to the current options")
23
24
25 #define INTERACTIVE_COMMAND(name, desc) \
26 static int icom_ ## name (char *line);
27
28 INTERACTIVE_COMMANDS
29
30 #undef INTERACTIVE_COMMAND
31
32 #define INTERACTIVE_COMMAND(_name, _desc) \
33 { \
34 .name = #_name, \
35 .handler = icom_ ## _name, \
36 .desc = _desc \
37 },
38
39 struct interactive_command icmds[] = {
40 INTERACTIVE_COMMANDS
41 {.name = NULL}
42 };
43
44 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
45
46 static int read_input_line(char *line, size_t size)
47 {
48 return fgets(line, size, stdin)? 1 : -1;
49 }
50
51 static int icom_run(__a_unused char *line)
52 {
53 return run_select_query(admissible_uids, fi);
54 }
55
56 static int icom_help(__a_unused char *line)
57 {
58 struct interactive_command *c;
59
60 FOR_EACH_COMMAND(c)
61 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
62 return 1;
63 }
64
65 void print_interactive_help(void)
66 {
67 icom_help(NULL);
68 }
69
70 static int icom_reset(__a_unused char *line)
71 {
72 select_cmdline_parser_init(&select_conf);
73 return 1;
74 }
75
76 static int icom_set(char *line)
77 {
78 struct select_cmdline_parser_params params = {
79 .override = 1,
80 .initialize = 0,
81 .check_required = 1,
82 .check_ambiguity = 0,
83 .print_errors = 1
84 };
85 return parse_select_options(line, &params, &admissible_uids, &fi);
86 }
87
88 static int icom_dump(__a_unused char *line)
89 {
90 select_cmdline_parser_dump(stdout, &select_conf);
91 return 1;
92 }
93
94 static int exec_interactive_command(char *line)
95 {
96 const char const *delim = "\t\n ";
97 int i;
98 char *cmd = adu_strdup(line + strspn(line, delim));
99 char *p = cmd + strcspn(cmd, delim);
100 int ret = -E_SYNTAX;
101
102 if (*p == '\0')
103 p = NULL;
104 else {
105 *p = '\0';
106 p++;
107 }
108 for (i = 0; icmds[i].name; i++) {
109 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
110 if (strcmp(icmds[i].name, cmd))
111 continue;
112 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
113 ret = icmds[i].handler(p);
114 break;
115 }
116 free(cmd);
117 return ret;
118 }
119
120 int com_interactive(void)
121 {
122 char line[255];
123 int ret = 1;
124
125 select_cmdline_parser_init(&select_conf);
126 while (read_input_line(line, sizeof(line)) >= 0) {
127 size_t len = strlen(line);
128 if (!len)
129 continue;
130 if (line[len - 1] == '\n')
131 line[len - 1] = '\0';
132 ret = exec_interactive_command(line);
133 if (ret < 0)
134 printf("%s\n", adu_strerror(-ret));
135 }
136 return ret;
137 }