X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=interactive.c;h=1c7562271a0948941c8b5e5ba41f8826dec1d7e9;hp=7b5ca7606e07bd7bef18a4c5ce26ede623cda632;hb=d0f564069d8445e5ffe03c57ff63721f4c47877e;hpb=df11124a676c84be661ab3dc3f255aa864fd79f7 diff --git a/interactive.c b/interactive.c index 7b5ca76..1c75622 100644 --- a/interactive.c +++ b/interactive.c @@ -1,9 +1,18 @@ +/* + * Copyright (C) 2008 Andre Noll + * + * Licensed under the GPL v2. For licencing details see COPYING. + */ + +/** \file interactive.c \brief Commands for interactive mode. */ + #include /* isspace() */ #include "adu.h" #include "format.h" #include "user.h" #include "string.h" +#include "cmdline.h" #include "select.cmdline.h" #include "select.h" #include "error.h" @@ -26,13 +35,16 @@ struct interactive_command { static struct uid_range *admissible_uids; static struct format_info *fi; +/** The set of supported interactive commands. */ #define INTERACTIVE_COMMANDS \ INTERACTIVE_COMMAND(set, "change the current configuration") \ INTERACTIVE_COMMAND(reset, "reset configuration to defaults") \ INTERACTIVE_COMMAND(help, "show list of commands and one-line descriptions") \ - INTERACTIVE_COMMAND(run, "start the query according to the current configuration") + INTERACTIVE_COMMAND(run, "start the query according to the current configuration") \ + INTERACTIVE_COMMAND(source, "read and execute interactive commands from a file") +/** \cond doxygen is not smart enough for this */ #define INTERACTIVE_COMMAND(name, desc) \ static int icom_ ## name (char *line); @@ -51,7 +63,9 @@ struct interactive_command icmds[] = { INTERACTIVE_COMMANDS {.name = NULL} }; +/** \endcond */ +/** Iterate over the list of all interactive commands. */ #define FOR_EACH_COMMAND(c) for (c = icmds; c->name; c++) static int read_input_line(char *line, size_t size) @@ -73,6 +87,9 @@ static int icom_help(__a_unused char *line) return 1; } +/** + * Print the list of commands with short descriptions. + */ void print_interactive_help(void) { struct interactive_command *c; @@ -164,6 +181,30 @@ static int exec_interactive_command(char *line) return ret; } +static int icom_source(char *args) +{ + char line[255]; + FILE *src = fopen(args, "r"); + int ret; + + if (!src) + return -ERRNO_TO_ERROR(errno); + while (fgets(line, sizeof(line), src)) { + ret = exec_interactive_command(line); + if (ret < 0) + goto out; + } + ret = 1; +out: + fclose(src); + return ret; +} + +/** + * The main function for interactive mode. + * + * \return Standard. + */ int com_interactive(void) { char line[255]; @@ -180,6 +221,7 @@ int com_interactive(void) ret = exec_interactive_command(line); if (ret < 0) printf("%s\n", adu_strerror(-ret)); + fflush(NULL); } return ret; }