Make it easier to print the help for select options.
[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 *p = '\0';
103 p++;
104 for (i = 0; icmds[i].name; i++) {
105 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
106 if (strcmp(icmds[i].name, cmd))
107 continue;
108 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
109 ret = icmds[i].handler(p);
110 break;
111 }
112 free(cmd);
113 return ret;
114 }
115
116 int com_interactive(void)
117 {
118 char line[255];
119 int ret = 1;
120
121 select_cmdline_parser_init(&select_conf);
122 while (read_input_line(line, sizeof(line)) >= 0) {
123 size_t len = strlen(line);
124 if (!len)
125 continue;
126 if (line[len - 1] == '\n')
127 line[len - 1] = '\0';
128 ret = exec_interactive_command(line);
129 if (ret < 0)
130 printf("%s\n", adu_strerror(-ret));
131 }
132 return ret;
133 }