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