]>
Commit | Line | Data |
---|---|---|
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 | ||
35 | bool isatty_safe(int fd); | |
36 | ||
37 | typedef 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 | ||
43 | int terminal_reset_defensive(int fd, TerminalResetFlags flags); | |
44 | int terminal_reset_defensive_locked(int fd, TerminalResetFlags flags); | |
45 | ||
46 | int terminal_set_cursor_position(int fd, unsigned row, unsigned column); | |
47 | ||
48 | int open_terminal(const char *name, int mode); | |
49 | ||
50 | /* Flags for tweaking the way we become the controlling process of a terminal. */ | |
51 | typedef 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 | ||
71 | int acquire_terminal(const char *name, AcquireTerminalFlags flags, usec_t timeout); | |
72 | int release_terminal(void); | |
73 | ||
74 | int terminal_new_session(void); | |
75 | void terminal_detach_session(void); | |
76 | ||
77 | int terminal_vhangup_fd(int fd); | |
78 | int terminal_vhangup(const char *tty); | |
79 | ||
80 | int terminal_set_size_fd(int fd, const char *ident, unsigned rows, unsigned cols); | |
81 | int proc_cmdline_tty_size(const char *tty, unsigned *ret_rows, unsigned *ret_cols); | |
82 | ||
83 | int chvt(int vt); | |
84 | ||
85 | int read_one_char(FILE *f, char *ret, usec_t timeout, bool echo, bool *need_nl); | |
86 | int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4); | |
87 | ||
88 | typedef int (*GetCompletionsCallback)(const char *key, char ***ret_list, void *userdata); | |
89 | int 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 | ||
92 | bool any_key_to_proceed(void); | |
93 | int show_menu(char **x, size_t n_columns, size_t column_width, unsigned ellipsize_percentage, const char *grey_prefix, bool with_numbers); | |
94 | ||
95 | int vt_disallocate(const char *name); | |
96 | ||
97 | int resolve_dev_console(char **ret); | |
98 | int get_kernel_consoles(char ***ret); | |
99 | bool tty_is_vc(const char *tty); | |
100 | bool tty_is_vc_resolve(const char *tty); | |
101 | bool tty_is_console(const char *tty) _pure_; | |
102 | int vtnr_from_tty(const char *tty); | |
103 | ||
104 | void reset_dev_console_fd(int fd, bool switch_to_text); | |
105 | int lock_dev_console(void); | |
106 | int make_console_stdio(void); | |
107 | ||
108 | int getenv_columns(void); | |
109 | int fd_columns(int fd); | |
110 | unsigned columns(void); | |
111 | int fd_lines(int fd); | |
112 | unsigned lines(void); | |
113 | ||
114 | void columns_lines_cache_reset(int _unused_ signum); | |
115 | void reset_terminal_feature_caches(void); | |
116 | ||
117 | bool on_tty(void); | |
118 | bool getenv_terminal_is_dumb(void); | |
119 | bool terminal_is_dumb(void); | |
120 | ||
121 | bool dev_console_colors_enabled(void); | |
122 | ||
123 | int get_ctty_devnr(pid_t pid, dev_t *ret); | |
124 | int get_ctty(pid_t, dev_t *ret_devnr, char **ret); | |
125 | ||
126 | int getttyname_malloc(int fd, char **ret); | |
127 | int getttyname_harder(int fd, char **ret); | |
128 | ||
129 | int ptsname_malloc(int fd, char **ret); | |
130 | ||
131 | int openpt_allocate(int flags, char **ret_peer_path); | |
132 | int openpt_allocate_in_namespace(const PidRef *pidref, int flags, char **ret_peer_path); | |
133 | ||
134 | int vt_restore(int fd); | |
135 | int vt_release(int fd, bool restore); | |
136 | ||
137 | void 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. */ | |
140 | assert_cc((TTY_MODE & ~0666) == 0); | |
141 | assert_cc((TTY_MODE & 0711) == 0600); | |
142 | ||
143 | void 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 | ||
148 | int get_default_background_color(double *ret_red, double *ret_green, double *ret_blue); | |
149 | int terminal_get_size_by_dsr(int input_fd, int output_fd, unsigned *ret_rows, unsigned *ret_columns); | |
150 | int terminal_fix_size(int input_fd, int output_fd); | |
151 | ||
152 | int terminal_get_terminfo_by_dcs(int fd, char **ret_name); | |
153 | int have_terminfo_file(const char *name); | |
154 | int query_term_for_tty(const char *tty, char **ret_term); | |
155 | ||
156 | int terminal_is_pty_fd(int fd); | |
157 | ||
158 | int pty_open_peer(int fd, int mode); | |
159 | ||
160 | static 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 | ||
166 | static inline bool vtnr_is_valid(unsigned n) { | |
167 | return n >= 1 && n <= 63; | |
168 | } |