flush output after each command in interactive mode.
[adu.git] / interactive.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file interactive.c \brief Commands for interactive mode. */
8
9 #include <ctype.h> /* isspace() */
10
11 #include "adu.h"
12 #include "format.h"
13 #include "user.h"
14 #include "string.h"
15 #include "select.cmdline.h"
16 #include "select.h"
17 #include "error.h"
18
19 /**
20  * Describes one valid command for interactive mode.
21  *
22  * When invoked in interactive mode, adu reads commands from stdin. There's a
23  * static array of all such commands.
24  */
25 struct interactive_command {
26         /** The name of the command. */
27         const char *name;
28         /** Pointer to The function that is being executed. */
29         int (*handler)(char *);
30         /** Help text. */
31         const char *desc;
32 };
33
34 static struct uid_range *admissible_uids;
35 static struct format_info *fi;
36
37 /** The set of supported interactive commands. */
38 #define INTERACTIVE_COMMANDS \
39         INTERACTIVE_COMMAND(set, "change the current configuration") \
40         INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \
41         INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \
42         INTERACTIVE_COMMAND(run, "start the query according to the current configuration") \
43         INTERACTIVE_COMMAND(source, "read and execute interactive commands from a file")
44
45
46 /** \cond doxygen is not smart enough for this */
47 #define INTERACTIVE_COMMAND(name, desc) \
48         static int icom_ ## name (char *line);
49
50 INTERACTIVE_COMMANDS
51
52 #undef INTERACTIVE_COMMAND
53
54 #define INTERACTIVE_COMMAND(_name, _desc) \
55         { \
56         .name = #_name, \
57         .handler = icom_ ## _name, \
58         .desc = _desc \
59         },
60
61 struct interactive_command icmds[] = {
62         INTERACTIVE_COMMANDS
63         {.name  = NULL}
64 };
65 /** \endcond */
66
67 /** Iterate over the list of all interactive commands. */
68 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
69
70 static int read_input_line(char *line, size_t size)
71 {
72         return fgets(line, size, stdin)? 1 : -1;
73 }
74
75 static int icom_run(__a_unused char *line)
76 {
77         return run_select_query(admissible_uids, fi);
78 }
79
80 static int icom_help(__a_unused char *line)
81 {
82         struct interactive_command *c;
83
84         FOR_EACH_COMMAND(c)
85                 fprintf(stdout, "%s\t%s\n", c->name, c->desc);
86         return 1;
87 }
88
89 /**
90  * Print the list of commands with short descriptions.
91  */
92 void print_interactive_help(void)
93 {
94         struct interactive_command *c;
95         FOR_EACH_COMMAND(c)
96                 fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
97 }
98
99 static int icom_reset(__a_unused char *line)
100 {
101         NOTICE_LOG("resetting configuration to default\n");
102         free_format_info(fi);
103         fi = NULL;
104         free(admissible_uids);
105         admissible_uids = NULL;
106         select_cmdline_parser_init(&select_conf);
107         return parse_select_options(NULL, NULL, &admissible_uids, &fi);
108 }
109
110 static int icom_set(char *line)
111 {
112         int ret;
113         struct select_cmdline_parser_params params = {
114                 .override = 1,
115                 .initialize = 0,
116                 .check_required = 1,
117                 .check_ambiguity = 0,
118                 .print_errors = 1
119         };
120         if (!line) {
121                 select_cmdline_parser_dump(stdout, &select_conf);
122                 return 1;
123         }
124
125         free_format_info(fi);
126         fi = NULL;
127         free(admissible_uids);
128         admissible_uids = NULL;
129         ret = parse_select_options(line, &params, &admissible_uids, &fi);
130         if (ret >= 0)
131                 return ret;
132         return icom_reset(NULL);
133 }
134
135 static int exec_interactive_command(char *line)
136 {
137         const char const *delim = "\t\n\f\r\v ";
138         int i;
139         char *cmd, *args;
140         int ret = -E_SYNTAX;
141         size_t len;
142
143         if (!line || !*line)
144                 return 1;
145         len = strlen(line);
146
147         while (len && isspace(line[len - 1])) {
148                 line[len - 1] = '\0';
149                 len--;
150         }
151         if (!len)
152                 return 1;
153         line += strspn(line, delim); /* skip initial whitespace */
154         if (!*line)
155                 return 1;
156         /* OK, we have a non-empty line */
157         if (*line == '#')
158                 return 1;
159         cmd = adu_strdup(line);
160         args = cmd + strcspn(cmd, delim);
161         if (!*args)
162                 args = NULL;
163         else {
164                 *args = '\0';
165                 args++;
166                 /* let args point to the next non-whitespace char */
167                 args += strspn(args, delim);
168                 if (!*args)
169                         args = NULL;
170         }
171         DEBUG_LOG("name: %s, args: %s.\n", cmd, args);
172         for (i = 0; icmds[i].name; i++) {
173                 if (strcmp(icmds[i].name, cmd))
174                         continue;
175                 INFO_LOG("exec cmd: %s, args: %s\n", cmd, args);
176                 ret = icmds[i].handler(args);
177                 break;
178         }
179         free(cmd);
180         return ret;
181 }
182
183 static int icom_source(char *args)
184 {
185         char line[255];
186         FILE *src = fopen(args, "r");
187         int ret;
188
189         if (!src)
190                 return -ERRNO_TO_ERROR(errno);
191         while (fgets(line, sizeof(line), src)) {
192                 ret = exec_interactive_command(line);
193                 if (ret < 0)
194                         goto out;
195         }
196         ret = 1;
197 out:
198         fclose(src);
199         return ret;
200 }
201
202 /**
203  * The main function for interactive mode.
204  *
205  * \return Standard.
206  */
207 int com_interactive(void)
208 {
209         char line[255];
210         int ret = 1;
211
212         select_cmdline_parser_init(&select_conf);
213         ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
214         if (ret< 0)
215                 return ret;
216         ret = read_uid_file();
217         if (ret < 0)
218                 return ret;
219         while (read_input_line(line, sizeof(line)) >= 0) {
220                 ret = exec_interactive_command(line);
221                 if (ret < 0)
222                         printf("%s\n", adu_strerror(-ret));
223                 fflush(NULL);
224         }
225         return ret;
226 }