Home | History | Annotate | Line # | Download | only in libedit
keymacro.c revision 1.3
      1  1.3  christos /*	$NetBSD: keymacro.c,v 1.3 2011/07/28 03:52:19 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1992, 1993
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to Berkeley by
      8  1.1  christos  * Christos Zoulas of Cornell University.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     19  1.1  christos  *    may be used to endorse or promote products derived from this software
     20  1.1  christos  *    without specific prior written permission.
     21  1.1  christos  *
     22  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  christos  * SUCH DAMAGE.
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos #include "config.h"
     36  1.1  christos #if !defined(lint) && !defined(SCCSID)
     37  1.1  christos #if 0
     38  1.3  christos static char sccsid[] = "@(#)key.c	8.1 (Berkeley) 6/4/93";
     39  1.1  christos #else
     40  1.3  christos __RCSID("$NetBSD: keymacro.c,v 1.3 2011/07/28 03:52:19 christos Exp $");
     41  1.1  christos #endif
     42  1.1  christos #endif /* not lint && not SCCSID */
     43  1.1  christos 
     44  1.1  christos /*
     45  1.2  christos  * keymacro.c: This module contains the procedures for maintaining
     46  1.2  christos  *	       the extended-key map.
     47  1.1  christos  *
     48  1.1  christos  *      An extended-key (key) is a sequence of keystrokes introduced
     49  1.1  christos  *	with a sequence introducer and consisting of an arbitrary
     50  1.2  christos  *	number of characters.  This module maintains a map (the
     51  1.2  christos  *	el->el_keymacro.map)
     52  1.1  christos  *	to convert these extended-key sequences into input strs
     53  1.1  christos  *	(XK_STR), editor functions (XK_CMD), or unix commands (XK_EXE).
     54  1.1  christos  *
     55  1.1  christos  *      Warning:
     56  1.1  christos  *	  If key is a substr of some other keys, then the longer
     57  1.1  christos  *	  keys are lost!!  That is, if the keys "abcd" and "abcef"
     58  1.2  christos  *	  are in el->el_keymacro.map, adding the key "abc" will cause
     59  1.2  christos  *	  the first two definitions to be lost.
     60  1.1  christos  *
     61  1.1  christos  *      Restrictions:
     62  1.1  christos  *      -------------
     63  1.1  christos  *      1) It is not possible to have one key that is a
     64  1.1  christos  *	   substr of another.
     65  1.1  christos  */
     66  1.1  christos #include <string.h>
     67  1.1  christos #include <stdlib.h>
     68  1.1  christos 
     69  1.1  christos #include "el.h"
     70  1.1  christos 
     71  1.1  christos /*
     72  1.2  christos  * The Nodes of the el->el_keymacro.map.  The el->el_keymacro.map is a
     73  1.2  christos  * linked list of these node elements
     74  1.1  christos  */
     75  1.1  christos struct keymacro_node_t {
     76  1.2  christos 	Char		 ch;		/* single character of key 	 */
     77  1.2  christos 	int		 type;		/* node type			 */
     78  1.2  christos 	keymacro_value_t val;		/* command code or pointer to str,  */
     79  1.1  christos 					/* if this is a leaf 		 */
     80  1.1  christos 	struct keymacro_node_t *next;	/* ptr to next char of this key  */
     81  1.2  christos 	struct keymacro_node_t *sibling;/* ptr to another key with same prefix*/
     82  1.1  christos };
     83  1.1  christos 
     84  1.1  christos private int		 node_trav(EditLine *, keymacro_node_t *, Char *,
     85  1.1  christos     keymacro_value_t *);
     86  1.1  christos private int		 node__try(EditLine *, keymacro_node_t *, const Char *,
     87  1.1  christos     keymacro_value_t *, int);
     88  1.1  christos private keymacro_node_t	*node__get(Int);
     89  1.1  christos private void		 node__free(keymacro_node_t *);
     90  1.1  christos private void		 node__put(EditLine *, keymacro_node_t *);
     91  1.2  christos private int		 node__delete(EditLine *, keymacro_node_t **,
     92  1.2  christos     const Char *);
     93  1.2  christos private int		 node_lookup(EditLine *, const Char *,
     94  1.2  christos     keymacro_node_t *, size_t);
     95  1.1  christos private int		 node_enum(EditLine *, keymacro_node_t *, size_t);
     96  1.1  christos 
     97  1.1  christos #define	KEY_BUFSIZ	EL_BUFSIZ
     98  1.1  christos 
     99  1.1  christos 
    100  1.1  christos /* keymacro_init():
    101  1.1  christos  *	Initialize the key maps
    102  1.1  christos  */
    103  1.1  christos protected int
    104  1.1  christos keymacro_init(EditLine *el)
    105  1.1  christos {
    106  1.1  christos 
    107  1.2  christos 	el->el_keymacro.buf = el_malloc(KEY_BUFSIZ *
    108  1.2  christos 	    sizeof(*el->el_keymacro.buf));
    109  1.1  christos 	if (el->el_keymacro.buf == NULL)
    110  1.1  christos 		return (-1);
    111  1.1  christos 	el->el_keymacro.map = NULL;
    112  1.1  christos 	keymacro_reset(el);
    113  1.1  christos 	return (0);
    114  1.1  christos }
    115  1.1  christos 
    116  1.1  christos /* keymacro_end():
    117  1.1  christos  *	Free the key maps
    118  1.1  christos  */
    119  1.1  christos protected void
    120  1.1  christos keymacro_end(EditLine *el)
    121  1.1  christos {
    122  1.1  christos 
    123  1.1  christos 	el_free((ptr_t) el->el_keymacro.buf);
    124  1.1  christos 	el->el_keymacro.buf = NULL;
    125  1.1  christos 	node__free(el->el_keymacro.map);
    126  1.1  christos }
    127  1.1  christos 
    128  1.1  christos 
    129  1.1  christos /* keymacro_map_cmd():
    130  1.1  christos  *	Associate cmd with a key value
    131  1.1  christos  */
    132  1.1  christos protected keymacro_value_t *
    133  1.1  christos keymacro_map_cmd(EditLine *el, int cmd)
    134  1.1  christos {
    135  1.1  christos 
    136  1.1  christos 	el->el_keymacro.val.cmd = (el_action_t) cmd;
    137  1.1  christos 	return (&el->el_keymacro.val);
    138  1.1  christos }
    139  1.1  christos 
    140  1.1  christos 
    141  1.1  christos /* keymacro_map_str():
    142  1.1  christos  *	Associate str with a key value
    143  1.1  christos  */
    144  1.1  christos protected keymacro_value_t *
    145  1.1  christos keymacro_map_str(EditLine *el, Char *str)
    146  1.1  christos {
    147  1.1  christos 
    148  1.1  christos 	el->el_keymacro.val.str = str;
    149  1.1  christos 	return (&el->el_keymacro.val);
    150  1.1  christos }
    151  1.1  christos 
    152  1.1  christos 
    153  1.1  christos /* keymacro_reset():
    154  1.2  christos  *	Takes all nodes on el->el_keymacro.map and puts them on free list.
    155  1.2  christos  *	Then initializes el->el_keymacro.map with arrow keys
    156  1.1  christos  *	[Always bind the ansi arrow keys?]
    157  1.1  christos  */
    158  1.1  christos protected void
    159  1.1  christos keymacro_reset(EditLine *el)
    160  1.1  christos {
    161  1.1  christos 
    162  1.1  christos 	node__put(el, el->el_keymacro.map);
    163  1.1  christos 	el->el_keymacro.map = NULL;
    164  1.1  christos 	return;
    165  1.1  christos }
    166  1.1  christos 
    167  1.1  christos 
    168  1.1  christos /* keymacro_get():
    169  1.1  christos  *	Calls the recursive function with entry point el->el_keymacro.map
    170  1.1  christos  *      Looks up *ch in map and then reads characters until a
    171  1.1  christos  *      complete match is found or a mismatch occurs. Returns the
    172  1.1  christos  *      type of the match found (XK_STR, XK_CMD, or XK_EXE).
    173  1.1  christos  *      Returns NULL in val.str and XK_STR for no match.
    174  1.1  christos  *      The last character read is returned in *ch.
    175  1.1  christos  */
    176  1.1  christos protected int
    177  1.1  christos keymacro_get(EditLine *el, Char *ch, keymacro_value_t *val)
    178  1.1  christos {
    179  1.1  christos 
    180  1.1  christos 	return (node_trav(el, el->el_keymacro.map, ch, val));
    181  1.1  christos }
    182  1.1  christos 
    183  1.1  christos 
    184  1.1  christos /* keymacro_add():
    185  1.2  christos  *      Adds key to the el->el_keymacro.map and associates the value in
    186  1.2  christos  *	val with it. If key is already is in el->el_keymacro.map, the new
    187  1.2  christos  *	code is applied to the existing key. Ntype specifies if code is a
    188  1.2  christos  *	command, an out str or a unix command.
    189  1.1  christos  */
    190  1.1  christos protected void
    191  1.1  christos keymacro_add(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
    192  1.1  christos {
    193  1.1  christos 
    194  1.1  christos 	if (key[0] == '\0') {
    195  1.1  christos 		(void) fprintf(el->el_errfile,
    196  1.1  christos 		    "keymacro_add: Null extended-key not allowed.\n");
    197  1.1  christos 		return;
    198  1.1  christos 	}
    199  1.1  christos 	if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) {
    200  1.1  christos 		(void) fprintf(el->el_errfile,
    201  1.1  christos 		    "keymacro_add: sequence-lead-in command not allowed\n");
    202  1.1  christos 		return;
    203  1.1  christos 	}
    204  1.1  christos 	if (el->el_keymacro.map == NULL)
    205  1.1  christos 		/* tree is initially empty.  Set up new node to match key[0] */
    206  1.1  christos 		el->el_keymacro.map = node__get(key[0]);
    207  1.1  christos 			/* it is properly initialized */
    208  1.1  christos 
    209  1.1  christos 	/* Now recurse through el->el_keymacro.map */
    210  1.1  christos 	(void) node__try(el, el->el_keymacro.map, key, val, ntype);
    211  1.1  christos 	return;
    212  1.1  christos }
    213  1.1  christos 
    214  1.1  christos 
    215  1.1  christos /* keymacro_clear():
    216  1.1  christos  *
    217  1.1  christos  */
    218  1.1  christos protected void
    219  1.1  christos keymacro_clear(EditLine *el, el_action_t *map, const Char *in)
    220  1.1  christos {
    221  1.1  christos #ifdef WIDECHAR
    222  1.1  christos         if (*in > N_KEYS) /* can't be in the map */
    223  1.1  christos                 return;
    224  1.1  christos #endif
    225  1.1  christos 	if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) &&
    226  1.1  christos 	    ((map == el->el_map.key &&
    227  1.1  christos 	    el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) ||
    228  1.1  christos 	    (map == el->el_map.alt &&
    229  1.1  christos 	    el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN)))
    230  1.1  christos 		(void) keymacro_delete(el, in);
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos 
    234  1.1  christos /* keymacro_delete():
    235  1.1  christos  *      Delete the key and all longer keys staring with key, if
    236  1.1  christos  *      they exists.
    237  1.1  christos  */
    238  1.1  christos protected int
    239  1.1  christos keymacro_delete(EditLine *el, const Char *key)
    240  1.1  christos {
    241  1.1  christos 
    242  1.1  christos 	if (key[0] == '\0') {
    243  1.1  christos 		(void) fprintf(el->el_errfile,
    244  1.1  christos 		    "keymacro_delete: Null extended-key not allowed.\n");
    245  1.1  christos 		return (-1);
    246  1.1  christos 	}
    247  1.1  christos 	if (el->el_keymacro.map == NULL)
    248  1.1  christos 		return (0);
    249  1.1  christos 
    250  1.1  christos 	(void) node__delete(el, &el->el_keymacro.map, key);
    251  1.1  christos 	return (0);
    252  1.1  christos }
    253  1.1  christos 
    254  1.1  christos 
    255  1.1  christos /* keymacro_print():
    256  1.1  christos  *	Print the binding associated with key key.
    257  1.1  christos  *	Print entire el->el_keymacro.map if null
    258  1.1  christos  */
    259  1.1  christos protected void
    260  1.1  christos keymacro_print(EditLine *el, const Char *key)
    261  1.1  christos {
    262  1.1  christos 
    263  1.1  christos 	/* do nothing if el->el_keymacro.map is empty and null key specified */
    264  1.1  christos 	if (el->el_keymacro.map == NULL && *key == 0)
    265  1.1  christos 		return;
    266  1.1  christos 
    267  1.1  christos 	el->el_keymacro.buf[0] = '"';
    268  1.1  christos 	if (node_lookup(el, key, el->el_keymacro.map, 1) <= -1)
    269  1.1  christos 		/* key is not bound */
    270  1.2  christos 		(void) fprintf(el->el_errfile, "Unbound extended key \"" FSTR
    271  1.2  christos 		    "\"\n", key);
    272  1.1  christos 	return;
    273  1.1  christos }
    274  1.1  christos 
    275  1.1  christos 
    276  1.1  christos /* node_trav():
    277  1.1  christos  *	recursively traverses node in tree until match or mismatch is
    278  1.1  christos  * 	found.  May read in more characters.
    279  1.1  christos  */
    280  1.1  christos private int
    281  1.1  christos node_trav(EditLine *el, keymacro_node_t *ptr, Char *ch, keymacro_value_t *val)
    282  1.1  christos {
    283  1.1  christos 
    284  1.1  christos 	if (ptr->ch == *ch) {
    285  1.1  christos 		/* match found */
    286  1.1  christos 		if (ptr->next) {
    287  1.1  christos 			/* key not complete so get next char */
    288  1.1  christos 			if (FUN(el,getc)(el, ch) != 1) {/* if EOF or error */
    289  1.1  christos 				val->cmd = ED_END_OF_FILE;
    290  1.1  christos 				return (XK_CMD);
    291  1.1  christos 				/* PWP: Pretend we just read an end-of-file */
    292  1.1  christos 			}
    293  1.1  christos 			return (node_trav(el, ptr->next, ch, val));
    294  1.1  christos 		} else {
    295  1.1  christos 			*val = ptr->val;
    296  1.1  christos 			if (ptr->type != XK_CMD)
    297  1.1  christos 				*ch = '\0';
    298  1.1  christos 			return (ptr->type);
    299  1.1  christos 		}
    300  1.1  christos 	} else {
    301  1.1  christos 		/* no match found here */
    302  1.1  christos 		if (ptr->sibling) {
    303  1.1  christos 			/* try next sibling */
    304  1.1  christos 			return (node_trav(el, ptr->sibling, ch, val));
    305  1.1  christos 		} else {
    306  1.1  christos 			/* no next sibling -- mismatch */
    307  1.1  christos 			val->str = NULL;
    308  1.1  christos 			return (XK_STR);
    309  1.1  christos 		}
    310  1.1  christos 	}
    311  1.1  christos }
    312  1.1  christos 
    313  1.1  christos 
    314  1.1  christos /* node__try():
    315  1.1  christos  * 	Find a node that matches *str or allocate a new one
    316  1.1  christos  */
    317  1.1  christos private int
    318  1.2  christos node__try(EditLine *el, keymacro_node_t *ptr, const Char *str,
    319  1.2  christos     keymacro_value_t *val, int ntype)
    320  1.1  christos {
    321  1.1  christos 
    322  1.1  christos 	if (ptr->ch != *str) {
    323  1.1  christos 		keymacro_node_t *xm;
    324  1.1  christos 
    325  1.1  christos 		for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
    326  1.1  christos 			if (xm->sibling->ch == *str)
    327  1.1  christos 				break;
    328  1.1  christos 		if (xm->sibling == NULL)
    329  1.1  christos 			xm->sibling = node__get(*str);	/* setup new node */
    330  1.1  christos 		ptr = xm->sibling;
    331  1.1  christos 	}
    332  1.1  christos 	if (*++str == '\0') {
    333  1.1  christos 		/* we're there */
    334  1.1  christos 		if (ptr->next != NULL) {
    335  1.1  christos 			node__put(el, ptr->next);
    336  1.1  christos 				/* lose longer keys with this prefix */
    337  1.1  christos 			ptr->next = NULL;
    338  1.1  christos 		}
    339  1.1  christos 		switch (ptr->type) {
    340  1.1  christos 		case XK_CMD:
    341  1.1  christos 		case XK_NOD:
    342  1.1  christos 			break;
    343  1.1  christos 		case XK_STR:
    344  1.1  christos 		case XK_EXE:
    345  1.1  christos 			if (ptr->val.str)
    346  1.1  christos 				el_free((ptr_t) ptr->val.str);
    347  1.1  christos 			break;
    348  1.1  christos 		default:
    349  1.1  christos 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n",
    350  1.1  christos 			    ptr->type));
    351  1.1  christos 			break;
    352  1.1  christos 		}
    353  1.1  christos 
    354  1.1  christos 		switch (ptr->type = ntype) {
    355  1.1  christos 		case XK_CMD:
    356  1.1  christos 			ptr->val = *val;
    357  1.1  christos 			break;
    358  1.1  christos 		case XK_STR:
    359  1.1  christos 		case XK_EXE:
    360  1.1  christos 			if ((ptr->val.str = Strdup(val->str)) == NULL)
    361  1.1  christos 				return -1;
    362  1.1  christos 			break;
    363  1.1  christos 		default:
    364  1.1  christos 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
    365  1.1  christos 			break;
    366  1.1  christos 		}
    367  1.1  christos 	} else {
    368  1.1  christos 		/* still more chars to go */
    369  1.1  christos 		if (ptr->next == NULL)
    370  1.1  christos 			ptr->next = node__get(*str);	/* setup new node */
    371  1.1  christos 		(void) node__try(el, ptr->next, str, val, ntype);
    372  1.1  christos 	}
    373  1.1  christos 	return (0);
    374  1.1  christos }
    375  1.1  christos 
    376  1.1  christos 
    377  1.1  christos /* node__delete():
    378  1.1  christos  *	Delete node that matches str
    379  1.1  christos  */
    380  1.1  christos private int
    381  1.1  christos node__delete(EditLine *el, keymacro_node_t **inptr, const Char *str)
    382  1.1  christos {
    383  1.1  christos 	keymacro_node_t *ptr;
    384  1.1  christos 	keymacro_node_t *prev_ptr = NULL;
    385  1.1  christos 
    386  1.1  christos 	ptr = *inptr;
    387  1.1  christos 
    388  1.1  christos 	if (ptr->ch != *str) {
    389  1.1  christos 		keymacro_node_t *xm;
    390  1.1  christos 
    391  1.1  christos 		for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
    392  1.1  christos 			if (xm->sibling->ch == *str)
    393  1.1  christos 				break;
    394  1.1  christos 		if (xm->sibling == NULL)
    395  1.1  christos 			return (0);
    396  1.1  christos 		prev_ptr = xm;
    397  1.1  christos 		ptr = xm->sibling;
    398  1.1  christos 	}
    399  1.1  christos 	if (*++str == '\0') {
    400  1.1  christos 		/* we're there */
    401  1.1  christos 		if (prev_ptr == NULL)
    402  1.1  christos 			*inptr = ptr->sibling;
    403  1.1  christos 		else
    404  1.1  christos 			prev_ptr->sibling = ptr->sibling;
    405  1.1  christos 		ptr->sibling = NULL;
    406  1.1  christos 		node__put(el, ptr);
    407  1.1  christos 		return (1);
    408  1.1  christos 	} else if (ptr->next != NULL &&
    409  1.1  christos 	    node__delete(el, &ptr->next, str) == 1) {
    410  1.1  christos 		if (ptr->next != NULL)
    411  1.1  christos 			return (0);
    412  1.1  christos 		if (prev_ptr == NULL)
    413  1.1  christos 			*inptr = ptr->sibling;
    414  1.1  christos 		else
    415  1.1  christos 			prev_ptr->sibling = ptr->sibling;
    416  1.1  christos 		ptr->sibling = NULL;
    417  1.1  christos 		node__put(el, ptr);
    418  1.1  christos 		return (1);
    419  1.1  christos 	} else {
    420  1.1  christos 		return (0);
    421  1.1  christos 	}
    422  1.1  christos }
    423  1.1  christos 
    424  1.1  christos 
    425  1.1  christos /* node__put():
    426  1.1  christos  *	Puts a tree of nodes onto free list using free(3).
    427  1.1  christos  */
    428  1.1  christos private void
    429  1.1  christos node__put(EditLine *el, keymacro_node_t *ptr)
    430  1.1  christos {
    431  1.1  christos 	if (ptr == NULL)
    432  1.1  christos 		return;
    433  1.1  christos 
    434  1.1  christos 	if (ptr->next != NULL) {
    435  1.1  christos 		node__put(el, ptr->next);
    436  1.1  christos 		ptr->next = NULL;
    437  1.1  christos 	}
    438  1.1  christos 	node__put(el, ptr->sibling);
    439  1.1  christos 
    440  1.1  christos 	switch (ptr->type) {
    441  1.1  christos 	case XK_CMD:
    442  1.1  christos 	case XK_NOD:
    443  1.1  christos 		break;
    444  1.1  christos 	case XK_EXE:
    445  1.1  christos 	case XK_STR:
    446  1.1  christos 		if (ptr->val.str != NULL)
    447  1.1  christos 			el_free((ptr_t) ptr->val.str);
    448  1.1  christos 		break;
    449  1.1  christos 	default:
    450  1.1  christos 		EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type));
    451  1.1  christos 		break;
    452  1.1  christos 	}
    453  1.1  christos 	el_free((ptr_t) ptr);
    454  1.1  christos }
    455  1.1  christos 
    456  1.1  christos 
    457  1.1  christos /* node__get():
    458  1.1  christos  *	Returns pointer to a keymacro_node_t for ch.
    459  1.1  christos  */
    460  1.1  christos private keymacro_node_t *
    461  1.1  christos node__get(Int ch)
    462  1.1  christos {
    463  1.1  christos 	keymacro_node_t *ptr;
    464  1.1  christos 
    465  1.1  christos 	ptr = (keymacro_node_t *) el_malloc((size_t) sizeof(keymacro_node_t));
    466  1.1  christos 	if (ptr == NULL)
    467  1.1  christos 		return NULL;
    468  1.1  christos 	ptr->ch = ch;
    469  1.1  christos 	ptr->type = XK_NOD;
    470  1.1  christos 	ptr->val.str = NULL;
    471  1.1  christos 	ptr->next = NULL;
    472  1.1  christos 	ptr->sibling = NULL;
    473  1.1  christos 	return (ptr);
    474  1.1  christos }
    475  1.1  christos 
    476  1.1  christos private void
    477  1.1  christos node__free(keymacro_node_t *k)
    478  1.1  christos {
    479  1.1  christos 	if (k == NULL)
    480  1.1  christos 		return;
    481  1.1  christos 	node__free(k->sibling);
    482  1.1  christos 	node__free(k->next);
    483  1.1  christos 	el_free((ptr_t) k);
    484  1.1  christos }
    485  1.1  christos 
    486  1.1  christos /* node_lookup():
    487  1.1  christos  *	look for the str starting at node ptr.
    488  1.1  christos  *	Print if last node
    489  1.1  christos  */
    490  1.1  christos private int
    491  1.1  christos node_lookup(EditLine *el, const Char *str, keymacro_node_t *ptr, size_t cnt)
    492  1.1  christos {
    493  1.1  christos 	ssize_t used;
    494  1.1  christos 
    495  1.1  christos 	if (ptr == NULL)
    496  1.1  christos 		return (-1);	/* cannot have null ptr */
    497  1.1  christos 
    498  1.1  christos 	if (!str || *str == 0) {
    499  1.1  christos 		/* no more chars in str.  node_enum from here. */
    500  1.1  christos 		(void) node_enum(el, ptr, cnt);
    501  1.1  christos 		return (0);
    502  1.1  christos 	} else {
    503  1.1  christos 		/* If match put this char into el->el_keymacro.buf.  Recurse */
    504  1.1  christos 		if (ptr->ch == *str) {
    505  1.1  christos 			/* match found */
    506  1.1  christos 			used = ct_visual_char(el->el_keymacro.buf + cnt,
    507  1.1  christos 			    KEY_BUFSIZ - cnt, ptr->ch);
    508  1.1  christos 			if (used == -1)
    509  1.1  christos 				return (-1); /* ran out of buffer space */
    510  1.1  christos 			if (ptr->next != NULL)
    511  1.1  christos 				/* not yet at leaf */
    512  1.1  christos 				return (node_lookup(el, str + 1, ptr->next,
    513  1.1  christos 				    used + cnt));
    514  1.1  christos 			else {
    515  1.1  christos 			    /* next node is null so key should be complete */
    516  1.1  christos 				if (str[1] == 0) {
    517  1.2  christos 					size_t px = cnt + used;
    518  1.2  christos 					el->el_keymacro.buf[px] = '"';
    519  1.2  christos 					el->el_keymacro.buf[px + 1] = '\0';
    520  1.1  christos 					keymacro_kprint(el, el->el_keymacro.buf,
    521  1.1  christos 					    &ptr->val, ptr->type);
    522  1.1  christos 					return (0);
    523  1.1  christos 				} else
    524  1.1  christos 					return (-1);
    525  1.1  christos 					/* mismatch -- str still has chars */
    526  1.1  christos 			}
    527  1.1  christos 		} else {
    528  1.1  christos 			/* no match found try sibling */
    529  1.1  christos 			if (ptr->sibling)
    530  1.1  christos 				return (node_lookup(el, str, ptr->sibling,
    531  1.1  christos 				    cnt));
    532  1.1  christos 			else
    533  1.1  christos 				return (-1);
    534  1.1  christos 		}
    535  1.1  christos 	}
    536  1.1  christos }
    537  1.1  christos 
    538  1.1  christos 
    539  1.1  christos /* node_enum():
    540  1.1  christos  *	Traverse the node printing the characters it is bound in buffer
    541  1.1  christos  */
    542  1.1  christos private int
    543  1.1  christos node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt)
    544  1.1  christos {
    545  1.1  christos         ssize_t used;
    546  1.1  christos 
    547  1.1  christos 	if (cnt >= KEY_BUFSIZ - 5) {	/* buffer too small */
    548  1.1  christos 		el->el_keymacro.buf[++cnt] = '"';
    549  1.1  christos 		el->el_keymacro.buf[++cnt] = '\0';
    550  1.1  christos 		(void) fprintf(el->el_errfile,
    551  1.1  christos 		    "Some extended keys too long for internal print buffer");
    552  1.2  christos 		(void) fprintf(el->el_errfile, " \"" FSTR "...\"\n",
    553  1.2  christos 		    el->el_keymacro.buf);
    554  1.1  christos 		return (0);
    555  1.1  christos 	}
    556  1.1  christos 	if (ptr == NULL) {
    557  1.1  christos #ifdef DEBUG_EDIT
    558  1.1  christos 		(void) fprintf(el->el_errfile,
    559  1.1  christos 		    "node_enum: BUG!! Null ptr passed\n!");
    560  1.1  christos #endif
    561  1.1  christos 		return (-1);
    562  1.1  christos 	}
    563  1.1  christos 	/* put this char at end of str */
    564  1.2  christos         used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt,
    565  1.2  christos 	    ptr->ch);
    566  1.1  christos 	if (ptr->next == NULL) {
    567  1.1  christos 		/* print this key and function */
    568  1.1  christos 		el->el_keymacro.buf[cnt + used   ] = '"';
    569  1.1  christos 		el->el_keymacro.buf[cnt + used + 1] = '\0';
    570  1.1  christos 		keymacro_kprint(el, el->el_keymacro.buf, &ptr->val, ptr->type);
    571  1.1  christos 	} else
    572  1.1  christos 		(void) node_enum(el, ptr->next, cnt + used);
    573  1.1  christos 
    574  1.1  christos 	/* go to sibling if there is one */
    575  1.1  christos 	if (ptr->sibling)
    576  1.1  christos 		(void) node_enum(el, ptr->sibling, cnt);
    577  1.1  christos 	return (0);
    578  1.1  christos }
    579  1.1  christos 
    580  1.1  christos 
    581  1.1  christos /* keymacro_kprint():
    582  1.1  christos  *	Print the specified key and its associated
    583  1.1  christos  *	function specified by val
    584  1.1  christos  */
    585  1.1  christos protected void
    586  1.1  christos keymacro_kprint(EditLine *el, const Char *key, keymacro_value_t *val, int ntype)
    587  1.1  christos {
    588  1.1  christos 	el_bindings_t *fp;
    589  1.1  christos 	char unparsbuf[EL_BUFSIZ];
    590  1.1  christos 	static const char fmt[] = "%-15s->  %s\n";
    591  1.1  christos 
    592  1.1  christos 	if (val != NULL)
    593  1.1  christos 		switch (ntype) {
    594  1.1  christos 		case XK_STR:
    595  1.1  christos 		case XK_EXE:
    596  1.1  christos 			(void) keymacro__decode_str(val->str, unparsbuf,
    597  1.1  christos 			    sizeof(unparsbuf),
    598  1.1  christos 			    ntype == XK_STR ? "\"\"" : "[]");
    599  1.1  christos 			(void) fprintf(el->el_outfile, fmt,
    600  1.1  christos 			    ct_encode_string(key, &el->el_scratch), unparsbuf);
    601  1.1  christos 			break;
    602  1.1  christos 		case XK_CMD:
    603  1.1  christos 			for (fp = el->el_map.help; fp->name; fp++)
    604  1.1  christos 				if (val->cmd == fp->func) {
    605  1.1  christos                     ct_wcstombs(unparsbuf, fp->name, sizeof(unparsbuf));
    606  1.1  christos                     unparsbuf[sizeof(unparsbuf) -1] = '\0';
    607  1.1  christos 					(void) fprintf(el->el_outfile, fmt,
    608  1.1  christos                         ct_encode_string(key, &el->el_scratch), unparsbuf);
    609  1.1  christos 					break;
    610  1.1  christos 				}
    611  1.1  christos #ifdef DEBUG_KEY
    612  1.1  christos 			if (fp->name == NULL)
    613  1.1  christos 				(void) fprintf(el->el_outfile,
    614  1.1  christos 				    "BUG! Command not found.\n");
    615  1.1  christos #endif
    616  1.1  christos 
    617  1.1  christos 			break;
    618  1.1  christos 		default:
    619  1.1  christos 			EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
    620  1.1  christos 			break;
    621  1.1  christos 		}
    622  1.1  christos 	else
    623  1.1  christos 		(void) fprintf(el->el_outfile, fmt, ct_encode_string(key,
    624  1.1  christos 		    &el->el_scratch), "no input");
    625  1.1  christos }
    626  1.1  christos 
    627  1.1  christos 
    628  1.1  christos #define ADDC(c) \
    629  1.1  christos 	if (b < eb) \
    630  1.1  christos 		*b++ = c; \
    631  1.1  christos 	else \
    632  1.1  christos 		b++
    633  1.1  christos /* keymacro__decode_str():
    634  1.1  christos  *	Make a printable version of the ey
    635  1.1  christos  */
    636  1.1  christos protected size_t
    637  1.1  christos keymacro__decode_str(const Char *str, char *buf, size_t len, const char *sep)
    638  1.1  christos {
    639  1.1  christos 	char *b = buf, *eb = b + len;
    640  1.1  christos 	const Char *p;
    641  1.1  christos 
    642  1.1  christos 	b = buf;
    643  1.1  christos 	if (sep[0] != '\0') {
    644  1.1  christos 		ADDC(sep[0]);
    645  1.1  christos 	}
    646  1.1  christos 	if (*str == '\0') {
    647  1.1  christos 		ADDC('^');
    648  1.1  christos 		ADDC('@');
    649  1.1  christos 		goto add_endsep;
    650  1.1  christos 	}
    651  1.1  christos 	for (p = str; *p != 0; p++) {
    652  1.1  christos 		Char dbuf[VISUAL_WIDTH_MAX];
    653  1.1  christos 		Char *p2 = dbuf;
    654  1.1  christos 		ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p);
    655  1.1  christos 		while (l-- > 0) {
    656  1.1  christos 			ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++);
    657  1.1  christos 			if (n == -1) /* ran out of space */
    658  1.1  christos 				goto add_endsep;
    659  1.1  christos 			else
    660  1.1  christos 				b += n;
    661  1.1  christos 		}
    662  1.1  christos 	}
    663  1.1  christos add_endsep:
    664  1.1  christos 	if (sep[0] != '\0' && sep[1] != '\0') {
    665  1.1  christos 		ADDC(sep[1]);
    666  1.1  christos 	}
    667  1.1  christos 	ADDC('\0');
    668  1.1  christos 	if ((size_t)(b - buf) >= len)
    669  1.1  christos 	    buf[len - 1] = '\0';
    670  1.1  christos 	return (size_t)(b - buf);
    671  1.1  christos }
    672