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