2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file interactive.c \brief Commands for interactive mode. */
9 #include <ctype.h> /* isspace() */
15 #include "select.cmdline.h"
20 * Describes one valid command for interactive mode.
22 * When invoked in interactive mode, adu reads commands from stdin. There's a
23 * static array of all such commands.
25 struct interactive_command
{
26 /** The name of the command. */
28 /** Pointer to The function that is being executed. */
29 int (*handler
)(char *);
34 static struct uid_range
*admissible_uids
;
35 static struct format_info
*fi
;
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")
46 /** \cond doxygen is not smart enough for this */
47 #define INTERACTIVE_COMMAND(name, desc) \
48 static int icom_ ## name (char *line);
52 #undef INTERACTIVE_COMMAND
54 #define INTERACTIVE_COMMAND(_name, _desc) \
57 .handler = icom_ ## _name, \
61 struct interactive_command icmds
[] = {
67 /** Iterate over the list of all interactive commands. */
68 #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++)
70 static int read_input_line(char *line
, size_t size
)
72 return fgets(line
, size
, stdin
)? 1 : -1;
75 static int icom_run(__a_unused
char *line
)
77 return run_select_query(admissible_uids
, fi
);
80 static int icom_help(__a_unused
char *line
)
82 struct interactive_command
*c
;
85 fprintf(stdout
, "%s\t%s\n", c
->name
, c
->desc
);
90 * Print the list of commands with short descriptions.
92 void print_interactive_help(void)
94 struct interactive_command
*c
;
96 fprintf(stdout
, "\t%s\t%s\n", c
->name
, c
->desc
);
99 static int icom_reset(__a_unused
char *line
)
101 NOTICE_LOG("resetting configuration to default\n");
102 free_format_info(fi
);
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
);
110 static int icom_set(char *line
)
113 struct select_cmdline_parser_params params
= {
117 .check_ambiguity
= 0,
121 select_cmdline_parser_dump(stdout
, &select_conf
);
125 free_format_info(fi
);
127 free(admissible_uids
);
128 admissible_uids
= NULL
;
129 ret
= parse_select_options(line
, ¶ms
, &admissible_uids
, &fi
);
132 return icom_reset(NULL
);
135 static int exec_interactive_command(char *line
)
137 const char const *delim
= "\t\n\f\r\v ";
147 while (len
&& isspace(line
[len
- 1])) {
148 line
[len
- 1] = '\0';
153 line
+= strspn(line
, delim
); /* skip initial whitespace */
156 /* OK, we have a non-empty line */
159 cmd
= adu_strdup(line
);
160 args
= cmd
+ strcspn(cmd
, delim
);
166 /* let args point to the next non-whitespace char */
167 args
+= strspn(args
, delim
);
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
))
175 INFO_LOG("exec cmd: %s, args: %s\n", cmd
, args
);
176 ret
= icmds
[i
].handler(args
);
183 static int icom_source(char *args
)
186 FILE *src
= fopen(args
, "r");
190 return -ERRNO_TO_ERROR(errno
);
191 while (fgets(line
, sizeof(line
), src
)) {
192 ret
= exec_interactive_command(line
);
203 * The main function for interactive mode.
207 int com_interactive(void)
212 select_cmdline_parser_init(&select_conf
);
213 ret
= parse_select_options(NULL
, NULL
, &admissible_uids
, &fi
);
216 ret
= read_uid_file();
219 while (read_input_line(line
, sizeof(line
)) >= 0) {
220 ret
= exec_interactive_command(line
);
222 printf("%s\n", adu_strerror(-ret
));