Re-arrange some code to approach our regular style.
This commit is contained in:
parent
92d41a0675
commit
6c4ae8ffb4
|
@ -1,4 +1,4 @@
|
||||||
/* $NetBSD: getch.c,v 1.11 1999/06/06 20:43:00 pk Exp $ */
|
/* $NetBSD: getch.c,v 1.12 1999/06/06 21:05:03 pk Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1981, 1993, 1994
|
* Copyright (c) 1981, 1993, 1994
|
||||||
|
@ -38,7 +38,7 @@
|
||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)getch.c 8.2 (Berkeley) 5/4/94";
|
static char sccsid[] = "@(#)getch.c 8.2 (Berkeley) 5/4/94";
|
||||||
#else
|
#else
|
||||||
__RCSID("$NetBSD: getch.c,v 1.11 1999/06/06 20:43:00 pk Exp $");
|
__RCSID("$NetBSD: getch.c,v 1.12 1999/06/06 21:05:03 pk Exp $");
|
||||||
#endif
|
#endif
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ struct key_entry {
|
||||||
union {
|
union {
|
||||||
keymap_t *next; /* next keymap is key is multi-key sequence */
|
keymap_t *next; /* next keymap is key is multi-key sequence */
|
||||||
int symbol; /* key symbol if key is a leaf entry */
|
int symbol; /* key symbol if key is a leaf entry */
|
||||||
} value;
|
} value;
|
||||||
};
|
};
|
||||||
/* Types of key structures we can have */
|
/* Types of key structures we can have */
|
||||||
#define KEYMAP_MULTI 1 /* part of a multi char sequence */
|
#define KEYMAP_MULTI 1 /* part of a multi char sequence */
|
||||||
|
@ -83,8 +83,8 @@ struct key_entry {
|
||||||
#define MAX_CHAR 256
|
#define MAX_CHAR 256
|
||||||
|
|
||||||
struct keymap {
|
struct keymap {
|
||||||
int count; /* count of number of key structs allocated */
|
int count; /* count of number of key structs allocated */
|
||||||
short mapping[MAX_CHAR]; /* mapping of key to allocated structs */
|
short mapping[MAX_CHAR]; /* mapping of key to allocated structs */
|
||||||
key_entry_t **key; /* dynamic array of keys */};
|
key_entry_t **key; /* dynamic array of keys */};
|
||||||
|
|
||||||
|
|
||||||
|
@ -94,22 +94,22 @@ struct keymap {
|
||||||
char inbuf[INBUF_SZ];
|
char inbuf[INBUF_SZ];
|
||||||
int start, end, working; /* pointers for manipulating inbuf data */
|
int start, end, working; /* pointers for manipulating inbuf data */
|
||||||
|
|
||||||
#define INC_POINTER(ptr) do { \
|
#define INC_POINTER(ptr) do { \
|
||||||
(ptr)++; \
|
(ptr)++; \
|
||||||
ptr %= INBUF_SZ; \
|
ptr %= INBUF_SZ; \
|
||||||
} while(/*CONSTCOND*/0)
|
} while(/*CONSTCOND*/0)
|
||||||
|
|
||||||
short state; /* state of the inkey function */
|
short state; /* state of the inkey function */
|
||||||
|
|
||||||
#define INKEY_NORM 0 /* no key backlog to process */
|
#define INKEY_NORM 0 /* no key backlog to process */
|
||||||
#define INKEY_ASSEMBLING 1 /* assembling a multi-key sequence */
|
#define INKEY_ASSEMBLING 1 /* assembling a multi-key sequence */
|
||||||
#define INKEY_BACKOUT 2 /* recovering from an unrecognised key */
|
#define INKEY_BACKOUT 2 /* recovering from an unrecognised key */
|
||||||
#define INKEY_TIMEOUT 3 /* multi-key sequence timeout */
|
#define INKEY_TIMEOUT 3 /* multi-key sequence timeout */
|
||||||
|
|
||||||
/* The termcap data we are interested in and the symbols they map to */
|
/* The termcap data we are interested in and the symbols they map to */
|
||||||
struct tcdata {
|
struct tcdata {
|
||||||
char *name; /* name of termcap entry */
|
char *name; /* name of termcap entry */
|
||||||
int symbol; /* the symbol associated with it */
|
int symbol; /* the symbol associated with it */
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct tcdata tc[] = {
|
const struct tcdata tc[] = {
|
||||||
|
@ -158,14 +158,9 @@ const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
|
||||||
keymap_t *base_keymap;
|
keymap_t *base_keymap;
|
||||||
|
|
||||||
/* prototypes for private functions */
|
/* prototypes for private functions */
|
||||||
keymap_t *
|
keymap_t *new_keymap(void); /* create a new keymap */
|
||||||
new_keymap(void); /* create a new keymap */
|
key_entry_t *new_key(void); /* create a new key entry */
|
||||||
|
unsigned inkey(int, int);
|
||||||
key_entry_t *
|
|
||||||
new_key(void); /* create a new key entry */
|
|
||||||
|
|
||||||
unsigned
|
|
||||||
inkey(int, int);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Init_getch - initialise all the pointers & structures needed to make
|
* Init_getch - initialise all the pointers & structures needed to make
|
||||||
|
@ -176,10 +171,10 @@ void
|
||||||
__init_getch(sp)
|
__init_getch(sp)
|
||||||
char *sp;
|
char *sp;
|
||||||
{
|
{
|
||||||
int i, j, length;
|
static char termcap[1024];
|
||||||
|
char entry[1024], termname[1024], *p;
|
||||||
|
int i, j, length;
|
||||||
keymap_t *current;
|
keymap_t *current;
|
||||||
static char termcap[1024];
|
|
||||||
char entry[1024], termname[1024], *p;
|
|
||||||
key_entry_t *the_key;
|
key_entry_t *the_key;
|
||||||
|
|
||||||
/* init the inkey state variable */
|
/* init the inkey state variable */
|
||||||
|
@ -195,69 +190,70 @@ static char termcap[1024];
|
||||||
strncpy(termname, sp, 1022);
|
strncpy(termname, sp, 1022);
|
||||||
termname[1023] = 0;
|
termname[1023] = 0;
|
||||||
|
|
||||||
if (tgetent(termcap, termname) > 0) {
|
if (tgetent(termcap, termname) <= 0)
|
||||||
for (i = 0; i < num_tcs; i++) {
|
return;
|
||||||
p = entry;
|
|
||||||
if (tgetstr(tc[i].name, &p) != NULL) {
|
|
||||||
current = base_keymap; /* always start with
|
|
||||||
* base keymap. */
|
|
||||||
length = strlen(entry);
|
|
||||||
|
|
||||||
for (j = 0; j < length - 1; j++) {
|
for (i = 0; i < num_tcs; i++) {
|
||||||
if (current->mapping[(unsigned) entry[j]] < 0) {
|
|
||||||
/* first time for this char */
|
|
||||||
current->mapping[(unsigned) entry[j]] = current->count; /* map new entry */
|
|
||||||
the_key = new_key();
|
|
||||||
/* multikey coz we are here */
|
|
||||||
the_key->type = KEYMAP_MULTI;
|
|
||||||
|
|
||||||
/* need for next key */
|
p = entry;
|
||||||
the_key->value.next
|
if (tgetstr(tc[i].name, &p) == NULL)
|
||||||
= new_keymap();
|
continue;
|
||||||
|
|
||||||
/* put into key array */
|
|
||||||
if ((current->key = realloc(current->key, (current->count + 1) * sizeof(key_entry_t *))) == NULL) {
|
|
||||||
fprintf(stderr,
|
|
||||||
"Could not malloc for key entry\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
current->key[current->count++]
|
|
||||||
= the_key;
|
|
||||||
|
|
||||||
}
|
current = base_keymap; /* always start with base keymap. */
|
||||||
/* next key uses this map... */
|
length = strlen(entry);
|
||||||
current = current->key[current->mapping[(unsigned) entry[j]]]->value.next;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* this is the last key in the sequence (it
|
for (j = 0; j < length - 1; j++) {
|
||||||
* may have been the only one but that does
|
if (current->mapping[(unsigned) entry[j]] < 0) {
|
||||||
* not matter) this means it is a leaf key and
|
/* first time for this char */
|
||||||
* should have a symbol associated with it */
|
current->mapping[(unsigned) entry[j]] = current->count; /* map new entry */
|
||||||
if (current->count > 0) {
|
|
||||||
/* if there were other keys then
|
|
||||||
we need to extend the mapping
|
|
||||||
array */
|
|
||||||
if ((current->key =
|
|
||||||
realloc(current->key,
|
|
||||||
(current->count + 1) *
|
|
||||||
sizeof(key_entry_t *)))
|
|
||||||
== NULL) {
|
|
||||||
fprintf(stderr,
|
|
||||||
"Could not malloc for key entry\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
current->mapping[(unsigned) entry[length - 1]]
|
|
||||||
= current->count;
|
|
||||||
the_key = new_key();
|
the_key = new_key();
|
||||||
the_key->type = KEYMAP_LEAF; /* leaf key */
|
/* multikey coz we are here */
|
||||||
|
the_key->type = KEYMAP_MULTI;
|
||||||
|
|
||||||
/* the associated symbol */
|
/* need for next key */
|
||||||
the_key->value.symbol = tc[i].symbol;
|
the_key->value.next = new_keymap();
|
||||||
|
|
||||||
|
/* put into key array */
|
||||||
|
if ((current->key = realloc(current->key, (current->count + 1) * sizeof(key_entry_t *))) == NULL) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Could not malloc for key entry\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
current->key[current->count++] = the_key;
|
current->key[current->count++] = the_key;
|
||||||
|
|
||||||
|
}
|
||||||
|
/* next key uses this map... */
|
||||||
|
current = current->key[current->mapping[(unsigned) entry[j]]]->value.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is the last key in the sequence (it may have been
|
||||||
|
* the only one but that does not matter) this means it is
|
||||||
|
* a leaf key and should have a symbol associated with it.
|
||||||
|
*/
|
||||||
|
if (current->count > 0) {
|
||||||
|
/*
|
||||||
|
* If there were other keys then we need to
|
||||||
|
* extend the mapping array.
|
||||||
|
*/
|
||||||
|
if ((current->key =
|
||||||
|
realloc(current->key,
|
||||||
|
(current->count + 1) *
|
||||||
|
sizeof(key_entry_t *))) == NULL) {
|
||||||
|
|
||||||
|
fprintf(stderr,
|
||||||
|
"Could not malloc for key entry\n");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
current->mapping[(unsigned) entry[length - 1]] = current->count;
|
||||||
|
the_key = new_key();
|
||||||
|
the_key->type = KEYMAP_LEAF; /* leaf key */
|
||||||
|
|
||||||
|
/* the associated symbol */
|
||||||
|
the_key->value.symbol = tc[i].symbol;
|
||||||
|
current->key[current->count++] = the_key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,19 +273,20 @@ new_keymap(void)
|
||||||
perror("Inkey: Cannot allocate new keymap");
|
perror("Inkey: Cannot allocate new keymap");
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
/* initialise the new map */
|
|
||||||
|
/* Initialise the new map */
|
||||||
new_map->count = 0;
|
new_map->count = 0;
|
||||||
for (i = 0; i < MAX_CHAR; i++) {
|
for (i = 0; i < MAX_CHAR; i++) {
|
||||||
new_map->mapping[i] = -1; /* no mapping for char */
|
new_map->mapping[i] = -1; /* no mapping for char */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* one does assume there will be at least one key mapped.... */
|
/* one does assume there will be at least one key mapped.... */
|
||||||
if ((new_map->key = malloc(sizeof(key_entry_t *))) == NULL) {
|
if ((new_map->key = malloc(sizeof(key_entry_t *))) == NULL) {
|
||||||
perror("Could not malloc first key ent");
|
perror("Could not malloc first key ent");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_map;
|
return (new_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -309,7 +306,7 @@ new_key(void)
|
||||||
new_one->type = 0;
|
new_one->type = 0;
|
||||||
new_one->value.next = NULL;
|
new_one->value.next = NULL;
|
||||||
|
|
||||||
return new_one;
|
return (new_one);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -349,60 +346,58 @@ reread:
|
||||||
end = working;
|
end = working;
|
||||||
state = INKEY_ASSEMBLING; /* go to the assembling
|
state = INKEY_ASSEMBLING; /* go to the assembling
|
||||||
* state now */
|
* state now */
|
||||||
} else
|
} else if (state == INKEY_BACKOUT) {
|
||||||
if (state == INKEY_BACKOUT) {
|
k = inbuf[working];
|
||||||
k = inbuf[working];
|
INC_POINTER(working);
|
||||||
INC_POINTER(working);
|
if (working == end) { /* see if we have run
|
||||||
if (working == end) { /* see if we have run
|
* out of keys in the
|
||||||
* out of keys in the
|
* backlog */
|
||||||
* backlog */
|
|
||||||
|
|
||||||
/* if we have then switch to
|
/* if we have then switch to
|
||||||
assembling */
|
assembling */
|
||||||
state = INKEY_ASSEMBLING;
|
state = INKEY_ASSEMBLING;
|
||||||
}
|
}
|
||||||
} else if (state == INKEY_ASSEMBLING) {
|
} else if (state == INKEY_ASSEMBLING) {
|
||||||
/* assembling a key sequence */
|
/* assembling a key sequence */
|
||||||
if (delay)
|
if (delay) {
|
||||||
{
|
if (__timeout(to ? DEFAULT_DELAY : delay) == ERR)
|
||||||
if (__timeout(to ? DEFAULT_DELAY : delay) == ERR)
|
|
||||||
return ERR;
|
return ERR;
|
||||||
} else {
|
} else {
|
||||||
if (to && (__timeout(DEFAULT_DELAY) == ERR))
|
if (to && (__timeout(DEFAULT_DELAY) == ERR))
|
||||||
return ERR;
|
|
||||||
}
|
|
||||||
if ((nchar = read(STDIN_FILENO, &c,
|
|
||||||
sizeof(char))) < 0)
|
|
||||||
return ERR;
|
return ERR;
|
||||||
if ((to || delay) && (__notimeout() == ERR))
|
}
|
||||||
|
if ((nchar = read(STDIN_FILENO, &c,
|
||||||
|
sizeof(char))) < 0)
|
||||||
|
return ERR;
|
||||||
|
if ((to || delay) && (__notimeout() == ERR))
|
||||||
return ERR;
|
return ERR;
|
||||||
|
|
||||||
k = (unsigned int) c;
|
k = (unsigned int) c;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
__CTRACE("inkey (state assembling) got '%s'\n", unctrl(k));
|
__CTRACE("inkey (state assembling) got '%s'\n", unctrl(k));
|
||||||
#endif
|
#endif
|
||||||
if (nchar == 0) { /* inter-char timeout,
|
if (nchar == 0) { /* inter-char timeout,
|
||||||
* start backing out */
|
* start backing out */
|
||||||
if (start == end)
|
if (start == end)
|
||||||
goto reread; /* no chars in the
|
/* no chars in the buffer, restart */
|
||||||
* buffer, restart */
|
goto reread;
|
||||||
k = inbuf[start];
|
|
||||||
state = INKEY_TIMEOUT;
|
k = inbuf[start];
|
||||||
} else {
|
state = INKEY_TIMEOUT;
|
||||||
inbuf[working] = k;
|
|
||||||
INC_POINTER(working);
|
|
||||||
end = working;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,
|
inbuf[working] = k;
|
||||||
"Inkey state screwed - exiting!!!");
|
INC_POINTER(working);
|
||||||
exit(2);
|
end = working;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Inkey state screwed - exiting!!!");
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
/* Check key has no special meaning and we have not timed out */
|
/* Check key has no special meaning and we have not timed out */
|
||||||
if ((current->mapping[k] < 0) || (state == INKEY_TIMEOUT)) {
|
if ((current->mapping[k] < 0) || (state == INKEY_TIMEOUT)) {
|
||||||
k = inbuf[start]; /* return the first key we
|
/* return the first key we know about */
|
||||||
* know about */
|
k = inbuf[start];
|
||||||
|
|
||||||
INC_POINTER(start);
|
INC_POINTER(start);
|
||||||
working = start;
|
working = start;
|
||||||
|
@ -420,20 +415,23 @@ reread:
|
||||||
start = working; /* eat the key sequence
|
start = working; /* eat the key sequence
|
||||||
* in inbuf */
|
* in inbuf */
|
||||||
|
|
||||||
if (start == end) { /* check if inbuf empty
|
/* check if inbuf empty now */
|
||||||
* now */
|
if (start == end) {
|
||||||
state = INKEY_NORM; /* if it is go
|
/* if it is go back to normal */
|
||||||
back to normal */
|
state = INKEY_NORM;
|
||||||
} else { /* otherwise go to backout
|
} else {
|
||||||
* state */
|
/* otherwise go to backout state */
|
||||||
state = INKEY_BACKOUT;
|
state = INKEY_BACKOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return the symbol */
|
/* return the symbol */
|
||||||
return current->key[current->mapping[k]]->value.symbol;
|
return current->key[current->mapping[k]]->value.symbol;
|
||||||
|
|
||||||
} else {/* step on to next part of the multi-key
|
} else {
|
||||||
* sequence */
|
/*
|
||||||
|
* Step on to next part of the multi-key
|
||||||
|
* sequence.
|
||||||
|
*/
|
||||||
current = current->key[current->mapping[k]]->value.next;
|
current = current->key[current->mapping[k]]->value.next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -500,9 +498,10 @@ wgetch(win)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ((nchar = read(STDIN_FILENO, &c, sizeof(char))) < 0)
|
|
||||||
|
if ((nchar = read(STDIN_FILENO, &c, sizeof(char))) < 0) {
|
||||||
inp = ERR;
|
inp = ERR;
|
||||||
else {
|
} else {
|
||||||
if (nchar == 0) {
|
if (nchar == 0) {
|
||||||
__restore_termios();
|
__restore_termios();
|
||||||
return ERR; /* we have timed out */
|
return ERR; /* we have timed out */
|
||||||
|
@ -513,11 +512,13 @@ wgetch(win)
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
__CTRACE("wgetch got '%s'\n", unctrl(inp));
|
__CTRACE("wgetch got '%s'\n", unctrl(inp));
|
||||||
#endif
|
#endif
|
||||||
if (win->delay > -1)
|
if (win->delay > -1) {
|
||||||
if (__delay() == ERR) {
|
if (__delay() == ERR) {
|
||||||
__restore_termios();
|
__restore_termios();
|
||||||
return ERR;
|
return ERR;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
__restore_termios();
|
__restore_termios();
|
||||||
if (__echoit) {
|
if (__echoit) {
|
||||||
mvwaddch(curscr,
|
mvwaddch(curscr,
|
||||||
|
@ -526,5 +527,6 @@ wgetch(win)
|
||||||
}
|
}
|
||||||
if (weset)
|
if (weset)
|
||||||
nocbreak();
|
nocbreak();
|
||||||
|
|
||||||
return ((inp < 0) || (inp == ERR) ? ERR : inp);
|
return ((inp < 0) || (inp == ERR) ? ERR : inp);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue