Add man page, README, INSTALL, generate index.html.
[adu.git] / interactive.c
1 #include <ctype.h> /* isspace() */
2
3 #include "adu.h"
4 #include "format.h"
5 #include "select.h"
6 #include "string.h"
7 #include "error.h"
8 #include "cmdline.h"
9
10 struct interactive_command {
11 const char *name;
12 int (*handler)(char *);
13 const char *desc;
14 };
15
16 static struct uid_range *admissible_uids;
17 static struct format_info *fi;
18
19 #define INTERACTIVE_COMMANDS \
20 INTERACTIVE_COMMAND(set, "change the current configuration") \
21 INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
22 INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
23 INTERACTIVE_COMMAND(run, "start the query according to the current configuration")
24
25
26 #define INTERACTIVE_COMMAND(name, desc) \
27 static int icom_ ## name (char *line);
28
29 INTERACTIVE_COMMANDS
30
31 #undef INTERACTIVE_COMMAND
32
33 #define INTERACTIVE_COMMAND(_name, _desc) \
34 { \
35 .name = #_name, \
36 .handler = icom_ ## _name, \
37 .desc = _desc \
38 },
39
40 struct interactive_command icmds[] = {
41 INTERACTIVE_COMMANDS
42 {.name = NULL}
43 };
44
45 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
46
47 static int read_input_line(char *line, size_t size)
48 {
49 return fgets(line, size, stdin)? 1 : -1;
50 }
51
52 static int icom_run(__a_unused char *line)
53 {
54 return run_select_query(admissible_uids, fi);
55 }
56
57 static int icom_help(__a_unused char *line)
58 {
59 struct interactive_command *c;
60
61 FOR_EACH_COMMAND(c)
62 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
63 return 1;
64 }
65
66 void print_interactive_help(void)
67 {
68 struct interactive_command *c;
69 FOR_EACH_COMMAND(c)
70 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
71 }
72
73 static int icom_reset(__a_unused char *line)
74 {
75 free_format_info(fi);
76 fi = NULL;
77 free(admissible_uids);
78 admissible_uids = NULL;
79 select_cmdline_parser_init(&select_conf);
80 return 1;
81 }
82
83 static int icom_set(char *line)
84 {
85 struct select_cmdline_parser_params params = {
86 .override = 1,
87 .initialize = 0,
88 .check_required = 1,
89 .check_ambiguity = 0,
90 .print_errors = 1
91 };
92 if (!line) {
93 select_cmdline_parser_dump(stdout, &select_conf);
94 return 1;
95 }
96
97 free_format_info(fi);
98 fi = NULL;
99 free(admissible_uids);
100 admissible_uids = NULL;
101 return parse_select_options(line, &params, &admissible_uids, &fi);
102 }
103
104 static int exec_interactive_command(char *line)
105 {
106 const char const *delim = "\t\n\f\r\v ";
107 int i;
108 char *cmd, *args;
109 int ret = -E_SYNTAX;
110 size_t len;
111
112 if (!line || !*line)
113 return 1;
114 len = strlen(line);
115
116 while (len && isspace(line[len - 1])) {
117 line[len - 1] = '\0';
118 len--;
119 }
120 if (!len)
121 return 1;
122 line += strspn(line, delim); /* skip initial whitespace */
123 if (!*line)
124 return 1;
125 /* OK, we have a non-empty line */
126 if (*line == '#')
127 return 1;
128 cmd = adu_strdup(line);
129 args = cmd + strcspn(cmd, delim);
130 if (!*args)
131 args = NULL;
132 else {
133 *args = '\0';
134 args++;
135 /* let p point to the next non-whitespace char */
136 args += strspn(args, delim);
137 if (!*args)
138 args = NULL;
139 }
140 DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
141 for (i = 0; icmds[i].name; i++) {
142 if (strcmp(icmds[i].name, cmd))
143 continue;
144 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
145 ret = icmds[i].handler(args);
146 break;
147 }
148 free(cmd);
149 return ret;
150 }
151
152 int com_interactive(void)
153 {
154 char line[255];
155 int ret = 1;
156
157 select_cmdline_parser_init(&select_conf);
158 ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
159 while (read_input_line(line, sizeof(line)) >= 0) {
160 ret = exec_interactive_command(line);
161 if (ret < 0)
162 printf("%s\n", adu_strerror(-ret));
163 }
164 return ret;
165 }