chartype.h revision 1.12
1/* $OpenBSD: chartype.h,v 1.12 2016/03/21 18:40:25 schwarze 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#ifndef _h_chartype_f 31#define _h_chartype_f 32 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(__OpenBSD__) 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_mbrtowc mbrtowc 58#define ct_wctob wctob 59#define ct_wctomb wctomb 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 STR(x) L ## x 70#define UC(c) c 71#define Isalpha(x) iswalpha(x) 72#define Isalnum(x) iswalnum(x) 73#define Isgraph(x) iswgraph(x) 74#define Isspace(x) iswspace(x) 75#define Isdigit(x) iswdigit(x) 76#define Iscntrl(x) iswcntrl(x) 77#define Isprint(x) iswprint(x) 78 79#define Isupper(x) iswupper(x) 80#define Islower(x) iswlower(x) 81#define Toupper(x) towupper(x) 82#define Tolower(x) towlower(x) 83 84#define IsASCII(x) (x < 0x100) 85 86#define Strlen(x) wcslen(x) 87#define Strchr(s,c) wcschr(s,c) 88#define Strrchr(s,c) wcsrchr(s,c) 89#define Strstr(s,v) wcsstr(s,v) 90#define Strdup(x) wcsdup(x) 91/* #define Strcpy(d,s) wcscpy(d,s) */ 92#define Strncpy(d,s,n) wcsncpy(d,s,n) 93#define Strncat(d,s,n) wcsncat(d,s,n) 94 95#define Strcmp(s,v) wcscmp(s,v) 96#define Strncmp(s,v,n) wcsncmp(s,v,n) 97#define Strcspn(s,r) wcscspn(s,r) 98 99#define Strtol(p,e,b) wcstol(p,e,b) 100 101#define Width(c) (wcwidth(c) == -1 ? 0 : wcwidth(c)) 102 103#else /* NARROW */ 104 105size_t ct_mbrtowc(wchar_t *, const char *, size_t, void *); 106#define ct_wctob(w) ((int)(w)) 107#define ct_wctomb error 108#define ct_wctomb_reset 109#define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a)) 110#define ct_mbstowcs(a, b, c) (strncpy(a, b, c), strlen(a)) 111 112#define Char char 113#define FUN(prefix,rest) prefix ## _ ## rest 114#define FUNW(type) type 115#define TYPE(type) type 116#define FSTR "%s" 117#define STR(x) x 118#define UC(c) (unsigned char)(c) 119 120#define Isalpha(x) isalpha((unsigned char)x) 121#define Isalnum(x) isalnum((unsigned char)x) 122#define Isgraph(x) isgraph((unsigned char)x) 123#define Isspace(x) isspace((unsigned char)x) 124#define Isdigit(x) isdigit((unsigned char)x) 125#define Iscntrl(x) iscntrl((unsigned char)x) 126#define Isprint(x) isprint((unsigned char)x) 127 128#define Isupper(x) isupper((unsigned char)x) 129#define Islower(x) islower((unsigned char)x) 130#define Toupper(x) toupper((unsigned char)x) 131#define Tolower(x) tolower((unsigned char)x) 132 133#define IsASCII(x) isascii((unsigned char)x) 134 135#define Strlen(x) strlen(x) 136#define Strchr(s,c) strchr(s,c) 137#define Strrchr(s,c) strrchr(s,c) 138#define Strstr(s,v) strstr(s,v) 139#define Strdup(x) strdup(x) 140/* #define Strcpy(d,s) strcpy(d,s) */ 141#define Strncpy(d,s,n) strncpy(d,s,n) 142#define Strncat(d,s,n) strncat(d,s,n) 143 144#define Strcmp(s,v) strcmp(s,v) 145#define Strncmp(s,v,n) strncmp(s,v,n) 146#define Strcspn(s,r) strcspn(s,r) 147 148#define Strtol(p,e,b) strtol(p,e,b) 149 150#define Width(c) 1 151 152#endif 153 154 155#ifdef WIDECHAR 156/* 157 * Conversion buffer 158 */ 159typedef struct ct_buffer_t { 160 char *cbuff; 161 size_t csize; 162 Char *wbuff; 163 size_t wsize; 164} ct_buffer_t; 165 166#define ct_encode_string __ct_encode_string 167/* Encode a wide-character string and return the UTF-8 encoded result. */ 168public char *ct_encode_string(const Char *, ct_buffer_t *); 169 170#define ct_decode_string __ct_decode_string 171/* Decode a (multi)?byte string and return the wide-character string result. */ 172public Char *ct_decode_string(const char *, ct_buffer_t *); 173 174/* Decode a (multi)?byte argv string array. 175 * The pointer returned must be free()d when done. */ 176protected Char **ct_decode_argv(int, const char *[], ct_buffer_t *); 177 178/* Resizes the conversion buffer(s) if needed. */ 179protected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t); 180protected ssize_t ct_encode_char(char *, size_t, Char); 181protected size_t ct_enc_width(Char); 182 183#define ct_free_argv(s) free(s) 184 185#else 186#define ct_encode_string(s, b) (s) 187#define ct_decode_string(s, b) (s) 188#define ct_decode_argv(l, s, b) (s) 189#define ct_conv_buff_resize(b, os, ns) 190#define ct_encode_char(d, l, s) (*d = s, 1) 191#define ct_free_argv(s) 192#endif 193 194#ifndef NARROWCHAR 195/* Encode a characted into the destination buffer, provided there is sufficent 196 * buffer space available. Returns the number of bytes used up (zero if the 197 * character cannot be encoded, -1 if there was not enough space available). */ 198 199/* The maximum buffer size to hold the most unwieldly visual representation, 200 * in this case \U+nnnnn. */ 201#define VISUAL_WIDTH_MAX 8 202 203/* The terminal is thought of in terms of X columns by Y lines. In the cases 204 * where a wide character takes up more than one column, the adjacent 205 * occupied column entries will contain this faux character. */ 206#define MB_FILL_CHAR ((Char)-1) 207 208/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn 209 * style visual expansions. */ 210protected int ct_visual_width(Char); 211 212/* Turn the given character into the appropriate visual format, matching 213 * the width given by ct_visual_width(). Returns the number of characters used 214 * up, or -1 if insufficient space. Buffer length is in count of Char's. */ 215protected ssize_t ct_visual_char(Char *, size_t, Char); 216 217/* Convert the given string into visual format, using the ct_visual_char() 218 * function. Uses a static buffer, so not threadsafe. */ 219protected const Char *ct_visual_string(const Char *); 220 221 222/* printable character, use ct_visual_width() to find out display width */ 223#define CHTYPE_PRINT ( 0) 224/* control character found inside the ASCII portion of the charset */ 225#define CHTYPE_ASCIICTL (-1) 226/* a \t */ 227#define CHTYPE_TAB (-2) 228/* a \n */ 229#define CHTYPE_NL (-3) 230/* non-printable character */ 231#define CHTYPE_NONPRINT (-4) 232/* classification of character c, as one of the above defines */ 233protected int ct_chr_class(Char c); 234#endif 235 236 237#endif /* _chartype_f */ 238