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