Commit | Line | Data |
---|---|---|
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 | |
8 | static char sccsid[] = "@(#)ex_tty.c 7.9 (Berkeley) %G%"; | |
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 | |
21 | gettmode() | |
22 | { | |
23 | ||
d266c416 | 24 | #ifndef USG3TTY |
91d78146 MH |
25 | if (gtty(1, &tty) < 0) |
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 MH |
34 | #else |
35 | if (ioctl(1, TCGETA, &tty) < 0) | |
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 | ||
47 | char *xPC; | |
48 | char **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 | }; |
55 | bool *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 | }; |
59 | char **fkeys[10] = { | |
60 | &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9 | |
61 | }; | |
62 | setterm(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]; | |
109 | int i, fnd; | |
110 | ||
111 | ioctl(0, TIOCGETD, &ldisc); | |
112 | if (ldisc == NTTYDISC) { | |
113 | sc[0] = olttyc.t_suspc; | |
114 | sc[1] = 0; | |
115 | if (olttyc.t_suspc == CTRL(z)) { | |
116 | for (i=0; i<=4; i++) | |
7d3f9b2b RC |
117 | if (arrows[i].cap && |
118 | arrows[i].cap[0] == CTRL(z)) | |
d266c416 MH |
119 | addmac(sc, NULL, NULL, arrows); |
120 | } else | |
121 | addmac(sc, "\32", "susp", arrows); | |
122 | } | |
123 | } | |
124 | #endif | |
125 | ||
d266c416 | 126 | if (tgoto(CM, 2, 2)[0] == 'O') /* OOPS */ |
91d78146 MH |
127 | CA = 0, CM = 0; |
128 | else | |
04379bab MH |
129 | CA = 1, costCM = cost(tgoto(CM, 8, 10)); |
130 | costSR = cost(SR); | |
131 | costAL = cost(AL); | |
17687128 MH |
132 | costDP = cost(tgoto(DOWN_PARM, 10, 10)); |
133 | costLP = cost(tgoto(LEFT_PARM, 10, 10)); | |
134 | costRP = cost(tgoto(RIGHT_PARM, 10, 10)); | |
91d78146 MH |
135 | PC = xPC ? xPC[0] : 0; |
136 | aoftspace = tspace; | |
887e3e0d | 137 | CP(ttytype, longname(ltcbuf, type)); |
91d78146 | 138 | /* proper strings to change tty type */ |
91d78146 MH |
139 | termreset(); |
140 | gettmode(); | |
141 | value(REDRAW) = AL && DL; | |
142 | value(OPTIMIZE) = !CA && !GT; | |
04379bab MH |
143 | if (ospeed == B1200 && !value(REDRAW)) |
144 | value(SLOWOPEN) = 1; /* see also gettmode above */ | |
91d78146 MH |
145 | if (unknown) |
146 | serror("%s: Unknown terminal type", type); | |
147 | } | |
148 | ||
2de97448 JB |
149 | setsize() |
150 | { | |
151 | register int l, i; | |
152 | struct winsize win; | |
153 | ||
154 | if (ioctl(0, TIOCGWINSZ, &win) < 0) { | |
155 | i = LINES = tgetnum("li"); | |
156 | COLUMNS = tgetnum("co"); | |
157 | } else { | |
158 | if ((LINES = winsz.ws_row = win.ws_row) == 0) | |
159 | LINES = tgetnum("li"); | |
160 | i = LINES; | |
161 | if ((COLUMNS = winsz.ws_col = win.ws_col) == 0) | |
162 | COLUMNS = tgetnum("co"); | |
163 | } | |
164 | if (LINES <= 5) | |
165 | LINES = 24; | |
166 | if (LINES > TUBELINES) | |
167 | LINES = TUBELINES; | |
168 | l = LINES; | |
169 | if (ospeed < B1200) | |
170 | l = 9; /* including the message line at the bottom */ | |
171 | else if (ospeed < B2400) | |
172 | l = 17; | |
173 | if (l > LINES) | |
174 | l = LINES; | |
175 | if (COLUMNS <= 4) | |
176 | COLUMNS = 1000; | |
177 | options[WINDOW].ovalue = options[WINDOW].odefault = l - 1; | |
178 | if (defwind) options[WINDOW].ovalue = defwind; | |
179 | options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2); | |
180 | if (i <= 0) | |
181 | LINES = 2; | |
182 | } | |
183 | ||
91d78146 MH |
184 | zap() |
185 | { | |
186 | register char *namp; | |
187 | register bool **fp; | |
188 | register char ***sp; | |
189 | ||
3c7b865a | 190 | namp = "ambsdadbeohchzinmincnsosulxbxnxtxx"; |
91d78146 MH |
191 | fp = sflags; |
192 | do { | |
193 | *(*fp++) = tgetflag(namp); | |
194 | namp += 2; | |
195 | } while (*namp); | |
17687128 | 196 | namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI"; |
91d78146 MH |
197 | sp = sstrs; |
198 | do { | |
199 | *(*sp++) = tgetstr(namp, &aoftspace); | |
200 | namp += 2; | |
201 | } while (*namp); | |
202 | } | |
203 | ||
204 | char * | |
205 | longname(bp, def) | |
206 | register char *bp; | |
207 | char *def; | |
208 | { | |
209 | register char *cp; | |
210 | ||
211 | while (*bp && *bp != ':' && *bp != '|') | |
212 | bp++; | |
213 | if (*bp == '|') { | |
214 | bp++; | |
215 | cp = bp; | |
216 | while (*cp && *cp != ':' && *cp != '|') | |
217 | cp++; | |
218 | *cp = 0; | |
219 | return (bp); | |
220 | } | |
221 | return (def); | |
222 | } | |
223 | ||
224 | char * | |
225 | fkey(i) | |
226 | int i; | |
227 | { | |
228 | if (0 <= i && i <= 9) | |
229 | return(*fkeys[i]); | |
230 | else | |
231 | return(NOSTR); | |
232 | } | |
04379bab MH |
233 | |
234 | /* | |
235 | * cost figures out how much (in characters) it costs to send the string | |
236 | * str to the terminal. It takes into account padding information, as | |
237 | * much as it can, for a typical case. (Right now the typical case assumes | |
238 | * the number of lines affected is the size of the screen, since this is | |
239 | * mainly used to decide if AL or SR is better, and this always happens | |
240 | * at the top of the screen. We assume cursor motion (CM) has little | |
241 | * padding, if any, required, so that case, which is really more important | |
242 | * than AL vs SR, won't be really affected.) | |
243 | */ | |
244 | static int costnum; | |
245 | cost(str) | |
246 | char *str; | |
247 | { | |
248 | int countnum(); | |
249 | ||
17687128 | 250 | if (str == NULL || *str=='O') /* OOPS */ |
04379bab MH |
251 | return 10000; /* infinity */ |
252 | costnum = 0; | |
253 | tputs(str, LINES, countnum); | |
254 | return costnum; | |
255 | } | |
256 | ||
257 | /* ARGSUSED */ | |
258 | countnum(ch) | |
259 | char ch; | |
260 | { | |
261 | costnum++; | |
262 | } |