Commit | Line | Data |
---|---|---|
299f2784 | 1 | /* Copyright (c) 1981 Regents of the University of California */ |
427286eb | 2 | static char *sccsid = "@(#)ex_tty.c 7.5 %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(); |
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++) | |
118 | if (arrows[i].cap[0] == CTRL(z)) | |
119 | addmac(sc, NULL, NULL, arrows); | |
120 | } else | |
121 | addmac(sc, "\32", "susp", arrows); | |
122 | } | |
123 | } | |
124 | #endif | |
125 | ||
91d78146 MH |
126 | options[WINDOW].ovalue = options[WINDOW].odefault = l - 1; |
127 | if (defwind) options[WINDOW].ovalue = defwind; | |
128 | options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2); | |
129 | COLUMNS = tgetnum("co"); | |
d266c416 | 130 | if (COLUMNS <= 4) |
91d78146 | 131 | COLUMNS = 1000; |
d266c416 | 132 | if (tgoto(CM, 2, 2)[0] == 'O') /* OOPS */ |
91d78146 MH |
133 | CA = 0, CM = 0; |
134 | else | |
04379bab MH |
135 | CA = 1, costCM = cost(tgoto(CM, 8, 10)); |
136 | costSR = cost(SR); | |
137 | costAL = cost(AL); | |
17687128 MH |
138 | costDP = cost(tgoto(DOWN_PARM, 10, 10)); |
139 | costLP = cost(tgoto(LEFT_PARM, 10, 10)); | |
140 | costRP = cost(tgoto(RIGHT_PARM, 10, 10)); | |
91d78146 MH |
141 | PC = xPC ? xPC[0] : 0; |
142 | aoftspace = tspace; | |
887e3e0d | 143 | CP(ttytype, longname(ltcbuf, type)); |
91d78146 MH |
144 | if (i <= 0) |
145 | LINES = 2; | |
146 | /* proper strings to change tty type */ | |
91d78146 MH |
147 | termreset(); |
148 | gettmode(); | |
149 | value(REDRAW) = AL && DL; | |
150 | value(OPTIMIZE) = !CA && !GT; | |
04379bab MH |
151 | if (ospeed == B1200 && !value(REDRAW)) |
152 | value(SLOWOPEN) = 1; /* see also gettmode above */ | |
91d78146 MH |
153 | if (unknown) |
154 | serror("%s: Unknown terminal type", type); | |
155 | } | |
156 | ||
157 | zap() | |
158 | { | |
159 | register char *namp; | |
160 | register bool **fp; | |
161 | register char ***sp; | |
162 | ||
3c7b865a | 163 | namp = "ambsdadbeohchzinmincnsosulxbxnxtxx"; |
91d78146 MH |
164 | fp = sflags; |
165 | do { | |
166 | *(*fp++) = tgetflag(namp); | |
167 | namp += 2; | |
168 | } while (*namp); | |
17687128 | 169 | namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI"; |
91d78146 MH |
170 | sp = sstrs; |
171 | do { | |
172 | *(*sp++) = tgetstr(namp, &aoftspace); | |
173 | namp += 2; | |
174 | } while (*namp); | |
175 | } | |
176 | ||
177 | char * | |
178 | longname(bp, def) | |
179 | register char *bp; | |
180 | char *def; | |
181 | { | |
182 | register char *cp; | |
183 | ||
184 | while (*bp && *bp != ':' && *bp != '|') | |
185 | bp++; | |
186 | if (*bp == '|') { | |
187 | bp++; | |
188 | cp = bp; | |
189 | while (*cp && *cp != ':' && *cp != '|') | |
190 | cp++; | |
191 | *cp = 0; | |
192 | return (bp); | |
193 | } | |
194 | return (def); | |
195 | } | |
196 | ||
197 | char * | |
198 | fkey(i) | |
199 | int i; | |
200 | { | |
201 | if (0 <= i && i <= 9) | |
202 | return(*fkeys[i]); | |
203 | else | |
204 | return(NOSTR); | |
205 | } | |
04379bab MH |
206 | |
207 | /* | |
208 | * cost figures out how much (in characters) it costs to send the string | |
209 | * str to the terminal. It takes into account padding information, as | |
210 | * much as it can, for a typical case. (Right now the typical case assumes | |
211 | * the number of lines affected is the size of the screen, since this is | |
212 | * mainly used to decide if AL or SR is better, and this always happens | |
213 | * at the top of the screen. We assume cursor motion (CM) has little | |
214 | * padding, if any, required, so that case, which is really more important | |
215 | * than AL vs SR, won't be really affected.) | |
216 | */ | |
217 | static int costnum; | |
218 | cost(str) | |
219 | char *str; | |
220 | { | |
221 | int countnum(); | |
222 | ||
17687128 | 223 | if (str == NULL || *str=='O') /* OOPS */ |
04379bab MH |
224 | return 10000; /* infinity */ |
225 | costnum = 0; | |
226 | tputs(str, LINES, countnum); | |
227 | return costnum; | |
228 | } | |
229 | ||
230 | /* ARGSUSED */ | |
231 | countnum(ch) | |
232 | char ch; | |
233 | { | |
234 | costnum++; | |
235 | } |