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