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