blob: e842f9f0d0e2998eb6ee75dca273269ea08c9a61 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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
bigbiff7b4c7a62015-01-01 19:44:14 -050016#ifdef HAVE_SYS_TTYDEFAULTS_H
17#include <sys/ttydefaults.h>
18#endif
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050019
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/* Storage for things detected while the login name was read. */
37struct chardata {
38 int erase; /* erase character */
39 int kill; /* kill character */
40 int eol; /* end-of-line character */
41 int parity; /* what parity did we see */
42 int capslock; /* upper case without lower case */
43};
44
45#define INIT_CHARDATA(ptr) do { \
46 (ptr)->erase = DEF_ERASE; \
47 (ptr)->kill = DEF_KILL; \
48 (ptr)->eol = CTRL('r'); \
49 (ptr)->parity = 0; \
50 (ptr)->capslock = 0; \
51 } while (0)
52
53extern int get_terminal_width(void);
bigbiff7b4c7a62015-01-01 19:44:14 -050054extern int get_terminal_name(int fd, const char **path, const char **name,
55 const char **number);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050056
57#define UL_TTY_KEEPCFLAGS (1 << 1)
58#define UL_TTY_UTF8 (1 << 2)
59
60static inline void reset_virtual_console(struct termios *tp, int flags)
61{
62 /* Use defaults of <sys/ttydefaults.h> for base settings */
63 tp->c_iflag |= TTYDEF_IFLAG;
64 tp->c_oflag |= TTYDEF_OFLAG;
65 tp->c_lflag |= TTYDEF_LFLAG;
66
67 if ((flags & UL_TTY_KEEPCFLAGS) == 0) {
68#ifdef CBAUD
69 tp->c_lflag &= ~CBAUD;
70#endif
71 tp->c_cflag |= (B38400 | TTYDEF_CFLAG);
72 }
73
74 /* Sane setting, allow eight bit characters, no carriage return delay
75 * the same result as `stty sane cr0 pass8'
76 */
bigbiff7b4c7a62015-01-01 19:44:14 -050077#ifndef IUCLC
78# define IUCLC 0
79#endif
80#ifndef NL0
81# define NL0 0
82#endif
83#ifndef CR0
84# define CR0 0
85#endif
86#ifndef BS0
87# define BS0 0
88#endif
89#ifndef VT0
90# define VT0 0
91#endif
92#ifndef FF0
93# define FF0 0
94#endif
95#ifndef OLCUC
96# define OLCUC 0
97#endif
98#ifndef OFILL
99# define OFILL 0
100#endif
101#ifndef NLDLY
102# define NLDLY 0
103#endif
104#ifndef CRDLY
105# define CRDLY 0
106#endif
107#ifndef BSDLY
108# define BSDLY 0
109#endif
110#ifndef VTDLY
111# define VTDLY 0
112#endif
113#ifndef FFDLY
114# define FFDLY 0
115#endif
116
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500117 tp->c_iflag |= (BRKINT | ICRNL | IMAXBEL);
118 tp->c_iflag &= ~(IGNBRK | INLCR | IGNCR | IXOFF | IUCLC | IXANY | ISTRIP);
119 tp->c_oflag |= (OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0);
120 tp->c_oflag &= ~(OLCUC | OCRNL | ONOCR | ONLRET | OFILL | \
121 NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
bigbiff7b4c7a62015-01-01 19:44:14 -0500122 tp->c_lflag |= (ISIG | ICANON | IEXTEN | ECHO|ECHOE|ECHOK|ECHOKE|ECHOCTL);
123 tp->c_lflag &= ~(ECHONL|ECHOPRT | NOFLSH | TOSTOP);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500124
125 if ((flags & UL_TTY_KEEPCFLAGS) == 0) {
126 tp->c_cflag |= (CREAD | CS8 | HUPCL);
127 tp->c_cflag &= ~(PARODD | PARENB);
128 }
129#ifdef OFDEL
130 tp->c_oflag &= ~OFDEL;
131#endif
132#ifdef XCASE
133 tp->c_lflag &= ~XCASE;
134#endif
135#ifdef IUTF8
136 if (flags & UL_TTY_UTF8)
137 tp->c_iflag |= IUTF8; /* Set UTF-8 input flag */
138 else
139 tp->c_iflag &= ~IUTF8;
140#endif
141 /* VTIME and VMIN can overlap with VEOF and VEOL since they are
142 * only used for non-canonical mode. We just set the at the
143 * beginning, so nothing bad should happen.
144 */
145 tp->c_cc[VTIME] = 0;
146 tp->c_cc[VMIN] = 1;
147 tp->c_cc[VINTR] = CINTR;
148 tp->c_cc[VQUIT] = CQUIT;
149 tp->c_cc[VERASE] = CERASE; /* ASCII DEL (0177) */
150 tp->c_cc[VKILL] = CKILL;
151 tp->c_cc[VEOF] = CEOF;
152#ifdef VSWTC
153 tp->c_cc[VSWTC] = _POSIX_VDISABLE;
154#elif defined(VSWTCH)
155 tp->c_cc[VSWTCH] = _POSIX_VDISABLE;
156#endif
157 tp->c_cc[VSTART] = CSTART;
158 tp->c_cc[VSTOP] = CSTOP;
159 tp->c_cc[VSUSP] = CSUSP;
160 tp->c_cc[VEOL] = _POSIX_VDISABLE;
161 tp->c_cc[VREPRINT] = CREPRINT;
162 tp->c_cc[VDISCARD] = CDISCARD;
163 tp->c_cc[VWERASE] = CWERASE;
164 tp->c_cc[VLNEXT] = CLNEXT;
165 tp->c_cc[VEOL2] = _POSIX_VDISABLE;
166}
167
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500168#endif /* UTIL_LINUX_TTYUTILS_H */