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