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