5 #include "select.cmdline.h"
7 struct select_args_info select_conf;
9 struct interactive_command {
11 int (*handler)(char *);
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") \
22 #define INTERACTIVE_COMMAND(name, desc) \
23 static int icom_ ## name (char *line);
27 #undef INTERACTIVE_COMMAND
29 #define INTERACTIVE_COMMAND(_name, _desc) \
32 .handler = icom_ ## _name, \
36 struct interactive_command icmds[] = {
41 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
43 static int read_input_line(char *line, size_t size)
45 return fgets(line, size, stdin)? 1 : -1;
48 static int icom_help(__a_unused char *line)
50 struct interactive_command *c;
53 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
57 static int icom_reset(__a_unused char *line)
59 select_cmdline_parser_init(&select_conf);
63 static struct uid_range *admissible_uids;
65 static int icom_set(char *line)
67 struct select_cmdline_parser_params params = {
74 if (select_cmdline_parser_string_ext(line, &select_conf, "select",
77 return parse_uid_arg(select_conf.uid_arg, &admissible_uids);
80 static int icom_dump(__a_unused char *line)
82 ERROR_LOG("dump: %s\n", select_conf.format_arg);
83 select_cmdline_parser_dump(stdout, &select_conf);
87 static int exec_interactive_command(char *line)
89 const char const *delim = "\t\n ";
91 char *cmd = adu_strdup(line + strspn(line, delim));
92 char *p = cmd + strcspn(cmd, delim);
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))
101 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
102 ret = icmds[i].handler(p);
109 int com_interactive(void)
114 while (read_input_line(line, sizeof(line)) >= 0) {
115 size_t len = strlen(line);
118 if (line[len - 1] == '\n')
119 line[len - 1] = '\0';
120 ret = exec_interactive_command(line);
122 printf("%s\n", adu_strerror(-ret));