Welcome to little lamb

Code » anopa » commit 5392d06

Tweak how --double-output is handled

author Olivier Brunel
2015-07-26 16:39:24 UTC
committer Olivier Brunel
2015-08-07 17:24:47 UTC
parent 7dd16fe26ab05e49a1a3288711d4933532fc5a8a

Tweak how --double-output is handled

Remove aa_init_output() to instead do the istty() calls when needed, and
simply have a aa_output_setboth() to enable(/disable) double output mode.

Makes things a bit simpler, and will allow to have it accounted for right
away (i.e. when option is processed) which will be useful when we're
using our own functions for strerr_* errors (to support double output).

src/anopa/aa-enable.c +1 -4
src/anopa/aa-reset.c +1 -4
src/anopa/aa-start.c +1 -3
src/anopa/aa-status.c +1 -4
src/anopa/aa-stop.c +1 -3
src/include/anopa/output.h +1 -1
src/libanopa/die_usage.c +0 -1
src/libanopa/die_version.c +0 -1
src/libanopa/output.c +19 -11
src/utils/aa-echo.c +1 -3
src/utils/aa-terminate.c +1 -4

diff --git a/src/anopa/aa-enable.c b/src/anopa/aa-enable.c
index 88d4080..838238e 100644
--- a/src/anopa/aa-enable.c
+++ b/src/anopa/aa-enable.c
@@ -201,7 +201,6 @@ main (int argc, char * const argv[])
     const char *path_list = NULL;
     const char *set_crash = NULL;
     const char *set_finish = NULL;
-    int mode_both = 0;
     int i;
     int r;
 
@@ -240,7 +239,7 @@ main (int argc, char * const argv[])
                 break;
 
             case 'D':
-                mode_both = 1;
+                aa_set_double_output (1);
                 break;
 
             case 'f':
@@ -296,8 +295,6 @@ main (int argc, char * const argv[])
     argc -= optind;
     argv += optind;
 
-    aa_init_output (mode_both);
-
     if (!path_list && argc < 1)
         dieusage (1);
 
