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