2015-01-09 07:40:56 +00:00
|
|
|
/* $NetBSD: keymacro.c,v 1.7 2011/08/16 16:25:15 christos Exp $ */
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Christos Zoulas of Cornell University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2005-08-07 20:55:59 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 05:00:24 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
#include "config.h"
|
1994-05-27 05:00:24 +00:00
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
2015-01-09 07:40:56 +00:00
|
|
|
#if 0
|
1994-05-27 05:00:24 +00:00
|
|
|
static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93";
|
2015-01-09 07:40:56 +00:00
|
|
|
#else
|
|
|
|
__RCSID("$NetBSD: keymacro.c,v 1.7 2011/08/16 16:25:15 christos Exp $");
|
|
|
|
#endif
|
1994-05-27 05:00:24 +00:00
|
|
|
#endif /* not lint && not SCCSID */
|
2001-10-01 08:41:27 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
/*
|
2015-01-09 07:40:56 +00:00
|
|
|
* keymacro.c: This module contains the procedures for maintaining
|
|
|
|
* the extended-key map.
|
1994-05-27 05:00:24 +00:00
|
|
|
*
|
1995-05-30 05:51:47 +00:00
|
|
|
* An extended-key (key) is a sequence of keystrokes introduced
|
2002-12-30 21:18:15 +00:00
|
|
|
* with a sequence introducer and consisting of an arbitrary
|
2015-01-09 07:40:56 +00:00
|
|
|
* number of characters. This module maintains a map (the
|
|
|
|
* el->el_keymacro.map)
|
1995-05-30 05:51:47 +00:00
|
|
|
* to convert these extended-key sequences into input strs
|
1994-05-27 05:00:24 +00:00
|
|
|
* (XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
|
|
|
|
*
|
|
|
|
* Warning:
|
|
|
|
* If key is a substr of some other keys, then the longer
|
|
|
|
* keys are lost!! That is, if the keys "abcd" and "abcef"
|
2015-01-09 07:40:56 +00:00
|
|
|
* are in el->el_keymacro.map, adding the key "abc" will cause
|
|
|
|
* the first two definitions to be lost.
|
1994-05-27 05:00:24 +00:00
|
|
|
*
|
|
|
|
* Restrictions:
|
|
|
|
* -------------
|
|
|
|
* 1) It is not possible to have one key that is a
|
|
|
|
* substr of another.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "el.h"
|
|
|
|
|
1995-05-30 05:51:47 +00:00
|
|
|
/*
|
2015-01-09 07:40:56 +00:00
|
|
|
* The Nodes of the el->el_keymacro.map. The el->el_keymacro.map is a
|
|
|
|
* linked list of these node elements
|
1994-05-27 05:00:24 +00:00
|
|
|
*/
|
2015-01-09 07:40:56 +00:00
|
|
|
struct keymacro_node_t {
|
|
|
|
Char ch; /* single character of key */
|
|
|
|
int type; /* node type */
|
|
|
|
keymacro_value_t val; /* command code or pointer to str, */
|
2001-10-01 08:41:27 +00:00
|
|
|
/* if this is a leaf */
|
2015-01-09 07:40:56 +00:00
|
|
|
struct keymacro_node_t *next; /* ptr to next char of this key */
|
|
|
|
struct keymacro_node_t *sibling;/* ptr to another key with same prefix*/
|
1994-05-27 05:00:24 +00:00
|
|
|
};
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
private int node_trav(EditLine *, keymacro_node_t *, Char *,
|
|
|
|
keymacro_value_t *);
|
|
|
|
private int node__try(EditLine *, keymacro_node_t *, const Char *,
|
|
|
|
keymacro_value_t *, int);
|
|
|
|
private keymacro_node_t *node__get(Int);
|
|
|
|
private void node__free(keymacro_node_t *);
|
|
|
|
private void node__put(EditLine *, keymacro_node_t *);
|
|
|
|
private int node__delete(EditLine *, keymacro_node_t **,
|
|
|
|
const Char *);
|
|
|
|
private int node_lookup(EditLine *, const Char *,
|
|
|
|
keymacro_node_t *, size_t);
|
|
|
|
private int node_enum(EditLine *, keymacro_node_t *, size_t);
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
#define KEY_BUFSIZ EL_BUFSIZ
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_init():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Initialize the key maps
|
|
|
|
*/
|
|
|
|
protected int
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_init(EditLine *el)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
el->el_keymacro.buf = el_malloc(KEY_BUFSIZ *
|
|
|
|
sizeof(*el->el_keymacro.buf));
|
|
|
|
if (el->el_keymacro.buf == NULL)
|
|
|
|
return -1;
|
|
|
|
el->el_keymacro.map = NULL;
|
|
|
|
keymacro_reset(el);
|
|
|
|
return 0;
|
1995-05-30 05:51:47 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_end():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Free the key maps
|
|
|
|
*/
|
|
|
|
protected void
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_end(EditLine *el)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
el_free(el->el_keymacro.buf);
|
|
|
|
el->el_keymacro.buf = NULL;
|
|
|
|
node__free(el->el_keymacro.map);
|
1995-05-30 05:51:47 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_map_cmd():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Associate cmd with a key value
|
|
|
|
*/
|
2015-01-09 07:40:56 +00:00
|
|
|
protected keymacro_value_t *
|
|
|
|
keymacro_map_cmd(EditLine *el, int cmd)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
el->el_keymacro.val.cmd = (el_action_t) cmd;
|
|
|
|
return &el->el_keymacro.val;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_map_str():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Associate str with a key value
|
|
|
|
*/
|
2015-01-09 07:40:56 +00:00
|
|
|
protected keymacro_value_t *
|
|
|
|
keymacro_map_str(EditLine *el, Char *str)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
el->el_keymacro.val.str = str;
|
|
|
|
return &el->el_keymacro.val;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_reset():
|
|
|
|
* Takes all nodes on el->el_keymacro.map and puts them on free list.
|
|
|
|
* Then initializes el->el_keymacro.map with arrow keys
|
1994-05-27 05:00:24 +00:00
|
|
|
* [Always bind the ansi arrow keys?]
|
|
|
|
*/
|
|
|
|
protected void
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_reset(EditLine *el)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
node__put(el, el->el_keymacro.map);
|
|
|
|
el->el_keymacro.map = NULL;
|
2001-10-01 08:41:27 +00:00
|
|
|
return;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_get():
|
|
|
|
* Calls the recursive function with entry point el->el_keymacro.map
|
1994-05-27 05:00:24 +00:00
|
|
|
* Looks up *ch in map and then reads characters until a
|
|
|
|
* complete match is found or a mismatch occurs. Returns the
|
|
|
|
* type of the match found (XK_STR, XK_CMD, or XK_EXE).
|
1995-05-30 05:51:47 +00:00
|
|
|
* Returns NULL in val.str and XK_STR for no match.
|
1994-05-27 05:00:24 +00:00
|
|
|
* The last character read is returned in *ch.
|
|
|
|
*/
|
|
|
|
protected int
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_get(EditLine *el, Char *ch, keymacro_value_t *val)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
return node_trav(el, el->el_keymacro.map, ch, val);
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_add():
|
|
|
|
* Adds key to the el->el_keymacro.map and associates the value in
|
|
|
|
* val with it. If key is already is in el->el_keymacro.map, the new
|
|
|
|
* code is applied to the existing key. Ntype specifies if code is a
|
|
|
|
* command, an out str or a unix command.
|
1994-05-27 05:00:24 +00:00
|
|
|
*/
|
|
|
|
protected void
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_add(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
if (key[0] == '\0') {
|
|
|
|
(void) fprintf(el->el_errfile,
|
2015-01-09 07:40:56 +00:00
|
|
|
"keymacro_add: Null extended-key not allowed.\n");
|
2001-10-01 08:41:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
|
|
|
|
(void) fprintf(el->el_errfile,
|
2015-01-09 07:40:56 +00:00
|
|
|
"keymacro_add: sequence-lead-in command not allowed\n");
|
2001-10-01 08:41:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
if (el->el_keymacro.map == NULL)
|
2001-10-01 08:41:27 +00:00
|
|
|
/* tree is initially empty. Set up new node to match key[0] */
|
2015-01-09 07:40:56 +00:00
|
|
|
el->el_keymacro.map = node__get(key[0]);
|
2001-10-01 08:41:27 +00:00
|
|
|
/* it is properly initialized */
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* Now recurse through el->el_keymacro.map */
|
|
|
|
(void) node__try(el, el->el_keymacro.map, key, val, ntype);
|
2001-10-01 08:41:27 +00:00
|
|
|
return;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_clear():
|
1994-05-27 05:00:24 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected void
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_clear(EditLine *el, el_action_t *map, const Char *in)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
#ifdef WIDECHAR
|
|
|
|
if (*in > N_KEYS) /* can't be in the map */
|
|
|
|
return;
|
|
|
|
#endif
|
2001-10-01 08:41:27 +00:00
|
|
|
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)))
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) keymacro_delete(el, in);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_delete():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Delete the key and all longer keys staring with key, if
|
|
|
|
* they exists.
|
|
|
|
*/
|
|
|
|
protected int
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_delete(EditLine *el, const Char *key)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
if (key[0] == '\0') {
|
|
|
|
(void) fprintf(el->el_errfile,
|
2015-01-09 07:40:56 +00:00
|
|
|
"keymacro_delete: Null extended-key not allowed.\n");
|
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
if (el->el_keymacro.map == NULL)
|
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) node__delete(el, &el->el_keymacro.map, key);
|
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_print():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Print the binding associated with key key.
|
2015-01-09 07:40:56 +00:00
|
|
|
* Print entire el->el_keymacro.map if null
|
1994-05-27 05:00:24 +00:00
|
|
|
*/
|
|
|
|
protected void
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_print(EditLine *el, const Char *key)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* do nothing if el->el_keymacro.map is empty and null key specified */
|
|
|
|
if (el->el_keymacro.map == NULL && *key == 0)
|
2001-10-01 08:41:27 +00:00
|
|
|
return;
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
el->el_keymacro.buf[0] = '"';
|
|
|
|
if (node_lookup(el, key, el->el_keymacro.map, (size_t)1) <= -1)
|
2001-10-01 08:41:27 +00:00
|
|
|
/* key is not bound */
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) fprintf(el->el_errfile, "Unbound extended key \"" FSTR
|
|
|
|
"\"\n", key);
|
2001-10-01 08:41:27 +00:00
|
|
|
return;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* node_trav():
|
|
|
|
* recursively traverses node in tree until match or mismatch is
|
|
|
|
* found. May read in more characters.
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
node_trav(EditLine *el, keymacro_node_t *ptr, Char *ch, keymacro_value_t *val)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if (ptr->ch == *ch) {
|
|
|
|
/* match found */
|
|
|
|
if (ptr->next) {
|
|
|
|
/* key not complete so get next char */
|
2015-01-09 07:40:56 +00:00
|
|
|
if (FUN(el,getc)(el, ch) != 1) {/* if EOF or error */
|
2001-10-01 08:41:27 +00:00
|
|
|
val->cmd = ED_END_OF_FILE;
|
2015-01-09 07:40:56 +00:00
|
|
|
return XK_CMD;
|
2001-10-01 08:41:27 +00:00
|
|
|
/* PWP: Pretend we just read an end-of-file */
|
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
return node_trav(el, ptr->next, ch, val);
|
2001-10-01 08:41:27 +00:00
|
|
|
} else {
|
|
|
|
*val = ptr->val;
|
|
|
|
if (ptr->type != XK_CMD)
|
|
|
|
*ch = '\0';
|
2015-01-09 07:40:56 +00:00
|
|
|
return ptr->type;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* no match found here */
|
|
|
|
if (ptr->sibling) {
|
|
|
|
/* try next sibling */
|
2015-01-09 07:40:56 +00:00
|
|
|
return node_trav(el, ptr->sibling, ch, val);
|
2001-10-01 08:41:27 +00:00
|
|
|
} else {
|
|
|
|
/* no next sibling -- mismatch */
|
|
|
|
val->str = NULL;
|
2015-01-09 07:40:56 +00:00
|
|
|
return XK_STR;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* node__try():
|
|
|
|
* Find a node that matches *str or allocate a new one
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
node__try(EditLine *el, keymacro_node_t *ptr, const Char *str,
|
|
|
|
keymacro_value_t *val, int ntype)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
if (ptr->ch != *str) {
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_node_t *xm;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
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;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2001-10-01 08:41:27 +00:00
|
|
|
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)
|
2015-01-09 07:40:56 +00:00
|
|
|
el_free(ptr->val.str);
|
2001-10-01 08:41:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
|
|
|
|
ptr->type));
|
|
|
|
break;
|
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
switch (ptr->type = ntype) {
|
|
|
|
case XK_CMD:
|
|
|
|
ptr->val = *val;
|
|
|
|
break;
|
|
|
|
case XK_STR:
|
|
|
|
case XK_EXE:
|
2015-01-09 07:40:56 +00:00
|
|
|
if ((ptr->val.str = Strdup(val->str)) == NULL)
|
2005-08-07 20:55:59 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
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);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* node__delete():
|
|
|
|
* Delete node that matches str
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
node__delete(EditLine *el, keymacro_node_t **inptr, const Char *str)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_node_t *ptr;
|
|
|
|
keymacro_node_t *prev_ptr = NULL;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
ptr = *inptr;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
if (ptr->ch != *str) {
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_node_t *xm;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
|
|
|
|
if (xm->sibling->ch == *str)
|
|
|
|
break;
|
|
|
|
if (xm->sibling == NULL)
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
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);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 1;
|
2001-10-01 08:41:27 +00:00
|
|
|
} else if (ptr->next != NULL &&
|
|
|
|
node__delete(el, &ptr->next, str) == 1) {
|
|
|
|
if (ptr->next != NULL)
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
if (prev_ptr == NULL)
|
|
|
|
*inptr = ptr->sibling;
|
|
|
|
else
|
|
|
|
prev_ptr->sibling = ptr->sibling;
|
|
|
|
ptr->sibling = NULL;
|
|
|
|
node__put(el, ptr);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 1;
|
2001-10-01 08:41:27 +00:00
|
|
|
} else {
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
/* node__put():
|
|
|
|
* Puts a tree of nodes onto free list using free(3).
|
|
|
|
*/
|
|
|
|
private void
|
2015-01-09 07:40:56 +00:00
|
|
|
node__put(EditLine *el, keymacro_node_t *ptr)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
return;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
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)
|
2015-01-09 07:40:56 +00:00
|
|
|
el_free(ptr->val.str);
|
2001-10-01 08:41:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
|
|
|
|
break;
|
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
el_free(ptr);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* node__get():
|
2015-01-09 07:40:56 +00:00
|
|
|
* Returns pointer to a keymacro_node_t for ch.
|
1994-05-27 05:00:24 +00:00
|
|
|
*/
|
2015-01-09 07:40:56 +00:00
|
|
|
private keymacro_node_t *
|
|
|
|
node__get(Int ch)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_node_t *ptr;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
ptr = el_malloc(sizeof(*ptr));
|
2001-10-01 08:41:27 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
return NULL;
|
|
|
|
ptr->ch = ch;
|
|
|
|
ptr->type = XK_NOD;
|
|
|
|
ptr->val.str = NULL;
|
|
|
|
ptr->next = NULL;
|
|
|
|
ptr->sibling = NULL;
|
2015-01-09 07:40:56 +00:00
|
|
|
return ptr;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2005-08-07 20:55:59 +00:00
|
|
|
private void
|
2015-01-09 07:40:56 +00:00
|
|
|
node__free(keymacro_node_t *k)
|
2005-08-07 20:55:59 +00:00
|
|
|
{
|
|
|
|
if (k == NULL)
|
|
|
|
return;
|
|
|
|
node__free(k->sibling);
|
|
|
|
node__free(k->next);
|
2015-01-09 07:40:56 +00:00
|
|
|
el_free(k);
|
2005-08-07 20:55:59 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
/* node_lookup():
|
|
|
|
* look for the str starting at node ptr.
|
|
|
|
* Print if last node
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
node_lookup(EditLine *el, const Char *str, keymacro_node_t *ptr, size_t cnt)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
ssize_t used;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
if (ptr == NULL)
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1; /* cannot have null ptr */
|
2001-10-01 08:41:27 +00:00
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
if (!str || *str == 0) {
|
2001-10-01 08:41:27 +00:00
|
|
|
/* no more chars in str. node_enum from here. */
|
|
|
|
(void) node_enum(el, ptr, cnt);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
} else {
|
2015-01-09 07:40:56 +00:00
|
|
|
/* If match put this char into el->el_keymacro.buf. Recurse */
|
2001-10-01 08:41:27 +00:00
|
|
|
if (ptr->ch == *str) {
|
|
|
|
/* match found */
|
2015-01-09 07:40:56 +00:00
|
|
|
used = ct_visual_char(el->el_keymacro.buf + cnt,
|
|
|
|
KEY_BUFSIZ - cnt, ptr->ch);
|
|
|
|
if (used == -1)
|
|
|
|
return -1; /* ran out of buffer space */
|
2001-10-01 08:41:27 +00:00
|
|
|
if (ptr->next != NULL)
|
|
|
|
/* not yet at leaf */
|
|
|
|
return (node_lookup(el, str + 1, ptr->next,
|
2015-01-09 07:40:56 +00:00
|
|
|
(size_t)used + cnt));
|
2001-10-01 08:41:27 +00:00
|
|
|
else {
|
|
|
|
/* next node is null so key should be complete */
|
|
|
|
if (str[1] == 0) {
|
2015-01-09 07:40:56 +00:00
|
|
|
size_t px = cnt + (size_t)used;
|
|
|
|
el->el_keymacro.buf[px] = '"';
|
|
|
|
el->el_keymacro.buf[px + 1] = '\0';
|
|
|
|
keymacro_kprint(el, el->el_keymacro.buf,
|
2001-10-01 08:41:27 +00:00
|
|
|
&ptr->val, ptr->type);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
} else
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
/* mismatch -- str still has chars */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* no match found try sibling */
|
|
|
|
if (ptr->sibling)
|
|
|
|
return (node_lookup(el, str, ptr->sibling,
|
|
|
|
cnt));
|
|
|
|
else
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* node_enum():
|
|
|
|
* Traverse the node printing the characters it is bound in buffer
|
|
|
|
*/
|
|
|
|
private int
|
2015-01-09 07:40:56 +00:00
|
|
|
node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2015-01-09 07:40:56 +00:00
|
|
|
ssize_t used;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */
|
2015-01-09 07:40:56 +00:00
|
|
|
el->el_keymacro.buf[++cnt] = '"';
|
|
|
|
el->el_keymacro.buf[++cnt] = '\0';
|
2001-10-01 08:41:27 +00:00
|
|
|
(void) fprintf(el->el_errfile,
|
1994-05-27 05:00:24 +00:00
|
|
|
"Some extended keys too long for internal print buffer");
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) fprintf(el->el_errfile, " \"" FSTR "...\"\n",
|
|
|
|
el->el_keymacro.buf);
|
|
|
|
return 0;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
if (ptr == NULL) {
|
1994-05-27 05:00:24 +00:00
|
|
|
#ifdef DEBUG_EDIT
|
2001-10-01 08:41:27 +00:00
|
|
|
(void) fprintf(el->el_errfile,
|
|
|
|
"node_enum: BUG!! Null ptr passed\n!");
|
1994-05-27 05:00:24 +00:00
|
|
|
#endif
|
2015-01-09 07:40:56 +00:00
|
|
|
return -1;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
|
|
|
/* put this char at end of str */
|
2015-01-09 07:40:56 +00:00
|
|
|
used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt,
|
|
|
|
ptr->ch);
|
2001-10-01 08:41:27 +00:00
|
|
|
if (ptr->next == NULL) {
|
|
|
|
/* print this key and function */
|
2015-01-09 07:40:56 +00:00
|
|
|
el->el_keymacro.buf[cnt + (size_t)used ] = '"';
|
|
|
|
el->el_keymacro.buf[cnt + (size_t)used + 1] = '\0';
|
|
|
|
keymacro_kprint(el, el->el_keymacro.buf, &ptr->val, ptr->type);
|
2001-10-01 08:41:27 +00:00
|
|
|
} else
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) node_enum(el, ptr->next, cnt + (size_t)used);
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
/* go to sibling if there is one */
|
|
|
|
if (ptr->sibling)
|
|
|
|
(void) node_enum(el, ptr->sibling, cnt);
|
2015-01-09 07:40:56 +00:00
|
|
|
return 0;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro_kprint():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Print the specified key and its associated
|
|
|
|
* function specified by val
|
|
|
|
*/
|
|
|
|
protected void
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro_kprint(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
2001-10-01 08:41:27 +00:00
|
|
|
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:
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) keymacro__decode_str(val->str, unparsbuf,
|
Merge the following changes from NetBSD:
chared.h 1.17, common.c 1.19, emacs.c 1.21, key.c 1.18, key.h 1.9, map.c 1.23,
term.c 1.42, term.h 1.17, vi.c 1.25:
# Print the actual eofc, instead of ^D\b\b.
# Change internal character decoding to prevent buffer oveflows.
key.c 1.19, key.h 1.10:
# move declaration to header file.
term.c 1.43:
# Coverity CID 806: Prevent NULL deref
term.c 1.44:
# Coverity CID 1668: Plug memory leak.
term.c 1.45:
# Fix compilation.
MFC after: 3 weeks
2007-03-11 18:30:22 +00:00
|
|
|
sizeof(unparsbuf),
|
|
|
|
ntype == XK_STR ? "\"\"" : "[]");
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) fprintf(el->el_outfile, fmt,
|
|
|
|
ct_encode_string(key, &el->el_scratch), unparsbuf);
|
2001-10-01 08:41:27 +00:00
|
|
|
break;
|
|
|
|
case XK_CMD:
|
|
|
|
for (fp = el->el_map.help; fp->name; fp++)
|
|
|
|
if (val->cmd == fp->func) {
|
2015-01-09 07:40:56 +00:00
|
|
|
ct_wcstombs(unparsbuf, fp->name, sizeof(unparsbuf));
|
|
|
|
unparsbuf[sizeof(unparsbuf) -1] = '\0';
|
2001-10-01 08:41:27 +00:00
|
|
|
(void) fprintf(el->el_outfile, fmt,
|
2015-01-09 07:40:56 +00:00
|
|
|
ct_encode_string(key, &el->el_scratch), unparsbuf);
|
2001-10-01 08:41:27 +00:00
|
|
|
break;
|
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
#ifdef DEBUG_KEY
|
2001-10-01 08:41:27 +00:00
|
|
|
if (fp->name == NULL)
|
|
|
|
(void) fprintf(el->el_outfile,
|
|
|
|
"BUG! Command not found.\n");
|
1994-05-27 05:00:24 +00:00
|
|
|
#endif
|
|
|
|
|
2001-10-01 08:41:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2015-01-09 07:40:56 +00:00
|
|
|
(void) fprintf(el->el_outfile, fmt, ct_encode_string(key,
|
|
|
|
&el->el_scratch), "no input");
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Merge the following changes from NetBSD:
chared.h 1.17, common.c 1.19, emacs.c 1.21, key.c 1.18, key.h 1.9, map.c 1.23,
term.c 1.42, term.h 1.17, vi.c 1.25:
# Print the actual eofc, instead of ^D\b\b.
# Change internal character decoding to prevent buffer oveflows.
key.c 1.19, key.h 1.10:
# move declaration to header file.
term.c 1.43:
# Coverity CID 806: Prevent NULL deref
term.c 1.44:
# Coverity CID 1668: Plug memory leak.
term.c 1.45:
# Fix compilation.
MFC after: 3 weeks
2007-03-11 18:30:22 +00:00
|
|
|
#define ADDC(c) \
|
|
|
|
if (b < eb) \
|
|
|
|
*b++ = c; \
|
|
|
|
else \
|
|
|
|
b++
|
2015-01-09 07:40:56 +00:00
|
|
|
/* keymacro__decode_str():
|
1994-05-27 05:00:24 +00:00
|
|
|
* Make a printable version of the ey
|
|
|
|
*/
|
2012-06-22 18:01:22 +00:00
|
|
|
protected size_t
|
2015-01-09 07:40:56 +00:00
|
|
|
keymacro__decode_str(const Char *str, char *buf, size_t len, const char *sep)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
Merge the following changes from NetBSD:
chared.h 1.17, common.c 1.19, emacs.c 1.21, key.c 1.18, key.h 1.9, map.c 1.23,
term.c 1.42, term.h 1.17, vi.c 1.25:
# Print the actual eofc, instead of ^D\b\b.
# Change internal character decoding to prevent buffer oveflows.
key.c 1.19, key.h 1.10:
# move declaration to header file.
term.c 1.43:
# Coverity CID 806: Prevent NULL deref
term.c 1.44:
# Coverity CID 1668: Plug memory leak.
term.c 1.45:
# Fix compilation.
MFC after: 3 weeks
2007-03-11 18:30:22 +00:00
|
|
|
char *b = buf, *eb = b + len;
|
2015-01-09 07:40:56 +00:00
|
|
|
const Char *p;
|
2001-10-01 08:41:27 +00:00
|
|
|
|
|
|
|
b = buf;
|
Merge the following changes from NetBSD:
chared.h 1.17, common.c 1.19, emacs.c 1.21, key.c 1.18, key.h 1.9, map.c 1.23,
term.c 1.42, term.h 1.17, vi.c 1.25:
# Print the actual eofc, instead of ^D\b\b.
# Change internal character decoding to prevent buffer oveflows.
key.c 1.19, key.h 1.10:
# move declaration to header file.
term.c 1.43:
# Coverity CID 806: Prevent NULL deref
term.c 1.44:
# Coverity CID 1668: Plug memory leak.
term.c 1.45:
# Fix compilation.
MFC after: 3 weeks
2007-03-11 18:30:22 +00:00
|
|
|
if (sep[0] != '\0') {
|
|
|
|
ADDC(sep[0]);
|
|
|
|
}
|
|
|
|
if (*str == '\0') {
|
|
|
|
ADDC('^');
|
|
|
|
ADDC('@');
|
2015-01-09 07:40:56 +00:00
|
|
|
goto add_endsep;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2001-10-01 08:41:27 +00:00
|
|
|
for (p = str; *p != 0; p++) {
|
2015-01-09 07:40:56 +00:00
|
|
|
Char dbuf[VISUAL_WIDTH_MAX];
|
|
|
|
Char *p2 = dbuf;
|
|
|
|
ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p);
|
|
|
|
while (l-- > 0) {
|
|
|
|
ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++);
|
|
|
|
if (n == -1) /* ran out of space */
|
|
|
|
goto add_endsep;
|
|
|
|
else
|
|
|
|
b += n;
|
2001-10-01 08:41:27 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
2015-01-09 07:40:56 +00:00
|
|
|
add_endsep:
|
Merge the following changes from NetBSD:
chared.h 1.17, common.c 1.19, emacs.c 1.21, key.c 1.18, key.h 1.9, map.c 1.23,
term.c 1.42, term.h 1.17, vi.c 1.25:
# Print the actual eofc, instead of ^D\b\b.
# Change internal character decoding to prevent buffer oveflows.
key.c 1.19, key.h 1.10:
# move declaration to header file.
term.c 1.43:
# Coverity CID 806: Prevent NULL deref
term.c 1.44:
# Coverity CID 1668: Plug memory leak.
term.c 1.45:
# Fix compilation.
MFC after: 3 weeks
2007-03-11 18:30:22 +00:00
|
|
|
if (sep[0] != '\0' && sep[1] != '\0') {
|
|
|
|
ADDC(sep[1]);
|
|
|
|
}
|
|
|
|
ADDC('\0');
|
2012-06-22 18:01:22 +00:00
|
|
|
if ((size_t)(b - buf) >= len)
|
Merge the following changes from NetBSD:
chared.h 1.17, common.c 1.19, emacs.c 1.21, key.c 1.18, key.h 1.9, map.c 1.23,
term.c 1.42, term.h 1.17, vi.c 1.25:
# Print the actual eofc, instead of ^D\b\b.
# Change internal character decoding to prevent buffer oveflows.
key.c 1.19, key.h 1.10:
# move declaration to header file.
term.c 1.43:
# Coverity CID 806: Prevent NULL deref
term.c 1.44:
# Coverity CID 1668: Plug memory leak.
term.c 1.45:
# Fix compilation.
MFC after: 3 weeks
2007-03-11 18:30:22 +00:00
|
|
|
buf[len - 1] = '\0';
|
2012-06-22 18:01:22 +00:00
|
|
|
return (size_t)(b - buf);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|