1 1.37 tnn /* $NetBSD: chartype.h,v 1.37 2022/04/11 19:37:20 tnn Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos 29 1.1 christos #ifndef _h_chartype_f 30 1.1 christos #define _h_chartype_f 31 1.1 christos 32 1.1 christos /* Ideally we should also test the value of the define to see if it 33 1.1 christos * supports non-BMP code points without requiring UTF-16, but nothing 34 1.1 christos * seems to actually advertise this properly, despite Unicode 3.1 having 35 1.1 christos * been around since 2001... */ 36 1.35 christos #if !defined(__NetBSD__) && \ 37 1.35 christos !defined(__sun) && \ 38 1.37 tnn !defined(__osf__) && \ 39 1.35 christos !(defined(__APPLE__) && defined(__MACH__)) && \ 40 1.35 christos !defined(__OpenBSD__) && \ 41 1.35 christos !defined(__FreeBSD__) && \ 42 1.35 christos !defined(__DragonFly__) 43 1.1 christos #ifndef __STDC_ISO_10646__ 44 1.1 christos /* In many places it is assumed that the first 127 code points are ASCII 45 1.1 christos * compatible, so ensure wchar_t indeed does ISO 10646 and not some other 46 1.1 christos * funky encoding that could break us in weird and wonderful ways. */ 47 1.1 christos #error wchar_t must store ISO 10646 characters 48 1.1 christos #endif 49 1.1 christos #endif 50 1.1 christos 51 1.1 christos /* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it... 52 1.1 christos * ref: ISO/IEC DTR 19769 53 1.1 christos */ 54 1.1 christos #if WCHAR_MAX < INT32_MAX 55 1.1 christos #warning Build environment does not support non-BMP characters 56 1.1 christos #endif 57 1.1 christos 58 1.1 christos /* 59 1.1 christos * Conversion buffer 60 1.1 christos */ 61 1.1 christos typedef struct ct_buffer_t { 62 1.1 christos char *cbuff; 63 1.1 christos size_t csize; 64 1.29 christos wchar_t *wbuff; 65 1.1 christos size_t wsize; 66 1.1 christos } ct_buffer_t; 67 1.1 christos 68 1.7 wiz /* Encode a wide-character string and return the UTF-8 encoded result. */ 69 1.31 christos char *ct_encode_string(const wchar_t *, ct_buffer_t *); 70 1.1 christos 71 1.7 wiz /* Decode a (multi)?byte string and return the wide-character string result. */ 72 1.31 christos wchar_t *ct_decode_string(const char *, ct_buffer_t *); 73 1.1 christos 74 1.1 christos /* Decode a (multi)?byte argv string array. 75 1.1 christos * The pointer returned must be free()d when done. */ 76 1.34 christos libedit_private wchar_t **ct_decode_argv(int, const char *[], ct_buffer_t *); 77 1.1 christos 78 1.32 christos /* Encode a character into the destination buffer, provided there is sufficient 79 1.1 christos * buffer space available. Returns the number of bytes used up (zero if the 80 1.1 christos * character cannot be encoded, -1 if there was not enough space available). */ 81 1.34 christos libedit_private ssize_t ct_encode_char(char *, size_t, wchar_t); 82 1.34 christos libedit_private size_t ct_enc_width(wchar_t); 83 1.1 christos 84 1.32 christos /* The maximum buffer size to hold the most unwieldy visual representation, 85 1.1 christos * in this case \U+nnnnn. */ 86 1.8 christos #define VISUAL_WIDTH_MAX ((size_t)8) 87 1.1 christos 88 1.1 christos /* The terminal is thought of in terms of X columns by Y lines. In the cases 89 1.21 christos * where a wide character takes up more than one column, the adjacent 90 1.1 christos * occupied column entries will contain this faux character. */ 91 1.36 christos #define MB_FILL_CHAR ((wint_t)-1) 92 1.1 christos 93 1.1 christos /* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn 94 1.1 christos * style visual expansions. */ 95 1.34 christos libedit_private int ct_visual_width(wchar_t); 96 1.1 christos 97 1.1 christos /* Turn the given character into the appropriate visual format, matching 98 1.1 christos * the width given by ct_visual_width(). Returns the number of characters used 99 1.29 christos * up, or -1 if insufficient space. Buffer length is in count of wchar_t's. */ 100 1.34 christos libedit_private ssize_t ct_visual_char(wchar_t *, size_t, wchar_t); 101 1.1 christos 102 1.1 christos /* Convert the given string into visual format, using the ct_visual_char() 103 1.1 christos * function. Uses a static buffer, so not threadsafe. */ 104 1.34 christos libedit_private const wchar_t *ct_visual_string(const wchar_t *, ct_buffer_t *); 105 1.1 christos 106 1.1 christos 107 1.1 christos /* printable character, use ct_visual_width() to find out display width */ 108 1.1 christos #define CHTYPE_PRINT ( 0) 109 1.1 christos /* control character found inside the ASCII portion of the charset */ 110 1.1 christos #define CHTYPE_ASCIICTL (-1) 111 1.1 christos /* a \t */ 112 1.1 christos #define CHTYPE_TAB (-2) 113 1.1 christos /* a \n */ 114 1.1 christos #define CHTYPE_NL (-3) 115 1.1 christos /* non-printable character */ 116 1.1 christos #define CHTYPE_NONPRINT (-4) 117 1.1 christos /* classification of character c, as one of the above defines */ 118 1.34 christos libedit_private int ct_chr_class(wchar_t c); 119 1.1 christos 120 1.1 christos #endif /* _chartype_f */ 121