]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/show-status.c
Merge pull request #9624 from poettering/service-state-flush
[thirdparty/systemd.git] / src / core / show-status.c
index 0f8cb3dbdaabf22210ff9fdb57ed19586361ca59..d8d2317d38ef1f1763c78da2457f8b9e94357224 100644 (file)
@@ -1,35 +1,34 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2014 Lennart Poettering
-***/
 
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "io-util.h"
 #include "parse-util.h"
 #include "show-status.h"
+#include "string-table.h"
 #include "string-util.h"
 #include "terminal-util.h"
 #include "util.h"
 
+static const char* const show_status_table[_SHOW_STATUS_MAX] = {
+        [SHOW_STATUS_NO] = "no",
+        [SHOW_STATUS_AUTO] = "auto",
+        [SHOW_STATUS_TEMPORARY] = "temporary",
+        [SHOW_STATUS_YES] = "yes",
+};
+
+DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(show_status, ShowStatus, SHOW_STATUS_YES);
+
 int parse_show_status(const char *v, ShowStatus *ret) {
-        int r;
+        ShowStatus s;
 
-        assert(v);
         assert(ret);
 
-        if (streq(v, "auto")) {
-                *ret = SHOW_STATUS_AUTO;
-                return 0;
-        }
+        s = show_status_from_string(v);
+        if (s < 0 || s == SHOW_STATUS_TEMPORARY)
+                return -EINVAL;
 
-        r = parse_boolean(v);
-        if (r < 0)
-                return r;
-
-        *ret = r ? SHOW_STATUS_YES : SHOW_STATUS_NO;
+        *ret = s;
         return 0;
 }
 
@@ -37,7 +36,7 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char
         static const char status_indent[] = "         "; /* "[" STATUS "] " */
         _cleanup_free_ char *s = NULL;
         _cleanup_close_ int fd = -1;
-        struct iovec iovec[6] = {};
+        struct iovec iovec[7] = {};
         int n = 0;
         static bool prev_ephemeral;
 
@@ -81,8 +80,7 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char
         }
 
         if (prev_ephemeral)
-                iovec[n++] = IOVEC_MAKE_STRING("\r" ANSI_ERASE_TO_END_OF_LINE);
-        prev_ephemeral = ephemeral;
+                iovec[n++] = IOVEC_MAKE_STRING(ANSI_REVERSE_LINEFEED "\r" ANSI_ERASE_TO_END_OF_LINE);
 
         if (status) {
                 if (!isempty(status)) {
@@ -94,8 +92,11 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char
         }
 
         iovec[n++] = IOVEC_MAKE_STRING(s);
-        if (!ephemeral)
-                iovec[n++] = IOVEC_MAKE_STRING("\n");
+        iovec[n++] = IOVEC_MAKE_STRING("\n");
+
+        if (prev_ephemeral && !ephemeral)
+                iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE);
+        prev_ephemeral = ephemeral;
 
         if (writev(fd, iovec, n) < 0)
                 return -errno;