1 /* $NetBSD: readline.c,v 1.61 2006/03/06 21:11:03 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 * 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.61 2006/03/06 21:11:03 christos 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 return 0; 732 } 733 len = 0; 734 for (; *cmd && *cmd != delim; cmd++) { 735 if (*cmd == '\\' && cmd[1] == delim) 736 cmd++; 737 if (len >= size) { 738 char *nwhat; 739 nwhat = realloc(what, 740 (size <<= 1)); 741 if (nwhat == NULL) { 742 free(what); 743 return 0; 744 } 745 what = nwhat; 746 } 747 what[len++] = *cmd; 748 } 749 what[len] = '\0'; 750 from = what; 751 if (*what == '\0') { 752 free(what); 753 if (search) { 754 from = strdup(search); 755 if (from == NULL) 756 return 0; 757 } else { 758 from = NULL; 759 return (-1); 760 } 761 } 762 cmd++; /* shift after delim */ 763 if (!*cmd) 764 continue; 765 766 size = 16; 767 with = realloc(to, size); 768 if (with == NULL) { 769 free(to); 770 return -1; 771 } 772 len = 0; 773 from_len = strlen(from); 774 for (; *cmd && *cmd != delim; cmd++) { 775 if (len + from_len + 1 >= size) { 776 char *nwith; 777 size += from_len + 1; 778 nwith = realloc(with, size); 779 if (nwith == NULL) { 780 free(with); 781 return -1; 782 } 783 with = nwith; 784 } 785 if (*cmd == '&') { 786 /* safe */ 787 (void)strcpy(&with[len], from); 788 len += from_len; 789 continue; 790 } 791 if (*cmd == '\\' 792 && (*(cmd + 1) == delim 793 || *(cmd + 1) == '&')) 794 cmd++; 795 with[len++] = *cmd; 796 } 797 with[len] = '\0'; 798 to = with; 799 } 800 801 aptr = _rl_compat_sub(tmp, from, to, g_on); 802 if (aptr) { 803 free(tmp); 804 tmp = aptr; 805 } 806 g_on = 0; 807 } 808 } 809 *result = tmp; 810 return (p_on? 2:1); 811 } 812 813 814 /* 815 * csh-style history expansion 816 */ 817 int 818 history_expand(char *str, char **output) 819 { 820 int ret = 0; 821 size_t idx, i, size; 822 char *tmp, *result; 823 824 if (h == NULL || e == NULL) 825 rl_initialize(); 826 827 if (history_expansion_char == 0) { 828 *output = strdup(str); 829 return(0); 830 } 831 832 *output = NULL; 833 if (str[0] == history_subst_char) { 834 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */ 835 *output = malloc(strlen(str) + 4 + 1); 836 if (*output == NULL) 837 return 0; 838 (*output)[0] = (*output)[1] = history_expansion_char; 839 (*output)[2] = ':'; 840 (*output)[3] = 's'; 841 (void)strcpy((*output) + 4, str); 842 str = *output; 843 } else { 844 *output = strdup(str); 845 if (*output == NULL) 846 return 0; 847 } 848 849 #define ADD_STRING(what, len) \ 850 { \ 851 if (idx + len + 1 > size) { \ 852 char *nresult = realloc(result, (size += len + 1));\ 853 if (nresult == NULL) { \ 854 free(*output); \ 855 return 0; \ 856 } \ 857 result = nresult; \ 858 } \ 859 (void)strncpy(&result[idx], what, len); \ 860 idx += len; \ 861 result[idx] = '\0'; \ 862 } 863 864 result = NULL; 865 size = idx = 0; 866 for (i = 0; str[i];) { 867 int qchar, loop_again; 868 size_t len, start, j; 869 870 qchar = 0; 871 loop_again = 1; 872 start = j = i; 873 loop: 874 for (; str[j]; j++) { 875 if (str[j] == '\\' && 876 str[j + 1] == history_expansion_char) { 877 (void)strcpy(&str[j], &str[j + 1]); 878 continue; 879 } 880 if (!loop_again) { 881 if (isspace((unsigned char) str[j]) 882 || str[j] == qchar) 883 break; 884 } 885 if (str[j] == history_expansion_char 886 && !strchr(history_no_expand_chars, str[j + 1]) 887 && (!history_inhibit_expansion_function || 888 (*history_inhibit_expansion_function)(str, 889 (int)j) == 0)) 890 break; 891 } 892 893 if (str[j] && loop_again) { 894 i = j; 895 qchar = (j > 0 && str[j - 1] == '"' )? '"':0; 896 j++; 897 if (str[j] == history_expansion_char) 898 j++; 899 loop_again = 0; 900 goto loop; 901 } 902 len = i - start; 903 tmp = &str[start]; 904 ADD_STRING(tmp, len); 905 906 if (str[i] == '\0' || str[i] != history_expansion_char) { 907 len = j - i; 908 tmp = &str[i]; 909 ADD_STRING(tmp, len); 910 if (start == 0) 911 ret = 0; 912 else 913 ret = 1; 914 break; 915 } 916 ret = _history_expand_command (str, i, (j - i), &tmp); 917 if (ret > 0 && tmp) { 918 len = strlen(tmp); 919 ADD_STRING(tmp, len); 920 free(tmp); 921 } 922 i = j; 923 } 924 925 /* ret is 2 for "print only" option */ 926 if (ret == 2) { 927 add_history(result); 928 #ifdef GDB_411_HACK 929 /* gdb 4.11 has been shipped with readline, where */ 930 /* history_expand() returned -1 when the line */ 931 /* should not be executed; in readline 2.1+ */ 932 /* it should return 2 in such a case */ 933 ret = -1; 934 #endif 935 } 936 free(*output); 937 *output = result; 938 939 return (ret); 940 } 941 942 /* 943 * Return a string consisting of arguments of "str" from "start" to "end". 944 */ 945 char * 946 history_arg_extract(int start, int end, const char *str) 947 { 948 size_t i, len, max; 949 char **arr, *result; 950 951 arr = history_tokenize(str); 952 if (!arr) 953 return(NULL); 954 if (arr && *arr == NULL) { 955 free(arr); 956 return(NULL); 957 } 958 959 for (max = 0; arr[max]; max++) 960 continue; 961 max--; 962 963 if (start == '$') 964 start = max; 965 if (end == '$') 966 end = max; 967 if (end < 0) 968 end = max + end + 1; 969 if (start < 0) 970 start = end; 971 972 if (start < 0 || end < 0 || start > max || end > max || start > end) 973 return(NULL); 974 975 for (i = start, len = 0; i <= end; i++) 976 len += strlen(arr[i]) + 1; 977 len++; 978 result = malloc(len); 979 if (result == NULL) 980 return NULL; 981 982 for (i = start, len = 0; i <= end; i++) { 983 (void)strcpy(result + len, arr[i]); 984 len += strlen(arr[i]); 985 if (i < end) 986 result[len++] = ' '; 987 } 988 result[len] = 0; 989 990 for (i = 0; arr[i]; i++) 991 free(arr[i]); 992 free(arr); 993 994 return(result); 995 } 996 997 /* 998 * Parse the string into individual tokens, 999 * similar to how shell would do it. 1000 */ 1001 char ** 1002 history_tokenize(const char *str) 1003 { 1004 int size = 1, idx = 0, i, start; 1005 size_t len; 1006 char **result = NULL, *temp, delim = '\0'; 1007 1008 for (i = 0; str[i];) { 1009 while (isspace((unsigned char) str[i])) 1010 i++; 1011 start = i; 1012 for (; str[i];) { 1013 if (str[i] == '\\') { 1014 if (str[i+1] != '\0') 1015 i++; 1016 } else if (str[i] == delim) 1017 delim = '\0'; 1018 else if (!delim && 1019 (isspace((unsigned char) str[i]) || 1020 strchr("()<>;&|$", str[i]))) 1021 break; 1022 else if (!delim && strchr("'`\"", str[i])) 1023 delim = str[i]; 1024 if (str[i]) 1025 i++; 1026 } 1027 1028 if (idx + 2 >= size) { 1029 char **nresult; 1030 size <<= 1; 1031 nresult = realloc(result, size * sizeof(char *)); 1032 if (nresult == NULL) { 1033 free(result); 1034 return NULL; 1035 } 1036 result = nresult; 1037 } 1038 len = i - start; 1039 temp = malloc(len + 1); 1040 if (temp == NULL) { 1041 for (i = 0; i < idx; i++) 1042 free(result[i]); 1043 free(result); 1044 return NULL; 1045 } 1046 (void)strncpy(temp, &str[start], len); 1047 temp[len] = '\0'; 1048 result[idx++] = temp; 1049 result[idx] = NULL; 1050 if (str[i]) 1051 i++; 1052 } 1053 return (result); 1054 } 1055 1056 1057 /* 1058 * limit size of history record to ``max'' events 1059 */ 1060 void 1061 stifle_history(int max) 1062 { 1063 HistEvent ev; 1064 1065 if (h == NULL || e == NULL) 1066 rl_initialize(); 1067 1068 if (history(h, &ev, H_SETSIZE, max) == 0) 1069 max_input_history = max; 1070 } 1071 1072 1073 /* 1074 * "unlimit" size of history - set the limit to maximum allowed int value 1075 */ 1076 int 1077 unstifle_history(void) 1078 { 1079 HistEvent ev; 1080 int omax; 1081 1082 history(h, &ev, H_SETSIZE, INT_MAX); 1083 omax = max_input_history; 1084 max_input_history = INT_MAX; 1085 return (omax); /* some value _must_ be returned */ 1086 } 1087 1088 1089 int 1090 history_is_stifled(void) 1091 { 1092 1093 /* cannot return true answer */ 1094 return (max_input_history != INT_MAX); 1095 } 1096 1097 1098 /* 1099 * read history from a file given 1100 */ 1101 int 1102 read_history(const char *filename) 1103 { 1104 HistEvent ev; 1105 1106 if (h == NULL || e == NULL) 1107 rl_initialize(); 1108 return (history(h, &ev, H_LOAD, filename) == -1); 1109 } 1110 1111 1112 /* 1113 * write history to a file given 1114 */ 1115 int 1116 write_history(const char *filename) 1117 { 1118 HistEvent ev; 1119 1120 if (h == NULL || e == NULL) 1121 rl_initialize(); 1122 return (history(h, &ev, H_SAVE, filename) == -1); 1123 } 1124 1125 1126 /* 1127 * returns history ``num''th event 1128 * 1129 * returned pointer points to static variable 1130 */ 1131 HIST_ENTRY * 1132 history_get(int num) 1133 { 1134 static HIST_ENTRY she; 1135 HistEvent ev; 1136 int curr_num; 1137 1138 if (h == NULL || e == NULL) 1139 rl_initialize(); 1140 1141 /* save current position */ 1142 if (history(h, &ev, H_CURR) != 0) 1143 return (NULL); 1144 curr_num = ev.num; 1145 1146 /* start from most recent */ 1147 if (history(h, &ev, H_FIRST) != 0) 1148 return (NULL); /* error */ 1149 1150 /* look backwards for event matching specified offset */ 1151 if (history(h, &ev, H_NEXT_EVENT, num)) 1152 return (NULL); 1153 1154 she.line = ev.str; 1155 she.data = NULL; 1156 1157 /* restore pointer to where it was */ 1158 (void)history(h, &ev, H_SET, curr_num); 1159 1160 return (&she); 1161 } 1162 1163 1164 /* 1165 * add the line to history table 1166 */ 1167 int 1168 add_history(const char *line) 1169 { 1170 HistEvent ev; 1171 1172 if (h == NULL || e == NULL) 1173 rl_initialize(); 1174 1175 (void)history(h, &ev, H_ENTER, line); 1176 if (history(h, &ev, H_GETSIZE) == 0) 1177 history_length = ev.num; 1178 1179 return (!(history_length > 0)); /* return 0 if all is okay */ 1180 } 1181 1182 1183 /* 1184 * remove the specified entry from the history list and return it. 1185 */ 1186 HIST_ENTRY * 1187 remove_history(int num) 1188 { 1189 static HIST_ENTRY she; 1190 HistEvent ev; 1191 1192 if (h == NULL || e == NULL) 1193 rl_initialize(); 1194 1195 if (history(h, &ev, H_DEL, num) != 0) 1196 return NULL; 1197 1198 she.line = ev.str; 1199 she.data = NULL; 1200 1201 return &she; 1202 } 1203 1204 1205 /* 1206 * clear the history list - delete all entries 1207 */ 1208 void 1209 clear_history(void) 1210 { 1211 HistEvent ev; 1212 1213 history(h, &ev, H_CLEAR); 1214 } 1215 1216 1217 /* 1218 * returns offset of the current history event 1219 */ 1220 int 1221 where_history(void) 1222 { 1223 HistEvent ev; 1224 int curr_num, off; 1225 1226 if (history(h, &ev, H_CURR) != 0) 1227 return (0); 1228 curr_num = ev.num; 1229 1230 history(h, &ev, H_FIRST); 1231 off = 1; 1232 while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0) 1233 off++; 1234 1235 return (off); 1236 } 1237 1238 1239 /* 1240 * returns current history event or NULL if there is no such event 1241 */ 1242 HIST_ENTRY * 1243 current_history(void) 1244 { 1245 1246 return (_move_history(H_CURR)); 1247 } 1248 1249 1250 /* 1251 * returns total number of bytes history events' data are using 1252 */ 1253 int 1254 history_total_bytes(void) 1255 { 1256 HistEvent ev; 1257 int curr_num, size; 1258 1259 if (history(h, &ev, H_CURR) != 0) 1260 return (-1); 1261 curr_num = ev.num; 1262 1263 history(h, &ev, H_FIRST); 1264 size = 0; 1265 do 1266 size += strlen(ev.str); 1267 while (history(h, &ev, H_NEXT) == 0); 1268 1269 /* get to the same position as before */ 1270 history(h, &ev, H_PREV_EVENT, curr_num); 1271 1272 return (size); 1273 } 1274 1275 1276 /* 1277 * sets the position in the history list to ``pos'' 1278 */ 1279 int 1280 history_set_pos(int pos) 1281 { 1282 HistEvent ev; 1283 int curr_num; 1284 1285 if (pos > history_length || pos < 0) 1286 return (-1); 1287 1288 history(h, &ev, H_CURR); 1289 curr_num = ev.num; 1290 1291 if (history(h, &ev, H_SET, pos)) { 1292 history(h, &ev, H_SET, curr_num); 1293 return(-1); 1294 } 1295 return (0); 1296 } 1297 1298 1299 /* 1300 * returns previous event in history and shifts pointer accordingly 1301 */ 1302 HIST_ENTRY * 1303 previous_history(void) 1304 { 1305 1306 return (_move_history(H_PREV)); 1307 } 1308 1309 1310 /* 1311 * returns next event in history and shifts pointer accordingly 1312 */ 1313 HIST_ENTRY * 1314 next_history(void) 1315 { 1316 1317 return (_move_history(H_NEXT)); 1318 } 1319 1320 1321 /* 1322 * searches for first history event containing the str 1323 */ 1324 int 1325 history_search(const char *str, int direction) 1326 { 1327 HistEvent ev; 1328 const char *strp; 1329 int curr_num; 1330 1331 if (history(h, &ev, H_CURR) != 0) 1332 return (-1); 1333 curr_num = ev.num; 1334 1335 for (;;) { 1336 if ((strp = strstr(ev.str, str)) != NULL) 1337 return (int) (strp - ev.str); 1338 if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0) 1339 break; 1340 } 1341 history(h, &ev, H_SET, curr_num); 1342 return (-1); 1343 } 1344 1345 1346 /* 1347 * searches for first history event beginning with str 1348 */ 1349 int 1350 history_search_prefix(const char *str, int direction) 1351 { 1352 HistEvent ev; 1353 1354 return (history(h, &ev, direction < 0? H_PREV_STR:H_NEXT_STR, str)); 1355 } 1356 1357 1358 /* 1359 * search for event in history containing str, starting at offset 1360 * abs(pos); continue backward, if pos<0, forward otherwise 1361 */ 1362 /* ARGSUSED */ 1363 int 1364 history_search_pos(const char *str, 1365 int direction __attribute__((__unused__)), int pos) 1366 { 1367 HistEvent ev; 1368 int curr_num, off; 1369 1370 off = (pos > 0) ? pos : -pos; 1371 pos = (pos > 0) ? 1 : -1; 1372 1373 if (history(h, &ev, H_CURR) != 0) 1374 return (-1); 1375 curr_num = ev.num; 1376 1377 if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0) 1378 return (-1); 1379 1380 1381 for (;;) { 1382 if (strstr(ev.str, str)) 1383 return (off); 1384 if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0) 1385 break; 1386 } 1387 1388 /* set "current" pointer back to previous state */ 1389 history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num); 1390 1391 return (-1); 1392 } 1393 1394 1395 /********************************/ 1396 /* completion functions */ 1397 1398 char * 1399 tilde_expand(char *name) 1400 { 1401 return fn_tilde_expand(name); 1402 } 1403 1404 char * 1405 filename_completion_function(const char *name, int state) 1406 { 1407 return fn_filename_completion_function(name, state); 1408 } 1409 1410 /* 1411 * a completion generator for usernames; returns _first_ username 1412 * which starts with supplied text 1413 * text contains a partial username preceded by random character 1414 * (usually '~'); state is ignored 1415 * it's callers responsibility to free returned value 1416 */ 1417 char * 1418 username_completion_function(const char *text, int state) 1419 { 1420 struct passwd *pwd, pwres; 1421 char pwbuf[1024]; 1422 1423 if (text[0] == '\0') 1424 return (NULL); 1425 1426 if (*text == '~') 1427 text++; 1428 1429 if (state == 0) 1430 setpwent(); 1431 1432 while (getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pwd) == 0 1433 && pwd != NULL && text[0] == pwd->pw_name[0] 1434 && strcmp(text, pwd->pw_name) == 0); 1435 1436 if (pwd == NULL) { 1437 endpwent(); 1438 return (NULL); 1439 } 1440 return (strdup(pwd->pw_name)); 1441 } 1442 1443 1444 /* 1445 * el-compatible wrapper to send TSTP on ^Z 1446 */ 1447 /* ARGSUSED */ 1448 static unsigned char 1449 _el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__))) 1450 { 1451 (void)kill(0, SIGTSTP); 1452 return CC_NORM; 1453 } 1454 1455 /* 1456 * Display list of strings in columnar format on readline's output stream. 1457 * 'matches' is list of strings, 'len' is number of strings in 'matches', 1458 * 'max' is maximum length of string in 'matches'. 1459 */ 1460 void 1461 rl_display_match_list(char **matches, int len, int max) 1462 { 1463 1464 fn_display_match_list(e, matches, len, max); 1465 } 1466 1467 static const char * 1468 /*ARGSUSED*/ 1469 _rl_completion_append_character_function(const char *dummy 1470 __attribute__((__unused__))) 1471 { 1472 static char buf[2]; 1473 buf[1] = rl_completion_append_character; 1474 return buf; 1475 } 1476 1477 1478 /* 1479 * complete word at current point 1480 */ 1481 /* ARGSUSED */ 1482 int 1483 rl_complete(int ignore __attribute__((__unused__)), int invoking_key) 1484 { 1485 if (h == NULL || e == NULL) 1486 rl_initialize(); 1487 1488 if (rl_inhibit_completion) { 1489 char arr[2]; 1490 arr[0] = (char)invoking_key; 1491 arr[1] = '\0'; 1492 el_insertstr(e, arr); 1493 return (CC_REFRESH); 1494 } 1495 1496 /* Just look at how many global variables modify this operation! */ 1497 return fn_complete(e, 1498 (CPFunction *)rl_completion_entry_function, 1499 rl_attempted_completion_function, 1500 rl_basic_word_break_characters, rl_special_prefixes, 1501 _rl_completion_append_character_function, rl_completion_query_items, 1502 &rl_completion_type, &rl_attempted_completion_over, 1503 &rl_point, &rl_end); 1504 } 1505 1506 1507 /* ARGSUSED */ 1508 static unsigned char 1509 _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch) 1510 { 1511 return (unsigned char)rl_complete(0, ch); 1512 } 1513 1514 /* 1515 * misc other functions 1516 */ 1517 1518 /* 1519 * bind key c to readline-type function func 1520 */ 1521 int 1522 rl_bind_key(int c, int func(int, int)) 1523 { 1524 int retval = -1; 1525 1526 if (h == NULL || e == NULL) 1527 rl_initialize(); 1528 1529 if (func == rl_insert) { 1530 /* XXX notice there is no range checking of ``c'' */ 1531 e->el_map.key[c] = ED_INSERT; 1532 retval = 0; 1533 } 1534 return (retval); 1535 } 1536 1537 1538 /* 1539 * read one key from input - handles chars pushed back 1540 * to input stream also 1541 */ 1542 int 1543 rl_read_key(void) 1544 { 1545 char fooarr[2 * sizeof(int)]; 1546 1547 if (e == NULL || h == NULL) 1548 rl_initialize(); 1549 1550 return (el_getc(e, fooarr)); 1551 } 1552 1553 1554 /* 1555 * reset the terminal 1556 */ 1557 /* ARGSUSED */ 1558 void 1559 rl_reset_terminal(const char *p __attribute__((__unused__))) 1560 { 1561 1562 if (h == NULL || e == NULL) 1563 rl_initialize(); 1564 el_reset(e); 1565 } 1566 1567 1568 /* 1569 * insert character ``c'' back into input stream, ``count'' times 1570 */ 1571 int 1572 rl_insert(int count, int c) 1573 { 1574 char arr[2]; 1575 1576 if (h == NULL || e == NULL) 1577 rl_initialize(); 1578 1579 /* XXX - int -> char conversion can lose on multichars */ 1580 arr[0] = c; 1581 arr[1] = '\0'; 1582 1583 for (; count > 0; count--) 1584 el_push(e, arr); 1585 1586 return (0); 1587 } 1588 1589 /*ARGSUSED*/ 1590 int 1591 rl_newline(int count, int c) 1592 { 1593 /* 1594 * Readline-4.0 appears to ignore the args. 1595 */ 1596 return rl_insert(1, '\n'); 1597 } 1598 1599 /*ARGSUSED*/ 1600 static unsigned char 1601 rl_bind_wrapper(EditLine *el, unsigned char c) 1602 { 1603 if (map[c] == NULL) 1604 return CC_ERROR; 1605 1606 _rl_update_pos(); 1607 1608 (*map[c])(NULL, c); 1609 1610 /* If rl_done was set by the above call, deal with it here */ 1611 if (rl_done) 1612 return CC_EOF; 1613 1614 return CC_NORM; 1615 } 1616 1617 int 1618 rl_add_defun(const char *name, Function *fun, int c) 1619 { 1620 char dest[8]; 1621 if (c >= sizeof(map) / sizeof(map[0]) || c < 0) 1622 return -1; 1623 map[(unsigned char)c] = fun; 1624 el_set(e, EL_ADDFN, name, name, rl_bind_wrapper); 1625 vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0); 1626 el_set(e, EL_BIND, dest, name); 1627 return 0; 1628 } 1629 1630 void 1631 rl_callback_read_char() 1632 { 1633 int count = 0, done = 0; 1634 const char *buf = el_gets(e, &count); 1635 char *wbuf; 1636 1637 if (buf == NULL || count-- <= 0) 1638 return; 1639 if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF]) 1640 done = 1; 1641 if (buf[count] == '\n' || buf[count] == '\r') 1642 done = 2; 1643 1644 if (done && rl_linefunc != NULL) { 1645 el_set(e, EL_UNBUFFERED, 0); 1646 if (done == 2) { 1647 if ((wbuf = strdup(buf)) != NULL) 1648 wbuf[count] = '\0'; 1649 } else 1650 wbuf = NULL; 1651 (*(void (*)(const char *))rl_linefunc)(wbuf); 1652 el_set(e, EL_UNBUFFERED, 1); 1653 } 1654 } 1655 1656 void 1657 rl_callback_handler_install (const char *prompt, VCPFunction *linefunc) 1658 { 1659 if (e == NULL) { 1660 rl_initialize(); 1661 } 1662 if (rl_prompt) 1663 free(rl_prompt); 1664 rl_prompt = prompt ? strdup(strchr(prompt, *prompt)) : NULL; 1665 rl_linefunc = linefunc; 1666 el_set(e, EL_UNBUFFERED, 1); 1667 } 1668 1669 void 1670 rl_callback_handler_remove(void) 1671 { 1672 el_set(e, EL_UNBUFFERED, 0); 1673 } 1674 1675 void 1676 rl_redisplay(void) 1677 { 1678 char a[2]; 1679 a[0] = e->el_tty.t_c[TS_IO][C_REPRINT]; 1680 a[1] = '\0'; 1681 el_push(e, a); 1682 } 1683 1684 int 1685 rl_get_previous_history(int count, int key) 1686 { 1687 char a[2]; 1688 a[0] = key; 1689 a[1] = '\0'; 1690 while (count--) 1691 el_push(e, a); 1692 return 0; 1693 } 1694 1695 void 1696 /*ARGSUSED*/ 1697 rl_prep_terminal(int meta_flag) 1698 { 1699 el_set(e, EL_PREP_TERM, 1); 1700 } 1701 1702 void 1703 rl_deprep_terminal() 1704 { 1705 el_set(e, EL_PREP_TERM, 0); 1706 } 1707 1708 int 1709 rl_read_init_file(const char *s) 1710 { 1711 return(el_source(e, s)); 1712 } 1713 1714 int 1715 rl_parse_and_bind(const char *line) 1716 { 1717 const char **argv; 1718 int argc; 1719 Tokenizer *tok; 1720 1721 tok = tok_init(NULL); 1722 tok_str(tok, line, &argc, &argv); 1723 argc = el_parse(e, argc, argv); 1724 tok_end(tok); 1725 return (argc ? 1 : 0); 1726 } 1727 1728 int 1729 rl_variable_bind(const char *var, const char *value) 1730 { 1731 /* 1732 * The proper return value is undocument, but this is what the 1733 * readline source seems to do. 1734 */ 1735 return ((el_set(e, EL_BIND, "", var, value) == -1) ? 1 : 0); 1736 } 1737 1738 void 1739 rl_stuff_char(int c) 1740 { 1741 char buf[2]; 1742 1743 buf[0] = c; 1744 buf[1] = '\0'; 1745 el_insertstr(e, buf); 1746 } 1747 1748 static int 1749 _rl_event_read_char(EditLine *el, char *cp) 1750 { 1751 int n, num_read = 0; 1752 1753 *cp = 0; 1754 while (rl_event_hook) { 1755 1756 (*rl_event_hook)(); 1757 1758 #if defined(FIONREAD) 1759 if (ioctl(el->el_infd, FIONREAD, &n) < 0) 1760 return(-1); 1761 if (n) 1762 num_read = read(el->el_infd, cp, 1); 1763 else 1764 num_read = 0; 1765 #elif defined(F_SETFL) && defined(O_NDELAY) 1766 if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0) 1767 return(-1); 1768 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0) 1769 return(-1); 1770 num_read = read(el->el_infd, cp, 1); 1771 if (fcntl(el->el_infd, F_SETFL, n)) 1772 return(-1); 1773 #else 1774 /* not non-blocking, but what you gonna do? */ 1775 num_read = read(el->el_infd, cp, 1); 1776 return(-1); 1777 #endif 1778 1779 if (num_read < 0 && errno == EAGAIN) 1780 continue; 1781 if (num_read == 0) 1782 continue; 1783 break; 1784 } 1785 if (!rl_event_hook) 1786 el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN); 1787 return(num_read); 1788 } 1789 1790 static void 1791 _rl_update_pos(void) 1792 { 1793 const LineInfo *li = el_line(e); 1794 1795 rl_point = li->cursor - li->buffer; 1796 rl_end = li->lastchar - li->buffer; 1797 } 1798