OSDN Git Service

Fix top -b.
authorElliott Hughes <enh@google.com>
Sat, 16 Apr 2016 01:38:53 +0000 (18:38 -0700)
committerRob Landley <rob@landley.net>
Sun, 17 Apr 2016 00:58:40 +0000 (19:58 -0500)
Batch mode should never output terminal escape sequences, should ignore
the keyboard, and should include a gap between datasets.

toys/posix/ps.c

index 26b4a4e..a1e1981 100644 (file)
@@ -1144,6 +1144,8 @@ static int header_line(int line, int rev)
 {
   if (!line) return 0;
 
+  if (toys.optflags&FLAG_b) rev = 0;
+
   printf("%s%*.*s%s\r\n", rev ? "\033[7m" : "",
     (toys.optflags&FLAG_b) ? 0 : -TT.width, TT.width, toybuf,
     rev ? "\033[0m" : "");
@@ -1339,7 +1341,8 @@ static void top_common(
         *pos = 0;
         lines = header_line(lines, 1);
       }
-      if (!recalc) printf("\033[%dH\033[J", 1+TT.height-lines);
+      if (!recalc && !(toys.optflags&FLAG_b))
+        printf("\033[%dH\033[J", 1+TT.height-lines);
       recalc = 1;
 
       for (i = 0; i<lines && i+topoff<mix.count; i++) {
@@ -1357,6 +1360,14 @@ static void top_common(
       if (timeout<=now) timeout = new.whence+TT.top.d;
       if (timeout<=now || timeout>now+TT.top.d) timeout = now+TT.top.d;
 
+      // In batch mode, we ignore the keyboard.
+      if (toys.optflags&FLAG_b) {
+        msleep(timeout-now);
+        // Make an obvious gap between datasets.
+        xputs("\n\n\n");
+        continue;
+      }
+
       i = scan_key_getsize(scratch, timeout-now, &TT.width, &TT.height);
       if (i==-1 || i==3 || toupper(i)=='Q') {
         done++;