Add rudimentary interactive support.
[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 };
13
14 #define INTERACTIVE_COMMANDS \
15 INTERACTIVE_COMMAND(dump) \
16 INTERACTIVE_COMMAND(set) \
17 INTERACTIVE_COMMAND(def) \
18
19 #define INTERACTIVE_COMMAND(name) \
20 static int icom_ ## name (char *line);
21
22 INTERACTIVE_COMMANDS
23
24 #undef INTERACTIVE_COMMAND
25
26 #define INTERACTIVE_COMMAND(_name) \
27 { \
28 .name = #_name, \
29 .handler = icom_ ## _name \
30 },
31
32 struct interactive_command icmds[] = {
33 INTERACTIVE_COMMANDS
34 {.name = NULL}
35 };
36
37 static int read_input_line(char *line, size_t size)
38 {
39 return fgets(line, size, stdin)? 1 : -1;
40 }
41
42 static int icom_def(__a_unused char *line)
43 {
44 select_cmdline_parser_init(&select_conf);
45 return 1;
46 }
47
48 static int icom_set(char *line)
49 {
50 struct select_cmdline_parser_params params = {
51 .override = 1,
52 .initialize = 0,
53 .check_required = 1,
54 .check_ambiguity = 0,
55 .print_errors = 1
56 };
57 return select_cmdline_parser_string_ext(line, &select_conf, "select",
58 &params)?
59 -E_SYNTAX : 1;
60 }
61
62 static int icom_dump(__a_unused char *line)
63 {
64 ERROR_LOG("dump: %s\n", select_conf.format_arg);
65 select_cmdline_parser_dump(stdout, &select_conf);
66 return 1;
67 }
68
69 static int exec_interactive_command(char *line)
70 {
71 const char const *delim = "\t\n ";
72 int i;
73 char *cmd = adu_strdup(line + strspn(line, delim));
74 char *p = cmd + strcspn(cmd, delim);
75 int ret = -E_SYNTAX;
76
77 *p = '\0';
78 p++;
79 for (i = 0; icmds[i].name; i++) {
80 ERROR_LOG("name: %s, cmd: %s.\n", icmds[i].name, cmd);
81 if (strcmp(icmds[i].name, cmd))
82 continue;
83 ERROR_LOG("exec cmd: %s, args: %s\n", cmd, p);
84 ret = icmds[i].handler(p);
85 break;
86 }
87 free(cmd);
88 return ret;
89 }
90
91 int com_interactive(void)
92 {
93 char line[255];
94 int ret = 1;
95
96 while (read_input_line(line, sizeof(line)) >= 0) {
97 size_t len = strlen(line);
98 if (!len)
99 continue;
100 if (line[len - 1] == '\n')
101 line[len - 1] = '\0';
102 ret = exec_interactive_command(line);
103 if (ret < 0)
104 printf("%s\n", adu_strerror(-ret));
105 }
106 return ret;
107 }