1 /*- 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.proprietary.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)ex_tty.c 8.1 (Berkeley) 06/09/93"; 10 #endif /* not lint */ 11 12 #include "ex.h" 13 #include "ex_tty.h" 14 15 /* 16 * Terminal type initialization routines, 17 * and calculation of flags at entry or after 18 * a shell escape which may change them. 19 */ 20 /* short ospeed = -1; mjm: def also in tputs.c of termcap.a */ 21 22 gettmode() 23 { 24 25 #ifndef USG3TTY 26 if (ioctl(1, TIOCGETP, &tty) < 0) 27 return; 28 if (ospeed != tty.sg_ospeed) 29 value(SLOWOPEN) = tty.sg_ospeed < B1200; 30 ospeed = tty.sg_ospeed; 31 normf = tty.sg_flags; 32 UPPERCASE = (tty.sg_flags & LCASE) != 0; 33 GT = (tty.sg_flags & XTABS) != XTABS && !XT; 34 NONL = (tty.sg_flags & CRMOD) == 0; 35 #else 36 if (ioctl(1, TCGETA, (char *) &tty) < 0) 37 return; 38 if (ospeed != (tty.c_cflag & CBAUD)) /* mjm */ 39 value(SLOWOPEN) = (tty.c_cflag & CBAUD) < B1200; 40 ospeed = tty.c_cflag & CBAUD; 41 normf = tty; 42 UPPERCASE = (tty.c_iflag & IUCLC) != 0; 43 GT = (tty.c_oflag & TABDLY) != TAB3 && !XT; 44 NONL = (tty.c_oflag & ONLCR) == 0; 45 #endif 46 } 47 48 char *xPC; 49 char **sstrs[] = { 50 &AL, &BC, &BT, &CD, &CE, &CL, &CM, &xCR, &CS, &DC, &DL, &DM, &DO, 51 &ED, &EI, &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9, 52 &HO, &IC, &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, &LL, &ND, &xNL, 53 &xPC, &RC, &SC, &SE, &SF, &SO, &SR, &TA, &TE, &TI, &UP, &VB, &VS, &VE, 54 &AL_PARM, &DL_PARM, &UP_PARM, &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM 55 }; 56 bool *sflags[] = { 57 &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI, &NC, &NS, &OS, &UL, 58 &XB, &XN, &XT, &XX 59 }; 60 char **fkeys[10] = { 61 &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9 62 }; 63 setterm(type) 64 char *type; 65 { 66 char *tgoto(); 67 register int unknown; 68 char ltcbuf[TCBUFSIZE]; 69 70 if (type[0] == 0) 71 type = "xx"; 72 unknown = 0; 73 putpad(TE); 74 if (tgetent(ltcbuf, type) != 1) { 75 unknown++; 76 CP(ltcbuf, "xx|dumb:"); 77 } 78 setsize(); 79 aoftspace = tspace; 80 zap(); 81 /* 82 * Initialize keypad arrow keys. 83 */ 84 arrows[0].cap = KU; arrows[0].mapto = "k"; arrows[0].descr = "up"; 85 arrows[1].cap = KD; arrows[1].mapto = "j"; arrows[1].descr = "down"; 86 arrows[2].cap = KL; arrows[2].mapto = "h"; arrows[2].descr = "left"; 87 arrows[3].cap = KR; arrows[3].mapto = "l"; arrows[3].descr = "right"; 88 arrows[4].cap = KH; arrows[4].mapto = "H"; arrows[4].descr = "home"; 89 90 /* 91 * Handle funny termcap capabilities 92 */ 93 if (CS && SC && RC) { 94 if (AL==NULL) AL=""; 95 if (DL==NULL) DL=""; 96 } 97 if (AL_PARM && AL==NULL) AL=""; 98 if (DL_PARM && DL==NULL) DL=""; 99 if (IC && IM==NULL) IM=""; 100 if (IC && EI==NULL) EI=""; 101 if (!GT) BT=NULL; /* If we can't tab, we can't backtab either */ 102 103 #ifdef TIOCLGET 104 /* 105 * Now map users susp char to ^Z, being careful that the susp 106 * overrides any arrow key, but only for hackers (=new tty driver). 107 */ 108 { 109 static char sc[2]; 110 int i; 111 112 ioctl(0, TIOCGETD, (char *) &ldisc); 113 if (ldisc == NTTYDISC) { 114 sc[0] = olttyc.t_suspc; 115 sc[1] = 0; 116 if (olttyc.t_suspc == CTRL('z')) { 117 for (i=0; i<=4; i++) 118 if (arrows[i].cap && 119 arrows[i].cap[0] == CTRL('z')) 120 addmac(sc, (char *) NULL, 121 (char *) NULL, arrows); 122 } else 123 addmac(sc, "\32", "susp", arrows); 124 } 125 } 126 #endif 127 128 if (tgoto(CM, 2, 2)[0] == 'O') /* OOPS */ 129 CA = 0, CM = 0; 130 else 131 CA = 1, costCM = cost(tgoto(CM, 8, 10)); 132 costSR = cost(SR); 133 costAL = cost(AL); 134 costDP = cost(tgoto(DOWN_PARM, 10, 10)); 135 costLP = cost(tgoto(LEFT_PARM, 10, 10)); 136 costRP = cost(tgoto(RIGHT_PARM, 10, 10)); 137 PC = xPC ? xPC[0] : 0; 138 aoftspace = tspace; 139 CP(ttytype, longname(ltcbuf, type)); 140 /* proper strings to change tty type */ 141 termreset(); 142 gettmode(); 143 value(REDRAW) = AL && DL; 144 value(OPTIMIZE) = !CA && !GT; 145 if (ospeed == B1200 && !value(REDRAW)) 146 value(SLOWOPEN) = 1; /* see also gettmode above */ 147 if (unknown) 148 serror("%s: Unknown terminal type", type); 149 } 150 151 setsize() 152 { 153 register int l, i; 154 #ifdef TIOCGWINSZ 155 struct winsize win; 156 157 if (ioctl(0, TIOCGWINSZ, (char *) &win) < 0) { 158 #endif 159 i = LINES = tgetnum("li"); 160 COLUMNS = tgetnum("co"); 161 #ifdef TIOCGWINSZ 162 } else { 163 if ((LINES = winsz.ws_row = win.ws_row) == 0) 164 LINES = tgetnum("li"); 165 i = LINES; 166 if ((COLUMNS = winsz.ws_col = win.ws_col) == 0) 167 COLUMNS = tgetnum("co"); 168 } 169 #endif 170 if (LINES <= 5) 171 LINES = 24; 172 if (LINES > TUBELINES) 173 LINES = TUBELINES; 174 l = LINES; 175 if (ospeed < B1200) 176 l = 9; /* including the message line at the bottom */ 177 else if (ospeed < B2400) 178 l = 17; 179 if (l > LINES) 180 l = LINES; 181 if (COLUMNS <= 4) 182 COLUMNS = 1000; 183 options[WINDOW].ovalue = options[WINDOW].odefault = l - 1; 184 if (defwind) options[WINDOW].ovalue = defwind; 185 options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2); 186 if (i <= 0) 187 LINES = 2; 188 } 189 190 zap() 191 { 192 register char *namp; 193 register bool **fp; 194 register char ***sp; 195 196 namp = "ambsdadbeohchzinmincnsosulxbxnxtxx"; 197 fp = sflags; 198 do { 199 *(*fp++) = tgetflag(namp); 200 namp += 2; 201 } while (*namp); 202 namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI"; 203 sp = sstrs; 204 do { 205 *(*sp++) = tgetstr(namp, &aoftspace); 206 namp += 2; 207 } while (*namp); 208 } 209 210 char * 211 longname(bp, def) 212 register char *bp; 213 char *def; 214 { 215 register char *cp; 216 217 while (*bp && *bp != ':' && *bp != '|') 218 bp++; 219 if (*bp == '|') { 220 bp++; 221 cp = bp; 222 while (*cp && *cp != ':' && *cp != '|') 223 cp++; 224 *cp = 0; 225 return (bp); 226 } 227 return (def); 228 } 229 230 char * 231 fkey(i) 232 int i; 233 { 234 if (0 <= i && i <= 9) 235 return(*fkeys[i]); 236 else 237 return(NOSTR); 238 } 239 240 /* 241 * cost figures out how much (in characters) it costs to send the string 242 * str to the terminal. It takes into account padding information, as 243 * much as it can, for a typical case. (Right now the typical case assumes 244 * the number of lines affected is the size of the screen, since this is 245 * mainly used to decide if AL or SR is better, and this always happens 246 * at the top of the screen. We assume cursor motion (CM) has little 247 * padding, if any, required, so that case, which is really more important 248 * than AL vs SR, won't be really affected.) 249 */ 250 static int costnum; 251 cost(str) 252 char *str; 253 { 254 int countnum(); 255 256 if (str == NULL || *str=='O') /* OOPS */ 257 return 10000; /* infinity */ 258 costnum = 0; 259 tputs(str, LINES, countnum); 260 return costnum; 261 } 262 263 /* ARGSUSED */ 264 countnum(ch) 265 char ch; 266 { 267 costnum++; 268 } 269