diff --git a/src/anopa/aa-reset.c b/src/anopa/aa-reset.c
index 1a1cef4..f5d902e 100644
--- a/src/anopa/aa-reset.c
+++ b/src/anopa/aa-reset.c
@@ -148,7 +148,6 @@ main (int argc, char * const argv[])
 {
     PROG = "aa-reset";
     const char *path_repo = "/run/services";
-    int mode_both = 0;
     intptr_t mode = MODE_NONE;
     int i;
     int r;
@@ -181,7 +180,7 @@ main (int argc, char * const argv[])
                 break;
 
             case 'D':
-                mode_both = 1;
+                aa_set_double_output (1);
                 break;
 
             case 'h':
@@ -206,8 +205,6 @@ main (int argc, char * const argv[])
     argc -= optind;
     argv += optind;
 
-    aa_init_output (mode_both);
-
     if (argc < 1 || mode == MODE_NONE)
         dieusage (1);
 
diff --git a/src/anopa/aa-start.c b/src/anopa/aa-start.c
index 75c1354..39c159a 100644
--- a/src/anopa/aa-start.c
+++ b/src/anopa/aa-start.c
@@ -236,7 +236,6 @@ main (int argc, char * const argv[])
     PROG = "aa-start";
     const char *path_repo = "/run/services";
     const char *path_list = NULL;
-    int mode_both = 0;
     int i;
 
     aa_secs_timeout = DEFAULT_TIMEOUT_SECS;
@@ -261,7 +260,7 @@ main (int argc, char * const argv[])
         switch (c)
         {
             case 'D':
-                mode_both = 1;
+                aa_set_double_output (1);
                 break;
 
             case 'h':
@@ -303,7 +302,6 @@ main (int argc, char * const argv[])
     argc -= optind;
     argv += optind;
 
-    aa_init_output (mode_both);
     cols = get_cols (1);
     is_utf8 = is_locale_utf8 ();
 
diff --git a/src/anopa/aa-status.c b/src/anopa/aa-status.c
index 52366ab..705b4c3 100644
--- a/src/anopa/aa-status.c
+++ b/src/anopa/aa-status.c
@@ -693,7 +693,6 @@ main (int argc, char * const argv[])
     PROG = "aa-status";
     const char *path_repo = "/run/services";
     const char *path_list = NULL;
-    int mode_both = 0;
     struct config cfg = { 0, };
     int all = 0;
     int i;
@@ -725,7 +724,7 @@ main (int argc, char * const argv[])
                 break;
 
             case 'D':
-                mode_both = 1;
+                aa_set_double_output (1);
                 break;
 
             case 'f':
@@ -764,8 +763,6 @@ main (int argc, char * const argv[])
     argc -= optind;
     argv += optind;
 
-    aa_init_output (mode_both);
-
     if (!all && !path_list && argc < 1)
         dieusage (1);
 
diff --git a/src/anopa/aa-stop.c b/src/anopa/aa-stop.c
index e340e8d..5a7a238 100644
--- a/src/anopa/aa-stop.c
+++ b/src/anopa/aa-stop.c
@@ -249,7 +249,6 @@ main (int argc, char * const argv[])
     PROG = "aa-stop";
     const char *path_repo = "/run/services";
     const char *path_list = NULL;
-    int mode_both = 0;
     int i;
 
     aa_secs_timeout = DEFAULT_TIMEOUT_SECS;
@@ -279,7 +278,7 @@ main (int argc, char * const argv[])
                 break;
 
             case 'D':
-                mode_both = 1;
+                aa_set_double_output (1);
                 break;
 
             case 'h':
@@ -318,7 +317,6 @@ main (int argc, char * const argv[])
     argc -= optind;
     argv += optind;
 
-    aa_init_output (mode_both);
     cols = get_cols (1);
     is_utf8 = is_locale_utf8 ();
 
diff --git a/src/include/anopa/output.h b/src/include/anopa/output.h
index 2142458..396899c 100644
--- a/src/include/anopa/output.h
+++ b/src/include/anopa/output.h
@@ -35,7 +35,7 @@
 #define AA_OUT      0
 #define AA_ERR      1
 
-extern void aa_init_output (int mode_both);
+extern void aa_set_double_output (int enabled);
 extern void aa_bb_noflush (int where, const char *s, int len);
 extern void aa_bb_flush (int where, const char *s, int len);
 #define aa_bs_noflush(w,s)  aa_bb_noflush ((w), (s), strlen (s))
diff --git a/src/libanopa/die_usage.c b/src/libanopa/die_usage.c
index 2ca2a65..47cd49b 100644
--- a/src/libanopa/die_usage.c
+++ b/src/libanopa/die_usage.c
@@ -28,7 +28,6 @@ extern char const *PROG;
 void
 aa_die_usage (int rc, const char *usage, const char *details)
 {
-    aa_init_output (0);
     aa_bs_noflush (AA_OUT, "Usage: ");
     aa_bs_noflush (AA_OUT, PROG);
     aa_bs_noflush (AA_OUT, " ");
diff --git a/src/libanopa/die_version.c b/src/libanopa/die_version.c
index 7bd1f88..1ece3a1 100644
--- a/src/libanopa/die_version.c
+++ b/src/libanopa/die_version.c
@@ -30,7 +30,6 @@ extern char const *PROG;
 void
 aa_die_version (void)
 {
-    aa_init_output (0);
     aa_bs_noflush (AA_OUT, PROG);
     aa_bs_noflush (AA_OUT, " v" ANOPA_VERSION "\n");
     aa_bs_flush (AA_OUT,
diff --git a/src/libanopa/output.c b/src/libanopa/output.c
index 2bb36e7..c25d59b 100644
--- a/src/libanopa/output.c
+++ b/src/libanopa/output.c
@@ -25,25 +25,33 @@
 #include <skalibs/buffer.h>
 #include <anopa/output.h>
 
-static int istty[2] = { 0, 0 };
-static int do_both = 0;
+static int istty[2] = { -1, 0 };
+static int double_output = 0;
+
+#define is_tty(n)           (istty[0] > -1 || chk_tty ()) && istty[n]
 
 #define putb_noflush(w,s,l) buffer_putnoflush ((w) ? buffer_2 : buffer_1small, s, l)
 #define putb_flush(w,s,l)   buffer_putflush ((w) ? buffer_2 : buffer_1small, s, l)
 
-void
-aa_init_output (int mode_both)
+static int
+chk_tty (void)
 {
     istty[0] = isatty (1);
     istty[1] = isatty (2);
-    do_both = mode_both;
+    return 1;
+}
+
+void
+aa_set_double_output (int enabled)
+{
+    double_output = !!enabled;
 }
 
 void
 aa_bb_noflush (int where, const char *s, int len)
 {
     putb_noflush (where, s, len);
-    if (do_both)
+    if (double_output)
         putb_noflush (!where, s, len);
 }
 
@@ -51,25 +59,25 @@ void
 aa_bb_flush (int where, const char *s, int len)
 {
     putb_flush (where, s, len);
-    if (do_both)
+    if (double_output)
         putb_flush (!where, s, len);
 }
 
 void
 aa_ib_noflush (int where, const char *s, int len)
 {
-    if (istty[where])
+    if (is_tty (where))
         putb_noflush (where, s, len);
-    if (do_both && istty[!where])
+    if (double_output && is_tty (!where))
         putb_noflush (!where, s, len);
 }
 
 void
 aa_ib_flush (int where, const char *s, int len)
 {
-    if (istty[where])
+    if (is_tty (where))
         putb_flush (where, s, len);
-    if (do_both && istty[!where])
+    if (double_output && is_tty (!where))
         putb_flush (!where, s, len);
 }
 
diff --git a/src/utils/aa-echo.c b/src/utils/aa-echo.c
index 5e45986..25f8529 100644
--- a/src/utils/aa-echo.c
+++ b/src/utils/aa-echo.c
@@ -70,7 +70,6 @@ int
 main (int argc, char * const argv[])
 {
     PROG = "aa-echo";
-    int mode_both = 0;
     int blank = 0;
     put_fn put = put_title;
     int where = AA_OUT;
@@ -102,7 +101,7 @@ main (int argc, char * const argv[])
                 break;
 
             case 'D':
-                mode_both = 1;
+                aa_set_double_output (1);
                 break;
 
             case 'e':
@@ -146,7 +145,6 @@ main (int argc, char * const argv[])
     if (argc < 1)
         dieusage (1);
 
-    aa_init_output (mode_both);
     if (blank)
         aa_bs_noflush (where, "\n");
     if (put)
diff --git a/src/utils/aa-terminate.c b/src/utils/aa-terminate.c
index 62dad82..45bb1c8 100644
--- a/src/utils/aa-terminate.c
+++ b/src/utils/aa-terminate.c
@@ -276,7 +276,6 @@ main (int argc, char * const argv[])
     stralloc sa_mounts = STRALLOC_ZERO;
     stralloc sa_loops = STRALLOC_ZERO;
     stralloc sa_dms = STRALLOC_ZERO;
-    int mode_both = 0;
     int apis = 0;
     int lazy = 0;
     int did_smthg;
@@ -305,7 +304,7 @@ main (int argc, char * const argv[])
                 break;
 
             case 'D':
-                mode_both = 1;
+                aa_set_double_output (1);
                 break;
 
             case 'h':
@@ -336,8 +335,6 @@ main (int argc, char * const argv[])
     if (argc > 0)
         dieusage (1);
 
-    aa_init_output (mode_both);
-
 again:
     for (;;)
     {