| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-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 |
int terminal_reset_ansi_seq(int fd); |
| 38 |
|
| 39 |
typedef enum TerminalResetFlags { |
| 40 |
TERMINAL_RESET_SWITCH_TO_TEXT = 1 << 0, |
| 41 |
TERMINAL_RESET_AVOID_ANSI_SEQ = 1 << 1, |
| 42 |
TERMINAL_RESET_FORCE_ANSI_SEQ = 1 << 2, |
| 43 |
} TerminalResetFlags; |
| 44 |
|
| 45 |
int terminal_reset_defensive(int fd, TerminalResetFlags flags); |
| 46 |
int terminal_reset_defensive_locked(int fd, TerminalResetFlags flags); |
| 47 |
|
| 48 |
int terminal_set_cursor_position(int fd, unsigned row, unsigned column); |
| 49 |
int terminal_get_cursor_position(int input_fd, int output_fd, unsigned *ret_rows, unsigned *ret_column); |
| 50 |
|
| 51 |
int open_terminal(const char *name, int mode); |
| 52 |
|
| 53 |
/* Flags for tweaking the way we become the controlling process of a terminal. */ |
| 54 |
typedef enum AcquireTerminalFlags { |
| 55 |
/* Try to become the controlling process of the TTY. If we can't return -EPERM. */ |
| 56 |
ACQUIRE_TERMINAL_TRY = 0, |
| 57 |
|
| 58 |
/* Tell the kernel to forcibly make us the controlling process of the TTY. Returns -EPERM if the kernel doesn't allow that. */ |
| 59 |
ACQUIRE_TERMINAL_FORCE = 1, |
| 60 |
|
| 61 |
/* If we can't become the controlling process of the TTY right-away, then wait until we can. */ |
| 62 |
ACQUIRE_TERMINAL_WAIT = 2, |
| 63 |
|
| 64 |
/* The combined mask of the above */ |
| 65 |
_ACQUIRE_TERMINAL_MODE_MASK = ACQUIRE_TERMINAL_TRY | ACQUIRE_TERMINAL_FORCE | ACQUIRE_TERMINAL_WAIT, |
| 66 |
|
| 67 |
/* 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 */ |
| 68 |
ACQUIRE_TERMINAL_PERMISSIVE = 1 << 2, |
| 69 |
|
| 70 |
/* Check for pending SIGTERM while waiting for inotify (SIGTERM must be blocked by caller) */ |
| 71 |
ACQUIRE_TERMINAL_WATCH_SIGTERM = 1 << 3, |
| 72 |
} AcquireTerminalFlags; |
| 73 |
|
| 74 |
int acquire_terminal(const char *name, AcquireTerminalFlags flags, usec_t timeout); |
| 75 |
int release_terminal(void); |
| 76 |
|
| 77 |
int terminal_new_session(void); |
| 78 |
void terminal_detach_session(void); |
| 79 |
|
| 80 |
int terminal_vhangup_fd(int fd); |
| 81 |
int terminal_vhangup(const char *tty); |
| 82 |
|
| 83 |
int terminal_set_size_fd(int fd, const char *ident, unsigned rows, unsigned cols); |
| 84 |
int proc_cmdline_tty_size(const char *tty, unsigned *ret_rows, unsigned *ret_cols); |
| 85 |
|
| 86 |
int chvt(int vt); |
| 87 |
|
| 88 |
int read_one_char(FILE *f, char *ret, usec_t timeout, bool echo, bool *need_nl); |
| 89 |
int ask_char(char *ret, const char *replies, const char *fmt, ...) _printf_(3, 4); |
| 90 |
|
| 91 |
typedef enum GetCompletionsFlags { |
| 92 |
/* Only return the items subject to preselection: typically you want to suppress meta entries such as |
| 93 |
* "list" or alias entries if this flag is set. */ |
| 94 |
GET_COMPLETIONS_PRESELECT = 1 << 0, |
| 95 |
} GetCompletionsFlags; |
| 96 |
|
| 97 |
typedef int (*GetCompletionsCallback)(const char *key, GetCompletionsFlags flags, char ***ret_list, void *userdata); |
| 98 |
int ask_string_full(char **ret, const char *prefill, GetCompletionsCallback get_completions, void *userdata, const char *text, ...) _printf_(5, 6); |
| 99 |
#define ask_string(ret, text, ...) ask_string_full(ret, NULL, NULL, NULL, text, ##__VA_ARGS__) |
| 100 |
|
| 101 |
bool any_key_to_proceed(void); |
| 102 |
int show_menu(char **x, size_t n_columns, size_t column_width, unsigned ellipsize_percentage, const char *grey_prefix, bool with_numbers); |
| 103 |
|
| 104 |
int vt_disallocate(const char *tty_path); |
| 105 |
|
| 106 |
int resolve_dev_console(char **ret); |
| 107 |
int get_kernel_consoles(char ***ret); |
| 108 |
bool tty_is_vc(const char *tty); |
| 109 |
bool tty_is_vc_resolve(const char *tty); |
| 110 |
bool tty_is_console(const char *tty) _pure_; |
| 111 |
int vtnr_from_tty(const char *tty); |
| 112 |
|
| 113 |
void reset_dev_console_fd(int fd, bool switch_to_text); |
| 114 |
int lock_dev_console(void); |
| 115 |
int make_console_stdio(void); |
| 116 |
|
| 117 |
int getenv_columns(void); |
| 118 |
int fd_columns(int fd); |
| 119 |
unsigned columns(void); |
| 120 |
int fd_lines(int fd); |
| 121 |
unsigned lines(void); |
| 122 |
|
| 123 |
void columns_lines_cache_reset(int _unused_ signum); |
| 124 |
void reset_terminal_feature_caches(void); |
| 125 |
|
| 126 |
bool on_tty(void); |
| 127 |
bool term_env_valid(const char *term); |
| 128 |
bool getenv_terminal_is_dumb(void); |
| 129 |
bool terminal_is_dumb(void); |
| 130 |
|
| 131 |
bool dev_console_colors_enabled(void); |
| 132 |
|
| 133 |
int get_ctty_devnr(pid_t pid, dev_t *ret); |
| 134 |
int get_ctty(pid_t pid, dev_t *ret_devnr, char **ret); |
| 135 |
|
| 136 |
int getttyname_malloc(int fd, char **ret); |
| 137 |
int getttyname_harder(int fd, char **ret); |
| 138 |
|
| 139 |
int ptsname_malloc(int fd, char **ret); |
| 140 |
|
| 141 |
int openpt_allocate(int flags, char **ret_peer_path); |
| 142 |
int openpt_allocate_in_namespace(const PidRef *pidref, int flags, char **ret_peer_path); |
| 143 |
|
| 144 |
int vt_restore(int fd); |
| 145 |
int vt_release(int fd, bool restore); |
| 146 |
|
| 147 |
void get_log_colors(int priority, const char **on, const char **off, const char **highlight); |
| 148 |
|
| 149 |
/* Assume TTY_MODE is defined in config.h. Also, this assumes there is a 'tty' group. */ |
| 150 |
assert_cc((TTY_MODE & ~0666) == 0); |
| 151 |
assert_cc((TTY_MODE & 0711) == 0600); |
| 152 |
|
| 153 |
void termios_disable_echo(struct termios *termios); |
| 154 |
|
| 155 |
/* A termios sentinel with all flag fields set to all-ones-bits. No real tcgetattr() result will ever |
| 156 |
* match this because no real terminal configuration uses all-ones in every flag field simultaneously. */ |
| 157 |
#define TERMIOS_NULL (struct termios) { \ |
| 158 |
.c_iflag = UINT_MAX, \ |
| 159 |
.c_oflag = UINT_MAX, \ |
| 160 |
.c_cflag = UINT_MAX, \ |
| 161 |
.c_lflag = UINT_MAX, \ |
| 162 |
} |
| 163 |
|
| 164 |
typedef struct TermiosResetContext { |
| 165 |
int *fd; |
| 166 |
struct termios *termios; |
| 167 |
} TermiosResetContext; |
| 168 |
|
| 169 |
void termios_reset(const TermiosResetContext *c); |
| 170 |
|
| 171 |
#define CLEANUP_TERMIOS_RESET(_fd, _termios) \ |
| 172 |
_cleanup_(termios_reset) _unused_ const TermiosResetContext \ |
| 173 |
CONCATENATE(_cleanup_termios_, UNIQ) = { \ |
| 174 |
.fd = &(_fd), \ |
| 175 |
.termios = &(_termios), \ |
| 176 |
} |
| 177 |
|
| 178 |
/* The $TERM value we use for terminals other than the Linux console */ |
| 179 |
#define FALLBACK_TERM "vt220" |
| 180 |
|
| 181 |
int get_default_background_color(double *ret_red, double *ret_green, double *ret_blue); |
| 182 |
int terminal_get_size(int input_fd, int output_fd, unsigned *ret_rows, unsigned *ret_columns, bool try_dsr, bool try_csi18); |
| 183 |
int terminal_fix_size(int input_fd, int output_fd); |
| 184 |
|
| 185 |
int terminal_get_terminfo_by_dcs(int fd, char **ret_name); |
| 186 |
int have_terminfo_file(const char *name); |
| 187 |
int query_term_for_tty(const char *tty, char **ret_term); |
| 188 |
|
| 189 |
int terminal_is_pty_fd(int fd); |
| 190 |
|
| 191 |
int pty_open_peer(int fd, int mode); |
| 192 |
|
| 193 |
static inline bool osc_char_is_valid(char c) { |
| 194 |
/* Checks whether the specified character is safe to be included inside an ANSI OSC sequence, as per |
| 195 |
* ECMA-48 5th edition, section 8.3.89 */ |
| 196 |
return (unsigned char) c >= 32 && (unsigned char) c < 127; |
| 197 |
} |
| 198 |
|
| 199 |
#define VTNR_MAX 63 |
| 200 |
|
| 201 |
static inline bool vtnr_is_valid(unsigned n) { |
| 202 |
return n >= 1 && n <= VTNR_MAX; |
| 203 |
} |