xref: /dflybsd-src/contrib/libedit/src/chartype.h (revision 698e9e6cd5f042847de67460caaa3fde98cdfe99)
1*cdf8408cSAntonio Huete Jimenez /*	$NetBSD: chartype.h,v 1.37 2022/04/11 19:37:20 tnn Exp $	*/
232fe07f8SJohn Marino 
332fe07f8SJohn Marino /*-
432fe07f8SJohn Marino  * Copyright (c) 2009 The NetBSD Foundation, Inc.
532fe07f8SJohn Marino  * All rights reserved.
632fe07f8SJohn Marino  *
732fe07f8SJohn Marino  * Redistribution and use in source and binary forms, with or without
832fe07f8SJohn Marino  * modification, are permitted provided that the following conditions
932fe07f8SJohn Marino  * are met:
1032fe07f8SJohn Marino  * 1. Redistributions of source code must retain the above copyright
1132fe07f8SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1232fe07f8SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1332fe07f8SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1432fe07f8SJohn Marino  *    documentation and/or other materials provided with the distribution.
1532fe07f8SJohn Marino  *
1632fe07f8SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1732fe07f8SJohn Marino  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1832fe07f8SJohn Marino  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1932fe07f8SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2032fe07f8SJohn Marino  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2132fe07f8SJohn Marino  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2232fe07f8SJohn Marino  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2332fe07f8SJohn Marino  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2432fe07f8SJohn Marino  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2532fe07f8SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2632fe07f8SJohn Marino  * POSSIBILITY OF SUCH DAMAGE.
2732fe07f8SJohn Marino  */
2832fe07f8SJohn Marino 
2932fe07f8SJohn Marino #ifndef _h_chartype_f
3032fe07f8SJohn Marino #define _h_chartype_f
3132fe07f8SJohn Marino 
3232fe07f8SJohn Marino /* Ideally we should also test the value of the define to see if it
3332fe07f8SJohn Marino  * supports non-BMP code points without requiring UTF-16, but nothing
3432fe07f8SJohn Marino  * seems to actually advertise this properly, despite Unicode 3.1 having
3532fe07f8SJohn Marino  * been around since 2001... */
36ae19eda8Szrj #if	!defined(__NetBSD__) && \
37ae19eda8Szrj 	!defined(__sun) && \
38*cdf8408cSAntonio Huete Jimenez 	!defined(__osf__) && \
39ae19eda8Szrj 	!(defined(__APPLE__) && defined(__MACH__)) && \
40ae19eda8Szrj 	!defined(__OpenBSD__) && \
41ae19eda8Szrj 	!defined(__FreeBSD__) && \
42ae19eda8Szrj 	!defined(__DragonFly__)
4332fe07f8SJohn Marino #ifndef __STDC_ISO_10646__
4432fe07f8SJohn Marino /* In many places it is assumed that the first 127 code points are ASCII
4532fe07f8SJohn Marino  * compatible, so ensure wchar_t indeed does ISO 10646 and not some other
4632fe07f8SJohn Marino  * funky encoding that could break us in weird and wonderful ways. */
4732fe07f8SJohn Marino 	#error wchar_t must store ISO 10646 characters
4832fe07f8SJohn Marino #endif
4932fe07f8SJohn Marino #endif
5032fe07f8SJohn Marino 
5132fe07f8SJohn Marino /* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it...
5232fe07f8SJohn Marino  * ref: ISO/IEC DTR 19769
5332fe07f8SJohn Marino  */
5432fe07f8SJohn Marino #if WCHAR_MAX < INT32_MAX
5532fe07f8SJohn Marino #warning Build environment does not support non-BMP characters
5632fe07f8SJohn Marino #endif
5732fe07f8SJohn Marino 
5832fe07f8SJohn Marino /*
5932fe07f8SJohn Marino  * Conversion buffer
6032fe07f8SJohn Marino  */
6132fe07f8SJohn Marino typedef struct ct_buffer_t {
6232fe07f8SJohn Marino         char    *cbuff;
6332fe07f8SJohn Marino         size_t  csize;
6412db70c8Szrj         wchar_t *wbuff;
6532fe07f8SJohn Marino         size_t  wsize;
6632fe07f8SJohn Marino } ct_buffer_t;
6732fe07f8SJohn Marino 
6832fe07f8SJohn Marino /* Encode a wide-character string and return the UTF-8 encoded result. */
6912db70c8Szrj char *ct_encode_string(const wchar_t *, ct_buffer_t *);
7032fe07f8SJohn Marino 
7132fe07f8SJohn Marino /* Decode a (multi)?byte string and return the wide-character string result. */
7212db70c8Szrj wchar_t *ct_decode_string(const char *, ct_buffer_t *);
7332fe07f8SJohn Marino 
7432fe07f8SJohn Marino /* Decode a (multi)?byte argv string array.
7532fe07f8SJohn Marino  * The pointer returned must be free()d when done. */
7612db70c8Szrj libedit_private wchar_t **ct_decode_argv(int, const char *[],  ct_buffer_t *);
7732fe07f8SJohn Marino 
7812db70c8Szrj /* Encode a character into the destination buffer, provided there is sufficient
7932fe07f8SJohn Marino  * buffer space available. Returns the number of bytes used up (zero if the
8032fe07f8SJohn Marino  * character cannot be encoded, -1 if there was not enough space available). */
8112db70c8Szrj libedit_private ssize_t ct_encode_char(char *, size_t, wchar_t);
8212db70c8Szrj libedit_private size_t ct_enc_width(wchar_t);
8332fe07f8SJohn Marino 
8412db70c8Szrj /* The maximum buffer size to hold the most unwieldy visual representation,
8532fe07f8SJohn Marino  * in this case \U+nnnnn. */
8632fe07f8SJohn Marino #define VISUAL_WIDTH_MAX ((size_t)8)
8732fe07f8SJohn Marino 
8832fe07f8SJohn Marino /* The terminal is thought of in terms of X columns by Y lines. In the cases
8932fe07f8SJohn Marino  * where a wide character takes up more than one column, the adjacent
9032fe07f8SJohn Marino  * occupied column entries will contain this faux character. */
9160ecde0cSDaniel Fojt #define MB_FILL_CHAR ((wint_t)-1)
9232fe07f8SJohn Marino 
9332fe07f8SJohn Marino /* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn
9432fe07f8SJohn Marino  * style visual expansions. */
9512db70c8Szrj libedit_private int ct_visual_width(wchar_t);
9632fe07f8SJohn Marino 
9732fe07f8SJohn Marino /* Turn the given character into the appropriate visual format, matching
9832fe07f8SJohn Marino  * the width given by ct_visual_width(). Returns the number of characters used
9912db70c8Szrj  * up, or -1 if insufficient space. Buffer length is in count of wchar_t's. */
10012db70c8Szrj libedit_private ssize_t ct_visual_char(wchar_t *, size_t, wchar_t);
10132fe07f8SJohn Marino 
10232fe07f8SJohn Marino /* Convert the given string into visual format, using the ct_visual_char()
10332fe07f8SJohn Marino  * function. Uses a static buffer, so not threadsafe. */
10412db70c8Szrj libedit_private const wchar_t *ct_visual_string(const wchar_t *, ct_buffer_t *);
10532fe07f8SJohn Marino 
10632fe07f8SJohn Marino 
10732fe07f8SJohn Marino /* printable character, use ct_visual_width() to find out display width */
10832fe07f8SJohn Marino #define CHTYPE_PRINT        ( 0)
10932fe07f8SJohn Marino /* control character found inside the ASCII portion of the charset */
11032fe07f8SJohn Marino #define CHTYPE_ASCIICTL     (-1)
11132fe07f8SJohn Marino /* a \t */
11232fe07f8SJohn Marino #define CHTYPE_TAB          (-2)
11332fe07f8SJohn Marino /* a \n */
11432fe07f8SJohn Marino #define CHTYPE_NL           (-3)
11532fe07f8SJohn Marino /* non-printable character */
11632fe07f8SJohn Marino #define CHTYPE_NONPRINT     (-4)
11732fe07f8SJohn Marino /* classification of character c, as one of the above defines */
11812db70c8Szrj libedit_private int ct_chr_class(wchar_t c);
11932fe07f8SJohn Marino 
12032fe07f8SJohn Marino #endif /* _chartype_f */
121