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