Commit | Line | Data |
---|---|---|
1e64b3ba JH |
1 | /*- |
2 | * Copyright (c) 1991, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
178c26c5 | 35 | static char sccsid[] = "@(#)term.c 8.41 (Berkeley) 1/23/94"; |
1e64b3ba JH |
36 | #endif /* not lint */ |
37 | ||
38 | #include <sys/types.h> | |
39 | #include <sys/time.h> | |
40 | ||
41 | #include <ctype.h> | |
42 | #include <curses.h> | |
43 | #include <errno.h> | |
44 | #include <stdlib.h> | |
45 | #include <string.h> | |
46 | #include <unistd.h> | |
47 | ||
48 | #include "vi.h" | |
49 | #include "seq.h" | |
50 | ||
51 | static int keycmp __P((const void *, const void *)); | |
52 | ||
53 | /* | |
54 | * If we're reading less than 20 characters, up the size of the tty buffer. | |
55 | * This shouldn't ever happen, other than the first time through, but it's | |
56 | * possible if a map is large enough. | |
57 | */ | |
58 | #define term_read_grow(sp, tty) \ | |
59 | (tty)->len - (tty)->cnt >= 20 ? 0 : __term_read_grow(sp, tty) | |
60 | static int __term_read_grow __P((SCR *, IBUF *)); | |
61 | ||
62 | /* | |
63 | * XXX | |
64 | * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE. | |
65 | */ | |
66 | typedef struct _tklist { | |
67 | char *ts; /* Key's termcap string. */ | |
68 | char *output; /* Corresponding vi command. */ | |
69 | char *name; /* Name. */ | |
70 | } TKLIST; | |
71 | static TKLIST const tklist[] = { | |
72 | {"kA", "O", "insert line"}, | |
73 | {"kD", "x", "delete character"}, | |
74 | {"kd", "j", "cursor down"}, | |
75 | {"kE", "D", "delete to eol"}, | |
76 | {"kF", "\004", "scroll down"}, | |
77 | {"kH", "$", "go to eol"}, | |
78 | {"kh", "^", "go to sol"}, | |
79 | {"kI", "i", "insert at cursor"}, | |
80 | {"kL", "dd", "delete line"}, | |
81 | {"kl", "h", "cursor left"}, | |
82 | {"kN", "\006", "page down"}, | |
83 | {"kP", "\002", "page up"}, | |
84 | {"kR", "\025", "scroll up"}, | |
85 | {"kS", "dG", "delete to end of screen"}, | |
86 | {"kr", "l", "cursor right"}, | |
87 | {"ku", "k", "cursor up"}, | |
88 | {NULL}, | |
89 | }; | |
90 | ||
91 | /* | |
92 | * XXX | |
93 | * THIS REQUIRES THAT ALL SCREENS SHARE A SPECIAL KEY SET. | |
94 | */ | |
95 | typedef struct _keylist { | |
96 | u_char value; /* Special value. */ | |
97 | CHAR_T ch; /* Key. */ | |
98 | } KEYLIST; | |
99 | static KEYLIST keylist[] = { | |
100 | {K_CARAT, '^'}, | |
101 | {K_CNTRLR, '\022'}, | |
102 | {K_CNTRLT, '\024'}, | |
103 | {K_CNTRLZ, '\032'}, | |
104 | {K_COLON, ':'}, | |
105 | {K_CR, '\r'}, | |
106 | {K_ESCAPE, '\033'}, | |
107 | {K_FORMFEED, '\f'}, | |
108 | {K_NL, '\n'}, | |
109 | {K_RIGHTBRACE, '}'}, | |
110 | {K_RIGHTPAREN, ')'}, | |
111 | {K_TAB, '\t'}, | |
112 | {K_VEOF, '\004'}, | |
113 | {K_VERASE, '\b'}, | |
114 | {K_VINTR, '\003'}, | |
115 | {K_VKILL, '\025'}, | |
116 | {K_VLNEXT, '\026'}, | |
117 | {K_VWERASE, '\027'}, | |
118 | {K_ZERO, '0'}, | |
119 | }; | |
120 | ||
121 | /* | |
122 | * term_init -- | |
123 | * Initialize the special key lookup table, and the special keys | |
124 | * defined by the terminal's termcap entry. | |
125 | */ | |
126 | int | |
127 | term_init(sp) | |
128 | SCR *sp; | |
129 | { | |
130 | extern CHNAME const asciiname[]; /* XXX */ | |
131 | GS *gp; | |
132 | KEYLIST *kp; | |
133 | TKLIST const *tkp; | |
134 | cc_t ch; | |
135 | int cnt; | |
136 | char *sbp, *t, buf[2 * 1024], sbuf[128]; | |
137 | ||
138 | /* | |
139 | * XXX | |
140 | * 8-bit, ASCII only, for now. Recompilation should get you | |
141 | * any 8-bit character set, as long as nul isn't a character. | |
142 | */ | |
143 | gp = sp->gp; | |
144 | gp->cname = asciiname; /* XXX */ | |
145 | ||
146 | /* Set keys found in the termios structure. */ | |
147 | #define TERMSET(name, val) { \ | |
148 | if ((ch = gp->original_termios.c_cc[name]) != _POSIX_VDISABLE) \ | |
149 | for (kp = keylist;; ++kp) \ | |
150 | if (kp->value == (val)) { \ | |
151 | kp->ch = ch; \ | |
152 | break; \ | |
153 | } \ | |
154 | } | |
155 | /* | |
156 | * VEOF, VERASE, VKILL are required by POSIX 1003.1-1990, | |
157 | * VWERASE is a 4.4BSD extension. | |
158 | */ | |
159 | #ifdef VEOF | |
160 | TERMSET(VEOF, K_VEOF); | |
161 | #endif | |
162 | #ifdef VERASE | |
163 | TERMSET(VERASE, K_VERASE); | |
164 | #endif | |
165 | #ifdef VINTR | |
166 | TERMSET(VINTR, K_VINTR); | |
167 | #endif | |
168 | #ifdef VKILL | |
169 | TERMSET(VKILL, K_VKILL); | |
170 | #endif | |
171 | #ifdef VWERASE | |
172 | TERMSET(VWERASE, K_VWERASE); | |
173 | #endif | |
174 | ||
175 | /* Sort the special key list. */ | |
176 | qsort(keylist, | |
177 | sizeof(keylist) / sizeof(keylist[0]), sizeof(keylist[0]), keycmp); | |
178 | ||
179 | /* Initialize the fast lookup table. */ | |
180 | CALLOC_RET(sp, | |
181 | gp->special_key, u_char *, MAX_FAST_KEY + 1, sizeof(u_char)); | |
182 | for (gp->max_special = 0, kp = keylist, | |
183 | cnt = sizeof(keylist) / sizeof(keylist[0]); cnt--; ++kp) { | |
184 | if (gp->max_special < kp->value) | |
185 | gp->max_special = kp->value; | |
186 | if (kp->ch <= MAX_FAST_KEY) | |
187 | gp->special_key[kp->ch] = kp->value; | |
188 | } | |
189 | ||
190 | /* Set key sequences found in the termcap entry. */ | |
191 | switch (tgetent(buf, O_STR(sp, O_TERM))) { | |
192 | case -1: | |
193 | msgq(sp, M_ERR, | |
194 | "tgetent: %s: %s.", O_STR(sp, O_TERM), strerror(errno)); | |
195 | return (0); | |
196 | case 0: | |
197 | msgq(sp, M_ERR, | |
198 | "%s: unknown terminal type.", O_STR(sp, O_TERM)); | |
199 | return (0); | |
200 | } | |
201 | ||
202 | for (tkp = tklist; tkp->name != NULL; ++tkp) { | |
203 | sbp = sbuf; | |
204 | if ((t = tgetstr(tkp->ts, &sbp)) == NULL) | |
205 | continue; | |
206 | if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t), | |
207 | tkp->output, strlen(tkp->output), SEQ_COMMAND, 0)) | |
208 | return (1); | |
209 | } | |
210 | return (0); | |
211 | } | |
212 | ||
213 | /* | |
214 | * term_push -- | |
215 | * Push keys onto the front of a buffer. | |
216 | * | |
217 | * There is a single input buffer in ex/vi. Characters are read onto the | |
218 | * end of the buffer by the terminal input routines, and pushed onto the | |
219 | * front of the buffer various other functions in ex/vi. Each key has an | |
220 | * associated flag value, which indicates if it has already been quoted, | |
221 | * if it is the result of a mapping or an abbreviation. | |
222 | */ | |
223 | int | |
224 | term_push(sp, s, len, cmap, flags) | |
225 | SCR *sp; | |
226 | CHAR_T *s; /* Characters. */ | |
227 | size_t len; /* Number of chars. */ | |
228 | u_int cmap; /* Map count. */ | |
229 | u_int flags; /* CH_* flags. */ | |
230 | { | |
231 | IBUF *tty; | |
232 | size_t nlen; | |
233 | ||
234 | /* If we have room, stuff the keys into the buffer. */ | |
235 | tty = sp->gp->tty; | |
236 | if (len <= tty->next || | |
237 | (tty->ch != NULL && tty->cnt == 0 && len <= tty->len)) { | |
238 | if (tty->cnt != 0) | |
239 | tty->next -= len; | |
240 | tty->cnt += len; | |
241 | memmove(tty->ch + tty->next, s, len * sizeof(CHAR_T)); | |
242 | memset(tty->chf + tty->next, flags, len); | |
243 | memset(tty->cmap + tty->next, cmap, len); | |
244 | return (0); | |
245 | } | |
246 | ||
247 | /* Get enough space plus a little extra. */ | |
248 | nlen = tty->cnt + len; | |
249 | if (nlen > tty->len) { | |
250 | size_t olen; | |
251 | ||
252 | nlen += 64; | |
253 | olen = tty->len; | |
254 | BINC_RET(sp, tty->ch, olen, nlen * sizeof(tty->ch[0])); | |
255 | olen = tty->len; | |
256 | BINC_RET(sp, tty->chf, olen, nlen * sizeof(tty->chf[0])); | |
257 | BINC_RET(sp, tty->cmap, tty->len, nlen * sizeof(tty->cmap[0])); | |
258 | } | |
259 | ||
260 | /* | |
261 | * If there are currently characters in the queue, shift them up, | |
262 | * leaving some extra room. | |
263 | */ | |
264 | #define TERM_PUSH_SHIFT 30 | |
265 | if (tty->cnt) { | |
266 | memmove(tty->ch + TERM_PUSH_SHIFT + len, | |
267 | tty->ch + tty->next, tty->cnt * sizeof(tty->ch[0])); | |
268 | memmove(tty->chf + TERM_PUSH_SHIFT + len, | |
269 | tty->chf + tty->next, tty->cnt * sizeof(tty->chf[0])); | |
270 | memmove(tty->cmap + TERM_PUSH_SHIFT + len, | |
271 | tty->cmap + tty->next, tty->cnt * sizeof(tty->cmap[0])); | |
272 | } | |
273 | ||
274 | /* Put the new characters into the queue. */ | |
275 | tty->next = TERM_PUSH_SHIFT; | |
276 | tty->cnt += len; | |
277 | memmove(tty->ch + TERM_PUSH_SHIFT, s, len * sizeof(tty->ch[0])); | |
278 | memset(tty->chf + TERM_PUSH_SHIFT, flags, len * sizeof(tty->chf[0])); | |
279 | memset(tty->cmap + TERM_PUSH_SHIFT, cmap, len * sizeof(tty->cmap[0])); | |
280 | return (0); | |
281 | } | |
282 | ||
283 | /* | |
284 | * Remove characters from the queue, simultaneously clearing the | |
285 | * flag and map counts. | |
286 | */ | |
287 | #define QREM_HEAD(q, len) { \ | |
288 | size_t __off = (q)->next; \ | |
289 | if (len == 1) { \ | |
290 | tty->chf[__off] = 0; \ | |
291 | tty->cmap[__off] = 0; \ | |
292 | } else { \ | |
293 | memset(tty->chf + __off, 0, len); \ | |
294 | memset(tty->cmap + __off, 0, len); \ | |
295 | } \ | |
296 | if (((q)->cnt -= len) == 0) \ | |
297 | (q)->next = 0; \ | |
298 | else \ | |
299 | (q)->next += len; \ | |
300 | } | |
301 | #define QREM_TAIL(q, len) { \ | |
302 | size_t __off = (q)->next + (q)->cnt - 1; \ | |
303 | if (len == 1) { \ | |
304 | tty->chf[__off] = 0; \ | |
305 | tty->cmap[__off] = 0; \ | |
306 | } else { \ | |
307 | memset(tty->chf + __off, 0, len); \ | |
308 | memset(tty->cmap + __off, 0, len); \ | |
309 | } \ | |
310 | if (((q)->cnt -= len) == 0) \ | |
311 | (q)->next = 0; \ | |
312 | } | |
313 | ||
314 | /* | |
315 | * term_key -- | |
316 | * Get the next key. | |
317 | * | |
318 | * !!! | |
319 | * The flag TXT_MAPNODIGIT probably needs some explanation. First, the idea | |
320 | * of mapping keys is that one or more keystrokes act like a function key. | |
321 | * What's going on is that vi is reading a number, and the character following | |
322 | * the number may or may not be mapped (TXT_MAPCOMMAND). For example, if the | |
323 | * user is entering the z command, a valid command is "z40+", and we don't want | |
324 | * to map the '+', i.e. if '+' is mapped to "xxx", we don't want to change it | |
325 | * into "z40xxx". However, if the user enters "35x", we want to put all of the | |
326 | * characters through the mapping code. | |
327 | * | |
328 | * Historical practice is a bit muddled here. (Surprise!) It always permitted | |
329 | * mapping digits as long as they weren't the first character of the map, e.g. | |
330 | * ":map ^A1 xxx" was okay. It also permitted the mapping of the digits 1-9 | |
331 | * (the digit 0 was a special case as it doesn't indicate the start of a count) | |
332 | * as the first character of the map, but then ignored those mappings. While | |
333 | * it's probably stupid to map digits, vi isn't your mother. | |
334 | * | |
335 | * The way this works is that the TXT_MAPNODIGIT causes term_key to return the | |
336 | * end-of-digit without "looking" at the next character, i.e. leaving it as the | |
337 | * user entered it. Presumably, the next term_key call will tell us how the | |
338 | * user wants it handled. | |
339 | * | |
340 | * There is one more complication. Users might map keys to digits, and, as | |
341 | * it's described above, the commands "map g 1G|d2g" would return the keys | |
342 | * "d2<end-of-digits>1G", when the user probably wanted "d21<end-of-digits>G". | |
343 | * So, if a map starts off with a digit we continue as before, otherwise, we | |
344 | * pretend that we haven't mapped the character and return <end-of-digits>. | |
345 | * | |
346 | * Now that that's out of the way, let's talk about Energizer Bunny macros. | |
347 | * It's easy to create macros that expand to a loop, e.g. map x 3x. It's | |
348 | * fairly easy to detect this example, because it's all internal to term_key. | |
349 | * If we're expanding a macro and it gets big enough, at some point we can | |
350 | * assume it's looping and kill it. The examples that are tough are the ones | |
351 | * where the parser is involved, e.g. map x "ayyx"byy. We do an expansion | |
352 | * on 'x', and get "ayyx"byy. We then return the first 4 characters, and then | |
353 | * find the looping macro again. There is no way that we can detect this | |
354 | * without doing a full parse of the command, because the character that might | |
355 | * cause the loop (in this case 'x') may be a literal character, e.g. the map | |
356 | * map x "ayy"xyy"byy is perfectly legal and won't cause a loop. | |
357 | * | |
358 | * Historic vi tried to detect looping macros by disallowing obvious cases in | |
359 | * the map command, maps that that ended with the same letter as they started | |
360 | * (which wrongly disallowed "map x 'x"), and detecting macros that expanded | |
361 | * too many times before keys were returned to the command parser. It didn't | |
362 | * get many (most?) of the tricky cases right, however, and it was certainly | |
363 | * possible to create macros that ran forever. And, even if it did figure out | |
364 | * what was going on, the user was usually tossed into ex mode. Finally, any | |
365 | * changes made before vi realized that the macro was recursing were left in | |
366 | * place. This implementation counts how many times each input character has | |
367 | * been mapped. If it reaches some arbitrary value, we flush all mapped keys | |
368 | * and return an error. | |
369 | * | |
370 | * XXX | |
371 | * The final issue is recovery. It would be possible to undo all of the work | |
372 | * that was done by the macro if we entered a record into the log so that we | |
373 | * knew when the macro started, and, in fact, this might be worth doing at some | |
374 | * point. Given that this might make the log grow unacceptably (consider that | |
375 | * cursor keys are done with maps), for now we leave any changes made in place. | |
376 | */ | |
377 | enum input | |
378 | term_key(sp, chp, flags) | |
379 | SCR *sp; | |
380 | CH *chp; | |
381 | u_int flags; | |
382 | { | |
383 | enum input rval; | |
384 | struct timeval t, *tp; | |
385 | CHAR_T ch; | |
386 | GS *gp; | |
387 | IBUF *tty; | |
388 | SEQ *qp; | |
389 | int cmap, ispartial, nr; | |
390 | ||
391 | gp = sp->gp; | |
392 | tty = gp->tty; | |
393 | ||
394 | /* | |
395 | * If the queue is empty, read more keys in. Since no timeout is | |
396 | * requested, s_key_read will either return an error or will read | |
397 | * some number of characters. | |
398 | */ | |
399 | loop: if (tty->cnt == 0) { | |
400 | if (term_read_grow(sp, tty)) | |
401 | return (INP_ERR); | |
402 | if (rval = sp->s_key_read(sp, &nr, NULL)) | |
403 | return (rval); | |
404 | /* | |
405 | * If there's something on the mode line that we wanted | |
406 | * the user to see, they just entered a character so we | |
407 | * can presume they saw it. | |
408 | */ | |
409 | if (F_ISSET(sp, S_UPDATE_MODE)) | |
410 | F_CLR(sp, S_UPDATE_MODE); | |
411 | } | |
412 | ||
413 | /* If the key is mappable and should be mapped, look it up. */ | |
414 | if (!(tty->chf[tty->next] & CH_NOMAP) && | |
415 | LF_ISSET(TXT_MAPCOMMAND | TXT_MAPINPUT)) { | |
416 | /* Set up timeout value. */ | |
417 | if (O_ISSET(sp, O_TIMEOUT)) { | |
418 | tp = &t; | |
419 | t.tv_sec = O_VAL(sp, O_KEYTIME) / 10; | |
420 | t.tv_usec = (O_VAL(sp, O_KEYTIME) % 10) * 100000L; | |
421 | } else | |
422 | tp = NULL; | |
423 | ||
424 | /* Get the next key. */ | |
425 | newmap: ch = tty->ch[tty->next]; | |
426 | if (ch < MAX_BIT_SEQ && !bit_test(gp->seqb, ch)) | |
427 | goto nomap; | |
428 | ||
429 | /* Search the map. */ | |
430 | remap: qp = seq_find(sp, NULL, &tty->ch[tty->next], tty->cnt, | |
431 | LF_ISSET(TXT_MAPCOMMAND) ? SEQ_COMMAND : SEQ_INPUT, | |
432 | &ispartial); | |
433 | ||
434 | /* | |
435 | * If get a partial match, read more characters and retry | |
436 | * the map. If no characters read, return the characters | |
437 | * unmapped. | |
438 | */ | |
439 | if (ispartial) { | |
440 | if (term_read_grow(sp, tty)) | |
441 | return (INP_ERR); | |
442 | if (rval = sp->s_key_read(sp, &nr, tp)) | |
443 | return (rval); | |
444 | if (nr) | |
445 | goto remap; | |
446 | goto nomap; | |
447 | } | |
448 | ||
449 | /* If no map, return the character. */ | |
450 | if (qp == NULL) | |
451 | goto nomap; | |
452 | ||
453 | /* | |
454 | * If looking for the end of a digit string, and the first | |
455 | * character of the map is it, pretend we haven't seen the | |
456 | * character. | |
457 | */ | |
458 | if (LF_ISSET(TXT_MAPNODIGIT) && !isdigit(qp->output[0])) | |
459 | goto not_digit_ch; | |
460 | ||
461 | /* | |
462 | * Only permit a character to be remapped a certain number | |
463 | * of times before we figure that it's not going to finish. | |
464 | */ | |
465 | if ((cmap = tty->cmap[tty->next]) > MAX_MAP_COUNT) { | |
466 | term_map_flush(sp, "Character remapped too many times"); | |
467 | return (INP_ERR); | |
468 | } | |
469 | ||
470 | /* Delete the mapped characters from the queue. */ | |
471 | QREM_HEAD(tty, qp->ilen); | |
472 | ||
473 | /* If remapping characters, push the character on the queue. */ | |
474 | if (O_ISSET(sp, O_REMAP)) { | |
475 | if (term_push(sp, qp->output, qp->olen, ++cmap, 0)) | |
476 | return (INP_ERR); | |
477 | goto newmap; | |
478 | } | |
479 | ||
480 | /* Else, push the characters on the queue and return one. */ | |
481 | if (term_push(sp, qp->output, qp->olen, 0, CH_NOMAP)) | |
482 | return (INP_ERR); | |
483 | } | |
484 | ||
485 | nomap: ch = tty->ch[tty->next]; | |
486 | if (LF_ISSET(TXT_MAPNODIGIT) && !isdigit(ch)) { | |
487 | not_digit_ch: chp->ch = NOT_DIGIT_CH; | |
488 | chp->value = 0; | |
489 | chp->flags = 0; | |
490 | return (INP_OK); | |
491 | } | |
492 | ||
493 | /* Fill in the return information. */ | |
494 | chp->ch = ch; | |
495 | chp->flags = tty->chf[tty->next]; | |
496 | chp->value = term_key_val(sp, ch); | |
497 | ||
498 | /* Delete the character from the queue. */ | |
499 | QREM_HEAD(tty, 1); | |
500 | ||
501 | /* | |
502 | * O_BEAUTIFY eliminates all control characters except | |
503 | * escape, form-feed, newline and tab. | |
504 | */ | |
505 | if (isprint(ch) || | |
506 | !LF_ISSET(TXT_BEAUTIFY) || !O_ISSET(sp, O_BEAUTIFY) || | |
507 | chp->value == K_ESCAPE || chp->value == K_FORMFEED || | |
508 | chp->value == K_NL || chp->value == K_TAB) | |
509 | return (INP_OK); | |
510 | ||
511 | goto loop; | |
512 | } | |
513 | ||
514 | /* | |
515 | * term_ab_flush -- | |
516 | * Flush any abbreviated keys. | |
517 | */ | |
518 | void | |
519 | term_ab_flush(sp, msg) | |
520 | SCR *sp; | |
521 | char *msg; | |
522 | { | |
523 | IBUF *tty; | |
524 | ||
525 | tty = sp->gp->tty; | |
526 | if (!tty->cnt || !(tty->chf[tty->next] & CH_ABBREVIATED)) | |
527 | return; | |
528 | do { | |
529 | QREM_HEAD(tty, 1); | |
530 | } while (tty->cnt && tty->chf[tty->next] & CH_ABBREVIATED); | |
531 | msgq(sp, M_ERR, "%s: keys flushed.", msg); | |
532 | ||
533 | } | |
534 | /* | |
535 | * term_map_flush -- | |
536 | * Flush any mapped keys. | |
537 | */ | |
538 | void | |
539 | term_map_flush(sp, msg) | |
540 | SCR *sp; | |
541 | char *msg; | |
542 | { | |
543 | IBUF *tty; | |
544 | ||
545 | tty = sp->gp->tty; | |
546 | if (!tty->cnt || !tty->cmap[tty->next]) | |
547 | return; | |
548 | do { | |
549 | QREM_HEAD(tty, 1); | |
550 | } while (tty->cnt && tty->cmap[tty->next]); | |
551 | msgq(sp, M_ERR, "%s: keys flushed.", msg); | |
552 | ||
553 | } | |
554 | ||
555 | /* | |
556 | * term_user_key -- | |
557 | * Get the next key, but require the user enter one. | |
558 | */ | |
559 | enum input | |
560 | term_user_key(sp, chp) | |
561 | SCR *sp; | |
562 | CH *chp; | |
563 | { | |
564 | enum input rval; | |
565 | IBUF *tty; | |
566 | int nr; | |
567 | ||
568 | /* | |
569 | * Read any keys the user has waiting. Make the race | |
570 | * condition as short as possible. | |
571 | */ | |
572 | if (rval = term_key_queue(sp)) | |
573 | return (rval); | |
574 | ||
575 | /* Wait and read another key. */ | |
576 | if (rval = sp->s_key_read(sp, &nr, NULL)) | |
577 | return (rval); | |
578 | ||
579 | /* Fill in the return information. */ | |
580 | tty = sp->gp->tty; | |
581 | chp->ch = tty->ch[tty->next + (tty->cnt - 1)]; | |
582 | chp->flags = 0; | |
583 | chp->value = term_key_val(sp, chp->ch); | |
584 | ||
585 | QREM_TAIL(tty, 1); | |
586 | return (INP_OK); | |
587 | } | |
588 | ||
589 | /* | |
590 | * term_key_queue -- | |
591 | * Read the keys off of the terminal queue until it's empty. | |
592 | */ | |
593 | int | |
594 | term_key_queue(sp) | |
595 | SCR *sp; | |
596 | { | |
597 | enum input rval; | |
598 | struct timeval t; | |
599 | IBUF *tty; | |
600 | int nr; | |
601 | ||
602 | t.tv_sec = 0; | |
603 | t.tv_usec = 0; | |
604 | for (tty = sp->gp->tty;;) { | |
605 | if (term_read_grow(sp, tty)) | |
606 | return (INP_ERR); | |
607 | if (rval = sp->s_key_read(sp, &nr, &t)) | |
608 | return (rval); | |
609 | if (nr == 0) | |
610 | break; | |
611 | } | |
612 | return (INP_OK); | |
613 | } | |
614 | ||
615 | /* | |
616 | * term_key_ch -- | |
617 | * Fill in the key for a value. | |
618 | */ | |
619 | int | |
620 | term_key_ch(sp, val, chp) | |
621 | SCR *sp; | |
622 | int val; | |
623 | CHAR_T *chp; | |
624 | { | |
625 | KEYLIST *kp; | |
626 | ||
627 | for (kp = keylist;; ++kp) | |
628 | if (kp->value == val) { | |
629 | *chp = kp->ch; | |
630 | return (0); | |
631 | } | |
178c26c5 | 632 | /* NOTREACHED */ |
1e64b3ba JH |
633 | } |
634 | ||
635 | /* | |
636 | * __term_key_val -- | |
637 | * Fill in the value for a key. This routine is the backup | |
638 | * for the term_key_val() macro. | |
639 | */ | |
640 | int | |
641 | __term_key_val(sp, ch) | |
642 | SCR *sp; | |
643 | ARG_CHAR_T ch; | |
644 | { | |
645 | KEYLIST k, *kp; | |
646 | ||
647 | k.ch = ch; | |
648 | kp = bsearch(&k, keylist, | |
649 | sizeof(keylist) / sizeof(keylist[0]), sizeof(keylist[0]), keycmp); | |
650 | return (kp == NULL ? 0 : kp->value); | |
651 | } | |
652 | ||
653 | /* | |
654 | * __term_read_grow -- | |
655 | * Grow the terminal queue. This routine is the backup for | |
656 | * the term_read_grow() macro. | |
657 | */ | |
658 | static int | |
659 | __term_read_grow(sp, tty) | |
660 | SCR *sp; | |
661 | IBUF *tty; | |
662 | { | |
663 | size_t alen, len, nlen; | |
664 | ||
665 | nlen = tty->len + 64; | |
666 | alen = tty->len - (tty->next + tty->cnt); | |
667 | ||
668 | len = tty->len; | |
669 | BINC_RET(sp, tty->ch, len, nlen * sizeof(tty->ch[0])); | |
670 | memset(tty->ch + tty->next + tty->cnt, 0, alen * sizeof(tty->ch[0])); | |
671 | ||
672 | len = tty->len; | |
673 | BINC_RET(sp, tty->chf, len, nlen * sizeof(tty->chf[0])); | |
674 | memset(tty->chf + tty->next + tty->cnt, 0, alen * sizeof(tty->chf[0])); | |
675 | ||
676 | BINC_RET(sp, tty->cmap, tty->len, nlen * sizeof(tty->cmap[0])); | |
677 | memset(tty->cmap + | |
678 | tty->next + tty->cnt, 0, alen * sizeof(tty->cmap[0])); | |
679 | return (0); | |
680 | } | |
681 | ||
682 | static int | |
683 | keycmp(ap, bp) | |
684 | const void *ap, *bp; | |
685 | { | |
686 | return (((KEYLIST *)ap)->ch - ((KEYLIST *)bp)->ch); | |
687 | } |