blob: 021156d3e71ce3346527756e2cb46012cb2e76d5 [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
16
17/* Some shorthands for control characters. */
18#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
19#define CR CTL('M') /* carriage return */
20#define NL CTL('J') /* line feed */
21#define BS CTL('H') /* back space */
22#define DEL CTL('?') /* delete */
23
24/* Defaults for line-editing etc. characters; you may want to change these. */
25#define DEF_ERASE DEL /* default erase character */
26#define DEF_INTR CTL('C') /* default interrupt character */
27#define DEF_QUIT CTL('\\') /* default quit char */
28#define DEF_KILL CTL('U') /* default kill char */
29#define DEF_EOF CTL('D') /* default EOF char */
30#define DEF_EOL 0
31#define DEF_SWITCH 0 /* default switch char */
32
33/* Storage for things detected while the login name was read. */
34struct chardata {
35 int erase; /* erase character */
36 int kill; /* kill character */
37 int eol; /* end-of-line character */
38 int parity; /* what parity did we see */
39 int capslock; /* upper case without lower case */
40};
41
42#define INIT_CHARDATA(ptr) do { \
43 (ptr)->erase = DEF_ERASE; \
44 (ptr)->kill = DEF_KILL; \
45 (ptr)->eol = CTRL('r'); \
46 (ptr)->parity = 0; \
47 (ptr)->capslock = 0; \
48 } while (0)
49
50extern int get_terminal_width(void);
51extern int get_terminal_name(const char **path, const char **name, const char **number);
52
53#define UL_TTY_KEEPCFLAGS (1 << 1)
54#define UL_TTY_UTF8 (1 << 2)
55
56static inline void reset_virtual_console(struct termios *tp, int flags)
57{
58 /* Use defaults of <sys/ttydefaults.h> for base settings */
59 tp->c_iflag |= TTYDEF_IFLAG;
60 tp->c_oflag |= TTYDEF_OFLAG;
61 tp->c_lflag |= TTYDEF_LFLAG;
62
63 if ((flags & UL_TTY_KEEPCFLAGS) == 0) {
64#ifdef CBAUD
65 tp->c_lflag &= ~CBAUD;
66#endif
67 tp->c_cflag |= (B38400 | TTYDEF_CFLAG);
68 }
69
70 /* Sane setting, allow eight bit characters, no carriage return delay
71 * the same result as `stty sane cr0 pass8'
72 */
73 tp->c_iflag |= (BRKINT | ICRNL | IMAXBEL);
74 tp->c_iflag &= ~(IGNBRK | INLCR | IGNCR | IXOFF | IUCLC | IXANY | ISTRIP);
75 tp->c_oflag |= (OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0);
76 tp->c_oflag &= ~(OLCUC | OCRNL | ONOCR | ONLRET | OFILL | \
77 NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
78 tp->c_lflag |= (ISIG | ICANON | IEXTEN | ECHO|ECHOE|ECHOK|ECHOKE);
79 tp->c_lflag &= ~(ECHONL|ECHOCTL|ECHOPRT | NOFLSH | TOSTOP);
80
81 if ((flags & UL_TTY_KEEPCFLAGS) == 0) {
82 tp->c_cflag |= (CREAD | CS8 | HUPCL);
83 tp->c_cflag &= ~(PARODD | PARENB);
84 }
85#ifdef OFDEL
86 tp->c_oflag &= ~OFDEL;
87#endif
88#ifdef XCASE
89 tp->c_lflag &= ~XCASE;
90#endif
91#ifdef IUTF8
92 if (flags & UL_TTY_UTF8)
93 tp->c_iflag |= IUTF8; /* Set UTF-8 input flag */
94 else
95 tp->c_iflag &= ~IUTF8;
96#endif
97 /* VTIME and VMIN can overlap with VEOF and VEOL since they are
98 * only used for non-canonical mode. We just set the at the
99 * beginning, so nothing bad should happen.
100 */
101 tp->c_cc[VTIME] = 0;
102 tp->c_cc[VMIN] = 1;
103 tp->c_cc[VINTR] = CINTR;
104 tp->c_cc[VQUIT] = CQUIT;
105 tp->c_cc[VERASE] = CERASE; /* ASCII DEL (0177) */
106 tp->c_cc[VKILL] = CKILL;
107 tp->c_cc[VEOF] = CEOF;
108#ifdef VSWTC
109 tp->c_cc[VSWTC] = _POSIX_VDISABLE;
110#elif defined(VSWTCH)
111 tp->c_cc[VSWTCH] = _POSIX_VDISABLE;
112#endif
113 tp->c_cc[VSTART] = CSTART;
114 tp->c_cc[VSTOP] = CSTOP;
115 tp->c_cc[VSUSP] = CSUSP;
116 tp->c_cc[VEOL] = _POSIX_VDISABLE;
117 tp->c_cc[VREPRINT] = CREPRINT;
118 tp->c_cc[VDISCARD] = CDISCARD;
119 tp->c_cc[VWERASE] = CWERASE;
120 tp->c_cc[VLNEXT] = CLNEXT;
121 tp->c_cc[VEOL2] = _POSIX_VDISABLE;
122}
123
124
125
126#endif /* UTIL_LINUX_TTYUTILS_H */