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