Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libedit PR/56618: Walter Lozano: Improve libedit compati...



details:   https://anonhg.NetBSD.org/src/rev/0f2e7eb09b40
branches:  trunk
changeset: 359587:0f2e7eb09b40
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Jan 11 18:30:15 2022 +0000

description:
PR/56618: Walter Lozano: Improve libedit compatibility with readline by
implementing:

    rl_copy_text, rl_erase_empty_line, rl_message, rl_on_new_line,
    rl_replace_line, rl_restore_prompt, rl_save_prompt

diffstat:

 lib/libedit/chared.c            |  33 +++++++++++++++++-
 lib/libedit/eln.c               |  10 ++++-
 lib/libedit/histedit.h          |   4 +-
 lib/libedit/readline.c          |  72 +++++++++++++++++++++++++++++++++++++++-
 lib/libedit/readline/readline.h |  12 +++++-
 5 files changed, 121 insertions(+), 10 deletions(-)

diffs (280 lines):

diff -r ca17f36a56a3 -r 0f2e7eb09b40 lib/libedit/chared.c
--- a/lib/libedit/chared.c      Tue Jan 11 11:10:46 2022 +0000
+++ b/lib/libedit/chared.c      Tue Jan 11 18:30:15 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chared.c,v 1.59 2019/07/23 10:18:52 christos Exp $     */
+/*     $NetBSD: chared.c,v 1.60 2022/01/11 18:30:15 christos Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)chared.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: chared.c,v 1.59 2019/07/23 10:18:52 christos Exp $");
+__RCSID("$NetBSD: chared.c,v 1.60 2022/01/11 18:30:15 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -624,6 +624,35 @@
                el->el_line.cursor = el->el_line.buffer;
 }
 
+/* el_wreplacestr():
+ *     Replace the contents of the line with the provided string
+ */
+int
+el_wreplacestr(EditLine *el, const wchar_t *s)
+{
+       size_t len;
+       wchar_t * p;
+
+       if (s == NULL || (len = wcslen(s)) == 0)
+               return -1;
+
+       if (el->el_line.buffer + len >= el->el_line.limit) {
+               if (!ch_enlargebufs(el, len))
+                       return -1;
+       }
+
+       p = el->el_line.buffer;
+       for (size_t i = 0; i < len; i++)
+               *p++ = *s++;
+
+       el->el_line.buffer[len] = '\0';
+       el->el_line.lastchar = el->el_line.buffer + len;
+       if (el->el_line.cursor > el->el_line.lastchar)
+               el->el_line.cursor = el->el_line.lastchar;
+
+       return 0;
+}
+
 /* el_cursor():
  *     Move the cursor to the left or the right of the current position
  */
diff -r ca17f36a56a3 -r 0f2e7eb09b40 lib/libedit/eln.c
--- a/lib/libedit/eln.c Tue Jan 11 11:10:46 2022 +0000
+++ b/lib/libedit/eln.c Tue Jan 11 18:30:15 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eln.c,v 1.36 2021/08/15 10:08:41 christos Exp $        */
+/*     $NetBSD: eln.c,v 1.37 2022/01/11 18:30:15 christos Exp $        */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 #include "config.h"
 #if !defined(lint) && !defined(SCCSID)
-__RCSID("$NetBSD: eln.c,v 1.36 2021/08/15 10:08:41 christos Exp $");
+__RCSID("$NetBSD: eln.c,v 1.37 2022/01/11 18:30:15 christos Exp $");
 #endif /* not lint && not SCCSID */
 
 #include <errno.h>
@@ -386,3 +386,9 @@
 {
        return el_winsertstr(el, ct_decode_string(str, &el->el_lgcyconv));
 }
+
+int
+el_replacestr(EditLine *el, const char *str)
+{
+       return el_wreplacestr(el, ct_decode_string(str, &el->el_lgcyconv));
+}
diff -r ca17f36a56a3 -r 0f2e7eb09b40 lib/libedit/histedit.h
--- a/lib/libedit/histedit.h    Tue Jan 11 11:10:46 2022 +0000
+++ b/lib/libedit/histedit.h    Tue Jan 11 18:30:15 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: histedit.h,v 1.58 2021/08/15 10:08:41 christos Exp $   */
+/*     $NetBSD: histedit.h,v 1.59 2022/01/11 18:30:15 christos Exp $   */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -179,6 +179,7 @@
 const LineInfo *el_line(EditLine *);
 int             el_insertstr(EditLine *, const char *);
 void            el_deletestr(EditLine *, int);
