]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
288a74cc RC |
2 | #pragma once |
3 | ||
288a74cc | 4 | #include <stdarg.h> |
71d35b6b | 5 | #include <stdbool.h> |
288a74cc | 6 | #include <stdio.h> |
37b8d2f6 | 7 | #include <syslog.h> |
11c3a366 | 8 | #include <sys/types.h> |
d02d4f83 | 9 | #include <termios.h> |
288a74cc RC |
10 | |
11 | #include "macro.h" | |
12 | #include "time-util.h" | |
13 | ||
6d1d8f66 | 14 | /* Regular colors */ |
66bb0059 | 15 | #define ANSI_BLACK "\x1B[0;30m" /* Some type of grey usually. */ |
6d1d8f66 AR |
16 | #define ANSI_RED "\x1B[0;31m" |
17 | #define ANSI_GREEN "\x1B[0;32m" | |
18 | #define ANSI_YELLOW "\x1B[0;33m" | |
19 | #define ANSI_BLUE "\x1B[0;34m" | |
20 | #define ANSI_MAGENTA "\x1B[0;35m" | |
66bb0059 ZJS |
21 | #define ANSI_CYAN "\x1B[0;36m" |
22 | #define ANSI_WHITE "\x1B[0;37m" /* This is actually rendered as light grey, legible even on a white | |
23 | * background. See ANSI_HIGHLIGHT_WHITE for real white. */ | |
24 | ||
25 | #define ANSI_BRIGHT_BLACK "\x1B[0;90m" | |
26 | #define ANSI_BRIGHT_RED "\x1B[0;91m" | |
27 | #define ANSI_BRIGHT_GREEN "\x1B[0;92m" | |
28 | #define ANSI_BRIGHT_YELLOW "\x1B[0;93m" | |
29 | #define ANSI_BRIGHT_BLUE "\x1B[0;94m" | |
30 | #define ANSI_BRIGHT_MAGENTA "\x1B[0;95m" | |
31 | #define ANSI_BRIGHT_CYAN "\x1B[0;96m" | |
32 | #define ANSI_BRIGHT_WHITE "\x1B[0;97m" | |
33 | ||
bb40c125 | 34 | #define ANSI_GREY "\x1B[0;38;5;245m" |
6d1d8f66 AR |
35 | |
36 | /* Bold/highlighted */ | |
5ee27251 ZJS |
37 | #define ANSI_HIGHLIGHT_BLACK "\x1B[0;1;30m" |
38 | #define ANSI_HIGHLIGHT_RED "\x1B[0;1;31m" | |
39 | #define ANSI_HIGHLIGHT_GREEN "\x1B[0;1;32m" | |
40 | #define _ANSI_HIGHLIGHT_YELLOW "\x1B[0;1;33m" /* This yellow is currently not displayed well by some terminals */ | |
41 | #define ANSI_HIGHLIGHT_BLUE "\x1B[0;1;34m" | |
42 | #define ANSI_HIGHLIGHT_MAGENTA "\x1B[0;1;35m" | |
43 | #define ANSI_HIGHLIGHT_CYAN "\x1B[0;1;36m" | |
44 | #define ANSI_HIGHLIGHT_WHITE "\x1B[0;1;37m" | |
45 | #define ANSI_HIGHLIGHT_YELLOW4 "\x1B[0;1;38;5;100m" | |
46 | #define ANSI_HIGHLIGHT_KHAKI3 "\x1B[0;1;38;5;185m" | |
47 | #define ANSI_HIGHLIGHT_GREY "\x1B[0;1;38;5;245m" | |
48 | ||
49 | #define ANSI_HIGHLIGHT_YELLOW ANSI_HIGHLIGHT_KHAKI3 /* Replacement yellow that is more legible */ | |
6d1d8f66 AR |
50 | |
51 | /* Underlined */ | |
45d82c3f | 52 | #define ANSI_GREY_UNDERLINE "\x1B[0;4;38;5;245m" |
25077313 | 53 | #define ANSI_BRIGHT_BLACK_UNDERLINE "\x1B[0;4;90m" |
6d1d8f66 AR |
54 | #define ANSI_HIGHLIGHT_RED_UNDERLINE "\x1B[0;1;4;31m" |
55 | #define ANSI_HIGHLIGHT_GREEN_UNDERLINE "\x1B[0;1;4;32m" | |
a851ba07 | 56 | #define ANSI_HIGHLIGHT_YELLOW_UNDERLINE "\x1B[0;1;4;38;5;185m" |
6d1d8f66 AR |
57 | #define ANSI_HIGHLIGHT_BLUE_UNDERLINE "\x1B[0;1;4;34m" |
58 | #define ANSI_HIGHLIGHT_MAGENTA_UNDERLINE "\x1B[0;1;4;35m" | |
1530bf25 | 59 | #define ANSI_HIGHLIGHT_GREY_UNDERLINE "\x1B[0;1;4;38;5;245m" |
6d1d8f66 AR |
60 | |
61 | /* Other ANSI codes */ | |
1fc464f6 | 62 | #define ANSI_UNDERLINE "\x1B[0;4m" |
6ad2049d LP |
63 | #define ANSI_ADD_UNDERLINE "\x1B[4m" |
64 | #define ANSI_ADD_UNDERLINE_GREY ANSI_ADD_UNDERLINE "\x1B[58;5;245m" | |
1fc464f6 | 65 | #define ANSI_HIGHLIGHT "\x1B[0;1;39m" |
1fc464f6 | 66 | #define ANSI_HIGHLIGHT_UNDERLINE "\x1B[0;1;4m" |
6d1d8f66 | 67 | |
c4fea19a | 68 | /* Fallback colors: 256 -> 16 */ |
25077313 DDM |
69 | #define ANSI_HIGHLIGHT_GREY_FALLBACK "\x1B[0;1;90m" |
70 | #define ANSI_HIGHLIGHT_GREY_FALLBACK_UNDERLINE "\x1B[0;1;4;90m" | |
71 | #define ANSI_HIGHLIGHT_YELLOW_FALLBACK "\x1B[0;1;33m" | |
72 | #define ANSI_HIGHLIGHT_YELLOW_FALLBACK_UNDERLINE "\x1B[0;1;4;33m" | |
c4fea19a | 73 | |
fc7eb132 OJ |
74 | /* Background colors */ |
75 | #define ANSI_BACKGROUND_BLUE "\x1B[44m" | |
76 | ||
6d1d8f66 | 77 | /* Reset/clear ANSI styles */ |
1fc464f6 LP |
78 | #define ANSI_NORMAL "\x1B[0m" |
79 | ||
6d1d8f66 | 80 | /* Erase characters until the end of the line */ |
288a74cc RC |
81 | #define ANSI_ERASE_TO_END_OF_LINE "\x1B[K" |
82 | ||
82554307 T |
83 | /* Move cursor up one line */ |
84 | #define ANSI_REVERSE_LINEFEED "\x1BM" | |
85 | ||
1fc464f6 LP |
86 | /* Set cursor to top left corner and clear screen */ |
87 | #define ANSI_HOME_CLEAR "\x1B[H\x1B[2J" | |
88 | ||
288a74cc RC |
89 | int reset_terminal_fd(int fd, bool switch_to_text); |
90 | int reset_terminal(const char *name); | |
fc7eb132 | 91 | int set_terminal_cursor_position(int fd, unsigned int row, unsigned int column); |
288a74cc RC |
92 | |
93 | int open_terminal(const char *name, int mode); | |
8854d795 LP |
94 | |
95 | /* Flags for tweaking the way we become the controlling process of a terminal. */ | |
96 | typedef enum AcquireTerminalFlags { | |
97 | /* Try to become the controlling process of the TTY. If we can't return -EPERM. */ | |
ef31828d | 98 | ACQUIRE_TERMINAL_TRY = 0, |
8854d795 LP |
99 | |
100 | /* Tell the kernel to forcibly make us the controlling process of the TTY. Returns -EPERM if the kernel doesn't allow that. */ | |
ef31828d | 101 | ACQUIRE_TERMINAL_FORCE = 1, |
8854d795 LP |
102 | |
103 | /* If we can't become the controlling process of the TTY right-away, then wait until we can. */ | |
ef31828d | 104 | ACQUIRE_TERMINAL_WAIT = 2, |
8854d795 LP |
105 | |
106 | /* Pick one of the above, and then OR this flag in, in order to request permissive behaviour, if we can't become controlling process then don't mind */ | |
ef31828d | 107 | ACQUIRE_TERMINAL_PERMISSIVE = 1 << 2, |
8854d795 LP |
108 | } AcquireTerminalFlags; |
109 | ||
c4fea19a MGR |
110 | /* Limits the use of ANSI colors to a subset. */ |
111 | typedef enum ColorMode { | |
112 | /* No colors, monochrome output. */ | |
a5efbf46 | 113 | COLOR_OFF, |
c4fea19a MGR |
114 | |
115 | /* All colors, no restrictions. */ | |
a5efbf46 | 116 | COLOR_ON, |
c4fea19a MGR |
117 | |
118 | /* Only the base 16 colors. */ | |
a5efbf46 | 119 | COLOR_16, |
c4fea19a MGR |
120 | |
121 | /* Only 256 colors. */ | |
a5efbf46 SS |
122 | COLOR_256, |
123 | ||
0b75493d | 124 | /* For truecolor or 24bit color support. */ |
a5efbf46 | 125 | COLOR_24BIT, |
c4fea19a | 126 | |
2d93c20e | 127 | _COLOR_INVALID = -EINVAL, |
c4fea19a MGR |
128 | } ColorMode; |
129 | ||
8854d795 | 130 | int acquire_terminal(const char *name, AcquireTerminalFlags flags, usec_t timeout); |
288a74cc RC |
131 | int release_terminal(void); |
132 | ||
133 | int terminal_vhangup_fd(int fd); | |
134 | int terminal_vhangup(const char *name); | |
135 | ||
51462135 | 136 | int terminal_set_size_fd(int fd, const char *ident, unsigned rows, unsigned cols); |
29f5a5ae | 137 | int proc_cmdline_tty_size(const char *tty, unsigned *ret_rows, unsigned *ret_cols); |
51462135 | 138 | |
288a74cc RC |
139 | int chvt(int vt); |
140 | ||
141 | int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl); | |
142 | int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4); | |
143 | int ask_string(char **ret, const char *text, ...) _printf_(2, 3); | |
144 | ||
145 | int vt_disallocate(const char *name); | |
146 | ||
7b912648 | 147 | int resolve_dev_console(char **ret); |
bef41af2 | 148 | int get_kernel_consoles(char ***ret); |
288a74cc RC |
149 | bool tty_is_vc(const char *tty); |
150 | bool tty_is_vc_resolve(const char *tty); | |
151 | bool tty_is_console(const char *tty) _pure_; | |
152 | int vtnr_from_tty(const char *tty); | |
153 | const char *default_term_for_tty(const char *tty); | |
154 | ||
288a74cc RC |
155 | int make_console_stdio(void); |
156 | ||
288a74cc RC |
157 | int fd_columns(int fd); |
158 | unsigned columns(void); | |
159 | int fd_lines(int fd); | |
160 | unsigned lines(void); | |
c6063244 | 161 | |
288a74cc | 162 | void columns_lines_cache_reset(int _unused_ signum); |
c6063244 | 163 | void reset_terminal_feature_caches(void); |
288a74cc RC |
164 | |
165 | bool on_tty(void); | |
ac96418b | 166 | bool terminal_is_dumb(void); |
c4fea19a | 167 | ColorMode get_color_mode(void); |
526664f6 | 168 | bool underline_enabled(void); |
c2b32159 | 169 | bool dev_console_colors_enabled(void); |
288a74cc | 170 | |
dcdd9030 | 171 | static inline bool colors_enabled(void) { |
dcdd9030 MGR |
172 | /* Returns true if colors are considered supported on our stdout. */ |
173 | return get_color_mode() != COLOR_OFF; | |
174 | } | |
175 | ||
5c98198f ZJS |
176 | #define DEFINE_ANSI_FUNC(name, NAME) \ |
177 | static inline const char *ansi_##name(void) { \ | |
178 | return colors_enabled() ? ANSI_##NAME : ""; \ | |
f6a8265b | 179 | } |
5c98198f | 180 | |
c4fea19a MGR |
181 | #define DEFINE_ANSI_FUNC_256(name, NAME, FALLBACK) \ |
182 | static inline const char *ansi_##name(void) { \ | |
183 | switch (get_color_mode()) { \ | |
184 | case COLOR_OFF: return ""; \ | |
185 | case COLOR_16: return ANSI_##FALLBACK; \ | |
186 | default : return ANSI_##NAME; \ | |
187 | } \ | |
188 | } | |
189 | ||
25077313 DDM |
190 | static inline const char *ansi_underline(void) { |
191 | return underline_enabled() ? ANSI_UNDERLINE : ANSI_NORMAL; | |
192 | } | |
193 | ||
6ad2049d LP |
194 | static inline const char *ansi_add_underline(void) { |
195 | return underline_enabled() ? ANSI_ADD_UNDERLINE : ""; | |
196 | } | |
197 | ||
198 | static inline const char *ansi_add_underline_grey(void) { | |
199 | return underline_enabled() ? | |
200 | (colors_enabled() ? ANSI_ADD_UNDERLINE_GREY : ANSI_ADD_UNDERLINE) : ""; | |
201 | } | |
202 | ||
9194c1e6 ZJS |
203 | #define DEFINE_ANSI_FUNC_UNDERLINE(name, NAME) \ |
204 | static inline const char *ansi_##name(void) { \ | |
205 | return underline_enabled() ? ANSI_##NAME##_UNDERLINE : \ | |
206 | colors_enabled() ? ANSI_##NAME : ""; \ | |
c4fea19a MGR |
207 | } |
208 | ||
209 | ||
9194c1e6 ZJS |
210 | #define DEFINE_ANSI_FUNC_UNDERLINE_256(name, NAME, FALLBACK) \ |
211 | static inline const char *ansi_##name(void) { \ | |
212 | switch (get_color_mode()) { \ | |
213 | case COLOR_OFF: return ""; \ | |
25077313 DDM |
214 | case COLOR_16: return underline_enabled() ? ANSI_##FALLBACK##_UNDERLINE : ANSI_##FALLBACK; \ |
215 | default : return underline_enabled() ? ANSI_##NAME##_UNDERLINE: ANSI_##NAME; \ | |
9194c1e6 | 216 | } \ |
f6a8265b | 217 | } |
526664f6 | 218 | |
003aa3e0 LP |
219 | DEFINE_ANSI_FUNC(normal, NORMAL); |
220 | DEFINE_ANSI_FUNC(highlight, HIGHLIGHT); | |
66bb0059 | 221 | DEFINE_ANSI_FUNC(black, BLACK); |
003aa3e0 LP |
222 | DEFINE_ANSI_FUNC(red, RED); |
223 | DEFINE_ANSI_FUNC(green, GREEN); | |
224 | DEFINE_ANSI_FUNC(yellow, YELLOW); | |
225 | DEFINE_ANSI_FUNC(blue, BLUE); | |
226 | DEFINE_ANSI_FUNC(magenta, MAGENTA); | |
66bb0059 ZJS |
227 | DEFINE_ANSI_FUNC(cyan, CYAN); |
228 | DEFINE_ANSI_FUNC(white, WHITE); | |
c4fea19a | 229 | DEFINE_ANSI_FUNC_256(grey, GREY, BRIGHT_BLACK); |
66bb0059 ZJS |
230 | |
231 | DEFINE_ANSI_FUNC(bright_black, BRIGHT_BLACK); | |
232 | DEFINE_ANSI_FUNC(bright_red, BRIGHT_RED); | |
233 | DEFINE_ANSI_FUNC(bright_green, BRIGHT_GREEN); | |
234 | DEFINE_ANSI_FUNC(bright_yellow, BRIGHT_YELLOW); | |
235 | DEFINE_ANSI_FUNC(bright_blue, BRIGHT_BLUE); | |
236 | DEFINE_ANSI_FUNC(bright_magenta, BRIGHT_MAGENTA); | |
237 | DEFINE_ANSI_FUNC(bright_cyan, BRIGHT_CYAN); | |
238 | DEFINE_ANSI_FUNC(bright_white, BRIGHT_WHITE); | |
239 | ||
c4fea19a MGR |
240 | DEFINE_ANSI_FUNC(highlight_black, HIGHLIGHT_BLACK); |
241 | DEFINE_ANSI_FUNC(highlight_red, HIGHLIGHT_RED); | |
242 | DEFINE_ANSI_FUNC(highlight_green, HIGHLIGHT_GREEN); | |
243 | DEFINE_ANSI_FUNC_256(highlight_yellow, HIGHLIGHT_YELLOW, HIGHLIGHT_YELLOW_FALLBACK); | |
25e4608b | 244 | DEFINE_ANSI_FUNC_256(highlight_yellow4, HIGHLIGHT_YELLOW4, HIGHLIGHT_YELLOW_FALLBACK); |
c4fea19a MGR |
245 | DEFINE_ANSI_FUNC(highlight_blue, HIGHLIGHT_BLUE); |
246 | DEFINE_ANSI_FUNC(highlight_magenta, HIGHLIGHT_MAGENTA); | |
247 | DEFINE_ANSI_FUNC(highlight_cyan, HIGHLIGHT_CYAN); | |
248 | DEFINE_ANSI_FUNC_256(highlight_grey, HIGHLIGHT_GREY, HIGHLIGHT_GREY_FALLBACK); | |
249 | DEFINE_ANSI_FUNC(highlight_white, HIGHLIGHT_WHITE); | |
003aa3e0 | 250 | |
5ee27251 ZJS |
251 | static inline const char* _ansi_highlight_yellow(void) { |
252 | return colors_enabled() ? _ANSI_HIGHLIGHT_YELLOW : ""; | |
253 | } | |
254 | ||
c4fea19a MGR |
255 | DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline, HIGHLIGHT); |
256 | DEFINE_ANSI_FUNC_UNDERLINE_256(grey_underline, GREY, BRIGHT_BLACK); | |
257 | DEFINE_ANSI_FUNC_UNDERLINE(highlight_red_underline, HIGHLIGHT_RED); | |
258 | DEFINE_ANSI_FUNC_UNDERLINE(highlight_green_underline, HIGHLIGHT_GREEN); | |
259 | DEFINE_ANSI_FUNC_UNDERLINE_256(highlight_yellow_underline, HIGHLIGHT_YELLOW, HIGHLIGHT_YELLOW_FALLBACK); | |
260 | DEFINE_ANSI_FUNC_UNDERLINE(highlight_blue_underline, HIGHLIGHT_BLUE); | |
261 | DEFINE_ANSI_FUNC_UNDERLINE(highlight_magenta_underline, HIGHLIGHT_MAGENTA); | |
262 | DEFINE_ANSI_FUNC_UNDERLINE_256(highlight_grey_underline, HIGHLIGHT_GREY, HIGHLIGHT_GREY_FALLBACK); | |
526664f6 | 263 | |
288a74cc RC |
264 | int get_ctty_devnr(pid_t pid, dev_t *d); |
265 | int get_ctty(pid_t, dev_t *_devnr, char **r); | |
266 | ||
267 | int getttyname_malloc(int fd, char **r); | |
268 | int getttyname_harder(int fd, char **r); | |
a07c35c3 | 269 | |
66cb2fde | 270 | int ptsname_malloc(int fd, char **ret); |
66cb2fde | 271 | |
ae1d13db FB |
272 | int openpt_allocate(int flags, char **ret_slave); |
273 | int openpt_allocate_in_namespace(pid_t pid, int flags, char **ret_slave); | |
40e1f4ea | 274 | int open_terminal_in_namespace(pid_t pid, const char *name, int mode); |
c83f349c LP |
275 | |
276 | int vt_default_utf8(void); | |
277 | int vt_reset_keyboard(int fd); | |
6179ede1 | 278 | int vt_restore(int fd); |
27dafac9 | 279 | int vt_release(int fd, bool restore_vt); |
37b8d2f6 ZJS |
280 | |
281 | void get_log_colors(int priority, const char **on, const char **off, const char **highlight); | |
1802d5f2 | 282 | |
312dff17 LP |
283 | static inline const char* ansi_highlight_green_red(bool b) { |
284 | return b ? ansi_highlight_green() : ansi_highlight_red(); | |
285 | } | |
286 | ||
1802d5f2 LP |
287 | /* This assumes there is a 'tty' group */ |
288 | #define TTY_MODE 0620 | |
d02d4f83 LP |
289 | |
290 | void termios_disable_echo(struct termios *termios); |