|
|
1.1 root 1: /* Copyright (c) 1981 Regents of the University of California */
2: static char *sccsid = "@(#)ex_tty.c 7.5 10/16/81";
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 (ioctl(1, TIOCGETP, &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: #ifdef FIOLOOKLD
110: extern int ntty_ld;
111: #endif
112: static char sc[2];
113: int i, fnd;
114:
115: #ifdef FIOLOOKLD
116: ldisc = ioctl(0, FIOLOOKLD, 0);
117: if (ldisc == ntty_ld)
118: #else
119: ioctl(0, TIOCGETD, &ldisc);
120: if (ldisc == NTTYDISC)
121: #endif
122: {
123: sc[0] = olttyc.t_suspc;
124: sc[1] = 0;
125: if (olttyc.t_suspc == CTRL(z)) {
126: for (i=0; i<=4; i++)
127: if (arrows[i].cap[0] == CTRL(z))
128: addmac(sc, NULL, NULL, arrows);
129: } else
130: addmac(sc, "\32", "susp", arrows);
131: }
132: }
133: #endif
134:
135: options[WINDOW].ovalue = options[WINDOW].odefault = l - 1;
136: if (defwind) options[WINDOW].ovalue = defwind;
137: options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2);
138: COLUMNS = tgetnum("co");
139: if (COLUMNS <= 4)
140: COLUMNS = 1000;
141: if (tgoto(CM, 2, 2)[0] == 'O') /* OOPS */
142: CA = 0, CM = 0;
143: else
144: CA = 1, costCM = cost(tgoto(CM, 8, 10));
145: costSR = cost(SR);
146: costAL = cost(AL);
147: costDP = cost(tgoto(DOWN_PARM, 10, 10));
148: costLP = cost(tgoto(LEFT_PARM, 10, 10));
149: costRP = cost(tgoto(RIGHT_PARM, 10, 10));
150: PC = xPC ? xPC[0] : 0;
151: aoftspace = tspace;
152: CP(ttytype, longname(ltcbuf, type));
153: if (i <= 0)
154: LINES = 2;
155: /* proper strings to change tty type */
156: termreset();
157: gettmode();
158: value(REDRAW) = AL && DL;
159: value(OPTIMIZE) = !CA && !GT;
160: if (ospeed == B1200 && !value(REDRAW))
161: value(SLOWOPEN) = 1; /* see also gettmode above */
162: if (unknown)
163: serror("%s: Unknown terminal type", type);
164: }
165:
166: zap()
167: {
168: register char *namp;
169: register bool **fp;
170: register char ***sp;
171:
172: namp = "ambsdadbeohchzinmincnsosulxbxnxtxx";
173: fp = sflags;
174: do {
175: *(*fp++) = tgetflag(namp);
176: namp += 2;
177: } while (*namp);
178: namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI";
179: sp = sstrs;
180: do {
181: *(*sp++) = tgetstr(namp, &aoftspace);
182: namp += 2;
183: } while (*namp);
184: }
185:
186: char *
187: longname(bp, def)
188: register char *bp;
189: char *def;
190: {
191: register char *cp;
192:
193: while (*bp && *bp != ':' && *bp != '|')
194: bp++;
195: if (*bp == '|') {
196: bp++;
197: cp = bp;
198: while (*cp && *cp != ':' && *cp != '|')
199: cp++;
200: *cp = 0;
201: return (bp);
202: }
203: return (def);
204: }
205:
206: char *
207: fkey(i)
208: int i;
209: {
210: if (0 <= i && i <= 9)
211: return(*fkeys[i]);
212: else
213: return(NOSTR);
214: }
215:
216: /*
217: * cost figures out how much (in characters) it costs to send the string
218: * str to the terminal. It takes into account padding information, as
219: * much as it can, for a typical case. (Right now the typical case assumes
220: * the number of lines affected is the size of the screen, since this is
221: * mainly used to decide if AL or SR is better, and this always happens
222: * at the top of the screen. We assume cursor motion (CM) has little
223: * padding, if any, required, so that case, which is really more important
224: * than AL vs SR, won't be really affected.)
225: */
226: static int costnum;
227: cost(str)
228: char *str;
229: {
230: int countnum();
231:
232: if (str == NULL || *str=='O') /* OOPS */
233: return 10000; /* infinity */
234: costnum = 0;
235: tputs(str, LINES, countnum);
236: return costnum;
237: }
238:
239: /* ARGSUSED */
240: countnum(ch)
241: char ch;
242: {
243: costnum++;
244: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.