Synchronize libedit with NetBSD

It incorporates and fixes our patches to get el_gets return the proper count of
characters in unicode mode.
This commit is contained in:
Baptiste Daroussin 2015-05-18 22:03:05 +00:00
parent abdc8d91e7
commit d44bc1335e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=283084
8 changed files with 117 additions and 73 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: chartype.c,v 1.10 2011/08/16 16:25:15 christos Exp $ */
/* $NetBSD: chartype.c,v 1.12 2015/02/22 02:16:19 christos Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@ -31,7 +31,7 @@
*/
#include "config.h"
#if !defined(lint) && !defined(SCCSID)
__RCSID("$NetBSD: chartype.c,v 1.10 2011/08/16 16:25:15 christos Exp $");
__RCSID("$NetBSD: chartype.c,v 1.12 2015/02/22 02:16:19 christos Exp $");
#endif /* not lint && not SCCSID */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -42,31 +42,46 @@ __FBSDID("$FreeBSD$");
#define CT_BUFSIZ ((size_t)1024)
#ifdef WIDECHAR
protected void
ct_conv_buff_resize(ct_buffer_t *conv, size_t mincsize, size_t minwsize)
protected int
ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
{
void *p;
if (mincsize > conv->csize) {
conv->csize = mincsize;
p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
if (p == NULL) {
conv->csize = 0;
el_free(conv->cbuff);
conv->cbuff = NULL;
} else
conv->cbuff = p;
}
if (minwsize > conv->wsize) {
conv->wsize = minwsize;
p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
if (p == NULL) {
conv->wsize = 0;
el_free(conv->wbuff);
conv->wbuff = NULL;
} else
conv->wbuff = p;
if (csize <= conv->csize)
return 0;
conv->csize = csize;
p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
if (p == NULL) {
conv->csize = 0;
el_free(conv->cbuff);
conv->cbuff = NULL;
return -1;
}
conv->cbuff = p;
return 0;
}
protected int
ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
{
void *p;
if (wsize <= conv->wsize)
return 0;
conv->wsize = wsize;
p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
if (p == NULL) {
conv->wsize = 0;
el_free(conv->wbuff);
conv->wbuff = NULL;
return -1;
}
conv->wbuff = p;
return 0;
}
@ -74,26 +89,22 @@ public char *
ct_encode_string(const Char *s, ct_buffer_t *conv)
{
char *dst;
ssize_t used = 0;
ssize_t used;
if (!s)
return NULL;
if (!conv->cbuff)
ct_conv_buff_resize(conv, CT_BUFSIZ, (size_t)0);
if (!conv->cbuff)
return NULL;
dst = conv->cbuff;
while (*s) {
used = (ssize_t)(conv->csize - (size_t)(dst - conv->cbuff));
if (used < 5) {
used = dst - conv->cbuff;
ct_conv_buff_resize(conv, conv->csize + CT_BUFSIZ,
(size_t)0);
if (!conv->cbuff)
for (;;) {
used = (ssize_t)(dst - conv->cbuff);
if ((conv->csize - (size_t)used) < 5) {
if (ct_conv_cbuff_resize(conv,
conv->csize + CT_BUFSIZ) == -1)
return NULL;
dst = conv->cbuff + used;
}
if (!*s)
break;
used = ct_encode_char(dst, (size_t)5, *s);
if (used == -1) /* failed to encode, need more buffer space */
abort();
@ -107,22 +118,19 @@ ct_encode_string(const Char *s, ct_buffer_t *conv)
public Char *
ct_decode_string(const char *s, ct_buffer_t *conv)
{
size_t len = 0;
size_t len;
if (!s)
return NULL;
if (!conv->wbuff)
ct_conv_buff_resize(conv, (size_t)0, CT_BUFSIZ);
if (!conv->wbuff)
return NULL;
len = ct_mbstowcs(NULL, s, (size_t)0);
if (len == (size_t)-1)
return NULL;
if (len > conv->wsize)
ct_conv_buff_resize(conv, (size_t)0, len + 1);
if (!conv->wbuff)
return NULL;
if (conv->wsize < ++len)
if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
return NULL;
ct_mbstowcs(conv->wbuff, s, conv->wsize);
return conv->wbuff;
}
@ -141,9 +149,9 @@ ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
* the argv strings. */
for (i = 0, bufspace = 0; i < argc; ++i)
bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
ct_conv_buff_resize(conv, (size_t)0, bufspace);
if (!conv->wsize)
return NULL;
if (conv->wsize < ++bufspace)
if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1)
return NULL;
wargv = el_malloc((size_t)argc * sizeof(*wargv));

View File

@ -1,4 +1,4 @@
/* $NetBSD: chartype.h,v 1.10 2011/11/16 01:45:10 christos Exp $ */
/* $NetBSD: chartype.h,v 1.15 2015/05/17 13:14:41 christos Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@ -39,7 +39,7 @@
* supports non-BMP code points without requiring UTF-16, but nothing
* seems to actually advertise this properly, despite Unicode 3.1 having
* been around since 2001... */
#if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__)) && !defined(__FreeBSD__)
#if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__)) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
#ifndef __STDC_ISO_10646__
/* In many places it is assumed that the first 127 code points are ASCII
* compatible, so ensure wchar_t indeed does ISO 10646 and not some other
@ -67,6 +67,7 @@
#define FUN(prefix,rest) prefix ## _w ## rest
#define FUNW(type) type ## _w
#define TYPE(type) type ## W
#define FCHAR "%lc"
#define FSTR "%ls"
#define STR(x) L ## x
#define UC(c) c
@ -121,6 +122,7 @@ Width(wchar_t c)
#define FUN(prefix,rest) prefix ## _ ## rest
#define FUNW(type) type
#define TYPE(type) type
#define FCHAR "%c"
#define FSTR "%s"
#define STR(x) x
#define UC(c) (unsigned char)(c)
@ -184,7 +186,8 @@ public Char *ct_decode_string(const char *, ct_buffer_t *);
protected Char **ct_decode_argv(int, const char *[], ct_buffer_t *);
/* Resizes the conversion buffer(s) if needed. */
protected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t);
protected int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
protected int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
protected ssize_t ct_encode_char(char *, size_t, Char);
protected size_t ct_enc_width(Char);
@ -194,7 +197,8 @@ protected size_t ct_enc_width(Char);
#define ct_encode_string(s, b) (s)
#define ct_decode_string(s, b) (s)
#define ct_decode_argv(l, s, b) (s)
#define ct_conv_buff_resize(b, os, ns)
#define ct_conv_cbuff_resize(b, s) ((s) == (0))
#define ct_conv_wbuff_resize(b, s) ((s) == (0))
#define ct_encode_char(d, l, s) (*d = s, 1)
#define ct_free_argv(s)
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: readline.h,v 1.31 2010/08/04 20:29:18 christos Exp $ */
/* $NetBSD: readline.h,v 1.34 2013/05/28 00:10:34 christos Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.

View File

@ -96,7 +96,7 @@ el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
*/
el->el_flags = 0;
#ifdef WIDECHAR
if (setlocale(LC_CTYPE, NULL) != NULL) {
if (setlocale(LC_CTYPE, NULL) != NULL){
if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
el->el_flags |= CHARSET_IS_UTF8;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: eln.c,v 1.17 2014/06/18 18:12:28 christos Exp $ */
/* $NetBSD: eln.c,v 1.19 2015/05/18 15:07:04 christos Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@ -34,7 +34,7 @@
*/
#include "config.h"
#if !defined(lint) && !defined(SCCSID)
__RCSID("$NetBSD: eln.c,v 1.17 2014/06/18 18:12:28 christos Exp $");
__RCSID("$NetBSD: eln.c,v 1.19 2015/05/18 15:07:04 christos Exp $");
#endif /* not lint && not SCCSID */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -77,18 +77,18 @@ public const char *
el_gets(EditLine *el, int *nread)
{
const wchar_t *tmp;
int nwread;
*nread = 0;
if (!(el->el_flags & CHARSET_IS_UTF8))
el->el_flags |= IGNORE_EXTCHARS;
tmp = el_wgets(el, &nwread);
tmp = el_wgets(el, nread);
if (tmp != NULL) {
size_t nwread = 0;
for (int i = 0; i < *nread; i++)
nwread += ct_enc_width(tmp[i]);
*nread = (int)nwread;
}
if (!(el->el_flags & CHARSET_IS_UTF8))
el->el_flags &= ~IGNORE_EXTCHARS;
for (int i = 0; i < nwread; i++)
*nread += ct_enc_width(tmp[i]);
return ct_encode_string(tmp, &el->el_lgcyconv);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: map.c,v 1.34 2014/07/06 18:15:34 christos Exp $ */
/* $NetBSD: map.c,v 1.35 2015/05/14 10:44:15 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)map.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: map.c,v 1.34 2014/07/06 18:15:34 christos Exp $");
__RCSID("$NetBSD: map.c,v 1.35 2015/05/14 10:44:15 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
#include <sys/cdefs.h>
@ -1302,8 +1302,8 @@ map_bind(EditLine *el, int argc, const Char **argv)
return 0;
default:
(void) fprintf(el->el_errfile,
"" FSTR ": Invalid switch `%c'.\n",
argv[0], p[1]);
"" FSTR ": Invalid switch `" FCHAR "'.\n",
argv[0], (Int)p[1]);
}
else
break;

View File

@ -1,4 +1,4 @@
/* $NetBSD: readline.c,v 1.113 2014/10/18 08:33:23 snj Exp $ */
/* $NetBSD: readline.c,v 1.115 2015/04/01 15:23: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.113 2014/10/18 08:33:23 snj Exp $");
__RCSID("$NetBSD: readline.c,v 1.115 2015/04/01 15:23:15 christos Exp $");
#endif /* not lint && not SCCSID */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -364,6 +364,37 @@ rl_initialize(void)
_el_rl_tstp);
el_set(e, EL_BIND, "^Z", "rl_tstp", NULL);
/*
* Set some readline compatible key-bindings.
*/
el_set(e, EL_BIND, "^R", "em-inc-search-prev", NULL);
/*
* Allow the use of Home/End keys.
*/
el_set(e, EL_BIND, "\\e[1~", "ed-move-to-beg", NULL);
el_set(e, EL_BIND, "\\e[4~", "ed-move-to-end", NULL);
el_set(e, EL_BIND, "\\e[7~", "ed-move-to-beg", NULL);
el_set(e, EL_BIND, "\\e[8~", "ed-move-to-end", NULL);
el_set(e, EL_BIND, "\\e[H", "ed-move-to-beg", NULL);
el_set(e, EL_BIND, "\\e[F", "ed-move-to-end", NULL);
/*
* Allow the use of the Delete/Insert keys.
*/
el_set(e, EL_BIND, "\\e[3~", "ed-delete-next-char", NULL);
el_set(e, EL_BIND, "\\e[2~", "ed-quoted-insert", NULL);
/*
* Ctrl-left-arrow and Ctrl-right-arrow for word moving.
*/
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-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);
/* read settings from configuration file */
el_source(e, NULL);
@ -950,7 +981,8 @@ loop:
for (; str[j]; j++) {
if (str[j] == '\\' &&
str[j + 1] == history_expansion_char) {
(void)strcpy(&str[j], &str[j + 1]);
len = strlen(&str[j + 1]) + 1;
memmove(&str[j], &str[j + 1], len);
continue;
}
if (!loop_again) {

View File

@ -1,4 +1,4 @@
/* $NetBSD: tty.c,v 1.46 2014/06/18 18:52:49 christos Exp $ */
/* $NetBSD: tty.c,v 1.47 2015/05/14 10:44:15 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)tty.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: tty.c,v 1.46 2014/06/18 18:52:49 christos Exp $");
__RCSID("$NetBSD: tty.c,v 1.47 2015/05/14 10:44:15 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
#include <sys/cdefs.h>
@ -1173,8 +1173,8 @@ tty_stty(EditLine *el, int argc __attribute__((__unused__)), const Char **argv)
break;
default:
(void) fprintf(el->el_errfile,
"%s: Unknown switch `%c'.\n",
name, argv[0][1]);
"%s: Unknown switch `" FCHAR "'.\n",
name, (Int)argv[0][1]);
return -1;
}