1 /* $NetBSD: readline.c,v 1.83 2009/04/08 21:31:31 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jaromir Dolecek. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include "config.h" 33 #if !defined(lint) && !defined(SCCSID) 34 __RCSID("$NetBSD: readline.c,v 1.83 2009/04/08 21:31:31 christos Exp $"); 35 #endif /* not lint && not SCCSID */ 36 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #include <stdio.h> 40 #include <dirent.h> 41 #include <string.h> 42 #include <pwd.h> 43 #include <ctype.h> 44 #include <stdlib.h> 45 #include <unistd.h> 46 #include <limits.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <setjmp.h> 50 #ifdef HAVE_VIS_H 51 #include <vis.h> 52 #else 53 #include "np/vis.h" 54 #endif 55 #include "readline/readline.h" 56 #include "el.h" 57 #include "fcns.h" /* for EL_NUM_FCNS */ 58 #include "histedit.h" 59 #include "filecomplete.h" 60 61 void rl_prep_terminal(int); 62 void rl_deprep_terminal(void); 63 64 /* for rl_complete() */ 65 #define TAB '\r' 66 67 /* see comment at the #ifdef for sense of this */ 68 /* #define GDB_411_HACK */ 69 70 /* readline compatibility stuff - look at readline sources/documentation */ 71 /* to see what these variables mean */ 72 const char *rl_library_version = "EditLine wrapper"; 73 static char empty[] = { '\0' }; 74 static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' }; 75 static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$', 76 '>', '<', '=', ';', '|', '&', '{', '(', '\0' }; 77 char *rl_readline_name = empty; 78 FILE *rl_instream = NULL; 79 FILE *rl_outstream = NULL; 80 int rl_point = 0; 81 int rl_end = 0; 82 char *rl_line_buffer = NULL; 83 VCPFunction *rl_linefunc = NULL; 84 int rl_done = 0; 85 VFunction *rl_event_hook = NULL; 86 KEYMAP_ENTRY_ARRAY emacs_standard_keymap, 87 emacs_meta_keymap, 88 emacs_ctlx_keymap; 89 90 int history_base = 1; /* probably never subject to change */ 91 int history_length = 0; 92 int max_input_history = 0; 93 char history_expansion_char = '!'; 94 char history_subst_char = '^'; 95 char *history_no_expand_chars = expand_chars; 96 Function *history_inhibit_expansion_function = NULL; 97 char *history_arg_extract(int start, int end, const char *str); 98 99 int rl_inhibit_completion = 0; 100 int rl_attempted_completion_over = 0; 101 char *rl_basic_word_break_characters = break_chars; 102 char *rl_completer_word_break_characters = NULL; 103 char *rl_completer_quote_characters = NULL; 104 Function *rl_completion_entry_function = NULL; 105 CPPFunction *rl_attempted_completion_function = NULL; 106 Function *rl_pre_input_hook = NULL; 107 Function *rl_startup1_hook = NULL; 108 int (*rl_getc_function)(FILE *) = NULL; 109 char *rl_terminal_name = NULL; 110 int rl_already_prompted = 0; 111 int rl_filename_completion_desired = 0; 112 int rl_ignore_completion_duplicates = 0; 113 int rl_catch_signals = 1; 114 int readline_echoing_p = 1; 115 int _rl_print_completions_horizontally = 0; 116 VFunction *rl_redisplay_function = NULL; 117 Function *rl_startup_hook = NULL; 118 VFunction *rl_completion_display_matches_hook = NULL; 119 VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal; 120 VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal; 121 122 /* 123 * The current prompt string. 124 */ 125 char *rl_prompt = NULL; 126 /* 127 * This is set to character indicating type of completion being done by 128 * rl_complete_internal(); this is available for application completion 129 * functions. 130 */ 131 int rl_completion_type = 0; 132 133 /* 134 * If more than this number of items results from query for possible 135 * completions, we ask user if they are sure to really display the list. 136 */ 137 int rl_completion_query_items = 100; 138 139 /* 140 * List of characters which are word break characters, but should be left 141 * in the parsed text when it is passed to the completion function. 142 * Shell uses this to help determine what kind of completing to do. 143 */ 144 char *rl_special_prefixes = NULL; 145 146 /* 147 * This is the character appended to the completed words if at the end of 148 * the line. Default is ' ' (a space). 149 */ 150 int rl_completion_append_character = ' '; 151 152 /* stuff below is used internally by libedit for readline emulation */ 153 154 static History *h = NULL; 155 static EditLine *e = NULL; 156 static Function *map[256]; 157 static jmp_buf topbuf; 158 159 /* internal functions */ 160 static unsigned char _el_rl_complete(EditLine *, int); 161 static unsigned char _el_rl_tstp(EditLine *, int); 162 static char *_get_prompt(EditLine *); 163 static int _getc_function(EditLine *, char *); 164 static HIST_ENTRY *_move_history(int); 165 static int _history_expand_command(const char *, size_t, size_t, 166 char **); 167 static char *_rl_compat_sub(const char *, const char *, 168 const char *, int); 169 static int _rl_event_read_char(EditLine *, char *); 170 static void _rl_update_pos(void); 171 172 173 /* ARGSUSED */ 174 static char * 175 _get_prompt(EditLine *el __attribute__((__unused__))) 176 { 177 rl_already_prompted = 1; 178 return (rl_prompt); 179 } 180 181 182 /* 183 * generic function for moving around history 184 */ 185 static HIST_ENTRY * 186 _move_history(int op) 187 { 188 HistEvent ev; 189 static HIST_ENTRY rl_he; 190 191 if (history(h, &ev, op) != 0) 192 return (HIST_ENTRY *) NULL; 193 194 rl_he.line = ev.str; 195 rl_he.data = NULL; 196 197 return (&rl_he); 198 } 199 200 201 /* 202 * read one key from user defined input function 203 */ 204 static int 205 /*ARGSUSED*/ 206 _getc_function(EditLine *el, char *c) 207 { 208 int i; 209 210 i = (*rl_getc_function)(NULL); 211 if (i == -1) 212 return 0; 213 *c = i; 214 return 1; 215 } 216 217 218 /* 219 * READLINE compatibility stuff 220 */ 221 222 /* 223 * Set the prompt 224 */ 225 int 226 rl_set_prompt(const char *prompt) 227 { 228 char *p; 229 230 if (!prompt) 231 prompt = ""; 232 if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0) 233 return 0; 234 if (rl_prompt) 235 free(rl_prompt); 236 rl_prompt = strdup(prompt); 237 if (rl_prompt == NULL) 238 return -1; 239 240 while ((p = strchr(rl_prompt, RL_PROMPT_END_IGNORE)) != NULL) 241 *p = RL_PROMPT_START_IGNORE; 242 243 return 0; 244 } 245 246 /* 247 * initialize rl compat stuff 248 */ 249 int 250 rl_initialize(void) 251 { 252 HistEvent ev; 253 const LineInfo *li; 254 int editmode = 1; 255 struct termios t; 256 257 if (e != NULL) 258 el_end(e); 259 if (h != NULL) 260 history_end(h); 261 262 if (!rl_instream) 263 rl_instream = stdin; 264 if (!rl_outstream) 265 rl_outstream = stdout; 266 267 /* 268 * See if we don't really want to run the editor 269 */ 270 if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0) 271 editmode = 0; 272 273 e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr); 274 275 if (!editmode) 276 el_set(e, EL_EDITMODE, 0); 277 278 h = history_init(); 279 if (!e || !h) 280 return (-1); 281 282 history(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */ 283 history_length = 0; 284 max_input_history = INT_MAX; 285 el_set(e, EL_HIST, history, h); 286 287 /* setup getc function if valid */ 288 if (rl_getc_function) 289 el_set(e, EL_GETCFN, _getc_function); 290 291 /* for proper prompt printing in readline() */ 292 if (rl_set_prompt("") == -1) { 293 history_end(h); 294 el_end(e); 295 return -1; 296 } 297 el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE); 298 el_set(e, EL_SIGNAL, rl_catch_signals); 299 300 /* set default mode to "emacs"-style and read setting afterwards */ 301 /* so this can be overriden */ 302 el_set(e, EL_EDITOR, "emacs"); 303 if (rl_terminal_name != NULL) 304 el_set(e, EL_TERMINAL, rl_terminal_name); 305 else 306 el_get(e, EL_TERMINAL, &rl_terminal_name); 307 308 /* 309 * Word completion - this has to go AFTER rebinding keys 310 * to emacs-style. 311 */ 312 el_set(e, EL_ADDFN, "rl_complete", 313 "ReadLine compatible completion function", 314 _el_rl_complete); 315 el_set(e, EL_BIND, "^I", "rl_complete", NULL); 316 317 /* 318 * Send TSTP when ^Z is pressed. 319 */ 320 el_set(e, EL_ADDFN, "rl_tstp", 321 "ReadLine compatible suspend function", 322 _el_rl_tstp); 323 el_set(e, EL_BIND, "^Z", "rl_tstp", NULL); 324 325 /* read settings from configuration file */ 326 el_source(e, NULL); 327 328 /* 329 * Unfortunately, some applications really do use rl_point 330 * and rl_line_buffer directly. 331 */ 332 li = el_line(e); 333 /* a cheesy way to get rid of const cast. */ 334 rl_line_buffer = memchr(li->buffer, *li->buffer, 1); 335 _rl_update_pos(); 336 337 if (rl_startup_hook) 338 (*rl_startup_hook)(NULL, 0); 339 340 return (0); 341 } 342 343 344 /* 345 * read one line from input stream and return it, chomping 346 * trailing newline (if there is any) 347 */ 348 char * 349 readline(const char *p) 350 { 351 HistEvent ev; 352 const char * volatile prompt = p; 353 int count; 354 const char *ret; 355 char *buf; 356 static int used_event_hook; 357 358 if (e == NULL || h == NULL) 359 rl_initialize(); 360 361 rl_done = 0; 362 363 (void)setjmp(topbuf); 364 365 /* update prompt accordingly to what has been passed */ 366 if (rl_set_prompt(prompt) == -1) 367 return NULL; 368 369 if (rl_pre_input_hook) 370 (*rl_pre_input_hook)(NULL, 0); 371 372 if (rl_event_hook && !(e->el_flags&NO_TTY)) { 373 el_set(e, EL_GETCFN, _rl_event_read_char); 374 used_event_hook = 1; 375 } 376 377 if (!rl_event_hook && used_event_hook) { 378 el_set(e, EL_GETCFN, EL_BUILTIN_GETCFN); 379 used_event_hook = 0; 380 } 381 382 rl_already_prompted = 0; 383 384 /* get one line from input stream */ 385 ret = el_gets(e, &count); 386 387 if (ret && count > 0) { 388 int lastidx; 389 390 buf = strdup(ret); 391 if (buf == NULL) 392 return NULL; 393 lastidx = count - 1; 394 if (buf[lastidx] == '\n') 395 buf[lastidx] = '\0'; 396 } else 397 buf = NULL; 398 399 history(h, &ev, H_GETSIZE); 400 history_length = ev.num; 401 402 return buf; 403 } 404 405 /* 406 * history functions 407 */ 408 409 /* 410 * is normally called before application starts to use 411 * history expansion functions 412 */ 413 void 414 using_history(void) 415 { 416 if (h == NULL || e == NULL) 417 rl_initialize(); 418 } 419 420 421 /* 422 * substitute ``what'' with ``with'', returning resulting string; if 423 * globally == 1, substitutes all occurrences of what, otherwise only the 424 * first one 425 */ 426 static char * 427 _rl_compat_sub(const char *str, const char *what, const char *with, 428 int globally) 429 { 430 const char *s; 431 char *r, *result; 432 size_t len, with_len, what_len; 433 434 len = strlen(str); 435 with_len = strlen(with); 436 what_len = strlen(what); 437 438 /* calculate length we need for result */ 439 s = str; 440 while (*s) { 441 if (*s == *what && !strncmp(s, what, what_len)) { 442 len += with_len - what_len; 443 if (!globally) 444 break; 445 s += what_len; 446 } else 447 s++; 448 } 449 r = result = malloc(len + 1); 450 if (result == NULL) 451 return NULL; 452 s = str; 453 while (*s) { 454 if (*s == *what && !strncmp(s, what, what_len)) { 455 (void)strncpy(r, with, with_len); 456 r += with_len; 457 s += what_len; 458 if (!globally) { 459 (void)strcpy(r, s); 460 return(result); 461 } 462 } else 463 *r++ = *s++; 464 } 465 *r = '\0'; 466 return(result); 467 } 468 469 static char *last_search_pat; /* last !?pat[?] search pattern */ 470 static char *last_search_match; /* last !?pat[?] that matched */ 471 472 const char * 473 get_history_event(const char *cmd, int *cindex, int qchar) 474 { 475 int idx, sign, sub, num, begin, ret; 476 size_t len; 477 char *pat; 478 const char *rptr; 479 HistEvent ev; 480 481 idx = *cindex; 482 if (cmd[idx++] != history_expansion_char) 483 return(NULL); 484 485 /* find out which event to take */ 486 if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') { 487 if (history(h, &ev, H_FIRST) != 0) 488 return(NULL); 489 *cindex = cmd[idx]? (idx + 1):idx; 490 return(ev.str); 491 } 492 sign = 0; 493 if (cmd[idx] == '-') { 494 sign = 1; 495 idx++; 496 } 497 498 if ('0' <= cmd[idx] && cmd[idx] <= '9') { 499 HIST_ENTRY *rl_he; 500 501 num = 0; 502 while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') { 503 num = num * 10 + cmd[idx] - '0'; 504 idx++; 505 } 506 if (sign) 507 num = history_length - num + 1; 508 509 if (!(rl_he = history_get(num))) 510 return(NULL); 511 512 *cindex = idx; 513 return(rl_he->line); 514 } 515 sub = 0; 516 if (cmd[idx] == '?') { 517 sub = 1; 518 idx++; 519 } 520 begin = idx; 521 while (cmd[idx]) { 522 if (cmd[idx] == '\n') 523 break; 524 if (sub && cmd[idx] == '?') 525 break; 526 if (!sub && (cmd[idx] == ':' || cmd[idx] == ' ' 527 || cmd[idx] == '\t' || cmd[idx] == qchar)) 528 break; 529 idx++; 530 } 531 len = idx - begin; 532 if (sub && cmd[idx] == '?') 533 idx++; 534 if (sub && len == 0 && last_search_pat && *last_search_pat) 535 pat = last_search_pat; 536 else if (len == 0) 537 return(NULL); 538 else { 539 if ((pat = malloc(len + 1)) == NULL) 540 return NULL; 541 (void)strncpy(pat, cmd + begin, len); 542 pat[len] = '\0'; 543 } 544 545 if (history(h, &ev, H_CURR) != 0) { 546 if (pat != last_search_pat) 547 free(pat); 548 return (NULL); 549 } 550 num = ev.num; 551 552 if (sub) { 553 if (pat != last_search_pat) { 554 if (last_search_pat) 555 free(last_search_pat); 556 last_search_pat = pat; 557 } 558 ret = history_search(pat, -1); 559 } else 560 ret = history_search_prefix(pat, -1); 561 562 if (ret == -1) { 563 /* restore to end of list on failed search */ 564 history(h, &ev, H_FIRST); 565 (void)fprintf(rl_outstream, "%s: Event not found\n", pat); 566 if (pat != last_search_pat) 567 free(pat); 568 return(NULL); 569 } 570 571 if (sub && len) { 572 if (last_search_match && last_search_match != pat) 573 free(last_search_match); 574 last_search_match = pat; 575 } 576 577 if (pat != last_search_pat) 578 free(pat); 579 580 if (history(h, &ev, H_CURR) != 0) 581 return(NULL); 582 *cindex = idx; 583 rptr = ev.str; 584 585 /* roll back to original position */ 586 (void)history(h, &ev, H_SET, num); 587 588 return rptr; 589 } 590 591 /* 592 * the real function doing history expansion - takes as argument command 593 * to do and data upon which the command should be executed 594 * does expansion the way I've understood readline documentation 595 * 596 * returns 0 if data was not modified, 1 if it was and 2 if the string 597 * should be only printed and not executed; in case of error, 598 * returns -1 and *result points to NULL 599 * it's callers responsibility to free() string returned in *result 600 */ 601 static int 602 _history_expand_command(const char *command, size_t offs, size_t cmdlen, 603 char **result) 604 { 605 char *tmp, *search = NULL, *aptr; 606 const char *ptr, *cmd; 607 static char *from = NULL, *to = NULL; 608 int start, end, idx, has_mods = 0; 609 int p_on = 0, g_on = 0; 610 611 *result = NULL; 612 aptr = NULL; 613 ptr = NULL; 614 615 /* First get event specifier */ 616 idx = 0; 617 618 if (strchr(":^*$", command[offs + 1])) { 619 char str[4]; 620 /* 621 * "!:" is shorthand for "!!:". 622 * "!^", "!*" and "!$" are shorthand for 623 * "!!:^", "!!:*" and "!!:$" respectively. 624 */ 625 str[0] = str[1] = '!'; 626 str[2] = '0'; 627 ptr = get_history_event(str, &idx, 0); 628 idx = (command[offs + 1] == ':')? 1:0; 629 has_mods = 1; 630 } else { 631 if (command[offs + 1] == '#') { 632 /* use command so far */ 633 if ((aptr = malloc(offs + 1)) == NULL) 634 return -1; 635 (void)strncpy(aptr, command, offs); 636 aptr[offs] = '\0'; 637 idx = 1; 638 } else { 639 int qchar; 640 641 qchar = (offs > 0 && command[offs - 1] == '"')? '"':0; 642 ptr = get_history_event(command + offs, &idx, qchar); 643 } 644 has_mods = command[offs + idx] == ':'; 645 } 646 647 if (ptr == NULL && aptr == NULL) 648 return(-1); 649 650 if (!has_mods) { 651 *result = strdup(aptr? aptr : ptr); 652 if (aptr) 653 free(aptr); 654 return(1); 655 } 656 657 cmd = command + offs + idx + 1; 658 659 /* Now parse any word designators */ 660 661 if (*cmd == '%') /* last word matched by ?pat? */ 662 tmp = strdup(last_search_match? last_search_match:""); 663 else if (strchr("^*$-0123456789", *cmd)) { 664 start = end = -1; 665 if (*cmd == '^') 666 start = end = 1, cmd++; 667 else if (*cmd == '$') 668 start = -1, cmd++; 669 else if (*cmd == '*') 670 start = 1, cmd++; 671 else if (*cmd == '-' || isdigit((unsigned char) *cmd)) { 672 start = 0; 673 while (*cmd && '0' <= *cmd && *cmd <= '9') 674 start = start * 10 + *cmd++ - '0'; 675 676 if (*cmd == '-') { 677 if (isdigit((unsigned char) cmd[1])) { 678 cmd++; 679 end = 0; 680 while (*cmd && '0' <= *cmd && *cmd <= '9') 681 end = end * 10 + *cmd++ - '0'; 682 } else if (cmd[1] == '$') { 683 cmd += 2; 684 end = -1; 685 } else { 686 cmd++; 687 end = -2; 688 } 689 } else if (*cmd == '*') 690 end = -1, cmd++; 691 else 692 end = start; 693 } 694 tmp = history_arg_extract(start, end, aptr? aptr:ptr); 695 if (tmp == NULL) { 696 (void)fprintf(rl_outstream, "%s: Bad word specifier", 697 command + offs + idx); 698 if (aptr) 699 free(aptr); 700 return(-1); 701 } 702 } else 703 tmp = strdup(aptr? aptr:ptr); 704 705 if (aptr) 706 free(aptr); 707 708 if (*cmd == '\0' || ((size_t)(cmd - (command + offs)) >= cmdlen)) { 709 *result = tmp; 710 return(1); 711 } 712 713 for (; *cmd; cmd++) { 714 if (*cmd == ':') 715 continue; 716 else if (*cmd == 'h') { /* remove trailing path */ 717 if ((aptr = strrchr(tmp, '/')) != NULL) 718 *aptr = '\0'; 719 } else if (*cmd == 't') { /* remove leading path */ 720 if ((aptr = strrchr(tmp, '/')) != NULL) { 721 aptr = strdup(aptr + 1); 722 free(tmp); 723 tmp = aptr; 724 } 725 } else if (*cmd == 'r') { /* remove trailing suffix */ 726 if ((aptr = strrchr(tmp, '.')) != NULL) 727 *aptr = '\0'; 728 } else if (*cmd == 'e') { /* remove all but suffix */ 729 if ((aptr = strrchr(tmp, '.')) != NULL) { 730 aptr = strdup(aptr); 731 free(tmp); 732 tmp = aptr; 733 } 734 } else if (*cmd == 'p') /* print only */ 735 p_on = 1; 736 else if (*cmd == 'g') 737 g_on = 2; 738 else if (*cmd == 's' || *cmd == '&') { 739 char *what, *with, delim; 740 size_t len, from_len; 741 size_t size; 742 743 if (*cmd == '&' && (from == NULL || to == NULL)) 744 continue; 745 else if (*cmd == 's') { 746 delim = *(++cmd), cmd++; 747 size = 16; 748 what = realloc(from, size); 749 if (what == NULL) { 750 free(from); 751 free(tmp); 752 return 0; 753 } 754 len = 0; 755 for (; *cmd && *cmd != delim; cmd++) { 756 if (*cmd == '\\' && cmd[1] == delim) 757 cmd++; 758 if (len >= size) { 759 char *nwhat; 760 nwhat = realloc(what, 761 (size <<= 1)); 762 if (nwhat == NULL) { 763 free(what); 764 free(tmp); 765 return 0; 766 } 767 what = nwhat; 768 } 769 what[len++] = *cmd; 770 } 771 what[len] = '\0'; 772 from = what; 773 if (*what == '\0') { 774 free(what); 775 if (search) { 776 from = strdup(search); 777 if (from == NULL) { 778 free(tmp); 779 return 0; 780 } 781 } else { 782 from = NULL; 783 free(tmp); 784 return (-1); 785 } 786 } 787 cmd++; /* shift after delim */ 788 if (!*cmd) 789 continue; 790 791 size = 16; 792 with = realloc(to, size); 793 if (with == NULL) { 794 free(to); 795 free(tmp); 796 return -1; 797 } 798 len = 0; 799 from_len = strlen(from); 800 for (; *cmd && *cmd != delim; cmd++) { 801 if (len + from_len + 1 >= size) { 802 char *nwith; 803 size += from_len + 1; 804 nwith = realloc(with, size); 805 if (nwith == NULL) { 806 free(with); 807 free(tmp); 808 return -1; 809 } 810 with = nwith; 811 } 812 if (*cmd == '&') { 813 /* safe */ 814 (void)strcpy(&with[len], from); 815 len += from_len; 816 continue; 817 } 818 if (*cmd == '\\' 819 && (*(cmd + 1) == delim 820 || *(cmd + 1) == '&')) 821 cmd++; 822 with[len++] = *cmd; 823 } 824 with[len] = '\0'; 825 to = with; 826 } 827 828 aptr = _rl_compat_sub(tmp, from, to, g_on); 829 if (aptr) { 830 free(tmp); 831 tmp = aptr; 832 } 833 g_on = 0; 834 } 835 } 836 *result = tmp; 837 return (p_on? 2:1); 838 } 839 840 841 /* 842 * csh-style history expansion 843 */ 844 int 845 history_expand(char *str, char **output) 846 { 847 int ret = 0; 848 size_t idx, i, size; 849 char *tmp, *result; 850 851 if (h == NULL || e == NULL) 852 rl_initialize(); 853 854 if (history_expansion_char == 0) { 855 *output = strdup(str); 856 return(0); 857 } 858 859 *output = NULL; 860 if (str[0] == history_subst_char) { 861 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */ 862 *output = malloc(strlen(str) + 4 + 1); 863 if (*output == NULL) 864 return 0; 865 (*output)[0] = (*output)[1] = history_expansion_char; 866 (*output)[2] = ':'; 867 (*output)[3] = 's'; 868 (void)strcpy((*output) + 4, str); 869 str = *output; 870 } else { 871 *output = strdup(str); 872 if (*output == NULL) 873 return 0; 874 } 875 876 #define ADD_STRING(what, len, fr) \ 877 { \ 878 if (idx + len + 1 > size) { \ 879 char *nresult = realloc(result, (size += len + 1));\ 880 if (nresult == NULL) { \ 881 free(*output); \ 882 if (/*CONSTCOND*/fr) \ 883 free(tmp); \ 884 return 0; \ 885 } \ 886 result = nresult; \ 887 } \ 888 (void)strncpy(&result[idx], what, len); \ 889 idx += len; \ 890 result[idx] = '\0'; \ 891 } 892 893 result = NULL; 894 size = idx = 0; 895 tmp = NULL; 896 for (i = 0; str[i];) { 897 int qchar, loop_again; 898 size_t len, start, j; 899 900 qchar = 0; 901 loop_again = 1; 902 start = j = i; 903 loop: 904 for (; str[j]; j++) { 905 if (str[j] == '\\' && 906 str[j + 1] == history_expansion_char) { 907 (void)strcpy(&str[j], &str[j + 1]); 908 continue; 909 } 910 if (!loop_again) { 911 if (isspace((unsigned char) str[j]) 912 || str[j] == qchar) 913 break; 914 } 915 if (str[j] == history_expansion_char 916 && !strchr(history_no_expand_chars, str[j + 1]) 917 && (!history_inhibit_expansion_function || 918 (*history_inhibit_expansion_function)(str, 919 (int)j) == 0)) 920 break; 921 } 922 923 if (str[j] && loop_again) { 924 i = j; 925 qchar = (j > 0 && str[j - 1] == '"' )? '"':0; 926 j++; 927 if (str[j] == history_expansion_char) 928 j++; 929 loop_again = 0; 930 goto loop; 931 } 932 len = i - start; 933 ADD_STRING(&str[start], len, 0); 934 935 if (str[i] == '\0' || str[i] != history_expansion_char) { 936 len = j - i; 937 ADD_STRING(&str[i], len, 0); 938 if (start == 0) 939 ret = 0; 940 else 941 ret = 1; 942 break; 943 } 944 ret = _history_expand_command (str, i, (j - i), &tmp); 945 if (ret > 0 && tmp) { 946 len = strlen(tmp); 947 ADD_STRING(tmp, len, 1); 948 } 949 if (tmp) { 950 free(tmp); 951 tmp = NULL; 952 } 953 i = j; 954 } 955 956 /* ret is 2 for "print only" option */ 957 if (ret == 2) { 958 add_history(result); 959 #ifdef GDB_411_HACK 960 /* gdb 4.11 has been shipped with readline, where */ 961 /* history_expand() returned -1 when the line */ 962 /* should not be executed; in readline 2.1+ */ 963 /* it should return 2 in such a case */ 964 ret = -1; 965 #endif 966 } 967 free(*output); 968 *output = result; 969 970 return (ret); 971 } 972 973 /* 974 * Return a string consisting of arguments of "str" from "start" to "end". 975 */ 976 char * 977 history_arg_extract(int start, int end, const char *str) 978 { 979 size_t i, len, max; 980 char **arr, *result; 981 982 arr = history_tokenize(str); 983 if (!arr) 984 return(NULL); 985 if (arr && *arr == NULL) { 986 free(arr); 987 return(NULL); 988 } 989 990 for (max = 0; arr[max]; max++) 991 continue; 992 max--; 993 994 if (start == '$') 995 start = (int)max; 996 if (end == '$') 997 end = (int)max; 998 if (end < 0) 999 end = (int)max + end + 1; 1000 if (start < 0) 1001 start = end; 1002 1003 if (start < 0 || end < 0 || (size_t)start > max || (size_t)end > max || start > end) 1004 return(NULL); 1005 1006 for (i = start, len = 0; i <= (size_t)end; i++) 1007 len += strlen(arr[i]) + 1; 1008 len++; 1009 result = malloc(len); 1010 if (result == NULL) 1011 return NULL; 1012 1013 for (i = start, len = 0; i <= (size_t)end; i++) { 1014 (void)strcpy(result + len, arr[i]); 1015 len += strlen(arr[i]); 1016 if (i < (size_t)end) 1017 result[len++] = ' '; 1018 } 1019 result[len] = '\0'; 1020 1021 for (i = 0; arr[i]; i++) 1022 free(arr[i]); 1023 free(arr); 1024 1025 return(result); 1026 } 1027 1028 /* 1029 * Parse the string into individual tokens, 1030 * similar to how shell would do it. 1031 */ 1032 char ** 1033 history_tokenize(const char *str) 1034 { 1035 int size = 1, idx = 0, i, start; 1036 size_t len; 1037 char **result = NULL, *temp, delim = '\0'; 1038 1039 for (i = 0; str[i];) { 1040 while (isspace((unsigned char) str[i])) 1041 i++; 1042 start = i; 1043 for (; str[i];) { 1044 if (str[i] == '\\') { 1045 if (str[i+1] != '\0') 1046 i++; 1047 } else if (str[i] == delim) 1048 delim = '\0'; 1049 else if (!delim && 1050 (isspace((unsigned char) str[i]) || 1051 strchr("()<>;&|$", str[i]))) 1052 break; 1053 else if (!delim && strchr("'`\"", str[i])) 1054 delim = str[i]; 1055 if (str[i]) 1056 i++; 1057 } 1058 1059 if (idx + 2 >= size) { 1060 char **nresult; 1061 size <<= 1; 1062 nresult = realloc(result, size * sizeof(char *)); 1063 if (nresult == NULL) { 1064 free(result); 1065 return NULL; 1066 } 1067 result = nresult; 1068 } 1069 len = i - start; 1070 temp = malloc(len + 1); 1071 if (temp == NULL) { 1072 for (i = 0; i < idx; i++) 1073 free(result[i]); 1074 free(result); 1075 return NULL; 1076 } 1077 (void)strncpy(temp, &str[start], len); 1078 temp[len] = '\0'; 1079 result[idx++] = temp; 1080 result[idx] = NULL; 1081 if (str[i]) 1082 i++; 1083 } 1084 return (result); 1085 } 1086 1087 1088 /* 1089 * limit size of history record to ``max'' events 1090 */ 1091 void 1092 stifle_history(int max) 1093 { 1094 HistEvent ev; 1095 1096 if (h == NULL || e == NULL) 1097 rl_initialize(); 1098 1099 if (history(h, &ev, H_SETSIZE, max) == 0) 1100 max_input_history = max; 1101 } 1102 1103 1104 /* 1105 * "unlimit" size of history - set the limit to maximum allowed int value 1106 */ 1107 int 1108 unstifle_history(void) 1109 { 1110 HistEvent ev; 1111 int omax; 1112 1113 history(h, &ev, H_SETSIZE, INT_MAX); 1114 omax = max_input_history; 1115 max_input_history = INT_MAX; 1116 return (omax); /* some value _must_ be returned */ 1117 } 1118 1119 1120 int 1121 history_is_stifled(void) 1122 { 1123 1124 /* cannot return true answer */ 1125 return (max_input_history != INT_MAX); 1126 } 1127 1128 1129 /* 1130 * read history from a file given 1131 */ 1132 int 1133 read_history(const char *filename) 1134 { 1135 HistEvent ev; 1136 1137 if (h == NULL || e == NULL) 1138 rl_initialize(); 1139 return (history(h, &ev, H_LOAD, filename) == -1); 1140 } 1141 1142 1143 /* 1144 * write history to a file given 1145 */ 1146 int 1147 write_history(const char *filename) 1148 { 1149 HistEvent ev; 1150 1151 if (h == NULL || e == NULL) 1152 rl_initialize(); 1153 return (history(h, &ev, H_SAVE, filename) == -1); 1154 } 1155 1156 1157 /* 1158 * returns history ``num''th event 1159 * 1160 * returned pointer points to static variable 1161 */ 1162 HIST_ENTRY * 1163 history_get(int num) 1164 { 1165 static HIST_ENTRY she; 1166 HistEvent ev; 1167 int curr_num; 1168 1169 if (h == NULL || e == NULL) 1170 rl_initialize(); 1171 1172 /* save current position */ 1173 if (history(h, &ev, H_CURR) != 0) 1174 return (NULL); 1175 curr_num = ev.num; 1176 1177 /* start from most recent */ 1178 if (history(h, &ev, H_FIRST) != 0) 1179 return (NULL); /* error */ 1180 1181 /* look backwards for event matching specified offset */ 1182 if (history(h, &ev, H_NEXT_EVENT, num + 1)) 1183 return (NULL); 1184 1185 she.line = ev.str; 1186 she.data = NULL; 1187 1188 /* restore pointer to where it was */ 1189 (void)history(h, &ev, H_SET, curr_num); 1190 1191 return (&she); 1192 } 1193 1194 1195 /* 1196 * add the line to history table 1197 */ 1198 int 1199 add_history(const char *line) 1200 { 1201 HistEvent ev; 1202 1203 if (h == NULL || e == NULL) 1204 rl_initialize(); 1205 1206 (void)history(h, &ev, H_ENTER, line); 1207 if (history(h, &ev, H_GETSIZE) == 0) 1208 history_length = ev.num; 1209 1210 return (!(history_length > 0)); /* return 0 if all is okay */ 1211 } 1212 1213 1214 /* 1215 * remove the specified entry from the history list and return it. 1216 */ 1217 HIST_ENTRY * 1218 remove_history(int num) 1219 { 1220 HIST_ENTRY *she; 1221 HistEvent ev; 1222 1223 if (h == NULL || e == NULL) 1224 rl_initialize(); 1225 1226 if (history(h, &ev, H_DEL, num) != 0) 1227 return NULL; 1228 1229 if ((she = malloc(sizeof(*she))) == NULL) 1230 return NULL; 1231 1232 she->line = ev.str; 1233 she->data = NULL; 1234 1235 return she; 1236 } 1237 1238 1239 /* 1240 * clear the history list - delete all entries 1241 */ 1242 void 1243 clear_history(void) 1244 { 1245 HistEvent ev; 1246 1247 history(h, &ev, H_CLEAR); 1248 } 1249 1250 1251 /* 1252 * returns offset of the current history event 1253 */ 1254 int 1255 where_history(void) 1256 { 1257 HistEvent ev; 1258 int curr_num, off; 1259 1260 if (history(h, &ev, H_CURR) != 0) 1261 return (0); 1262 curr_num = ev.num; 1263 1264 history(h, &ev, H_FIRST); 1265 off = 1; 1266 while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0) 1267 off++; 1268 1269 return (off); 1270 } 1271 1272 1273 /* 1274 * returns current history event or NULL if there is no such event 1275 */ 1276 HIST_ENTRY * 1277 current_history(void) 1278 { 1279 1280 return (_move_history(H_CURR)); 1281 } 1282 1283 1284 /* 1285 * returns total number of bytes history events' data are using 1286 */ 1287 int 1288 history_total_bytes(void) 1289 { 1290 HistEvent ev; 1291 int curr_num; 1292 size_t size; 1293 1294 if (history(h, &ev, H_CURR) != 0) 1295 return (-1); 1296 curr_num = ev.num; 1297 1298 history(h, &ev, H_FIRST); 1299 size = 0; 1300 do 1301 size += strlen(ev.str); 1302 while (history(h, &ev, H_NEXT) == 0); 1303 1304 /* get to the same position as before */ 1305 history(h, &ev, H_PREV_EVENT, curr_num); 1306 1307 return (int)(size); 1308 } 1309 1310 1311 /* 1312 * sets the position in the history list to ``pos'' 1313 */ 1314 int 1315 history_set_pos(int pos) 1316 { 1317 HistEvent ev; 1318 int curr_num; 1319 1320 if (pos > history_length || pos < 0) 1321 return (-1); 1322 1323 history(h, &ev, H_CURR); 1324 curr_num = ev.num; 1325 1326 if (history(h, &ev, H_SET, pos)) { 1327 history(h, &ev, H_SET, curr_num); 1328 return(-1); 1329 } 1330 return (0); 1331 } 1332 1333 1334 /* 1335 * returns previous event in history and shifts pointer accordingly 1336 */ 1337 HIST_ENTRY * 1338 previous_history(void) 1339 { 1340 1341 return (_move_history(H_PREV)); 1342 } 1343 1344 1345 /* 1346 * returns next event in history and shifts pointer accordingly 1347 */ 1348 HIST_ENTRY * 1349 next_history(void) 1350 { 1351 1352 return (_move_history(H_NEXT)); 1353 } 1354 1355 1356 /* 1357 * searches for first history event containing the str 1358 */ 1359 int 1360 history_search(const char *str, int direction) 1361 { 1362 HistEvent ev; 1363 const char *strp; 1364 int curr_num; 1365 1366 if (history(h, &ev, H_CURR) != 0) 1367 return (-1); 1368 curr_num = ev.num; 1369 1370 for (;;) { 1371 if ((strp = strstr(ev.str, str)) != NULL) 1372 return (int) (strp - ev.str); 1373 if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0) 1374 break; 1375 } 1376 history(h, &ev, H_SET, curr_num); 1377 return (-1); 1378 } 1379 1380 1381 /* 1382 * searches for first history event beginning with str 1383 */ 1384 int 1385 history_search_prefix(const char *str, int direction) 1386 { 1387 HistEvent ev; 1388 1389 return (history(h, &ev, direction < 0? H_PREV_STR:H_NEXT_STR, str)); 1390 } 1391 1392 1393 /* 1394 * search for event in history containing str, starting at offset 1395 * abs(pos); continue backward, if pos<0, forward otherwise 1396 */ 1397 /* ARGSUSED */ 1398 int 1399 history_search_pos(const char *str, 1400 int direction __attribute__((__unused__)), int pos) 1401 { 1402 HistEvent ev; 1403 int curr_num, off; 1404 1405 off = (pos > 0) ? pos : -pos; 1406 pos = (pos > 0) ? 1 : -1; 1407 1408 if (history(h, &ev, H_CURR) != 0) 1409 return (-1); 1410 curr_num = ev.num; 1411 1412 if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0) 1413 return (-1); 1414 1415 1416 for (;;) { 1417 if (strstr(ev.str, str)) 1418 return (off); 1419 if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0) 1420 break; 1421 } 1422 1423 /* set "current" pointer back to previous state */ 1424 history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num); 1425 1426 return (-1); 1427 } 1428 1429 1430 /********************************/ 1431 /* completion functions */ 1432 1433 char * 1434 tilde_expand(char *name) 1435 { 1436 return fn_tilde_expand(name); 1437 } 1438 1439 char * 1440 filename_completion_function(const char *name, int state) 1441 { 1442 return fn_filename_completion_function(name, state); 1443 } 1444 1445 /* 1446 * a completion generator for usernames; returns _first_ username 1447 * which starts with supplied text 1448 * text contains a partial username preceded by random character 1449 * (usually '~'); state is ignored 1450 * it's callers responsibility to free returned value 1451 */ 1452 char * 1453 username_completion_function(const char *text, int state) 1454 { 1455 struct passwd *pwd, pwres; 1456 char pwbuf[1024]; 1457 1458 if (text[0] == '\0') 1459 return (NULL); 1460 1461 if (*text == '~') 1462 text++; 1463 1464 if (state == 0) 1465 setpwent(); 1466 1467 while (getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pwd) == 0 1468 && pwd != NULL && text[0] == pwd->pw_name[0] 1469 && strcmp(text, pwd->pw_name) == 0); 1470 1471 if (pwd == NULL) { 1472 endpwent(); 1473 return (NULL); 1474 } 1475 return (strdup(pwd->pw_name)); 1476 } 1477 1478 1479 /* 1480 * el-compatible wrapper to send TSTP on ^Z 1481 */ 1482 /* ARGSUSED */ 1483 static unsigned char 1484 _el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__))) 1485 { 1486 (void)kill(0, SIGTSTP); 1487 return CC_NORM; 1488 } 1489 1490 /* 1491 * Display list of strings in columnar format on readline's output stream. 1492 * 'matches' is list of strings, 'len' is number of strings in 'matches', 1493 * 'max' is maximum length of string in 'matches'. 1494 */ 1495 void 1496 rl_display_match_list(char **matches, int len, int max) 1497 { 1498 1499 fn_display_match_list(e, matches, (size_t)len, (size_t)max); 1500 } 1501 1502 static const char * 1503 /*ARGSUSED*/ 1504 _rl_completion_append_character_function(const char *dummy 1505 __attribute__((__unused__))) 1506 { 1507 static char buf[2]; 1508 buf[0] = rl_completion_append_character; 1509 buf[1] = '\0'; 1510 return buf; 1511 } 1512 1513 1514 /* 1515 * complete word at current point 1516 */ 1517 /* ARGSUSED */ 1518 int 1519 rl_complete(int ignore __attribute__((__unused__)), int invoking_key) 1520 { 1521 if (h == NULL || e == NULL) 1522 rl_initialize(); 1523 1524 if (rl_inhibit_completion) { 1525 char arr[2]; 1526 arr[0] = (char)invoking_key; 1527 arr[1] = '\0'; 1528 el_insertstr(e, arr); 1529 return (CC_REFRESH); 1530 } 1531 1532 /* Just look at how many global variables modify this operation! */ 1533 return fn_complete(e, 1534 (CPFunction *)rl_completion_entry_function, 1535 rl_attempted_completion_function, 1536 rl_basic_word_break_characters, rl_special_prefixes, 1537 _rl_completion_append_character_function, 1538 (size_t)rl_completion_query_items, 1539 &rl_completion_type, &rl_attempted_completion_over, 1540 &rl_point, &rl_end); 1541 } 1542 1543 1544 /* ARGSUSED */ 1545 static unsigned char 1546 _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch) 1547 { 1548 return (unsigned char)rl_complete(0, ch); 1549 } 1550 1551 /* 1552 * misc other functions 1553 */ 1554 1555 /* 1556 * bind key c to readline-type function func 1557 */ 1558 int 1559 rl_bind_key(int c, int func(int, int)) 1560 { 1561 int retval = -1; 1562 1563 if (h == NULL || e == NULL) 1564 rl_initialize(); 1565 1566 if (func == rl_insert) { 1567 /* XXX notice there is no range checking of ``c'' */ 1568 e->el_map.key[c] = ED_INSERT; 1569 retval = 0; 1570 } 1571 return (retval); 1572 } 1573 1574 1575 /* 1576 * read one key from input - handles chars pushed back 1577 * to input stream also 1578 */ 1579 int 1580 rl_read_key(void) 1581 { 1582 char fooarr[2 * sizeof(int)]; 1583 1584 if (e == NULL || h == NULL) 1585 rl_initialize(); 1586 1587 return (el_getc(e, fooarr)); 1588 } 1589 1590 1591 /* 1592 * reset the terminal 1593 */ 1594 /* ARGSUSED */ 1595 void 1596 rl_reset_terminal(const char *p __attribute__((__unused__))) 1597 { 1598 1599 if (h == NULL || e == NULL) 1600 rl_initialize(); 1601 el_reset(e); 1602 } 1603 1604 1605 /* 1606 * insert character ``c'' back into input stream, ``count'' times 1607 */ 1608 int 1609 rl_insert(int count, int c) 1610 { 1611 char arr[2]; 1612 1613 if (h == NULL || e == NULL) 1614 rl_initialize(); 1615 1616 /* XXX - int -> char conversion can lose on multichars */ 1617 arr[0] = c; 1618 arr[1] = '\0'; 1619 1620 for (; count > 0; count--) 1621 el_push(e, arr); 1622 1623 return (0); 1624 } 1625 1626 /*ARGSUSED*/ 1627 int 1628 rl_newline(int count, int c) 1629 { 1630 /* 1631 * Readline-4.0 appears to ignore the args. 1632 */ 1633 return rl_insert(1, '\n'); 1634 } 1635 1636 /*ARGSUSED*/ 1637 static unsigned char 1638 rl_bind_wrapper(EditLine *el, unsigned char c) 1639 { 1640 if (map[c] == NULL) 1641 return CC_ERROR; 1642 1643 _rl_update_pos(); 1644 1645 (*map[c])(NULL, c); 1646 1647 /* If rl_done was set by the above call, deal with it here */ 1648 if (rl_done) 1649 return CC_EOF; 1650 1651 return CC_NORM; 1652 } 1653 1654 int 1655 rl_add_defun(const char *name, Function *fun, int c) 1656 { 1657 char dest[8]; 1658 if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0) 1659 return -1; 1660 map[(unsigned char)c] = fun; 1661 el_set(e, EL_ADDFN, name, name, rl_bind_wrapper); 1662 vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0); 1663 el_set(e, EL_BIND, dest, name); 1664 return 0; 1665 } 1666 1667 void 1668 rl_callback_read_char() 1669 { 1670 int count = 0, done = 0; 1671 const char *buf = el_gets(e, &count); 1672 char *wbuf; 1673 1674 if (buf == NULL || count-- <= 0) 1675 return; 1676 if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF]) 1677 done = 1; 1678 if (buf[count] == '\n' || buf[count] == '\r') 1679 done = 2; 1680 1681 if (done && rl_linefunc != NULL) { 1682 el_set(e, EL_UNBUFFERED, 0); 1683 if (done == 2) { 1684 if ((wbuf = strdup(buf)) != NULL) 1685 wbuf[count] = '\0'; 1686 } else 1687 wbuf = NULL; 1688 (*(void (*)(const char *))rl_linefunc)(wbuf); 1689 el_set(e, EL_UNBUFFERED, 1); 1690 } 1691 } 1692 1693 void 1694 rl_callback_handler_install(const char *prompt, VCPFunction *linefunc) 1695 { 1696 if (e == NULL) { 1697 rl_initialize(); 1698 } 1699 (void)rl_set_prompt(prompt); 1700 rl_linefunc = linefunc; 1701 el_set(e, EL_UNBUFFERED, 1); 1702 } 1703 1704 void 1705 rl_callback_handler_remove(void) 1706 { 1707 el_set(e, EL_UNBUFFERED, 0); 1708 rl_linefunc = NULL; 1709 } 1710 1711 void 1712 rl_redisplay(void) 1713 { 1714 char a[2]; 1715 a[0] = e->el_tty.t_c[TS_IO][C_REPRINT]; 1716 a[1] = '\0'; 1717 el_push(e, a); 1718 } 1719 1720 int 1721 rl_get_previous_history(int count, int key) 1722 { 1723 char a[2]; 1724 a[0] = key; 1725 a[1] = '\0'; 1726 while (count--) 1727 el_push(e, a); 1728 return 0; 1729 } 1730 1731 void 1732 /*ARGSUSED*/ 1733 rl_prep_terminal(int meta_flag) 1734 { 1735 el_set(e, EL_PREP_TERM, 1); 1736 } 1737 1738 void 1739 rl_deprep_terminal(void) 1740 { 1741 el_set(e, EL_PREP_TERM, 0); 1742 } 1743 1744 int 1745 rl_read_init_file(const char *s) 1746 { 1747 return(el_source(e, s)); 1748 } 1749 1750 int 1751 rl_parse_and_bind(const char *line) 1752 { 1753 const char **argv; 1754 int argc; 1755 Tokenizer *tok; 1756 1757 tok = tok_init(NULL); 1758 tok_str(tok, line, &argc, &argv); 1759 argc = el_parse(e, argc, argv); 1760 tok_end(tok); 1761 return (argc ? 1 : 0); 1762 } 1763 1764 int 1765 rl_variable_bind(const char *var, const char *value) 1766 { 1767 /* 1768 * The proper return value is undocument, but this is what the 1769 * readline source seems to do. 1770 */ 1771 return ((el_set(e, EL_BIND, "", var, value) == -1) ? 1 : 0); 1772 } 1773 1774 void 1775 rl_stuff_char(int c) 1776 { 1777 char buf[2]; 1778 1779 buf[0] = c; 1780 buf[1] = '\0'; 1781 el_insertstr(e, buf); 1782 } 1783 1784 static int 1785 _rl_event_read_char(EditLine *el, char *cp) 1786 { 1787 int n; 1788 ssize_t num_read = 0; 1789 1790 *cp = '\0'; 1791 while (rl_event_hook) { 1792 1793 (*rl_event_hook)(); 1794 1795 #if defined(FIONREAD) 1796 if (ioctl(el->el_infd, FIONREAD, &n) < 0) 1797 return(-1); 1798 if (n) 1799 num_read = read(el->el_infd, cp, 1); 1800 else 1801 num_read = 0; 1802 #elif defined(F_SETFL) && defined(O_NDELAY) 1803 if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0) 1804 return(-1); 1805 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0) 1806 return(-1); 1807 num_read = read(el->el_infd, cp, 1); 1808 if (fcntl(el->el_infd, F_SETFL, n)) 1809 return(-1); 1810 #else 1811 /* not non-blocking, but what you gonna do? */ 1812 num_read = read(el->el_infd, cp, 1); 1813 return(-1); 1814 #endif 1815 1816 if (num_read < 0 && errno == EAGAIN) 1817 continue; 1818 if (num_read == 0) 1819 continue; 1820 break; 1821 } 1822 if (!rl_event_hook) 1823 el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN); 1824 return (int)num_read; 1825 } 1826 1827 static void 1828 _rl_update_pos(void) 1829 { 1830 const LineInfo *li = el_line(e); 1831 1832 rl_point = (int)(li->cursor - li->buffer); 1833 rl_end = (int)(li->lastchar - li->buffer); 1834 } 1835 1836 void 1837 rl_get_screen_size(int *rows, int *cols) 1838 { 1839 if (rows) 1840 el_get(e, EL_GETTC, "li", rows); 1841 if (cols) 1842 el_get(e, EL_GETTC, "co", cols); 1843 } 1844 1845 void 1846 rl_set_screen_size(int rows, int cols) 1847 { 1848 char buf[64]; 1849 (void)snprintf(buf, sizeof(buf), "%d", rows); 1850 el_set(e, EL_SETTC, "li", buf); 1851 (void)snprintf(buf, sizeof(buf), "%d", cols); 1852 el_set(e, EL_SETTC, "co", buf); 1853 } 1854 1855 char ** 1856 rl_completion_matches(const char *str, rl_compentry_func_t *fun) 1857 { 1858 size_t len, max, i, j, min; 1859 char **list, *match, *a, *b; 1860 1861 len = 1; 1862 max = 10; 1863 if ((list = malloc(max * sizeof(*list))) == NULL) 1864 return NULL; 1865 1866 while ((match = (*fun)(str, (int)(len - 1))) != NULL) { 1867 list[len++] = match; 1868 if (len == max) { 1869 char **nl; 1870 max += 10; 1871 if ((nl = realloc(list, max * sizeof(*nl))) == NULL) 1872 goto out; 1873 list = nl; 1874 } 1875 } 1876 if (len == 1) 1877 goto out; 1878 list[len] = NULL; 1879 if (len == 2) { 1880 if ((list[0] = strdup(list[1])) == NULL) 1881 goto out; 1882 return list; 1883 } 1884 qsort(&list[1], len - 1, sizeof(*list), 1885 (int (*)(const void *, const void *)) strcmp); 1886 min = SIZE_T_MAX; 1887 for (i = 1, a = list[i]; i < len - 1; i++, a = b) { 1888 b = list[i + 1]; 1889 for (j = 0; a[j] && a[j] == b[j]; j++) 1890 continue; 1891 if (min > j) 1892 min = j; 1893 } 1894 if (min == 0 && *str) { 1895 if ((list[0] = strdup(str)) == NULL) 1896 goto out; 1897 } else { 1898 if ((list[0] = malloc(min + 1)) == NULL) 1899 goto out; 1900 (void)memcpy(list[0], list[1], min); 1901 list[0][min] = '\0'; 1902 } 1903 return list; 1904 1905 out: 1906 free(list); 1907 return NULL; 1908 } 1909 1910 char * 1911 rl_filename_completion_function (const char *text, int state) 1912 { 1913 return fn_filename_completion_function(text, state); 1914 } 1915 1916 void 1917 rl_forced_update_display(void) 1918 { 1919 el_set(e, EL_REFRESH); 1920 } 1921 1922 int 1923 _rl_abort_internal(void) 1924 { 1925 el_beep(e); 1926 longjmp(topbuf, 1); 1927 /*NOTREACHED*/ 1928 } 1929 1930 int 1931 _rl_qsort_string_compare(char **s1, char **s2) 1932 { 1933 return strcoll(*s1, *s2); 1934 } 1935 1936 int 1937 /*ARGSUSED*/ 1938 rl_kill_text(int from, int to) 1939 { 1940 return 0; 1941 } 1942 1943 Keymap 1944 rl_make_bare_keymap(void) 1945 { 1946 return NULL; 1947 } 1948 1949 Keymap 1950 rl_get_keymap(void) 1951 { 1952 return NULL; 1953 } 1954 1955 void 1956 /*ARGSUSED*/ 1957 rl_set_keymap(Keymap k) 1958 { 1959 } 1960 1961 int 1962 /*ARGSUSED*/ 1963 rl_generic_bind(int type, const char * keyseq, const char * data, Keymap k) 1964 { 1965 return 0; 1966 } 1967 1968 int 1969 /*ARGSUSED*/ 1970 rl_bind_key_in_map(int key, Function *fun, Keymap k) 1971 { 1972 return 0; 1973 } 1974