Add spl's around queue manipulation
[unix-history] / usr / src / usr.bin / ex / ex_tty.c
CommitLineData
299f2784 1/* Copyright (c) 1981 Regents of the University of California */
7d3f9b2b 2static char *sccsid = "@(#)ex_tty.c 7.6 %G%";
91d78146
MH
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 */
299f2784 11/* short ospeed = -1; mjm: def also in tputs.c of termcap.a */
91d78146
MH
12
13gettmode()
14{
15
d266c416 16#ifndef USG3TTY
91d78146
MH
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;
d266c416
MH
26#else
27 if (ioctl(1, TCGETA, &tty) < 0)
28 return;
299f2784 29 if (ospeed != (tty.c_cflag & CBAUD)) /* mjm */
d266c416
MH
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;
a87bd40c 35 NONL = (tty.c_oflag & ONLCR) == 0;
d266c416 36#endif
91d78146
MH
37}
38
39char *xPC;
40char **sstrs[] = {
17687128
MH
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,
3c7b865a 43 &HO, &IC, &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, &LL, &ND, &xNL,
17687128
MH
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
91d78146
MH
46};
47bool *sflags[] = {
d266c416 48 &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI, &NC, &NS, &OS, &UL,
3c7b865a 49 &XB, &XN, &XT, &XX
91d78146
MH
50};
51char **fkeys[10] = {
52 &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9
53};
54setterm(type)
55 char *type;
56{
d266c416 57 char *tgoto();
91d78146
MH
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++;
887e3e0d 68 CP(ltcbuf, "xx|dumb:");
91d78146
MH
69 }
70 i = LINES = tgetnum("li");
71 if (LINES <= 5)
72 LINES = 24;
ff59773d
MH
73 if (LINES > TUBELINES)
74 LINES = TUBELINES;
91d78146
MH
75 l = LINES;
76 if (ospeed < B1200)
887e3e0d 77 l = 9; /* including the message line at the bottom */
91d78146 78 else if (ospeed < B2400)
887e3e0d 79 l = 17;
ff59773d
MH
80 if (l > LINES)
81 l = LINES;
91d78146
MH
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
3c7b865a
MH
93 /*
94 * Handle funny termcap capabilities
95 */
17687128
MH
96 if (CS && SC && RC) AL=DL="";
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];
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++)
7d3f9b2b
RC
118 if (arrows[i].cap &&
119 arrows[i].cap[0] == CTRL(z))
d266c416
MH
120 addmac(sc, NULL, NULL, arrows);
121 } else
122 addmac(sc, "\32", "susp", arrows);
123 }
124 }
125#endif
126
91d78146
MH
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");
d266c416 131 if (COLUMNS <= 4)
91d78146 132 COLUMNS = 1000;
d266c416 133 if (tgoto(CM, 2, 2)[0] == 'O') /* OOPS */
91d78146
MH
134 CA = 0, CM = 0;
135 else
04379bab
MH
136 CA = 1, costCM = cost(tgoto(CM, 8, 10));
137 costSR = cost(SR);
138 costAL = cost(AL);
17687128
MH
139 costDP = cost(tgoto(DOWN_PARM, 10, 10));
140 costLP = cost(tgoto(LEFT_PARM, 10, 10));
141 costRP = cost(tgoto(RIGHT_PARM, 10, 10));
91d78146
MH
142 PC = xPC ? xPC[0] : 0;
143 aoftspace = tspace;
887e3e0d 144 CP(ttytype, longname(ltcbuf, type));
91d78146
MH
145 if (i <= 0)
146 LINES = 2;
147 /* proper strings to change tty type */
91d78146
MH
148 termreset();
149 gettmode();
150 value(REDRAW) = AL && DL;
151 value(OPTIMIZE) = !CA && !GT;
04379bab
MH
152 if (ospeed == B1200 && !value(REDRAW))
153 value(SLOWOPEN) = 1; /* see also gettmode above */
91d78146
MH
154 if (unknown)
155 serror("%s: Unknown terminal type", type);
156}
157
158zap()
159{
160 register char *namp;
161 register bool **fp;
162 register char ***sp;
163
3c7b865a 164 namp = "ambsdadbeohchzinmincnsosulxbxnxtxx";
91d78146
MH
165 fp = sflags;
166 do {
167 *(*fp++) = tgetflag(namp);
168 namp += 2;
169 } while (*namp);
17687128 170 namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI";
91d78146
MH
171 sp = sstrs;
172 do {
173 *(*sp++) = tgetstr(namp, &aoftspace);
174 namp += 2;
175 } while (*namp);
176}
177
178char *
179longname(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
198char *
199fkey(i)
200 int i;
201{
202 if (0 <= i && i <= 9)
203 return(*fkeys[i]);
204 else
205 return(NOSTR);
206}
04379bab
MH
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 */
218static int costnum;
219cost(str)
220char *str;
221{
222 int countnum();
223
17687128 224 if (str == NULL || *str=='O') /* OOPS */
04379bab
MH
225 return 10000; /* infinity */
226 costnum = 0;
227 tputs(str, LINES, countnum);
228 return costnum;
229}
230
231/* ARGSUSED */
232countnum(ch)
233char ch;
234{
235 costnum++;
236}