1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "time-util.h"
14 #define ANSI_BLACK "\x1B[0;30m" /* Some type of grey usually. */
15 #define ANSI_RED "\x1B[0;31m"
16 #define ANSI_GREEN "\x1B[0;32m"
17 #define ANSI_YELLOW "\x1B[0;33m"
18 #define ANSI_BLUE "\x1B[0;34m"
19 #define ANSI_MAGENTA "\x1B[0;35m"
20 #define ANSI_CYAN "\x1B[0;36m"
21 #define ANSI_WHITE "\x1B[0;37m" /* This is actually rendered as light grey, legible even on a white
22 * background. See ANSI_HIGHLIGHT_WHITE for real white. */
24 #define ANSI_BRIGHT_BLACK "\x1B[0;90m"
25 #define ANSI_BRIGHT_RED "\x1B[0;91m"
26 #define ANSI_BRIGHT_GREEN "\x1B[0;92m"
27 #define ANSI_BRIGHT_YELLOW "\x1B[0;93m"
28 #define ANSI_BRIGHT_BLUE "\x1B[0;94m"
29 #define ANSI_BRIGHT_MAGENTA "\x1B[0;95m"
30 #define ANSI_BRIGHT_CYAN "\x1B[0;96m"
31 #define ANSI_BRIGHT_WHITE "\x1B[0;97m"
33 #define ANSI_GREY "\x1B[0;38;5;245m"
35 /* Bold/highlighted */
36 #define ANSI_HIGHLIGHT_BLACK "\x1B[0;1;30m"
37 #define ANSI_HIGHLIGHT_RED "\x1B[0;1;31m"
38 #define ANSI_HIGHLIGHT_GREEN "\x1B[0;1;32m"
39 #define _ANSI_HIGHLIGHT_YELLOW "\x1B[0;1;33m" /* This yellow is currently not displayed well by some terminals */
40 #define ANSI_HIGHLIGHT_BLUE "\x1B[0;1;34m"
41 #define ANSI_HIGHLIGHT_MAGENTA "\x1B[0;1;35m"
42 #define ANSI_HIGHLIGHT_CYAN "\x1B[0;1;36m"
43 #define ANSI_HIGHLIGHT_WHITE "\x1B[0;1;37m"
44 #define ANSI_HIGHLIGHT_YELLOW4 "\x1B[0;1;38;5;100m"
45 #define ANSI_HIGHLIGHT_KHAKI3 "\x1B[0;1;38;5;185m"
46 #define ANSI_HIGHLIGHT_GREY "\x1B[0;1;38;5;245m"
48 #define ANSI_HIGHLIGHT_YELLOW ANSI_HIGHLIGHT_KHAKI3 /* Replacement yellow that is more legible */
51 #define ANSI_GREY_UNDERLINE "\x1B[0;4;38;5;245m"
52 #define ANSI_BRIGHT_BLACK_UNDERLINE "\x1B[0;4;90m"
53 #define ANSI_HIGHLIGHT_RED_UNDERLINE "\x1B[0;1;4;31m"
54 #define ANSI_HIGHLIGHT_GREEN_UNDERLINE "\x1B[0;1;4;32m"
55 #define ANSI_HIGHLIGHT_YELLOW_UNDERLINE "\x1B[0;1;4;38;5;185m"
56 #define ANSI_HIGHLIGHT_BLUE_UNDERLINE "\x1B[0;1;4;34m"
57 #define ANSI_HIGHLIGHT_MAGENTA_UNDERLINE "\x1B[0;1;4;35m"
58 #define ANSI_HIGHLIGHT_GREY_UNDERLINE "\x1B[0;1;4;38;5;245m"
60 /* Other ANSI codes */
61 #define ANSI_UNDERLINE "\x1B[0;4m"
62 #define ANSI_ADD_UNDERLINE "\x1B[4m"
63 #define ANSI_ADD_UNDERLINE_GREY ANSI_ADD_UNDERLINE "\x1B[58;5;245m"
64 #define ANSI_HIGHLIGHT "\x1B[0;1;39m"
65 #define ANSI_HIGHLIGHT_UNDERLINE "\x1B[0;1;4m"
67 /* Fallback colors: 256 -> 16 */
68 #define ANSI_HIGHLIGHT_GREY_FALLBACK "\x1B[0;1;90m"
69 #define ANSI_HIGHLIGHT_GREY_FALLBACK_UNDERLINE "\x1B[0;1;4;90m"
70 #define ANSI_HIGHLIGHT_YELLOW_FALLBACK "\x1B[0;1;33m"
71 #define ANSI_HIGHLIGHT_YELLOW_FALLBACK_UNDERLINE "\x1B[0;1;4;33m"
73 /* Background colors */
74 #define ANSI_BACKGROUND_BLUE "\x1B[44m"
76 /* Reset/clear ANSI styles */
77 #define ANSI_NORMAL "\x1B[0m"
79 /* Erase characters until the end of the line */
80 #define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
82 /* Move cursor up one line */
83 #define ANSI_REVERSE_LINEFEED "\x1BM"
85 /* Set cursor to top left corner and clear screen */
86 #define ANSI_HOME_CLEAR "\x1B[H\x1B[2J"
88 int reset_terminal_fd(int fd
, bool switch_to_text
);
89 int reset_terminal(const char *name
);
90 int set_terminal_cursor_position(int fd
, unsigned int row
, unsigned int column
);
92 int open_terminal(const char *name
, int mode
);
94 /* Flags for tweaking the way we become the controlling process of a terminal. */
95 typedef enum AcquireTerminalFlags
{
96 /* Try to become the controlling process of the TTY. If we can't return -EPERM. */
97 ACQUIRE_TERMINAL_TRY
= 0,
99 /* Tell the kernel to forcibly make us the controlling process of the TTY. Returns -EPERM if the kernel doesn't allow that. */
100 ACQUIRE_TERMINAL_FORCE
= 1,
102 /* If we can't become the controlling process of the TTY right-away, then wait until we can. */
103 ACQUIRE_TERMINAL_WAIT
= 2,
105 /* 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 */
106 ACQUIRE_TERMINAL_PERMISSIVE
= 1 << 2,
107 } AcquireTerminalFlags
;
109 /* Limits the use of ANSI colors to a subset. */
110 typedef enum ColorMode
{
111 /* No colors, monochrome output. */
114 /* All colors, no restrictions. */
117 /* Only the base 16 colors. */
120 /* Only 256 colors. */
123 /* For truecolor or 24bit color support. */
126 _COLOR_INVALID
= -EINVAL
,
129 int acquire_terminal(const char *name
, AcquireTerminalFlags flags
, usec_t timeout
);
130 int release_terminal(void);
132 int terminal_vhangup_fd(int fd
);
133 int terminal_vhangup(const char *name
);
135 int terminal_set_size_fd(int fd
, const char *ident
, unsigned rows
, unsigned cols
);
136 int proc_cmdline_tty_size(const char *tty
, unsigned *ret_rows
, unsigned *ret_cols
);
140 int read_one_char(FILE *f
, char *ret
, usec_t timeout
, bool *need_nl
);
141 int ask_char(char *ret
, const char *replies
, const char *text
, ...) _printf_(3, 4);
142 int ask_string(char **ret
, const char *text
, ...) _printf_(2, 3);
144 int vt_disallocate(const char *name
);
146 int resolve_dev_console(char **ret
);
147 int get_kernel_consoles(char ***ret
);
148 bool tty_is_vc(const char *tty
);
149 bool tty_is_vc_resolve(const char *tty
);
150 bool tty_is_console(const char *tty
) _pure_
;
151 int vtnr_from_tty(const char *tty
);
152 const char *default_term_for_tty(const char *tty
);
154 int make_console_stdio(void);
156 int fd_columns(int fd
);
157 unsigned columns(void);
158 int fd_lines(int fd
);
159 unsigned lines(void);
161 void columns_lines_cache_reset(int _unused_ signum
);
162 void reset_terminal_feature_caches(void);
165 bool terminal_is_dumb(void);
166 ColorMode
get_color_mode(void);
167 bool underline_enabled(void);
168 bool dev_console_colors_enabled(void);
170 static inline bool colors_enabled(void) {
172 /* Returns true if colors are considered supported on our stdout. */
173 return get_color_mode() != COLOR_OFF
;
176 #define DEFINE_ANSI_FUNC(name, NAME) \
177 static inline const char *ansi_##name(void) { \
178 return colors_enabled() ? ANSI_##NAME : ""; \
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; \
190 static inline const char *ansi_underline(void) {
191 return underline_enabled() ? ANSI_UNDERLINE
: ANSI_NORMAL
;
194 static inline const char *ansi_add_underline(void) {
195 return underline_enabled() ? ANSI_ADD_UNDERLINE
: "";
198 static inline const char *ansi_add_underline_grey(void) {
199 return underline_enabled() ?
200 (colors_enabled() ? ANSI_ADD_UNDERLINE_GREY
: ANSI_ADD_UNDERLINE
) : "";
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 : ""; \
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 ""; \
214 case COLOR_16: return underline_enabled() ? ANSI_##FALLBACK##_UNDERLINE : ANSI_##FALLBACK; \
215 default : return underline_enabled() ? ANSI_##NAME##_UNDERLINE: ANSI_##NAME; \
219 DEFINE_ANSI_FUNC(normal
, NORMAL
);
220 DEFINE_ANSI_FUNC(highlight
, HIGHLIGHT
);
221 DEFINE_ANSI_FUNC(black
, BLACK
);
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
);
227 DEFINE_ANSI_FUNC(cyan
, CYAN
);
228 DEFINE_ANSI_FUNC(white
, WHITE
);
229 DEFINE_ANSI_FUNC_256(grey
, GREY
, BRIGHT_BLACK
);
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
);
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
);
244 DEFINE_ANSI_FUNC_256(highlight_yellow4
, HIGHLIGHT_YELLOW4
, HIGHLIGHT_YELLOW_FALLBACK
);
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
);
251 static inline const char* _ansi_highlight_yellow(void) {
252 return colors_enabled() ? _ANSI_HIGHLIGHT_YELLOW
: "";
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
);
264 int get_ctty_devnr(pid_t pid
, dev_t
*d
);
265 int get_ctty(pid_t
, dev_t
*_devnr
, char **r
);
267 int getttyname_malloc(int fd
, char **r
);
268 int getttyname_harder(int fd
, char **r
);
270 int ptsname_malloc(int fd
, char **ret
);
272 int openpt_allocate(int flags
, char **ret_slave
);
273 int openpt_allocate_in_namespace(pid_t pid
, int flags
, char **ret_slave
);
274 int open_terminal_in_namespace(pid_t pid
, const char *name
, int mode
);
276 int vt_default_utf8(void);
277 int vt_reset_keyboard(int fd
);
278 int vt_restore(int fd
);
279 int vt_release(int fd
, bool restore_vt
);
281 void get_log_colors(int priority
, const char **on
, const char **off
, const char **highlight
);
283 static inline const char* ansi_highlight_green_red(bool b
) {
284 return b
? ansi_highlight_green() : ansi_highlight_red();
287 /* This assumes there is a 'tty' group */
288 #define TTY_MODE 0620