1 1.4 christos /* $NetBSD: key.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */ 2 1.1 christos /*- 3 1.1 christos * Copyright (c) 1991, 1993, 1994 4 1.1 christos * The Regents of the University of California. All rights reserved. 5 1.1 christos * Copyright (c) 1991, 1993, 1994, 1995, 1996 6 1.1 christos * Keith Bostic. All rights reserved. 7 1.1 christos * 8 1.1 christos * See the LICENSE file for redistribution information. 9 1.1 christos */ 10 1.1 christos 11 1.1 christos #include "config.h" 12 1.1 christos 13 1.4 christos #include <sys/cdefs.h> 14 1.4 christos #if 0 15 1.1 christos #ifndef lint 16 1.1 christos static const char sccsid[] = "Id: key.c,v 10.48 2001/06/25 15:19:10 skimo Exp (Berkeley) Date: 2001/06/25 15:19:10 "; 17 1.1 christos #endif /* not lint */ 18 1.4 christos #else 19 1.4 christos __RCSID("$NetBSD: key.c,v 1.4 2014/01/26 21:43:45 christos Exp $"); 20 1.4 christos #endif 21 1.1 christos 22 1.1 christos #include <sys/types.h> 23 1.1 christos #include <sys/queue.h> 24 1.1 christos #include <sys/time.h> 25 1.1 christos 26 1.1 christos #include <bitstring.h> 27 1.1 christos #include <ctype.h> 28 1.1 christos #include <errno.h> 29 1.1 christos #include <limits.h> 30 1.1 christos #include <locale.h> 31 1.1 christos #include <stdio.h> 32 1.1 christos #include <stdlib.h> 33 1.1 christos #include <string.h> 34 1.1 christos #include <unistd.h> 35 1.1 christos 36 1.1 christos #include "common.h" 37 1.1 christos #include "../vi/vi.h" 38 1.1 christos 39 1.1 christos static int v_event_append __P((SCR *, EVENT *)); 40 1.1 christos static int v_event_grow __P((SCR *, int)); 41 1.1 christos static int v_key_cmp __P((const void *, const void *)); 42 1.1 christos static void v_keyval __P((SCR *, int, scr_keyval_t)); 43 1.1 christos static void v_sync __P((SCR *, int)); 44 1.1 christos 45 1.1 christos /* 46 1.1 christos * !!! 47 1.1 christos * Historic vi always used: 48 1.1 christos * 49 1.1 christos * ^D: autoindent deletion 50 1.1 christos * ^H: last character deletion 51 1.1 christos * ^W: last word deletion 52 1.1 christos * ^Q: quote the next character (if not used in flow control). 53 1.1 christos * ^V: quote the next character 54 1.1 christos * 55 1.1 christos * regardless of the user's choices for these characters. The user's erase 56 1.1 christos * and kill characters worked in addition to these characters. Nvi wires 57 1.1 christos * down the above characters, but in addition permits the VEOF, VERASE, VKILL 58 1.1 christos * and VWERASE characters described by the user's termios structure. 59 1.1 christos * 60 1.1 christos * Ex was not consistent with this scheme, as it historically ran in tty 61 1.1 christos * cooked mode. This meant that the scroll command and autoindent erase 62 1.1 christos * characters were mapped to the user's EOF character, and the character 63 1.1 christos * and word deletion characters were the user's tty character and word 64 1.1 christos * deletion characters. This implementation makes it all consistent, as 65 1.1 christos * described above for vi. 66 1.1 christos * 67 1.1 christos * !!! 68 1.1 christos * This means that all screens share a special key set. 69 1.1 christos */ 70 1.1 christos KEYLIST keylist[] = { 71 1.1 christos {K_BACKSLASH, '\\'}, /* \ */ 72 1.1 christos {K_CARAT, '^'}, /* ^ */ 73 1.1 christos {K_CNTRLD, '\004'}, /* ^D */ 74 1.1 christos {K_CNTRLR, '\022'}, /* ^R */ 75 1.1 christos {K_CNTRLT, '\024'}, /* ^T */ 76 1.1 christos {K_CNTRLZ, '\032'}, /* ^Z */ 77 1.1 christos {K_COLON, ':'}, /* : */ 78 1.1 christos {K_CR, '\r'}, /* \r */ 79 1.1 christos {K_ESCAPE, '\033'}, /* ^[ */ 80 1.1 christos {K_FORMFEED, '\f'}, /* \f */ 81 1.1 christos {K_HEXCHAR, '\030'}, /* ^X */ 82 1.1 christos {K_NL, '\n'}, /* \n */ 83 1.1 christos {K_RIGHTBRACE, '}'}, /* } */ 84 1.1 christos {K_RIGHTPAREN, ')'}, /* ) */ 85 1.1 christos {K_TAB, '\t'}, /* \t */ 86 1.1 christos {K_VERASE, '\b'}, /* \b */ 87 1.1 christos {K_VKILL, '\025'}, /* ^U */ 88 1.1 christos {K_VLNEXT, '\021'}, /* ^Q */ 89 1.1 christos {K_VLNEXT, '\026'}, /* ^V */ 90 1.1 christos {K_VWERASE, '\027'}, /* ^W */ 91 1.1 christos {K_ZERO, '0'}, /* 0 */ 92 1.1 christos 93 1.1 christos #define ADDITIONAL_CHARACTERS 4 94 1.1 christos {K_NOTUSED, 0}, /* VEOF, VERASE, VKILL, VWERASE */ 95 1.1 christos {K_NOTUSED, 0}, 96 1.1 christos {K_NOTUSED, 0}, 97 1.1 christos {K_NOTUSED, 0}, 98 1.1 christos }; 99 1.1 christos static int nkeylist = 100 1.1 christos (sizeof(keylist) / sizeof(keylist[0])) - ADDITIONAL_CHARACTERS; 101 1.1 christos 102 1.1 christos /* 103 1.1 christos * v_key_init -- 104 1.1 christos * Initialize the special key lookup table. 105 1.1 christos * 106 1.1 christos * PUBLIC: int v_key_init __P((SCR *)); 107 1.1 christos */ 108 1.1 christos int 109 1.1 christos v_key_init(SCR *sp) 110 1.1 christos { 111 1.2 christos int ch; 112 1.1 christos GS *gp; 113 1.1 christos KEYLIST *kp; 114 1.1 christos int cnt; 115 1.1 christos 116 1.1 christos gp = sp->gp; 117 1.1 christos 118 1.1 christos /* 119 1.1 christos * XXX 120 1.1 christos * 8-bit only, for now. Recompilation should get you any 8-bit 121 1.1 christos * character set, as long as nul isn't a character. 122 1.1 christos */ 123 1.1 christos (void)setlocale(LC_ALL, ""); 124 1.1 christos #if __linux__ 125 1.1 christos /* 126 1.1 christos * In libc 4.5.26, setlocale(LC_ALL, ""), doesn't setup the table 127 1.1 christos * for ctype(3c) correctly. This bug is fixed in libc 4.6.x. 128 1.1 christos * 129 1.1 christos * This code works around this problem for libc 4.5.x users. 130 1.1 christos * Note that this code is harmless if you're using libc 4.6.x. 131 1.1 christos */ 132 1.1 christos (void)setlocale(LC_CTYPE, ""); 133 1.1 christos #endif 134 1.1 christos v_key_ilookup(sp); 135 1.1 christos 136 1.1 christos v_keyval(sp, K_CNTRLD, KEY_VEOF); 137 1.1 christos v_keyval(sp, K_VERASE, KEY_VERASE); 138 1.1 christos v_keyval(sp, K_VKILL, KEY_VKILL); 139 1.1 christos v_keyval(sp, K_VWERASE, KEY_VWERASE); 140 1.1 christos 141 1.1 christos /* Sort the special key list. */ 142 1.1 christos qsort(keylist, nkeylist, sizeof(keylist[0]), v_key_cmp); 143 1.1 christos 144 1.1 christos /* Initialize the fast lookup table. */ 145 1.2 christos for (kp = keylist, cnt = nkeylist; cnt--; ++kp) 146 1.2 christos gp->special_key[kp->ch] = kp->value; 147 1.1 christos 148 1.1 christos /* Find a non-printable character to use as a message separator. */ 149 1.2 christos for (ch = 1; ch <= UCHAR_MAX; ++ch) 150 1.2 christos if (!isprint(ch)) { 151 1.1 christos gp->noprint = ch; 152 1.1 christos break; 153 1.1 christos } 154 1.1 christos if (ch != gp->noprint) { 155 1.1 christos msgq(sp, M_ERR, "079|No non-printable character found"); 156 1.1 christos return (1); 157 1.1 christos } 158 1.1 christos return (0); 159 1.1 christos } 160 1.1 christos 161 1.1 christos /* 162 1.1 christos * v_keyval -- 163 1.1 christos * Set key values. 164 1.1 christos * 165 1.1 christos * We've left some open slots in the keylist table, and if these values exist, 166 1.1 christos * we put them into place. Note, they may reset (or duplicate) values already 167 1.1 christos * in the table, so we check for that first. 168 1.1 christos */ 169 1.1 christos static void 170 1.1 christos v_keyval(SCR *sp, int val, scr_keyval_t name) 171 1.1 christos { 172 1.1 christos KEYLIST *kp; 173 1.1 christos CHAR_T ch; 174 1.1 christos int dne; 175 1.1 christos 176 1.1 christos /* Get the key's value from the screen. */ 177 1.1 christos if (sp->gp->scr_keyval(sp, name, &ch, &dne)) 178 1.1 christos return; 179 1.1 christos if (dne) 180 1.1 christos return; 181 1.1 christos 182 1.1 christos /* Check for duplication. */ 183 1.1 christos for (kp = keylist; kp->value != K_NOTUSED; ++kp) 184 1.1 christos if (kp->ch == ch) { 185 1.1 christos kp->value = val; 186 1.1 christos return; 187 1.1 christos } 188 1.1 christos 189 1.1 christos /* Add a new entry. */ 190 1.1 christos if (kp->value == K_NOTUSED) { 191 1.1 christos keylist[nkeylist].ch = ch; 192 1.1 christos keylist[nkeylist].value = val; 193 1.1 christos ++nkeylist; 194 1.1 christos } 195 1.1 christos } 196 1.1 christos 197 1.1 christos /* 198 1.1 christos * v_key_ilookup -- 199 1.1 christos * Build the fast-lookup key display array. 200 1.1 christos * 201 1.1 christos * PUBLIC: void v_key_ilookup __P((SCR *)); 202 1.1 christos */ 203 1.1 christos void 204 1.1 christos v_key_ilookup(SCR *sp) 205 1.1 christos { 206 1.1 christos UCHAR_T ch; 207 1.2 christos unsigned char *p, *t; 208 1.1 christos GS *gp; 209 1.1 christos size_t len; 210 1.1 christos 211 1.1 christos for (gp = sp->gp, ch = 0;; ++ch) { 212 1.1 christos for (p = gp->cname[ch].name, t = v_key_name(sp, ch), 213 1.1 christos len = gp->cname[ch].len = sp->clen; len--;) 214 1.1 christos *p++ = *t++; 215 1.1 christos if (ch == MAX_FAST_KEY) 216 1.1 christos break; 217 1.1 christos } 218 1.1 christos } 219 1.1 christos 220 1.1 christos /* 221 1.1 christos * v_key_len -- 222 1.1 christos * Return the length of the string that will display the key. 223 1.1 christos * This routine is the backup for the KEY_LEN() macro. 224 1.1 christos * 225 1.1 christos * PUBLIC: size_t v_key_len __P((SCR *, ARG_CHAR_T)); 226 1.1 christos */ 227 1.1 christos size_t 228 1.1 christos v_key_len(SCR *sp, ARG_CHAR_T ch) 229 1.1 christos { 230 1.1 christos (void)v_key_name(sp, ch); 231 1.1 christos return (sp->clen); 232 1.1 christos } 233 1.1 christos 234 1.1 christos /* 235 1.1 christos * v_key_name -- 236 1.1 christos * Return the string that will display the key. This routine 237 1.1 christos * is the backup for the KEY_NAME() macro. 238 1.1 christos * 239 1.1 christos * PUBLIC: u_char *v_key_name __P((SCR *, ARG_CHAR_T)); 240 1.1 christos */ 241 1.1 christos u_char * 242 1.1 christos v_key_name(SCR *sp, ARG_CHAR_T ach) 243 1.1 christos { 244 1.1 christos static const char hexdigit[] = "0123456789abcdef"; 245 1.1 christos static const char octdigit[] = "01234567"; 246 1.2 christos int ch; 247 1.2 christos size_t len, i; 248 1.2 christos const char *chp; 249 1.2 christos 250 1.2 christos if (INTISWIDE(ach)) 251 1.2 christos goto vis; 252 1.2 christos ch = (unsigned char)ach; 253 1.1 christos 254 1.1 christos /* See if the character was explicitly declared printable or not. */ 255 1.1 christos if ((chp = O_STR(sp, O_PRINT)) != NULL) 256 1.1 christos for (; *chp != '\0'; ++chp) 257 1.1 christos if (*chp == ch) 258 1.1 christos goto pr; 259 1.1 christos if ((chp = O_STR(sp, O_NOPRINT)) != NULL) 260 1.1 christos for (; *chp != '\0'; ++chp) 261 1.1 christos if (*chp == ch) 262 1.1 christos goto nopr; 263 1.1 christos 264 1.1 christos /* 265 1.1 christos * Historical (ARPA standard) mappings. Printable characters are left 266 1.1 christos * alone. Control characters less than 0x20 are represented as '^' 267 1.1 christos * followed by the character offset from the '@' character in the ASCII 268 1.1 christos * character set. Del (0x7f) is represented as '^' followed by '?'. 269 1.1 christos * 270 1.1 christos * XXX 271 1.1 christos * The following code depends on the current locale being identical to 272 1.1 christos * the ASCII map from 0x40 to 0x5f (since 0x1f + 0x40 == 0x5f). I'm 273 1.1 christos * told that this is a reasonable assumption... 274 1.1 christos * 275 1.1 christos * XXX 276 1.1 christos * This code will only work with CHAR_T's that are multiples of 8-bit 277 1.1 christos * bytes. 278 1.1 christos * 279 1.1 christos * XXX 280 1.1 christos * NB: There's an assumption here that all printable characters take 281 1.1 christos * up a single column on the screen. This is not always correct. 282 1.1 christos */ 283 1.2 christos if (isprint(ch)) { 284 1.1 christos pr: sp->cname[0] = ch; 285 1.1 christos len = 1; 286 1.1 christos goto done; 287 1.1 christos } 288 1.2 christos nopr: if (iscntrl(ch) && (ch < 0x20 || ch == 0x7f)) { 289 1.1 christos sp->cname[0] = '^'; 290 1.1 christos sp->cname[1] = ch == 0x7f ? '?' : '@' + ch; 291 1.1 christos len = 2; 292 1.2 christos goto done; 293 1.2 christos } 294 1.2 christos vis: for (i = 1; i <= sizeof(CHAR_T); ++i) 295 1.2 christos if ((ach >> i * CHAR_BIT) == 0) 296 1.2 christos break; 297 1.2 christos ch = (ach >> --i * CHAR_BIT) & UCHAR_MAX; 298 1.2 christos if (O_ISSET(sp, O_OCTAL)) { 299 1.1 christos sp->cname[0] = '\\'; 300 1.2 christos sp->cname[1] = octdigit[(ch & 0300) >> 6]; 301 1.2 christos sp->cname[2] = octdigit[(ch & 070) >> 3]; 302 1.2 christos sp->cname[3] = octdigit[ ch & 07 ]; 303 1.1 christos } else { 304 1.1 christos sp->cname[0] = '\\'; 305 1.1 christos sp->cname[1] = 'x'; 306 1.2 christos sp->cname[2] = hexdigit[(ch & 0xf0) >> 4]; 307 1.2 christos sp->cname[3] = hexdigit[ ch & 0x0f ]; 308 1.1 christos } 309 1.2 christos len = 4; 310 1.1 christos done: sp->cname[sp->clen = len] = '\0'; 311 1.1 christos return (sp->cname); 312 1.1 christos } 313 1.1 christos 314 1.1 christos /* 315 1.1 christos * v_key_val -- 316 1.1 christos * Fill in the value for a key. This routine is the backup 317 1.1 christos * for the KEY_VAL() macro. 318 1.1 christos * 319 1.2 christos * PUBLIC: e_key_t v_key_val __P((SCR *, ARG_CHAR_T)); 320 1.1 christos */ 321 1.2 christos e_key_t 322 1.1 christos v_key_val(SCR *sp, ARG_CHAR_T ch) 323 1.1 christos { 324 1.1 christos KEYLIST k, *kp; 325 1.1 christos 326 1.1 christos k.ch = ch; 327 1.1 christos kp = bsearch(&k, keylist, nkeylist, sizeof(keylist[0]), v_key_cmp); 328 1.1 christos return (kp == NULL ? K_NOTUSED : kp->value); 329 1.1 christos } 330 1.1 christos 331 1.1 christos /* 332 1.1 christos * v_event_push -- 333 1.1 christos * Push events/keys onto the front of the buffer. 334 1.1 christos * 335 1.1 christos * There is a single input buffer in ex/vi. Characters are put onto the 336 1.1 christos * end of the buffer by the terminal input routines, and pushed onto the 337 1.1 christos * front of the buffer by various other functions in ex/vi. Each key has 338 1.1 christos * an associated flag value, which indicates if it has already been quoted, 339 1.1 christos * and if it is the result of a mapping or an abbreviation. 340 1.1 christos * 341 1.2 christos * PUBLIC: int v_event_push __P((SCR *, EVENT *, const CHAR_T *, size_t, u_int)); 342 1.1 christos */ 343 1.1 christos int 344 1.2 christos v_event_push(SCR *sp, EVENT *p_evp, const CHAR_T *p_s, size_t nitems, u_int flags) 345 1.1 christos 346 1.1 christos /* Push event. */ 347 1.1 christos /* Push characters. */ 348 1.1 christos /* Number of items to push. */ 349 1.1 christos /* CH_* flags. */ 350 1.1 christos { 351 1.1 christos EVENT *evp; 352 1.1 christos WIN *wp; 353 1.1 christos size_t total; 354 1.1 christos 355 1.1 christos /* If we have room, stuff the items into the buffer. */ 356 1.1 christos wp = sp->wp; 357 1.1 christos if (nitems <= wp->i_next || 358 1.1 christos (wp->i_event != NULL && wp->i_cnt == 0 && nitems <= wp->i_nelem)) { 359 1.1 christos if (wp->i_cnt != 0) 360 1.1 christos wp->i_next -= nitems; 361 1.1 christos goto copy; 362 1.1 christos } 363 1.1 christos 364 1.1 christos /* 365 1.1 christos * If there are currently items in the queue, shift them up, 366 1.1 christos * leaving some extra room. Get enough space plus a little 367 1.1 christos * extra. 368 1.1 christos */ 369 1.1 christos #define TERM_PUSH_SHIFT 30 370 1.1 christos total = wp->i_cnt + wp->i_next + nitems + TERM_PUSH_SHIFT; 371 1.1 christos if (total >= wp->i_nelem && v_event_grow(sp, MAX(total, 64))) 372 1.1 christos return (1); 373 1.1 christos if (wp->i_cnt) 374 1.1 christos MEMMOVE(wp->i_event + TERM_PUSH_SHIFT + nitems, 375 1.1 christos wp->i_event + wp->i_next, wp->i_cnt); 376 1.1 christos wp->i_next = TERM_PUSH_SHIFT; 377 1.1 christos 378 1.1 christos /* Put the new items into the queue. */ 379 1.1 christos copy: wp->i_cnt += nitems; 380 1.1 christos for (evp = wp->i_event + wp->i_next; nitems--; ++evp) { 381 1.1 christos if (p_evp != NULL) 382 1.1 christos *evp = *p_evp++; 383 1.1 christos else { 384 1.1 christos evp->e_event = E_CHARACTER; 385 1.1 christos evp->e_c = *p_s++; 386 1.1 christos evp->e_value = KEY_VAL(sp, evp->e_c); 387 1.1 christos FL_INIT(evp->e_flags, flags); 388 1.1 christos } 389 1.1 christos } 390 1.1 christos return (0); 391 1.1 christos } 392 1.1 christos 393 1.1 christos /* 394 1.1 christos * v_event_append -- 395 1.1 christos * Append events onto the tail of the buffer. 396 1.1 christos */ 397 1.1 christos static int 398 1.1 christos v_event_append(SCR *sp, EVENT *argp) 399 1.1 christos { 400 1.1 christos CHAR_T *s; /* Characters. */ 401 1.1 christos EVENT *evp; 402 1.1 christos WIN *wp; 403 1.1 christos size_t nevents; /* Number of events. */ 404 1.1 christos 405 1.1 christos /* Grow the buffer as necessary. */ 406 1.1 christos nevents = argp->e_event == E_STRING ? argp->e_len : 1; 407 1.1 christos wp = sp->wp; 408 1.1 christos if (wp->i_event == NULL || 409 1.1 christos nevents > wp->i_nelem - (wp->i_next + wp->i_cnt)) 410 1.1 christos v_event_grow(sp, MAX(nevents, 64)); 411 1.1 christos evp = wp->i_event + wp->i_next + wp->i_cnt; 412 1.1 christos wp->i_cnt += nevents; 413 1.1 christos 414 1.1 christos /* Transform strings of characters into single events. */ 415 1.1 christos if (argp->e_event == E_STRING) 416 1.1 christos for (s = argp->e_csp; nevents--; ++evp) { 417 1.1 christos evp->e_event = E_CHARACTER; 418 1.1 christos evp->e_c = *s++; 419 1.1 christos evp->e_value = KEY_VAL(sp, evp->e_c); 420 1.1 christos evp->e_flags = 0; 421 1.1 christos } 422 1.1 christos else 423 1.1 christos *evp = *argp; 424 1.1 christos return (0); 425 1.1 christos } 426 1.1 christos 427 1.1 christos /* Remove events from the queue. */ 428 1.1 christos #define QREM(len) { \ 429 1.1 christos if ((wp->i_cnt -= len) == 0) \ 430 1.1 christos wp->i_next = 0; \ 431 1.1 christos else \ 432 1.1 christos wp->i_next += len; \ 433 1.1 christos } 434 1.1 christos 435 1.1 christos /* 436 1.1 christos * v_event_get -- 437 1.1 christos * Return the next event. 438 1.1 christos * 439 1.1 christos * !!! 440 1.1 christos * The flag EC_NODIGIT probably needs some explanation. First, the idea of 441 1.1 christos * mapping keys is that one or more keystrokes act like a function key. 442 1.1 christos * What's going on is that vi is reading a number, and the character following 443 1.1 christos * the number may or may not be mapped (EC_MAPCOMMAND). For example, if the 444 1.1 christos * user is entering the z command, a valid command is "z40+", and we don't want 445 1.1 christos * to map the '+', i.e. if '+' is mapped to "xxx", we don't want to change it 446 1.1 christos * into "z40xxx". However, if the user enters "35x", we want to put all of the 447 1.1 christos * characters through the mapping code. 448 1.1 christos * 449 1.1 christos * Historical practice is a bit muddled here. (Surprise!) It always permitted 450 1.1 christos * mapping digits as long as they weren't the first character of the map, e.g. 451 1.1 christos * ":map ^A1 xxx" was okay. It also permitted the mapping of the digits 1-9 452 1.1 christos * (the digit 0 was a special case as it doesn't indicate the start of a count) 453 1.1 christos * as the first character of the map, but then ignored those mappings. While 454 1.1 christos * it's probably stupid to map digits, vi isn't your mother. 455 1.1 christos * 456 1.1 christos * The way this works is that the EC_MAPNODIGIT causes term_key to return the 457 1.1 christos * end-of-digit without "looking" at the next character, i.e. leaving it as the 458 1.1 christos * user entered it. Presumably, the next term_key call will tell us how the 459 1.1 christos * user wants it handled. 460 1.1 christos * 461 1.1 christos * There is one more complication. Users might map keys to digits, and, as 462 1.1 christos * it's described above, the commands: 463 1.1 christos * 464 1.1 christos * :map g 1G 465 1.1 christos * d2g 466 1.1 christos * 467 1.1 christos * would return the keys "d2<end-of-digits>1G", when the user probably wanted 468 1.1 christos * "d21<end-of-digits>G". So, if a map starts off with a digit we continue as 469 1.1 christos * before, otherwise, we pretend we haven't mapped the character, and return 470 1.1 christos * <end-of-digits>. 471 1.1 christos * 472 1.1 christos * Now that that's out of the way, let's talk about Energizer Bunny macros. 473 1.1 christos * It's easy to create macros that expand to a loop, e.g. map x 3x. It's 474 1.1 christos * fairly easy to detect this example, because it's all internal to term_key. 475 1.1 christos * If we're expanding a macro and it gets big enough, at some point we can 476 1.1 christos * assume it's looping and kill it. The examples that are tough are the ones 477 1.1 christos * where the parser is involved, e.g. map x "ayyx"byy. We do an expansion 478 1.1 christos * on 'x', and get "ayyx"byy. We then return the first 4 characters, and then 479 1.1 christos * find the looping macro again. There is no way that we can detect this 480 1.1 christos * without doing a full parse of the command, because the character that might 481 1.1 christos * cause the loop (in this case 'x') may be a literal character, e.g. the map 482 1.1 christos * map x "ayy"xyy"byy is perfectly legal and won't cause a loop. 483 1.1 christos * 484 1.1 christos * Historic vi tried to detect looping macros by disallowing obvious cases in 485 1.1 christos * the map command, maps that that ended with the same letter as they started 486 1.1 christos * (which wrongly disallowed "map x 'x"), and detecting macros that expanded 487 1.1 christos * too many times before keys were returned to the command parser. It didn't 488 1.1 christos * get many (most?) of the tricky cases right, however, and it was certainly 489 1.1 christos * possible to create macros that ran forever. And, even if it did figure out 490 1.1 christos * what was going on, the user was usually tossed into ex mode. Finally, any 491 1.1 christos * changes made before vi realized that the macro was recursing were left in 492 1.1 christos * place. We recover gracefully, but the only recourse the user has in an 493 1.1 christos * infinite macro loop is to interrupt. 494 1.1 christos * 495 1.1 christos * !!! 496 1.1 christos * It is historic practice that mapping characters to themselves as the first 497 1.1 christos * part of the mapped string was legal, and did not cause infinite loops, i.e. 498 1.1 christos * ":map! { {^M^T" and ":map n nz." were known to work. The initial, matching 499 1.1 christos * characters were returned instead of being remapped. 500 1.1 christos * 501 1.1 christos * !!! 502 1.1 christos * It is also historic practice that the macro "map ] ]]^" caused a single ] 503 1.1 christos * keypress to behave as the command ]] (the ^ got the map past the vi check 504 1.1 christos * for "tail recursion"). Conversely, the mapping "map n nn^" went recursive. 505 1.1 christos * What happened was that, in the historic vi, maps were expanded as the keys 506 1.1 christos * were retrieved, but not all at once and not centrally. So, the keypress ] 507 1.1 christos * pushed ]]^ on the stack, and then the first ] from the stack was passed to 508 1.1 christos * the ]] command code. The ]] command then retrieved a key without entering 509 1.1 christos * the mapping code. This could bite us anytime a user has a map that depends 510 1.1 christos * on secondary keys NOT being mapped. I can't see any possible way to make 511 1.1 christos * this work in here without the complete abandonment of Rationality Itself. 512 1.1 christos * 513 1.1 christos * XXX 514 1.1 christos * The final issue is recovery. It would be possible to undo all of the work 515 1.1 christos * that was done by the macro if we entered a record into the log so that we 516 1.1 christos * knew when the macro started, and, in fact, this might be worth doing at some 517 1.1 christos * point. Given that this might make the log grow unacceptably (consider that 518 1.1 christos * cursor keys are done with maps), for now we leave any changes made in place. 519 1.1 christos * 520 1.1 christos * PUBLIC: int v_event_get __P((SCR *, EVENT *, int, u_int32_t)); 521 1.1 christos */ 522 1.1 christos int 523 1.1 christos v_event_get(SCR *sp, EVENT *argp, int timeout, u_int32_t flags) 524 1.1 christos { 525 1.1 christos EVENT *evp, ev; 526 1.1 christos GS *gp; 527 1.1 christos SEQ *qp; 528 1.1 christos int init_nomap, ispartial, istimeout, remap_cnt; 529 1.1 christos WIN *wp; 530 1.1 christos 531 1.1 christos gp = sp->gp; 532 1.1 christos wp = sp->wp; 533 1.1 christos 534 1.1 christos /* If simply checking for interrupts, argp may be NULL. */ 535 1.1 christos if (argp == NULL) 536 1.1 christos argp = &ev; 537 1.1 christos 538 1.1 christos retry: istimeout = remap_cnt = 0; 539 1.1 christos 540 1.1 christos /* 541 1.1 christos * If the queue isn't empty and we're timing out for characters, 542 1.1 christos * return immediately. 543 1.1 christos */ 544 1.1 christos if (wp->i_cnt != 0 && LF_ISSET(EC_TIMEOUT)) 545 1.1 christos return (0); 546 1.1 christos 547 1.1 christos /* 548 1.1 christos * If the queue is empty, we're checking for interrupts, or we're 549 1.1 christos * timing out for characters, get more events. 550 1.1 christos */ 551 1.1 christos if (wp->i_cnt == 0 || LF_ISSET(EC_INTERRUPT | EC_TIMEOUT)) { 552 1.1 christos /* 553 1.1 christos * If we're reading new characters, check any scripting 554 1.1 christos * windows for input. 555 1.1 christos */ 556 1.1 christos if (F_ISSET(gp, G_SCRWIN) && sscr_input(sp)) 557 1.1 christos return (1); 558 1.1 christos loop: if (gp->scr_event(sp, argp, 559 1.1 christos LF_ISSET(EC_INTERRUPT | EC_QUOTED | EC_RAW), timeout)) 560 1.1 christos return (1); 561 1.1 christos switch (argp->e_event) { 562 1.1 christos case E_ERR: 563 1.1 christos case E_SIGHUP: 564 1.1 christos case E_SIGTERM: 565 1.1 christos /* 566 1.1 christos * Fatal conditions cause the file to be synced to 567 1.1 christos * disk immediately. 568 1.1 christos */ 569 1.1 christos v_sync(sp, RCV_ENDSESSION | RCV_PRESERVE | 570 1.1 christos (argp->e_event == E_SIGTERM ? 0: RCV_EMAIL)); 571 1.1 christos return (1); 572 1.1 christos case E_TIMEOUT: 573 1.1 christos istimeout = 1; 574 1.1 christos break; 575 1.1 christos case E_INTERRUPT: 576 1.1 christos /* Set the global interrupt flag. */ 577 1.1 christos F_SET(sp->gp, G_INTERRUPTED); 578 1.1 christos 579 1.1 christos /* 580 1.1 christos * If the caller was interested in interrupts, return 581 1.1 christos * immediately. 582 1.1 christos */ 583 1.1 christos if (LF_ISSET(EC_INTERRUPT)) 584 1.1 christos return (0); 585 1.1 christos goto append; 586 1.1 christos default: 587 1.1 christos append: if (v_event_append(sp, argp)) 588 1.1 christos return (1); 589 1.1 christos break; 590 1.1 christos } 591 1.1 christos } 592 1.1 christos 593 1.1 christos /* 594 1.1 christos * If the caller was only interested in interrupts or timeouts, return 595 1.1 christos * immediately. (We may have gotten characters, and that's okay, they 596 1.1 christos * were queued up for later use.) 597 1.1 christos */ 598 1.1 christos if (LF_ISSET(EC_INTERRUPT | EC_TIMEOUT)) 599 1.1 christos return (0); 600 1.1 christos 601 1.1 christos newmap: evp = &wp->i_event[wp->i_next]; 602 1.1 christos 603 1.1 christos /* 604 1.1 christos * If the next event in the queue isn't a character event, return 605 1.1 christos * it, we're done. 606 1.1 christos */ 607 1.1 christos if (evp->e_event != E_CHARACTER) { 608 1.1 christos *argp = *evp; 609 1.1 christos QREM(1); 610 1.1 christos return (0); 611 1.1 christos } 612 1.1 christos 613 1.1 christos /* 614 1.1 christos * If the key isn't mappable because: 615 1.1 christos * 616 1.1 christos * + ... the timeout has expired 617 1.1 christos * + ... it's not a mappable key 618 1.1 christos * + ... neither the command or input map flags are set 619 1.1 christos * + ... there are no maps that can apply to it 620 1.1 christos * 621 1.1 christos * return it forthwith. 622 1.1 christos */ 623 1.1 christos if (istimeout || FL_ISSET(evp->e_flags, CH_NOMAP) || 624 1.1 christos !LF_ISSET(EC_MAPCOMMAND | EC_MAPINPUT) || 625 1.2 christos ((evp->e_c & ~MAX_BIT_SEQ) == 0 && 626 1.2 christos !bit_test(gp->seqb, evp->e_c))) 627 1.1 christos goto nomap; 628 1.1 christos 629 1.1 christos /* Search the map. */ 630 1.1 christos qp = seq_find(sp, NULL, evp, NULL, wp->i_cnt, 631 1.1 christos LF_ISSET(EC_MAPCOMMAND) ? SEQ_COMMAND : SEQ_INPUT, &ispartial); 632 1.1 christos 633 1.1 christos /* 634 1.1 christos * If get a partial match, get more characters and retry the map. 635 1.1 christos * If time out without further characters, return the characters 636 1.1 christos * unmapped. 637 1.1 christos * 638 1.1 christos * !!! 639 1.1 christos * <escape> characters are a problem. Cursor keys start with <escape> 640 1.1 christos * characters, so there's almost always a map in place that begins with 641 1.1 christos * an <escape> character. If we timeout <escape> keys in the same way 642 1.1 christos * that we timeout other keys, the user will get a noticeable pause as 643 1.1 christos * they enter <escape> to terminate input mode. If key timeout is set 644 1.1 christos * for a slow link, users will get an even longer pause. Nvi used to 645 1.1 christos * simply timeout <escape> characters at 1/10th of a second, but this 646 1.1 christos * loses over PPP links where the latency is greater than 100Ms. 647 1.1 christos */ 648 1.1 christos if (ispartial) { 649 1.1 christos if (O_ISSET(sp, O_TIMEOUT)) 650 1.1 christos timeout = (evp->e_value == K_ESCAPE ? 651 1.1 christos O_VAL(sp, O_ESCAPETIME) : 652 1.1 christos O_VAL(sp, O_KEYTIME)) * 100; 653 1.1 christos else 654 1.1 christos timeout = 0; 655 1.1 christos goto loop; 656 1.1 christos } 657 1.1 christos 658 1.1 christos /* If no map, return the character. */ 659 1.1 christos if (qp == NULL) { 660 1.1 christos nomap: if (!ISDIGIT(evp->e_c) && LF_ISSET(EC_MAPNODIGIT)) 661 1.1 christos goto not_digit; 662 1.1 christos *argp = *evp; 663 1.1 christos QREM(1); 664 1.1 christos return (0); 665 1.1 christos } 666 1.1 christos 667 1.1 christos /* 668 1.1 christos * If looking for the end of a digit string, and the first character 669 1.1 christos * of the map is it, pretend we haven't seen the character. 670 1.1 christos */ 671 1.1 christos if (LF_ISSET(EC_MAPNODIGIT) && 672 1.1 christos qp->output != NULL && !ISDIGIT(qp->output[0])) { 673 1.1 christos not_digit: argp->e_c = CH_NOT_DIGIT; 674 1.1 christos argp->e_value = K_NOTUSED; 675 1.1 christos argp->e_event = E_CHARACTER; 676 1.1 christos FL_INIT(argp->e_flags, 0); 677 1.1 christos return (0); 678 1.1 christos } 679 1.1 christos 680 1.1 christos /* Find out if the initial segments are identical. */ 681 1.1 christos init_nomap = !e_memcmp(qp->output, &wp->i_event[wp->i_next], qp->ilen); 682 1.1 christos 683 1.1 christos /* Delete the mapped characters from the queue. */ 684 1.1 christos QREM(qp->ilen); 685 1.1 christos 686 1.1 christos /* If keys mapped to nothing, go get more. */ 687 1.1 christos if (qp->output == NULL) 688 1.1 christos goto retry; 689 1.1 christos 690 1.1 christos /* If remapping characters... */ 691 1.1 christos if (O_ISSET(sp, O_REMAP)) { 692 1.1 christos /* 693 1.1 christos * Periodically check for interrupts. Always check the first 694 1.1 christos * time through, because it's possible to set up a map that 695 1.1 christos * will return a character every time, but will expand to more, 696 1.1 christos * e.g. "map! a aaaa" will always return a 'a', but we'll never 697 1.1 christos * get anywhere useful. 698 1.1 christos */ 699 1.1 christos if ((++remap_cnt == 1 || remap_cnt % 10 == 0) && 700 1.1 christos (gp->scr_event(sp, &ev, 701 1.1 christos EC_INTERRUPT, 0) || ev.e_event == E_INTERRUPT)) { 702 1.1 christos F_SET(sp->gp, G_INTERRUPTED); 703 1.1 christos argp->e_event = E_INTERRUPT; 704 1.1 christos return (0); 705 1.1 christos } 706 1.1 christos 707 1.1 christos /* 708 1.1 christos * If an initial part of the characters mapped, they are not 709 1.1 christos * further remapped -- return the first one. Push the rest 710 1.1 christos * of the characters, or all of the characters if no initial 711 1.1 christos * part mapped, back on the queue. 712 1.1 christos */ 713 1.1 christos if (init_nomap) { 714 1.1 christos if (v_event_push(sp, NULL, qp->output + qp->ilen, 715 1.1 christos qp->olen - qp->ilen, CH_MAPPED)) 716 1.1 christos return (1); 717 1.1 christos if (v_event_push(sp, NULL, 718 1.1 christos qp->output, qp->ilen, CH_NOMAP | CH_MAPPED)) 719 1.1 christos return (1); 720 1.1 christos evp = &wp->i_event[wp->i_next]; 721 1.1 christos goto nomap; 722 1.1 christos } 723 1.1 christos if (v_event_push(sp, NULL, qp->output, qp->olen, CH_MAPPED)) 724 1.1 christos return (1); 725 1.1 christos goto newmap; 726 1.1 christos } 727 1.1 christos 728 1.1 christos /* Else, push the characters on the queue and return one. */ 729 1.1 christos if (v_event_push(sp, NULL, qp->output, qp->olen, CH_MAPPED | CH_NOMAP)) 730 1.1 christos return (1); 731 1.1 christos 732 1.1 christos goto nomap; 733 1.1 christos } 734 1.1 christos 735 1.1 christos /* 736 1.1 christos * v_sync -- 737 1.1 christos * Walk the screen lists, sync'ing files to their backup copies. 738 1.1 christos */ 739 1.1 christos static void 740 1.1 christos v_sync(SCR *sp, int flags) 741 1.1 christos { 742 1.1 christos GS *gp; 743 1.1 christos WIN *wp; 744 1.1 christos 745 1.1 christos gp = sp->gp; 746 1.3 christos TAILQ_FOREACH(wp, &gp->dq, q) 747 1.3 christos TAILQ_FOREACH(sp, &wp->scrq, q) 748 1.3 christos rcv_sync(sp, flags); 749 1.3 christos TAILQ_FOREACH(sp, &gp->hq, q) 750 1.1 christos rcv_sync(sp, flags); 751 1.1 christos } 752 1.1 christos 753 1.1 christos /* 754 1.1 christos * v_event_err -- 755 1.1 christos * Unexpected event. 756 1.1 christos * 757 1.1 christos * PUBLIC: void v_event_err __P((SCR *, EVENT *)); 758 1.1 christos */ 759 1.1 christos void 760 1.1 christos v_event_err(SCR *sp, EVENT *evp) 761 1.1 christos { 762 1.1 christos switch (evp->e_event) { 763 1.1 christos case E_CHARACTER: 764 1.1 christos msgq(sp, M_ERR, "276|Unexpected character event"); 765 1.1 christos break; 766 1.1 christos case E_EOF: 767 1.1 christos msgq(sp, M_ERR, "277|Unexpected end-of-file event"); 768 1.1 christos break; 769 1.1 christos case E_INTERRUPT: 770 1.1 christos msgq(sp, M_ERR, "279|Unexpected interrupt event"); 771 1.1 christos break; 772 1.1 christos case E_IPCOMMAND: 773 1.1 christos msgq(sp, M_ERR, "318|Unexpected command or input"); 774 1.1 christos break; 775 1.1 christos case E_REPAINT: 776 1.1 christos msgq(sp, M_ERR, "281|Unexpected repaint event"); 777 1.1 christos break; 778 1.1 christos case E_STRING: 779 1.1 christos msgq(sp, M_ERR, "285|Unexpected string event"); 780 1.1 christos break; 781 1.1 christos case E_TIMEOUT: 782 1.1 christos msgq(sp, M_ERR, "286|Unexpected timeout event"); 783 1.1 christos break; 784 1.1 christos case E_WRESIZE: 785 1.1 christos msgq(sp, M_ERR, "316|Unexpected resize event"); 786 1.1 christos break; 787 1.1 christos 788 1.1 christos /* 789 1.1 christos * Theoretically, none of these can occur, as they're handled at the 790 1.1 christos * top editor level. 791 1.1 christos */ 792 1.1 christos case E_ERR: 793 1.1 christos case E_SIGHUP: 794 1.1 christos case E_SIGTERM: 795 1.1 christos default: 796 1.1 christos abort(); 797 1.1 christos } 798 1.1 christos } 799 1.1 christos 800 1.1 christos /* 801 1.1 christos * v_event_flush -- 802 1.1 christos * Flush any flagged keys, returning if any keys were flushed. 803 1.1 christos * 804 1.1 christos * PUBLIC: int v_event_flush __P((SCR *, u_int)); 805 1.1 christos */ 806 1.1 christos int 807 1.1 christos v_event_flush(SCR *sp, u_int flags) 808 1.1 christos { 809 1.1 christos WIN *wp; 810 1.1 christos int rval; 811 1.1 christos 812 1.1 christos for (rval = 0, wp = sp->wp; wp->i_cnt != 0 && 813 1.1 christos FL_ISSET(wp->i_event[wp->i_next].e_flags, flags); rval = 1) 814 1.1 christos QREM(1); 815 1.1 christos return (rval); 816 1.1 christos } 817 1.1 christos 818 1.1 christos /* 819 1.1 christos * v_event_grow -- 820 1.1 christos * Grow the terminal queue. 821 1.1 christos */ 822 1.1 christos static int 823 1.1 christos v_event_grow(SCR *sp, int add) 824 1.1 christos { 825 1.1 christos WIN *wp; 826 1.1 christos size_t new_nelem, olen; 827 1.1 christos 828 1.1 christos wp = sp->wp; 829 1.1 christos new_nelem = wp->i_nelem + add; 830 1.1 christos olen = wp->i_nelem * sizeof(wp->i_event[0]); 831 1.1 christos BINC_RET(sp, EVENT, wp->i_event, olen, new_nelem * sizeof(EVENT)); 832 1.1 christos wp->i_nelem = olen / sizeof(wp->i_event[0]); 833 1.1 christos return (0); 834 1.1 christos } 835 1.1 christos 836 1.1 christos /* 837 1.1 christos * v_key_cmp -- 838 1.1 christos * Compare two keys for sorting. 839 1.1 christos */ 840 1.1 christos static int 841 1.1 christos v_key_cmp(const void *ap, const void *bp) 842 1.1 christos { 843 1.2 christos return (((const KEYLIST *)ap)->ch - ((const KEYLIST *)bp)->ch); 844 1.1 christos } 845