chiark / gitweb /
util: unify usage of on_tty() in util.c
authorLennart Poettering <lennart@poettering.net>
Thu, 18 Oct 2012 21:59:41 +0000 (23:59 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Oct 2012 22:07:55 +0000 (00:07 +0200)
TODO
src/journal/journal-verify.c
src/journal/journalctl.c
src/login/loginctl.c
src/shared/pager.c
src/shared/util.c
src/shared/util.h
src/systemctl/systemctl.c

diff --git a/TODO b/TODO
index bb7d8eea2112bca9865614bd113a15b961b9daf5..1c33ead227e904191569bb1c8e8ecb3d32b0511b 100644 (file)
--- a/TODO
+++ b/TODO
@@ -19,6 +19,8 @@ F18:
 
 Features:
 
 
 Features:
 
+* drop --follow from systemctl
+
 * don't show cgroup in "systemctl status" if empty/non-existant, especially for foreign .mount units
 
 * timedated: export boolean that clarifies whether NTP is even available
 * don't show cgroup in "systemctl status" if empty/non-existant, especially for foreign .mount units
 
 * timedated: export boolean that clarifies whether NTP is even available
index 551f04af35d40ae260788a1312f8142aec911418..1a67d5a04bddff7eff3f32a5dfdd166320bfc022 100644 (file)
@@ -178,7 +178,7 @@ static void draw_progress(uint64_t p, usec_t *last_usec) {
         unsigned n, i, j, k;
         usec_t z, x;
 
         unsigned n, i, j, k;
         usec_t z, x;
 
-        if (!isatty(STDOUT_FILENO))
+        if (!on_tty())
                 return;
 
         z = now(CLOCK_MONOTONIC);
                 return;
 
         z = now(CLOCK_MONOTONIC);
@@ -212,7 +212,7 @@ static void draw_progress(uint64_t p, usec_t *last_usec) {
 static void flush_progress(void) {
         unsigned n, i;
 
 static void flush_progress(void) {
         unsigned n, i;
 
-        if (!isatty(STDOUT_FILENO))
+        if (!on_tty())
                 return;
 
         n = (3 * columns()) / 4;
                 return;
 
         n = (3 * columns()) / 4;
index d6f19e900a7cde303f3521b09e30008c4ecc78ab..f4b651855771a42b7708cb1607ba43668f7b273d 100644 (file)
@@ -400,21 +400,6 @@ static int parse_argv(int argc, char *argv[]) {
         return 1;
 }
 
         return 1;
 }
 
-static bool on_tty(void) {
-        static int t = -1;
-
-        /* Note that this is invoked relatively early, before we start
-         * the pager. That means the value we return reflects whether
-         * we originally were started on a tty, not if we currently
-         * are. But this is intended, since we want colour and so on
-         * when run in our own pager. */
-
-        if (_unlikely_(t < 0))
-                t = isatty(STDOUT_FILENO) > 0;
-
-        return t;
-}
-
 static int generate_new_id128(void) {
         sd_id128_t id;
         int r;
 static int generate_new_id128(void) {
         sd_id128_t id;
         int r;
@@ -697,7 +682,7 @@ static int setup_keys(void) {
                 goto finish;
         }
 
                 goto finish;
         }
 
-        if (isatty(STDOUT_FILENO)) {
+        if (on_tty()) {
                 fprintf(stderr,
                         "\n"
                         "The new key pair has been generated. The " ANSI_HIGHLIGHT_ON "secret sealing key" ANSI_HIGHLIGHT_OFF " has been written to\n"
                 fprintf(stderr,
                         "\n"
                         "The new key pair has been generated. The " ANSI_HIGHLIGHT_ON "secret sealing key" ANSI_HIGHLIGHT_OFF " has been written to\n"
@@ -719,7 +704,7 @@ static int setup_keys(void) {
 
         printf("/%llx-%llx\n", (unsigned long long) n, (unsigned long long) arg_interval);
 
 
         printf("/%llx-%llx\n", (unsigned long long) n, (unsigned long long) arg_interval);
 
-        if (isatty(STDOUT_FILENO)) {
+        if (on_tty()) {
                 char tsb[FORMAT_TIMESPAN_MAX], *hn;
 
                 fprintf(stderr,
                 char tsb[FORMAT_TIMESPAN_MAX], *hn;
 
                 fprintf(stderr,
@@ -982,8 +967,6 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
                 goto finish;
         }
 
-        on_tty();
-
         if (!arg_no_pager && !arg_follow)
                 pager_open();
 
         if (!arg_no_pager && !arg_follow)
                 pager_open();
 
index 97c6617d7d4ef3f131d3e36584acdd6afba7b52b..24941fef34c0945b4ef1e777ee5435286ff9bcdd 100644 (file)
@@ -50,26 +50,9 @@ static enum transport {
 static bool arg_ask_password = true;
 static const char *arg_host = NULL;
 
 static bool arg_ask_password = true;
 static const char *arg_host = NULL;
 
-static bool on_tty(void) {
-        static int t = -1;
-
-        /* Note that this is invoked relatively early, before we start
-         * the pager. That means the value we return reflects whether
-         * we originally were started on a tty, not if we currently
-         * are. But this is intended, since we want colour and so on
-         * when run in our own pager. */
-
-        if (_unlikely_(t < 0))
-                t = isatty(STDOUT_FILENO) > 0;
-
-        return t;
-}
-
 static void pager_open_if_enabled(void) {
 
         /* Cache result before we open the pager */
 static void pager_open_if_enabled(void) {
 
         /* Cache result before we open the pager */
-        on_tty();
-
         if (arg_no_pager)
                 return;
 
         if (arg_no_pager)
                 return;
 
index 6799787e85b0cf7d339e23d553b328b6fbd9d3ab..488a12c763839f156471d1b56606fae5f06b8188 100644 (file)
@@ -57,7 +57,7 @@ int pager_open(void) {
                 if (!*pager || streq(pager, "cat"))
                         return 0;
 
                 if (!*pager || streq(pager, "cat"))
                         return 0;
 
-        if (isatty(STDOUT_FILENO) <= 0)
+        if (!on_tty())
                 return 0;
 
         /* Determine and cache number of columns before we spawn the
                 return 0;
 
         /* Determine and cache number of columns before we spawn the
index 462b541b41d6d7560b9ec0b792eb1f801633079b..527a5800fec407d6cc096f66d22dc22cbfa6d7cf 100644 (file)
@@ -2175,28 +2175,25 @@ int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
 }
 
 int ask(char *ret, const char *replies, const char *text, ...) {
 }
 
 int ask(char *ret, const char *replies, const char *text, ...) {
-        bool on_tty;
 
         assert(ret);
         assert(replies);
         assert(text);
 
 
         assert(ret);
         assert(replies);
         assert(text);
 
-        on_tty = isatty(STDOUT_FILENO);
-
         for (;;) {
                 va_list ap;
                 char c;
                 int r;
                 bool need_nl = true;
 
         for (;;) {
                 va_list ap;
                 char c;
                 int r;
                 bool need_nl = true;
 
-                if (on_tty)
+                if (on_tty())
                         fputs(ANSI_HIGHLIGHT_ON, stdout);
 
                 va_start(ap, text);
                 vprintf(text, ap);
                 va_end(ap);
 
                         fputs(ANSI_HIGHLIGHT_ON, stdout);
 
                 va_start(ap, text);
                 vprintf(text, ap);
                 va_end(ap);
 
-                if (on_tty)
+                if (on_tty())
                         fputs(ANSI_HIGHLIGHT_OFF, stdout);
 
                 fflush(stdout);
                         fputs(ANSI_HIGHLIGHT_OFF, stdout);
 
                 fflush(stdout);
@@ -3820,6 +3817,15 @@ void columns_cache_reset(int signum) {
         cached_columns = 0;
 }
 
         cached_columns = 0;
 }
 
+bool on_tty(void) {
+        static int cached_on_tty = -1;
+
+        if (_unlikely_(cached_on_tty < 0))
+                cached_on_tty = isatty(STDOUT_FILENO) > 0;
+
+        return cached_on_tty;
+}
+
 int fd_lines(int fd) {
         struct winsize ws;
         zero(ws);
 int fd_lines(int fd) {
         struct winsize ws;
         zero(ws);
index 662c3d1f398e4941b8ba9267e0b6bbbe043454ea..e19f76c1ea5b79eee1fd00dffd6effbfef72299d 100644 (file)
@@ -385,6 +385,7 @@ int status_welcome(void);
 int fd_columns(int fd);
 unsigned columns(void);
 void columns_cache_reset(int _unused_ signum);
 int fd_columns(int fd);
 unsigned columns(void);
 void columns_cache_reset(int _unused_ signum);
+bool on_tty(void);
 
 int fd_lines(int fd);
 unsigned lines(void);
 
 int fd_lines(int fd);
 unsigned lines(void);
index 82c801e4d99ce598fc2fce47afa156956b959aec..d77d534e0bde068ddebdfeb907d616f060bebea0 100644 (file)
@@ -132,26 +132,8 @@ static bool private_bus = false;
 static int daemon_reload(DBusConnection *bus, char **args);
 static void halt_now(enum action a);
 
 static int daemon_reload(DBusConnection *bus, char **args);
 static void halt_now(enum action a);
 
-static bool on_tty(void) {
-        static int t = -1;
-
-        /* Note that this is invoked relatively early, before we start
-         * the pager. That means the value we return reflects whether
-         * we originally were started on a tty, not if we currently
-         * are. But this is intended, since we want colour and so on
-         * when run in our own pager. */
-
-        if (_unlikely_(t < 0))
-                t = isatty(STDOUT_FILENO) > 0;
-
-        return t;
-}
-
 static void pager_open_if_enabled(void) {
 
 static void pager_open_if_enabled(void) {
 
-        /* Cache result before we open the pager */
-        on_tty();
-
         if (arg_no_pager)
                 return;
 
         if (arg_no_pager)
                 return;