Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/gen Wide char support from J.R. Oldroyd
details: https://anonhg.NetBSD.org/src/rev/b8728c684633
branches: trunk
changeset: 784886:b8728c684633
user: christos <christos%NetBSD.org@localhost>
date: Wed Feb 13 04:49:59 2013 +0000
description:
Wide char support from J.R. Oldroyd
diffstat:
lib/libc/gen/vis.c | 581 +++++++++++++++++++++++-----------------------------
1 files changed, 256 insertions(+), 325 deletions(-)
diffs (truncated from 767 to 300 lines):
diff -r bd17d59597ca -r b8728c684633 lib/libc/gen/vis.c
--- a/lib/libc/gen/vis.c Wed Feb 13 04:22:36 2013 +0000
+++ b/lib/libc/gen/vis.c Wed Feb 13 04:49:59 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: vis.c,v 1.45 2012/12/14 21:38:18 christos Exp $ */
+/* $NetBSD: vis.c,v 1.46 2013/02/13 04:49:59 christos Exp $ */
/*-
* Copyright (c) 1989, 1993
@@ -57,8 +57,12 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: vis.c,v 1.45 2012/12/14 21:38:18 christos Exp $");
+__RCSID("$NetBSD: vis.c,v 1.46 2013/02/13 04:49:59 christos Exp $");
#endif /* LIBC_SCCS and not lint */
+#ifdef __FBSDID
+__FBSDID("$FreeBSD$");
+#define _DIAGASSERT(x) assert(x)
+#endif
#include "namespace.h"
#include <sys/types.h>
@@ -67,6 +71,8 @@
#include <vis.h>
#include <errno.h>
#include <stdlib.h>
+#include <wchar.h>
+#include <wctype.h>
#ifdef __weak_alias
__weak_alias(strvisx,_strvisx)
@@ -78,65 +84,59 @@
#include <stdio.h>
#include <string.h>
-static char *do_svis(char *, size_t *, int, int, int, const char *);
+static wchar_t *do_svis(wchar_t *, int, int, int, const wchar_t *);
#undef BELL
-#define BELL '\a'
+#define BELL L'\a'
-#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
-#define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
-#define issafe(c) (c == '\b' || c == BELL || c == '\r')
-#define xtoa(c) "0123456789abcdef"[c]
-#define XTOA(c) "0123456789ABCDEF"[c]
+#define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
+#define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n')
+#define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r')
+#define xtoa(c) L"0123456789abcdef"[c]
+#define XTOA(c) L"0123456789ABCDEF"[c]
#define MAXEXTRAS 9
#define MAKEEXTRALIST(flag, extra, orig_str) \
do { \
- const char *orig = orig_str; \
- const char *o = orig; \
- char *e; \
+ const wchar_t *orig = orig_str; \
+ const wchar_t *o = orig; \
+ wchar_t *e; \
while (*o++) \
continue; \
- extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
+ extra = calloc((size_t)((o - orig) + MAXEXTRAS), sizeof(*extra)); \
if (!extra) break; \
- for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
+ for (o = orig, e = extra; (*e++ = *o++) != L'\0';) \
continue; \
e--; \
if (flag & VIS_GLOB) { \
- *e++ = '*'; \
- *e++ = '?'; \
- *e++ = '['; \
- *e++ = '#'; \
+ *e++ = L'*'; \
+ *e++ = L'?'; \
+ *e++ = L'['; \
+ *e++ = L'#'; \
} \
- if (flag & VIS_SP) *e++ = ' '; \
- if (flag & VIS_TAB) *e++ = '\t'; \
- if (flag & VIS_NL) *e++ = '\n'; \
- if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
- *e = '\0'; \
+ if (flag & VIS_SP) *e++ = L' '; \
+ if (flag & VIS_TAB) *e++ = L'\t'; \
+ if (flag & VIS_NL) *e++ = L'\n'; \
+ if ((flag & VIS_NOSLASH) == 0) *e++ = L'\\'; \
+ *e = L'\0'; \
} while (/*CONSTCOND*/0)
/*
* This is do_hvis, for HTTP style (RFC 1808)
*/
-static char *
-do_hvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
+static wchar_t *
+do_hvis(wchar_t *dst, wint_t c, int flag, wint_t nextc, const wchar_t *extra)
{
-
- if ((isascii(c) && isalnum(c))
+ if (iswalnum(c)
/* safe */
- || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
+ || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
/* extra */
- || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
- || c == ',') {
- dst = do_svis(dst, dlen, c, flag, nextc, extra);
- } else {
- if (dlen) {
- if (*dlen < 3)
- return NULL;
- *dlen -= 3;
- }
- *dst++ = '%';
+ || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
+ || c == L',')
+ dst = do_svis(dst, c, flag, nextc, extra);
+ else {
+ *dst++ = L'%';
*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
*dst++ = xtoa((unsigned int)c & 0xf);
}
@@ -148,27 +148,21 @@
* This is do_mvis, for Quoted-Printable MIME (RFC 2045)
* NB: No handling of long lines or CRLF.
*/
-static char *
-do_mvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
+static wchar_t *
+do_mvis(wchar_t *dst, wint_t c, int flag, wint_t nextc, const wchar_t *extra)
{
- if ((c != '\n') &&
+ if ((c != L'\n') &&
/* Space at the end of the line */
- ((isspace(c) && (nextc == '\r' || nextc == '\n')) ||
+ ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
/* Out of range */
- (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
+ (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
/* Specific char to be escaped */
- strchr("#$@[\\]^`{|}~", c) != NULL)) {
- if (dlen) {
- if (*dlen < 3)
- return NULL;
- *dlen -= 3;
- }
- *dst++ = '=';
+ wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
+ *dst++ = L'=';
*dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
*dst++ = XTOA((unsigned int)c & 0xf);
- } else {
- dst = do_svis(dst, dlen, c, flag, nextc, extra);
- }
+ } else
+ dst = do_svis(dst, c, flag, nextc, extra);
return dst;
}
@@ -181,107 +175,87 @@
* extra: Pointer to the list of extra characters to be
* backslash-protected.
*/
-static char *
-do_svis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
+static wchar_t *
+do_svis(wchar_t *dst, wint_t c, int flag, wint_t nextc, const wchar_t *extra)
{
- int isextra;
- size_t odlen = dlen ? *dlen : 0;
+ int iswextra;
- isextra = strchr(extra, c) != NULL;
-#define HAVE(x) \
- do { \
- if (dlen) { \
- if (*dlen < (x)) \
- goto out; \
- *dlen -= (x); \
- } \
- } while (/*CONSTCOND*/0)
- if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
- ((flag & VIS_SAFE) && issafe(c)))) {
- HAVE(1);
+ iswextra = wcschr(extra, c) != NULL;
+ if (!iswextra && (iswgraph(c) || iswwhite(c) ||
+ ((flag & VIS_SAFE) && iswsafe(c)))) {
*dst++ = c;
return dst;
}
if (flag & VIS_CSTYLE) {
- HAVE(2);
switch (c) {
- case '\n':
- *dst++ = '\\'; *dst++ = 'n';
+ case L'\n':
+ *dst++ = L'\\'; *dst++ = L'n';
return dst;
- case '\r':
- *dst++ = '\\'; *dst++ = 'r';
+ case L'\r':
+ *dst++ = L'\\'; *dst++ = L'r';
return dst;
- case '\b':
- *dst++ = '\\'; *dst++ = 'b';
+ case L'\b':
+ *dst++ = L'\\'; *dst++ = L'b';
return dst;
case BELL:
- *dst++ = '\\'; *dst++ = 'a';
+ *dst++ = L'\\'; *dst++ = L'a';
return dst;
- case '\v':
- *dst++ = '\\'; *dst++ = 'v';
+ case L'\v':
+ *dst++ = L'\\'; *dst++ = L'v';
return dst;
- case '\t':
- *dst++ = '\\'; *dst++ = 't';
+ case L'\t':
+ *dst++ = L'\\'; *dst++ = L't';
return dst;
- case '\f':
- *dst++ = '\\'; *dst++ = 'f';
+ case L'\f':
+ *dst++ = L'\\'; *dst++ = L'f';
return dst;
- case ' ':
- *dst++ = '\\'; *dst++ = 's';
+ case L' ':
+ *dst++ = L'\\'; *dst++ = L's';
return dst;
- case '\0':
- *dst++ = '\\'; *dst++ = '0';
- if (isoctal(nextc)) {
- HAVE(2);
- *dst++ = '0';
- *dst++ = '0';
+ case L'\0':
+ *dst++ = L'\\'; *dst++ = L'0';
+ if (iswoctal(nextc)) {
+ *dst++ = L'0';
+ *dst++ = L'0';
}
return dst;
default:
- if (isgraph(c)) {
- *dst++ = '\\'; *dst++ = c;
+ if (iswgraph(c)) {
+ *dst++ = L'\\';
+ *dst++ = c;
return dst;
}
- if (dlen)
- *dlen = odlen;
}
}
- if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
- HAVE(4);
- *dst++ = '\\';
- *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';
- *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';
- *dst++ = (c & 07) + '0';
+ if (iswextra || ((c & 0177) == L' ') || (flag & VIS_OCTAL)) {
+ *dst++ = L'\\';
+ *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
+ *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
+ *dst++ = (c & 07) + L'0';
} else {
- if ((flag & VIS_NOSLASH) == 0) {
- HAVE(1);
- *dst++ = '\\';
- }
+ if ((flag & VIS_NOSLASH) == 0)
+ *dst++ = L'\\';
if (c & 0200) {
- HAVE(1);
- c &= 0177; *dst++ = 'M';
+ c &= 0177;
+ *dst++ = L'M';
}
- if (iscntrl(c)) {
- HAVE(2);
- *dst++ = '^';
+ if (iswcntrl(c)) {
+ *dst++ = L'^';
if (c == 0177)
- *dst++ = '?';
+ *dst++ = L'?';
else
- *dst++ = c + '@';
+ *dst++ = c + L'@';
Home |
Main Index |
Thread Index |
Old Index