]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/terminal-util.h
openssl-util: allow to build with openssl without UI support (#38041)
[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
0c15577a 4#include "forward.h"
288a74cc 5
6d1d8f66 6/* Erase characters until the end of the line */
288a74cc
RC
7#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
8
e4924fb0
LP
9/* Erase characters until end of screen */
10#define ANSI_ERASE_TO_END_OF_SCREEN "\x1B[J"
11
82554307
T
12/* Move cursor up one line */
13#define ANSI_REVERSE_LINEFEED "\x1BM"
14
1fc464f6
LP
15/* Set cursor to top left corner and clear screen */
16#define ANSI_HOME_CLEAR "\x1B[H\x1B[2J"
17
ddba81d8
LP
18/* Push/pop a window title off the stack of window titles */
19#define ANSI_WINDOW_TITLE_PUSH "\x1b[22;2t"
20#define ANSI_WINDOW_TITLE_POP "\x1b[23;2t"
21
5321b957
ZJS
22/* The "device control string" ("DCS") start sequence */
23#define ANSI_DCS "\eP"
b8311af8 24
03674247
LP
25/* The "operating system command" ("OSC") start sequence */
26#define ANSI_OSC "\e]"
27
5321b957
ZJS
28/* ANSI "string terminator" character ("ST"). Terminal emulators typically allow three different ones: 0x07,
29 * 0x9c, and 0x1B 0x5C. We'll avoid 0x07 (BEL, aka ^G) since it might trigger unexpected TTY signal handling.
30 * And we'll avoid 0x9c since that's also valid regular codepoint in UTF-8 and elsewhere, and creates
31 * ambiguities. Because of that some terminal emulators explicitly choose not to support it. Hence we use
32 * 0x1B 0x5c. */
33#define ANSI_ST "\e\\"
34
76270f5c
MY
35bool isatty_safe(int fd);
36
9ab703d8
LP
37typedef enum TerminalResetFlags {
38 TERMINAL_RESET_SWITCH_TO_TEXT = 1 << 0,
5b3eaf9e
LP
39 TERMINAL_RESET_AVOID_ANSI_SEQ = 1 << 1,
40 TERMINAL_RESET_FORCE_ANSI_SEQ = 1 << 2,
9ab703d8
LP
41} TerminalResetFlags;
42
43int terminal_reset_defensive(int fd, TerminalResetFlags flags);
44int terminal_reset_defensive_locked(int fd, TerminalResetFlags flags);
288a74cc 45
53f0ab51
LP
46int terminal_set_cursor_position(int fd, unsigned row, unsigned column);
47
288a74cc 48int open_terminal(const char *name, int mode);
8854d795
LP
49
50/* Flags for tweaking the way we become the controlling process of a terminal. */
51typedef enum AcquireTerminalFlags {
52 /* Try to become the controlling process of the TTY. If we can't return -EPERM. */
ef31828d 53 ACQUIRE_TERMINAL_TRY = 0,
8854d795
LP
54
55 /* Tell the kernel to forcibly make us the controlling process of the TTY. Returns -EPERM if the kernel doesn't allow that. */
ef31828d 56 ACQUIRE_TERMINAL_FORCE = 1,
8854d795
LP
57
58 /* If we can't become the controlling process of the TTY right-away, then wait until we can. */
ef31828d 59 ACQUIRE_TERMINAL_WAIT = 2,
8854d795 60
789f4f7e
LP
61 /* The combined mask of the above */
62 _ACQUIRE_TERMINAL_MODE_MASK = ACQUIRE_TERMINAL_TRY | ACQUIRE_TERMINAL_FORCE | ACQUIRE_TERMINAL_WAIT,
63
8854d795 64 /* 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 65 ACQUIRE_TERMINAL_PERMISSIVE = 1 << 2,
789f4f7e
LP
66
67 /* Check for pending SIGTERM while waiting for inotify (SIGTERM must be blocked by caller) */
68 ACQUIRE_TERMINAL_WATCH_SIGTERM = 1 << 3,
8854d795
LP
69} AcquireTerminalFlags;
70
7147e10c
LP
71int acquire_terminal(const char *name, AcquireTerminalFlags flags, usec_t timeout);
72int release_terminal(void);
73
1de12823 74int terminal_new_session(void);
0c15577a 75void terminal_detach_session(void);
1de12823 76
288a74cc 77int terminal_vhangup_fd(int fd);
bc3477fd 78int terminal_vhangup(const char *tty);
288a74cc 79
51462135 80int terminal_set_size_fd(int fd, const char *ident, unsigned rows, unsigned cols);
29f5a5ae 81int proc_cmdline_tty_size(const char *tty, unsigned *ret_rows, unsigned *ret_cols);
51462135 82
288a74cc
RC
83int chvt(int vt);
84
8fcd8576 85int read_one_char(FILE *f, char *ret, usec_t timeout, bool echo, bool *need_nl);
288a74cc 86int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
94a2b1cd
LP
87
88typedef int (*GetCompletionsCallback)(const char *key, char ***ret_list, void *userdata);
89int ask_string_full(char **ret, GetCompletionsCallback cb, void *userdata, const char *text, ...) _printf_(4, 5);
90#define ask_string(ret, text, ...) ask_string_full(ret, NULL, NULL, text, ##__VA_ARGS__)
91
91ea3dcf 92bool any_key_to_proceed(void);
b6478aa1 93int show_menu(char **x, size_t n_columns, size_t column_width, unsigned ellipsize_percentage, const char *grey_prefix, bool with_numbers);
288a74cc
RC
94
95int vt_disallocate(const char *name);
96
7b912648 97int resolve_dev_console(char **ret);
bef41af2 98int get_kernel_consoles(char ***ret);
288a74cc
RC
99bool tty_is_vc(const char *tty);
100bool tty_is_vc_resolve(const char *tty);
101bool tty_is_console(const char *tty) _pure_;
102int vtnr_from_tty(const char *tty);
288a74cc 103
2736295d 104void reset_dev_console_fd(int fd, bool switch_to_text);
4a24cc85 105int lock_dev_console(void);
288a74cc
RC
106int make_console_stdio(void);
107
e8139b15 108int getenv_columns(void);
288a74cc
RC
109int fd_columns(int fd);
110unsigned columns(void);
111int fd_lines(int fd);
112unsigned lines(void);
c6063244 113
288a74cc 114void columns_lines_cache_reset(int _unused_ signum);
c6063244 115void reset_terminal_feature_caches(void);
288a74cc
RC
116
117bool on_tty(void);
1b889631 118bool getenv_terminal_is_dumb(void);
ac96418b 119bool terminal_is_dumb(void);
288a74cc 120
217bd588
MY
121bool dev_console_colors_enabled(void);
122
ac508b11
LP
123int get_ctty_devnr(pid_t pid, dev_t *ret);
124int get_ctty(pid_t, dev_t *ret_devnr, char **ret);
288a74cc 125
ac508b11
LP
126int getttyname_malloc(int fd, char **ret);
127int getttyname_harder(int fd, char **ret);
a07c35c3 128
66cb2fde 129int ptsname_malloc(int fd, char **ret);
66cb2fde 130
274da0b6
JW
131int openpt_allocate(int flags, char **ret_peer_path);
132int openpt_allocate_in_namespace(const PidRef *pidref, int flags, char **ret_peer_path);
c83f349c 133
6179ede1 134int vt_restore(int fd);
4827c9fd 135int vt_release(int fd, bool restore);
37b8d2f6
ZJS
136
137void get_log_colors(int priority, const char **on, const char **off, const char **highlight);
1802d5f2 138
a4d18914
YW
139/* Assume TTY_MODE is defined in config.h. Also, this assumes there is a 'tty' group. */
140assert_cc((TTY_MODE & ~0666) == 0);
141assert_cc((TTY_MODE & 0711) == 0600);
d02d4f83
LP
142
143void termios_disable_echo(struct termios *termios);
63e9c383 144
ad6ca4a6
ZJS
145/* The $TERM value we use for terminals other than the Linux console */
146#define FALLBACK_TERM "vt220"
147
63e9c383 148int get_default_background_color(double *ret_red, double *ret_green, double *ret_blue);
3390be38 149int terminal_get_size_by_dsr(int input_fd, int output_fd, unsigned *ret_rows, unsigned *ret_columns);
63c631d7 150int terminal_fix_size(int input_fd, int output_fd);
ce3a1593 151
5321b957 152int terminal_get_terminfo_by_dcs(int fd, char **ret_name);
e3b050a5
ZJS
153int have_terminfo_file(const char *name);
154int query_term_for_tty(const char *tty, char **ret_term);
5321b957 155
ce3a1593 156int terminal_is_pty_fd(int fd);
fc9dc71a 157
fc9dc71a 158int pty_open_peer(int fd, int mode);
0823d96a
LP
159
160static inline bool osc_char_is_valid(char c) {
161 /* Checks whether the specified character is safe to be included inside an ANSI OSC sequence, as per
162 * ECMA-48 5th edition, section 8.3.89 */
5b5545f1 163 return (unsigned char) c >= 32 && (unsigned char) c < 127;
0823d96a 164}
57e55f93
LP
165
166static inline bool vtnr_is_valid(unsigned n) {
167 return n >= 1 && n <= 63;
168}