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