diff options
author | Michael Biebl <biebl@debian.org> | 2018-12-21 22:06:22 +0100 |
---|---|---|
committer | Michael Biebl <biebl@debian.org> | 2018-12-21 22:06:22 +0100 |
commit | 6e866b331d7cd4a5e0759dd160dea6edabd3678e (patch) | |
tree | 4d24c1ffe4ae946f04d8910956090e8d13aecd9a /src/core/show-status.c | |
parent | b012e92123bdc9fa10c2f079ec5bd9313b23e21a (diff) | |
download | systemd-6e866b331d7cd4a5e0759dd160dea6edabd3678e.tar.gz |
New upstream version 240
Diffstat (limited to 'src/core/show-status.c')
-rw-r--r-- | src/core/show-status.c | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/src/core/show-status.c b/src/core/show-status.c index 63262cc716..f748a82084 100644 --- a/src/core/show-status.c +++ b/src/core/show-status.c @@ -5,34 +5,38 @@ #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; - } - - 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; } -int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) { +int status_vprintf(const char *status, ShowStatusFlags flags, const char *format, va_list ap) { 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; @@ -53,7 +57,7 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char if (fd < 0) return fd; - if (ellipse) { + if (FLAGS_SET(flags, SHOW_STATUS_ELLIPSIZE)) { char *e; size_t emax, sl; int c; @@ -69,15 +73,12 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char emax = 3; e = ellipsize(s, emax, 50); - if (e) { - free(s); - s = e; - } + if (e) + free_and_replace(s, e); } 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)) { @@ -89,8 +90,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 && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL)) + iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE); + prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL) ; if (writev(fd, iovec, n) < 0) return -errno; @@ -98,14 +102,14 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char return 0; } -int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) { +int status_printf(const char *status, ShowStatusFlags flags, const char *format, ...) { va_list ap; int r; assert(format); va_start(ap, format); - r = status_vprintf(status, ellipse, ephemeral, format, ap); + r = status_vprintf(status, flags, format, ap); va_end(ap); return r; |