]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/basic/terminal-util.h
Add systemd-analyze verb to list runtime unit properties (#37665)
[thirdparty/systemd.git] / src / basic / terminal-util.h
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2#pragma once
3
4#include "forward.h"
5
6/* Erase characters until the end of the line */
7#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
8
9/* Erase characters until end of screen */
10#define ANSI_ERASE_TO_END_OF_SCREEN "\x1B[J"
11
12/* Move cursor up one line */
13#define ANSI_REVERSE_LINEFEED "\x1BM"
14
15/* Set cursor to top left corner and clear screen */
16#define ANSI_HOME_CLEAR "\x1B[H\x1B[2J"
17
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
22/* The "device control string" ("DCS") start sequence */
23#define ANSI_DCS "\eP"
24
25/* The "operating system command" ("OSC") start sequence */
26#define ANSI_OSC "\e]"
27
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
35bool isatty_safe(int fd);
36
37typedef enum TerminalResetFlags {
38 TERMINAL_RESET_SWITCH_TO_TEXT = 1 << 0,
39 TERMINAL_RESET_AVOID_ANSI_SEQ = 1 << 1,
40 TERMINAL_RESET_FORCE_ANSI_SEQ = 1 << 2,
41} TerminalResetFlags;
42
43int terminal_reset_defensive(int fd, TerminalResetFlags flags);
44int terminal_reset_defensive_locked(int fd, TerminalResetFlags flags);
45
46int terminal_set_cursor_position(int fd, unsigned row, unsigned column);
47
48int open_terminal(const char *name, int mode);
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. */
53 ACQUIRE_TERMINAL_TRY = 0,
54
55 /* Tell the kernel to forcibly make us the controlling process of the TTY. Returns -EPERM if the kernel doesn't allow that. */
56 ACQUIRE_TERMINAL_FORCE = 1,
57
58 /* If we can't become the controlling process of the TTY right-away, then wait until we can. */
59 ACQUIRE_TERMINAL_WAIT = 2,
60
61 /* The combined mask of the above */
62 _ACQUIRE_TERMINAL_MODE_MASK = ACQUIRE_TERMINAL_TRY | ACQUIRE_TERMINAL_FORCE | ACQUIRE_TERMINAL_WAIT,
63
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 */
65 ACQUIRE_TERMINAL_PERMISSIVE = 1 << 2,
66
67 /* Check for pending SIGTERM while waiting for inotify (SIGTERM must be blocked by caller) */
68 ACQUIRE_TERMINAL_WATCH_SIGTERM = 1 << 3,
69} AcquireTerminalFlags;
70
71int acquire_terminal(const char *name, AcquireTerminalFlags flags, usec_t timeout);
72int release_terminal(void);
73
74int terminal_new_session(void);
75void terminal_detach_session(void);
76
77int terminal_vhangup_fd(int fd);
78int terminal_vhangup(const char *tty);
79
80int terminal_set_size_fd(int fd, const char *ident, unsigned rows, unsigned cols);
81int proc_cmdline_tty_size(const char *tty, unsigned *ret_rows, unsigned *ret_cols);
82
83int chvt(int vt);
84
85int read_one_char(FILE *f, char *ret, usec_t timeout, bool echo, bool *need_nl);
86int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
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
92bool any_key_to_proceed(void);
93int show_menu(char **x, size_t n_columns, size_t column_width, unsigned ellipsize_percentage, const char *grey_prefix, bool with_numbers);
94
95int vt_disallocate(const char *name);
96
97int resolve_dev_console(char **ret);
98int get_kernel_consoles(char ***ret);
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);
103
104void reset_dev_console_fd(int fd, bool switch_to_text);
105int lock_dev_console(void);
106int make_console_stdio(void);
107
108int getenv_columns(void);
109int fd_columns(int fd);
110unsigned columns(void);
111int fd_lines(int fd);
112unsigned lines(void);
113
114void columns_lines_cache_reset(int _unused_ signum);
115void reset_terminal_feature_caches(void);
116
117bool on_tty(void);
118bool getenv_terminal_is_dumb(void);
119bool terminal_is_dumb(void);
120
121bool dev_console_colors_enabled(void);
122
123int get_ctty_devnr(pid_t pid, dev_t *ret);
124int get_ctty(pid_t, dev_t *ret_devnr, char **ret);
125
126int getttyname_malloc(int fd, char **ret);
127int getttyname_harder(int fd, char **ret);
128
129int ptsname_malloc(int fd, char **ret);
130
131int openpt_allocate(int flags, char **ret_peer_path);
132int openpt_allocate_in_namespace(const PidRef *pidref, int flags, char **ret_peer_path);
133
134int vt_restore(int fd);
135int vt_release(int fd, bool restore);
136
137void get_log_colors(int priority, const char **on, const char **off, const char **highlight);
138
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);
142
143void termios_disable_echo(struct termios *termios);
144
145/* The $TERM value we use for terminals other than the Linux console */
146#define FALLBACK_TERM "vt220"
147
148int get_default_background_color(double *ret_red, double *ret_green, double *ret_blue);
149int terminal_get_size_by_dsr(int input_fd, int output_fd, unsigned *ret_rows, unsigned *ret_columns);
150int terminal_fix_size(int input_fd, int output_fd);
151
152int terminal_get_terminfo_by_dcs(int fd, char **ret_name);
153int have_terminfo_file(const char *name);
154int query_term_for_tty(const char *tty, char **ret_term);
155
156int terminal_is_pty_fd(int fd);
157
158int pty_open_peer(int fd, int mode);
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 */
163 return (unsigned char) c >= 32 && (unsigned char) c < 127;
164}
165
166static inline bool vtnr_is_valid(unsigned n) {
167 return n >= 1 && n <= 63;
168}