]> git.tuebingen.mpg.de Git - adu.git/blobdiff - interactive.c
Add \brief command to file header to make doxygen happy.
[adu.git] / interactive.c
index 62ff5edb774ded49b643e14c4a94f72336f4115c..4c0392784604d4af06701dcdee355197d7ba868a 100644 (file)
@@ -1,15 +1,31 @@
+/*
+ * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
 #include <ctype.h> /* isspace() */
 
 #include "adu.h"
 #include "format.h"
-#include "select.h"
+#include "user.h"
 #include "string.h"
+#include "select.cmdline.h"
+#include "select.h"
 #include "error.h"
-#include "cmdline.h"
 
+/**
+ * Describes one valid command for interactive mode.
+ *
+ * When invoked in interactive mode, adu reads commands from stdin. There's a
+ * static array of all such commands.
+ */
 struct interactive_command {
+       /** The name of the command. */
        const char *name;
+       /** Pointer to The function that is being executed. */
        int (*handler)(char *);
+       /** Help text. */
        const char *desc;
 };
 
@@ -20,7 +36,8 @@ static struct format_info *fi;
        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")
 
 
 #define INTERACTIVE_COMMAND(name, desc) \
@@ -65,21 +82,25 @@ static int icom_help(__a_unused char *line)
 
 void print_interactive_help(void)
 {
-       icom_help(NULL);
+       struct interactive_command *c;
+       FOR_EACH_COMMAND(c)
+               fprintf(stdout, "\t%s\t%s\n", c->name, c->desc);
 }
 
 static int icom_reset(__a_unused char *line)
 {
+       NOTICE_LOG("resetting configuration to default\n");
        free_format_info(fi);
        fi = NULL;
        free(admissible_uids);
        admissible_uids = NULL;
        select_cmdline_parser_init(&select_conf);
-       return 1;
+       return parse_select_options(NULL, NULL, &admissible_uids, &fi);
 }
 
 static int icom_set(char *line)
 {
+       int ret;
        struct select_cmdline_parser_params params = {
                .override = 1,
                .initialize = 0,
@@ -96,7 +117,10 @@ static int icom_set(char *line)
        fi = NULL;
        free(admissible_uids);
        admissible_uids = NULL;
-       return parse_select_options(line, &params, &admissible_uids, &fi);
+       ret = parse_select_options(line, &params, &admissible_uids, &fi);
+       if (ret >= 0)
+               return ret;
+       return icom_reset(NULL);
 }
 
 static int exec_interactive_command(char *line)
@@ -130,7 +154,7 @@ static int exec_interactive_command(char *line)
        else {
                *args = '\0';
                args++;
-               /* let p point to the next non-whitespace char */
+               /* let args point to the next non-whitespace char */
                args += strspn(args, delim);
                if (!*args)
                        args = NULL;
@@ -147,6 +171,25 @@ 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;
+}
+
 int com_interactive(void)
 {
        char line[255];
@@ -154,6 +197,11 @@ int com_interactive(void)
 
        select_cmdline_parser_init(&select_conf);
        ret = parse_select_options(NULL, NULL, &admissible_uids, &fi);
+       if (ret< 0)
+               return ret;
+       ret = read_uid_file();
+       if (ret < 0)
+               return ret;
        while (read_input_line(line, sizeof(line)) >= 0) {
                ret = exec_interactive_command(line);
                if (ret < 0)