1220220Sobrien/* $NetBSD: chartype.h,v 1.6 2010/04/20 02:01:13 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$ 29220220Sobrien */ 30220220Sobrien 31220220Sobrien#ifndef _h_chartype_f 32220220Sobrien#define _h_chartype_f 33220220Sobrien 34220220Sobrien 35220220Sobrien 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... */ 42220220Sobrien#if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__)) 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 58220220Sobrien#define ct_mbtowc mbtowc 59220220Sobrien#define ct_mbtowc_reset mbtowc(0,0,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 66220220Sobrien#define Int wint_t 67220220Sobrien#define FUN(prefix,rest) prefix ## _w ## rest 68220220Sobrien#define FUNW(type) type ## _w 69220220Sobrien#define TYPE(type) type ## W 70220220Sobrien#define FSTR "%ls" 71220220Sobrien#define STR(x) L ## x 72220220Sobrien#define UC(c) c 73220220Sobrien#define Isalpha(x) iswalpha(x) 74220220Sobrien#define Isalnum(x) iswalnum(x) 75220220Sobrien#define Isgraph(x) iswgraph(x) 76220220Sobrien#define Isspace(x) iswspace(x) 77220220Sobrien#define Isdigit(x) iswdigit(x) 78220220Sobrien#define Iscntrl(x) iswcntrl(x) 79220220Sobrien#define Isprint(x) iswprint(x) 80220220Sobrien 81220220Sobrien#define Isupper(x) iswupper(x) 82220220Sobrien#define Islower(x) iswlower(x) 83220220Sobrien#define Toupper(x) towupper(x) 84220220Sobrien#define Tolower(x) towlower(x) 85220220Sobrien 86220220Sobrien#define IsASCII(x) (x < 0x100) 87220220Sobrien 88220220Sobrien#define Strlen(x) wcslen(x) 89220220Sobrien#define Strchr(s,c) wcschr(s,c) 90220220Sobrien#define Strrchr(s,c) wcsrchr(s,c) 91220220Sobrien#define Strstr(s,v) wcsstr(s,v) 92220220Sobrien#define Strdup(x) wcsdup(x) 93220220Sobrien#define Strcpy(d,s) wcscpy(d,s) 94220220Sobrien#define Strncpy(d,s,n) wcsncpy(d,s,n) 95220220Sobrien#define Strncat(d,s,n) wcsncat(d,s,n) 96220220Sobrien 97220220Sobrien#define Strcmp(s,v) wcscmp(s,v) 98220220Sobrien#define Strncmp(s,v,n) wcsncmp(s,v,n) 99220220Sobrien#define Strcspn(s,r) wcscspn(s,r) 100220220Sobrien 101220220Sobrien#define Strtol(p,e,b) wcstol(p,e,b) 102220220Sobrien 103220220Sobrien#define Width(c) wcwidth(c) 104220220Sobrien 105220220Sobrien#else /* NARROW */ 106220220Sobrien 107220220Sobrien#define ct_mbtowc error 108220220Sobrien#define ct_mbtowc_reset 109220220Sobrien#define ct_wctomb error 110220220Sobrien#define ct_wctomb_reset 111220220Sobrien#define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a)) 112220220Sobrien#define ct_mbstowcs(a, b, c) (strncpy(a, b, c), strlen(a)) 113220220Sobrien 114220220Sobrien#define Char char 115220220Sobrien#define Int int 116220220Sobrien#define FUN(prefix,rest) prefix ## _ ## rest 117220220Sobrien#define FUNW(type) type 118220220Sobrien#define TYPE(type) type 119220220Sobrien#define FSTR "%s" 120220220Sobrien#define STR(x) x 121220220Sobrien#define UC(c) (unsigned char)(c) 122220220Sobrien 123220220Sobrien#define Isalpha(x) isalpha((unsigned char)x) 124220220Sobrien#define Isalnum(x) isalnum((unsigned char)x) 125220220Sobrien#define Isgraph(x) isgraph((unsigned char)x) 126220220Sobrien#define Isspace(x) isspace((unsigned char)x) 127220220Sobrien#define Isdigit(x) isdigit((unsigned char)x) 128220220Sobrien#define Iscntrl(x) iscntrl((unsigned char)x) 129220220Sobrien#define Isprint(x) isprint((unsigned char)x) 130220220Sobrien 131220220Sobrien#define Isupper(x) isupper((unsigned char)x) 132220220Sobrien#define Islower(x) islower((unsigned char)x) 133220220Sobrien#define Toupper(x) toupper((unsigned char)x) 134220220Sobrien#define Tolower(x) tolower((unsigned char)x) 135220220Sobrien 136220220Sobrien#define IsASCII(x) isascii((unsigned char)x) 137220220Sobrien 138220220Sobrien#define Strlen(x) strlen(x) 139220220Sobrien#define Strchr(s,c) strchr(s,c) 140220220Sobrien#define Strrchr(s,c) strrchr(s,c) 141220220Sobrien#define Strstr(s,v) strstr(s,v) 142220220Sobrien#define Strdup(x) strdup(x) 143220220Sobrien#define Strcpy(d,s) strcpy(d,s) 144220220Sobrien#define Strncpy(d,s,n) strncpy(d,s,n) 145220220Sobrien#define Strncat(d,s,n) strncat(d,s,n) 146220220Sobrien 147220220Sobrien#define Strcmp(s,v) strcmp(s,v) 148220220Sobrien#define Strncmp(s,v,n) strncmp(s,v,n) 149220220Sobrien#define Strcspn(s,r) strcspn(s,r) 150220220Sobrien 151220220Sobrien#define Strtol(p,e,b) strtol(p,e,b) 152220220Sobrien 153220220Sobrien#define Width(c) 1 154220220Sobrien 155220220Sobrien#endif 156220220Sobrien 157220220Sobrien 158220220Sobrien#ifdef WIDECHAR 159220220Sobrien/* 160220220Sobrien * Conversion buffer 161220220Sobrien */ 162220220Sobrientypedef struct ct_buffer_t { 163220220Sobrien char *cbuff; 164220220Sobrien size_t csize; 165220220Sobrien Char *wbuff; 166220220Sobrien size_t wsize; 167220220Sobrien} ct_buffer_t; 168220220Sobrien 169220220Sobrien#define ct_encode_string __ct_encode_string 170220220Sobrien/* Encode a wide character string and return the UTF-8 encoded result. */ 171220220Sobrienpublic char *ct_encode_string(const Char *, ct_buffer_t *); 172220220Sobrien 173220220Sobrien#define ct_decode_string __ct_decode_string 174220220Sobrien/* Decode a (multi)?byte string and return the wide character string result. */ 175220220Sobrienpublic Char *ct_decode_string(const char *, ct_buffer_t *); 176220220Sobrien 177220220Sobrien/* Decode a (multi)?byte argv string array. 178220220Sobrien * The pointer returned must be free()d when done. */ 179220220Sobrienprotected Char **ct_decode_argv(int, const char *[], ct_buffer_t *); 180220220Sobrien 181220220Sobrien/* Resizes the conversion buffer(s) if needed. */ 182220220Sobrienprotected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t); 183220220Sobrienprotected ssize_t ct_encode_char(char *, size_t, Char); 184220220Sobrienprotected size_t ct_enc_width(Char); 185220220Sobrien 186220220Sobrien#define ct_free_argv(s) el_free(s) 187220220Sobrien 188220220Sobrien#else 189220220Sobrien#define ct_encode_string(s, b) (s) 190220220Sobrien#define ct_decode_string(s, b) (s) 191220220Sobrien#define ct_decode_argv(l, s, b) (s) 192220220Sobrien#define ct_conv_buff_resize(b, os, ns) 193220220Sobrien#define ct_encode_char(d, l, s) (*d = s, 1) 194220220Sobrien#define ct_free_argv(s) 195220220Sobrien#endif 196220220Sobrien 197220220Sobrien#ifndef NARROWCHAR 198220220Sobrien/* Encode a characted into the destination buffer, provided there is sufficent 199220220Sobrien * buffer space available. Returns the number of bytes used up (zero if the 200220220Sobrien * character cannot be encoded, -1 if there was not enough space available). */ 201220220Sobrien 202220220Sobrien/* The maximum buffer size to hold the most unwieldly visual representation, 203220220Sobrien * in this case \U+nnnnn. */ 204220220Sobrien#define VISUAL_WIDTH_MAX 8 205220220Sobrien 206220220Sobrien/* The terminal is thought of in terms of X columns by Y lines. In the cases 207220220Sobrien * where a wide character takes up more than one column, the adjacent 208220220Sobrien * occupied column entries will contain this faux character. */ 209220220Sobrien#define MB_FILL_CHAR ((Char)-1) 210220220Sobrien 211220220Sobrien/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn 212220220Sobrien * style visual expansions. */ 213220220Sobrienprotected int ct_visual_width(Char); 214220220Sobrien 215220220Sobrien/* Turn the given character into the appropriate visual format, matching 216220220Sobrien * the width given by ct_visual_width(). Returns the number of characters used 217220220Sobrien * up, or -1 if insufficient space. Buffer length is in count of Char's. */ 218220220Sobrienprotected ssize_t ct_visual_char(Char *, size_t, Char); 219220220Sobrien 220220220Sobrien/* Convert the given string into visual format, using the ct_visual_char() 221220220Sobrien * function. Uses a static buffer, so not threadsafe. */ 222220220Sobrienprotected const Char *ct_visual_string(const Char *); 223220220Sobrien 224220220Sobrien 225220220Sobrien/* printable character, use ct_visual_width() to find out display width */ 226220220Sobrien#define CHTYPE_PRINT ( 0) 227220220Sobrien/* control character found inside the ASCII portion of the charset */ 228220220Sobrien#define CHTYPE_ASCIICTL (-1) 229220220Sobrien/* a \t */ 230220220Sobrien#define CHTYPE_TAB (-2) 231220220Sobrien/* a \n */ 232220220Sobrien#define CHTYPE_NL (-3) 233220220Sobrien/* non-printable character */ 234220220Sobrien#define CHTYPE_NONPRINT (-4) 235220220Sobrien/* classification of character c, as one of the above defines */ 236220220Sobrienprotected int ct_chr_class(Char c); 237220220Sobrien#endif 238220220Sobrien 239220220Sobrien 240220220Sobrien#endif /* _chartype_f */ 241