]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/ttyutils.h
Merge branch 'lscpu-output-fix' of https://github.com/kerolasa/util-linux
[thirdparty/util-linux.git] / include / ttyutils.h
1 /*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 *
5 * Written by Karel Zak <kzak@redhat.com>
6 */
7 #ifndef UTIL_LINUX_TTYUTILS_H
8 #define UTIL_LINUX_TTYUTILS_H
9
10 #include <stdlib.h>
11 #include <termios.h>
12 #include <limits.h>
13 #ifdef HAVE_SYS_IOCTL_H
14 #include <sys/ioctl.h>
15 #endif
16 #ifdef HAVE_SYS_TTYDEFAULTS_H
17 #include <sys/ttydefaults.h>
18 #endif
19
20 /* Some shorthands for control characters. */
21 #define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
22 #define CR CTL('M') /* carriage return */
23 #define NL CTL('J') /* line feed */
24 #define BS CTL('H') /* back space */
25 #define DEL CTL('?') /* delete */
26
27 /* Defaults for line-editing etc. characters; you may want to change these. */
28 #define DEF_ERASE DEL /* default erase character */
29 #define DEF_INTR CTL('C') /* default interrupt character */
30 #define DEF_QUIT CTL('\\') /* default quit char */
31 #define DEF_KILL CTL('U') /* default kill char */
32 #define DEF_EOF CTL('D') /* default EOF char */
33 #define DEF_EOL 0
34 #define DEF_SWITCH 0 /* default switch char */
35
36 /* Fallback for termios->c_cc[] */
37 #ifndef CREPRINT
38 # define CREPRINT ('r' & 037)
39 #endif
40 #ifndef CDISCARD
41 # define CDISCARD ('o' & 037)
42 #endif
43
44 /* Default termios->iflag */
45 #ifndef TTYDEF_IFLAG
46 # define TTYDEF_IFLAG (BRKINT | ICRNL | IMAXBEL | IXON | IXANY)
47 #endif
48
49 /* Default termios->oflag */
50 #ifndef TTYDEF_OFLAG
51 # define TTYDEF_OFLAG (OPOST | ONLCR /*| OXTABS*/)
52 #endif
53
54 /* Default termios->lflag */
55 #ifndef TTYDEF_LFLAG
56 # define TTYDEF_LFLAG (ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL)
57 #endif
58
59 /* Default termios->cflag */
60 #ifndef TTYDEF_CFLAG
61 # define TTYDEF_CFLAG (CREAD | CS8 | HUPCL)
62 #endif
63
64 /* Storage for things detected while the login name was read. */
65 struct chardata {
66 int erase; /* erase character */
67 int kill; /* kill character */
68 int eol; /* end-of-line character */
69 int parity; /* what parity did we see */
70 int capslock; /* upper case without lower case */
71 };
72
73 #define INIT_CHARDATA(ptr) do { \
74 (ptr)->erase = DEF_ERASE; \
75 (ptr)->kill = DEF_KILL; \
76 (ptr)->eol = CTRL('r'); \
77 (ptr)->parity = 0; \
78 (ptr)->capslock = 0; \
79 } while (0)
80
81 extern int get_terminal_dimension(int *cols, int *lines);
82 extern int get_terminal_width(int default_width);
83 extern int get_terminal_type(const char **type);
84 extern int get_terminal_name(const char **path, const char **name,
85 const char **number);
86
87 #define UL_TTY_KEEPCFLAGS (1 << 1)
88 #define UL_TTY_UTF8 (1 << 2)
89
90 static inline void reset_virtual_console(struct termios *tp, int flags)
91 {
92 /* Use defaults of <sys/ttydefaults.h> for base settings */
93 tp->c_iflag |= TTYDEF_IFLAG;
94 tp->c_oflag |= TTYDEF_OFLAG;
95 tp->c_lflag |= TTYDEF_LFLAG;
96
97 if ((flags & UL_TTY_KEEPCFLAGS) == 0) {
98 #ifdef CBAUD
99 tp->c_lflag &= ~CBAUD;
100 #endif
101 tp->c_cflag |= (B38400 | TTYDEF_CFLAG);
102 }
103
104 /* Sane setting, allow eight bit characters, no carriage return delay
105 * the same result as `stty sane cr0 pass8'
106 */
107 #ifndef IUCLC
108 # define IUCLC 0
109 #endif
110 #ifndef NL0
111 # define NL0 0
112 #endif
113 #ifndef CR0
114 # define CR0 0
115 #endif
116 #ifndef BS0
117 # define BS0 0
118 #endif
119 #ifndef VT0
120 # define VT0 0
121 #endif
122 #ifndef FF0
123 # define FF0 0
124 #endif
125 #ifndef OLCUC
126 # define OLCUC 0
127 #endif
128 #ifndef OFILL
129 # define OFILL 0
130 #endif
131 #ifndef NLDLY
132 # define NLDLY 0
133 #endif
134 #ifndef CRDLY
135 # define CRDLY 0
136 #endif
137 #ifndef BSDLY
138 # define BSDLY 0
139 #endif
140 #ifndef VTDLY
141 # define VTDLY 0
142 #endif
143 #ifndef FFDLY
144 # define FFDLY 0
145 #endif
146 #ifndef TAB0
147 # define TAB0 0
148 #endif
149 #ifndef TABDLY
150 # define TABDLY 0
151 #endif
152
153 tp->c_iflag |= (BRKINT | ICRNL | IMAXBEL);
154 tp->c_iflag &= ~(IGNBRK | INLCR | IGNCR | IXOFF | IUCLC | IXANY | ISTRIP);
155 tp->c_oflag |= (OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0);
156 tp->c_oflag &= ~(OLCUC | OCRNL | ONOCR | ONLRET | OFILL | \
157 NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
158 tp->c_lflag |= (ISIG | ICANON | IEXTEN | ECHO|ECHOE|ECHOK|ECHOKE|ECHOCTL);
159 tp->c_lflag &= ~(ECHONL|ECHOPRT | NOFLSH | TOSTOP);
160
161 if ((flags & UL_TTY_KEEPCFLAGS) == 0) {
162 tp->c_cflag |= (CREAD | CS8 | HUPCL);
163 tp->c_cflag &= ~(PARODD | PARENB);
164 }
165 #ifdef OFDEL
166 tp->c_oflag &= ~OFDEL;
167 #endif
168 #ifdef XCASE
169 tp->c_lflag &= ~XCASE;
170 #endif
171 #ifdef IUTF8
172 if (flags & UL_TTY_UTF8)
173 tp->c_iflag |= IUTF8; /* Set UTF-8 input flag */
174 else
175 tp->c_iflag &= ~IUTF8;
176 #endif
177 /* VTIME and VMIN can overlap with VEOF and VEOL since they are
178 * only used for non-canonical mode. We just set the at the
179 * beginning, so nothing bad should happen.
180 */
181 tp->c_cc[VTIME] = 0;
182 tp->c_cc[VMIN] = 1;
183 tp->c_cc[VINTR] = CINTR;
184 tp->c_cc[VQUIT] = CQUIT;
185 tp->c_cc[VERASE] = CERASE; /* ASCII DEL (0177) */
186 tp->c_cc[VKILL] = CKILL;
187 tp->c_cc[VEOF] = CEOF;
188 #ifdef VSWTC
189 tp->c_cc[VSWTC] = _POSIX_VDISABLE;
190 #elif defined(VSWTCH)
191 tp->c_cc[VSWTCH] = _POSIX_VDISABLE;
192 #endif
193 tp->c_cc[VSTART] = CSTART;
194 tp->c_cc[VSTOP] = CSTOP;
195 tp->c_cc[VSUSP] = CSUSP;
196 tp->c_cc[VEOL] = _POSIX_VDISABLE;
197 tp->c_cc[VREPRINT] = CREPRINT;
198 tp->c_cc[VDISCARD] = CDISCARD;
199 tp->c_cc[VWERASE] = CWERASE;
200 tp->c_cc[VLNEXT] = CLNEXT;
201 tp->c_cc[VEOL2] = _POSIX_VDISABLE;
202 }
203
204 #endif /* UTIL_LINUX_TTYUTILS_H */