NetBSD Problem Report #56618

From www@netbsd.org  Tue Jan 11 17:30:17 2022
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id C16C61A923A
	for <gnats-bugs@gnats.NetBSD.org>; Tue, 11 Jan 2022 17:30:17 +0000 (UTC)
Message-Id: <20220111173015.D9C3C1A923B@mollari.NetBSD.org>
Date: Tue, 11 Jan 2022 17:30:15 +0000 (UTC)
From: walter.lozano@collabora.com
Reply-To: walter.lozano@collabora.com
To: gnats-bugs@NetBSD.org
Subject: Improve libedit compatibility with readline
X-Send-Pr-Version: www-1.0

>Number:         56618
>Category:       lib
>Synopsis:       Improve libedit compatibility with readline
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue Jan 11 17:35:00 +0000 2022
>Last-Modified:  Tue Jan 11 19:25:01 +0000 2022
>Originator:     Walter Lozano
>Release:        
>Organization:
Collabora
>Environment:
>Description:
In order to improve libedit compatibility some additional symbols should be implemented

rl_copy_text
rl_save_prompt
rl_replace_line
rl_restore_prompt
rl_message
rl_erase_empty_line
rl_on_new_line
>How-To-Repeat:

>Fix:
I have prepared the following patch in order to improve the support. Please let me know if I should submit the proposed changes in other way, I did my best to follow the guidelines in the website.

Index: chared.c
===================================================================
RCS file: /cvsroot/src/lib/libedit/chared.c,v
retrieving revision 1.59
diff -u -r1.59 chared.c
--- chared.c	23 Jul 2019 10:18:52 -0000	1.59
+++ chared.c	11 Jan 2022 17:24:18 -0000
@@ -624,6 +624,30 @@
 		el->el_line.cursor = el->el_line.buffer;
 }

+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
  */
Index: eln.c
===================================================================
RCS file: /cvsroot/src/lib/libedit/eln.c,v
retrieving revision 1.36
diff -u -r1.36 eln.c
--- eln.c	15 Aug 2021 10:08:41 -0000	1.36
+++ eln.c	11 Jan 2022 17:24:18 -0000
@@ -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));
+}
Index: histedit.h
===================================================================
RCS file: /cvsroot/src/lib/libedit/histedit.h,v
retrieving revision 1.58
diff -u -r1.58 histedit.h
--- histedit.h	15 Aug 2021 10:08:41 -0000	1.58
+++ histedit.h	11 Jan 2022 17:24:18 -0000
@@ -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 ====
Index: readline.c
===================================================================
RCS file: /cvsroot/src/lib/libedit/readline.c,v
retrieving revision 1.168
diff -u -r1.168 readline.c
--- readline.c	10 Sep 2021 18:51:36 -0000	1.168
+++ readline.c	11 Jan 2022 17:24:18 -0000
@@ -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,42 @@
 	rl_line_buffer[rl_end] = '\0';
 }

+char *
+rl_copy_text(int from, int to)
+{
+	const LineInfo *li = el_line(e);
+	int 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 = to - from;
+	out = (char *) el_malloc((size_t)len + 1);
+	strncpy(out, li->buffer + from , (size_t)len);
+	out[len] = 0;
+
+	return out;
+}
+
+void
+rl_replace_line (const char * text, __attribute__((unused)) int clear_undo)
+{
+	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 +2344,22 @@
 		el_get(e, EL_GETTC, "co", cols);
 }

+#define MAX_MESSAGE 160
+void
+rl_message (const char *format, ...)
+{
+	va_list args;
+	va_start (args, format);
+
+	char msg[160];
+
+	vsnprintf (msg, sizeof(msg), format, args);
+
+	rl_set_prompt(msg);
+	rl_redisplay();
+	//rl_replace_line(msg, 0);
+}
+
 void
 rl_set_screen_size(int rows, int cols)
 {
cvs diff: Diffing TEST
cvs diff: Diffing readline
Index: readline/readline.h
===================================================================
RCS file: /cvsroot/src/lib/libedit/readline/readline.h,v
retrieving revision 1.47
diff -u -r1.47 readline.h
--- readline/readline.h	21 Aug 2021 12:34:59 -0000	1.47
+++ readline/readline.h	11 Jan 2022 17:24:18 -0000
@@ -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 *);
@@ -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

>Audit-Trail:
From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56618 CVS commit: src/lib/libedit
Date: Tue, 11 Jan 2022 13:30:15 -0500

 Module Name:	src
 Committed By:	christos
 Date:		Tue Jan 11 18:30:15 UTC 2022

 Modified Files:
 	src/lib/libedit: chared.c eln.c histedit.h readline.c
 	src/lib/libedit/readline: readline.h

 Log Message:
 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


 To generate a diff of this commit:
 cvs rdiff -u -r1.59 -r1.60 src/lib/libedit/chared.c
 cvs rdiff -u -r1.36 -r1.37 src/lib/libedit/eln.c
 cvs rdiff -u -r1.58 -r1.59 src/lib/libedit/histedit.h
 cvs rdiff -u -r1.168 -r1.169 src/lib/libedit/readline.c
 cvs rdiff -u -r1.47 -r1.48 src/lib/libedit/readline/readline.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: Walter Lozano <walter.lozano@collabora.com>
To: gnats-bugs@netbsd.org, lib-bug-people@netbsd.org, gnats-admin@netbsd.org,
 netbsd-bugs@netbsd.org
Cc: 
Subject: Re: PR/56618 CVS commit: src/lib/libedit
Date: Tue, 11 Jan 2022 16:22:55 -0300

 Hi Christos,

 On 1/11/22 15:35, Christos Zoulas wrote:
 > The following reply was made to PR lib/56618; it has been noted by GNATS.
 >
 > From: "Christos Zoulas" <christos@netbsd.org>
 > To: gnats-bugs@gnats.NetBSD.org
 > Cc:
 > Subject: PR/56618 CVS commit: src/lib/libedit
 > Date: Tue, 11 Jan 2022 13:30:15 -0500
 >
 >   Module Name:	src
 >   Committed By:	christos
 >   Date:		Tue Jan 11 18:30:15 UTC 2022
 >   
 >   Modified Files:
 >   	src/lib/libedit: chared.c eln.c histedit.h readline.c
 >   	src/lib/libedit/readline: readline.h
 >   
 >   Log Message:
 >   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
 >   
 >   
 >   To generate a diff of this commit:
 >   cvs rdiff -u -r1.59 -r1.60 src/lib/libedit/chared.c
 >   cvs rdiff -u -r1.36 -r1.37 src/lib/libedit/eln.c
 >   cvs rdiff -u -r1.58 -r1.59 src/lib/libedit/histedit.h
 >   cvs rdiff -u -r1.168 -r1.169 src/lib/libedit/readline.c
 >   cvs rdiff -u -r1.47 -r1.48 src/lib/libedit/readline/readline.h
 >   
 >   Please note that diffs are not public domain; they are subject to the
 >   copyright notices on the relevant files.
 >   


 That was really fast, thank you!

 Regards,

 Walter

 -- 
 Walter Lozano
 Collabora Ltd.

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.