1296435Spfg/* $NetBSD: chartype.h,v 1.15 2015/05/17 13:14:41 christos Exp $ */ 2220220Sobrien 3220220Sobrien/*- 4220220Sobrien * Copyright (c) 2009 The NetBSD Foundation, Inc. 5220220Sobrien * All rights reserved. 6220220Sobrien * 7220220Sobrien * Redistribution and use in source and binary forms, with or without 8220220Sobrien * modification, are permitted provided that the following conditions 9220220Sobrien * are met: 10220220Sobrien * 1. Redistributions of source code must retain the above copyright 11220220Sobrien * notice, this list of conditions and the following disclaimer. 12220220Sobrien * 2. Redistributions in binary form must reproduce the above copyright 13220220Sobrien * notice, this list of conditions and the following disclaimer in the 14220220Sobrien * documentation and/or other materials provided with the distribution. 15220220Sobrien * 16220220Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17220220Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18220220Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19220220Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20220220Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21220220Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22220220Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23220220Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24220220Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25220220Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26220220Sobrien * POSSIBILITY OF SUCH DAMAGE. 27220624Sobrien * 28220624Sobrien * $FreeBSD: releng/11.0/lib/libedit/chartype.h 298896 2016-05-01 19:37:33Z pfg $ 29220220Sobrien */ 30220220Sobrien 31220220Sobrien#ifndef _h_chartype_f 32220220Sobrien#define _h_chartype_f 33220220Sobrien 34220220Sobrien 35296435Spfg 36220220Sobrien#ifdef WIDECHAR 37220220Sobrien 38220220Sobrien/* Ideally we should also test the value of the define to see if it 39220220Sobrien * supports non-BMP code points without requiring UTF-16, but nothing 40220220Sobrien * seems to actually advertise this properly, despite Unicode 3.1 having 41220220Sobrien * been around since 2001... */ 42283084Sbapt#if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__)) && !defined(__OpenBSD__) && !defined(__FreeBSD__) 43220220Sobrien#ifndef __STDC_ISO_10646__ 44220220Sobrien/* In many places it is assumed that the first 127 code points are ASCII 45220220Sobrien * compatible, so ensure wchar_t indeed does ISO 10646 and not some other 46220220Sobrien * funky encoding that could break us in weird and wonderful ways. */ 47220220Sobrien #error wchar_t must store ISO 10646 characters 48220220Sobrien#endif 49220220Sobrien#endif 50220220Sobrien 51220220Sobrien/* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it... 52220220Sobrien * ref: ISO/IEC DTR 19769 53220220Sobrien */ 54220220Sobrien#if WCHAR_MAX < INT32_MAX 55220220Sobrien#warning Build environment does not support non-BMP characters 56220220Sobrien#endif 57220220Sobrien 58296435Spfg#define ct_mbtowc mbtowc 59296435Spfg#define ct_mbtowc_reset mbtowc(0,0,(size_t)0) 60220220Sobrien#define ct_wctomb wctomb 61220220Sobrien#define ct_wctomb_reset wctomb(0,0) 62220220Sobrien#define ct_wcstombs wcstombs 63220220Sobrien#define ct_mbstowcs mbstowcs 64220220Sobrien 65220220Sobrien#define Char wchar_t 66296435Spfg#define Int wint_t 67220220Sobrien#define FUN(prefix,rest) prefix ## _w ## rest 68220220Sobrien#define FUNW(type) type ## _w 69220220Sobrien#define TYPE(type) type ## W 70296435Spfg#define FCHAR "%lc" 71220220Sobrien#define FSTR "%ls" 72296435Spfg#define STR(x) L ## x 73220220Sobrien#define UC(c) c 74220220Sobrien#define Isalpha(x) iswalpha(x) 75220220Sobrien#define Isalnum(x) iswalnum(x) 76220220Sobrien#define Isgraph(x) iswgraph(x) 77220220Sobrien#define Isspace(x) iswspace(x) 78220220Sobrien#define Isdigit(x) iswdigit(x) 79220220Sobrien#define Iscntrl(x) iswcntrl(x) 80220220Sobrien#define Isprint(x) iswprint(x) 81220220Sobrien 82220220Sobrien#define Isupper(x) iswupper(x) 83220220Sobrien#define Islower(x) iswlower(x) 84220220Sobrien#define Toupper(x) towupper(x) 85220220Sobrien#define Tolower(x) towlower(x) 86220220Sobrien 87220220Sobrien#define IsASCII(x) (x < 0x100) 88220220Sobrien 89220220Sobrien#define Strlen(x) wcslen(x) 90220220Sobrien#define Strchr(s,c) wcschr(s,c) 91220220Sobrien#define Strrchr(s,c) wcsrchr(s,c) 92220220Sobrien#define Strstr(s,v) wcsstr(s,v) 93220220Sobrien#define Strdup(x) wcsdup(x) 94220220Sobrien#define Strcpy(d,s) wcscpy(d,s) 95220220Sobrien#define Strncpy(d,s,n) wcsncpy(d,s,n) 96220220Sobrien#define Strncat(d,s,n) wcsncat(d,s,n) 97220220Sobrien 98220220Sobrien#define Strcmp(s,v) wcscmp(s,v) 99220220Sobrien#define Strncmp(s,v,n) wcsncmp(s,v,n) 100220220Sobrien#define Strcspn(s,r) wcscspn(s,r) 101220220Sobrien 102220220Sobrien#define Strtol(p,e,b) wcstol(p,e,b) 103220220Sobrien 104276881Sbaptstatic inline int 105276881SbaptWidth(wchar_t c) 106276881Sbapt{ 107276881Sbapt int w = wcwidth(c); 108276881Sbapt return w < 0 ? 0 : w; 109276881Sbapt} 110220220Sobrien 111220220Sobrien#else /* NARROW */ 112220220Sobrien 113296435Spfg#define ct_mbtowc error 114296435Spfg#define ct_mbtowc_reset 115220220Sobrien#define ct_wctomb error 116296435Spfg#define ct_wctomb_reset 117220220Sobrien#define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a)) 118220220Sobrien#define ct_mbstowcs(a, b, c) (strncpy(a, b, c), strlen(a)) 119220220Sobrien 120220220Sobrien#define Char char 121296435Spfg#define Int int 122220220Sobrien#define FUN(prefix,rest) prefix ## _ ## rest 123220220Sobrien#define FUNW(type) type 124220220Sobrien#define TYPE(type) type 125296435Spfg#define FCHAR "%c" 126220220Sobrien#define FSTR "%s" 127296435Spfg#define STR(x) x 128220220Sobrien#define UC(c) (unsigned char)(c) 129220220Sobrien 130220220Sobrien#define Isalpha(x) isalpha((unsigned char)x) 131220220Sobrien#define Isalnum(x) isalnum((unsigned char)x) 132220220Sobrien#define Isgraph(x) isgraph((unsigned char)x) 133220220Sobrien#define Isspace(x) isspace((unsigned char)x) 134220220Sobrien#define Isdigit(x) isdigit((unsigned char)x) 135220220Sobrien#define Iscntrl(x) iscntrl((unsigned char)x) 136220220Sobrien#define Isprint(x) isprint((unsigned char)x) 137220220Sobrien 138220220Sobrien#define Isupper(x) isupper((unsigned char)x) 139220220Sobrien#define Islower(x) islower((unsigned char)x) 140220220Sobrien#define Toupper(x) toupper((unsigned char)x) 141220220Sobrien#define Tolower(x) tolower((unsigned char)x) 142220220Sobrien 143220220Sobrien#define IsASCII(x) isascii((unsigned char)x) 144220220Sobrien 145220220Sobrien#define Strlen(x) strlen(x) 146220220Sobrien#define Strchr(s,c) strchr(s,c) 147220220Sobrien#define Strrchr(s,c) strrchr(s,c) 148220220Sobrien#define Strstr(s,v) strstr(s,v) 149220220Sobrien#define Strdup(x) strdup(x) 150220220Sobrien#define Strcpy(d,s) strcpy(d,s) 151220220Sobrien#define Strncpy(d,s,n) strncpy(d,s,n) 152220220Sobrien#define Strncat(d,s,n) strncat(d,s,n) 153220220Sobrien 154220220Sobrien#define Strcmp(s,v) strcmp(s,v) 155220220Sobrien#define Strncmp(s,v,n) strncmp(s,v,n) 156220220Sobrien#define Strcspn(s,r) strcspn(s,r) 157220220Sobrien 158220220Sobrien#define Strtol(p,e,b) strtol(p,e,b) 159220220Sobrien 160220220Sobrien#define Width(c) 1 161220220Sobrien 162220220Sobrien#endif 163220220Sobrien 164220220Sobrien 165220220Sobrien#ifdef WIDECHAR 166220220Sobrien/* 167220220Sobrien * Conversion buffer 168220220Sobrien */ 169220220Sobrientypedef struct ct_buffer_t { 170220220Sobrien char *cbuff; 171220220Sobrien size_t csize; 172220220Sobrien Char *wbuff; 173220220Sobrien size_t wsize; 174220220Sobrien} ct_buffer_t; 175220220Sobrien 176220220Sobrien#define ct_encode_string __ct_encode_string 177276881Sbapt/* Encode a wide-character string and return the UTF-8 encoded result. */ 178220220Sobrienpublic char *ct_encode_string(const Char *, ct_buffer_t *); 179220220Sobrien 180220220Sobrien#define ct_decode_string __ct_decode_string 181276881Sbapt/* Decode a (multi)?byte string and return the wide-character string result. */ 182220220Sobrienpublic Char *ct_decode_string(const char *, ct_buffer_t *); 183220220Sobrien 184220220Sobrien/* Decode a (multi)?byte argv string array. 185220220Sobrien * The pointer returned must be free()d when done. */ 186220220Sobrienprotected Char **ct_decode_argv(int, const char *[], ct_buffer_t *); 187220220Sobrien 188220220Sobrien/* Resizes the conversion buffer(s) if needed. */ 189283084Sbaptprotected int ct_conv_cbuff_resize(ct_buffer_t *, size_t); 190283084Sbaptprotected int ct_conv_wbuff_resize(ct_buffer_t *, size_t); 191220220Sobrienprotected ssize_t ct_encode_char(char *, size_t, Char); 192220220Sobrienprotected size_t ct_enc_width(Char); 193220220Sobrien 194220220Sobrien#define ct_free_argv(s) el_free(s) 195220220Sobrien 196220220Sobrien#else 197220220Sobrien#define ct_encode_string(s, b) (s) 198220220Sobrien#define ct_decode_string(s, b) (s) 199220220Sobrien#define ct_decode_argv(l, s, b) (s) 200283084Sbapt#define ct_conv_cbuff_resize(b, s) ((s) == (0)) 201283084Sbapt#define ct_conv_wbuff_resize(b, s) ((s) == (0)) 202220220Sobrien#define ct_encode_char(d, l, s) (*d = s, 1) 203220220Sobrien#define ct_free_argv(s) 204220220Sobrien#endif 205220220Sobrien 206220220Sobrien#ifndef NARROWCHAR 207298896Spfg/* Encode a characted into the destination buffer, provided there is sufficient 208220220Sobrien * buffer space available. Returns the number of bytes used up (zero if the 209220220Sobrien * character cannot be encoded, -1 if there was not enough space available). */ 210220220Sobrien 211298896Spfg/* The maximum buffer size to hold the most unwieldy visual representation, 212220220Sobrien * in this case \U+nnnnn. */ 213276881Sbapt#define VISUAL_WIDTH_MAX ((size_t)8) 214220220Sobrien 215220220Sobrien/* The terminal is thought of in terms of X columns by Y lines. In the cases 216296435Spfg * where a wide character takes up more than one column, the adjacent 217220220Sobrien * occupied column entries will contain this faux character. */ 218220220Sobrien#define MB_FILL_CHAR ((Char)-1) 219220220Sobrien 220220220Sobrien/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn 221220220Sobrien * style visual expansions. */ 222220220Sobrienprotected int ct_visual_width(Char); 223220220Sobrien 224220220Sobrien/* Turn the given character into the appropriate visual format, matching 225220220Sobrien * the width given by ct_visual_width(). Returns the number of characters used 226220220Sobrien * up, or -1 if insufficient space. Buffer length is in count of Char's. */ 227220220Sobrienprotected ssize_t ct_visual_char(Char *, size_t, Char); 228220220Sobrien 229220220Sobrien/* Convert the given string into visual format, using the ct_visual_char() 230220220Sobrien * function. Uses a static buffer, so not threadsafe. */ 231220220Sobrienprotected const Char *ct_visual_string(const Char *); 232220220Sobrien 233220220Sobrien 234220220Sobrien/* printable character, use ct_visual_width() to find out display width */ 235220220Sobrien#define CHTYPE_PRINT ( 0) 236220220Sobrien/* control character found inside the ASCII portion of the charset */ 237220220Sobrien#define CHTYPE_ASCIICTL (-1) 238220220Sobrien/* a \t */ 239220220Sobrien#define CHTYPE_TAB (-2) 240220220Sobrien/* a \n */ 241220220Sobrien#define CHTYPE_NL (-3) 242220220Sobrien/* non-printable character */ 243220220Sobrien#define CHTYPE_NONPRINT (-4) 244220220Sobrien/* classification of character c, as one of the above defines */ 245220220Sobrienprotected int ct_chr_class(Char c); 246220220Sobrien#endif 247220220Sobrien 248220220Sobrien 249220220Sobrien#endif /* _chartype_f */ 250