* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)term.c 8.41 (Berkeley) 1/23/94";
static int keycmp
__P((const void *, const void *));
* If we're reading less than 20 characters, up the size of the tty buffer.
* This shouldn't ever happen, other than the first time through, but it's
* possible if a map is large enough.
#define term_read_grow(sp, tty) \
(tty)->len - (tty)->cnt >= 20 ? 0 : __term_read_grow(sp, tty)
static int __term_read_grow
__P((SCR
*, IBUF
*));
* THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
char *ts
; /* Key's termcap string. */
char *output
; /* Corresponding vi command. */
static TKLIST
const tklist
[] = {
{"kA", "O", "insert line"},
{"kD", "x", "delete character"},
{"kd", "j", "cursor down"},
{"kE", "D", "delete to eol"},
{"kF", "\004", "scroll down"},
{"kH", "$", "go to eol"},
{"kh", "^", "go to sol"},
{"kI", "i", "insert at cursor"},
{"kL", "dd", "delete line"},
{"kl", "h", "cursor left"},
{"kN", "\006", "page down"},
{"kP", "\002", "page up"},
{"kR", "\025", "scroll up"},
{"kS", "dG", "delete to end of screen"},
{"kr", "l", "cursor right"},
{"ku", "k", "cursor up"},
* THIS REQUIRES THAT ALL SCREENS SHARE A SPECIAL KEY SET.
typedef struct _keylist
{
u_char value
; /* Special value. */
static KEYLIST keylist
[] = {
* Initialize the special key lookup table, and the special keys
* defined by the terminal's termcap entry.
extern CHNAME
const asciiname
[]; /* XXX */
char *sbp
, *t
, buf
[2 * 1024], sbuf
[128];
* 8-bit, ASCII only, for now. Recompilation should get you
* any 8-bit character set, as long as nul isn't a character.
gp
->cname
= asciiname
; /* XXX */
/* Set keys found in the termios structure. */
#define TERMSET(name, val) { \
if ((ch = gp->original_termios.c_cc[name]) != _POSIX_VDISABLE) \
for (kp = keylist;; ++kp) \
if (kp->value == (val)) { \
* VEOF, VERASE, VKILL are required by POSIX 1003.1-1990,
* VWERASE is a 4.4BSD extension.
TERMSET(VERASE
, K_VERASE
);
TERMSET(VWERASE
, K_VWERASE
);
/* Sort the special key list. */
sizeof(keylist
) / sizeof(keylist
[0]), sizeof(keylist
[0]), keycmp
);
/* Initialize the fast lookup table. */
gp
->special_key
, u_char
*, MAX_FAST_KEY
+ 1, sizeof(u_char
));
for (gp
->max_special
= 0, kp
= keylist
,
cnt
= sizeof(keylist
) / sizeof(keylist
[0]); cnt
--; ++kp
) {
if (gp
->max_special
< kp
->value
)
gp
->max_special
= kp
->value
;
if (kp
->ch
<= MAX_FAST_KEY
)
gp
->special_key
[kp
->ch
] = kp
->value
;
/* Set key sequences found in the termcap entry. */
switch (tgetent(buf
, O_STR(sp
, O_TERM
))) {
"tgetent: %s: %s.", O_STR(sp
, O_TERM
), strerror(errno
));
"%s: unknown terminal type.", O_STR(sp
, O_TERM
));
for (tkp
= tklist
; tkp
->name
!= NULL
; ++tkp
) {
if ((t
= tgetstr(tkp
->ts
, &sbp
)) == NULL
)
if (seq_set(sp
, tkp
->name
, strlen(tkp
->name
), t
, strlen(t
),
tkp
->output
, strlen(tkp
->output
), SEQ_COMMAND
, 0))
* Push keys onto the front of a buffer.
* There is a single input buffer in ex/vi. Characters are read onto the
* end of the buffer by the terminal input routines, and pushed onto the
* front of the buffer various other functions in ex/vi. Each key has an
* associated flag value, which indicates if it has already been quoted,
* if it is the result of a mapping or an abbreviation.
term_push(sp
, s
, len
, cmap
, flags
)
CHAR_T
*s
; /* Characters. */
size_t len
; /* Number of chars. */
u_int cmap
; /* Map count. */
u_int flags
; /* CH_* flags. */
/* If we have room, stuff the keys into the buffer. */
(tty
->ch
!= NULL
&& tty
->cnt
== 0 && len
<= tty
->len
)) {
memmove(tty
->ch
+ tty
->next
, s
, len
* sizeof(CHAR_T
));
memset(tty
->chf
+ tty
->next
, flags
, len
);
memset(tty
->cmap
+ tty
->next
, cmap
, len
);
/* Get enough space plus a little extra. */
BINC_RET(sp
, tty
->ch
, olen
, nlen
* sizeof(tty
->ch
[0]));
BINC_RET(sp
, tty
->chf
, olen
, nlen
* sizeof(tty
->chf
[0]));
BINC_RET(sp
, tty
->cmap
, tty
->len
, nlen
* sizeof(tty
->cmap
[0]));
* If there are currently characters in the queue, shift them up,
* leaving some extra room.
#define TERM_PUSH_SHIFT 30
memmove(tty
->ch
+ TERM_PUSH_SHIFT
+ len
,
tty
->ch
+ tty
->next
, tty
->cnt
* sizeof(tty
->ch
[0]));
memmove(tty
->chf
+ TERM_PUSH_SHIFT
+ len
,
tty
->chf
+ tty
->next
, tty
->cnt
* sizeof(tty
->chf
[0]));
memmove(tty
->cmap
+ TERM_PUSH_SHIFT
+ len
,
tty
->cmap
+ tty
->next
, tty
->cnt
* sizeof(tty
->cmap
[0]));
/* Put the new characters into the queue. */
tty
->next
= TERM_PUSH_SHIFT
;
memmove(tty
->ch
+ TERM_PUSH_SHIFT
, s
, len
* sizeof(tty
->ch
[0]));
memset(tty
->chf
+ TERM_PUSH_SHIFT
, flags
, len
* sizeof(tty
->chf
[0]));
memset(tty
->cmap
+ TERM_PUSH_SHIFT
, cmap
, len
* sizeof(tty
->cmap
[0]));
* Remove characters from the queue, simultaneously clearing the
#define QREM_HEAD(q, len) { \
size_t __off = (q)->next; \
memset(tty->chf + __off, 0, len); \
memset(tty->cmap + __off, 0, len); \
if (((q)->cnt -= len) == 0) \
#define QREM_TAIL(q, len) { \
size_t __off = (q)->next + (q)->cnt - 1; \
memset(tty->chf + __off, 0, len); \
memset(tty->cmap + __off, 0, len); \
if (((q)->cnt -= len) == 0) \
* The flag TXT_MAPNODIGIT probably needs some explanation. First, the idea
* of mapping keys is that one or more keystrokes act like a function key.
* What's going on is that vi is reading a number, and the character following
* the number may or may not be mapped (TXT_MAPCOMMAND). For example, if the
* user is entering the z command, a valid command is "z40+", and we don't want
* to map the '+', i.e. if '+' is mapped to "xxx", we don't want to change it
* into "z40xxx". However, if the user enters "35x", we want to put all of the
* characters through the mapping code.
* Historical practice is a bit muddled here. (Surprise!) It always permitted
* mapping digits as long as they weren't the first character of the map, e.g.
* ":map ^A1 xxx" was okay. It also permitted the mapping of the digits 1-9
* (the digit 0 was a special case as it doesn't indicate the start of a count)
* as the first character of the map, but then ignored those mappings. While
* it's probably stupid to map digits, vi isn't your mother.
* The way this works is that the TXT_MAPNODIGIT causes term_key to return the
* end-of-digit without "looking" at the next character, i.e. leaving it as the
* user entered it. Presumably, the next term_key call will tell us how the
* There is one more complication. Users might map keys to digits, and, as
* it's described above, the commands "map g 1G|d2g" would return the keys
* "d2<end-of-digits>1G", when the user probably wanted "d21<end-of-digits>G".
* So, if a map starts off with a digit we continue as before, otherwise, we
* pretend that we haven't mapped the character and return <end-of-digits>.
* Now that that's out of the way, let's talk about Energizer Bunny macros.
* It's easy to create macros that expand to a loop, e.g. map x 3x. It's
* fairly easy to detect this example, because it's all internal to term_key.
* If we're expanding a macro and it gets big enough, at some point we can
* assume it's looping and kill it. The examples that are tough are the ones
* where the parser is involved, e.g. map x "ayyx"byy. We do an expansion
* on 'x', and get "ayyx"byy. We then return the first 4 characters, and then
* find the looping macro again. There is no way that we can detect this
* without doing a full parse of the command, because the character that might
* cause the loop (in this case 'x') may be a literal character, e.g. the map
* map x "ayy"xyy"byy is perfectly legal and won't cause a loop.
* Historic vi tried to detect looping macros by disallowing obvious cases in
* the map command, maps that that ended with the same letter as they started
* (which wrongly disallowed "map x 'x"), and detecting macros that expanded
* too many times before keys were returned to the command parser. It didn't
* get many (most?) of the tricky cases right, however, and it was certainly
* possible to create macros that ran forever. And, even if it did figure out
* what was going on, the user was usually tossed into ex mode. Finally, any
* changes made before vi realized that the macro was recursing were left in
* place. This implementation counts how many times each input character has
* been mapped. If it reaches some arbitrary value, we flush all mapped keys
* The final issue is recovery. It would be possible to undo all of the work
* that was done by the macro if we entered a record into the log so that we
* knew when the macro started, and, in fact, this might be worth doing at some
* point. Given that this might make the log grow unacceptably (consider that
* cursor keys are done with maps), for now we leave any changes made in place.
* If the queue is empty, read more keys in. Since no timeout is
* requested, s_key_read will either return an error or will read
* some number of characters.
loop
: if (tty
->cnt
== 0) {
if (term_read_grow(sp
, tty
))
if (rval
= sp
->s_key_read(sp
, &nr
, NULL
))
* If there's something on the mode line that we wanted
* the user to see, they just entered a character so we
* can presume they saw it.
if (F_ISSET(sp
, S_UPDATE_MODE
))
F_CLR(sp
, S_UPDATE_MODE
);
/* If the key is mappable and should be mapped, look it up. */
if (!(tty
->chf
[tty
->next
] & CH_NOMAP
) &&
LF_ISSET(TXT_MAPCOMMAND
| TXT_MAPINPUT
)) {
/* Set up timeout value. */
if (O_ISSET(sp
, O_TIMEOUT
)) {
t
.tv_sec
= O_VAL(sp
, O_KEYTIME
) / 10;
t
.tv_usec
= (O_VAL(sp
, O_KEYTIME
) % 10) * 100000L;
newmap
: ch
= tty
->ch
[tty
->next
];
if (ch
< MAX_BIT_SEQ
&& !bit_test(gp
->seqb
, ch
))
remap
: qp
= seq_find(sp
, NULL
, &tty
->ch
[tty
->next
], tty
->cnt
,
LF_ISSET(TXT_MAPCOMMAND
) ? SEQ_COMMAND
: SEQ_INPUT
,
* If get a partial match, read more characters and retry
* the map. If no characters read, return the characters
if (term_read_grow(sp
, tty
))
if (rval
= sp
->s_key_read(sp
, &nr
, tp
))
/* If no map, return the character. */
* If looking for the end of a digit string, and the first
* character of the map is it, pretend we haven't seen the
if (LF_ISSET(TXT_MAPNODIGIT
) && !isdigit(qp
->output
[0]))
* Only permit a character to be remapped a certain number
* of times before we figure that it's not going to finish.
if ((cmap
= tty
->cmap
[tty
->next
]) > MAX_MAP_COUNT
) {
term_map_flush(sp
, "Character remapped too many times");
/* Delete the mapped characters from the queue. */
QREM_HEAD(tty
, qp
->ilen
);
/* If remapping characters, push the character on the queue. */
if (O_ISSET(sp
, O_REMAP
)) {
if (term_push(sp
, qp
->output
, qp
->olen
, ++cmap
, 0))
/* Else, push the characters on the queue and return one. */
if (term_push(sp
, qp
->output
, qp
->olen
, 0, CH_NOMAP
))
nomap
: ch
= tty
->ch
[tty
->next
];
if (LF_ISSET(TXT_MAPNODIGIT
) && !isdigit(ch
)) {
not_digit_ch
: chp
->ch
= NOT_DIGIT_CH
;
/* Fill in the return information. */
chp
->flags
= tty
->chf
[tty
->next
];
chp
->value
= term_key_val(sp
, ch
);
/* Delete the character from the queue. */
* O_BEAUTIFY eliminates all control characters except
* escape, form-feed, newline and tab.
!LF_ISSET(TXT_BEAUTIFY
) || !O_ISSET(sp
, O_BEAUTIFY
) ||
chp
->value
== K_ESCAPE
|| chp
->value
== K_FORMFEED
||
chp
->value
== K_NL
|| chp
->value
== K_TAB
)
* Flush any abbreviated keys.
if (!tty
->cnt
|| !(tty
->chf
[tty
->next
] & CH_ABBREVIATED
))
} while (tty
->cnt
&& tty
->chf
[tty
->next
] & CH_ABBREVIATED
);
msgq(sp
, M_ERR
, "%s: keys flushed.", msg
);
if (!tty
->cnt
|| !tty
->cmap
[tty
->next
])
} while (tty
->cnt
&& tty
->cmap
[tty
->next
]);
msgq(sp
, M_ERR
, "%s: keys flushed.", msg
);
* Get the next key, but require the user enter one.
* Read any keys the user has waiting. Make the race
* condition as short as possible.
if (rval
= term_key_queue(sp
))
/* Wait and read another key. */
if (rval
= sp
->s_key_read(sp
, &nr
, NULL
))
/* Fill in the return information. */
chp
->ch
= tty
->ch
[tty
->next
+ (tty
->cnt
- 1)];
chp
->value
= term_key_val(sp
, chp
->ch
);
* Read the keys off of the terminal queue until it's empty.
for (tty
= sp
->gp
->tty
;;) {
if (term_read_grow(sp
, tty
))
if (rval
= sp
->s_key_read(sp
, &nr
, &t
))
* Fill in the key for a value.
term_key_ch(sp
, val
, chp
)
for (kp
= keylist
;; ++kp
)
* Fill in the value for a key. This routine is the backup
* for the term_key_val() macro.
kp
= bsearch(&k
, keylist
,
sizeof(keylist
) / sizeof(keylist
[0]), sizeof(keylist
[0]), keycmp
);
return (kp
== NULL
? 0 : kp
->value
);
* Grow the terminal queue. This routine is the backup for
* the term_read_grow() macro.
__term_read_grow(sp
, tty
)
alen
= tty
->len
- (tty
->next
+ tty
->cnt
);
BINC_RET(sp
, tty
->ch
, len
, nlen
* sizeof(tty
->ch
[0]));
memset(tty
->ch
+ tty
->next
+ tty
->cnt
, 0, alen
* sizeof(tty
->ch
[0]));
BINC_RET(sp
, tty
->chf
, len
, nlen
* sizeof(tty
->chf
[0]));
memset(tty
->chf
+ tty
->next
+ tty
->cnt
, 0, alen
* sizeof(tty
->chf
[0]));
BINC_RET(sp
, tty
->cmap
, tty
->len
, nlen
* sizeof(tty
->cmap
[0]));
tty
->next
+ tty
->cnt
, 0, alen
* sizeof(tty
->cmap
[0]));
return (((KEYLIST
*)ap
)->ch
- ((KEYLIST
*)bp
)->ch
);