]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/terminal-util.h
Merge pull request #18007 from fw-strlen/ipv6_masq_and_dnat
[thirdparty/systemd.git] / src / basic / terminal-util.h
CommitLineData
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>
288a74cc
RC
9
10#include "macro.h"
11#include "time-util.h"
12
6d1d8f66 13/* Regular colors */
66bb0059 14#define ANSI_BLACK "\x1B[0;30m" /* Some type of grey usually. */
6d1d8f66
AR
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"
66bb0059
ZJS
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. */
23
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"
32
bb40c125 33#define ANSI_GREY "\x1B[0;38;5;245m"
6d1d8f66
AR
34
35/* Bold/highlighted */
5ee27251
ZJS
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"
47
48#define ANSI_HIGHLIGHT_YELLOW ANSI_HIGHLIGHT_KHAKI3 /* Replacement yellow that is more legible */
6d1d8f66
AR
49
50/* Underlined */
45d82c3f 51#define ANSI_GREY_UNDERLINE "\x1B[0;4;38;5;245m"
6d1d8f66
AR
52#define ANSI_HIGHLIGHT_RED_UNDERLINE "\x1B[0;1;4;31m"
53#define ANSI_HIGHLIGHT_GREEN_UNDERLINE "\x1B[0;1;4;32m"
a851ba07 54#define ANSI_HIGHLIGHT_YELLOW_UNDERLINE "\x1B[0;1;4;38;5;185m"
6d1d8f66
AR
55#define ANSI_HIGHLIGHT_BLUE_UNDERLINE "\x1B[0;1;4;34m"
56#define ANSI_HIGHLIGHT_MAGENTA_UNDERLINE "\x1B[0;1;4;35m"
1530bf25 57#define ANSI_HIGHLIGHT_GREY_UNDERLINE "\x1B[0;1;4;38;5;245m"
6d1d8f66
AR
58
59/* Other ANSI codes */
1fc464f6
LP
60#define ANSI_UNDERLINE "\x1B[0;4m"
61#define ANSI_HIGHLIGHT "\x1B[0;1;39m"
1fc464f6 62#define ANSI_HIGHLIGHT_UNDERLINE "\x1B[0;1;4m"
6d1d8f66 63
c4fea19a
MGR
64/* Fallback colors: 256 -> 16 */
65#define ANSI_HIGHLIGHT_GREY_FALLBACK "\x1B[0;1;90m"
66#define ANSI_HIGHLIGHT_YELLOW_FALLBACK "\x1B[0;1;33m"
67
6d1d8f66 68/* Reset/clear ANSI styles */
1fc464f6
LP
69#define ANSI_NORMAL "\x1B[0m"
70
6d1d8f66 71/* Erase characters until the end of the line */
288a74cc
RC
72#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
73
82554307
T
74/* Move cursor up one line */
75#define ANSI_REVERSE_LINEFEED "\x1BM"
76
1fc464f6
LP
77/* Set cursor to top left corner and clear screen */
78#define ANSI_HOME_CLEAR "\x1B[H\x1B[2J"
79
288a74cc
RC
80int reset_terminal_fd(int fd, bool switch_to_text);
81int reset_terminal(const char *name);
82
83int open_terminal(const char *name, int mode);
8854d795
LP
84
85/* Flags for tweaking the way we become the controlling process of a terminal. */
86typedef enum AcquireTerminalFlags {
87 /* Try to become the controlling process of the TTY. If we can't return -EPERM. */
ef31828d 88 ACQUIRE_TERMINAL_TRY = 0,
8854d795
LP
89
90 /* Tell the kernel to forcibly make us the controlling process of the TTY. Returns -EPERM if the kernel doesn't allow that. */
ef31828d 91 ACQUIRE_TERMINAL_FORCE = 1,
8854d795
LP
92
93 /* If we can't become the controlling process of the TTY right-away, then wait until we can. */
ef31828d 94 ACQUIRE_TERMINAL_WAIT = 2,
8854d795
LP
95
96 /* 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 97 ACQUIRE_TERMINAL_PERMISSIVE = 1 << 2,
8854d795
LP
98} AcquireTerminalFlags;
99
c4fea19a
MGR
100/* Limits the use of ANSI colors to a subset. */
101typedef enum ColorMode {
102 /* No colors, monochrome output. */
103 COLOR_OFF = 0,
104
105 /* All colors, no restrictions. */
106 COLOR_ON = 1,
107
108 /* Only the base 16 colors. */
109 COLOR_16 = 16,
110
111 /* Only 256 colors. */
112 COLOR_256 = 256,
113
2d93c20e 114 _COLOR_INVALID = -EINVAL,
c4fea19a
MGR
115} ColorMode;
116
8854d795 117int acquire_terminal(const char *name, AcquireTerminalFlags flags, usec_t timeout);
288a74cc
RC
118int release_terminal(void);
119
120int terminal_vhangup_fd(int fd);
121int terminal_vhangup(const char *name);
122
123int chvt(int vt);
124
125int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
126int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
127int ask_string(char **ret, const char *text, ...) _printf_(2, 3);
128
129int vt_disallocate(const char *name);
130
7b912648 131int resolve_dev_console(char **ret);
bef41af2 132int get_kernel_consoles(char ***ret);
288a74cc
RC
133bool tty_is_vc(const char *tty);
134bool tty_is_vc_resolve(const char *tty);
135bool tty_is_console(const char *tty) _pure_;
136int vtnr_from_tty(const char *tty);
137const char *default_term_for_tty(const char *tty);
138
288a74cc
RC
139int make_console_stdio(void);
140
288a74cc
RC
141int fd_columns(int fd);
142unsigned columns(void);
143int fd_lines(int fd);
144unsigned lines(void);
c6063244 145
288a74cc 146void columns_lines_cache_reset(int _unused_ signum);
c6063244 147void reset_terminal_feature_caches(void);
288a74cc
RC
148
149bool on_tty(void);
ac96418b 150bool terminal_is_dumb(void);
c4fea19a 151ColorMode get_color_mode(void);
526664f6 152bool underline_enabled(void);
c2b32159 153bool dev_console_colors_enabled(void);
288a74cc 154
dcdd9030
MGR
155static inline bool colors_enabled(void) {
156
157 /* Returns true if colors are considered supported on our stdout. */
158 return get_color_mode() != COLOR_OFF;
159}
160
5c98198f
ZJS
161#define DEFINE_ANSI_FUNC(name, NAME) \
162 static inline const char *ansi_##name(void) { \
163 return colors_enabled() ? ANSI_##NAME : ""; \
f6a8265b 164 }
5c98198f 165
c4fea19a
MGR
166#define DEFINE_ANSI_FUNC_256(name, NAME, FALLBACK) \
167 static inline const char *ansi_##name(void) { \
168 switch (get_color_mode()) { \
169 case COLOR_OFF: return ""; \
170 case COLOR_16: return ANSI_##FALLBACK; \
171 default : return ANSI_##NAME; \
172 } \
173 }
174
175#define DEFINE_ANSI_FUNC_UNDERLINE(name, NAME) \
176 static inline const char *ansi_##name(void) { \
177 return underline_enabled() ? ANSI_##NAME ANSI_UNDERLINE : \
178 colors_enabled() ? ANSI_##NAME : ""; \
179 }
180
181
182#define DEFINE_ANSI_FUNC_UNDERLINE_256(name, NAME, FALLBACK) \
183 static inline const char *ansi_##name(void) { \
184 switch (get_color_mode()) { \
185 case COLOR_OFF: return ""; \
186 case COLOR_16: return underline_enabled() ? ANSI_##FALLBACK ANSI_UNDERLINE : ANSI_##FALLBACK; \
187 default : return underline_enabled() ? ANSI_##NAME ANSI_UNDERLINE: ANSI_##NAME; \
188 } \
f6a8265b 189 }
526664f6 190
003aa3e0
LP
191DEFINE_ANSI_FUNC(normal, NORMAL);
192DEFINE_ANSI_FUNC(highlight, HIGHLIGHT);
66bb0059 193DEFINE_ANSI_FUNC(black, BLACK);
003aa3e0
LP
194DEFINE_ANSI_FUNC(red, RED);
195DEFINE_ANSI_FUNC(green, GREEN);
196DEFINE_ANSI_FUNC(yellow, YELLOW);
197DEFINE_ANSI_FUNC(blue, BLUE);
198DEFINE_ANSI_FUNC(magenta, MAGENTA);
66bb0059
ZJS
199DEFINE_ANSI_FUNC(cyan, CYAN);
200DEFINE_ANSI_FUNC(white, WHITE);
c4fea19a 201DEFINE_ANSI_FUNC_256(grey, GREY, BRIGHT_BLACK);
66bb0059
ZJS
202
203DEFINE_ANSI_FUNC(bright_black, BRIGHT_BLACK);
204DEFINE_ANSI_FUNC(bright_red, BRIGHT_RED);
205DEFINE_ANSI_FUNC(bright_green, BRIGHT_GREEN);
206DEFINE_ANSI_FUNC(bright_yellow, BRIGHT_YELLOW);
207DEFINE_ANSI_FUNC(bright_blue, BRIGHT_BLUE);
208DEFINE_ANSI_FUNC(bright_magenta, BRIGHT_MAGENTA);
209DEFINE_ANSI_FUNC(bright_cyan, BRIGHT_CYAN);
210DEFINE_ANSI_FUNC(bright_white, BRIGHT_WHITE);
211
c4fea19a
MGR
212DEFINE_ANSI_FUNC(highlight_black, HIGHLIGHT_BLACK);
213DEFINE_ANSI_FUNC(highlight_red, HIGHLIGHT_RED);
214DEFINE_ANSI_FUNC(highlight_green, HIGHLIGHT_GREEN);
215DEFINE_ANSI_FUNC_256(highlight_yellow, HIGHLIGHT_YELLOW, HIGHLIGHT_YELLOW_FALLBACK);
25e4608b 216DEFINE_ANSI_FUNC_256(highlight_yellow4, HIGHLIGHT_YELLOW4, HIGHLIGHT_YELLOW_FALLBACK);
c4fea19a
MGR
217DEFINE_ANSI_FUNC(highlight_blue, HIGHLIGHT_BLUE);
218DEFINE_ANSI_FUNC(highlight_magenta, HIGHLIGHT_MAGENTA);
219DEFINE_ANSI_FUNC(highlight_cyan, HIGHLIGHT_CYAN);
220DEFINE_ANSI_FUNC_256(highlight_grey, HIGHLIGHT_GREY, HIGHLIGHT_GREY_FALLBACK);
221DEFINE_ANSI_FUNC(highlight_white, HIGHLIGHT_WHITE);
003aa3e0 222
5ee27251
ZJS
223static inline const char* _ansi_highlight_yellow(void) {
224 return colors_enabled() ? _ANSI_HIGHLIGHT_YELLOW : "";
225}
226
c4fea19a
MGR
227DEFINE_ANSI_FUNC_UNDERLINE(underline, NORMAL);
228DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline, HIGHLIGHT);
229DEFINE_ANSI_FUNC_UNDERLINE_256(grey_underline, GREY, BRIGHT_BLACK);
230DEFINE_ANSI_FUNC_UNDERLINE(highlight_red_underline, HIGHLIGHT_RED);
231DEFINE_ANSI_FUNC_UNDERLINE(highlight_green_underline, HIGHLIGHT_GREEN);
232DEFINE_ANSI_FUNC_UNDERLINE_256(highlight_yellow_underline, HIGHLIGHT_YELLOW, HIGHLIGHT_YELLOW_FALLBACK);
233DEFINE_ANSI_FUNC_UNDERLINE(highlight_blue_underline, HIGHLIGHT_BLUE);
234DEFINE_ANSI_FUNC_UNDERLINE(highlight_magenta_underline, HIGHLIGHT_MAGENTA);
235DEFINE_ANSI_FUNC_UNDERLINE_256(highlight_grey_underline, HIGHLIGHT_GREY, HIGHLIGHT_GREY_FALLBACK);
526664f6 236
288a74cc
RC
237int get_ctty_devnr(pid_t pid, dev_t *d);
238int get_ctty(pid_t, dev_t *_devnr, char **r);
239
240int getttyname_malloc(int fd, char **r);
241int getttyname_harder(int fd, char **r);
a07c35c3 242
66cb2fde 243int ptsname_malloc(int fd, char **ret);
66cb2fde 244
ae1d13db
FB
245int openpt_allocate(int flags, char **ret_slave);
246int openpt_allocate_in_namespace(pid_t pid, int flags, char **ret_slave);
40e1f4ea 247int open_terminal_in_namespace(pid_t pid, const char *name, int mode);
c83f349c
LP
248
249int vt_default_utf8(void);
250int vt_reset_keyboard(int fd);
6179ede1 251int vt_restore(int fd);
27dafac9 252int vt_release(int fd, bool restore_vt);
37b8d2f6
ZJS
253
254void get_log_colors(int priority, const char **on, const char **off, const char **highlight);
1802d5f2
LP
255
256/* This assumes there is a 'tty' group */
257#define TTY_MODE 0620