]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: normalize ShowStatus
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 23 Jul 2018 12:55:26 +0000 (21:55 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 23 Jul 2018 12:55:26 +0000 (21:55 +0900)
src/core/dbus-manager.c
src/core/main.c
src/core/manager.c
src/core/show-status.c
src/core/show-status.h

index 4ed68af1e008cf34eafea080bdf5c05ffa1849bc..4c0f79b983c7f4dfa27f65e5128c48bfaa263d81 100644 (file)
@@ -232,7 +232,7 @@ static int property_get_show_status(
         assert(reply);
         assert(m);
 
-        b = m->show_status > 0;
+        b = IN_SET(m->show_status, SHOW_STATUS_TEMPORARY, SHOW_STATUS_YES);
         return sd_bus_message_append_basic(reply, 'b', &b);
 }
 
index 6bffd6f362b37b190ffc2f62c10341564435efcf..b7e98502eb9d6666139db5e33e2853c8dee51705 100644 (file)
@@ -95,7 +95,7 @@ static int arg_crash_chvt = -1;
 static bool arg_crash_shell = false;
 static bool arg_crash_reboot = false;
 static char *arg_confirm_spawn = NULL;
-static ShowStatus arg_show_status = _SHOW_STATUS_UNSET;
+static ShowStatus arg_show_status = _SHOW_STATUS_INVALID;
 static bool arg_switched_root = false;
 static bool arg_no_pager = false;
 static bool arg_service_watchdogs = true;
@@ -470,7 +470,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
 
         } else if (streq(key, "quiet") && !value) {
 
-                if (arg_show_status == _SHOW_STATUS_UNSET)
+                if (arg_show_status == _SHOW_STATUS_INVALID)
                         arg_show_status = SHOW_STATUS_AUTO;
 
         } else if (streq(key, "debug") && !value) {
@@ -1219,7 +1219,7 @@ static int status_welcome(void) {
         _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL;
         int r;
 
-        if (arg_show_status <= 0)
+        if (IN_SET(arg_show_status, SHOW_STATUS_NO, SHOW_STATUS_AUTO))
                 return 0;
 
         r = parse_os_release(NULL,
@@ -1985,7 +1985,7 @@ static int load_configuration(int argc, char **argv, const char **ret_error_mess
         }
 
         /* Initialize the show status setting if it hasn't been set explicitly yet */
-        if (arg_show_status == _SHOW_STATUS_UNSET)
+        if (arg_show_status == _SHOW_STATUS_INVALID)
                 arg_show_status = SHOW_STATUS_YES;
 
         return 0;
index ada8712fd504b664be575dc49a92fbc362f85f24..c819dd10690038383c58c10d63b44b973c0bc826 100644 (file)
@@ -3912,7 +3912,7 @@ void manager_set_show_status(Manager *m, ShowStatus mode) {
                           mode == SHOW_STATUS_NO ? "Disabling" : "Enabling");
         m->show_status = mode;
 
-        if (mode > 0)
+        if (IN_SET(mode, SHOW_STATUS_TEMPORARY, SHOW_STATUS_YES))
                 (void) touch("/run/systemd/show-status");
         else
                 (void) unlink("/run/systemd/show-status");
@@ -3934,7 +3934,7 @@ static bool manager_get_show_status(Manager *m, StatusType type) {
         if (type != STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0)
                 return false;
 
-        return m->show_status > 0;
+        return IN_SET(m->show_status, SHOW_STATUS_TEMPORARY, SHOW_STATUS_YES);
 }
 
 const char *manager_get_confirm_spawn(Manager *m) {
index fd9aeb9416f82a3dbe681fd40472da561fd99bbf..9e5e7a50b941bc751c3c4fd5e6b1d501bf754931 100644 (file)
@@ -5,26 +5,30 @@
 #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_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;
-        }
-
-        r = parse_boolean(v);
-        if (r < 0)
-                return r;
+        s = show_status_from_string(v);
+        if (s < 0 || s == SHOW_STATUS_TEMPORARY)
+                return -EINVAL;
 
-        *ret = r ? SHOW_STATUS_YES : SHOW_STATUS_NO;
+        *ret = s;
         return 0;
 }
 
index 1a80de33d9e2596c20dc72c31c4d1976a54e3a63..3fce567a0298e046a45ec3ebc670e87aadac31b5 100644 (file)
@@ -8,13 +8,15 @@
 /* Manager status */
 
 typedef enum ShowStatus {
-        _SHOW_STATUS_UNSET = -2,
-        SHOW_STATUS_AUTO = -1,
-        SHOW_STATUS_NO = 0,
-        SHOW_STATUS_YES = 1,
-        SHOW_STATUS_TEMPORARY = 2,
+        SHOW_STATUS_NO,
+        SHOW_STATUS_AUTO,
+        SHOW_STATUS_TEMPORARY,
+        SHOW_STATUS_YES,
+        _SHOW_STATUS_INVALID = -1,
 } ShowStatus;
 
+ShowStatus show_status_from_string(const char *v) _const_;
+const char* show_status_to_string(ShowStatus s) _pure_;
 int parse_show_status(const char *v, ShowStatus *ret);
 
 int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);