1 /* $NetBSD: set.c,v 1.14 2010/02/10 10:34:59 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 __RCSID("$NetBSD: set.c,v 1.14 2010/02/10 10:34:59 roy Exp $"); 34 35 #include <err.h> 36 #include <stdio.h> 37 #include <term.h> 38 #include <termios.h> 39 #include <unistd.h> 40 #include "extern.h" 41 42 #define CHK(val, dft) (val <= 0 ? dft : val) 43 44 int set_tabs __P((void)); 45 46 /* 47 * Reset the terminal mode bits to a sensible state. Very useful after 48 * a child program dies in raw mode. 49 */ 50 void 51 reset_mode() 52 { 53 tcgetattr(STDERR_FILENO, &mode); 54 55 #if defined(VDISCARD) && defined(CDISCARD) 56 mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD); 57 #endif 58 mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF); 59 mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE); 60 #if defined(VFLUSH) && defined(CFLUSH) 61 mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH); 62 #endif 63 mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR); 64 mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL); 65 #if defined(VLNEXT) && defined(CLNEXT) 66 mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT); 67 #endif 68 mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT); 69 #if defined(VREPRINT) && defined(CRPRNT) 70 mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT); 71 #endif 72 mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART); 73 mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP); 74 mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP); 75 #if defined(VWERASE) && defined(CWERASE) 76 mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE); 77 #endif 78 79 mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR 80 #ifdef IUCLC 81 | IUCLC 82 #endif 83 #ifdef IXANY 84 | IXANY 85 #endif 86 | IXOFF); 87 88 mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON 89 #ifdef IMAXBEL 90 | IMAXBEL 91 #endif 92 ); 93 94 mode.c_oflag &= ~(0 95 #ifdef OLCUC 96 | OLCUC 97 #endif 98 #ifdef OCRNL 99 | OCRNL 100 #endif 101 #ifdef ONOCR 102 | ONOCR 103 #endif 104 #ifdef ONLRET 105 | ONLRET 106 #endif 107 #ifdef OFILL 108 | OFILL 109 #endif 110 #ifdef OFDEL 111 | OFDEL 112 #endif 113 #ifdef NLDLY 114 | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY 115 #endif 116 ); 117 118 mode.c_oflag |= (OPOST 119 #ifdef ONLCR 120 | ONLCR 121 #endif 122 ); 123 124 mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD); 125 mode.c_cflag |= (CS8 | CREAD); 126 mode.c_lflag &= ~(ECHONL | NOFLSH | TOSTOP 127 #ifdef ECHOPTR 128 | ECHOPRT 129 #endif 130 #ifdef XCASE 131 | XCASE 132 #endif 133 ); 134 135 mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK 136 #ifdef ECHOCTL 137 | ECHOCTL 138 #endif 139 #ifdef ECHOKE 140 | ECHOKE 141 #endif 142 ); 143 144 tcsetattr(STDERR_FILENO, TCSADRAIN, &mode); 145 } 146 147 /* 148 * Determine the erase, interrupt, and kill characters from the termcap 149 * entry and command line and update their values in 'mode'. 150 */ 151 void 152 set_control_chars(int erasechar, int intrchar, int killchar) 153 { 154 155 if (mode.c_cc[VERASE] == 0 || erasechar != 0) { 156 if (erasechar == 0) { 157 if (over_strike && 158 key_backspace != NULL && 159 key_backspace[1] == '\0') 160 mode.c_cc[VERASE] = key_backspace[1]; 161 else 162 mode.c_cc[VERASE] = CERASE; 163 } else 164 mode.c_cc[VERASE] = erasechar; 165 } 166 167 if (mode.c_cc[VINTR] == 0 || intrchar != 0) 168 mode.c_cc[VINTR] = intrchar ? intrchar : CINTR; 169 170 if (mode.c_cc[VKILL] == 0 || killchar != 0) 171 mode.c_cc[VKILL] = killchar ? killchar : CKILL; 172 } 173 174 /* 175 * Set up various conversions in 'mode', including parity, tabs, returns, 176 * echo, and case, according to the termcap entry. If the program we're 177 * running was named with a leading upper-case character, map external 178 * uppercase to internal lowercase. 179 */ 180 void 181 set_conversions(int usingupper) 182 { 183 184 #ifdef ONLCR 185 mode.c_oflag |= ONLCR; 186 #endif 187 mode.c_iflag |= ICRNL; 188 mode.c_lflag |= ECHO; 189 mode.c_oflag |= OXTABS; 190 if (newline != NULL && newline[0] == '\n' && !newline[1]) { /* Newline, not linefeed. */ 191 #ifdef ONLCR 192 mode.c_oflag &= ~ONLCR; 193 #endif 194 mode.c_iflag &= ~ICRNL; 195 } 196 if (tab) /* Print tabs. */ 197 mode.c_oflag &= ~OXTABS; 198 mode.c_lflag |= (ECHOE | ECHOK); 199 } 200 201 # 202 /* Output startup string. */ 203 void 204 set_init() 205 { 206 const char *bp; 207 int settle; 208 209 #ifdef TAB3 210 if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) { 211 oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET); 212 tcsetattr(STDERR_FILENO, TCSADRAIN, &oldmode); 213 } 214 #endif 215 settle = set_tabs(); 216 217 if (isreset) { 218 if (reset_1string) { 219 tputs(reset_1string, 0, outc); 220 settle = 1; 221 } 222 if (reset_2string) { 223 tputs(reset_2string, 0, outc); 224 settle = 1; 225 } 226 if ((bp = reset_file) || (bp = init_file)) { 227 tset_cat(bp); 228 settle = 1; 229 } 230 } 231 232 if (settle) { 233 (void)putc('\r', stderr); 234 (void)fflush(stderr); 235 (void)sleep(1); /* Settle the terminal. */ 236 } 237 } 238 239 /* 240 * Set the hardware tabs on the terminal, using the ct (clear all tabs), 241 * st (set one tab) and ch (horizontal cursor addressing) capabilities. 242 * This is done before if and is, so they can patch in case we blow this. 243 * Return nonzero if we set any tab stops, zero if not. 244 */ 245 int 246 set_tabs() 247 { 248 int c; 249 char *out; 250 251 if (set_tab) { 252 if (clear_all_tabs) { 253 (void)putc('\r', stderr); /* Force to left margin. */ 254 tputs(clear_all_tabs, 0, outc); 255 } 256 257 for (c = 8; c < ncolumns; c += 8) { 258 if (column_address) 259 out = vtparm(column_address, c); 260 else 261 out = NULL; 262 if (out) 263 tputs(out, 1, outc); 264 else 265 (void)fprintf(stderr, "%s", " "); 266 /* Set the tab. */ 267 tputs(set_tab, 0, outc); 268 } 269 putc('\r', stderr); 270 return (1); 271 } 272 return (0); 273 } 274