key.c (225736) | key.c (237738) |
---|---|
1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Christos Zoulas of Cornell University. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 15 unchanged lines hidden (view full) --- 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * | 1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Christos Zoulas of Cornell University. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 15 unchanged lines hidden (view full) --- 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * |
32 * $NetBSD: key.c,v 1.19 2006/03/23 20:22:51 christos Exp $ | 32 * $NetBSD: key.c,v 1.20 2009/02/15 21:55:23 christos Exp $ |
33 */ 34 35#if !defined(lint) && !defined(SCCSID) 36static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93"; 37#endif /* not lint && not SCCSID */ 38#include <sys/cdefs.h> | 33 */ 34 35#if !defined(lint) && !defined(SCCSID) 36static char sccsid[] = "@(#)key.c 8.1 (Berkeley) 6/4/93"; 37#endif /* not lint && not SCCSID */ 38#include <sys/cdefs.h> |
39__FBSDID("$FreeBSD: stable/9/lib/libedit/key.c 167457 2007-03-11 18:30:22Z stefanf $"); | 39__FBSDID("$FreeBSD: stable/9/lib/libedit/key.c 237738 2012-06-29 03:01:38Z pfg $"); |
40 41/* 42 * key.c: This module contains the procedures for maintaining 43 * the extended-key map. 44 * 45 * An extended-key (key) is a sequence of keystrokes introduced 46 * with a sequence introducer and consisting of an arbitrary 47 * number of characters. This module maintains a map (the el->el_key.map) --- 33 unchanged lines hidden (view full) --- 81 key_value_t *); 82private int node__try(EditLine *, key_node_t *, const char *, 83 key_value_t *, int); 84private key_node_t *node__get(int); 85private void node__free(key_node_t *); 86private void node__put(EditLine *, key_node_t *); 87private int node__delete(EditLine *, key_node_t **, const char *); 88private int node_lookup(EditLine *, const char *, key_node_t *, | 40 41/* 42 * key.c: This module contains the procedures for maintaining 43 * the extended-key map. 44 * 45 * An extended-key (key) is a sequence of keystrokes introduced 46 * with a sequence introducer and consisting of an arbitrary 47 * number of characters. This module maintains a map (the el->el_key.map) --- 33 unchanged lines hidden (view full) --- 81 key_value_t *); 82private int node__try(EditLine *, key_node_t *, const char *, 83 key_value_t *, int); 84private key_node_t *node__get(int); 85private void node__free(key_node_t *); 86private void node__put(EditLine *, key_node_t *); 87private int node__delete(EditLine *, key_node_t **, const char *); 88private int node_lookup(EditLine *, const char *, key_node_t *, |
89 int); 90private int node_enum(EditLine *, key_node_t *, int); | 89 size_t); 90private int node_enum(EditLine *, key_node_t *, size_t); |
91 92#define KEY_BUFSIZ EL_BUFSIZ 93 94 95/* key_init(): 96 * Initialize the key maps 97 */ 98protected int --- 374 unchanged lines hidden (view full) --- 473 el_free((ptr_t) k); 474} 475 476/* node_lookup(): 477 * look for the str starting at node ptr. 478 * Print if last node 479 */ 480private int | 91 92#define KEY_BUFSIZ EL_BUFSIZ 93 94 95/* key_init(): 96 * Initialize the key maps 97 */ 98protected int --- 374 unchanged lines hidden (view full) --- 473 el_free((ptr_t) k); 474} 475 476/* node_lookup(): 477 * look for the str starting at node ptr. 478 * Print if last node 479 */ 480private int |
481node_lookup(EditLine *el, const char *str, key_node_t *ptr, int cnt) | 481node_lookup(EditLine *el, const char *str, key_node_t *ptr, size_t cnt) |
482{ | 482{ |
483 int ncnt; | 483 size_t ncnt; |
484 485 if (ptr == NULL) 486 return (-1); /* cannot have null ptr */ 487 488 if (*str == 0) { 489 /* no more chars in str. node_enum from here. */ 490 (void) node_enum(el, ptr, cnt); 491 return (0); 492 } else { 493 /* If match put this char into el->el_key.buf. Recurse */ 494 if (ptr->ch == *str) { 495 /* match found */ | 484 485 if (ptr == NULL) 486 return (-1); /* cannot have null ptr */ 487 488 if (*str == 0) { 489 /* no more chars in str. node_enum from here. */ 490 (void) node_enum(el, ptr, cnt); 491 return (0); 492 } else { 493 /* If match put this char into el->el_key.buf. Recurse */ 494 if (ptr->ch == *str) { 495 /* match found */ |
496 ncnt = key__decode_char(el->el_key.buf, KEY_BUFSIZ, cnt, | 496 ncnt = key__decode_char(el->el_key.buf, 497 (size_t)KEY_BUFSIZ, cnt, |
497 (unsigned char) ptr->ch); 498 if (ptr->next != NULL) 499 /* not yet at leaf */ 500 return (node_lookup(el, str + 1, ptr->next, 501 ncnt + 1)); 502 else { 503 /* next node is null so key should be complete */ 504 if (str[1] == 0) { --- 17 unchanged lines hidden (view full) --- 522 } 523} 524 525 526/* node_enum(): 527 * Traverse the node printing the characters it is bound in buffer 528 */ 529private int | 498 (unsigned char) ptr->ch); 499 if (ptr->next != NULL) 500 /* not yet at leaf */ 501 return (node_lookup(el, str + 1, ptr->next, 502 ncnt + 1)); 503 else { 504 /* next node is null so key should be complete */ 505 if (str[1] == 0) { --- 17 unchanged lines hidden (view full) --- 523 } 524} 525 526 527/* node_enum(): 528 * Traverse the node printing the characters it is bound in buffer 529 */ 530private int |
530node_enum(EditLine *el, key_node_t *ptr, int cnt) | 531node_enum(EditLine *el, key_node_t *ptr, size_t cnt) |
531{ | 532{ |
532 int ncnt; | 533 size_t ncnt; |
533 534 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */ 535 el->el_key.buf[++cnt] = '"'; 536 el->el_key.buf[++cnt] = '\0'; 537 (void) fprintf(el->el_errfile, 538 "Some extended keys too long for internal print buffer"); 539 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf); 540 return (0); 541 } 542 if (ptr == NULL) { 543#ifdef DEBUG_EDIT 544 (void) fprintf(el->el_errfile, 545 "node_enum: BUG!! Null ptr passed\n!"); 546#endif 547 return (-1); 548 } 549 /* put this char at end of str */ | 534 535 if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */ 536 el->el_key.buf[++cnt] = '"'; 537 el->el_key.buf[++cnt] = '\0'; 538 (void) fprintf(el->el_errfile, 539 "Some extended keys too long for internal print buffer"); 540 (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf); 541 return (0); 542 } 543 if (ptr == NULL) { 544#ifdef DEBUG_EDIT 545 (void) fprintf(el->el_errfile, 546 "node_enum: BUG!! Null ptr passed\n!"); 547#endif 548 return (-1); 549 } 550 /* put this char at end of str */ |
550 ncnt = key__decode_char(el->el_key.buf, KEY_BUFSIZ, cnt, | 551 ncnt = key__decode_char(el->el_key.buf, (size_t)KEY_BUFSIZ, cnt, |
551 (unsigned char)ptr->ch); 552 if (ptr->next == NULL) { 553 /* print this key and function */ 554 el->el_key.buf[ncnt + 1] = '"'; 555 el->el_key.buf[ncnt + 2] = '\0'; 556 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type); 557 } else 558 (void) node_enum(el, ptr->next, ncnt + 1); --- 51 unchanged lines hidden (view full) --- 610#define ADDC(c) \ 611 if (b < eb) \ 612 *b++ = c; \ 613 else \ 614 b++ 615/* key__decode_char(): 616 * Put a printable form of char in buf. 617 */ | 552 (unsigned char)ptr->ch); 553 if (ptr->next == NULL) { 554 /* print this key and function */ 555 el->el_key.buf[ncnt + 1] = '"'; 556 el->el_key.buf[ncnt + 2] = '\0'; 557 key_kprint(el, el->el_key.buf, &ptr->val, ptr->type); 558 } else 559 (void) node_enum(el, ptr->next, ncnt + 1); --- 51 unchanged lines hidden (view full) --- 611#define ADDC(c) \ 612 if (b < eb) \ 613 *b++ = c; \ 614 else \ 615 b++ 616/* key__decode_char(): 617 * Put a printable form of char in buf. 618 */ |
618protected int 619key__decode_char(char *buf, int cnt, int off, int ch) | 619protected size_t 620key__decode_char(char *buf, size_t cnt, size_t off, int ch) |
620{ 621 char *sb = buf + off; 622 char *eb = buf + cnt; 623 char *b = sb; 624 625 ch = (unsigned char)ch; 626 if (ch == 0) { 627 ADDC('^'); 628 ADDC('@'); | 621{ 622 char *sb = buf + off; 623 char *eb = buf + cnt; 624 char *b = sb; 625 626 ch = (unsigned char)ch; 627 if (ch == 0) { 628 ADDC('^'); 629 ADDC('@'); |
629 return b - sb; | 630 return (int)(b - sb); |
630 } 631 if (iscntrl(ch)) { 632 ADDC('^'); 633 if (ch == '\177') 634 ADDC('?'); 635 else 636 ADDC(toascii(ch) | 0100); 637 } else if (ch == '^') { --- 5 unchanged lines hidden (view full) --- 643 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) { 644 ADDC(ch); 645 } else { 646 ADDC('\\'); 647 ADDC((((unsigned int) ch >> 6) & 7) + '0'); 648 ADDC((((unsigned int) ch >> 3) & 7) + '0'); 649 ADDC((ch & 7) + '0'); 650 } | 631 } 632 if (iscntrl(ch)) { 633 ADDC('^'); 634 if (ch == '\177') 635 ADDC('?'); 636 else 637 ADDC(toascii(ch) | 0100); 638 } else if (ch == '^') { --- 5 unchanged lines hidden (view full) --- 644 } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) { 645 ADDC(ch); 646 } else { 647 ADDC('\\'); 648 ADDC((((unsigned int) ch >> 6) & 7) + '0'); 649 ADDC((((unsigned int) ch >> 3) & 7) + '0'); 650 ADDC((ch & 7) + '0'); 651 } |
651 return b - sb; | 652 return (size_t)(b - sb); |
652} 653 654 655/* key__decode_str(): 656 * Make a printable version of the ey 657 */ | 653} 654 655 656/* key__decode_str(): 657 * Make a printable version of the ey 658 */ |
658protected int 659key__decode_str(const char *str, char *buf, int len, const char *sep) | 659protected size_t 660key__decode_str(const char *str, char *buf, size_t len, const char *sep) |
660{ 661 char *b = buf, *eb = b + len; 662 const char *p; 663 664 b = buf; 665 if (sep[0] != '\0') { 666 ADDC(sep[0]); 667 } --- 26 unchanged lines hidden (view full) --- 694 ADDC((*p & 7) + '0'); 695 } 696 } 697 if (sep[0] != '\0' && sep[1] != '\0') { 698 ADDC(sep[1]); 699 } 700done: 701 ADDC('\0'); | 661{ 662 char *b = buf, *eb = b + len; 663 const char *p; 664 665 b = buf; 666 if (sep[0] != '\0') { 667 ADDC(sep[0]); 668 } --- 26 unchanged lines hidden (view full) --- 695 ADDC((*p & 7) + '0'); 696 } 697 } 698 if (sep[0] != '\0' && sep[1] != '\0') { 699 ADDC(sep[1]); 700 } 701done: 702 ADDC('\0'); |
702 if (b - buf >= len) | 703 if ((size_t)(b - buf) >= len) |
703 buf[len - 1] = '\0'; | 704 buf[len - 1] = '\0'; |
704 return b - buf; | 705 return (size_t)(b - buf); |
705} | 706} |