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