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