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