1 1.25 rillig /* $NetBSD: keymacro.c,v 1.25 2025/01/03 00:40:08 rillig 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.25 rillig __RCSID("$NetBSD: keymacro.c,v 1.25 2025/01/03 00:40:08 rillig 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.20 christos * (XK_STR) or editor functions (XK_CMD). 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.13 christos #include <stdlib.h> 67 1.1 christos #include <string.h> 68 1.1 christos 69 1.1 christos #include "el.h" 70 1.21 christos #include "fcns.h" 71 1.1 christos 72 1.1 christos /* 73 1.2 christos * The Nodes of the el->el_keymacro.map. The el->el_keymacro.map is a 74 1.2 christos * linked list of these node elements 75 1.1 christos */ 76 1.1 christos struct keymacro_node_t { 77 1.18 christos wchar_t ch; /* single character of key */ 78 1.2 christos int type; /* node type */ 79 1.2 christos keymacro_value_t val; /* command code or pointer to str, */ 80 1.13 christos /* if this is a leaf */ 81 1.1 christos struct keymacro_node_t *next; /* ptr to next char of this key */ 82 1.2 christos struct keymacro_node_t *sibling;/* ptr to another key with same prefix*/ 83 1.1 christos }; 84 1.1 christos 85 1.19 christos static int node_trav(EditLine *, keymacro_node_t *, wchar_t *, 86 1.1 christos keymacro_value_t *); 87 1.19 christos static int node__try(EditLine *, keymacro_node_t *, 88 1.18 christos const wchar_t *, keymacro_value_t *, int); 89 1.19 christos static keymacro_node_t *node__get(wint_t); 90 1.19 christos static void node__free(keymacro_node_t *); 91 1.19 christos static void node__put(EditLine *, keymacro_node_t *); 92 1.19 christos static int node__delete(EditLine *, keymacro_node_t **, 93 1.18 christos const wchar_t *); 94 1.19 christos static int node_lookup(EditLine *, const wchar_t *, 95 1.2 christos keymacro_node_t *, size_t); 96 1.19 christos static int node_enum(EditLine *, keymacro_node_t *, size_t); 97 1.1 christos 98 1.1 christos #define KEY_BUFSIZ EL_BUFSIZ 99 1.1 christos 100 1.1 christos 101 1.1 christos /* keymacro_init(): 102 1.1 christos * Initialize the key maps 103 1.1 christos */ 104 1.22 christos libedit_private int 105 1.1 christos keymacro_init(EditLine *el) 106 1.1 christos { 107 1.1 christos 108 1.24 christos el->el_keymacro.buf = el_calloc(KEY_BUFSIZ, 109 1.2 christos sizeof(*el->el_keymacro.buf)); 110 1.1 christos if (el->el_keymacro.buf == NULL) 111 1.5 christos return -1; 112 1.1 christos el->el_keymacro.map = NULL; 113 1.1 christos keymacro_reset(el); 114 1.5 christos return 0; 115 1.1 christos } 116 1.1 christos 117 1.1 christos /* keymacro_end(): 118 1.1 christos * Free the key maps 119 1.1 christos */ 120 1.22 christos libedit_private void 121 1.1 christos keymacro_end(EditLine *el) 122 1.1 christos { 123 1.1 christos 124 1.4 christos el_free(el->el_keymacro.buf); 125 1.1 christos el->el_keymacro.buf = NULL; 126 1.1 christos node__free(el->el_keymacro.map); 127 1.1 christos } 128 1.1 christos 129 1.1 christos 130 1.1 christos /* keymacro_map_cmd(): 131 1.1 christos * Associate cmd with a key value 132 1.1 christos */ 133 1.22 christos libedit_private keymacro_value_t * 134 1.1 christos keymacro_map_cmd(EditLine *el, int cmd) 135 1.1 christos { 136 1.1 christos 137 1.1 christos el->el_keymacro.val.cmd = (el_action_t) cmd; 138 1.5 christos return &el->el_keymacro.val; 139 1.1 christos } 140 1.1 christos 141 1.1 christos 142 1.1 christos /* keymacro_map_str(): 143 1.1 christos * Associate str with a key value 144 1.1 christos */ 145 1.22 christos libedit_private keymacro_value_t * 146 1.18 christos keymacro_map_str(EditLine *el, wchar_t *str) 147 1.1 christos { 148 1.1 christos 149 1.1 christos el->el_keymacro.val.str = str; 150 1.5 christos return &el->el_keymacro.val; 151 1.1 christos } 152 1.1 christos 153 1.1 christos 154 1.1 christos /* keymacro_reset(): 155 1.2 christos * Takes all nodes on el->el_keymacro.map and puts them on free list. 156 1.2 christos * Then initializes el->el_keymacro.map with arrow keys 157 1.1 christos * [Always bind the ansi arrow keys?] 158 1.1 christos */ 159 1.22 christos libedit_private void 160 1.1 christos keymacro_reset(EditLine *el) 161 1.1 christos { 162 1.1 christos 163 1.1 christos node__put(el, el->el_keymacro.map); 164 1.1 christos el->el_keymacro.map = NULL; 165 1.1 christos return; 166 1.1 christos } 167 1.1 christos 168 1.1 christos 169 1.1 christos /* keymacro_get(): 170 1.1 christos * Calls the recursive function with entry point el->el_keymacro.map 171 1.1 christos * Looks up *ch in map and then reads characters until a 172 1.1 christos * complete match is found or a mismatch occurs. Returns the 173 1.20 christos * type of the match found (XK_STR or XK_CMD). 174 1.1 christos * Returns NULL in val.str and XK_STR for no match. 175 1.23 christos * Returns XK_NOD for end of file or read error. 176 1.1 christos * The last character read is returned in *ch. 177 1.1 christos */ 178 1.22 christos libedit_private int 179 1.18 christos keymacro_get(EditLine *el, wchar_t *ch, keymacro_value_t *val) 180 1.1 christos { 181 1.1 christos 182 1.5 christos return node_trav(el, el->el_keymacro.map, ch, val); 183 1.1 christos } 184 1.1 christos 185 1.1 christos 186 1.1 christos /* keymacro_add(): 187 1.2 christos * Adds key to the el->el_keymacro.map and associates the value in 188 1.2 christos * val with it. If key is already is in el->el_keymacro.map, the new 189 1.2 christos * code is applied to the existing key. Ntype specifies if code is a 190 1.2 christos * command, an out str or a unix command. 191 1.1 christos */ 192 1.22 christos libedit_private void 193 1.18 christos keymacro_add(EditLine *el, const wchar_t *key, keymacro_value_t *val, 194 1.18 christos int ntype) 195 1.1 christos { 196 1.1 christos 197 1.1 christos if (key[0] == '\0') { 198 1.1 christos (void) fprintf(el->el_errfile, 199 1.1 christos "keymacro_add: Null extended-key not allowed.\n"); 200 1.1 christos return; 201 1.1 christos } 202 1.1 christos if (ntype == XK_CMD && val->cmd == ED_SEQUENCE_LEAD_IN) { 203 1.1 christos (void) fprintf(el->el_errfile, 204 1.1 christos "keymacro_add: sequence-lead-in command not allowed\n"); 205 1.1 christos return; 206 1.1 christos } 207 1.1 christos if (el->el_keymacro.map == NULL) 208 1.1 christos /* tree is initially empty. Set up new node to match key[0] */ 209 1.1 christos el->el_keymacro.map = node__get(key[0]); 210 1.1 christos /* it is properly initialized */ 211 1.1 christos 212 1.1 christos /* Now recurse through el->el_keymacro.map */ 213 1.1 christos (void) node__try(el, el->el_keymacro.map, key, val, ntype); 214 1.1 christos return; 215 1.1 christos } 216 1.1 christos 217 1.1 christos 218 1.1 christos /* keymacro_clear(): 219 1.1 christos * 220 1.1 christos */ 221 1.22 christos libedit_private void 222 1.18 christos keymacro_clear(EditLine *el, el_action_t *map, const wchar_t *in) 223 1.1 christos { 224 1.1 christos if (*in > N_KEYS) /* can't be in the map */ 225 1.1 christos return; 226 1.1 christos if ((map[(unsigned char)*in] == ED_SEQUENCE_LEAD_IN) && 227 1.1 christos ((map == el->el_map.key && 228 1.1 christos el->el_map.alt[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN) || 229 1.1 christos (map == el->el_map.alt && 230 1.1 christos el->el_map.key[(unsigned char)*in] != ED_SEQUENCE_LEAD_IN))) 231 1.1 christos (void) keymacro_delete(el, in); 232 1.1 christos } 233 1.1 christos 234 1.1 christos 235 1.1 christos /* keymacro_delete(): 236 1.1 christos * Delete the key and all longer keys staring with key, if 237 1.1 christos * they exists. 238 1.1 christos */ 239 1.22 christos libedit_private int 240 1.18 christos keymacro_delete(EditLine *el, const wchar_t *key) 241 1.1 christos { 242 1.1 christos 243 1.1 christos if (key[0] == '\0') { 244 1.1 christos (void) fprintf(el->el_errfile, 245 1.1 christos "keymacro_delete: Null extended-key not allowed.\n"); 246 1.5 christos return -1; 247 1.1 christos } 248 1.1 christos if (el->el_keymacro.map == NULL) 249 1.5 christos return 0; 250 1.1 christos 251 1.1 christos (void) node__delete(el, &el->el_keymacro.map, key); 252 1.5 christos return 0; 253 1.1 christos } 254 1.1 christos 255 1.1 christos 256 1.1 christos /* keymacro_print(): 257 1.1 christos * Print the binding associated with key key. 258 1.1 christos * Print entire el->el_keymacro.map if null 259 1.1 christos */ 260 1.22 christos libedit_private void 261 1.18 christos keymacro_print(EditLine *el, const wchar_t *key) 262 1.1 christos { 263 1.1 christos 264 1.1 christos /* do nothing if el->el_keymacro.map is empty and null key specified */ 265 1.1 christos if (el->el_keymacro.map == NULL && *key == 0) 266 1.1 christos return; 267 1.1 christos 268 1.1 christos el->el_keymacro.buf[0] = '"'; 269 1.6 christos if (node_lookup(el, key, el->el_keymacro.map, (size_t)1) <= -1) 270 1.1 christos /* key is not bound */ 271 1.16 christos (void) fprintf(el->el_errfile, "Unbound extended key \"%ls" 272 1.2 christos "\"\n", key); 273 1.1 christos return; 274 1.1 christos } 275 1.1 christos 276 1.1 christos 277 1.1 christos /* node_trav(): 278 1.1 christos * recursively traverses node in tree until match or mismatch is 279 1.13 christos * found. May read in more characters. 280 1.1 christos */ 281 1.19 christos static int 282 1.18 christos node_trav(EditLine *el, keymacro_node_t *ptr, wchar_t *ch, 283 1.18 christos keymacro_value_t *val) 284 1.1 christos { 285 1.1 christos 286 1.1 christos if (ptr->ch == *ch) { 287 1.1 christos /* match found */ 288 1.1 christos if (ptr->next) { 289 1.1 christos /* key not complete so get next char */ 290 1.23 christos if (el_wgetc(el, ch) != 1) 291 1.23 christos return XK_NOD; 292 1.5 christos return node_trav(el, ptr->next, ch, val); 293 1.1 christos } else { 294 1.1 christos *val = ptr->val; 295 1.1 christos if (ptr->type != XK_CMD) 296 1.1 christos *ch = '\0'; 297 1.5 christos return ptr->type; 298 1.1 christos } 299 1.1 christos } else { 300 1.1 christos /* no match found here */ 301 1.1 christos if (ptr->sibling) { 302 1.1 christos /* try next sibling */ 303 1.5 christos return node_trav(el, ptr->sibling, ch, val); 304 1.1 christos } else { 305 1.1 christos /* no next sibling -- mismatch */ 306 1.1 christos val->str = NULL; 307 1.5 christos return XK_STR; 308 1.1 christos } 309 1.1 christos } 310 1.1 christos } 311 1.1 christos 312 1.1 christos 313 1.1 christos /* node__try(): 314 1.13 christos * Find a node that matches *str or allocate a new one 315 1.1 christos */ 316 1.19 christos static int 317 1.18 christos node__try(EditLine *el, keymacro_node_t *ptr, const wchar_t *str, 318 1.2 christos keymacro_value_t *val, int ntype) 319 1.1 christos { 320 1.1 christos 321 1.1 christos if (ptr->ch != *str) { 322 1.1 christos keymacro_node_t *xm; 323 1.1 christos 324 1.1 christos for (xm = ptr; xm->sibling != NULL; xm = xm->sibling) 325 1.1 christos if (xm->sibling->ch == *str) 326 1.1 christos break; 327 1.1 christos if (xm->sibling == NULL) 328 1.1 christos xm->sibling = node__get(*str); /* setup new node */ 329 1.1 christos ptr = xm->sibling; 330 1.1 christos } 331 1.1 christos if (*++str == '\0') { 332 1.1 christos /* we're there */ 333 1.1 christos if (ptr->next != NULL) { 334 1.1 christos node__put(el, ptr->next); 335 1.1 christos /* lose longer keys with this prefix */ 336 1.1 christos ptr->next = NULL; 337 1.1 christos } 338 1.1 christos switch (ptr->type) { 339 1.1 christos case XK_CMD: 340 1.1 christos case XK_NOD: 341 1.1 christos break; 342 1.1 christos case XK_STR: 343 1.1 christos if (ptr->val.str) 344 1.4 christos el_free(ptr->val.str); 345 1.1 christos break; 346 1.1 christos default: 347 1.1 christos EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", 348 1.1 christos ptr->type)); 349 1.1 christos } 350 1.1 christos 351 1.1 christos switch (ptr->type = ntype) { 352 1.1 christos case XK_CMD: 353 1.1 christos ptr->val = *val; 354 1.1 christos break; 355 1.1 christos case XK_STR: 356 1.17 christos if ((ptr->val.str = wcsdup(val->str)) == NULL) 357 1.1 christos return -1; 358 1.1 christos break; 359 1.1 christos default: 360 1.1 christos EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype)); 361 1.1 christos } 362 1.1 christos } else { 363 1.1 christos /* still more chars to go */ 364 1.1 christos if (ptr->next == NULL) 365 1.1 christos ptr->next = node__get(*str); /* setup new node */ 366 1.1 christos (void) node__try(el, ptr->next, str, val, ntype); 367 1.1 christos } 368 1.5 christos return 0; 369 1.1 christos } 370 1.1 christos 371 1.1 christos 372 1.1 christos /* node__delete(): 373 1.1 christos * Delete node that matches str 374 1.1 christos */ 375 1.19 christos static int 376 1.18 christos node__delete(EditLine *el, keymacro_node_t **inptr, const wchar_t *str) 377 1.1 christos { 378 1.1 christos keymacro_node_t *ptr; 379 1.1 christos keymacro_node_t *prev_ptr = NULL; 380 1.1 christos 381 1.1 christos ptr = *inptr; 382 1.1 christos 383 1.1 christos if (ptr->ch != *str) { 384 1.1 christos keymacro_node_t *xm; 385 1.1 christos 386 1.1 christos for (xm = ptr; xm->sibling != NULL; xm = xm->sibling) 387 1.1 christos if (xm->sibling->ch == *str) 388 1.1 christos break; 389 1.1 christos if (xm->sibling == NULL) 390 1.5 christos return 0; 391 1.1 christos prev_ptr = xm; 392 1.1 christos ptr = xm->sibling; 393 1.1 christos } 394 1.1 christos if (*++str == '\0') { 395 1.1 christos /* we're there */ 396 1.1 christos if (prev_ptr == NULL) 397 1.1 christos *inptr = ptr->sibling; 398 1.1 christos else 399 1.1 christos prev_ptr->sibling = ptr->sibling; 400 1.1 christos ptr->sibling = NULL; 401 1.1 christos node__put(el, ptr); 402 1.5 christos return 1; 403 1.1 christos } else if (ptr->next != NULL && 404 1.1 christos node__delete(el, &ptr->next, str) == 1) { 405 1.1 christos if (ptr->next != NULL) 406 1.5 christos return 0; 407 1.1 christos if (prev_ptr == NULL) 408 1.1 christos *inptr = ptr->sibling; 409 1.1 christos else 410 1.1 christos prev_ptr->sibling = ptr->sibling; 411 1.1 christos ptr->sibling = NULL; 412 1.1 christos node__put(el, ptr); 413 1.5 christos return 1; 414 1.1 christos } else { 415 1.5 christos return 0; 416 1.1 christos } 417 1.1 christos } 418 1.1 christos 419 1.1 christos 420 1.1 christos /* node__put(): 421 1.1 christos * Puts a tree of nodes onto free list using free(3). 422 1.1 christos */ 423 1.19 christos static void 424 1.1 christos node__put(EditLine *el, keymacro_node_t *ptr) 425 1.1 christos { 426 1.1 christos if (ptr == NULL) 427 1.1 christos return; 428 1.1 christos 429 1.1 christos if (ptr->next != NULL) { 430 1.1 christos node__put(el, ptr->next); 431 1.1 christos ptr->next = NULL; 432 1.1 christos } 433 1.1 christos node__put(el, ptr->sibling); 434 1.1 christos 435 1.1 christos switch (ptr->type) { 436 1.1 christos case XK_CMD: 437 1.1 christos case XK_NOD: 438 1.1 christos break; 439 1.1 christos case XK_STR: 440 1.1 christos if (ptr->val.str != NULL) 441 1.4 christos el_free(ptr->val.str); 442 1.1 christos break; 443 1.1 christos default: 444 1.1 christos EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type)); 445 1.1 christos } 446 1.4 christos el_free(ptr); 447 1.1 christos } 448 1.1 christos 449 1.1 christos 450 1.1 christos /* node__get(): 451 1.1 christos * Returns pointer to a keymacro_node_t for ch. 452 1.1 christos */ 453 1.19 christos static keymacro_node_t * 454 1.9 christos node__get(wint_t ch) 455 1.1 christos { 456 1.1 christos keymacro_node_t *ptr; 457 1.1 christos 458 1.4 christos ptr = el_malloc(sizeof(*ptr)); 459 1.1 christos if (ptr == NULL) 460 1.1 christos return NULL; 461 1.18 christos ptr->ch = ch; 462 1.1 christos ptr->type = XK_NOD; 463 1.1 christos ptr->val.str = NULL; 464 1.1 christos ptr->next = NULL; 465 1.1 christos ptr->sibling = NULL; 466 1.5 christos return ptr; 467 1.1 christos } 468 1.1 christos 469 1.19 christos static void 470 1.1 christos node__free(keymacro_node_t *k) 471 1.1 christos { 472 1.1 christos if (k == NULL) 473 1.1 christos return; 474 1.1 christos node__free(k->sibling); 475 1.1 christos node__free(k->next); 476 1.4 christos el_free(k); 477 1.1 christos } 478 1.1 christos 479 1.1 christos /* node_lookup(): 480 1.1 christos * look for the str starting at node ptr. 481 1.1 christos * Print if last node 482 1.1 christos */ 483 1.19 christos static int 484 1.18 christos node_lookup(EditLine *el, const wchar_t *str, keymacro_node_t *ptr, 485 1.18 christos size_t cnt) 486 1.1 christos { 487 1.1 christos ssize_t used; 488 1.1 christos 489 1.1 christos if (ptr == NULL) 490 1.5 christos return -1; /* cannot have null ptr */ 491 1.1 christos 492 1.1 christos if (!str || *str == 0) { 493 1.1 christos /* no more chars in str. node_enum from here. */ 494 1.1 christos (void) node_enum(el, ptr, cnt); 495 1.5 christos return 0; 496 1.1 christos } else { 497 1.1 christos /* If match put this char into el->el_keymacro.buf. Recurse */ 498 1.1 christos if (ptr->ch == *str) { 499 1.1 christos /* match found */ 500 1.1 christos used = ct_visual_char(el->el_keymacro.buf + cnt, 501 1.1 christos KEY_BUFSIZ - cnt, ptr->ch); 502 1.1 christos if (used == -1) 503 1.5 christos return -1; /* ran out of buffer space */ 504 1.1 christos if (ptr->next != NULL) 505 1.1 christos /* not yet at leaf */ 506 1.1 christos return (node_lookup(el, str + 1, ptr->next, 507 1.7 christos (size_t)used + cnt)); 508 1.1 christos else { 509 1.1 christos /* next node is null so key should be complete */ 510 1.1 christos if (str[1] == 0) { 511 1.7 christos size_t px = cnt + (size_t)used; 512 1.2 christos el->el_keymacro.buf[px] = '"'; 513 1.2 christos el->el_keymacro.buf[px + 1] = '\0'; 514 1.1 christos keymacro_kprint(el, el->el_keymacro.buf, 515 1.1 christos &ptr->val, ptr->type); 516 1.5 christos return 0; 517 1.1 christos } else 518 1.5 christos return -1; 519 1.1 christos /* mismatch -- str still has chars */ 520 1.1 christos } 521 1.1 christos } else { 522 1.1 christos /* no match found try sibling */ 523 1.1 christos if (ptr->sibling) 524 1.1 christos return (node_lookup(el, str, ptr->sibling, 525 1.1 christos cnt)); 526 1.1 christos else 527 1.5 christos return -1; 528 1.1 christos } 529 1.1 christos } 530 1.1 christos } 531 1.1 christos 532 1.1 christos 533 1.1 christos /* node_enum(): 534 1.1 christos * Traverse the node printing the characters it is bound in buffer 535 1.1 christos */ 536 1.19 christos static int 537 1.1 christos node_enum(EditLine *el, keymacro_node_t *ptr, size_t cnt) 538 1.1 christos { 539 1.1 christos ssize_t used; 540 1.1 christos 541 1.1 christos if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */ 542 1.1 christos el->el_keymacro.buf[++cnt] = '"'; 543 1.1 christos el->el_keymacro.buf[++cnt] = '\0'; 544 1.1 christos (void) fprintf(el->el_errfile, 545 1.1 christos "Some extended keys too long for internal print buffer"); 546 1.16 christos (void) fprintf(el->el_errfile, " \"%ls...\"\n", 547 1.2 christos el->el_keymacro.buf); 548 1.5 christos return 0; 549 1.1 christos } 550 1.1 christos if (ptr == NULL) { 551 1.1 christos #ifdef DEBUG_EDIT 552 1.1 christos (void) fprintf(el->el_errfile, 553 1.1 christos "node_enum: BUG!! Null ptr passed\n!"); 554 1.1 christos #endif 555 1.5 christos return -1; 556 1.1 christos } 557 1.1 christos /* put this char at end of str */ 558 1.2 christos used = ct_visual_char(el->el_keymacro.buf + cnt, KEY_BUFSIZ - cnt, 559 1.2 christos ptr->ch); 560 1.1 christos if (ptr->next == NULL) { 561 1.1 christos /* print this key and function */ 562 1.7 christos el->el_keymacro.buf[cnt + (size_t)used ] = '"'; 563 1.7 christos el->el_keymacro.buf[cnt + (size_t)used + 1] = '\0'; 564 1.1 christos keymacro_kprint(el, el->el_keymacro.buf, &ptr->val, ptr->type); 565 1.1 christos } else 566 1.7 christos (void) node_enum(el, ptr->next, cnt + (size_t)used); 567 1.1 christos 568 1.1 christos /* go to sibling if there is one */ 569 1.1 christos if (ptr->sibling) 570 1.1 christos (void) node_enum(el, ptr->sibling, cnt); 571 1.5 christos return 0; 572 1.1 christos } 573 1.1 christos 574 1.1 christos 575 1.1 christos /* keymacro_kprint(): 576 1.1 christos * Print the specified key and its associated 577 1.1 christos * function specified by val 578 1.1 christos */ 579 1.22 christos libedit_private void 580 1.18 christos keymacro_kprint(EditLine *el, const wchar_t *key, keymacro_value_t *val, 581 1.18 christos int ntype) 582 1.1 christos { 583 1.1 christos el_bindings_t *fp; 584 1.1 christos char unparsbuf[EL_BUFSIZ]; 585 1.1 christos static const char fmt[] = "%-15s-> %s\n"; 586 1.1 christos 587 1.1 christos if (val != NULL) 588 1.1 christos switch (ntype) { 589 1.1 christos case XK_STR: 590 1.1 christos (void) keymacro__decode_str(val->str, unparsbuf, 591 1.13 christos sizeof(unparsbuf), 592 1.1 christos ntype == XK_STR ? "\"\"" : "[]"); 593 1.1 christos (void) fprintf(el->el_outfile, fmt, 594 1.1 christos ct_encode_string(key, &el->el_scratch), unparsbuf); 595 1.1 christos break; 596 1.1 christos case XK_CMD: 597 1.1 christos for (fp = el->el_map.help; fp->name; fp++) 598 1.1 christos if (val->cmd == fp->func) { 599 1.16 christos wcstombs(unparsbuf, fp->name, sizeof(unparsbuf)); 600 1.1 christos unparsbuf[sizeof(unparsbuf) -1] = '\0'; 601 1.1 christos (void) fprintf(el->el_outfile, fmt, 602 1.1 christos ct_encode_string(key, &el->el_scratch), unparsbuf); 603 1.1 christos break; 604 1.1 christos } 605 1.1 christos #ifdef DEBUG_KEY 606 1.1 christos if (fp->name == NULL) 607 1.1 christos (void) fprintf(el->el_outfile, 608 1.1 christos "BUG! Command not found.\n"); 609 1.1 christos #endif 610 1.1 christos 611 1.1 christos break; 612 1.1 christos default: 613 1.1 christos EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype)); 614 1.1 christos } 615 1.1 christos else 616 1.1 christos (void) fprintf(el->el_outfile, fmt, ct_encode_string(key, 617 1.1 christos &el->el_scratch), "no input"); 618 1.1 christos } 619 1.1 christos 620 1.1 christos 621 1.1 christos #define ADDC(c) \ 622 1.1 christos if (b < eb) \ 623 1.1 christos *b++ = c; \ 624 1.1 christos else \ 625 1.1 christos b++ 626 1.1 christos /* keymacro__decode_str(): 627 1.1 christos * Make a printable version of the ey 628 1.1 christos */ 629 1.22 christos libedit_private size_t 630 1.18 christos keymacro__decode_str(const wchar_t *str, char *buf, size_t len, 631 1.18 christos const char *sep) 632 1.1 christos { 633 1.1 christos char *b = buf, *eb = b + len; 634 1.18 christos const wchar_t *p; 635 1.1 christos 636 1.1 christos b = buf; 637 1.1 christos if (sep[0] != '\0') { 638 1.1 christos ADDC(sep[0]); 639 1.1 christos } 640 1.1 christos if (*str == '\0') { 641 1.1 christos ADDC('^'); 642 1.1 christos ADDC('@'); 643 1.1 christos goto add_endsep; 644 1.1 christos } 645 1.1 christos for (p = str; *p != 0; p++) { 646 1.18 christos wchar_t dbuf[VISUAL_WIDTH_MAX]; 647 1.18 christos wchar_t *p2 = dbuf; 648 1.1 christos ssize_t l = ct_visual_char(dbuf, VISUAL_WIDTH_MAX, *p); 649 1.1 christos while (l-- > 0) { 650 1.1 christos ssize_t n = ct_encode_char(b, (size_t)(eb - b), *p2++); 651 1.1 christos if (n == -1) /* ran out of space */ 652 1.1 christos goto add_endsep; 653 1.1 christos else 654 1.1 christos b += n; 655 1.1 christos } 656 1.1 christos } 657 1.1 christos add_endsep: 658 1.1 christos if (sep[0] != '\0' && sep[1] != '\0') { 659 1.1 christos ADDC(sep[1]); 660 1.1 christos } 661 1.1 christos ADDC('\0'); 662 1.1 christos if ((size_t)(b - buf) >= len) 663 1.1 christos buf[len - 1] = '\0'; 664 1.1 christos return (size_t)(b - buf); 665 1.1 christos } 666