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