summaryrefslogtreecommitdiffstats
path: root/lib/libedit/key.c
diff options
context:
space:
mode:
authorobrien <obrien@FreeBSD.org>2001-10-01 08:41:27 +0000
committerobrien <obrien@FreeBSD.org>2001-10-01 08:41:27 +0000
commit90300f853673b3879ab4d6a60683ff69b564c58b (patch)
treef9436ba88ca8f8420af319b0a12dd175381b507b /lib/libedit/key.c
parentf805e363ed51ace3356b37eb173c3d6435c35022 (diff)
downloadFreeBSD-src-90300f853673b3879ab4d6a60683ff69b564c58b.zip
FreeBSD-src-90300f853673b3879ab4d6a60683ff69b564c58b.tar.gz
+ Sync with NetBSD, bringing in feature enhancements.
+ Convert to ANSI-C function definitions + style(9) Submitted by: kris
Diffstat (limited to 'lib/libedit/key.c')
-rw-r--r--lib/libedit/key.c862
1 files changed, 409 insertions, 453 deletions
diff --git a/lib/libedit/key.c b/lib/libedit/key.c
index 027ebe7..dcb0b66 100644
--- a/lib/libedit/key.c
+++ b/lib/libedit/key.c
@@ -32,6 +32,8 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
+ *
+ * $NetBSD: key.c,v 1.11 2001/01/23 15:55:30 jdolecek Exp $
*/
#include <sys/cdefs.h>
@@ -39,6 +41,8 @@ __FBSDID("$FreeBSD$");
#if !defined(lint) && !defined(SCCSID)
static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
#endif /* not lint && not SCCSID */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
/*
* key.c: This module contains the procedures for maintaining
@@ -72,40 +76,41 @@ static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
* of these node elements
*/
struct key_node_t {
- char ch; /* single character of key */
- int type; /* node type */
- key_value_t val; /* command code or pointer to str, */
- /* if this is a leaf */
- struct key_node_t *next; /* ptr to next char of this key */
- struct key_node_t *sibling; /* ptr to another key with same prefix */
+ char ch; /* single character of key */
+ int type; /* node type */
+ key_value_t val; /* command code or pointer to str, */
+ /* if this is a leaf */
+ struct key_node_t *next; /* ptr to next char of this key */
+ struct key_node_t *sibling; /* ptr to another key with same prefix*/
};
-private int node_trav __P((EditLine *, key_node_t *, char *,
- key_value_t *));
-private int node__try __P((key_node_t *, char *,
- key_value_t *, int));
-private key_node_t *node__get __P((int));
-private void node__put __P((key_node_t *));
-private int node__delete __P((key_node_t **, char *));
-private int node_lookup __P((EditLine *, char *, key_node_t *,
- int));
-private int node_enum __P((EditLine *, key_node_t *, int));
-private int key__decode_char __P((char *, int, int));
+private int node_trav(EditLine *, key_node_t *, char *,
+ key_value_t *);
+private int node__try(EditLine *, key_node_t *, const char *,
+ key_value_t *, int);
+private key_node_t *node__get(int);
+private void node__put(EditLine *, key_node_t *);
+private int node__delete(EditLine *, key_node_t **, char *);
+private int node_lookup(EditLine *, char *, key_node_t *, int);
+private int node_enum(EditLine *, key_node_t *, int);
+private int key__decode_char(char *, int, int);
-#define KEY_BUFSIZ EL_BUFSIZ
+#define KEY_BUFSIZ EL_BUFSIZ
/* key_init():
* Initialize the key maps
*/
protected int
-key_init(el)
- EditLine *el;
+key_init(EditLine *el)
{
- el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
- el->el_key.map = NULL;
- key_reset(el);
- return 0;
+
+ el->el_key.buf = (char *) el_malloc(KEY_BUFSIZ);
+ if (el->el_key.buf == NULL)
+ return (-1);
+ el->el_key.map = NULL;
+ key_reset(el);
+ return (0);
}
@@ -113,13 +118,13 @@ key_init(el)
* Free the key maps
*/
protected void
-key_end(el)
- EditLine *el;
+key_end(EditLine *el)
{
- el_free((ptr_t) el->el_key.buf);
- el->el_key.buf = NULL;
- /* XXX: provide a function to clear the keys */
- el->el_key.map = NULL;
+
+ el_free((ptr_t) el->el_key.buf);
+ el->el_key.buf = NULL;
+ /* XXX: provide a function to clear the keys */
+ el->el_key.map = NULL;
}
@@ -127,12 +132,11 @@ key_end(el)
* Associate cmd with a key value
*/
protected key_value_t *
-key_map_cmd(el, cmd)
- EditLine *el;
- int cmd;
+key_map_cmd(EditLine *el, int cmd)
{
- el->el_key.val.cmd = (el_action_t) cmd;
- return &el->el_key.val;
+
+ el->el_key.val.cmd = (el_action_t) cmd;
+ return (&el->el_key.val);
}
@@ -140,12 +144,11 @@ key_map_cmd(el, cmd)
* Associate str with a key value
*/
protected key_value_t *
-key_map_str(el, str)
- EditLine *el;
- char *str;
+key_map_str(EditLine *el, char *str)
{
- el->el_key.val.str = str;
- return &el->el_key.val;
+
+ el->el_key.val.str = str;
+ return (&el->el_key.val);
}
@@ -155,12 +158,12 @@ key_map_str(el, str)
* [Always bind the ansi arrow keys?]
*/
protected void
-key_reset(el)
- EditLine *el;
+key_reset(EditLine *el)
{
- node__put(el->el_key.map);
- el->el_key.map = NULL;
- return;
+
+ node__put(el, el->el_key.map);
+ el->el_key.map = NULL;
+ return;
}
@@ -173,14 +176,11 @@ key_reset(el)
* The last character read is returned in *ch.
*/
protected int
-key_get(el, ch, val)
- EditLine *el;
- char *ch;
- key_value_t *val;
+key_get(EditLine *el, char *ch, key_value_t *val)
{
- return node_trav(el, el->el_key.map, ch, val);
-}
+ return (node_trav(el, el->el_key.map, ch, val));
+}
/* key_add():
@@ -190,31 +190,27 @@ key_get(el, ch, val)
* out str or a unix command.
*/
protected void
-key_add(el, key, val, ntype)
- EditLine *el;
- char *key;
- key_value_t *val;
- int ntype;
+key_add(EditLine *el, const char *key, key_value_t *val, int ntype)
{
- if (key[0] == '\0') {
- (void) fprintf(el->el_errfile,
- "key_add: Null extended-key not allowed.\n");
- return;
- }
- if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
- (void) fprintf(el->el_errfile,
- "key_add: sequence-lead-in command not allowed\n");
- return;
- }
-
- if (el->el_key.map == NULL)
- /* tree is initially empty. Set up new node to match key[0] */
- el->el_key.map = node__get(key[0]); /* it is properly initialized */
+ if (key[0] == '\0') {
+ (void) fprintf(el->el_errfile,
+ "key_add: Null extended-key not allowed.\n");
+ return;
+ }
+ if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
+ (void) fprintf(el->el_errfile,
+ "key_add: sequence-lead-in command not allowed\n");
+ return;
+ }
+ if (el->el_key.map == NULL)
+ /* tree is initially empty. Set up new node to match key[0] */
+ el->el_key.map = node__get(key[0]);
+ /* it is properly initialized */
- /* Now recurse through el->el_key.map */
- (void) node__try(el->el_key.map, key, val, ntype);
- return;
+ /* Now recurse through el->el_key.map */
+ (void) node__try(el, el->el_key.map, key, val, ntype);
+ return;
}
@@ -222,17 +218,15 @@ key_add(el, key, val, ntype)
*
*/
protected void
-key_clear(el, map, in)
- EditLine *el;
- el_action_t *map;
- char *in;
+key_clear(EditLine *el, el_action_t *map, char *in)
{
- if ((map[(unsigned char) *in] == ED_SEQUENCE_LEAD_IN) &&
- ((map == el->el_map.key &&
- el->el_map.alt[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN) ||
- (map == el->el_map.alt &&
- el->el_map.key[(unsigned char) *in] != ED_SEQUENCE_LEAD_IN)))
- (void) key_delete(el, in);
+
+ if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
+ ((map == el->el_map.key &&
+ el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
+ (map == el->el_map.alt &&
+ el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
+ (void) key_delete(el, in);
}
@@ -241,21 +235,19 @@ key_clear(el, map, in)
* they exists.
*/
protected int
-key_delete(el, key)
- EditLine *el;
- char *key;
+key_delete(EditLine *el, char *key)
{
- if (key[0] == '\0') {
- (void) fprintf(el->el_errfile,
- "key_delete: Null extended-key not allowed.\n");
- return -1;
- }
- if (el->el_key.map == NULL)
- return 0;
+ if (key[0] == '\0') {
+ (void) fprintf(el->el_errfile,
+ "key_delete: Null extended-key not allowed.\n");
+ return (-1);
+ }
+ if (el->el_key.map == NULL)
+ return (0);
- (void) node__delete(&el->el_key.map, key);
- return 0;
+ (void) node__delete(el, &el->el_key.map, key);
+ return (0);
}
@@ -264,19 +256,19 @@ key_delete(el, key)
* Print entire el->el_key.map if null
*/
protected void
-key_print(el, key)
- EditLine *el;
- char *key;
+key_print(EditLine *el, char *key)
{
- /* do nothing if el->el_key.map is empty and null key specified */
- if (el->el_key.map == NULL && *key == 0)
- return;
- el->el_key.buf[0] = '"';
- if (node_lookup(el, key, el->el_key.map, 1) <= -1)
- /* key is not bound */
- (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n", key);
- return;
+ /* do nothing if el->el_key.map is empty and null key specified */
+ if (el->el_key.map == NULL && *key == 0)
+ return;
+
+ el->el_key.buf[0] = '"';
+ if (node_lookup(el, key, el->el_key.map, 1) <= -1)
+ /* key is not bound */
+ (void) fprintf(el->el_errfile, "Unbound extended key \"%s\"\n",
+ key);
+ return;
}
@@ -285,41 +277,36 @@ key_print(el, key)
* found. May read in more characters.
*/
private int
-node_trav(el, ptr, ch, val)
- EditLine *el;
- key_node_t *ptr;
- char *ch;
- key_value_t *val;
+node_trav(EditLine *el, key_node_t *ptr, char *ch, key_value_t *val)
{
- if (ptr->ch == *ch) {
- /* match found */
- if (ptr->next) {
- /* key not complete so get next char */
- if (el_getc(el, ch) != 1) { /* if EOF or error */
- val->cmd = ED_END_OF_FILE;
- return XK_CMD;/* PWP: Pretend we just read an end-of-file */
- }
- return node_trav(el, ptr->next, ch, val);
- }
- else {
- *val = ptr->val;
- if (ptr->type != XK_CMD)
- *ch = '\0';
- return ptr->type;
- }
- }
- else {
- /* no match found here */
- if (ptr->sibling) {
- /* try next sibling */
- return node_trav(el, ptr->sibling, ch, val);
- }
- else {
- /* no next sibling -- mismatch */
- val->str = NULL;
- return XK_STR;
+
+ if (ptr->ch == *ch) {
+ /* match found */
+ if (ptr->next) {
+ /* key not complete so get next char */
+ if (el_getc(el, ch) != 1) { /* if EOF or error */
+ val->cmd = ED_END_OF_FILE;
+ return (XK_CMD);
+ /* PWP: Pretend we just read an end-of-file */
+ }
+ return (node_trav(el, ptr->next, ch, val));
+ } else {
+ *val = ptr->val;
+ if (ptr->type != XK_CMD)
+ *ch = '\0';
+ return (ptr->type);
+ }
+ } else {
+ /* no match found here */
+ if (ptr->sibling) {
+ /* try next sibling */
+ return (node_trav(el, ptr->sibling, ch, val));
+ } else {
+ /* no next sibling -- mismatch */
+ val->str = NULL;
+ return (XK_STR);
+ }
}
- }
}
@@ -327,63 +314,60 @@ node_trav(el, ptr, ch, val)
* Find a node that matches *str or allocate a new one
*/
private int
-node__try(ptr, str, val, ntype)
- key_node_t *ptr;
- char *str;
- key_value_t *val;
- int ntype;
+node__try(EditLine *el, key_node_t *ptr, const char *str, key_value_t *val, int ntype)
{
- if (ptr->ch != *str) {
- key_node_t *xm;
- for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
- if (xm->sibling->ch == *str)
- break;
- if (xm->sibling == NULL)
- xm->sibling = node__get(*str); /* setup new node */
- ptr = xm->sibling;
- }
+ if (ptr->ch != *str) {
+ key_node_t *xm;
- if (*++str == '\0') {
- /* we're there */
- if (ptr->next != NULL) {
- node__put(ptr->next); /* lose longer keys with this prefix */
- ptr->next = NULL;
- }
- switch (ptr->type) {
- case XK_CMD:
- case XK_NOD:
- break;
- case XK_STR:
- case XK_EXE:
- if (ptr->val.str)
- el_free((ptr_t) ptr->val.str);
- break;
- default:
- abort();
- break;
+ for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
+ if (xm->sibling->ch == *str)
+ break;
+ if (xm->sibling == NULL)
+ xm->sibling = node__get(*str); /* setup new node */
+ ptr = xm->sibling;
}
+ if (*++str == '\0') {
+ /* we're there */
+ if (ptr->next != NULL) {
+ node__put(el, ptr->next);
+ /* lose longer keys with this prefix */
+ ptr->next = NULL;
+ }
+ switch (ptr->type) {
+ case XK_CMD:
+ case XK_NOD:
+ break;
+ case XK_STR:
+ case XK_EXE:
+ if (ptr->val.str)
+ el_free((ptr_t) ptr->val.str);
+ break;
+ default:
+ EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
+ ptr->type));
+ break;
+ }
- switch (ptr->type = ntype) {
- case XK_CMD:
- ptr->val = *val;
- break;
- case XK_STR:
- case XK_EXE:
- ptr->val.str = strdup(val->str);
- break;
- default:
- abort();
- break;
+ switch (ptr->type = ntype) {
+ case XK_CMD:
+ ptr->val = *val;
+ break;
+ case XK_STR:
+ case XK_EXE:
+ ptr->val.str = strdup(val->str);
+ break;
+ default:
+ EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
+ break;
+ }
+ } else {
+ /* still more chars to go */
+ if (ptr->next == NULL)
+ ptr->next = node__get(*str); /* setup new node */
+ (void) node__try(el, ptr->next, str, val, ntype);
}
- }
- else {
- /* still more chars to go */
- if (ptr->next == NULL)
- ptr->next = node__get(*str); /* setup new node */
- (void) node__try(ptr->next, str, val, ntype);
- }
- return 0;
+ return (0);
}
@@ -391,84 +375,79 @@ node__try(ptr, str, val, ntype)
* Delete node that matches str
*/
private int
-node__delete(inptr, str)
- key_node_t **inptr;
- char *str;
+node__delete(EditLine *el, key_node_t **inptr, char *str)
{
- key_node_t *ptr;
- key_node_t *prev_ptr = NULL;
+ key_node_t *ptr;
+ key_node_t *prev_ptr = NULL;
- ptr = *inptr;
+ ptr = *inptr;
- if (ptr->ch != *str) {
- key_node_t *xm;
+ if (ptr->ch != *str) {
+ key_node_t *xm;
- for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
- if (xm->sibling->ch == *str)
- break;
- if (xm->sibling == NULL)
- return 0;
- prev_ptr = xm;
- ptr = xm->sibling;
- }
-
- if (*++str == '\0') {
- /* we're there */
- if (prev_ptr == NULL)
- *inptr = ptr->sibling;
- else
- prev_ptr->sibling = ptr->sibling;
- ptr->sibling = NULL;
- node__put(ptr);
- return 1;
- }
- else if (ptr->next != NULL && node__delete(&ptr->next, str) == 1) {
- if (ptr->next != NULL)
- return 0;
- if (prev_ptr == NULL)
- *inptr = ptr->sibling;
- else
- prev_ptr->sibling = ptr->sibling;
- ptr->sibling = NULL;
- node__put(ptr);
- return 1;
- }
- else {
- return 0;
- }
+ for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
+ if (xm->sibling->ch == *str)
+ break;
+ if (xm->sibling == NULL)
+ return (0);
+ prev_ptr = xm;
+ ptr = xm->sibling;
+ }
+ if (*++str == '\0') {
+ /* we're there */
+ if (prev_ptr == NULL)
+ *inptr = ptr->sibling;
+ else
+ prev_ptr->sibling = ptr->sibling;
+ ptr->sibling = NULL;
+ node__put(el, ptr);
+ return (1);
+ } else if (ptr->next != NULL &&
+ node__delete(el, &ptr->next, str) == 1) {
+ if (ptr->next != NULL)
+ return (0);
+ if (prev_ptr == NULL)
+ *inptr = ptr->sibling;
+ else
+ prev_ptr->sibling = ptr->sibling;
+ ptr->sibling = NULL;
+ node__put(el, ptr);
+ return (1);
+ } else {
+ return (0);
+ }
}
+
/* node__put():
* Puts a tree of nodes onto free list using free(3).
*/
private void
-node__put(ptr)
- key_node_t *ptr;
+node__put(EditLine *el, key_node_t *ptr)
{
- if (ptr == NULL)
- return;
+ if (ptr == NULL)
+ return;
- if (ptr->next != NULL) {
- node__put(ptr->next);
- ptr->next = NULL;
- }
-
- node__put(ptr->sibling);
-
- switch (ptr->type) {
- case XK_CMD:
- case XK_NOD:
- break;
- case XK_EXE:
- case XK_STR:
- if (ptr->val.str != NULL)
- el_free((ptr_t) ptr->val.str);
- break;
- default:
- abort();
- break;
- }
- el_free((ptr_t) ptr);
+ if (ptr->next != NULL) {
+ node__put(el, ptr->next);
+ ptr->next = NULL;
+ }
+ node__put(el, ptr->sibling);
+
+ switch (ptr->type) {
+ case XK_CMD:
+ case XK_NOD:
+ break;
+ case XK_EXE:
+ case XK_STR:
+ if (ptr->val.str != NULL)
+ el_free((ptr_t) ptr->val.str);
+ break;
+ default:
+ EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
+ break;
+ }
+ el_free((ptr_t) ptr);
}
@@ -476,18 +455,19 @@ node__put(ptr)
* Returns pointer to an key_node_t for ch.
*/
private key_node_t *
-node__get(ch)
- int ch;
+node__get(int ch)
{
- key_node_t *ptr;
-
- ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
- ptr->ch = ch;
- ptr->type = XK_NOD;
- ptr->val.str = NULL;
- ptr->next = NULL;
- ptr->sibling = NULL;
- return ptr;
+ key_node_t *ptr;
+
+ ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t));
+ if (ptr == NULL)
+ return NULL;
+ ptr->ch = ch;
+ ptr->type = XK_NOD;
+ ptr->val.str = NULL;
+ ptr->next = NULL;
+ ptr->sibling = NULL;
+ return (ptr);
}
@@ -497,51 +477,48 @@ node__get(ch)
* Print if last node
*/
private int
-node_lookup(el, str, ptr, cnt)
- EditLine *el;
- char *str;
- key_node_t *ptr;
- int cnt;
+node_lookup(EditLine *el, char *str, key_node_t *ptr, int cnt)
{
- int ncnt;
-
- if (ptr == NULL)
- return -1; /* cannot have null ptr */
-
- if (*str == 0) {
- /* no more chars in str. node_enum from here. */
- (void) node_enum(el, ptr, cnt);
- return 0;
- }
- else {
- /* If match put this char into el->el_key.buf. Recurse */
- if (ptr->ch == *str) {
- /* match found */
- ncnt = key__decode_char(el->el_key.buf, cnt,
- (unsigned char) ptr->ch);
- if (ptr->next != NULL)
- /* not yet at leaf */
- return node_lookup(el, str + 1, ptr->next, ncnt + 1);
- else {
- /* next node is null so key should be complete */
- if (str[1] == 0) {
- el->el_key.buf[ncnt + 1] = '"';
- el->el_key.buf[ncnt + 2] = '\0';
- key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
- return 0;
+ int ncnt;
+
+ if (ptr == NULL)
+ return (-1); /* cannot have null ptr */
+
+ if (*str == 0) {
+ /* no more chars in str. node_enum from here. */
+ (void) node_enum(el, ptr, cnt);
+ return (0);
+ } else {
+ /* If match put this char into el->el_key.buf. Recurse */
+ if (ptr->ch == *str) {
+ /* match found */
+ ncnt = key__decode_char(el->el_key.buf, cnt,
+ (unsigned char) ptr->ch);
+ if (ptr->next != NULL)
+ /* not yet at leaf */
+ return (node_lookup(el, str + 1, ptr->next,
+ ncnt + 1));
+ else {
+ /* next node is null so key should be complete */
+ if (str[1] == 0) {
+ el->el_key.buf[ncnt + 1] = '"';
+ el->el_key.buf[ncnt + 2] = '\0';
+ key_kprint(el, el->el_key.buf,
+ &ptr->val, ptr->type);
+ return (0);
+ } else
+ return (-1);
+ /* mismatch -- str still has chars */
+ }
+ } else {
+ /* no match found try sibling */
+ if (ptr->sibling)
+ return (node_lookup(el, str, ptr->sibling,
+ cnt));
+ else
+ return (-1);
}
- else
- return -1;/* mismatch -- str still has chars */
- }
- }
- else {
- /* no match found try sibling */
- if (ptr->sibling)
- return node_lookup(el, str, ptr->sibling, cnt);
- else
- return -1;
}
- }
}
@@ -549,44 +526,39 @@ node_lookup(el, str, ptr, cnt)
* Traverse the node printing the characters it is bound in buffer
*/
private int
-node_enum(el, ptr, cnt)
- EditLine *el;
- key_node_t *ptr;
- int cnt;
+node_enum(EditLine *el, key_node_t *ptr, int cnt)
{
- int ncnt;
+ int ncnt;
- if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
- el->el_key.buf[++cnt] = '"';
- el->el_key.buf[++cnt] = '\0';
- (void) fprintf(el->el_errfile,
+ if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
+ el->el_key.buf[++cnt] = '"';
+ el->el_key.buf[++cnt] = '\0';
+ (void) fprintf(el->el_errfile,
"Some extended keys too long for internal print buffer");
- (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
- return 0;
- }
-
- if (ptr == NULL) {
+ (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf);
+ return (0);
+ }
+ if (ptr == NULL) {
#ifdef DEBUG_EDIT
- (void) fprintf(el->el_errfile, "node_enum: BUG!! Null ptr passed\n!");
+ (void) fprintf(el->el_errfile,
+ "node_enum: BUG!! Null ptr passed\n!");
#endif
- return -1;
- }
-
- /* put this char at end of str */
- ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
- if (ptr->next == NULL) {
- /* print this key and function */
- el->el_key.buf[ncnt + 1] = '"';
- el->el_key.buf[ncnt + 2] = '\0';
- key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
- }
- else
- (void) node_enum(el, ptr->next, ncnt + 1);
-
- /* go to sibling if there is one */
- if (ptr->sibling)
- (void) node_enum(el, ptr->sibling, cnt);
- return 0;
+ return (-1);
+ }
+ /* put this char at end of str */
+ ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch);
+ if (ptr->next == NULL) {
+ /* print this key and function */
+ el->el_key.buf[ncnt + 1] = '"';
+ el->el_key.buf[ncnt + 2] = '\0';
+ key_kprint(el, el->el_key.buf, &ptr->val, ptr->type);
+ } else
+ (void) node_enum(el, ptr->next, ncnt + 1);
+
+ /* go to sibling if there is one */
+ if (ptr->sibling)
+ (void) node_enum(el, ptr->sibling, cnt);
+ return (0);
}
@@ -595,42 +567,40 @@ node_enum(el, ptr, cnt)
* function specified by val
*/
protected void
-key_kprint(el, key, val, ntype)
- EditLine *el;
- char *key;
- key_value_t *val;
- int ntype;
+key_kprint(EditLine *el, char *key, key_value_t *val, int ntype)
{
- el_bindings_t *fp;
- char unparsbuf[EL_BUFSIZ];
- static const char fmt[] = "%-15s-> %s\n";
-
- if (val != NULL)
- switch (ntype) {
- case XK_STR:
- case XK_EXE:
- (void) fprintf(el->el_errfile, fmt, key,
- key__decode_str(val->str, unparsbuf,
- ntype == XK_STR ? "\"\"" : "[]"));
- break;
- case XK_CMD:
- for (fp = el->el_map.help; fp->name; fp++)
- if (val->cmd == fp->func) {
- (void) fprintf(el->el_errfile, fmt, key, fp->name);
- break;
- }
+ el_bindings_t *fp;
+ char unparsbuf[EL_BUFSIZ];
+ static const char fmt[] = "%-15s-> %s\n";
+
+ if (val != NULL)
+ switch (ntype) {
+ case XK_STR:
+ case XK_EXE:
+ (void) fprintf(el->el_outfile, fmt, key,
+ key__decode_str(val->str, unparsbuf,
+ ntype == XK_STR ? "\"\"" : "[]"));
+ break;
+ case XK_CMD:
+ for (fp = el->el_map.help; fp->name; fp++)
+ if (val->cmd == fp->func) {
+ (void) fprintf(el->el_outfile, fmt,
+ key, fp->name);
+ break;
+ }
#ifdef DEBUG_KEY
- if (fp->name == NULL)
- (void) fprintf(el->el_errfile, "BUG! Command not found.\n");
+ if (fp->name == NULL)
+ (void) fprintf(el->el_outfile,
+ "BUG! Command not found.\n");
#endif
- break;
- default:
- abort();
- break;
- }
- else
- (void) fprintf(el->el_errfile, fmt, key, "no input");
+ break;
+ default:
+ EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
+ break;
+ }
+ else
+ (void) fprintf(el->el_outfile, fmt, key, "no input");
}
@@ -638,93 +608,79 @@ key_kprint(el, key, val, ntype)
* Put a printable form of char in buf.
*/
private int
-key__decode_char(buf, cnt, ch)
- char *buf;
- int cnt, ch;
+key__decode_char(char *buf, int cnt, int ch)
{
- ch = (unsigned char)ch;
-
- if (ch == 0) {
- buf[cnt++] = '^';
- buf[cnt] = '@';
- return cnt;
- }
-
- if (iscntrl(ch)) {
- buf[cnt++] = '^';
- if (ch == 0177)
- buf[cnt] = '?';
- else
- buf[cnt] = toascii(ch) | 0100;
- }
- else if (ch == '^') {
- buf[cnt++] = '\\';
- buf[cnt] = '^';
- }
- else if (ch == '\\') {
- buf[cnt++] = '\\';
- buf[cnt] = '\\';
- }
- else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
- buf[cnt] = ch;
- }
- else {
- buf[cnt++] = '\\';
- buf[cnt++] = ((ch >> 6) & 7) + '0';
- buf[cnt++] = ((ch >> 3) & 7) + '0';
- buf[cnt] = (ch & 7) + '0';
- }
- return cnt;
+ ch = (unsigned char)ch;
+
+ if (ch == 0) {
+ buf[cnt++] = '^';
+ buf[cnt] = '@';
+ return (cnt);
+ }
+ if (iscntrl(ch)) {
+ buf[cnt++] = '^';
+ if (ch == 0177)
+ buf[cnt] = '?';
+ else
+ buf[cnt] = toascii(ch) | 0100;
+ } else if (ch == '^') {
+ buf[cnt++] = '\\';
+ buf[cnt] = '^';
+ } else if (ch == '\\') {
+ buf[cnt++] = '\\';
+ buf[cnt] = '\\';
+ } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) {
+ buf[cnt] = ch;
+ } else {
+ buf[cnt++] = '\\';
+ buf[cnt++] = (((unsigned int) ch >> 6) & 7) + '0';
+ buf[cnt++] = (((unsigned int) ch >> 3) & 7) + '0';
+ buf[cnt] = (ch & 7) + '0';
+ }
+ return (cnt);
}
/* key__decode_str():
* Make a printable version of the ey
*/
protected char *
-key__decode_str(str, buf, sep)
- char *str;
- char *buf;
- char *sep;
+key__decode_str(char *str, char *buf, char *sep)
{
- char *b, *p;
-
- b = buf;
- if (sep[0] != '\0')
- *b++ = sep[0];
- if (*str == 0) {
- *b++ = '^';
- *b++ = '@';
- if (sep[0] != '\0' && sep[1] != '\0')
- *b++ = sep[1];
- *b++ = 0;
- return buf;
- }
-
- for (p = str; *p != 0; p++) {
- if (iscntrl((unsigned char) *p)) {
- *b++ = '^';
- if (*p == '\177')
- *b++ = '?';
- else
- *b++ = toascii(*p) | 0100;
- }
- else if (*p == '^' || *p == '\\') {
- *b++ = '\\';
- *b++ = *p;
- }
- else if (*p == ' ' || (isprint((unsigned char) *p) &&
- !isspace((unsigned char) *p))) {
- *b++ = *p;
+ char *b, *p;
+
+ b = buf;
+ if (sep[0] != '\0')
+ *b++ = sep[0];
+ if (*str == 0) {
+ *b++ = '^';
+ *b++ = '@';
+ if (sep[0] != '\0' && sep[1] != '\0')
+ *b++ = sep[1];
+ *b++ = 0;
+ return (buf);
}
- else {
- *b++ = '\\';
- *b++ = ((*p >> 6) & 7) + '0';
- *b++ = ((*p >> 3) & 7) + '0';
- *b++ = (*p & 7) + '0';
+ for (p = str; *p != 0; p++) {
+ if (iscntrl((unsigned char) *p)) {
+ *b++ = '^';
+ if (*p == '\177')
+ *b++ = '?';
+ else
+ *b++ = toascii(*p) | 0100;
+ } else if (*p == '^' || *p == '\\') {
+ *b++ = '\\';
+ *b++ = *p;
+ } else if (*p == ' ' || (isprint((unsigned char) *p) &&
+ !isspace((unsigned char) *p))) {
+ *b++ = *p;
+ } else {
+ *b++ = '\\';
+ *b++ = (((unsigned int) *p >> 6) & 7) + '0';
+ *b++ = (((unsigned int) *p >> 3) & 7) + '0';
+ *b++ = (*p & 7) + '0';
+ }
}
- }
- if (sep[0] != '\0' && sep[1] != '\0')
- *b++ = sep[1];
- *b++ = 0;
- return buf; /* should check for overflow */
+ if (sep[0] != '\0' && sep[1] != '\0')
+ *b++ = sep[1];
+ *b++ = 0;
+ return (buf); /* should check for overflow */
}
OpenPOWER on IntegriCloud