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