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