chiark / gitweb /
Organize the ANSI codes and add missing colors
[elogind.git] / src / basic / terminal-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27
28 #include "macro.h"
29 #include "time-util.h"
30
31 /* Regular colors */
32 #define ANSI_BLACK   "\x1B[0;30m"
33 #define ANSI_RED     "\x1B[0;31m"
34 #define ANSI_GREEN   "\x1B[0;32m"
35 #define ANSI_YELLOW  "\x1B[0;33m"
36 #define ANSI_BLUE    "\x1B[0;34m"
37 #define ANSI_MAGENTA "\x1B[0;35m"
38 #define ANSI_CYAN    "\x1B[0;36m"
39 #define ANSI_WHITE   "\x1B[0;37m"
40
41 /* Bold/highlighted */
42 #define ANSI_HIGHLIGHT_BLACK   "\x1B[0;1;30m"
43 #define ANSI_HIGHLIGHT_RED     "\x1B[0;1;31m"
44 #define ANSI_HIGHLIGHT_GREEN   "\x1B[0;1;32m"
45 #define ANSI_HIGHLIGHT_YELLOW  "\x1B[0;1;33m"
46 #define ANSI_HIGHLIGHT_BLUE    "\x1B[0;1;34m"
47 #define ANSI_HIGHLIGHT_MAGENTA "\x1B[0;1;35m"
48 #define ANSI_HIGHLIGHT_CYAN    "\x1B[0;1;36m"
49 #define ANSI_HIGHLIGHT_WHITE   "\x1B[0;1;37m"
50
51 /* Underlined */
52 #define ANSI_HIGHLIGHT_BLACK_UNDERLINE   "\x1B[0;1;4;30m"
53 #define ANSI_HIGHLIGHT_RED_UNDERLINE     "\x1B[0;1;4;31m"
54 #define ANSI_HIGHLIGHT_GREEN_UNDERLINE   "\x1B[0;1;4;32m"
55 #define ANSI_HIGHLIGHT_YELLOW_UNDERLINE  "\x1B[0;1;4;33m"
56 #define ANSI_HIGHLIGHT_BLUE_UNDERLINE    "\x1B[0;1;4;34m"
57 #define ANSI_HIGHLIGHT_MAGENTA_UNDERLINE "\x1B[0;1;4;35m"
58 #define ANSI_HIGHLIGHT_CYAN_UNDERLINE    "\x1B[0;1;4;36m"
59 #define ANSI_HIGHLIGHT_WHITE_UNDERLINE   "\x1B[0;1;4;37m"
60
61 /* Other ANSI codes */
62 #define ANSI_UNDERLINE "\x1B[0;4m"
63 #define ANSI_HIGHLIGHT "\x1B[0;1;39m"
64 #define ANSI_HIGHLIGHT_UNDERLINE "\x1B[0;1;4m"
65
66 /* Reset/clear ANSI styles */
67 #define ANSI_NORMAL "\x1B[0m"
68
69 /* Erase characters until the end of the line */
70 #define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
71
72 /* Set cursor to top left corner and clear screen */
73 #define ANSI_HOME_CLEAR "\x1B[H\x1B[2J"
74
75 #if 0 /// UNNEEDED by elogind
76 int reset_terminal_fd(int fd, bool switch_to_text);
77 int reset_terminal(const char *name);
78 #endif // 0
79
80 int open_terminal(const char *name, int mode);
81 #if 0 /// UNNEEDED by elogind
82
83 /* Flags for tweaking the way we become the controlling process of a terminal. */
84 typedef enum AcquireTerminalFlags {
85         /* Try to become the controlling process of the TTY. If we can't return -EPERM. */
86         ACQUIRE_TERMINAL_TRY = 0,
87
88         /* Tell the kernel to forcibly make us the controlling process of the TTY. Returns -EPERM if the kernel doesn't allow that. */
89         ACQUIRE_TERMINAL_FORCE = 1,
90
91         /* If we can't become the controlling process of the TTY right-away, then wait until we can. */
92         ACQUIRE_TERMINAL_WAIT = 2,
93
94         /* 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 */
95         ACQUIRE_TERMINAL_PERMISSIVE = 4,
96 } AcquireTerminalFlags;
97
98 int acquire_terminal(const char *name, AcquireTerminalFlags flags, usec_t timeout);
99 int release_terminal(void);
100
101 int terminal_vhangup_fd(int fd);
102 int terminal_vhangup(const char *name);
103 #endif // 0
104
105 int chvt(int vt);
106
107 #if 0 /// UNNEEDED by elogind
108 int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
109 int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
110 int ask_string(char **ret, const char *text, ...) _printf_(2, 3);
111
112 int vt_disallocate(const char *name);
113
114 #endif // 0
115 int resolve_dev_console(char **ret);
116 int get_kernel_consoles(char ***ret);
117 bool tty_is_vc(const char *tty);
118 #if 0 /// UNNEEDED by elogind
119 bool tty_is_vc_resolve(const char *tty);
120 #endif // 0
121 bool tty_is_console(const char *tty) _pure_;
122 int vtnr_from_tty(const char *tty);
123 #if 0 /// UNNEEDED by elogind
124 const char *default_term_for_tty(const char *tty);
125 #endif // 0
126
127 int make_stdio(int fd);
128 int make_null_stdio(void);
129 #if 0 /// UNNEEDED by elogind
130 int make_console_stdio(void);
131 #endif // 0
132
133 int fd_columns(int fd);
134 unsigned columns(void);
135 int fd_lines(int fd);
136 unsigned lines(void);
137
138 #if 0 /// UNNEEDED by elogind
139 void columns_lines_cache_reset(int _unused_ signum);
140 #endif // 0
141 void reset_terminal_feature_caches(void);
142
143 bool on_tty(void);
144 bool terminal_is_dumb(void);
145 bool colors_enabled(void);
146 bool underline_enabled(void);
147 bool dev_console_colors_enabled(void);
148
149 #define DEFINE_ANSI_FUNC(name, NAME)                            \
150         static inline const char *ansi_##name(void) {           \
151                 return colors_enabled() ? ANSI_##NAME : "";     \
152         }                                                       \
153         struct __useless_struct_to_allow_trailing_semicolon__
154
155 #define DEFINE_ANSI_FUNC_UNDERLINE(name, NAME, REPLACEMENT)             \
156         static inline const char *ansi_##name(void) {                   \
157                 return underline_enabled() ? ANSI_##NAME :              \
158                         colors_enabled() ? ANSI_##REPLACEMENT : "";     \
159         }                                                               \
160         struct __useless_struct_to_allow_trailing_semicolon__
161
162
163 DEFINE_ANSI_FUNC(highlight,                  HIGHLIGHT);
164 DEFINE_ANSI_FUNC(highlight_red,              HIGHLIGHT_RED);
165 DEFINE_ANSI_FUNC(highlight_green,            HIGHLIGHT_GREEN);
166 DEFINE_ANSI_FUNC(highlight_yellow,           HIGHLIGHT_YELLOW);
167 DEFINE_ANSI_FUNC(highlight_blue,             HIGHLIGHT_BLUE);
168 DEFINE_ANSI_FUNC(normal,                     NORMAL);
169
170 DEFINE_ANSI_FUNC_UNDERLINE(underline,                  UNDERLINE, NORMAL);
171 DEFINE_ANSI_FUNC_UNDERLINE(highlight_underline,        HIGHLIGHT_UNDERLINE, HIGHLIGHT);
172 DEFINE_ANSI_FUNC_UNDERLINE(highlight_red_underline,    HIGHLIGHT_RED_UNDERLINE, HIGHLIGHT_RED);
173 DEFINE_ANSI_FUNC_UNDERLINE(highlight_green_underline,  HIGHLIGHT_GREEN_UNDERLINE, HIGHLIGHT_GREEN);
174 DEFINE_ANSI_FUNC_UNDERLINE(highlight_yellow_underline, HIGHLIGHT_YELLOW_UNDERLINE, HIGHLIGHT_YELLOW);
175 DEFINE_ANSI_FUNC_UNDERLINE(highlight_blue_underline,   HIGHLIGHT_BLUE_UNDERLINE, HIGHLIGHT_BLUE);
176
177 int get_ctty_devnr(pid_t pid, dev_t *d);
178 int get_ctty(pid_t, dev_t *_devnr, char **r);
179
180 int getttyname_malloc(int fd, char **r);
181 int getttyname_harder(int fd, char **r);
182
183 #if 0 /// UNNEEDED by elogind
184 int ptsname_malloc(int fd, char **ret);
185 int ptsname_namespace(int pty, char **ret);
186
187 int openpt_in_namespace(pid_t pid, int flags);
188 int open_terminal_in_namespace(pid_t pid, const char *name, int mode);
189 #endif // 0
190
191 int vt_default_utf8(void);
192 int vt_reset_keyboard(int fd);