Add format string doku, simplify format string handling.
[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 static int icom_reset(__a_unused char *line)
66 {
67 select_cmdline_parser_init(&select_conf);
68 return 1;
69 }
70
71 static int icom_set(char *line)
72 {
73 struct select_cmdline_parser_params params = {
74 .override = 1,
75 .initialize = 0,
76 .check_required = 1,
77 .check_ambiguity = 0,
78 .print_errors = 1
79 };
80 return parse_select_options(line, &params, &admissible_uids, &fi);
81 }
82
83 static int icom_dump(__a_unused char *line)
84 {
85 select_cmdline_parser_dump(stdout, &select_conf);
86 return 1;
87 }
88
89 static int exec_interactive_command(char *line)
90 {
91 const char const *delim = "\t\n ";
92 int i;
93 char *cmd = adu_strdup(line + strspn(line, delim));
94 char *p = cmd + strcspn(cmd, delim);
95 int ret = -E_SYNTAX;
96
97 *p = '\0';
98 p++;
99 for (i = 0; icmds[i].name; i++) {
100 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
101 if (strcmp(icmds[i].name, cmd))
102 continue;
103 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
104 ret = icmds[i].handler(p);
105 break;
106 }
107 free(cmd);
108 return ret;
109 }
110
111 int com_interactive(void)
112 {
113 char line[255];
114 int ret = 1;
115
116 select_cmdline_parser_init(&select_conf);
117 while (read_input_line(line, sizeof(line)) >= 0) {
118 size_t len = strlen(line);
119 if (!len)
120 continue;
121 if (line[len - 1] == '\n')
122 line[len - 1] = '\0';
123 ret = exec_interactive_command(line);
124 if (ret < 0)
125 printf("%s\n", adu_strerror(-ret));
126 }
127 return ret;
128 }