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