[augeas-devel] augeas: master - Use readline only when stdin is a tty, use getline otherwise

David Lutterkort lutter at fedoraproject.org
Thu Apr 2 22:08:57 UTC 2009


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=eb527714b51daa0f0f73175e820ee41b1451129d
Commit:        eb527714b51daa0f0f73175e820ee41b1451129d
Parent:        ae9dd65582997b0c8e2d060180047db109897660
Author:        Raphael Pinson <raphink at gmail.com>
AuthorDate:    Tue Mar 31 12:02:30 2009 -0700
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Thu Apr 2 14:52:37 2009 -0700

Use readline only when stdin is a tty, use getline otherwise

---
 src/augtool.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/augtool.c b/src/augtool.c
index 0869960..a33b0ba 100644
--- a/src/augtool.c
+++ b/src/augtool.c
@@ -47,15 +47,20 @@ static unsigned int flags = AUG_NONE;
 const char *root = NULL;
 char *loadpath = NULL;
 
-static char *cleanpath(char *path) {
+
+static char *cleanstr(char *path, const char sep) {
     if (path == NULL || strlen(path) == 0)
         return path;
     char *e = path + strlen(path) - 1;
-    while (e != path && (*e == SEP || isspace(*e)))
+    while (e >= path && (*e == sep || isspace(*e)))
         *e-- = '\0';
     return path;
 }
 
+static char *cleanpath(char *path) {
+    return cleanstr(path, SEP);
+}
+
 /*
  * Dup PATH and split it into a directory and basename. The returned value
  * points to the copy of PATH. Adding strlen(PATH)+1 to it gives the
@@ -622,11 +627,17 @@ static int main_loop(void) {
     char *line;
     char *cmd, *args[maxargs];
     int ret = 0;
+    size_t len = 0;
 
     while(1) {
         char *dup_line;
 
-        line = readline("augtool> ");
+        if (isatty(fileno(stdin))) {
+            line = readline("augtool> ");
+        } else if (getline(&line, &len, stdin) == -1) {
+            return ret;
+        }
+        cleanstr(line, '\n');
         if (line == NULL) {
             printf("\n");
             return ret;
@@ -644,7 +655,8 @@ static int main_loop(void) {
             r = run_command(cmd, maxargs, args);
             if (r < 0)
                 ret = -1;
-            add_history(line);
+            if (isatty(fileno(stdin)))
+                add_history(line);
         }
         free(dup_line);
     }




More information about the augeas-devel mailing list