1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/* $NetBSD: set.c,v 1.6 1994/12/07 05:08:10 jtc Exp $ */
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)set.c 8.2 (Berkeley) 2/28/94";
#endif
static char rcsid[] = "$NetBSD: set.c,v 1.6 1994/12/07 05:08:10 jtc Exp $";
#endif /* not lint */
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include "extern.h"
#define CHK(val, dft) (val <= 0 ? dft : val)
int set_tabs __P((void));
/*
* Reset the terminal mode bits to a sensible state. Very useful after
* a child program dies in raw mode.
*/
void
reset_mode()
{
tcgetattr(STDERR_FILENO, &mode);
#if defined(VDISCARD) && defined(CDISCARD)
mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
#endif
mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
#if defined(VFLUSH) && defined(CFLUSH)
mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
#endif
mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
#if defined(VLNEXT) && defined(CLNEXT)
mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
#endif
mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
#if defined(VREPRINT) && defined(CRPRNT)
mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
#endif
mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
#if defined(VWERASE) && defined(CWERASE)
mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
#endif
mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
#ifdef IUCLC
| IUCLC
#endif
#ifdef IXANY
| IXANY
#endif
| IXOFF);
mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
#ifdef IMAXBEL
| IMAXBEL
#endif
);
mode.c_oflag &= ~(0
#ifdef OLCUC
| OLCUC
#endif
#ifdef OCRNL
| OCRNL
#endif
#ifdef ONOCR
| ONOCR
#endif
#ifdef ONLRET
| ONLRET
#endif
#ifdef OFILL
| OFILL
#endif
#ifdef OFDEL
| OFDEL
#endif
#ifdef NLDLY
| NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY
#endif
);
mode.c_oflag |= (OPOST
#ifdef ONLCR
| ONLCR
#endif
);
mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
mode.c_cflag |= (CS8 | CREAD);
mode.c_lflag &= ~(ECHONL | NOFLSH | TOSTOP
#ifdef ECHOPTR
| ECHOPRT
#endif
#ifdef XCASE
| XCASE
#endif
);
mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
#ifdef ECHOCTL
| ECHOCTL
#endif
#ifdef ECHOKE
| ECHOKE
#endif
);
tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
}
/*
* Determine the erase, interrupt, and kill characters from the termcap
* entry and command line and update their values in 'mode'.
*/
void
set_control_chars()
{
char *bp, *p, bs_char, buf[1024];
bp = buf;
p = tgetstr("kb", &bp);
if (p == NULL || p[1] != '\0')
p = tgetstr("bc", &bp);
if (p != NULL && p[1] == '\0')
bs_char = p[0];
else if (tgetflag("bs"))
bs_char = CTRL('h');
else
bs_char = 0;
if (erasechar == 0 && !tgetflag("os") && mode.c_cc[VERASE] != CERASE) {
if (tgetflag("bs") || bs_char != 0)
erasechar = -1;
}
if (erasechar < 0)
erasechar = (bs_char != 0) ? bs_char : CTRL('h');
if (mode.c_cc[VERASE] == 0 || erasechar != 0)
mode.c_cc[VERASE] = erasechar ? erasechar : CERASE;
if (mode.c_cc[VINTR] == 0 || intrchar != 0)
mode.c_cc[VINTR] = intrchar ? intrchar : CINTR;
if (mode.c_cc[VKILL] == 0 || killchar != 0)
mode.c_cc[VKILL] = killchar ? killchar : CKILL;
}
/*
* Set up various conversions in 'mode', including parity, tabs, returns,
* echo, and case, according to the termcap entry. If the program we're
* running was named with a leading upper-case character, map external
* uppercase to internal lowercase.
*/
void
set_conversions(usingupper)
int usingupper;
{
if (tgetflag("UC") || usingupper) {
#ifdef IUCLC
mode.c_iflag |= IUCLC;
mode.c_oflag |= OLCUC;
#endif
} else if (tgetflag("LC")) {
#ifdef IUCLC
mode.c_iflag &= ~IUCLC;
mode.c_oflag &= ~OLCUC;
#endif
}
mode.c_iflag &= ~(PARMRK | INPCK);
mode.c_lflag |= ICANON;
if (tgetflag("EP")) {
mode.c_cflag |= PARENB;
mode.c_cflag &= ~PARODD;
}
if (tgetflag("OP")) {
mode.c_cflag |= PARENB;
mode.c_cflag |= PARODD;
}
#ifdef ONLCR
mode.c_oflag |= ONLCR;
#endif
mode.c_iflag |= ICRNL;
mode.c_lflag |= ECHO;
mode.c_oflag |= OXTABS;
if (tgetflag("NL")) { /* Newline, not linefeed. */
#ifdef ONLCR
mode.c_oflag &= ~ONLCR;
#endif
mode.c_iflag &= ~ICRNL;
}
if (tgetflag("HD")) /* Half duplex. */
mode.c_lflag &= ~ECHO;
if (tgetflag("pt")) /* Print tabs. */
mode.c_oflag &= ~OXTABS;
mode.c_lflag |= (ECHOE | ECHOK);
}
/* Output startup string. */
void
set_init()
{
char *bp, buf[1024];
int settle;
bp = buf;
if (tgetstr("pc", &bp) != 0) /* Get/set pad character. */
PC = buf[0];
#ifdef TAB3
if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
tcsetattr(STDERR_FILENO, TCSADRAIN, &oldmode);
}
#endif
settle = set_tabs();
if (isreset) {
bp = buf;
if (tgetstr("rs", &bp) != 0 || tgetstr("is", &bp) != 0) {
tputs(buf, 0, outc);
settle = 1;
}
bp = buf;
if (tgetstr("rf", &bp) != 0 || tgetstr("if", &bp) != 0) {
cat(buf);
settle = 1;
}
}
if (settle) {
(void)putc('\r', stderr);
(void)fflush(stderr);
(void)sleep(1); /* Settle the terminal. */
}
}
/*
* Set the hardware tabs on the terminal, using the ct (clear all tabs),
* st (set one tab) and ch (horizontal cursor addressing) capabilities.
* This is done before if and is, so they can patch in case we blow this.
* Return nonzero if we set any tab stops, zero if not.
*/
int
set_tabs()
{
int c;
char *capsp, *clear_tabs;
char *set_column, *set_pos, *set_tab, *tg_out;
char caps[1024];
capsp = caps;
set_tab = tgetstr("st", &capsp);
if (set_tab && (clear_tabs = tgetstr("ct", &capsp))) {
(void)putc('\r', stderr); /* Force to left margin. */
tputs(clear_tabs, 0, outc);
}
set_column = tgetstr("ch", &capsp);
set_pos = set_column ? NULL : tgetstr("cm", &capsp);
if (set_tab) {
for (c = 8; c < columns; c += 8) {
/*
* Get to the right column. "OOPS" is returned by
* tgoto() if it can't do the job. (*snarl*)
*/
tg_out = "OOPS";
if (set_column)
tg_out = tgoto(set_column, 0, c);
if (*tg_out == 'O' && set_pos)
tg_out = tgoto(set_pos, c, lines - 1);
if (*tg_out != 'O')
tputs(tg_out, 1, outc);
else
(void)fprintf(stderr, "%s", " ");
/* Set the tab. */
tputs(set_tab, 0, outc);
}
putc('\r', stderr);
return (1);
}
return (0);
}
|