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