[augeas-devel] [PATCH 2/2] Refactor augtool.c to use only readline

raphink at gmail.com raphink at gmail.com
Mon Jan 10 10:53:05 UTC 2011


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

---
 src/augtool.c |   43 +++++++++++--------------------------------
 1 files changed, 11 insertions(+), 32 deletions(-)

diff --git a/src/augtool.c b/src/augtool.c
index a889993..7a6a2b3 100644
--- a/src/augtool.c
+++ b/src/augtool.c
@@ -1157,40 +1157,22 @@ run_command(const char *line) {
 static int main_loop(void) {
     char *line = NULL;
     int ret = 0;
-    size_t len = 0;
     char inputline [128];
     enum command_result code;
     bool end_reached = false;
     bool get_line = true;
+    // make readline silent by default
+    rl_outstream = fopen("/dev/null", "w");
 
-    FILE *fp;
-    if (inputfile) {
-       fp = fopen(inputfile, "r");
+    if (inputfile)
+        freopen(inputfile, "r", stdin);
 
-       if (fp == NULL) {
-           fprintf(stderr, "Failed to open input file %s.\n", inputfile);
-           return -1;
-       }
-    }
+    if (echo || isatty(fileno(stdin)))
+        rl_outstream = NULL;
 
     while(1) {
         if (get_line) {
-            if (inputfile) {
-                if (fgets(inputline, sizeof(inputline), fp) == NULL) {
-                    line = NULL;
-                } else {
-                    line = inputline;
-                }
-                if (echo)
-                    printf(AUGTOOL_PROMPT "%s", line);
-            } else if (isatty(fileno(stdin))) {
-                line = readline(AUGTOOL_PROMPT);
-            } else {
-                if (getline(&line, &len, stdin) == -1)
-                    line = NULL;
-                if (echo && line != NULL)
-                    printf(AUGTOOL_PROMPT "%s", line);
-            }
+            line = readline(AUGTOOL_PROMPT);
         } else {
             line = NULL;
         }
@@ -1199,24 +1181,21 @@ static int main_loop(void) {
             if (auto_save) {
                 strncpy(inputline, "save", sizeof(inputline));
                 line = inputline;
-                if (echo) {
-                   printf(AUGTOOL_PROMPT "%s\n", line);
-                } else if (isatty(fileno(stdin))) {
-                   printf ("%s\n", line);
-                }
-                // Avoid looping here
                 auto_save = false;
             } else {
                 end_reached = true;
             }
             get_line = false;
         }
+
         cleanstr(line, '\n');
+
         if (end_reached) {
-            if (isatty(fileno(stdin)))
+            if (echo || isatty(fileno(stdin)))
                 printf("\n");
             return ret;
         }
+
         if (*line == '\0' || *line == '#')
             continue;
 
-- 
1.7.0.4




More information about the augeas-devel mailing list