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