From 86e1c02be45d853efa683bb014e410459c98c583 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Sat, 27 Oct 2007 22:27:16 +0000 Subject: [PATCH] Sync with NetBSD. --- include/histedit.h | 10 ++--- lib/libedit/readline.c | 79 ++++++++++++++++++++++++++++----- lib/libedit/readline/readline.h | 8 ++-- 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/include/histedit.h b/include/histedit.h index d14bc70c97..634f5a56ea 100644 --- a/include/histedit.h +++ b/include/histedit.h @@ -1,5 +1,5 @@ -/* $DragonFly: src/include/histedit.h,v 1.5 2007/05/05 00:27:39 pavalos Exp $ */ -/* $NetBSD: histedit.h,v 1.31 2006/12/15 22:13:33 christos Exp $ */ +/* $DragonFly: src/include/histedit.h,v 1.6 2007/10/27 22:27:16 pavalos Exp $ */ +/* $NetBSD: histedit.h,v 1.32 2007/06/10 20:20:28 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -132,10 +132,10 @@ unsigned char _el_fn_complete(EditLine *, int); #define EL_UNBUFFERED 15 /* , int); */ #define EL_PREP_TERM 16 /* , int); */ #define EL_GETTC 17 /* , const char *, ..., NULL); */ -#define EL_GETFP 18 /* , int, FILE **) */ -#define EL_SETFP 19 /* , int, FILE *) */ +#define EL_GETFP 18 /* , int, FILE **) */ +#define EL_SETFP 19 /* , int, FILE *) */ -#define EL_BUILTIN_GETCFN (NULL) +#define EL_BUILTIN_GETCFN (NULL) /* * Source named file or $PWD/.editrc or $HOME/.editrc diff --git a/lib/libedit/readline.c b/lib/libedit/readline.c index 402c5a2bd7..423f7c3dfd 100644 --- a/lib/libedit/readline.c +++ b/lib/libedit/readline.c @@ -29,8 +29,8 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $NetBSD: readline.c,v 1.70 2006/11/24 00:01:17 christos Exp $ - * $DragonFly: src/lib/libedit/readline.c,v 1.2 2007/05/05 00:27:39 pavalos Exp $ + * $NetBSD: readline.c,v 1.72 2007/08/12 07:41:51 christos Exp $ + * $DragonFly: src/lib/libedit/readline.c,v 1.3 2007/10/27 22:27:16 pavalos Exp $ */ #include "config.h" @@ -109,7 +109,7 @@ Function *rl_completion_entry_function = NULL; CPPFunction *rl_attempted_completion_function = NULL; Function *rl_pre_input_hook = NULL; Function *rl_startup1_hook = NULL; -Function *rl_getc_function = NULL; +int (*rl_getc_function)(FILE *) = NULL; char *rl_terminal_name = NULL; int rl_already_prompted = 0; int rl_filename_completion_desired = 0; @@ -211,7 +211,7 @@ _getc_function(EditLine *el, char *c) { int i; - i = (*rl_getc_function)(NULL, 0); + i = (*rl_getc_function)(NULL); if (i == -1) return 0; *c = i; @@ -1166,7 +1166,7 @@ history_get(int num) return (NULL); /* error */ /* look backwards for event matching specified offset */ - if (history(h, &ev, H_NEXT_EVENT, num)) + if (history(h, &ev, H_NEXT_EVENT, num + 1)) return (NULL); she.line = ev.str; @@ -1204,7 +1204,7 @@ add_history(const char *line) HIST_ENTRY * remove_history(int num) { - static HIST_ENTRY she; + HIST_ENTRY *she; HistEvent ev; if (h == NULL || e == NULL) @@ -1213,10 +1213,13 @@ remove_history(int num) if (history(h, &ev, H_DEL, num) != 0) return NULL; - she.line = ev.str; - she.data = NULL; + if ((she = malloc(sizeof(*she))) == NULL) + return NULL; + + she->line = ev.str; + she->data = NULL; - return &she; + return she; } @@ -1687,7 +1690,7 @@ rl_callback_read_char() } void -rl_callback_handler_install (const char *prompt, VCPFunction *linefunc) +rl_callback_handler_install(const char *prompt, VCPFunction *linefunc) { if (e == NULL) { rl_initialize(); @@ -1703,6 +1706,7 @@ void rl_callback_handler_remove(void) { el_set(e, EL_UNBUFFERED, 0); + rl_linefunc = NULL; } void @@ -1848,6 +1852,61 @@ rl_set_screen_size(int rows, int cols) el_set(e, EL_SETTC, "co", buf); } +char ** +rl_completion_matches(const char *str, rl_compentry_func_t *fun) +{ + size_t len, max, i, j, min; + char **list, *match, *a, *b; + + len = 1; + max = 10; + if ((list = malloc(max * sizeof(*list))) == NULL) + return NULL; + + while ((match = (*fun)(str, (int)(len - 1))) != NULL) { + if (len == max) { + char **nl; + max += 10; + if ((nl = realloc(list, max * sizeof(*nl))) == NULL) + goto out; + list = nl; + } + list[len++] = match; + } + if (len == 1) + goto out; + list[len] = NULL; + if (len == 2) { + if ((list[0] = strdup(list[1])) == NULL) + goto out; + return list; + } + qsort(&list[1], len - 1, sizeof(*list), + (int (*)(const void *, const void *)) strcmp); + min = SIZE_T_MAX; + for (i = 1, a = list[i]; i < len - 1; i++, a = b) { + b = list[i + 1]; + for (j = 0; a[j] && a[j] == b[j]; j++) + continue; + if (min > j) + min = j; + } + if (min == 0 && *str) { + if ((list[0] = strdup(str)) == NULL) + goto out; + } else { + if ((list[0] = malloc(min + 1)) == NULL) + goto out; + (void)memcpy(list[0], list[1], min); + list[0][min] = '\0'; + } + return list; + +out: + free(list); + return NULL; +} + char * rl_filename_completion_function (const char *text, int state) { diff --git a/lib/libedit/readline/readline.h b/lib/libedit/readline/readline.h index 440b73c68d..b67e6ced16 100644 --- a/lib/libedit/readline/readline.h +++ b/lib/libedit/readline/readline.h @@ -29,8 +29,8 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $NetBSD: readline.h,v 1.19 2006/11/24 00:01:17 christos Exp $ - * $DragonFly: src/lib/libedit/readline/readline.h,v 1.2 2007/05/05 00:27:40 pavalos Exp $ + * $NetBSD: readline.h,v 1.21 2007/08/12 07:41:51 christos Exp $ + * $DragonFly: src/lib/libedit/readline/readline.h,v 1.3 2007/10/27 22:27:16 pavalos Exp $ */ #ifndef _READLINE_H_ #define _READLINE_H_ @@ -45,6 +45,7 @@ typedef void VFunction(void); typedef void VCPFunction(char *); typedef char *CPFunction(const char *, int); typedef char **CPPFunction(const char *, int, int); +typedef char *rl_compentry_func_t(const char *, int); typedef struct _hist_entry { const char *line; @@ -119,7 +120,7 @@ extern KEYMAP_ENTRY_ARRAY emacs_standard_keymap, emacs_ctlx_keymap; extern int rl_filename_completion_desired; extern int rl_ignore_completion_duplicates; -extern Function *rl_getc_function; +extern int (*rl_getc_function)(FILE *); extern VFunction *rl_redisplay_function; extern VFunction *rl_completion_display_matches_hook; extern VFunction *rl_prep_term_function; @@ -184,6 +185,7 @@ void rl_set_screen_size(int, int); char *rl_filename_completion_function (const char *, int); int _rl_abort_internal(void); int _rl_qsort_string_compare(char **, char **); +char **rl_completion_matches(const char *, rl_compentry_func_t *); /* * The following are not implemented -- 2.41.0