+int             el_replacestr(EditLine *el, const char *str);
 
 
 /*
@@ -278,6 +279,7 @@
 const LineInfoW        *el_wline(EditLine *);
 int             el_winsertstr(EditLine *, const wchar_t *);
 #define          el_wdeletestr  el_deletestr
+int             el_wreplacestr(EditLine *, const wchar_t *);
 
 /*
  * ==== History ====
diff -r ca17f36a56a3 -r 0f2e7eb09b40 lib/libedit/readline.c
--- a/lib/libedit/readline.c    Tue Jan 11 11:10:46 2022 +0000
+++ b/lib/libedit/readline.c    Tue Jan 11 18:30:15 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: readline.c,v 1.168 2021/09/10 18:51:36 rillig Exp $    */
+/*     $NetBSD: readline.c,v 1.169 2022/01/11 18:30:15 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.168 2021/09/10 18:51:36 rillig Exp $");
+__RCSID("$NetBSD: readline.c,v 1.169 2022/01/11 18:30:15 christos Exp $");
 #endif /* not lint && not SCCSID */
 
 #include <sys/types.h>
@@ -43,6 +43,7 @@
 #include <limits.h>
 #include <pwd.h>
 #include <setjmp.h>
+#include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -136,11 +137,13 @@
 int _rl_echoing_p;
 int history_max_entries;
 char *rl_display_prompt;
+int rl_erase_empty_line;
 
 /*
  * The current prompt string.
  */
 char *rl_prompt = NULL;
+char *rl_prompt_saved = NULL;
 /*
  * This is set to character indicating type of completion being done by
  * rl_complete_internal(); this is available for application completion
@@ -278,6 +281,21 @@
        return 0;
 }
 
+void
+rl_save_prompt(void)
+{
+       rl_prompt_saved = strdup(rl_prompt);
+}
+
+void
+rl_restore_prompt(void)
+{
+       if (!rl_prompt_saved)
+               return;
+       rl_prompt = rl_prompt_saved;
+       rl_prompt_saved = NULL;
+}
+
 /*
  * initialize rl compat stuff
  */
@@ -2281,6 +2299,41 @@
        rl_line_buffer[rl_end] = '\0';
 }
 
+char *
+rl_copy_text(int from, int to)
+{
+       const LineInfo *li = el_line(e);
+       size_t len;
+       char * out;
+
+       if (from > to)
+               return NULL;
+
+       if (li->buffer + from > li->lastchar)
+               from = (int)(li->lastchar - li->buffer);
+
+       if (li->buffer + to > li->lastchar)
+               to = (int)(li->lastchar - li->buffer);
+
+       len = (size_t)(to - from);
+       out = el_malloc((size_t)len + 1);
+       (void)strlcpy(out, li->buffer + from , len);
+
+       return out;
+}
+
+void
+rl_replace_line(const char * text, int clear_undo __attribute__((__unused__)))
+{
+       if (!text || *text == 0)
+               return;
+
+       if (h == NULL || e == NULL)
+               rl_initialize();
+
+       el_replacestr(e, text);
+}
+
 void
 rl_get_screen_size(int *rows, int *cols)
 {
@@ -2290,6 +2343,21 @@
                el_get(e, EL_GETTC, "co", cols);
 }
 
+#define MAX_MESSAGE 160
+void
+rl_message(const char *format, ...)
+{
+       char msg[MAX_MESSAGE];
+       va_list args;
+
+       va_start(args, format);
+       vsnprintf(msg, sizeof(msg), format, args);
+       va_end(args);
+
+       rl_set_prompt(msg);
+       rl_redisplay();
+}
+
 void
 rl_set_screen_size(int rows, int cols)
 {
diff -r ca17f36a56a3 -r 0f2e7eb09b40 lib/libedit/readline/readline.h
--- a/lib/libedit/readline/readline.h   Tue Jan 11 11:10:46 2022 +0000
+++ b/lib/libedit/readline/readline.h   Tue Jan 11 18:30:15 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: readline.h,v 1.47 2021/08/21 12:34:59 christos Exp $   */
+/*     $NetBSD: readline.h,v 1.48 2022/01/11 18:30:15 christos Exp $   */
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -153,6 +153,7 @@
 extern int             _rl_echoing_p;
 extern int             history_max_entries;
 extern char            *rl_display_prompt;
+extern int             rl_erase_empty_line;
 
 /* supported functions */
 char           *readline(const char *);
@@ -180,7 +181,7 @@
 int             history_search_pos(const char *, int, int);
 int             read_history(const char *);
 int             write_history(const char *);
-int             history_truncate_file (const char *, int);
+int             history_truncate_file(const char *, int);
 int             history_expand(char *, char **);
 char          **history_tokenize(const char *);
 const char     *get_history_event(const char *, int *, int);
@@ -215,7 +216,7 @@
 HISTORY_STATE  *history_get_history_state(void);
 void            rl_get_screen_size(int *, int *);
 void            rl_set_screen_size(int, int);
-char           *rl_filename_completion_function (const char *, 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 *);
@@ -226,6 +227,11 @@
 void            rl_echo_signal_char(int);
 int             rl_crlf(void);
 int             rl_ding(void);
+char           *rl_copy_text(int, int);
+void            rl_replace_line(const char *, int);
+void            rl_message(const char *format, ...);
+void            rl_save_prompt(void);
+void            rl_restore_prompt(void);
 
 /*
  * The following are not implemented



Home | Main Index | Thread Index | Old Index