]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/show-status.c
Merge pull request #1962 from mbiebl/install-completion-networkctl
[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 "alloc-util.h"
23 #include "fd-util.h"
24 #include "io-util.h"
25 #include "parse-util.h"
26 #include "show-status.h"
27 #include "string-util.h"
28 #include "terminal-util.h"
29 #include "util.h"
30
31 int parse_show_status(const char *v, ShowStatus *ret) {
32 int r;
33
34 assert(v);
35 assert(ret);
36
37 if (streq(v, "auto")) {
38 *ret = SHOW_STATUS_AUTO;
39 return 0;
40 }
41
42 r = parse_boolean(v);
43 if (r < 0)
44 return r;
45
46 *ret = r ? SHOW_STATUS_YES : SHOW_STATUS_NO;
47 return 0;
48 }
49
50 int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) {
51 static const char status_indent[] = " "; /* "[" STATUS "] " */
52 _cleanup_free_ char *s = NULL;
53 _cleanup_close_ int fd = -1;
54 struct iovec iovec[6] = {};
55 int n = 0;
56 static bool prev_ephemeral;
57
58 assert(format);
59
60 /* This is independent of logging, as status messages are
61 * optional and go exclusively to the console. */
62
63 if (vasprintf(&s, format, ap) < 0)
64 return log_oom();
65
66 fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
67 if (fd < 0)
68 return fd;
69
70 if (ellipse) {
71 char *e;
72 size_t emax, sl;
73 int c;
74
75 c = fd_columns(fd);
76 if (c <= 0)
77 c = 80;
78
79 sl = status ? sizeof(status_indent)-1 : 0;
80
81 emax = c - sl - 1;
82 if (emax < 3)
83 emax = 3;
84
85 e = ellipsize(s, emax, 50);
86 if (e) {
87 free(s);
88 s = e;
89 }
90 }
91
92 if (prev_ephemeral)
93 IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE);
94 prev_ephemeral = ephemeral;
95
96 if (status) {
97 if (!isempty(status)) {
98 IOVEC_SET_STRING(iovec[n++], "[");
99 IOVEC_SET_STRING(iovec[n++], status);
100 IOVEC_SET_STRING(iovec[n++], "] ");
101 } else
102 IOVEC_SET_STRING(iovec[n++], status_indent);
103 }
104
105 IOVEC_SET_STRING(iovec[n++], s);
106 if (!ephemeral)
107 IOVEC_SET_STRING(iovec[n++], "\n");
108
109 if (writev(fd, iovec, n) < 0)
110 return -errno;
111
112 return 0;
113 }
114
115 int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) {
116 va_list ap;
117 int r;
118
119 assert(format);
120
121 va_start(ap, format);
122 r = status_vprintf(status, ellipse, ephemeral, format, ap);
123 va_end(ap);
124
125 return r;
126 }