[augeas-devel] [PATCH] Add --autosave option to augtool (fix ticket #172). Define AUGTOOL_PROMPT as a constant. Do not print (null) lines when using --echo.

raphink at gmail.com raphink at gmail.com
Wed Jan 5 11:52:26 UTC 2011


From: Raphaël Pinson <raphink at gmail.com>

---
 src/augtool.c |   38 ++++++++++++++++++++++++++++++--------
 1 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/src/augtool.c b/src/augtool.c
index 303e765..dbf48e2 100644
--- a/src/augtool.c
+++ b/src/augtool.c
@@ -44,6 +44,7 @@ char *loadpath = NULL;
 const char *inputfile = NULL;
 int echo = 0;
 bool print_version = false;
+bool auto_save = false;
 
 /*
  * General utilities
@@ -79,6 +80,8 @@ struct command_opt_def {
 
 #define CMD_OPT_DEF_LAST { .type = CMD_NONE, .name = NULL }
 
+#define AUGTOOL_PROMPT "augtool> "
+
 /* Handlers return one of these */
 enum command_result {
     CMD_RES_OK,
@@ -1023,6 +1026,7 @@ static void usage(void) {
     fprintf(stderr, "  -I, --include DIR  search DIR for modules; can be given mutiple times\n");
     fprintf(stderr, "  -e, --echo         echo commands when reading from a file\n");
     fprintf(stderr, "  -f, --file FILE    read commands from FILE\n");
+    fprintf(stderr, "  --autosave         automatically save at the end of instructions\n");
     fprintf(stderr, "  --nostdinc         do not search the builtin default directories for modules\n");
     fprintf(stderr, "  --noload           do not load any files into the tree on startup\n");
     fprintf(stderr, "  --noautoload       do not autoload modules from the search path\n");
@@ -1038,7 +1042,8 @@ static void parse_opts(int argc, char **argv) {
         VAL_NO_STDINC = CHAR_MAX + 1,
         VAL_NO_LOAD = VAL_NO_STDINC + 1,
         VAL_NO_AUTOLOAD = VAL_NO_LOAD + 1,
-        VAL_VERSION = VAL_NO_AUTOLOAD + 1
+        VAL_VERSION = VAL_NO_AUTOLOAD + 1,
+        VAL_AUTOSAVE = VAL_VERSION + 1
     };
     struct option options[] = {
         { "help",      0, 0, 'h' },
@@ -1053,6 +1058,7 @@ static void parse_opts(int argc, char **argv) {
         { "noload",    0, 0, VAL_NO_LOAD },
         { "noautoload", 0, 0, VAL_NO_AUTOLOAD },
         { "version",   0, 0, VAL_VERSION },
+        { "autosave",  0, 0, VAL_AUTOSAVE },
         { 0, 0, 0, 0}
     };
     int idx;
@@ -1096,6 +1102,9 @@ static void parse_opts(int argc, char **argv) {
             flags |= AUG_NO_MODL_AUTOLOAD;
             print_version = true;
             break;
+        case VAL_AUTOSAVE:
+            auto_save = true;
+            break;
         default:
             usage();
             break;
@@ -1152,6 +1161,7 @@ static int main_loop(void) {
     size_t len = 0;
     char inputline [128];
     enum command_result code;
+    bool end_reached = false;
 
     FILE *fp;
     if (inputfile) {
@@ -1171,19 +1181,31 @@ static int main_loop(void) {
                 line = inputline;
             }
             if (echo)
-                printf("augtool> %s", line);
+                printf("%s%s", AUGTOOL_PROMPT, line);
         } else if (isatty(fileno(stdin))) {
-            line = readline("augtool> ");
+            line = readline(AUGTOOL_PROMPT);
         } else {
             if (getline(&line, &len, stdin) == -1)
-                return ret;
-            if (echo)
-                printf("augtool> %s", line);
+                line = NULL;
+            if (echo && line != NULL)
+                printf("%s%s", AUGTOOL_PROMPT, line);
         }
 
-        cleanstr(line, '\n');
         if (line == NULL) {
-            printf("\n");
+            if (auto_save) {
+                line = "save";
+                if (echo)
+                   printf("%s%s\n", AUGTOOL_PROMPT, line);
+                // Avoid looping here
+                auto_save = false;
+            } else {
+                end_reached = true;
+            }
+        }
+        cleanstr(line, '\n');
+        if (end_reached) {
+            if (isatty(fileno(stdin)))
+                printf("\n");
             return ret;
         }
         if (*line == '\0' || *line == '#')
-- 
1.7.0.4




More information about the augeas-devel mailing list