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