|
|
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: /* ! 72: * Following lines deleted by Gershon Shamay. Reason - while working under ! 73: * the window manager (came over the net), vi didn't work well in a window ! 74: * with fewer than 6 lines. Why should anyone have such a small window ? ! 75: * Because. In any case, the original code is not nice either, takes revenge ! 76: * on them by deciding to use 24 lines regardless.... So here we go: ! 77: if (LINES <= 5) ! 78: LINES = 24; ! 79: */ ! 80: if (LINES > TUBELINES) ! 81: LINES = TUBELINES; ! 82: l = LINES; ! 83: if (ospeed < B1200) ! 84: l = 9; /* including the message line at the bottom */ ! 85: else if (ospeed < B2400) ! 86: l = 17; ! 87: if (l > LINES) ! 88: l = LINES; ! 89: aoftspace = tspace; ! 90: zap(); ! 91: /* ! 92: * Initialize keypad arrow keys. ! 93: */ ! 94: arrows[0].cap = KU; arrows[0].mapto = "k"; arrows[0].descr = "up"; ! 95: arrows[1].cap = KD; arrows[1].mapto = "j"; arrows[1].descr = "down"; ! 96: arrows[2].cap = KL; arrows[2].mapto = "h"; arrows[2].descr = "left"; ! 97: arrows[3].cap = KR; arrows[3].mapto = "l"; arrows[3].descr = "right"; ! 98: arrows[4].cap = KH; arrows[4].mapto = "H"; arrows[4].descr = "home"; ! 99: ! 100: /* ! 101: * Handle funny termcap capabilities ! 102: */ ! 103: if (CS && SC && RC) AL=DL=""; ! 104: if (AL_PARM && AL==NULL) AL=""; ! 105: if (DL_PARM && DL==NULL) DL=""; ! 106: if (IC && IM==NULL) IM=""; ! 107: if (IC && EI==NULL) EI=""; ! 108: if (!GT) BT=NULL; /* If we can't tab, we can't backtab either */ ! 109: ! 110: #ifdef TIOCLGET ! 111: /* ! 112: * Now map users susp char to ^Z, being careful that the susp ! 113: * overrides any arrow key, but only for hackers (=new tty driver). ! 114: */ ! 115: { ! 116: static char sc[2]; ! 117: int i, fnd; ! 118: ! 119: ioctl(0, TIOCGETD, &ldisc); ! 120: if (ldisc == NTTYDISC) { ! 121: sc[0] = olttyc.t_suspc; ! 122: sc[1] = 0; ! 123: if (olttyc.t_suspc == CTRL(z)) { ! 124: for (i=0; i<=4; i++) ! 125: if (arrows[i].cap && ! 126: arrows[i].cap[0] == CTRL(z)) ! 127: addmac(sc, NULL, NULL, arrows); ! 128: } else ! 129: addmac(sc, "\32", "susp", arrows); ! 130: } ! 131: } ! 132: #endif ! 133: ! 134: options[WINDOW].ovalue = options[WINDOW].odefault = l - 1; ! 135: if (defwind) options[WINDOW].ovalue = defwind; ! 136: options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2); ! 137: COLUMNS = tgetnum("co"); ! 138: if (COLUMNS <= 4) ! 139: COLUMNS = 1000; ! 140: if (tgoto(CM, 2, 2)[0] == 'O') /* OOPS */ ! 141: CA = 0, CM = 0; ! 142: else ! 143: CA = 1, costCM = cost(tgoto(CM, 8, 10)); ! 144: costSR = cost(SR); ! 145: costAL = cost(AL); ! 146: costDP = cost(tgoto(DOWN_PARM, 10, 10)); ! 147: costLP = cost(tgoto(LEFT_PARM, 10, 10)); ! 148: costRP = cost(tgoto(RIGHT_PARM, 10, 10)); ! 149: PC = xPC ? xPC[0] : 0; ! 150: aoftspace = tspace; ! 151: CP(ttytype, longname(ltcbuf, type)); ! 152: if (i <= 0) ! 153: LINES = 2; ! 154: /* proper strings to change tty type */ ! 155: termreset(); ! 156: gettmode(); ! 157: value(REDRAW) = AL && DL; ! 158: value(OPTIMIZE) = !CA && !GT; ! 159: if (ospeed == B1200 && !value(REDRAW)) ! 160: value(SLOWOPEN) = 1; /* see also gettmode above */ ! 161: if (unknown) ! 162: serror("%s: Unknown terminal type", type); ! 163: } ! 164: ! 165: zap() ! 166: { ! 167: register char *namp; ! 168: register bool **fp; ! 169: register char ***sp; ! 170: ! 171: namp = "ambsdadbeohchzinmincnsosulxbxnxtxx"; ! 172: fp = sflags; ! 173: do { ! 174: *(*fp++) = tgetflag(namp); ! 175: namp += 2; ! 176: } while (*namp); ! 177: namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI"; ! 178: sp = sstrs; ! 179: do { ! 180: *(*sp++) = tgetstr(namp, &aoftspace); ! 181: namp += 2; ! 182: } while (*namp); ! 183: } ! 184: ! 185: char * ! 186: longname(bp, def) ! 187: register char *bp; ! 188: char *def; ! 189: { ! 190: register char *cp; ! 191: ! 192: while (*bp && *bp != ':' && *bp != '|') ! 193: bp++; ! 194: if (*bp == '|') { ! 195: bp++; ! 196: cp = bp; ! 197: while (*cp && *cp != ':' && *cp != '|') ! 198: cp++; ! 199: *cp = 0; ! 200: return (bp); ! 201: } ! 202: return (def); ! 203: } ! 204: ! 205: char * ! 206: fkey(i) ! 207: int i; ! 208: { ! 209: if (0 <= i && i <= 9) ! 210: return(*fkeys[i]); ! 211: else ! 212: return(NOSTR); ! 213: } ! 214: ! 215: /* ! 216: * cost figures out how much (in characters) it costs to send the string ! 217: * str to the terminal. It takes into account padding information, as ! 218: * much as it can, for a typical case. (Right now the typical case assumes ! 219: * the number of lines affected is the size of the screen, since this is ! 220: * mainly used to decide if AL or SR is better, and this always happens ! 221: * at the top of the screen. We assume cursor motion (CM) has little ! 222: * padding, if any, required, so that case, which is really more important ! 223: * than AL vs SR, won't be really affected.) ! 224: */ ! 225: static int costnum; ! 226: cost(str) ! 227: char *str; ! 228: { ! 229: int countnum(); ! 230: ! 231: if (str == NULL || *str=='O') /* OOPS */ ! 232: return 10000; /* infinity */ ! 233: costnum = 0; ! 234: tputs(str, LINES, countnum); ! 235: return costnum; ! 236: } ! 237: ! 238: /* ARGSUSED */ ! 239: countnum(ch) ! 240: char ch; ! 241: { ! 242: costnum++; ! 243: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.