From deb293c7fb3ac0787ba9de8d4423b0037998fc13 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 9 Dec 2023 17:01:42 -0500 Subject: bfstd: Wrap is[w]{alpha,digit,...}() --- src/bfstd.c | 43 ++++++++++++------------------------------- src/bfstd.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/bfstd.c b/src/bfstd.c index b5006d1..9ffa8e6 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -647,16 +647,6 @@ wint_t xmbrtowc(const char *str, size_t *i, size_t len, mbstate_t *mb) { } } -/** - * Work around https://github.com/llvm/llvm-project/issues/65532 by forcing a - * function, not a macro, to be called. - */ -#if __FreeBSD__ && SANITIZE_MEMORY -# define BFS_INTERCEPT(fn) (fn) -#else -# define BFS_INTERCEPT(fn) fn -#endif - size_t xstrwidth(const char *str) { size_t len = strlen(str); size_t ret = 0; @@ -689,17 +679,17 @@ static unsigned char ctype_cache[UCHAR_MAX + 1]; /** Initialize the ctype cache. */ static void char_cache_init(void) { for (size_t c = 0; c <= UCHAR_MAX; ++c) { - if (BFS_INTERCEPT(isprint)(c)) { + if (xisprint(c)) { ctype_cache[c] |= IS_PRINT; } - if (BFS_INTERCEPT(isspace)(c)) { + if (xisspace(c)) { ctype_cache[c] |= IS_SPACE; } } } /** Check if a character is printable. */ -static bool xisprint(unsigned char c, enum wesc_flags flags) { +static bool wesc_isprint(unsigned char c, enum wesc_flags flags) { if (ctype_cache[c] & IS_PRINT) { return true; } @@ -715,21 +705,12 @@ static bool xisprint(unsigned char c, enum wesc_flags flags) { } /** Check if a wide character is printable. */ -static bool xiswprint(wchar_t c, enum wesc_flags flags) { -#if __FreeBSD__ && SANITIZE_MEMORY -// Work around https://github.com/llvm/llvm-project/issues/65532 -# define bfs_iswprint (iswprint) -# define bfs_iswspace (iswspace) -#else -# define bfs_iswprint iswprint -# define bfs_iswspace iswspace -#endif - - if (bfs_iswprint(c)) { +static bool wesc_iswprint(wchar_t c, enum wesc_flags flags) { + if (xiswprint(c)) { return true; } - if (!(flags & WESC_SHELL) && bfs_iswspace(c)) { + if (!(flags & WESC_SHELL) && xiswspace(c)) { return true; } @@ -753,18 +734,18 @@ static size_t printable_len(const char *str, size_t len, enum wesc_flags flags) } for (size_t j = 0; j < sizeof(word); ++i, ++j) { - if (!xisprint(str[i], flags)) { + if (!wesc_isprint(str[i], flags)) { return i; } } } for (; i < len; ++i) { - unsigned char c = str[i]; - if (!isascii(c)) { + char c = str[i]; + if (!xisascii(c)) { goto multibyte; } - if (!xisprint(c, flags)) { + if (!wesc_isprint(c, flags)) { return i; } } @@ -777,7 +758,7 @@ multibyte:; if (wc == WEOF) { break; } - if (!xiswprint(wc, flags)) { + if (!wesc_iswprint(wc, flags)) { break; } } @@ -825,7 +806,7 @@ static char *dollar_quote(char *dest, char *end, const char *str, size_t len, en wint_t wc = xmbrtowc(str, &i, len, &mb); if (wc != WEOF) { - safe = xiswprint(wc, flags); + safe = wesc_iswprint(wc, flags); } for (size_t j = start; safe && j < i; ++j) { diff --git a/src/bfstd.h b/src/bfstd.h index 6cb2d7b..58e504c 100644 --- a/src/bfstd.h +++ b/src/bfstd.h @@ -9,8 +9,39 @@ #define BFS_BFSTD_H #include "config.h" +#include "sanity.h" #include +#include + +/** + * Work around https://github.com/llvm/llvm-project/issues/65532 by forcing a + * function, not a macro, to be called. + */ +#if __FreeBSD__ && SANITIZE_MEMORY +# define BFS_INTERCEPT(fn) (fn) +#else +# define BFS_INTERCEPT(fn) fn +#endif + +/** + * Wrap isalpha()/isdigit()/etc. + */ +#define BFS_ISCTYPE(fn, c) BFS_INTERCEPT(fn)((unsigned char)(c)) + +#define xisalnum(c) BFS_ISCTYPE(isalnum, c) +#define xisalpha(c) BFS_ISCTYPE(isalpha, c) +#define xisascii(c) BFS_ISCTYPE(isascii, c) +#define xiscntrl(c) BFS_ISCTYPE(iscntrl, c) +#define xisdigit(c) BFS_ISCTYPE(isdigit, c) +#define xislower(c) BFS_ISCTYPE(islower, c) +#define xisgraph(c) BFS_ISCTYPE(isgraph, c) +#define xisprint(c) BFS_ISCTYPE(isprint, c) +#define xispunct(c) BFS_ISCTYPE(ispunct, c) +#define xisspace(c) BFS_ISCTYPE(isspace, c) +#define xisupper(c) BFS_ISCTYPE(isupper, c) +#define xisxdigit(c) BFS_ISCTYPE(isxdigit, c) + // #include /** @@ -359,6 +390,25 @@ wint_t xmbrtowc(const char *str, size_t *i, size_t len, mbstate_t *mb); */ size_t xstrwidth(const char *str); +#include + +/** + * Wrap iswalpha()/iswdigit()/etc. + */ +#define BFS_ISWCTYPE(fn, c) BFS_INTERCEPT(fn)(c) + +#define xiswalnum(c) BFS_ISWCTYPE(iswalnum, c) +#define xiswalpha(c) BFS_ISWCTYPE(iswalpha, c) +#define xiswcntrl(c) BFS_ISWCTYPE(iswcntrl, c) +#define xiswdigit(c) BFS_ISWCTYPE(iswdigit, c) +#define xiswlower(c) BFS_ISWCTYPE(iswlower, c) +#define xiswgraph(c) BFS_ISWCTYPE(iswgraph, c) +#define xiswprint(c) BFS_ISWCTYPE(iswprint, c) +#define xiswpunct(c) BFS_ISWCTYPE(iswpunct, c) +#define xiswspace(c) BFS_ISWCTYPE(iswspace, c) +#define xiswupper(c) BFS_ISWCTYPE(iswupper, c) +#define xiswxdigit(c) BFS_ISWCTYPE(iswxdigit, c) + // #include /** -- cgit v1.2.3