]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/show-status.c
io-util.h: move iovec stuff from macro.h to io-util.h
[thirdparty/systemd.git] / src / core / show-status.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "fd-util.h"
23 #include "io-util.h"
24 #include "parse-util.h"
25 #include "show-status.h"
26 #include "string-util.h"
27 #include "terminal-util.h"
28 #include "util.h"
29
30 int parse_show_status(const char *v, ShowStatus *ret) {
31 int r;
32
33 assert(v);
34 assert(ret);
35
36 if (streq(v, "auto")) {
37 *ret = SHOW_STATUS_AUTO;
38 return 0;
39 }
40
41 r = parse_boolean(v);
42 if (r < 0)
43 return r;
44
45 *ret = r ? SHOW_STATUS_YES : SHOW_STATUS_NO;
46 return 0;
47 }
48
49 int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) {
50 static const char status_indent[] = " "; /* "[" STATUS "] " */
51 _cleanup_free_ char *s = NULL;
52 _cleanup_close_ int fd = -1;
53 struct iovec iovec[6] = {};
54 int n = 0;
55 static bool prev_ephemeral;
56
57 assert(format);
58
59 /* This is independent of logging, as status messages are
60 * optional and go exclusively to the console. */
61
62 if (vasprintf(&s, format, ap) < 0)
63 return log_oom();
64
65 fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
66 if (fd < 0)
67 return fd;
68
69 if (ellipse) {
70 char *e;
71 size_t emax, sl;
72 int c;
73
74 c = fd_columns(fd);
75 if (c <= 0)
76 c = 80;
77
78 sl = status ? sizeof(status_indent)-1 : 0;
79
80 emax = c - sl - 1;
81 if (emax < 3)
82 emax = 3;
83
84 e = ellipsize(s, emax, 50);
85 if (e) {
86 free(s);
87 s = e;
88 }
89 }
90
91 if (prev_ephemeral)
92 IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE);
93 prev_ephemeral = ephemeral;
94
95 if (status) {
96 if (!isempty(status)) {
97 IOVEC_SET_STRING(iovec[n++], "[");
98 IOVEC_SET_STRING(iovec[n++], status);
99 IOVEC_SET_STRING(iovec[n++], "] ");
100 } else
101 IOVEC_SET_STRING(iovec[n++], status_indent);
102 }
103
104 IOVEC_SET_STRING(iovec[n++], s);
105 if (!ephemeral)
106 IOVEC_SET_STRING(iovec[n++], "\n");
107
108 if (writev(fd, iovec, n) < 0)
109 return -errno;
110
111 return 0;
112 }
113
114 int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) {
115 va_list ap;
116 int r;
117
118 assert(format);
119
120 va_start(ap, format);
121 r = status_vprintf(status, ellipse, ephemeral, format, ap);
122 va_end(ap);
123
124 return r;
125 }