chartype.h revision 1.15
1/* $OpenBSD: chartype.h,v 1.15 2016/04/09 20:15:26 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#ifndef NARROWCHAR 34 35/* Ideally we should also test the value of the define to see if it 36 * supports non-BMP code points without requiring UTF-16, but nothing 37 * seems to actually advertise this properly, despite Unicode 3.1 having 38 * been around since 2001... */ 39#if !defined(__NetBSD__) && !defined(__OpenBSD__) 40#ifndef __STDC_ISO_10646__ 41/* In many places it is assumed that the first 127 code points are ASCII 42 * compatible, so ensure wchar_t indeed does ISO 10646 and not some other 43 * funky encoding that could break us in weird and wonderful ways. */ 44 #error wchar_t must store ISO 10646 characters 45#endif 46#endif 47 48/* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it... 49 * ref: ISO/IEC DTR 19769 50 */ 51#if WCHAR_MAX < INT32_MAX 52#warning Build environment does not support non-BMP characters 53#endif 54 55#define Char wchar_t 56#define FUN(prefix,rest) prefix ## _w ## rest 57#define FUNW(type) type ## _w 58#define TYPE(type) type ## W 59#define STR(x) L ## x 60 61#define Strlen(x) wcslen(x) 62#define Strchr(s,c) wcschr(s,c) 63#define Strdup(x) wcsdup(x) 64#define Strncpy(d,s,n) wcsncpy(d,s,n) 65#define Strncat(d,s,n) wcsncat(d,s,n) 66#define Strcmp(s,v) wcscmp(s,v) 67#define Strncmp(s,v,n) wcsncmp(s,v,n) 68 69#else /* NARROW */ 70 71#define Char char 72#define FUN(prefix,rest) prefix ## _ ## rest 73#define FUNW(type) type 74#define TYPE(type) type 75#define STR(x) x 76 77#define Strlen(x) strlen(x) 78#define Strchr(s,c) strchr(s,c) 79#define Strdup(x) strdup(x) 80#define Strncpy(d,s,n) strncpy(d,s,n) 81#define Strncat(d,s,n) strncat(d,s,n) 82 83#define Strcmp(s,v) strcmp(s,v) 84#define Strncmp(s,v,n) strncmp(s,v,n) 85#endif 86 87 88#ifndef NARROWCHAR 89/* 90 * Conversion buffer 91 */ 92typedef struct ct_buffer_t { 93 char *cbuff; 94 size_t csize; 95 Char *wbuff; 96 size_t wsize; 97} ct_buffer_t; 98 99#define ct_encode_string __ct_encode_string 100/* Encode a wide-character string and return the UTF-8 encoded result. */ 101public char *ct_encode_string(const Char *, ct_buffer_t *); 102 103#define ct_decode_string __ct_decode_string 104/* Decode a (multi)?byte string and return the wide-character string result. */ 105public Char *ct_decode_string(const char *, ct_buffer_t *); 106 107/* Decode a (multi)?byte argv string array. 108 * The pointer returned must be free()d when done. */ 109protected Char **ct_decode_argv(int, const char *[], ct_buffer_t *); 110 111/* Resizes the conversion buffer(s) if needed. */ 112protected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t); 113protected ssize_t ct_encode_char(char *, size_t, Char); 114protected size_t ct_enc_width(Char); 115 116#else 117#define ct_encode_string(s, b) (s) 118#define ct_decode_string(s, b) (s) 119#endif 120 121#ifndef NARROWCHAR 122/* Encode a characted into the destination buffer, provided there is sufficent 123 * buffer space available. Returns the number of bytes used up (zero if the 124 * character cannot be encoded, -1 if there was not enough space available). */ 125 126/* The maximum buffer size to hold the most unwieldly visual representation, 127 * in this case \U+nnnnn. */ 128#define VISUAL_WIDTH_MAX 8 129 130/* The terminal is thought of in terms of X columns by Y lines. In the cases 131 * where a wide character takes up more than one column, the adjacent 132 * occupied column entries will contain this faux character. */ 133#define MB_FILL_CHAR ((Char)-1) 134 135/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn 136 * style visual expansions. */ 137protected int ct_visual_width(Char); 138 139/* Turn the given character into the appropriate visual format, matching 140 * the width given by ct_visual_width(). Returns the number of characters used 141 * up, or -1 if insufficient space. Buffer length is in count of Char's. */ 142protected ssize_t ct_visual_char(Char *, size_t, Char); 143 144/* Convert the given string into visual format, using the ct_visual_char() 145 * function. Uses a static buffer, so not threadsafe. */ 146protected const Char *ct_visual_string(const Char *); 147 148 149/* printable character, use ct_visual_width() to find out display width */ 150#define CHTYPE_PRINT ( 0) 151/* control character found inside the ASCII portion of the charset */ 152#define CHTYPE_ASCIICTL (-1) 153/* a \t */ 154#define CHTYPE_TAB (-2) 155/* a \n */ 156#define CHTYPE_NL (-3) 157/* non-printable character */ 158#define CHTYPE_NONPRINT (-4) 159/* classification of character c, as one of the above defines */ 160protected int ct_chr_class(Char c); 161#endif 162 163#endif /* _chartype_f */ 164