Home | History | Annotate | Line # | Download | only in libcurses
getch.c revision 1.52
      1  1.52       jdc /*	$NetBSD: getch.c,v 1.52 2009/02/09 12:45:59 jdc Exp $	*/
      2   1.8     mikel 
      3   1.1       cgd /*
      4   1.7       cgd  * Copyright (c) 1981, 1993, 1994
      5   1.5       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.42       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.8     mikel #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.8     mikel #if 0
     35   1.7       cgd static char sccsid[] = "@(#)getch.c	8.2 (Berkeley) 5/4/94";
     36   1.8     mikel #else
     37  1.52       jdc __RCSID("$NetBSD: getch.c,v 1.52 2009/02/09 12:45:59 jdc Exp $");
     38   1.8     mikel #endif
     39  1.10       mrg #endif					/* not lint */
     40   1.1       cgd 
     41  1.10       mrg #include <string.h>
     42  1.10       mrg #include <stdlib.h>
     43  1.10       mrg #include <unistd.h>
     44  1.10       mrg #include <stdio.h>
     45   1.7       cgd #include "curses.h"
     46  1.16     blymn #include "curses_private.h"
     47  1.48     blymn #include "keymap.h"
     48   1.1       cgd 
     49  1.48     blymn short	state;		/* state of the inkey function */
     50  1.10       mrg 
     51  1.13    simonb static const struct tcdata tc[] = {
     52  1.25       jdc 	{"!1", KEY_SSAVE},
     53  1.25       jdc 	{"!2", KEY_SSUSPEND},
     54  1.25       jdc 	{"!3", KEY_SUNDO},
     55  1.25       jdc 	{"#1", KEY_SHELP},
     56  1.25       jdc 	{"#2", KEY_SHOME},
     57  1.25       jdc 	{"#3", KEY_SIC},
     58  1.25       jdc 	{"#4", KEY_SLEFT},
     59  1.25       jdc 	{"%0", KEY_REDO},
     60  1.25       jdc 	{"%1", KEY_HELP},
     61  1.25       jdc 	{"%2", KEY_MARK},
     62  1.25       jdc 	{"%3", KEY_MESSAGE},
     63  1.25       jdc 	{"%4", KEY_MOVE},
     64  1.25       jdc 	{"%5", KEY_NEXT},
     65  1.25       jdc 	{"%6", KEY_OPEN},
     66  1.25       jdc 	{"%7", KEY_OPTIONS},
     67  1.25       jdc 	{"%8", KEY_PREVIOUS},
     68  1.25       jdc 	{"%9", KEY_PRINT},
     69  1.25       jdc 	{"%a", KEY_SMESSAGE},
     70  1.25       jdc 	{"%b", KEY_SMOVE},
     71  1.25       jdc 	{"%c", KEY_SNEXT},
     72  1.25       jdc 	{"%d", KEY_SOPTIONS},
     73  1.25       jdc 	{"%e", KEY_SPREVIOUS},
     74  1.25       jdc 	{"%f", KEY_SPRINT},
     75  1.25       jdc 	{"%g", KEY_SREDO},
     76  1.25       jdc 	{"%h", KEY_SREPLACE},
     77  1.25       jdc 	{"%i", KEY_SRIGHT},
     78  1.25       jdc 	{"%j", KEY_SRSUME},
     79  1.25       jdc 	{"&0", KEY_SCANCEL},
     80  1.25       jdc 	{"&1", KEY_REFERENCE},
     81  1.25       jdc 	{"&2", KEY_REFRESH},
     82  1.25       jdc 	{"&3", KEY_REPLACE},
     83  1.25       jdc 	{"&4", KEY_RESTART},
     84  1.25       jdc 	{"&5", KEY_RESUME},
     85  1.25       jdc 	{"&6", KEY_SAVE},
     86  1.25       jdc 	{"&7", KEY_SUSPEND},
     87  1.25       jdc 	{"&8", KEY_UNDO},
     88  1.25       jdc 	{"&9", KEY_SBEG},
     89  1.25       jdc 	{"*0", KEY_SFIND},
     90  1.25       jdc 	{"*1", KEY_SCOMMAND},
     91  1.25       jdc 	{"*2", KEY_SCOPY},
     92  1.25       jdc 	{"*3", KEY_SCREATE},
     93  1.25       jdc 	{"*4", KEY_SDC},
     94  1.25       jdc 	{"*5", KEY_SDL},
     95  1.25       jdc 	{"*6", KEY_SELECT},
     96  1.25       jdc 	{"*7", KEY_SEND},
     97  1.25       jdc 	{"*8", KEY_SEOL},
     98  1.25       jdc 	{"*9", KEY_SEXIT},
     99  1.25       jdc 	{"@0", KEY_FIND},
    100  1.25       jdc 	{"@1", KEY_BEG},
    101  1.25       jdc 	{"@2", KEY_CANCEL},
    102  1.25       jdc 	{"@3", KEY_CLOSE},
    103  1.25       jdc 	{"@4", KEY_COMMAND},
    104  1.25       jdc 	{"@5", KEY_COPY},
    105  1.25       jdc 	{"@6", KEY_CREATE},
    106  1.25       jdc 	{"@7", KEY_END},
    107  1.25       jdc 	{"@8", KEY_ENTER},
    108  1.25       jdc 	{"@9", KEY_EXIT},
    109  1.25       jdc 	{"F1", KEY_F(11)},
    110  1.25       jdc 	{"F2", KEY_F(12)},
    111  1.25       jdc 	{"F3", KEY_F(13)},
    112  1.25       jdc 	{"F4", KEY_F(14)},
    113  1.25       jdc 	{"F5", KEY_F(15)},
    114  1.25       jdc 	{"F6", KEY_F(16)},
    115  1.25       jdc 	{"F7", KEY_F(17)},
    116  1.25       jdc 	{"F8", KEY_F(18)},
    117  1.25       jdc 	{"F9", KEY_F(19)},
    118  1.25       jdc 	{"FA", KEY_F(20)},
    119  1.25       jdc 	{"FB", KEY_F(21)},
    120  1.25       jdc 	{"FC", KEY_F(22)},
    121  1.25       jdc 	{"FD", KEY_F(23)},
    122  1.25       jdc 	{"FE", KEY_F(24)},
    123  1.25       jdc 	{"FF", KEY_F(25)},
    124  1.25       jdc 	{"FG", KEY_F(26)},
    125  1.25       jdc 	{"FH", KEY_F(27)},
    126  1.25       jdc 	{"FI", KEY_F(28)},
    127  1.25       jdc 	{"FJ", KEY_F(29)},
    128  1.25       jdc 	{"FK", KEY_F(30)},
    129  1.25       jdc 	{"FL", KEY_F(31)},
    130  1.25       jdc 	{"FM", KEY_F(32)},
    131  1.25       jdc 	{"FN", KEY_F(33)},
    132  1.25       jdc 	{"FO", KEY_F(34)},
    133  1.25       jdc 	{"FP", KEY_F(35)},
    134  1.25       jdc 	{"FQ", KEY_F(36)},
    135  1.25       jdc 	{"FR", KEY_F(37)},
    136  1.25       jdc 	{"FS", KEY_F(38)},
    137  1.25       jdc 	{"FT", KEY_F(39)},
    138  1.25       jdc 	{"FU", KEY_F(40)},
    139  1.25       jdc 	{"FV", KEY_F(41)},
    140  1.25       jdc 	{"FW", KEY_F(42)},
    141  1.25       jdc 	{"FX", KEY_F(43)},
    142  1.25       jdc 	{"FY", KEY_F(44)},
    143  1.25       jdc 	{"FZ", KEY_F(45)},
    144  1.25       jdc 	{"Fa", KEY_F(46)},
    145  1.25       jdc 	{"Fb", KEY_F(47)},
    146  1.25       jdc 	{"Fc", KEY_F(48)},
    147  1.25       jdc 	{"Fd", KEY_F(49)},
    148  1.25       jdc 	{"Fe", KEY_F(50)},
    149  1.25       jdc 	{"Ff", KEY_F(51)},
    150  1.25       jdc 	{"Fg", KEY_F(52)},
    151  1.25       jdc 	{"Fh", KEY_F(53)},
    152  1.25       jdc 	{"Fi", KEY_F(54)},
    153  1.25       jdc 	{"Fj", KEY_F(55)},
    154  1.25       jdc 	{"Fk", KEY_F(56)},
    155  1.25       jdc 	{"Fl", KEY_F(57)},
    156  1.25       jdc 	{"Fm", KEY_F(58)},
    157  1.25       jdc 	{"Fn", KEY_F(59)},
    158  1.25       jdc 	{"Fo", KEY_F(60)},
    159  1.25       jdc 	{"Fp", KEY_F(61)},
    160  1.25       jdc 	{"Fq", KEY_F(62)},
    161  1.25       jdc 	{"Fr", KEY_F(63)},
    162  1.10       mrg 	{"K1", KEY_A1},
    163  1.10       mrg 	{"K2", KEY_B2},
    164  1.10       mrg 	{"K3", KEY_A3},
    165  1.10       mrg 	{"K4", KEY_C1},
    166  1.10       mrg 	{"K5", KEY_C3},
    167  1.25       jdc 	{"Km", KEY_MOUSE},
    168  1.10       mrg 	{"k0", KEY_F0},
    169  1.10       mrg 	{"k1", KEY_F(1)},
    170  1.10       mrg 	{"k2", KEY_F(2)},
    171  1.10       mrg 	{"k3", KEY_F(3)},
    172  1.10       mrg 	{"k4", KEY_F(4)},
    173  1.10       mrg 	{"k5", KEY_F(5)},
    174  1.10       mrg 	{"k6", KEY_F(6)},
    175  1.10       mrg 	{"k7", KEY_F(7)},
    176  1.10       mrg 	{"k8", KEY_F(8)},
    177  1.10       mrg 	{"k9", KEY_F(9)},
    178  1.25       jdc 	{"k;", KEY_F(10)},
    179  1.10       mrg 	{"kA", KEY_IL},
    180  1.10       mrg 	{"ka", KEY_CATAB},
    181  1.25       jdc 	{"kB", KEY_BTAB},
    182  1.10       mrg 	{"kb", KEY_BACKSPACE},
    183  1.10       mrg 	{"kC", KEY_CLEAR},
    184  1.10       mrg 	{"kD", KEY_DC},
    185  1.10       mrg 	{"kd", KEY_DOWN},
    186  1.10       mrg 	{"kE", KEY_EOL},
    187  1.10       mrg 	{"kF", KEY_SF},
    188  1.10       mrg 	{"kH", KEY_LL},
    189  1.10       mrg 	{"kh", KEY_HOME},
    190  1.10       mrg 	{"kI", KEY_IC},
    191  1.10       mrg 	{"kL", KEY_DL},
    192  1.10       mrg 	{"kl", KEY_LEFT},
    193  1.25       jdc 	{"kM", KEY_EIC},
    194  1.10       mrg 	{"kN", KEY_NPAGE},
    195  1.10       mrg 	{"kP", KEY_PPAGE},
    196  1.10       mrg 	{"kR", KEY_SR},
    197  1.10       mrg 	{"kr", KEY_RIGHT},
    198  1.10       mrg 	{"kS", KEY_EOS},
    199  1.10       mrg 	{"kT", KEY_STAB},
    200  1.10       mrg 	{"kt", KEY_CTAB},
    201  1.10       mrg 	{"ku", KEY_UP}
    202  1.10       mrg };
    203  1.10       mrg /* Number of TC entries .... */
    204  1.13    simonb static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
    205  1.10       mrg 
    206  1.48     blymn int	ESCDELAY = 300;		/* Delay in ms between keys for esc seq's */
    207  1.48     blymn 
    208  1.48     blymn /* Key buffer */
    209  1.48     blymn #define INBUF_SZ 16		/* size of key buffer - must be larger than
    210  1.48     blymn 				 * longest multi-key sequence */
    211  1.50       jdc static wchar_t	inbuf[INBUF_SZ];
    212  1.50       jdc static int	start, end, working; /* pointers for manipulating inbuf data */
    213  1.48     blymn 
    214  1.10       mrg /* prototypes for private functions */
    215  1.37     blymn static void add_key_sequence(SCREEN *screen, char *sequence, int key_type);
    216  1.48     blymn static key_entry_t *add_new_key(keymap_t *current, char ch, int key_type,
    217  1.48     blymn         int symbol);
    218  1.37     blymn static void delete_key_sequence(keymap_t *current, int key_type);
    219  1.37     blymn static void do_keyok(keymap_t *current, int key_type, bool flag, int *retval);
    220  1.48     blymn static keymap_t *new_keymap(void); /* create a new keymap */
    221  1.48     blymn static key_entry_t *new_key(void); /* create a new key entry */
    222  1.20     blymn static wchar_t		inkey(int to, int delay);
    223  1.20     blymn 
    224  1.20     blymn /*
    225  1.35     blymn  * Free the storage associated with the given keymap
    226  1.35     blymn  */
    227  1.35     blymn void
    228  1.35     blymn _cursesi_free_keymap(keymap_t *map)
    229  1.35     blymn {
    230  1.35     blymn 	int i;
    231  1.35     blymn 
    232  1.35     blymn 	  /* check for, and free, child keymaps */
    233  1.35     blymn 	for (i = 0; i < MAX_CHAR; i++) {
    234  1.35     blymn 		if (map->mapping[i] >= 0) {
    235  1.35     blymn 			if (map->key[map->mapping[i]]->type == KEYMAP_MULTI)
    236  1.35     blymn 				_cursesi_free_keymap(
    237  1.35     blymn 					map->key[map->mapping[i]]->value.next);
    238  1.35     blymn 		}
    239  1.35     blymn 	}
    240  1.35     blymn 
    241  1.35     blymn 	  /* now free any allocated keymap structs */
    242  1.35     blymn 	for (i = 0; i < map->count; i += KEYMAP_ALLOC_CHUNK) {
    243  1.35     blymn 		free(map->key[i]);
    244  1.35     blymn 	}
    245  1.36     blymn 
    246  1.35     blymn 	free(map->key);
    247  1.35     blymn 	free(map);
    248  1.35     blymn }
    249  1.35     blymn 
    250  1.48     blymn 
    251  1.35     blymn /*
    252  1.20     blymn  * Add a new key entry to the keymap pointed to by current.  Entry
    253  1.20     blymn  * contains the character to add to the keymap, type is the type of
    254  1.20     blymn  * entry to add (either multikey or leaf) and symbol is the symbolic
    255  1.20     blymn  * value for a leaf type entry.  The function returns a pointer to the
    256  1.20     blymn  * new keymap entry.
    257  1.20     blymn  */
    258  1.20     blymn static key_entry_t *
    259  1.20     blymn add_new_key(keymap_t *current, char chr, int key_type, int symbol)
    260  1.20     blymn {
    261  1.20     blymn 	key_entry_t *the_key;
    262  1.37     blymn         int i, ki;
    263  1.20     blymn 
    264  1.20     blymn #ifdef DEBUG
    265  1.47       jdc 	__CTRACE(__CTRACE_MISC,
    266  1.47       jdc 	    "Adding character %s of type %d, symbol 0x%x\n",
    267  1.47       jdc 	    unctrl(chr), key_type, symbol);
    268  1.20     blymn #endif
    269  1.33     blymn 	if (current->mapping[(unsigned char) chr] < 0) {
    270  1.37     blymn 		if (current->mapping[(unsigned char) chr] == MAPPING_UNUSED) {
    271  1.37     blymn 			  /* first time for this char */
    272  1.37     blymn 			current->mapping[(unsigned char) chr] =
    273  1.37     blymn 				current->count;	/* map new entry */
    274  1.37     blymn 			ki = current->count;
    275  1.48     blymn 
    276  1.37     blymn 			  /* make sure we have room in the key array first */
    277  1.37     blymn 			if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
    278  1.37     blymn 			{
    279  1.37     blymn 				if ((current->key =
    280  1.37     blymn 				     realloc(current->key,
    281  1.37     blymn 					     ki * sizeof(key_entry_t *)
    282  1.37     blymn 					     + KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t *))) == NULL) {
    283  1.37     blymn 					fprintf(stderr,
    284  1.37     blymn 					  "Could not malloc for key entry\n");
    285  1.37     blymn 					exit(1);
    286  1.37     blymn 				}
    287  1.48     blymn 
    288  1.37     blymn 				the_key = new_key();
    289  1.37     blymn 				for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
    290  1.37     blymn 					current->key[ki + i] = &the_key[i];
    291  1.37     blymn 				}
    292  1.20     blymn 			}
    293  1.37     blymn                 } else {
    294  1.37     blymn 			  /* the mapping was used but freed, reuse it */
    295  1.37     blymn 			ki = - current->mapping[(unsigned char) chr];
    296  1.37     blymn 			current->mapping[(unsigned char) chr] = ki;
    297  1.37     blymn 		}
    298  1.48     blymn 
    299  1.37     blymn 		current->count++;
    300  1.48     blymn 
    301  1.37     blymn 		  /* point at the current key array element to use */
    302  1.37     blymn 		the_key = current->key[ki];
    303  1.48     blymn 
    304  1.20     blymn 		the_key->type = key_type;
    305  1.20     blymn 
    306  1.20     blymn 		switch (key_type) {
    307  1.20     blymn 		  case KEYMAP_MULTI:
    308  1.20     blymn 			    /* need for next key */
    309  1.20     blymn #ifdef DEBUG
    310  1.47       jdc 			  __CTRACE(__CTRACE_MISC, "Creating new keymap\n");
    311  1.20     blymn #endif
    312  1.20     blymn 			  the_key->value.next = new_keymap();
    313  1.37     blymn 			  the_key->enable = TRUE;
    314  1.20     blymn 			  break;
    315  1.20     blymn 
    316  1.20     blymn 		  case KEYMAP_LEAF:
    317  1.20     blymn 				/* the associated symbol for the key */
    318  1.20     blymn #ifdef DEBUG
    319  1.47       jdc 			  __CTRACE(__CTRACE_MISC, "Adding leaf key\n");
    320  1.20     blymn #endif
    321  1.20     blymn 			  the_key->value.symbol = symbol;
    322  1.37     blymn 			  the_key->enable = TRUE;
    323  1.20     blymn 			  break;
    324  1.20     blymn 
    325  1.20     blymn 		  default:
    326  1.20     blymn 			  fprintf(stderr, "add_new_key: bad type passed\n");
    327  1.20     blymn 			  exit(1);
    328  1.20     blymn 		}
    329  1.20     blymn 	} else {
    330  1.20     blymn 		  /* the key is already known - just return the address. */
    331  1.20     blymn #ifdef DEBUG
    332  1.47       jdc 		__CTRACE(__CTRACE_MISC, "Keymap already known\n");
    333  1.20     blymn #endif
    334  1.33     blymn 		the_key = current->key[current->mapping[(unsigned char) chr]];
    335  1.20     blymn 	}
    336  1.20     blymn 
    337  1.20     blymn         return the_key;
    338  1.20     blymn }
    339  1.10       mrg 
    340  1.10       mrg /*
    341  1.37     blymn  * Delete the given key symbol from the key mappings for the screen.
    342  1.37     blymn  *
    343  1.37     blymn  */
    344  1.37     blymn void
    345  1.37     blymn delete_key_sequence(keymap_t *current, int key_type)
    346  1.37     blymn {
    347  1.37     blymn 	key_entry_t *key;
    348  1.37     blymn 	int i;
    349  1.37     blymn 
    350  1.37     blymn 	  /*
    351  1.37     blymn 	   * we need to iterate over all the keys as there may be
    352  1.37     blymn 	   * multiple instances of the leaf symbol.
    353  1.37     blymn 	   */
    354  1.37     blymn 	for (i = 0; i < MAX_CHAR; i++) {
    355  1.37     blymn 		if (current->mapping[i] < 0)
    356  1.37     blymn 			continue; /* no mapping for the key, next! */
    357  1.37     blymn 
    358  1.37     blymn 		key = current->key[current->mapping[i]];
    359  1.37     blymn 
    360  1.37     blymn 		if (key->type == KEYMAP_MULTI) {
    361  1.37     blymn 			  /* have not found the leaf, recurse down */
    362  1.37     blymn 			delete_key_sequence(key->value.next, key_type);
    363  1.37     blymn 			  /* if we deleted the last key in the map, free */
    364  1.37     blymn 			if (key->value.next->count == 0)
    365  1.37     blymn 				_cursesi_free_keymap(key->value.next);
    366  1.37     blymn 		} else if ((key->type == KEYMAP_LEAF)
    367  1.37     blymn 			   && (key->value.symbol == key_type)) {
    368  1.37     blymn 			  /*
    369  1.37     blymn 			   * delete the mapping by negating the current
    370  1.37     blymn 			   * index - this "holds" the position in the
    371  1.37     blymn 			   * allocation just in case we later re-add
    372  1.37     blymn 			   * the key for that mapping.
    373  1.37     blymn 			   */
    374  1.37     blymn 			current->mapping[i] = - current->mapping[i];
    375  1.37     blymn 			current->count--;
    376  1.37     blymn 		}
    377  1.37     blymn 	}
    378  1.37     blymn }
    379  1.48     blymn 
    380  1.37     blymn /*
    381  1.37     blymn  * Add the sequence of characters given in sequence as the key mapping
    382  1.37     blymn  * for the given key symbol.
    383  1.37     blymn  */
    384  1.37     blymn void
    385  1.37     blymn add_key_sequence(SCREEN *screen, char *sequence, int key_type)
    386  1.37     blymn {
    387  1.37     blymn 	key_entry_t *tmp_key;
    388  1.37     blymn 	keymap_t *current;
    389  1.37     blymn 	int length, j, key_ent;
    390  1.37     blymn 
    391  1.48     blymn #ifdef DEBUG
    392  1.48     blymn 	__CTRACE(__CTRACE_MISC, "add_key_sequence: add key sequence: %s(%s)\n",
    393  1.48     blymn 	    sequence, keyname(key_type));
    394  1.48     blymn #endif /* DEBUG */
    395  1.37     blymn 	current = screen->base_keymap;	/* always start with
    396  1.37     blymn 					 * base keymap. */
    397  1.37     blymn 	length = (int) strlen(sequence);
    398  1.37     blymn 
    399  1.49     blymn 	/*
    400  1.49     blymn 	 * OK - we really should never get a zero length string here, either
    401  1.49     blymn 	 * the termcap entry is there and it has a value or we are not called
    402  1.49     blymn 	 * at all.  Unfortunately, if someone assigns a termcap string to the
    403  1.49     blymn 	 * ^@ value we get passed a null string which messes up our length.
    404  1.49     blymn 	 * So, if we get a null string then just insert a leaf value in
    405  1.49     blymn 	 * the 0th char position of the root keymap.  Note that we are
    406  1.49     blymn 	 * totally screwed if someone terminates a multichar sequence
    407  1.49     blymn 	 * with ^@... oh well.
    408  1.49     blymn 	 */
    409  1.49     blymn 	if (length == 0)
    410  1.49     blymn 		length = 1;
    411  1.49     blymn 
    412  1.37     blymn 	for (j = 0; j < length - 1; j++) {
    413  1.37     blymn 		  /* add the entry to the struct */
    414  1.37     blymn 		tmp_key = add_new_key(current, sequence[j], KEYMAP_MULTI, 0);
    415  1.48     blymn 
    416  1.37     blymn 		  /* index into the key array - it's
    417  1.37     blymn 		     clearer if we stash this */
    418  1.37     blymn 		key_ent = current->mapping[(unsigned char) sequence[j]];
    419  1.37     blymn 
    420  1.37     blymn 		current->key[key_ent] = tmp_key;
    421  1.48     blymn 
    422  1.37     blymn 		  /* next key uses this map... */
    423  1.37     blymn 		current = current->key[key_ent]->value.next;
    424  1.37     blymn 	}
    425  1.37     blymn 
    426  1.37     blymn 	/*
    427  1.37     blymn 	 * This is the last key in the sequence (it may have been the
    428  1.37     blymn 	 * only one but that does not matter) this means it is a leaf
    429  1.37     blymn 	 * key and should have a symbol associated with it.
    430  1.37     blymn 	 */
    431  1.37     blymn 	tmp_key = add_new_key(current, sequence[length - 1], KEYMAP_LEAF,
    432  1.37     blymn 			      key_type);
    433  1.37     blymn 	current->key[current->mapping[(int)sequence[length - 1]]] = tmp_key;
    434  1.37     blymn }
    435  1.37     blymn 
    436  1.37     blymn /*
    437  1.10       mrg  * Init_getch - initialise all the pointers & structures needed to make
    438  1.10       mrg  * getch work in keypad mode.
    439  1.10       mrg  *
    440  1.10       mrg  */
    441  1.10       mrg void
    442  1.35     blymn __init_getch(SCREEN *screen)
    443  1.10       mrg {
    444  1.27     blymn 	char entry[1024], *p;
    445  1.37     blymn 	int     i;
    446  1.20     blymn 	size_t limit;
    447  1.20     blymn #ifdef DEBUG
    448  1.37     blymn 	int k, length;
    449  1.20     blymn #endif
    450  1.10       mrg 
    451  1.10       mrg 	/* init the inkey state variable */
    452  1.10       mrg 	state = INKEY_NORM;
    453  1.10       mrg 
    454  1.10       mrg 	/* init the base keymap */
    455  1.35     blymn 	screen->base_keymap = new_keymap();
    456  1.10       mrg 
    457  1.10       mrg 	/* key input buffer pointers */
    458  1.10       mrg 	start = end = working = 0;
    459  1.10       mrg 
    460  1.10       mrg 	/* now do the termcap snarfing ... */
    461  1.35     blymn 
    462  1.27     blymn 	for (i = 0; i < num_tcs; i++) {
    463  1.27     blymn 		p = entry;
    464  1.27     blymn 		limit = 1023;
    465  1.35     blymn 		if (t_getstr(screen->cursesi_genbuf, tc[i].name,
    466  1.48     blymn 			     &p, &limit) != (char *) NULL) {
    467  1.20     blymn #ifdef DEBUG
    468  1.47       jdc 			__CTRACE(__CTRACE_INIT,
    469  1.47       jdc 			    "Processing termcap entry %s, sequence ",
    470  1.47       jdc 			    tc[i].name);
    471  1.37     blymn 			length = (int) strlen(entry);
    472  1.27     blymn 			for (k = 0; k <= length -1; k++)
    473  1.47       jdc 				__CTRACE(__CTRACE_INIT, "%s", unctrl(entry[k]));
    474  1.47       jdc 			__CTRACE(__CTRACE_INIT, "\n");
    475  1.27     blymn #endif
    476  1.37     blymn 			add_key_sequence(screen, entry, tc[i].symbol);
    477  1.12        pk 		}
    478  1.48     blymn 
    479  1.10       mrg 	}
    480  1.10       mrg }
    481  1.10       mrg 
    482  1.10       mrg 
    483  1.10       mrg /*
    484  1.10       mrg  * new_keymap - allocates & initialises a new keymap structure.  This
    485  1.10       mrg  * function returns a pointer to the new keymap.
    486  1.10       mrg  *
    487  1.10       mrg  */
    488  1.13    simonb static keymap_t *
    489  1.10       mrg new_keymap(void)
    490  1.10       mrg {
    491  1.10       mrg 	int     i;
    492  1.10       mrg 	keymap_t *new_map;
    493  1.10       mrg 
    494  1.10       mrg 	if ((new_map = malloc(sizeof(keymap_t))) == NULL) {
    495  1.10       mrg 		perror("Inkey: Cannot allocate new keymap");
    496  1.10       mrg 		exit(2);
    497  1.10       mrg 	}
    498  1.12        pk 
    499  1.12        pk 	/* Initialise the new map */
    500  1.10       mrg 	new_map->count = 0;
    501  1.10       mrg 	for (i = 0; i < MAX_CHAR; i++) {
    502  1.37     blymn 		new_map->mapping[i] = MAPPING_UNUSED; /* no mapping for char */
    503  1.10       mrg 	}
    504  1.10       mrg 
    505  1.23   thorpej 	/* key array will be allocated when first key is added */
    506  1.23   thorpej 	new_map->key = NULL;
    507  1.23   thorpej 
    508  1.20     blymn 	return new_map;
    509  1.10       mrg }
    510  1.10       mrg 
    511  1.10       mrg /*
    512  1.10       mrg  * new_key - allocates & initialises a new key entry.  This function returns
    513  1.10       mrg  * a pointer to the newly allocated key entry.
    514  1.10       mrg  *
    515  1.10       mrg  */
    516  1.13    simonb static key_entry_t *
    517  1.10       mrg new_key(void)
    518  1.10       mrg {
    519  1.10       mrg 	key_entry_t *new_one;
    520  1.20     blymn 	int i;
    521  1.36     blymn 
    522  1.20     blymn 	if ((new_one = malloc(KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t)))
    523  1.20     blymn 	    == NULL) {
    524  1.20     blymn 		perror("inkey: Cannot allocate new key entry chunk");
    525  1.10       mrg 		exit(2);
    526  1.10       mrg 	}
    527  1.10       mrg 
    528  1.20     blymn 	for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
    529  1.20     blymn 		new_one[i].type = 0;
    530  1.20     blymn 		new_one[i].value.next = NULL;
    531  1.20     blymn 	}
    532  1.36     blymn 
    533  1.20     blymn 	return new_one;
    534  1.10       mrg }
    535  1.10       mrg 
    536  1.10       mrg /*
    537  1.10       mrg  * inkey - do the work to process keyboard input, check for multi-key
    538  1.10       mrg  * sequences and return the appropriate symbol if we get a match.
    539  1.10       mrg  *
    540  1.10       mrg  */
    541  1.10       mrg 
    542  1.16     blymn wchar_t
    543  1.20     blymn inkey(int to, int delay)
    544  1.10       mrg {
    545  1.21       jdc 	wchar_t		 k;
    546  1.37     blymn 	int              c, mapping;
    547  1.35     blymn 	keymap_t	*current = _cursesi_screen->base_keymap;
    548  1.35     blymn 	FILE            *infd = _cursesi_screen->infd;
    549  1.10       mrg 
    550  1.25       jdc 	k = 0;		/* XXX gcc -Wuninitialized */
    551  1.25       jdc 
    552  1.46  christos #ifdef DEBUG
    553  1.47       jdc 	__CTRACE(__CTRACE_INPUT, "inkey (%d, %d)\n", to, delay);
    554  1.46  christos #endif
    555  1.10       mrg 	for (;;) {		/* loop until we get a complete key sequence */
    556  1.10       mrg reread:
    557  1.10       mrg 		if (state == INKEY_NORM) {
    558  1.10       mrg 			if (delay && __timeout(delay) == ERR)
    559  1.10       mrg 				return ERR;
    560  1.44       jdc 			c = getchar();
    561  1.44       jdc 			if (c == EOF) {
    562  1.35     blymn 				clearerr(infd);
    563  1.10       mrg 				return ERR;
    564  1.22     blymn 			}
    565  1.48     blymn 
    566  1.10       mrg 			if (delay && (__notimeout() == ERR))
    567  1.10       mrg 				return ERR;
    568  1.22     blymn 
    569  1.16     blymn 			k = (wchar_t) c;
    570  1.10       mrg #ifdef DEBUG
    571  1.47       jdc 			__CTRACE(__CTRACE_INPUT,
    572  1.47       jdc 			    "inkey (state normal) got '%s'\n", unctrl(k));
    573  1.10       mrg #endif
    574  1.10       mrg 
    575  1.10       mrg 			working = start;
    576  1.10       mrg 			inbuf[working] = k;
    577  1.10       mrg 			INC_POINTER(working);
    578  1.10       mrg 			end = working;
    579  1.10       mrg 			state = INKEY_ASSEMBLING;	/* go to the assembling
    580  1.10       mrg 							 * state now */
    581  1.12        pk 		} else if (state == INKEY_BACKOUT) {
    582  1.12        pk 			k = inbuf[working];
    583  1.12        pk 			INC_POINTER(working);
    584  1.12        pk 			if (working == end) {	/* see if we have run
    585  1.12        pk 						 * out of keys in the
    586  1.12        pk 						 * backlog */
    587  1.12        pk 
    588  1.41       jdc 				/* if we have then switch to assembling */
    589  1.12        pk 				state = INKEY_ASSEMBLING;
    590  1.12        pk 			}
    591  1.12        pk 		} else if (state == INKEY_ASSEMBLING) {
    592  1.12        pk 			/* assembling a key sequence */
    593  1.12        pk 			if (delay) {
    594  1.41       jdc 				if (__timeout(to ? (ESCDELAY / 100) : delay)
    595  1.41       jdc 				    == ERR)
    596  1.41       jdc 					return ERR;
    597  1.12        pk 			} else {
    598  1.41       jdc 				if (to && (__timeout(ESCDELAY / 100) == ERR))
    599  1.10       mrg 					return ERR;
    600  1.12        pk 			}
    601  1.22     blymn 
    602  1.22     blymn 			c = getchar();
    603  1.52       jdc 			if (ferror(infd)) {
    604  1.35     blymn 				clearerr(infd);
    605  1.12        pk 				return ERR;
    606  1.22     blymn 			}
    607  1.48     blymn 
    608  1.12        pk 			if ((to || delay) && (__notimeout() == ERR))
    609  1.10       mrg 					return ERR;
    610  1.14    simonb 
    611  1.10       mrg #ifdef DEBUG
    612  1.47       jdc 			__CTRACE(__CTRACE_INPUT,
    613  1.47       jdc 			    "inkey (state assembling) got '%s'\n", unctrl(k));
    614  1.10       mrg #endif
    615  1.52       jdc 			if (feof(infd) || c == -1) {	/* inter-char timeout,
    616  1.52       jdc 							 * start backing out */
    617  1.35     blymn 				clearerr(infd);
    618  1.12        pk 				if (start == end)
    619  1.12        pk 					/* no chars in the buffer, restart */
    620  1.12        pk 					goto reread;
    621  1.12        pk 
    622  1.12        pk 				k = inbuf[start];
    623  1.12        pk 				state = INKEY_TIMEOUT;
    624  1.10       mrg 			} else {
    625  1.52       jdc 				k = (wchar_t) c;
    626  1.12        pk 				inbuf[working] = k;
    627  1.12        pk 				INC_POINTER(working);
    628  1.12        pk 				end = working;
    629  1.10       mrg 			}
    630  1.12        pk 		} else {
    631  1.12        pk 			fprintf(stderr, "Inkey state screwed - exiting!!!");
    632  1.12        pk 			exit(2);
    633  1.12        pk 		}
    634  1.10       mrg 
    635  1.37     blymn 		  /*
    636  1.37     blymn 		   * Check key has no special meaning and we have not
    637  1.37     blymn 		   * timed out and the key has not been disabled
    638  1.37     blymn 		   */
    639  1.37     blymn 		mapping = current->mapping[k];
    640  1.37     blymn 		if (((state == INKEY_TIMEOUT) || (mapping < 0))
    641  1.37     blymn 			|| ((current->key[mapping]->type == KEYMAP_LEAF)
    642  1.37     blymn 			    && (current->key[mapping]->enable == FALSE))) {
    643  1.12        pk 			/* return the first key we know about */
    644  1.12        pk 			k = inbuf[start];
    645  1.10       mrg 
    646  1.10       mrg 			INC_POINTER(start);
    647  1.10       mrg 			working = start;
    648  1.10       mrg 
    649  1.10       mrg 			if (start == end) {	/* only one char processed */
    650  1.10       mrg 				state = INKEY_NORM;
    651  1.10       mrg 			} else {/* otherwise we must have more than one char
    652  1.10       mrg 				 * to backout */
    653  1.10       mrg 				state = INKEY_BACKOUT;
    654  1.10       mrg 			}
    655  1.10       mrg 			return k;
    656  1.10       mrg 		} else {	/* must be part of a multikey sequence */
    657  1.10       mrg 			/* check for completed key sequence */
    658  1.10       mrg 			if (current->key[current->mapping[k]]->type == KEYMAP_LEAF) {
    659  1.10       mrg 				start = working;	/* eat the key sequence
    660  1.10       mrg 							 * in inbuf */
    661  1.10       mrg 
    662  1.12        pk 				/* check if inbuf empty now */
    663  1.12        pk 				if (start == end) {
    664  1.12        pk 					/* if it is go back to normal */
    665  1.12        pk 					state = INKEY_NORM;
    666  1.12        pk 				} else {
    667  1.12        pk 					/* otherwise go to backout state */
    668  1.10       mrg 					state = INKEY_BACKOUT;
    669  1.10       mrg 				}
    670  1.10       mrg 
    671  1.10       mrg 				/* return the symbol */
    672  1.10       mrg 				return current->key[current->mapping[k]]->value.symbol;
    673  1.10       mrg 
    674  1.12        pk 			} else {
    675  1.12        pk 				/*
    676  1.12        pk 				 * Step on to next part of the multi-key
    677  1.12        pk 				 * sequence.
    678  1.12        pk 				 */
    679  1.10       mrg 				current = current->key[current->mapping[k]]->value.next;
    680  1.10       mrg 			}
    681  1.10       mrg 		}
    682  1.10       mrg 	}
    683  1.10       mrg }
    684  1.10       mrg 
    685  1.18     blymn #ifndef _CURSES_USE_MACROS
    686  1.18     blymn /*
    687  1.18     blymn  * getch --
    688  1.18     blymn  *	Read in a character from stdscr.
    689  1.18     blymn  */
    690  1.18     blymn int
    691  1.18     blymn getch(void)
    692  1.18     blymn {
    693  1.18     blymn 	return wgetch(stdscr);
    694  1.18     blymn }
    695  1.18     blymn 
    696  1.18     blymn /*
    697  1.18     blymn  * mvgetch --
    698  1.18     blymn  *      Read in a character from stdscr at the given location.
    699  1.18     blymn  */
    700  1.18     blymn int
    701  1.18     blymn mvgetch(int y, int x)
    702  1.18     blymn {
    703  1.18     blymn 	return mvwgetch(stdscr, y, x);
    704  1.18     blymn }
    705  1.18     blymn 
    706  1.18     blymn /*
    707  1.18     blymn  * mvwgetch --
    708  1.18     blymn  *      Read in a character from stdscr at the given location in the
    709  1.18     blymn  *      given window.
    710  1.18     blymn  */
    711  1.18     blymn int
    712  1.18     blymn mvwgetch(WINDOW *win, int y, int x)
    713  1.18     blymn {
    714  1.18     blymn 	if (wmove(win, y, x) == ERR)
    715  1.18     blymn 		return ERR;
    716  1.18     blymn 
    717  1.18     blymn 	return wgetch(win);
    718  1.18     blymn }
    719  1.18     blymn 
    720  1.18     blymn #endif
    721  1.18     blymn 
    722  1.37     blymn /*
    723  1.37     blymn  * keyok --
    724  1.37     blymn  *      Set the enable flag for a keysym, if the flag is false then
    725  1.37     blymn  * getch will not return this keysym even if the matching key sequence
    726  1.37     blymn  * is seen.
    727  1.37     blymn  */
    728  1.37     blymn int
    729  1.37     blymn keyok(int key_type, bool flag)
    730  1.37     blymn {
    731  1.37     blymn 	int result = ERR;
    732  1.48     blymn 
    733  1.37     blymn 	do_keyok(_cursesi_screen->base_keymap, key_type, flag, &result);
    734  1.37     blymn 	return result;
    735  1.37     blymn }
    736  1.37     blymn 
    737  1.37     blymn /*
    738  1.37     blymn  * do_keyok --
    739  1.37     blymn  *       Does the actual work for keyok, we need to recurse through the
    740  1.37     blymn  * keymaps finding the passed key symbol.
    741  1.37     blymn  */
    742  1.37     blymn void
    743  1.37     blymn do_keyok(keymap_t *current, int key_type, bool flag, int *retval)
    744  1.37     blymn {
    745  1.37     blymn 	key_entry_t *key;
    746  1.37     blymn 	int i;
    747  1.37     blymn 
    748  1.37     blymn 	  /*
    749  1.37     blymn 	   * we need to iterate over all the keys as there may be
    750  1.37     blymn 	   * multiple instances of the leaf symbol.
    751  1.37     blymn 	   */
    752  1.37     blymn 	for (i = 0; i < MAX_CHAR; i++) {
    753  1.37     blymn 		if (current->mapping[i] < 0)
    754  1.37     blymn 			continue; /* no mapping for the key, next! */
    755  1.37     blymn 
    756  1.37     blymn 		key = current->key[current->mapping[i]];
    757  1.37     blymn 
    758  1.37     blymn 		if (key->type == KEYMAP_MULTI)
    759  1.37     blymn 			do_keyok(key->value.next, key_type, flag, retval);
    760  1.37     blymn 		else if ((key->type == KEYMAP_LEAF)
    761  1.37     blymn 			 && (key->value.symbol == key_type)) {
    762  1.37     blymn 			key->enable = flag;
    763  1.37     blymn 			*retval = OK; /* we found at least one instance, ok */
    764  1.37     blymn 		}
    765  1.37     blymn 	}
    766  1.37     blymn }
    767  1.37     blymn 
    768  1.37     blymn /*
    769  1.37     blymn  * define_key --
    770  1.37     blymn  *      Add a custom mapping of a key sequence to key symbol.
    771  1.37     blymn  *
    772  1.37     blymn  */
    773  1.37     blymn int
    774  1.37     blymn define_key(char *sequence, int symbol)
    775  1.37     blymn {
    776  1.37     blymn 
    777  1.37     blymn 	if (symbol <= 0)
    778  1.37     blymn 		return ERR;
    779  1.37     blymn 
    780  1.37     blymn 	if (sequence == NULL)
    781  1.37     blymn 		delete_key_sequence(_cursesi_screen->base_keymap, symbol);
    782  1.37     blymn 	else
    783  1.37     blymn 		add_key_sequence(_cursesi_screen, sequence, symbol);
    784  1.37     blymn 
    785  1.37     blymn 	return OK;
    786  1.37     blymn }
    787  1.48     blymn 
    788   1.1       cgd /*
    789   1.4   mycroft  * wgetch --
    790   1.4   mycroft  *	Read in a character from the window.
    791   1.1       cgd  */
    792   1.4   mycroft int
    793  1.18     blymn wgetch(WINDOW *win)
    794   1.4   mycroft {
    795  1.30    itojun 	int inp, weset;
    796  1.30    itojun 	int c;
    797  1.35     blymn 	FILE *infd = _cursesi_screen->infd;
    798   1.1       cgd 
    799  1.51       jdc #ifdef DEBUG
    800  1.51       jdc 	__CTRACE(__CTRACE_INPUT, "wgetch: win(%p)\n", win);
    801  1.51       jdc #endif
    802   1.5       cgd 	if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
    803  1.10       mrg 	    && win->curx == win->maxx - 1 && win->cury == win->maxy - 1
    804  1.10       mrg 	    && __echoit)
    805   1.4   mycroft 		return (ERR);
    806  1.25       jdc 
    807  1.32    itojun 	if (is_wintouched(win))
    808  1.32    itojun 		wrefresh(win);
    809   1.4   mycroft #ifdef DEBUG
    810  1.47       jdc 	__CTRACE(__CTRACE_INPUT, "wgetch: __echoit = %d, "
    811  1.52       jdc 	    "__rawmode = %d, __nl = %d, flags = %#.4x, delay = %d\n",
    812  1.52       jdc 	    __echoit, __rawmode, _cursesi_screen->nl, win->flags, win->delay);
    813   1.4   mycroft #endif
    814  1.50       jdc 	if (_cursesi_screen->resized) {
    815  1.50       jdc 		_cursesi_screen->resized = 0;
    816  1.51       jdc #ifdef DEBUG
    817  1.51       jdc 		__CTRACE(__CTRACE_INPUT, "wgetch returning KEY_RESIZE\n");
    818  1.51       jdc #endif
    819  1.50       jdc 		return KEY_RESIZE;
    820  1.50       jdc 	}
    821  1.50       jdc 	if (_cursesi_screen->unget_pos) {
    822  1.50       jdc #ifdef DEBUG
    823  1.50       jdc 		__CTRACE(__CTRACE_INPUT, "wgetch returning char at %d\n",
    824  1.50       jdc 		    _cursesi_screen->unget_pos);
    825  1.50       jdc #endif
    826  1.50       jdc 		_cursesi_screen->unget_pos--;
    827  1.50       jdc 		c = _cursesi_screen->unget_list[_cursesi_screen->unget_pos];
    828  1.50       jdc 		if (__echoit)
    829  1.50       jdc 			waddch(win, (chtype) c);
    830  1.50       jdc 		return c;
    831  1.50       jdc 	}
    832   1.4   mycroft 	if (__echoit && !__rawmode) {
    833   1.1       cgd 		cbreak();
    834   1.4   mycroft 		weset = 1;
    835   1.4   mycroft 	} else
    836   1.4   mycroft 		weset = 0;
    837   1.4   mycroft 
    838  1.10       mrg 	__save_termios();
    839  1.10       mrg 
    840  1.10       mrg 	if (win->flags & __KEYPAD) {
    841  1.10       mrg 		switch (win->delay)
    842  1.10       mrg 		{
    843  1.10       mrg 		case -1:
    844  1.10       mrg 			inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0);
    845  1.10       mrg 			break;
    846  1.10       mrg 		case 0:
    847  1.19       jdc 			if (__nodelay() == ERR) {
    848  1.19       jdc 				__restore_termios();
    849  1.19       jdc 				return ERR;
    850  1.19       jdc 			}
    851  1.10       mrg 			inp = inkey(0, 0);
    852  1.10       mrg 			break;
    853  1.10       mrg 		default:
    854  1.10       mrg 			inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay);
    855  1.10       mrg 			break;
    856  1.10       mrg 		}
    857  1.10       mrg 	} else {
    858  1.10       mrg 		switch (win->delay)
    859  1.10       mrg 		{
    860  1.10       mrg 		case -1:
    861  1.46  christos 			if (__delay() == ERR) {
    862  1.46  christos 				__restore_termios();
    863  1.46  christos 				return ERR;
    864  1.46  christos 			}
    865  1.10       mrg 			break;
    866  1.10       mrg 		case 0:
    867  1.10       mrg 			if (__nodelay() == ERR) {
    868  1.10       mrg 				__restore_termios();
    869  1.10       mrg 				return ERR;
    870  1.10       mrg 			}
    871  1.10       mrg 			break;
    872  1.10       mrg 		default:
    873  1.10       mrg 			if (__timeout(win->delay) == ERR) {
    874  1.10       mrg 				__restore_termios();
    875  1.10       mrg 				return ERR;
    876  1.10       mrg 			}
    877  1.10       mrg 			break;
    878  1.10       mrg 		}
    879  1.12        pk 
    880  1.22     blymn 		c = getchar();
    881  1.35     blymn 		if (feof(infd)) {
    882  1.35     blymn 			clearerr(infd);
    883  1.22     blymn 			__restore_termios();
    884  1.22     blymn 			return ERR;	/* we have timed out */
    885  1.22     blymn 		}
    886  1.48     blymn 
    887  1.35     blymn 		if (ferror(infd)) {
    888  1.35     blymn 			clearerr(infd);
    889  1.10       mrg 			inp = ERR;
    890  1.12        pk 		} else {
    891  1.30    itojun 			inp = c;
    892  1.10       mrg 		}
    893  1.10       mrg 	}
    894   1.4   mycroft #ifdef DEBUG
    895  1.15    simonb 	if (inp > 255)
    896  1.20     blymn 		  /* we have a key symbol - treat it differently */
    897  1.20     blymn 		  /* XXXX perhaps __unctrl should be expanded to include
    898  1.20     blymn 		   * XXXX the keysyms in the table....
    899  1.20     blymn 		   */
    900  1.47       jdc 		__CTRACE(__CTRACE_INPUT, "wgetch assembled keysym 0x%x\n", inp);
    901  1.15    simonb 	else
    902  1.47       jdc 		__CTRACE(__CTRACE_INPUT, "wgetch got '%s'\n", unctrl(inp));
    903   1.4   mycroft #endif
    904  1.12        pk 	if (win->delay > -1) {
    905  1.10       mrg 		if (__delay() == ERR) {
    906  1.10       mrg 			__restore_termios();
    907  1.10       mrg 			return ERR;
    908  1.10       mrg 		}
    909  1.12        pk 	}
    910  1.12        pk 
    911  1.10       mrg 	__restore_termios();
    912  1.27     blymn 
    913  1.26   mycroft 	if (__echoit)
    914  1.16     blymn 		waddch(win, (chtype) inp);
    915  1.27     blymn 
    916   1.1       cgd 	if (weset)
    917   1.1       cgd 		nocbreak();
    918  1.40       jdc 
    919  1.40       jdc 	if (_cursesi_screen->nl && inp == 13)
    920  1.40       jdc 		inp = 10;
    921  1.12        pk 
    922  1.10       mrg 	return ((inp < 0) || (inp == ERR) ? ERR : inp);
    923  1.22     blymn }
    924  1.22     blymn 
    925  1.22     blymn /*
    926  1.22     blymn  * ungetch --
    927  1.22     blymn  *     Put the character back into the input queue.
    928  1.22     blymn  */
    929  1.22     blymn int
    930  1.22     blymn ungetch(int c)
    931  1.22     blymn {
    932  1.50       jdc 	return __unget((wint_t) c);
    933  1.50       jdc }
    934  1.50       jdc 
    935  1.50       jdc /*
    936  1.50       jdc  * __unget --
    937  1.50       jdc  *    Do the work for ungetch() and unget_wch();
    938  1.50       jdc  */
    939  1.50       jdc int
    940  1.50       jdc __unget(wint_t c)
    941  1.50       jdc {
    942  1.50       jdc 	wchar_t	*p;
    943  1.50       jdc 	int	len;
    944  1.50       jdc 
    945  1.50       jdc #ifdef DEBUG
    946  1.50       jdc 	__CTRACE(__CTRACE_INPUT, "__unget(%x)\n", c);
    947  1.50       jdc #endif
    948  1.50       jdc 	if (_cursesi_screen->unget_pos >= _cursesi_screen->unget_len) {
    949  1.50       jdc 		len = _cursesi_screen->unget_len + 32;
    950  1.50       jdc 		if ((p = realloc(_cursesi_screen->unget_list,
    951  1.50       jdc 		    sizeof(wchar_t) * len)) == NULL) {
    952  1.50       jdc 			/* Can't realloc(), so just lose the oldest entry */
    953  1.50       jdc 			memmove(_cursesi_screen->unget_list,
    954  1.50       jdc 			    _cursesi_screen->unget_list + sizeof(wchar_t),
    955  1.50       jdc 			    _cursesi_screen->unget_len - 1);
    956  1.50       jdc 			_cursesi_screen->unget_list[_cursesi_screen->unget_len
    957  1.50       jdc 			    - 1] = c;
    958  1.50       jdc 			_cursesi_screen->unget_pos =
    959  1.50       jdc 			    _cursesi_screen->unget_len;
    960  1.50       jdc 			return OK;
    961  1.50       jdc 		} else {
    962  1.50       jdc 			_cursesi_screen->unget_pos =
    963  1.50       jdc 			    _cursesi_screen->unget_len;
    964  1.50       jdc 			_cursesi_screen->unget_len = len;
    965  1.50       jdc 			_cursesi_screen->unget_list = p;
    966  1.50       jdc 		}
    967  1.50       jdc 	}
    968  1.50       jdc 	_cursesi_screen->unget_list[_cursesi_screen->unget_pos] = c;
    969  1.50       jdc 	_cursesi_screen->unget_pos++;
    970  1.50       jdc 	return OK;
    971   1.1       cgd }
    972