Make --uid take a comma-separated list of uids and move it to select.ggo.
[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 select_args_info select_conf;
8
9 struct interactive_command {
10 const char *name;
11 int (*handler)(char *);
12 const char *desc;
13 };
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
21
22 #define INTERACTIVE_COMMAND(name, desc) \
23 static int icom_ ## name (char *line);
24
25 INTERACTIVE_COMMANDS
26
27 #undef INTERACTIVE_COMMAND
28
29 #define INTERACTIVE_COMMAND(_name, _desc) \
30 { \
31 .name = #_name, \
32 .handler = icom_ ## _name, \
33 .desc = _desc \
34 },
35
36 struct interactive_command icmds[] = {
37 INTERACTIVE_COMMANDS
38 {.name = NULL}
39 };
40
41 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
42
43 static int read_input_line(char *line, size_t size)
44 {
45 return fgets(line, size, stdin)? 1 : -1;
46 }
47
48 static int icom_help(__a_unused char *line)
49 {
50 struct interactive_command *c;
51
52 FOR_EACH_COMMAND(c)
53 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
54 return 1;
55 }
56
57 static int icom_reset(__a_unused char *line)
58 {
59 select_cmdline_parser_init(&select_conf);
60 return 1;
61 }
62
63 static struct uid_range *admissible_uids;
64
65 static int icom_set(char *line)
66 {
67 struct select_cmdline_parser_params params = {
68 .override = 1,
69 .initialize = 0,
70 .check_required = 1,
71 .check_ambiguity = 0,
72 .print_errors = 1
73 };
74 if (select_cmdline_parser_string_ext(line, &select_conf, "select",
75 &params))
76 return -E_SYNTAX;
77 return parse_uid_arg(select_conf.uid_arg, &admissible_uids);
78 }
79
80 static int icom_dump(__a_unused char *line)
81 {
82 ERROR_LOG("dump: %s\n", select_conf.format_arg);
83 select_cmdline_parser_dump(stdout, &select_conf);
84 return 1;
85 }
86
87 static int exec_interactive_command(char *line)
88 {
89 const char const *delim = "\t\n ";
90 int i;
91 char *cmd = adu_strdup(line + strspn(line, delim));
92 char *p = cmd + strcspn(cmd, delim);
93 int ret = -E_SYNTAX;
94
95 *p = '\0';
96 p++;
97 for (i = 0; icmds[i].name; i++) {
98 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
99 if (strcmp(icmds[i].name, cmd))
100 continue;
101 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
102 ret = icmds[i].handler(p);
103 break;
104 }
105 free(cmd);
106 return ret;
107 }
108
109 int com_interactive(void)
110 {
111 char line[255];
112 int ret = 1;
113
114 while (read_input_line(line, sizeof(line)) >= 0) {
115 size_t len = strlen(line);
116 if (!len)
117 continue;
118 if (line[len - 1] == '\n')
119 line[len - 1] = '\0';
120 ret = exec_interactive_command(line);
121 if (ret < 0)
122 printf("%s\n", adu_strerror(-ret));
123 }
124 return ret;
125 }