/* $NetBSD: chartype.h,v 1.37 2022/04/11 19:37:20 tnn Exp $ */12/*-3* Copyright (c) 2009 The NetBSD Foundation, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS16* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED17* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR18* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS19* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR20* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF21* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS22* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN23* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)24* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE25* POSSIBILITY OF SUCH DAMAGE.26*/2728#ifndef _h_chartype_f29#define _h_chartype_f3031/* Ideally we should also test the value of the define to see if it32* supports non-BMP code points without requiring UTF-16, but nothing33* seems to actually advertise this properly, despite Unicode 3.1 having34* been around since 2001... */35#if !defined(__NetBSD__) && \36!defined(__sun) && \37!defined(__osf__) && \38!(defined(__APPLE__) && defined(__MACH__)) && \39!defined(__OpenBSD__) && \40!defined(__FreeBSD__) && \41!defined(__DragonFly__)42#ifndef __STDC_ISO_10646__43/* In many places it is assumed that the first 127 code points are ASCII44* compatible, so ensure wchar_t indeed does ISO 10646 and not some other45* funky encoding that could break us in weird and wonderful ways. */46#error wchar_t must store ISO 10646 characters47#endif48#endif4950/* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it...51* ref: ISO/IEC DTR 1976952*/53#if WCHAR_MAX < INT32_MAX54#warning Build environment does not support non-BMP characters55#endif5657/*58* Conversion buffer59*/60typedef struct ct_buffer_t {61char *cbuff;62size_t csize;63wchar_t *wbuff;64size_t wsize;65} ct_buffer_t;6667/* Encode a wide-character string and return the UTF-8 encoded result. */68char *ct_encode_string(const wchar_t *, ct_buffer_t *);6970/* Decode a (multi)?byte string and return the wide-character string result. */71wchar_t *ct_decode_string(const char *, ct_buffer_t *);7273/* Decode a (multi)?byte argv string array.74* The pointer returned must be free()d when done. */75libedit_private wchar_t **ct_decode_argv(int, const char *[], ct_buffer_t *);7677/* Encode a character into the destination buffer, provided there is sufficient78* buffer space available. Returns the number of bytes used up (zero if the79* character cannot be encoded, -1 if there was not enough space available). */80libedit_private ssize_t ct_encode_char(char *, size_t, wchar_t);81libedit_private size_t ct_enc_width(wchar_t);8283/* The maximum buffer size to hold the most unwieldy visual representation,84* in this case \U+nnnnn. */85#define VISUAL_WIDTH_MAX ((size_t)8)8687/* The terminal is thought of in terms of X columns by Y lines. In the cases88* where a wide character takes up more than one column, the adjacent89* occupied column entries will contain this faux character. */90#define MB_FILL_CHAR ((wint_t)-1)9192/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn93* style visual expansions. */94libedit_private int ct_visual_width(wchar_t);9596/* Turn the given character into the appropriate visual format, matching97* the width given by ct_visual_width(). Returns the number of characters used98* up, or -1 if insufficient space. Buffer length is in count of wchar_t's. */99libedit_private ssize_t ct_visual_char(wchar_t *, size_t, wchar_t);100101/* Convert the given string into visual format, using the ct_visual_char()102* function. Uses a static buffer, so not threadsafe. */103libedit_private const wchar_t *ct_visual_string(const wchar_t *, ct_buffer_t *);104105106/* printable character, use ct_visual_width() to find out display width */107#define CHTYPE_PRINT ( 0)108/* control character found inside the ASCII portion of the charset */109#define CHTYPE_ASCIICTL (-1)110/* a \t */111#define CHTYPE_TAB (-2)112/* a \n */113#define CHTYPE_NL (-3)114/* non-printable character */115#define CHTYPE_NONPRINT (-4)116/* classification of character c, as one of the above defines */117libedit_private int ct_chr_class(wchar_t c);118119#endif /* _chartype_f */120121122