Coverage Report

Created: 2026-08-13 07:17

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