chartype.h revision 343091
1/* $NetBSD: chartype.h,v 1.25 2016/03/07 00:05:20 christos Exp $ */ 2 3/*- 4 * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD: stable/11/lib/libedit/chartype.h 343091 2019-01-16 12:04:29Z bapt $ 29 */ 30 31#ifndef _h_chartype_f 32#define _h_chartype_f 33 34 35#ifdef WIDECHAR 36 37/* Ideally we should also test the value of the define to see if it 38 * supports non-BMP code points without requiring UTF-16, but nothing 39 * seems to actually advertise this properly, despite Unicode 3.1 having 40 * been around since 2001... */ 41#if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__)) && !defined(__OpenBSD__) && !defined(__FreeBSD__) 42#ifndef __STDC_ISO_10646__ 43/* In many places it is assumed that the first 127 code points are ASCII 44 * compatible, so ensure wchar_t indeed does ISO 10646 and not some other 45 * funky encoding that could break us in weird and wonderful ways. */ 46 #error wchar_t must store ISO 10646 characters 47#endif 48#endif 49 50/* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it... 51 * ref: ISO/IEC DTR 19769 52 */ 53#if WCHAR_MAX < INT32_MAX 54#warning Build environment does not support non-BMP characters 55#endif 56 57#define ct_wctob wctob 58#define ct_wctomb wctomb 59#define ct_wcrtomb wcrtomb 60#define ct_wctomb_reset wctomb(0,0) 61#define ct_wcstombs wcstombs 62#define ct_mbstowcs mbstowcs 63 64#define Char wchar_t 65#define FUN(prefix,rest) prefix ## _w ## rest 66#define FUNW(type) type ## _w 67#define TYPE(type) type ## W 68#define FSTR "%ls" 69#define FSTARSTR "%.*ls" 70#define STR(x) L ## x 71#define UC(c) c 72#define Isalpha(x) iswalpha(x) 73#define Isalnum(x) iswalnum(x) 74#define Isgraph(x) iswgraph(x) 75#define Isspace(x) iswspace(x) 76#define Isdigit(x) iswdigit(x) 77#define Iscntrl(x) iswcntrl(x) 78#define Isprint(x) iswprint(x) 79 80#define Isupper(x) iswupper(x) 81#define Islower(x) iswlower(x) 82#define Toupper(x) towupper(x) 83#define Tolower(x) towlower(x) 84 85#define IsASCII(x) (x < 0x100) 86 87#define Strlen(x) wcslen(x) 88#define Strchr(s,c) wcschr(s,c) 89#define Strrchr(s,c) wcsrchr(s,c) 90#define Strstr(s,v) wcsstr(s,v) 91#define Strdup(x) wcsdup(x) 92#define Strcpy(d,s) wcscpy(d,s) 93#define Strncpy(d,s,n) wcsncpy(d,s,n) 94#define Strncat(d,s,n) wcsncat(d,s,n) 95 96#define Strcmp(s,v) wcscmp(s,v) 97#define Strncmp(s,v,n) wcsncmp(s,v,n) 98#define Strcspn(s,r) wcscspn(s,r) 99 100#define Strtol(p,e,b) wcstol(p,e,b) 101 102static inline int 103Width(wchar_t c) 104{ 105 int w = wcwidth(c); 106 return w < 0 ? 0 : w; 107} 108 109#else /* NARROW */ 110 111#define ct_wctob(w) ((int)(w)) 112#define ct_wctomb error 113#define ct_wcrtomb error 114#define ct_wctomb_reset 115#define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a)) 116#define ct_mbstowcs(a, b, c) (strncpy(a, b, c), strlen(a)) 117 118#define Char char 119#define FUN(prefix,rest) prefix ## _ ## rest 120#define FUNW(type) type 121#define TYPE(type) type 122#define FSTR "%s" 123#define FSTARSTR "%.*s" 124#define STR(x) x 125#define UC(c) (unsigned char)(c) 126 127#define Isalpha(x) isalpha((unsigned char)x) 128#define Isalnum(x) isalnum((unsigned char)x) 129#define Isgraph(x) isgraph((unsigned char)x) 130#define Isspace(x) isspace((unsigned char)x) 131#define Isdigit(x) isdigit((unsigned char)x) 132#define Iscntrl(x) iscntrl((unsigned char)x) 133#define Isprint(x) isprint((unsigned char)x) 134 135#define Isupper(x) isupper((unsigned char)x) 136#define Islower(x) islower((unsigned char)x) 137#define Toupper(x) toupper((unsigned char)x) 138#define Tolower(x) tolower((unsigned char)x) 139 140#define IsASCII(x) isascii((unsigned char)x) 141 142#define Strlen(x) strlen(x) 143#define Strchr(s,c) strchr(s,c) 144#define Strrchr(s,c) strrchr(s,c) 145#define Strstr(s,v) strstr(s,v) 146#define Strdup(x) strdup(x) 147#define Strcpy(d,s) strcpy(d,s) 148#define Strncpy(d,s,n) strncpy(d,s,n) 149#define Strncat(d,s,n) strncat(d,s,n) 150 151#define Strcmp(s,v) strcmp(s,v) 152#define Strncmp(s,v,n) strncmp(s,v,n) 153#define Strcspn(s,r) strcspn(s,r) 154 155#define Strtol(p,e,b) strtol(p,e,b) 156 157#define Width(c) 1 158 159#endif 160 161 162#ifdef WIDECHAR 163/* 164 * Conversion buffer 165 */ 166typedef struct ct_buffer_t { 167 char *cbuff; 168 size_t csize; 169 Char *wbuff; 170 size_t wsize; 171} ct_buffer_t; 172 173#define ct_encode_string __ct_encode_string 174/* Encode a wide-character string and return the UTF-8 encoded result. */ 175public char *ct_encode_string(const Char *, ct_buffer_t *); 176 177#define ct_decode_string __ct_decode_string 178/* Decode a (multi)?byte string and return the wide-character string result. */ 179public Char *ct_decode_string(const char *, ct_buffer_t *); 180 181/* Decode a (multi)?byte argv string array. 182 * The pointer returned must be free()d when done. */ 183protected Char **ct_decode_argv(int, const char *[], ct_buffer_t *); 184 185/* Resizes the conversion buffer(s) if needed. */ 186protected int ct_conv_cbuff_resize(ct_buffer_t *, size_t); 187protected int ct_conv_wbuff_resize(ct_buffer_t *, size_t); 188protected ssize_t ct_encode_char(char *, size_t, Char); 189protected size_t ct_enc_width(Char); 190 191#define ct_free_argv(s) el_free(s) 192 193#else 194#define ct_encode_string(s, b) (s) 195#define ct_decode_string(s, b) (s) 196#define ct_decode_argv(l, s, b) (s) 197#define ct_conv_cbuff_resize(b, s) ((s) == (0)) 198#define ct_conv_wbuff_resize(b, s) ((s) == (0)) 199#define ct_encode_char(d, l, s) (*d = s, 1) 200#define ct_free_argv(s) 201#endif 202 203#ifndef NARROWCHAR 204/* Encode a characted into the destination buffer, provided there is sufficient 205 * buffer space available. Returns the number of bytes used up (zero if the 206 * character cannot be encoded, -1 if there was not enough space available). */ 207 208/* The maximum buffer size to hold the most unwieldy visual representation, 209 * in this case \U+nnnnn. */ 210#define VISUAL_WIDTH_MAX ((size_t)8) 211 212/* The terminal is thought of in terms of X columns by Y lines. In the cases 213 * where a wide character takes up more than one column, the adjacent 214 * occupied column entries will contain this faux character. */ 215#define MB_FILL_CHAR ((Char)-1) 216 217/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn 218 * style visual expansions. */ 219protected int ct_visual_width(Char); 220 221/* Turn the given character into the appropriate visual format, matching 222 * the width given by ct_visual_width(). Returns the number of characters used 223 * up, or -1 if insufficient space. Buffer length is in count of Char's. */ 224protected ssize_t ct_visual_char(Char *, size_t, Char); 225 226/* Convert the given string into visual format, using the ct_visual_char() 227 * function. Uses a static buffer, so not threadsafe. */ 228protected const Char *ct_visual_string(const Char *); 229 230 231/* printable character, use ct_visual_width() to find out display width */ 232#define CHTYPE_PRINT ( 0) 233/* control character found inside the ASCII portion of the charset */ 234#define CHTYPE_ASCIICTL (-1) 235/* a \t */ 236#define CHTYPE_TAB (-2) 237/* a \n */ 238#define CHTYPE_NL (-3) 239/* non-printable character */ 240#define CHTYPE_NONPRINT (-4) 241/* classification of character c, as one of the above defines */ 242protected int ct_chr_class(Char c); 243#endif 244 245size_t ct_mbrtowc(wchar_t *, const char *, size_t); 246 247 248#endif /* _chartype_f */ 249