]>
Commit | Line | Data |
---|---|---|
0f23ee0c KZ |
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 | */ | |
0b6198ec DR |
7 | #ifndef UTIL_LINUX_TTYUTILS_H |
8 | #define UTIL_LINUX_TTYUTILS_H | |
879a7ab4 | 9 | |
e872e320 | 10 | #include <stdlib.h> |
879a7ab4 | 11 | #include <termios.h> |
7e9a9af1 | 12 | #include <limits.h> |
e872e320 PU |
13 | #ifdef HAVE_SYS_IOCTL_H |
14 | #include <sys/ioctl.h> | |
15 | #endif | |
00c505d9 MF |
16 | #ifdef HAVE_SYS_TTYDEFAULTS_H |
17 | #include <sys/ttydefaults.h> | |
18 | #endif | |
879a7ab4 | 19 | |
f5664477 WF |
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 | ||
67129513 SJ |
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 | ||
a73f59fa KZ |
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 | ||
f5664477 WF |
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 | ||
f46a8d7e | 81 | extern int get_terminal_dimension(int *cols, int *lines); |
43b4f7ea | 82 | extern int get_terminal_width(int default_width); |
4d9b788d | 83 | extern int get_terminal_type(const char **type); |
a6d9d23b | 84 | extern int get_terminal_stdfd(void); |
285c1f3a | 85 | extern int get_terminal_name(const char **path, const char **name, |
507341f8 | 86 | const char **number); |
4e76adb0 | 87 | |
879a7ab4 KZ |
88 | #define UL_TTY_KEEPCFLAGS (1 << 1) |
89 | #define UL_TTY_UTF8 (1 << 2) | |
90 | ||
91 | static inline void reset_virtual_console(struct termios *tp, int flags) | |
92 | { | |
93 | /* Use defaults of <sys/ttydefaults.h> for base settings */ | |
94 | tp->c_iflag |= TTYDEF_IFLAG; | |
95 | tp->c_oflag |= TTYDEF_OFLAG; | |
96 | tp->c_lflag |= TTYDEF_LFLAG; | |
97 | ||
98 | if ((flags & UL_TTY_KEEPCFLAGS) == 0) { | |
99 | #ifdef CBAUD | |
100 | tp->c_lflag &= ~CBAUD; | |
101 | #endif | |
102 | tp->c_cflag |= (B38400 | TTYDEF_CFLAG); | |
103 | } | |
104 | ||
105 | /* Sane setting, allow eight bit characters, no carriage return delay | |
106 | * the same result as `stty sane cr0 pass8' | |
107 | */ | |
c32270d4 CE |
108 | #ifndef IUCLC |
109 | # define IUCLC 0 | |
110 | #endif | |
111 | #ifndef NL0 | |
112 | # define NL0 0 | |
113 | #endif | |
114 | #ifndef CR0 | |
115 | # define CR0 0 | |
116 | #endif | |
117 | #ifndef BS0 | |
118 | # define BS0 0 | |
119 | #endif | |
120 | #ifndef VT0 | |
121 | # define VT0 0 | |
122 | #endif | |
123 | #ifndef FF0 | |
124 | # define FF0 0 | |
125 | #endif | |
126 | #ifndef OLCUC | |
127 | # define OLCUC 0 | |
128 | #endif | |
129 | #ifndef OFILL | |
130 | # define OFILL 0 | |
131 | #endif | |
132 | #ifndef NLDLY | |
133 | # define NLDLY 0 | |
134 | #endif | |
135 | #ifndef CRDLY | |
136 | # define CRDLY 0 | |
137 | #endif | |
138 | #ifndef BSDLY | |
139 | # define BSDLY 0 | |
140 | #endif | |
141 | #ifndef VTDLY | |
142 | # define VTDLY 0 | |
143 | #endif | |
144 | #ifndef FFDLY | |
145 | # define FFDLY 0 | |
67129513 SJ |
146 | #endif |
147 | #ifndef TAB0 | |
148 | # define TAB0 0 | |
149 | #endif | |
150 | #ifndef TABDLY | |
151 | # define TABDLY 0 | |
c32270d4 CE |
152 | #endif |
153 | ||
879a7ab4 KZ |
154 | tp->c_iflag |= (BRKINT | ICRNL | IMAXBEL); |
155 | tp->c_iflag &= ~(IGNBRK | INLCR | IGNCR | IXOFF | IUCLC | IXANY | ISTRIP); | |
156 | tp->c_oflag |= (OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0); | |
157 | tp->c_oflag &= ~(OLCUC | OCRNL | ONOCR | ONLRET | OFILL | \ | |
158 | NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY); | |
b8b8e768 KZ |
159 | tp->c_lflag |= (ISIG | ICANON | IEXTEN | ECHO|ECHOE|ECHOK|ECHOKE|ECHOCTL); |
160 | tp->c_lflag &= ~(ECHONL|ECHOPRT | NOFLSH | TOSTOP); | |
879a7ab4 KZ |
161 | |
162 | if ((flags & UL_TTY_KEEPCFLAGS) == 0) { | |
163 | tp->c_cflag |= (CREAD | CS8 | HUPCL); | |
164 | tp->c_cflag &= ~(PARODD | PARENB); | |
165 | } | |
166 | #ifdef OFDEL | |
167 | tp->c_oflag &= ~OFDEL; | |
168 | #endif | |
169 | #ifdef XCASE | |
170 | tp->c_lflag &= ~XCASE; | |
171 | #endif | |
172 | #ifdef IUTF8 | |
173 | if (flags & UL_TTY_UTF8) | |
174 | tp->c_iflag |= IUTF8; /* Set UTF-8 input flag */ | |
175 | else | |
176 | tp->c_iflag &= ~IUTF8; | |
177 | #endif | |
178 | /* VTIME and VMIN can overlap with VEOF and VEOL since they are | |
179 | * only used for non-canonical mode. We just set the at the | |
180 | * beginning, so nothing bad should happen. | |
181 | */ | |
182 | tp->c_cc[VTIME] = 0; | |
183 | tp->c_cc[VMIN] = 1; | |
184 | tp->c_cc[VINTR] = CINTR; | |
185 | tp->c_cc[VQUIT] = CQUIT; | |
186 | tp->c_cc[VERASE] = CERASE; /* ASCII DEL (0177) */ | |
187 | tp->c_cc[VKILL] = CKILL; | |
188 | tp->c_cc[VEOF] = CEOF; | |
189 | #ifdef VSWTC | |
190 | tp->c_cc[VSWTC] = _POSIX_VDISABLE; | |
191 | #elif defined(VSWTCH) | |
192 | tp->c_cc[VSWTCH] = _POSIX_VDISABLE; | |
193 | #endif | |
194 | tp->c_cc[VSTART] = CSTART; | |
195 | tp->c_cc[VSTOP] = CSTOP; | |
196 | tp->c_cc[VSUSP] = CSUSP; | |
197 | tp->c_cc[VEOL] = _POSIX_VDISABLE; | |
198 | tp->c_cc[VREPRINT] = CREPRINT; | |
199 | tp->c_cc[VDISCARD] = CDISCARD; | |
200 | tp->c_cc[VWERASE] = CWERASE; | |
201 | tp->c_cc[VLNEXT] = CLNEXT; | |
202 | tp->c_cc[VEOL2] = _POSIX_VDISABLE; | |
203 | } | |
0b6198ec DR |
204 | |
205 | #endif /* UTIL_LINUX_TTYUTILS_H */ |