From Ingo Schwartze:

Next step:  Remove #ifdef'ing in read_char(), in the same style
as we did for setlocale(3) in el.c.

A few remarks are required to explain the choices made.

 * On first sight, handling mbrtowc(3) seems a bit less trivial
   than handling setlocale(3) because its prototype uses the data
   type mbstate_t from <wchar.h>.  However, it turns out that
   "histedit.h" already includes <wchar.h> unconditionally (i don't
   like headers including other headers, but that ship has sailed,
   people are by now certainly used to the fact that including
   "histedit.h" doesn't require including <wchar.h> before), and
   "histedit.h" is of course included all over the place.  So from
   that perspective, there is no problem with using mbrtowc(3)
   unconditionally ever for !WIDECHAR.

 * However, <wchar.h> also defines the mbrtowc(3) prototype,
   so we cannot just #define mbrtowc away, or including the header
   will break.  It would also be a bad idea to porovide a local
   implementation of mbrtowc() and hope that it overrides the one
   in libc.  Besides, the required prototype is subtly different:
   While mbrtowc(3) takes "wchar_t *" as its first argument, we
   need a function that takes "Char *".  So unfortunately, we have
   to keep a ct_mbrtowc #define, at least until we can maybe get
   rid of "Char *" in the more remote future.

 * After getting rid of the #else clause in read_char(), we can
   pull "return 1;" into the default: clause.  After that, we can
   get rid of the ugly "goto again_lastbyte;" and just "break;".
   As a bonus, that also gets rid of the ugly CONSTCOND.

 * While here, delete the unused ct_mbtowc() from chartype.h.
This commit is contained in:
christos 2016-02-14 14:47:48 +00:00
parent 542c3c7262
commit 61ee30487d
3 changed files with 25 additions and 19 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: chartype.c,v 1.13 2016/02/11 19:21:04 christos Exp $ */ /* $NetBSD: chartype.c,v 1.14 2016/02/14 14:47:48 christos Exp $ */
/*- /*-
* Copyright (c) 2009 The NetBSD Foundation, Inc. * Copyright (c) 2009 The NetBSD Foundation, Inc.
@ -38,7 +38,7 @@
*/ */
#include "config.h" #include "config.h"
#if !defined(lint) && !defined(SCCSID) #if !defined(lint) && !defined(SCCSID)
__RCSID("$NetBSD: chartype.c,v 1.13 2016/02/11 19:21:04 christos Exp $"); __RCSID("$NetBSD: chartype.c,v 1.14 2016/02/14 14:47:48 christos Exp $");
#endif /* not lint && not SCCSID */ #endif /* not lint && not SCCSID */
#include "el.h" #include "el.h"
#include <stdlib.h> #include <stdlib.h>
@ -210,6 +210,20 @@ ct_encode_char(char *dst, size_t len, Char c)
} }
return l; return l;
} }
#else
size_t
ct_mbrtowc(char *wc, const char *s, size_t n,
void *mbs __attribute((__unused__))) {
if (s == NULL)
return 0;
if (n == 0)
return (size_t)-2;
if (wc != NULL)
*wc = *s;
return *s != '\0';
}
#endif #endif
protected const Char * protected const Char *

View File

@ -1,4 +1,4 @@
/* $NetBSD: chartype.h,v 1.17 2016/02/11 19:10:18 christos Exp $ */ /* $NetBSD: chartype.h,v 1.18 2016/02/14 14:47:48 christos Exp $ */
/*- /*-
* Copyright (c) 2009 The NetBSD Foundation, Inc. * Copyright (c) 2009 The NetBSD Foundation, Inc.
@ -60,7 +60,6 @@
#warning Build environment does not support non-BMP characters #warning Build environment does not support non-BMP characters
#endif #endif
#define ct_mbtowc mbtowc
#define ct_mbrtowc mbrtowc #define ct_mbrtowc mbrtowc
#define ct_wctomb wctomb #define ct_wctomb wctomb
#define ct_wctomb_reset wctomb(0,0) #define ct_wctomb_reset wctomb(0,0)
@ -115,8 +114,7 @@ Width(wchar_t c)
#else /* NARROW */ #else /* NARROW */
#define ct_mbtowc error size_t ct_mbrtowc(char *, const char *, size_t, void *);
#define ct_mbrtowc error
#define ct_wctomb error #define ct_wctomb error
#define ct_wctomb_reset #define ct_wctomb_reset
#define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a)) #define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a))

View File

@ -1,4 +1,4 @@
/* $NetBSD: read.c,v 1.76 2016/02/12 15:36:08 christos Exp $ */ /* $NetBSD: read.c,v 1.77 2016/02/14 14:47:48 christos Exp $ */
/*- /*-
* Copyright (c) 1992, 1993 * Copyright (c) 1992, 1993
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: read.c,v 1.76 2016/02/12 15:36:08 christos Exp $"); __RCSID("$NetBSD: read.c,v 1.77 2016/02/14 14:47:48 christos Exp $");
#endif #endif
#endif /* not lint && not SCCSID */ #endif /* not lint && not SCCSID */
@ -337,10 +337,9 @@ read_char(EditLine *el, Char *cp)
return 0; return 0;
} }
#ifdef WIDECHAR for (;;) {
do {
mbstate_t mbs; mbstate_t mbs;
again_lastbyte:
++cbp; ++cbp;
/* This only works because UTF8 is stateless */ /* This only works because UTF8 is stateless */
memset(&mbs, 0, sizeof(mbs)); memset(&mbs, 0, sizeof(mbs));
@ -353,7 +352,7 @@ again_lastbyte:
*/ */
cbuf[0] = cbuf[cbp - 1]; cbuf[0] = cbuf[cbp - 1];
cbp = 0; cbp = 0;
goto again_lastbyte; break;
} else { } else {
/* Invalid byte, discard it. */ /* Invalid byte, discard it. */
cbp = 0; cbp = 0;
@ -375,14 +374,9 @@ again_lastbyte:
goto again; goto again;
default: default:
/* Valid character, process it. */ /* Valid character, process it. */
break;
}
} while (/*CONSTCOND*/0);
#else
*cp = (Char)(unsigned char)cbuf[0];
#endif
return 1; return 1;
}
}
} }
/* read_pop(): /* read_pop():