1 /* $OpenBSD: chartype.h,v 1.4 2011/07/07 16:15:47 nicm Exp $ */ 2 /* $NetBSD: chartype.h,v 1.5 2010/04/15 00:55:57 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 2009 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the NetBSD 19 * Foundation, Inc. and its contributors. 20 * 4. Neither the name of The NetBSD Foundation nor the names of its 21 * contributors may be used to endorse or promote products derived 22 * from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #ifndef _h_chartype_f 38 #define _h_chartype_f 39 40 41 42 #ifdef WIDECHAR 43 44 /* Ideally we should also test the value of the define to see if it 45 * supports non-BMP code points without requiring UTF-16, but nothing 46 * seems to actually advertise this properly, despite Unicode 3.1 having 47 * been around since 2001... */ 48 #if !defined(__NetBSD__) && !defined(__OpenBSD__) 49 #ifndef __STDC_ISO_10646__ 50 /* In many places it is assumed that the first 127 code points are ASCII 51 * compatible, so ensure wchar_t indeed does ISO 10646 and not some other 52 * funky encoding that could break us in weird and wonderful ways. */ 53 #error wchar_t must store ISO 10646 characters 54 #endif 55 #endif 56 57 /* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it... 58 * ref: ISO/IEC DTR 19769 59 */ 60 #if WCHAR_MAX < INT32_MAX 61 #warning Build environment does not support non-BMP characters 62 #endif 63 64 #define ct_mbtowc mbtowc 65 #define ct_mbtowc_reset mbtowc(0,0,0) 66 #define ct_wctomb wctomb 67 #define ct_wctomb_reset wctomb(0,0) 68 #define ct_wcstombs wcstombs 69 #define ct_mbstowcs mbstowcs 70 71 #define Char wchar_t 72 #define Int wint_t 73 #define FUN(prefix,rest) prefix ## _w ## rest 74 #define FUNW(type) type ## _w 75 #define TYPE(type) type ## W 76 #define FSTR "%ls" 77 #define STR(x) L ## x 78 #define UC(c) c 79 #define Isalpha(x) iswalpha(x) 80 #define Isalnum(x) iswalnum(x) 81 #define Isgraph(x) iswgraph(x) 82 #define Isspace(x) iswspace(x) 83 #define Isdigit(x) iswdigit(x) 84 #define Iscntrl(x) iswcntrl(x) 85 #define Isprint(x) iswprint(x) 86 87 #define Isupper(x) iswupper(x) 88 #define Islower(x) iswlower(x) 89 #define Toupper(x) towupper(x) 90 #define Tolower(x) towlower(x) 91 92 #define IsASCII(x) (x < 0x100) 93 94 #define Strlen(x) wcslen(x) 95 #define Strchr(s,c) wcschr(s,c) 96 #define Strrchr(s,c) wcsrchr(s,c) 97 #define Strstr(s,v) wcsstr(s,v) 98 #define Strdup(x) wcsdup(x) 99 /* #define Strcpy(d,s) wcscpy(d,s) */ 100 #define Strncpy(d,s,n) wcsncpy(d,s,n) 101 #define Strncat(d,s,n) wcsncat(d,s,n) 102 103 #define Strcmp(s,v) wcscmp(s,v) 104 #define Strncmp(s,v,n) wcsncmp(s,v,n) 105 #define Strcspn(s,r) wcscspn(s,r) 106 107 #define Strtol(p,e,b) wcstol(p,e,b) 108 109 #define Width(c) (wcwidth(c) == -1 ? 0 : wcwidth(c)) 110 111 #else /* NARROW */ 112 113 #define ct_mbtowc error 114 #define ct_mbtowc_reset 115 #define ct_wctomb error 116 #define ct_wctomb_reset 117 #define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a)) 118 #define ct_mbstowcs(a, b, c) (strncpy(a, b, c), strlen(a)) 119 120 #define Char char 121 #define Int int 122 #define FUN(prefix,rest) prefix ## _ ## rest 123 #define FUNW(type) type 124 #define TYPE(type) type 125 #define FSTR "%s" 126 #define STR(x) x 127 #define UC(c) (unsigned char)(c) 128 129 #define Isalpha(x) isalpha((unsigned char)x) 130 #define Isalnum(x) isalnum((unsigned char)x) 131 #define Isgraph(x) isgraph((unsigned char)x) 132 #define Isspace(x) isspace((unsigned char)x) 133 #define Isdigit(x) isdigit((unsigned char)x) 134 #define Iscntrl(x) iscntrl((unsigned char)x) 135 #define Isprint(x) isprint((unsigned char)x) 136 137 #define Isupper(x) isupper((unsigned char)x) 138 #define Islower(x) islower((unsigned char)x) 139 #define Toupper(x) toupper((unsigned char)x) 140 #define Tolower(x) tolower((unsigned char)x) 141 142 #define IsASCII(x) isascii((unsigned char)x) 143 144 #define Strlen(x) strlen(x) 145 #define Strchr(s,c) strchr(s,c) 146 #define Strrchr(s,c) strrchr(s,c) 147 #define Strstr(s,v) strstr(s,v) 148 #define Strdup(x) strdup(x) 149 /* #define Strcpy(d,s) strcpy(d,s) */ 150 #define Strncpy(d,s,n) strncpy(d,s,n) 151 #define Strncat(d,s,n) strncat(d,s,n) 152 153 #define Strcmp(s,v) strcmp(s,v) 154 #define Strncmp(s,v,n) strncmp(s,v,n) 155 #define Strcspn(s,r) strcspn(s,r) 156 157 #define Strtol(p,e,b) strtol(p,e,b) 158 159 #define Width(c) 1 160 161 #endif 162 163 164 #ifdef WIDECHAR 165 /* 166 * Conversion buffer 167 */ 168 typedef struct ct_buffer_t { 169 char *cbuff; 170 size_t csize; 171 Char *wbuff; 172 size_t wsize; 173 } ct_buffer_t; 174 175 #define ct_encode_string __ct_encode_string 176 /* Encode a wide-character string and return the UTF-8 encoded result. */ 177 public char *ct_encode_string(const Char *, ct_buffer_t *); 178 179 #define ct_decode_string __ct_decode_string 180 /* Decode a (multi)?byte string and return the wide-character string result. */ 181 public Char *ct_decode_string(const char *, ct_buffer_t *); 182 183 /* Decode a (multi)?byte argv string array. 184 * The pointer returned must be free()d when done. */ 185 protected Char **ct_decode_argv(int, const char *[], ct_buffer_t *); 186 187 /* Resizes the conversion buffer(s) if needed. */ 188 protected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t); 189 protected ssize_t ct_encode_char(char *, size_t, Char); 190 protected size_t ct_enc_width(Char); 191 192 #define ct_free_argv(s) el_free(s) 193 194 #else 195 #define ct_encode_string(s, b) (s) 196 #define ct_decode_string(s, b) (s) 197 #define ct_decode_argv(l, s, b) (s) 198 #define ct_conv_buff_resize(b, os, ns) 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 sufficent 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 unwieldly visual representation, 209 * in this case \U+nnnnn. */ 210 #define VISUAL_WIDTH_MAX 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. */ 219 protected 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. */ 224 protected 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. */ 228 protected 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 */ 242 protected int ct_chr_class(Char c); 243 #endif 244 245 246 #endif /* _chartype_f */ 247