1 /* $OpenBSD: readline.c,v 1.10 2011/07/08 05:41:11 nicm 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 = realloc(what, 790 (size <<= 1)); 791 if (nwhat == NULL) { 792 free(what); 793 free(tmp); 794 return 0; 795 } 796 what = nwhat; 797 } 798 what[len++] = *cmd; 799 } 800 what[len] = '\0'; 801 from = what; 802 if (*what == '\0') { 803 free(what); 804 if (search) { 805 from = strdup(search); 806 if (from == NULL) { 807 free(tmp); 808 return 0; 809 } 810 } else { 811 from = NULL; 812 free(tmp); 813 return (-1); 814 } 815 } 816 cmd++; /* shift after delim */ 817 if (!*cmd) 818 continue; 819 820 size = 16; 821 with = realloc(to, size); 822 if (with == NULL) { 823 free(to); 824 free(tmp); 825 return -1; 826 } 827 len = 0; 828 from_len = strlen(from); 829 for (; *cmd && *cmd != delim; cmd++) { 830 if (len + from_len + 1 >= size) { 831 char *nwith; 832 size += from_len + 1; 833 nwith = realloc(with, size); 834 if (nwith == NULL) { 835 free(with); 836 free(tmp); 837 return -1; 838 } 839 with = nwith; 840 } 841 if (*cmd == '&') { 842 /* safe */ 843 (void)strlcpy(&with[len], from, 844 size - len); 845 len += from_len; 846 continue; 847 } 848 if (*cmd == '\\' 849 && (*(cmd + 1) == delim 850 || *(cmd + 1) == '&')) 851 cmd++; 852 with[len++] = *cmd; 853 } 854 with[len] = '\0'; 855 to = with; 856 } 857 858 aptr = _rl_compat_sub(tmp, from, to, g_on); 859 if (aptr) { 860 free(tmp); 861 tmp = aptr; 862 } 863 g_on = 0; 864 } 865 } 866 *result = tmp; 867 return (p_on? 2:1); 868 } 869 870 871 /* 872 * csh-style history expansion 873 */ 874 int 875 history_expand(char *str, char **output) 876 { 877 int ret = 0; 878 size_t idx, i, size; 879 char *tmp, *result; 880 881 if (h == NULL || e == NULL) 882 rl_initialize(); 883 884 if (history_expansion_char == 0) { 885 *output = strdup(str); 886 return(0); 887 } 888 889 *output = NULL; 890 if (str[0] == history_subst_char) { 891 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */ 892 size_t sz = 4 + strlen(str) + 1; 893 *output = malloc(sz); 894 if (*output == NULL) 895 return 0; 896 (*output)[0] = (*output)[1] = history_expansion_char; 897 (*output)[2] = ':'; 898 (*output)[3] = 's'; 899 (void)strlcpy((*output) + 4, str, sz - 4); 900 str = *output; 901 } else { 902 *output = strdup(str); 903 if (*output == NULL) 904 return 0; 905 } 906 907 #define ADD_STRING(what, len, fr) \ 908 { \ 909 if (idx + len + 1 > size) { \ 910 char *nresult = realloc(result, (size += len + 1));\ 911 if (nresult == NULL) { \ 912 free(*output); \ 913 if (/*CONSTCOND*/fr) \ 914 free(tmp); \ 915 return 0; \ 916 } \ 917 result = nresult; \ 918 } \ 919 (void)strncpy(&result[idx], what, len); \ 920 idx += len; \ 921 result[idx] = '\0'; \ 922 } 923 924 result = NULL; 925 size = idx = 0; 926 tmp = NULL; 927 for (i = 0; str[i];) { 928 int qchar, loop_again; 929 size_t len, start, j; 930 931 qchar = 0; 932 loop_again = 1; 933 start = j = i; 934 loop: 935 for (; str[j]; j++) { 936 if (str[j] == '\\' && 937 str[j + 1] == history_expansion_char) { 938 size_t sz = strlen(&str[j]) + 1; 939 (void)strlcpy(&str[j], &str[j + 1], sz); 940 continue; 941 } 942 if (!loop_again) { 943 if (isspace((unsigned char) str[j]) 944 || str[j] == qchar) 945 break; 946 } 947 if (str[j] == history_expansion_char 948 && !strchr(history_no_expand_chars, str[j + 1]) 949 && (!history_inhibit_expansion_function || 950 (*history_inhibit_expansion_function)(str, 951 (int)j) == 0)) 952 break; 953 } 954 955 if (str[j] && loop_again) { 956 i = j; 957 qchar = (j > 0 && str[j - 1] == '"' )? '"':0; 958 j++; 959 if (str[j] == history_expansion_char) 960 j++; 961 loop_again = 0; 962 goto loop; 963 } 964 len = i - start; 965 ADD_STRING(&str[start], len, 0); 966 967 if (str[i] == '\0' || str[i] != history_expansion_char) { 968 len = j - i; 969 ADD_STRING(&str[i], len, 0); 970 if (start == 0) 971 ret = 0; 972 else 973 ret = 1; 974 break; 975 } 976 ret = _history_expand_command (str, i, (j - i), &tmp); 977 if (ret > 0 && tmp) { 978 len = strlen(tmp); 979 ADD_STRING(tmp, len, 1); 980 } 981 if (tmp) { 982 free(tmp); 983 tmp = NULL; 984 } 985 i = j; 986 } 987 988 /* ret is 2 for "print only" option */ 989 if (ret == 2) { 990 add_history(result); 991 #ifdef GDB_411_HACK 992 /* gdb 4.11 has been shipped with readline, where */ 993 /* history_expand() returned -1 when the line */ 994 /* should not be executed; in readline 2.1+ */ 995 /* it should return 2 in such a case */ 996 ret = -1; 997 #endif 998 } 999 free(*output); 1000 *output = result; 1001 1002 return (ret); 1003 } 1004 1005 /* 1006 * Return a string consisting of arguments of "str" from "start" to "end". 1007 */ 1008 char * 1009 history_arg_extract(int start, int end, const char *str) 1010 { 1011 size_t i, len, max; 1012 char **arr, *result = NULL; 1013 1014 arr = history_tokenize(str); 1015 if (!arr) 1016 return NULL; 1017 if (arr && *arr == NULL) 1018 goto out; 1019 1020 for (max = 0; arr[max]; max++) 1021 continue; 1022 max--; 1023 1024 if (start == '$') 1025 start = (int)max; 1026 if (end == '$') 1027 end = (int)max; 1028 if (end < 0) 1029 end = (int)max + end + 1; 1030 if (start < 0) 1031 start = end; 1032 1033 if (start < 0 || end < 0 || (size_t)start > max || 1034 (size_t)end > max || start > end) 1035 goto out; 1036 1037 for (i = start, len = 0; i <= (size_t)end; i++) 1038 len += strlen(arr[i]) + 1; 1039 len++; 1040 max = len; 1041 result = malloc(len); 1042 if (result == NULL) 1043 goto out; 1044 1045 for (i = start, len = 0; i <= (size_t)end; i++) { 1046 (void)strlcpy(result + len, arr[i], max - len); 1047 len += strlen(arr[i]); 1048 if (i < (size_t)end) 1049 result[len++] = ' '; 1050 } 1051 result[len] = '\0'; 1052 1053 out: 1054 for (i = 0; arr[i]; i++) 1055 free(arr[i]); 1056 free(arr); 1057 1058 return result; 1059 } 1060 1061 /* 1062 * Parse the string into individual tokens, 1063 * similar to how shell would do it. 1064 */ 1065 char ** 1066 history_tokenize(const char *str) 1067 { 1068 int size = 1, idx = 0, i, start; 1069 size_t len; 1070 char **result = NULL, *temp, delim = '\0'; 1071 1072 for (i = 0; str[i];) { 1073 while (isspace((unsigned char) str[i])) 1074 i++; 1075 start = i; 1076 for (; str[i];) { 1077 if (str[i] == '\\') { 1078 if (str[i+1] != '\0') 1079 i++; 1080 } else if (str[i] == delim) 1081 delim = '\0'; 1082 else if (!delim && 1083 (isspace((unsigned char) str[i]) || 1084 strchr("()<>;&|$", str[i]))) 1085 break; 1086 else if (!delim && strchr("'`\"", str[i])) 1087 delim = str[i]; 1088 if (str[i]) 1089 i++; 1090 } 1091 1092 if (idx + 2 >= size) { 1093 char **nresult; 1094 size <<= 1; 1095 nresult = realloc(result, size * sizeof(char *)); 1096 if (nresult == NULL) { 1097 free(result); 1098 return NULL; 1099 } 1100 result = nresult; 1101 } 1102 len = i - start; 1103 temp = malloc(len + 1); 1104 if (temp == NULL) { 1105 for (i = 0; i < idx; i++) 1106 free(result[i]); 1107 free(result); 1108 return NULL; 1109 } 1110 (void)strncpy(temp, &str[start], len); 1111 temp[len] = '\0'; 1112 result[idx++] = temp; 1113 result[idx] = NULL; 1114 if (str[i]) 1115 i++; 1116 } 1117 return (result); 1118 } 1119 1120 1121 /* 1122 * limit size of history record to ``max'' events 1123 */ 1124 void 1125 stifle_history(int max) 1126 { 1127 HistEvent ev; 1128 1129 if (h == NULL || e == NULL) 1130 rl_initialize(); 1131 1132 if (history(h, &ev, H_SETSIZE, max) == 0) 1133 max_input_history = max; 1134 } 1135 1136 1137 /* 1138 * "unlimit" size of history - set the limit to maximum allowed int value 1139 */ 1140 int 1141 unstifle_history(void) 1142 { 1143 HistEvent ev; 1144 int omax; 1145 1146 history(h, &ev, H_SETSIZE, INT_MAX); 1147 omax = max_input_history; 1148 max_input_history = INT_MAX; 1149 return (omax); /* some value _must_ be returned */ 1150 } 1151 1152 1153 int 1154 history_is_stifled(void) 1155 { 1156 1157 /* cannot return true answer */ 1158 return (max_input_history != INT_MAX); 1159 } 1160 1161 static const char _history_tmp_template[] = "/tmp/.historyXXXXXX"; 1162 1163 int 1164 history_truncate_file (const char *filename, int nlines) 1165 { 1166 int ret = 0; 1167 FILE *fp, *tp; 1168 char template[sizeof(_history_tmp_template)]; 1169 char buf[4096]; 1170 int fd; 1171 char *cp; 1172 off_t off; 1173 int count = 0; 1174 ssize_t left = 0; 1175 1176 if (filename == NULL && (filename = _default_history_file()) == NULL) 1177 return errno; 1178 if ((fp = fopen(filename, "r+")) == NULL) 1179 return errno; 1180 strlcpy(template, _history_tmp_template, sizeof(template)); 1181 if ((fd = mkstemp(template)) == -1) { 1182 ret = errno; 1183 goto out1; 1184 } 1185 1186 if ((tp = fdopen(fd, "r+")) == NULL) { 1187 close(fd); 1188 ret = errno; 1189 goto out2; 1190 } 1191 1192 for(;;) { 1193 if (fread(buf, sizeof(buf), 1, fp) != 1) { 1194 if (ferror(fp)) { 1195 ret = errno; 1196 break; 1197 } 1198 if (fseeko(fp, (off_t)sizeof(buf) * count, SEEK_SET) == 1199 (off_t)-1) { 1200 ret = errno; 1201 break; 1202 } 1203 left = fread(buf, 1, sizeof(buf), fp); 1204 if (ferror(fp)) { 1205 ret = errno; 1206 break; 1207 } 1208 if (left == 0) { 1209 count--; 1210 left = sizeof(buf); 1211 } else if (fwrite(buf, (size_t)left, 1, tp) != 1) { 1212 ret = errno; 1213 break; 1214 } 1215 fflush(tp); 1216 break; 1217 } 1218 if (fwrite(buf, sizeof(buf), 1, tp) != 1) { 1219 ret = errno; 1220 break; 1221 } 1222 count++; 1223 } 1224 if (ret) 1225 goto out3; 1226 cp = buf + left - 1; 1227 if(*cp != '\n') 1228 cp++; 1229 for(;;) { 1230 while (--cp >= buf) { 1231 if (*cp == '\n') { 1232 if (--nlines == 0) { 1233 if (++cp >= buf + sizeof(buf)) { 1234 count++; 1235 cp = buf; 1236 } 1237 break; 1238 } 1239 } 1240 } 1241 if (nlines <= 0 || count == 0) 1242 break; 1243 count--; 1244 if (fseeko(tp, (off_t)sizeof(buf) * count, SEEK_SET) < 0) { 1245 ret = errno; 1246 break; 1247 } 1248 if (fread(buf, sizeof(buf), 1, tp) != 1) { 1249 if (ferror(tp)) { 1250 ret = errno; 1251 break; 1252 } 1253 ret = EAGAIN; 1254 break; 1255 } 1256 cp = buf + sizeof(buf); 1257 } 1258 1259 if (ret || nlines > 0) 1260 goto out3; 1261 1262 if (fseeko(fp, 0, SEEK_SET) == (off_t)-1) { 1263 ret = errno; 1264 goto out3; 1265 } 1266 1267 if (fseeko(tp, (off_t)sizeof(buf) * count + (cp - buf), SEEK_SET) == 1268 (off_t)-1) { 1269 ret = errno; 1270 goto out3; 1271 } 1272 1273 for(;;) { 1274 if ((left = fread(buf, 1, sizeof(buf), tp)) == 0) { 1275 if (ferror(fp)) 1276 ret = errno; 1277 break; 1278 } 1279 if (fwrite(buf, (size_t)left, 1, fp) != 1) { 1280 ret = errno; 1281 break; 1282 } 1283 } 1284 fflush(fp); 1285 if((off = ftello(fp)) > 0) 1286 (void)ftruncate(fileno(fp), off); 1287 out3: 1288 fclose(tp); 1289 out2: 1290 unlink(template); 1291 out1: 1292 fclose(fp); 1293 1294 return ret; 1295 } 1296 1297 1298 /* 1299 * read history from a file given 1300 */ 1301 int 1302 read_history(const char *filename) 1303 { 1304 HistEvent ev; 1305 1306 if (h == NULL || e == NULL) 1307 rl_initialize(); 1308 if (filename == NULL && (filename = _default_history_file()) == NULL) 1309 return errno; 1310 return (history(h, &ev, H_LOAD, filename) == -1 ? 1311 (errno ? errno : EINVAL) : 0); 1312 } 1313 1314 1315 /* 1316 * write history to a file given 1317 */ 1318 int 1319 write_history(const char *filename) 1320 { 1321 HistEvent ev; 1322 1323 if (h == NULL || e == NULL) 1324 rl_initialize(); 1325 if (filename == NULL && (filename = _default_history_file()) == NULL) 1326 return errno; 1327 return (history(h, &ev, H_SAVE, filename) == -1 ? 1328 (errno ? errno : EINVAL) : 0); 1329 } 1330 1331 1332 /* 1333 * returns history ``num''th event 1334 * 1335 * returned pointer points to static variable 1336 */ 1337 HIST_ENTRY * 1338 history_get(int num) 1339 { 1340 static HIST_ENTRY she; 1341 HistEvent ev; 1342 int curr_num; 1343 1344 if (h == NULL || e == NULL) 1345 rl_initialize(); 1346 1347 /* save current position */ 1348 if (history(h, &ev, H_CURR) != 0) 1349 return (NULL); 1350 curr_num = ev.num; 1351 1352 /* start from the oldest */ 1353 if (history(h, &ev, H_LAST) != 0) 1354 return (NULL); /* error */ 1355 1356 /* look forwards for event matching specified offset */ 1357 if (history(h, &ev, H_NEXT_EVDATA, num, &she.data)) 1358 return (NULL); 1359 1360 she.line = ev.str; 1361 1362 /* restore pointer to where it was */ 1363 (void)history(h, &ev, H_SET, curr_num); 1364 1365 return (&she); 1366 } 1367 1368 1369 /* 1370 * add the line to history table 1371 */ 1372 int 1373 add_history(const char *line) 1374 { 1375 HistEvent ev; 1376 1377 if (h == NULL || e == NULL) 1378 rl_initialize(); 1379 1380 (void)history(h, &ev, H_ENTER, line); 1381 if (history(h, &ev, H_GETSIZE) == 0) 1382 history_length = ev.num; 1383 1384 return (!(history_length > 0)); /* return 0 if all is okay */ 1385 } 1386 1387 1388 /* 1389 * remove the specified entry from the history list and return it. 1390 */ 1391 HIST_ENTRY * 1392 remove_history(int num) 1393 { 1394 HIST_ENTRY *he; 1395 HistEvent ev; 1396 1397 if (h == NULL || e == NULL) 1398 rl_initialize(); 1399 1400 if ((he = malloc(sizeof(*he))) == NULL) 1401 return NULL; 1402 1403 if (history(h, &ev, H_DELDATA, num, &he->data) != 0) { 1404 free(he); 1405 return NULL; 1406 } 1407 1408 he->line = ev.str; 1409 if (history(h, &ev, H_GETSIZE) == 0) 1410 history_length = ev.num; 1411 1412 return he; 1413 } 1414 1415 1416 /* 1417 * replace the line and data of the num-th entry 1418 */ 1419 HIST_ENTRY * 1420 replace_history_entry(int num, const char *line, histdata_t data) 1421 { 1422 HIST_ENTRY *he; 1423 HistEvent ev; 1424 int curr_num; 1425 1426 if (h == NULL || e == NULL) 1427 rl_initialize(); 1428 1429 /* save current position */ 1430 if (history(h, &ev, H_CURR) != 0) 1431 return NULL; 1432 curr_num = ev.num; 1433 1434 /* start from the oldest */ 1435 if (history(h, &ev, H_LAST) != 0) 1436 return NULL; /* error */ 1437 1438 if ((he = malloc(sizeof(*he))) == NULL) 1439 return NULL; 1440 1441 /* look forwards for event matching specified offset */ 1442 if (history(h, &ev, H_NEXT_EVDATA, num, &he->data)) 1443 goto out; 1444 1445 he->line = strdup(ev.str); 1446 if (he->line == NULL) 1447 goto out; 1448 1449 if (history(h, &ev, H_REPLACE, line, data)) 1450 goto out; 1451 1452 /* restore pointer to where it was */ 1453 if (history(h, &ev, H_SET, curr_num)) 1454 goto out; 1455 1456 return he; 1457 out: 1458 free(he); 1459 return NULL; 1460 } 1461 1462 /* 1463 * clear the history list - delete all entries 1464 */ 1465 void 1466 clear_history(void) 1467 { 1468 HistEvent ev; 1469 1470 (void)history(h, &ev, H_CLEAR); 1471 history_length = 0; 1472 } 1473 1474 1475 /* 1476 * returns offset of the current history event 1477 */ 1478 int 1479 where_history(void) 1480 { 1481 HistEvent ev; 1482 int curr_num, off; 1483 1484 if (history(h, &ev, H_CURR) != 0) 1485 return (0); 1486 curr_num = ev.num; 1487 1488 (void)history(h, &ev, H_FIRST); 1489 off = 1; 1490 while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0) 1491 off++; 1492 1493 return (off); 1494 } 1495 1496 1497 /* 1498 * returns current history event or NULL if there is no such event 1499 */ 1500 HIST_ENTRY * 1501 current_history(void) 1502 { 1503 1504 return (_move_history(H_CURR)); 1505 } 1506 1507 1508 /* 1509 * returns total number of bytes history events' data are using 1510 */ 1511 int 1512 history_total_bytes(void) 1513 { 1514 HistEvent ev; 1515 int curr_num; 1516 size_t size; 1517 1518 if (history(h, &ev, H_CURR) != 0) 1519 return (-1); 1520 curr_num = ev.num; 1521 1522 (void)history(h, &ev, H_FIRST); 1523 size = 0; 1524 do 1525 size += strlen(ev.str) * sizeof(*ev.str); 1526 while (history(h, &ev, H_NEXT) == 0); 1527 1528 /* get to the same position as before */ 1529 history(h, &ev, H_PREV_EVENT, curr_num); 1530 1531 return (int)(size); 1532 } 1533 1534 1535 /* 1536 * sets the position in the history list to ``pos'' 1537 */ 1538 int 1539 history_set_pos(int pos) 1540 { 1541 HistEvent ev; 1542 int curr_num; 1543 1544 if (pos >= history_length || pos < 0) 1545 return (-1); 1546 1547 (void)history(h, &ev, H_CURR); 1548 curr_num = ev.num; 1549 1550 /* 1551 * use H_DELDATA to set to nth history (without delete) by passing 1552 * (void **)-1 1553 */ 1554 if (history(h, &ev, H_DELDATA, pos, (void **)-1)) { 1555 (void)history(h, &ev, H_SET, curr_num); 1556 return(-1); 1557 } 1558 return (0); 1559 } 1560 1561 1562 /* 1563 * returns previous event in history and shifts pointer accordingly 1564 */ 1565 HIST_ENTRY * 1566 previous_history(void) 1567 { 1568 1569 return (_move_history(H_PREV)); 1570 } 1571 1572 1573 /* 1574 * returns next event in history and shifts pointer accordingly 1575 */ 1576 HIST_ENTRY * 1577 next_history(void) 1578 { 1579 1580 return (_move_history(H_NEXT)); 1581 } 1582 1583 1584 /* 1585 * searches for first history event containing the str 1586 */ 1587 int 1588 history_search(const char *str, int direction) 1589 { 1590 HistEvent ev; 1591 const char *strp; 1592 int curr_num; 1593 1594 if (history(h, &ev, H_CURR) != 0) 1595 return (-1); 1596 curr_num = ev.num; 1597 1598 for (;;) { 1599 if ((strp = strstr(ev.str, str)) != NULL) 1600 return (int) (strp - ev.str); 1601 if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0) 1602 break; 1603 } 1604 (void)history(h, &ev, H_SET, curr_num); 1605 return (-1); 1606 } 1607 1608 1609 /* 1610 * searches for first history event beginning with str 1611 */ 1612 int 1613 history_search_prefix(const char *str, int direction) 1614 { 1615 HistEvent ev; 1616 1617 return (history(h, &ev, direction < 0 ? 1618 H_PREV_STR : H_NEXT_STR, str)); 1619 } 1620 1621 1622 /* 1623 * search for event in history containing str, starting at offset 1624 * abs(pos); continue backward, if pos<0, forward otherwise 1625 */ 1626 /* ARGSUSED */ 1627 int 1628 history_search_pos(const char *str, 1629 int direction __attribute__((__unused__)), int pos) 1630 { 1631 HistEvent ev; 1632 int curr_num, off; 1633 1634 off = (pos > 0) ? pos : -pos; 1635 pos = (pos > 0) ? 1 : -1; 1636 1637 if (history(h, &ev, H_CURR) != 0) 1638 return (-1); 1639 curr_num = ev.num; 1640 1641 if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0) 1642 return (-1); 1643 1644 for (;;) { 1645 if (strstr(ev.str, str)) 1646 return (off); 1647 if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0) 1648 break; 1649 } 1650 1651 /* set "current" pointer back to previous state */ 1652 (void)history(h, &ev, 1653 pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num); 1654 1655 return (-1); 1656 } 1657 1658 1659 /********************************/ 1660 /* completion functions */ 1661 1662 char * 1663 tilde_expand(char *name) 1664 { 1665 return fn_tilde_expand(name); 1666 } 1667 1668 char * 1669 filename_completion_function(const char *name, int state) 1670 { 1671 return fn_filename_completion_function(name, state); 1672 } 1673 1674 /* 1675 * a completion generator for usernames; returns _first_ username 1676 * which starts with supplied text 1677 * text contains a partial username preceded by random character 1678 * (usually '~'); state is ignored 1679 * it's callers responsibility to free returned value 1680 */ 1681 char * 1682 username_completion_function(const char *text, int state) 1683 { 1684 struct passwd *pwd; 1685 1686 if (text[0] == '\0') 1687 return (NULL); 1688 1689 if (*text == '~') 1690 text++; 1691 1692 if (state == 0) 1693 setpwent(); 1694 1695 while ((pwd = getpwent()) != NULL && text[0] == pwd->pw_name[0] 1696 && strcmp(text, pwd->pw_name) == 0); 1697 1698 if (pwd == NULL) { 1699 endpwent(); 1700 return NULL; 1701 } 1702 return strdup(pwd->pw_name); 1703 } 1704 1705 1706 /* 1707 * el-compatible wrapper to send TSTP on ^Z 1708 */ 1709 /* ARGSUSED */ 1710 static unsigned char 1711 _el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__))) 1712 { 1713 (void)kill(0, SIGTSTP); 1714 return CC_NORM; 1715 } 1716 1717 /* 1718 * Display list of strings in columnar format on readline's output stream. 1719 * 'matches' is list of strings, 'len' is number of strings in 'matches', 1720 * 'max' is maximum length of string in 'matches'. 1721 */ 1722 void 1723 rl_display_match_list(char **matches, int len, int max) 1724 { 1725 1726 fn_display_match_list(e, matches, (size_t)len, (size_t)max); 1727 } 1728 1729 static const char * 1730 /*ARGSUSED*/ 1731 _rl_completion_append_character_function(const char *dummy 1732 __attribute__((__unused__))) 1733 { 1734 static char buf[2]; 1735 buf[0] = rl_completion_append_character; 1736 buf[1] = '\0'; 1737 return buf; 1738 } 1739 1740 1741 /* 1742 * complete word at current point 1743 */ 1744 /* ARGSUSED */ 1745 int 1746 rl_complete(int ignore __attribute__((__unused__)), int invoking_key) 1747 { 1748 #ifdef WIDECHAR 1749 static ct_buffer_t wbreak_conv, sprefix_conv; 1750 #endif 1751 1752 if (h == NULL || e == NULL) 1753 rl_initialize(); 1754 1755 if (rl_inhibit_completion) { 1756 char arr[2]; 1757 arr[0] = (char)invoking_key; 1758 arr[1] = '\0'; 1759 el_insertstr(e, arr); 1760 return (CC_REFRESH); 1761 } 1762 1763 /* Just look at how many global variables modify this operation! */ 1764 return fn_complete(e, 1765 (CPFunction *)rl_completion_entry_function, 1766 rl_attempted_completion_function, 1767 ct_decode_string(rl_basic_word_break_characters, &wbreak_conv), 1768 ct_decode_string(rl_special_prefixes, &sprefix_conv), 1769 _rl_completion_append_character_function, 1770 (size_t)rl_completion_query_items, 1771 &rl_completion_type, &rl_attempted_completion_over, 1772 &rl_point, &rl_end); 1773 1774 1775 } 1776 1777 1778 /* ARGSUSED */ 1779 static unsigned char 1780 _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch) 1781 { 1782 return (unsigned char)rl_complete(0, ch); 1783 } 1784 1785 /* 1786 * misc other functions 1787 */ 1788 1789 /* 1790 * bind key c to readline-type function func 1791 */ 1792 int 1793 rl_bind_key(int c, rl_command_func_t *func) 1794 { 1795 int retval = -1; 1796 1797 if (h == NULL || e == NULL) 1798 rl_initialize(); 1799 1800 if (func == rl_insert) { 1801 /* XXX notice there is no range checking of ``c'' */ 1802 e->el_map.key[c] = ED_INSERT; 1803 retval = 0; 1804 } 1805 return (retval); 1806 } 1807 1808 1809 /* 1810 * read one key from input - handles chars pushed back 1811 * to input stream also 1812 */ 1813 int 1814 rl_read_key(void) 1815 { 1816 char fooarr[2 * sizeof(int)]; 1817 1818 if (e == NULL || h == NULL) 1819 rl_initialize(); 1820 1821 return (el_getc(e, fooarr)); 1822 } 1823 1824 1825 /* 1826 * reset the terminal 1827 */ 1828 /* ARGSUSED */ 1829 void 1830 rl_reset_terminal(const char *p __attribute__((__unused__))) 1831 { 1832 1833 if (h == NULL || e == NULL) 1834 rl_initialize(); 1835 el_reset(e); 1836 } 1837 1838 1839 /* 1840 * insert character ``c'' back into input stream, ``count'' times 1841 */ 1842 int 1843 rl_insert(int count, int c) 1844 { 1845 char arr[2]; 1846 1847 if (h == NULL || e == NULL) 1848 rl_initialize(); 1849 1850 /* XXX - int -> char conversion can lose on multichars */ 1851 arr[0] = c; 1852 arr[1] = '\0'; 1853 1854 for (; count > 0; count--) 1855 el_push(e, arr); 1856 1857 return (0); 1858 } 1859 1860 int 1861 rl_insert_text(const char *text) 1862 { 1863 if (!text || *text == 0) 1864 return (0); 1865 1866 if (h == NULL || e == NULL) 1867 rl_initialize(); 1868 1869 if (el_insertstr(e, text) < 0) 1870 return (0); 1871 return (int)strlen(text); 1872 } 1873 1874 /*ARGSUSED*/ 1875 int 1876 rl_newline(int count, int c) 1877 { 1878 /* 1879 * Readline-4.0 appears to ignore the args. 1880 */ 1881 return rl_insert(1, '\n'); 1882 } 1883 1884 /*ARGSUSED*/ 1885 static unsigned char 1886 rl_bind_wrapper(EditLine *el, unsigned char c) 1887 { 1888 if (map[c] == NULL) 1889 return CC_ERROR; 1890 1891 _rl_update_pos(); 1892 1893 (*map[c])(NULL, c); 1894 1895 /* If rl_done was set by the above call, deal with it here */ 1896 if (rl_done) 1897 return CC_EOF; 1898 1899 return CC_NORM; 1900 } 1901 1902 int 1903 rl_add_defun(const char *name, Function *fun, int c) 1904 { 1905 char dest[8]; 1906 if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0) 1907 return -1; 1908 map[(unsigned char)c] = fun; 1909 el_set(e, EL_ADDFN, name, name, rl_bind_wrapper); 1910 vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0); 1911 el_set(e, EL_BIND, dest, name, NULL); 1912 return 0; 1913 } 1914 1915 void 1916 rl_callback_read_char() 1917 { 1918 int count = 0, done = 0; 1919 const char *buf = el_gets(e, &count); 1920 char *wbuf; 1921 1922 if (buf == NULL || count-- <= 0) 1923 return; 1924 if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF]) 1925 done = 1; 1926 if (buf[count] == '\n' || buf[count] == '\r') 1927 done = 2; 1928 1929 if (done && rl_linefunc != NULL) { 1930 el_set(e, EL_UNBUFFERED, 0); 1931 if (done == 2) { 1932 if ((wbuf = strdup(buf)) != NULL) 1933 wbuf[count] = '\0'; 1934 } else 1935 wbuf = NULL; 1936 (*(void (*)(const char *))rl_linefunc)(wbuf); 1937 //el_set(e, EL_UNBUFFERED, 1); 1938 } 1939 } 1940 1941 void 1942 rl_callback_handler_install(const char *prompt, VCPFunction *linefunc) 1943 { 1944 if (e == NULL) { 1945 rl_initialize(); 1946 } 1947 (void)rl_set_prompt(prompt); 1948 rl_linefunc = linefunc; 1949 el_set(e, EL_UNBUFFERED, 1); 1950 } 1951 1952 void 1953 rl_callback_handler_remove(void) 1954 { 1955 el_set(e, EL_UNBUFFERED, 0); 1956 rl_linefunc = NULL; 1957 } 1958 1959 void 1960 rl_redisplay(void) 1961 { 1962 char a[2]; 1963 a[0] = e->el_tty.t_c[TS_IO][C_REPRINT]; 1964 a[1] = '\0'; 1965 el_push(e, a); 1966 } 1967 1968 int 1969 rl_get_previous_history(int count, int key) 1970 { 1971 char a[2]; 1972 a[0] = key; 1973 a[1] = '\0'; 1974 while (count--) 1975 el_push(e, a); 1976 return 0; 1977 } 1978 1979 void 1980 /*ARGSUSED*/ 1981 rl_prep_terminal(int meta_flag) 1982 { 1983 el_set(e, EL_PREP_TERM, 1); 1984 } 1985 1986 void 1987 rl_deprep_terminal(void) 1988 { 1989 el_set(e, EL_PREP_TERM, 0); 1990 } 1991 1992 int 1993 rl_read_init_file(const char *s) 1994 { 1995 return(el_source(e, s)); 1996 } 1997 1998 int 1999 rl_parse_and_bind(const char *line) 2000 { 2001 const char **argv; 2002 int argc; 2003 Tokenizer *tok; 2004 2005 tok = tok_init(NULL); 2006 tok_str(tok, line, &argc, &argv); 2007 argc = el_parse(e, argc, argv); 2008 tok_end(tok); 2009 return (argc ? 1 : 0); 2010 } 2011 2012 int 2013 rl_variable_bind(const char *var, const char *value) 2014 { 2015 /* 2016 * The proper return value is undocument, but this is what the 2017 * readline source seems to do. 2018 */ 2019 return ((el_set(e, EL_BIND, "", var, value, NULL) == -1) ? 1 : 0); 2020 } 2021 2022 void 2023 rl_stuff_char(int c) 2024 { 2025 char buf[2]; 2026 2027 buf[0] = c; 2028 buf[1] = '\0'; 2029 el_insertstr(e, buf); 2030 } 2031 2032 static int 2033 _rl_event_read_char(EditLine *el, char *cp) 2034 { 2035 int n; 2036 ssize_t num_read = 0; 2037 2038 *cp = '\0'; 2039 while (rl_event_hook) { 2040 2041 (*rl_event_hook)(); 2042 2043 #if defined(FIONREAD) 2044 if (ioctl(el->el_infd, FIONREAD, &n) < 0) 2045 return(-1); 2046 if (n) 2047 num_read = read(el->el_infd, cp, 1); 2048 else 2049 num_read = 0; 2050 #elif defined(F_SETFL) && defined(O_NDELAY) 2051 if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0) 2052 return(-1); 2053 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0) 2054 return(-1); 2055 num_read = read(el->el_infd, cp, 1); 2056 if (fcntl(el->el_infd, F_SETFL, n)) 2057 return(-1); 2058 #else 2059 /* not non-blocking, but what you gonna do? */ 2060 num_read = read(el->el_infd, cp, 1); 2061 return(-1); 2062 #endif 2063 2064 if (num_read < 0 && errno == EAGAIN) 2065 continue; 2066 if (num_read == 0) 2067 continue; 2068 break; 2069 } 2070 if (!rl_event_hook) 2071 el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN); 2072 return (int)num_read; 2073 } 2074 2075 static void 2076 _rl_update_pos(void) 2077 { 2078 const LineInfo *li = el_line(e); 2079 2080 rl_point = (int)(li->cursor - li->buffer); 2081 rl_end = (int)(li->lastchar - li->buffer); 2082 } 2083 2084 void 2085 rl_get_screen_size(int *rows, int *cols) 2086 { 2087 if (rows) 2088 el_get(e, EL_GETTC, "li", rows); 2089 if (cols) 2090 el_get(e, EL_GETTC, "co", cols); 2091 } 2092 2093 void 2094 rl_set_screen_size(int rows, int cols) 2095 { 2096 char buf[64]; 2097 (void)snprintf(buf, sizeof(buf), "%d", rows); 2098 el_set(e, EL_SETTC, "li", buf, NULL); 2099 (void)snprintf(buf, sizeof(buf), "%d", cols); 2100 el_set(e, EL_SETTC, "co", buf, NULL); 2101 } 2102 2103 char ** 2104 rl_completion_matches(const char *str, rl_compentry_func_t *fun) 2105 { 2106 size_t len, max, i, j, min; 2107 char **list, *match, *a, *b; 2108 2109 len = 1; 2110 max = 10; 2111 if ((list = malloc(max * sizeof(*list))) == NULL) 2112 return NULL; 2113 2114 while ((match = (*fun)(str, (int)(len - 1))) != NULL) { 2115 list[len++] = match; 2116 if (len == max) { 2117 char **nl; 2118 max += 10; 2119 if ((nl = realloc(list, max * sizeof(*nl))) == NULL) 2120 goto out; 2121 list = nl; 2122 } 2123 } 2124 if (len == 1) 2125 goto out; 2126 list[len] = NULL; 2127 if (len == 2) { 2128 if ((list[0] = strdup(list[1])) == NULL) 2129 goto out; 2130 return list; 2131 } 2132 qsort(&list[1], len - 1, sizeof(*list), 2133 (int (*)(const void *, const void *)) strcmp); 2134 min = SIZE_T_MAX; 2135 for (i = 1, a = list[i]; i < len - 1; i++, a = b) { 2136 b = list[i + 1]; 2137 for (j = 0; a[j] && a[j] == b[j]; j++) 2138 continue; 2139 if (min > j) 2140 min = j; 2141 } 2142 if (min == 0 && *str) { 2143 if ((list[0] = strdup(str)) == NULL) 2144 goto out; 2145 } else { 2146 if ((list[0] = malloc(min + 1)) == NULL) 2147 goto out; 2148 (void)memcpy(list[0], list[1], min); 2149 list[0][min] = '\0'; 2150 } 2151 return list; 2152 2153 out: 2154 free(list); 2155 return NULL; 2156 } 2157 2158 char * 2159 rl_filename_completion_function (const char *text, int state) 2160 { 2161 return fn_filename_completion_function(text, state); 2162 } 2163 2164 void 2165 rl_forced_update_display(void) 2166 { 2167 el_set(e, EL_REFRESH); 2168 } 2169 2170 int 2171 _rl_abort_internal(void) 2172 { 2173 el_beep(e); 2174 longjmp(topbuf, 1); 2175 /*NOTREACHED*/ 2176 } 2177 2178 int 2179 _rl_qsort_string_compare(char **s1, char **s2) 2180 { 2181 return strcoll(*s1, *s2); 2182 } 2183 2184 HISTORY_STATE * 2185 history_get_history_state(void) 2186 { 2187 HISTORY_STATE *hs; 2188 2189 if ((hs = malloc(sizeof(HISTORY_STATE))) == NULL) 2190 return (NULL); 2191 hs->length = history_length; 2192 return (hs); 2193 } 2194 2195 int 2196 /*ARGSUSED*/ 2197 rl_kill_text(int from, int to) 2198 { 2199 return 0; 2200 } 2201 2202 Keymap 2203 rl_make_bare_keymap(void) 2204 { 2205 return NULL; 2206 } 2207 2208 Keymap 2209 rl_get_keymap(void) 2210 { 2211 return NULL; 2212 } 2213 2214 void 2215 /*ARGSUSED*/ 2216 rl_set_keymap(Keymap k) 2217 { 2218 } 2219 2220 int 2221 /*ARGSUSED*/ 2222 rl_generic_bind(int type, const char * keyseq, const char * data, Keymap k) 2223 { 2224 return 0; 2225 } 2226 2227 int 2228 /*ARGSUSED*/ 2229 rl_bind_key_in_map(int key, rl_command_func_t *fun, Keymap k) 2230 { 2231 return 0; 2232 } 2233 2234 /* unsupported, but needed by python */ 2235 void 2236 rl_cleanup_after_signal(void) 2237 { 2238 } 2239 2240 int 2241 rl_on_new_line(void) 2242 { 2243 return 0; 2244 } 2245