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