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