Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libedit/readline.c,v rcsdiff: /ftp/cvs/cvsroot/src/lib/libedit/readline.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.137 retrieving revision 1.149 diff -u -p -r1.137 -r1.149 --- src/lib/libedit/readline.c 2016/08/24 13:10:59 1.137 +++ src/lib/libedit/readline.c 2019/01/10 18:41:56 1.149 @@ -1,4 +1,4 @@ -/* $NetBSD: readline.c,v 1.137 2016/08/24 13:10:59 christos Exp $ */ +/* $NetBSD: readline.c,v 1.149 2019/01/10 18:41:56 christos Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include "config.h" #if !defined(lint) && !defined(SCCSID) -__RCSID("$NetBSD: readline.c,v 1.137 2016/08/24 13:10:59 christos Exp $"); +__RCSID("$NetBSD: readline.c,v 1.149 2019/01/10 18:41:56 christos Exp $"); #endif /* not lint && not SCCSID */ #include @@ -80,7 +80,7 @@ int rl_end = 0; char *rl_line_buffer = NULL; rl_vcpfunc_t *rl_linefunc = NULL; int rl_done = 0; -VFunction *rl_event_hook = NULL; +rl_hook_func_t *rl_event_hook = NULL; KEYMAP_ENTRY_ARRAY emacs_standard_keymap, emacs_meta_keymap, emacs_ctlx_keymap; @@ -122,6 +122,7 @@ int readline_echoing_p = 1; int _rl_print_completions_horizontally = 0; VFunction *rl_redisplay_function = NULL; Function *rl_startup_hook = NULL; +int rl_did_startup_hook = 0; VFunction *rl_completion_display_matches_hook = NULL; VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal; VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal; @@ -196,7 +197,7 @@ _getc_function(EditLine *el __attribute_ { int i; - i = (*rl_getc_function)(NULL); + i = (*rl_getc_function)(rl_instream); if (i == -1) return 0; *c = (wchar_t)i; @@ -289,7 +290,9 @@ rl_initialize(void) if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0) editmode = 0; - e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr); + e = el_init_internal(rl_readline_name, rl_instream, rl_outstream, + stderr, fileno(rl_instream), fileno(rl_outstream), fileno(stderr), + NO_RESET); if (!editmode) el_set(e, EL_EDITMODE, 0); @@ -316,7 +319,7 @@ rl_initialize(void) el_end(e); return -1; } - el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE); + el_set(e, EL_PROMPT_ESC, _get_prompt, RL_PROMPT_START_IGNORE); el_set(e, EL_SIGNAL, rl_catch_signals); /* set default mode to "emacs"-style and read setting afterwards */ @@ -371,7 +374,7 @@ rl_initialize(void) el_set(e, EL_BIND, "\\e[1;5C", "em-next-word", NULL); el_set(e, EL_BIND, "\\e[1;5D", "ed-prev-word", NULL); el_set(e, EL_BIND, "\\e[5C", "em-next-word", NULL); - el_set(e, EL_BIND, "\\e[5D", "ed-erev-word", NULL); + el_set(e, EL_BIND, "\\e[5D", "ed-prev-word", NULL); el_set(e, EL_BIND, "\\e\\e[C", "em-next-word", NULL); el_set(e, EL_BIND, "\\e\\e[D", "ed-prev-word", NULL); @@ -385,8 +388,7 @@ rl_initialize(void) _resize_fun(e, &rl_line_buffer); _rl_update_pos(); - if (rl_startup_hook) - (*rl_startup_hook)(NULL, 0); + tty_end(e, TCSADRAIN); return 0; } @@ -408,19 +410,26 @@ readline(const char *p) if (e == NULL || h == NULL) rl_initialize(); + if (rl_did_startup_hook == 0 && rl_startup_hook) { + rl_did_startup_hook = 1; + (*rl_startup_hook)(NULL, 0); + } + tty_init(e); + rl_done = 0; (void)setjmp(topbuf); + buf = NULL; /* update prompt accordingly to what has been passed */ if (rl_set_prompt(prompt) == -1) - return NULL; + goto out; if (rl_pre_input_hook) (*rl_pre_input_hook)(NULL, 0); - if (rl_event_hook && !(e->el_flags&NO_TTY)) { + if (rl_event_hook && !(e->el_flags & NO_TTY)) { el_set(e, EL_GETCFN, _rl_event_read_char); used_event_hook = 1; } @@ -440,7 +449,7 @@ readline(const char *p) buf = strdup(ret); if (buf == NULL) - return NULL; + goto out; lastidx = count - 1; if (buf[lastidx] == '\n') buf[lastidx] = '\0'; @@ -450,6 +459,8 @@ readline(const char *p) history(h, &ev, H_GETSIZE); history_length = ev.num; +out: + tty_end(e, TCSADRAIN); return buf; } @@ -556,7 +567,7 @@ get_history_event(const char *cmd, int * idx++; } if (sign) - num = history_length - num + 1; + num = history_length - num + history_base; if (!(he = history_get(num))) return NULL; @@ -1344,8 +1355,14 @@ read_history(const char *filename) rl_initialize(); if (filename == NULL && (filename = _default_history_file()) == NULL) return errno; - return history(h, &ev, H_LOAD, filename) == -1 ? - (errno ? errno : EINVAL) : 0; + errno = 0; + if (history(h, &ev, H_LOAD, filename) == -1) + return errno ? errno : EINVAL; + if (history(h, &ev, H_GETSIZE) == 0) + history_length = ev.num; + if (history_length < 0) + return EINVAL; + return 0; } @@ -1365,6 +1382,28 @@ write_history(const char *filename) (errno ? errno : EINVAL) : 0; } +int +append_history(int n, const char *filename) +{ + HistEvent ev; + FILE *fp; + + if (h == NULL || e == NULL) + rl_initialize(); + if (filename == NULL && (filename = _default_history_file()) == NULL) + return errno; + + if ((fp = fopen(filename, "a")) == NULL) + return errno; + + if (history(h, &ev, H_NSAVE_FP, (size_t)n, fp) == -1) { + int serrno = errno ? errno : EINVAL; + fclose(fp); + return serrno; + } + fclose(fp); + return 0; +} /* * returns history ``num''th event @@ -1812,18 +1851,6 @@ _el_rl_tstp(EditLine *el __attribute__(( return CC_NORM; } -/* - * Display list of strings in columnar format on readline's output stream. - * 'matches' is list of strings, 'len' is number of strings in 'matches', - * 'max' is maximum length of string in 'matches'. - */ -void -rl_display_match_list(char **matches, int len, int max) -{ - - fn_display_match_list(e, matches, (size_t)len, (size_t)max); -} - static const char * /*ARGSUSED*/ _rl_completion_append_character_function(const char *dummy @@ -1837,6 +1864,19 @@ _rl_completion_append_character_function /* + * Display list of strings in columnar format on readline's output stream. + * 'matches' is list of strings, 'len' is number of strings in 'matches', + * 'max' is maximum length of string in 'matches'. + */ +void +rl_display_match_list(char **matches, int len, int max) +{ + + fn_display_match_list(e, matches, (size_t)len, (size_t)max, + _rl_completion_append_character_function); +} + +/* * complete word at current point */ /* ARGSUSED */ @@ -2034,8 +2074,8 @@ rl_callback_read_char(void) if (done && rl_linefunc != NULL) { el_set(e, EL_UNBUFFERED, 0); if (done == 2) { - if ((wbuf = strdup(buf)) != NULL) - wbuf[count] = '\0'; + if ((wbuf = strdup(buf)) != NULL) + wbuf[count] = '\0'; } else wbuf = NULL; (*(void (*)(const char *))rl_linefunc)(wbuf); @@ -2057,8 +2097,9 @@ rl_callback_handler_install(const char * void rl_callback_handler_remove(void) { - el_set(e, EL_UNBUFFERED, 0); rl_linefunc = NULL; + el_end(e); + e = NULL; } void @@ -2368,3 +2409,25 @@ rl_set_keyboard_input_timeout(int u __at { return 0; } + +void +rl_resize_terminal(void) +{ + el_resize(e); +} + +void +rl_reset_after_signal(void) +{ + if (rl_prep_term_function) + (*rl_prep_term_function)(); +} + +void +rl_echo_signal_char(int sig) +{ + int c = tty_get_signal_character(e, sig); + if (c == -1) + return; + re_putc(e, c, 0); +}