chartype.h revision 220624
1220220Sobrien/*	$NetBSD: chartype.h,v 1.6 2010/04/20 02:01:13 christos Exp $	*/
2220220Sobrien
3220220Sobrien/*-
4220220Sobrien * Copyright (c) 2009 The NetBSD Foundation, Inc.
5220220Sobrien * All rights reserved.
6220220Sobrien *
7220220Sobrien * Redistribution and use in source and binary forms, with or without
8220220Sobrien * modification, are permitted provided that the following conditions
9220220Sobrien * are met:
10220220Sobrien * 1. Redistributions of source code must retain the above copyright
11220220Sobrien *    notice, this list of conditions and the following disclaimer.
12220220Sobrien * 2. Redistributions in binary form must reproduce the above copyright
13220220Sobrien *    notice, this list of conditions and the following disclaimer in the
14220220Sobrien *    documentation and/or other materials provided with the distribution.
15220220Sobrien * 3. All advertising materials mentioning features or use of this software
16220220Sobrien *    must display the following acknowledgement:
17220220Sobrien *        This product includes software developed by the NetBSD
18220220Sobrien *        Foundation, Inc. and its contributors.
19220220Sobrien * 4. Neither the name of The NetBSD Foundation nor the names of its
20220220Sobrien *    contributors may be used to endorse or promote products derived
21220220Sobrien *    from this software without specific prior written permission.
22220220Sobrien *
23220220Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24220220Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25220220Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26220220Sobrien * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27220220Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28220220Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29220220Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30220220Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31220220Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32220220Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33220220Sobrien * POSSIBILITY OF SUCH DAMAGE.
34220624Sobrien *
35220624Sobrien * $FreeBSD: head/lib/libedit/chartype.h 220624 2011-04-14 15:42:15Z obrien $
36220220Sobrien */
37220220Sobrien
38220220Sobrien#ifndef _h_chartype_f
39220220Sobrien#define _h_chartype_f
40220220Sobrien
41220220Sobrien
42220220Sobrien
43220220Sobrien#ifdef WIDECHAR
44220220Sobrien
45220220Sobrien/* Ideally we should also test the value of the define to see if it
46220220Sobrien * supports non-BMP code points without requiring UTF-16, but nothing
47220220Sobrien * seems to actually advertise this properly, despite Unicode 3.1 having
48220220Sobrien * been around since 2001... */
49220220Sobrien#if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__))
50220220Sobrien#ifndef __STDC_ISO_10646__
51220220Sobrien/* In many places it is assumed that the first 127 code points are ASCII
52220220Sobrien * compatible, so ensure wchar_t indeed does ISO 10646 and not some other
53220220Sobrien * funky encoding that could break us in weird and wonderful ways. */
54220220Sobrien	#error wchar_t must store ISO 10646 characters
55220220Sobrien#endif
56220220Sobrien#endif
57220220Sobrien
58220220Sobrien/* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it...
59220220Sobrien * ref: ISO/IEC DTR 19769
60220220Sobrien */
61220220Sobrien#if WCHAR_MAX < INT32_MAX
62220220Sobrien#warning Build environment does not support non-BMP characters
63220220Sobrien#endif
64220220Sobrien
65220220Sobrien#define ct_mbtowc            mbtowc
66220220Sobrien#define ct_mbtowc_reset      mbtowc(0,0,0)
67220220Sobrien#define ct_wctomb            wctomb
68220220Sobrien#define ct_wctomb_reset      wctomb(0,0)
69220220Sobrien#define ct_wcstombs          wcstombs
70220220Sobrien#define ct_mbstowcs          mbstowcs
71220220Sobrien
72220220Sobrien#define Char			wchar_t
73220220Sobrien#define Int			wint_t
74220220Sobrien#define FUN(prefix,rest)	prefix ## _w ## rest
75220220Sobrien#define FUNW(type)		type ## _w
76220220Sobrien#define TYPE(type)		type ## W
77220220Sobrien#define FSTR			"%ls"
78220220Sobrien#define STR(x) 			L ## x
79220220Sobrien#define UC(c)			c
80220220Sobrien#define Isalpha(x)  iswalpha(x)
81220220Sobrien#define Isalnum(x)  iswalnum(x)
82220220Sobrien#define Isgraph(x)  iswgraph(x)
83220220Sobrien#define Isspace(x)  iswspace(x)
84220220Sobrien#define Isdigit(x)  iswdigit(x)
85220220Sobrien#define Iscntrl(x)  iswcntrl(x)
86220220Sobrien#define Isprint(x)  iswprint(x)
87220220Sobrien
88220220Sobrien#define Isupper(x)  iswupper(x)
89220220Sobrien#define Islower(x)  iswlower(x)
90220220Sobrien#define Toupper(x)  towupper(x)
91220220Sobrien#define Tolower(x)  towlower(x)
92220220Sobrien
93220220Sobrien#define IsASCII(x)  (x < 0x100)
94220220Sobrien
95220220Sobrien#define Strlen(x)       wcslen(x)
96220220Sobrien#define Strchr(s,c)     wcschr(s,c)
97220220Sobrien#define Strrchr(s,c)    wcsrchr(s,c)
98220220Sobrien#define Strstr(s,v)     wcsstr(s,v)
99220220Sobrien#define Strdup(x)       wcsdup(x)
100220220Sobrien#define Strcpy(d,s)     wcscpy(d,s)
101220220Sobrien#define Strncpy(d,s,n)  wcsncpy(d,s,n)
102220220Sobrien#define Strncat(d,s,n)  wcsncat(d,s,n)
103220220Sobrien
104220220Sobrien#define Strcmp(s,v)     wcscmp(s,v)
105220220Sobrien#define Strncmp(s,v,n)  wcsncmp(s,v,n)
106220220Sobrien#define Strcspn(s,r)    wcscspn(s,r)
107220220Sobrien
108220220Sobrien#define Strtol(p,e,b)   wcstol(p,e,b)
109220220Sobrien
110220220Sobrien#define Width(c)	wcwidth(c)
111220220Sobrien
112220220Sobrien#else /* NARROW */
113220220Sobrien
114220220Sobrien#define ct_mbtowc            error
115220220Sobrien#define ct_mbtowc_reset
116220220Sobrien#define ct_wctomb            error
117220220Sobrien#define ct_wctomb_reset
118220220Sobrien#define ct_wcstombs(a, b, c)    (strncpy(a, b, c), strlen(a))
119220220Sobrien#define ct_mbstowcs(a, b, c)    (strncpy(a, b, c), strlen(a))
120220220Sobrien
121220220Sobrien#define Char			char
122220220Sobrien#define Int			int
123220220Sobrien#define FUN(prefix,rest)	prefix ## _ ## rest
124220220Sobrien#define FUNW(type)		type
125220220Sobrien#define TYPE(type)		type
126220220Sobrien#define FSTR			"%s"
127220220Sobrien#define STR(x) 			x
128220220Sobrien#define UC(c)			(unsigned char)(c)
129220220Sobrien
130220220Sobrien#define Isalpha(x)  isalpha((unsigned char)x)
131220220Sobrien#define Isalnum(x)  isalnum((unsigned char)x)
132220220Sobrien#define Isgraph(x)  isgraph((unsigned char)x)
133220220Sobrien#define Isspace(x)  isspace((unsigned char)x)
134220220Sobrien#define Isdigit(x)  isdigit((unsigned char)x)
135220220Sobrien#define Iscntrl(x)  iscntrl((unsigned char)x)
136220220Sobrien#define Isprint(x)  isprint((unsigned char)x)
137220220Sobrien
138220220Sobrien#define Isupper(x)  isupper((unsigned char)x)
139220220Sobrien#define Islower(x)  islower((unsigned char)x)
140220220Sobrien#define Toupper(x)  toupper((unsigned char)x)
141220220Sobrien#define Tolower(x)  tolower((unsigned char)x)
142220220Sobrien
143220220Sobrien#define IsASCII(x)  isascii((unsigned char)x)
144220220Sobrien
145220220Sobrien#define Strlen(x)       strlen(x)
146220220Sobrien#define Strchr(s,c)     strchr(s,c)
147220220Sobrien#define Strrchr(s,c)    strrchr(s,c)
148220220Sobrien#define Strstr(s,v)     strstr(s,v)
149220220Sobrien#define Strdup(x)       strdup(x)
150220220Sobrien#define Strcpy(d,s)     strcpy(d,s)
151220220Sobrien#define Strncpy(d,s,n)  strncpy(d,s,n)
152220220Sobrien#define Strncat(d,s,n)  strncat(d,s,n)
153220220Sobrien
154220220Sobrien#define Strcmp(s,v)     strcmp(s,v)
155220220Sobrien#define Strncmp(s,v,n)  strncmp(s,v,n)
156220220Sobrien#define Strcspn(s,r)    strcspn(s,r)
157220220Sobrien
158220220Sobrien#define Strtol(p,e,b)   strtol(p,e,b)
159220220Sobrien
160220220Sobrien#define Width(c)	1
161220220Sobrien
162220220Sobrien#endif
163220220Sobrien
164220220Sobrien
165220220Sobrien#ifdef WIDECHAR
166220220Sobrien/*
167220220Sobrien * Conversion buffer
168220220Sobrien */
169220220Sobrientypedef struct ct_buffer_t {
170220220Sobrien        char    *cbuff;
171220220Sobrien        size_t  csize;
172220220Sobrien        Char *wbuff;
173220220Sobrien        size_t  wsize;
174220220Sobrien} ct_buffer_t;
175220220Sobrien
176220220Sobrien#define ct_encode_string __ct_encode_string
177220220Sobrien/* Encode a wide character string and return the UTF-8 encoded result. */
178220220Sobrienpublic char *ct_encode_string(const Char *, ct_buffer_t *);
179220220Sobrien
180220220Sobrien#define ct_decode_string __ct_decode_string
181220220Sobrien/* Decode a (multi)?byte string and return the wide character string result. */
182220220Sobrienpublic Char *ct_decode_string(const char *, ct_buffer_t *);
183220220Sobrien
184220220Sobrien/* Decode a (multi)?byte argv string array.
185220220Sobrien * The pointer returned must be free()d when done. */
186220220Sobrienprotected Char **ct_decode_argv(int, const char *[],  ct_buffer_t *);
187220220Sobrien
188220220Sobrien/* Resizes the conversion buffer(s) if needed. */
189220220Sobrienprotected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t);
190220220Sobrienprotected ssize_t ct_encode_char(char *, size_t, Char);
191220220Sobrienprotected size_t ct_enc_width(Char);
192220220Sobrien
193220220Sobrien#define ct_free_argv(s)	el_free(s)
194220220Sobrien
195220220Sobrien#else
196220220Sobrien#define	ct_encode_string(s, b)	(s)
197220220Sobrien#define ct_decode_string(s, b)	(s)
198220220Sobrien#define ct_decode_argv(l, s, b)	(s)
199220220Sobrien#define ct_conv_buff_resize(b, os, ns)
200220220Sobrien#define ct_encode_char(d, l, s)	(*d = s, 1)
201220220Sobrien#define ct_free_argv(s)
202220220Sobrien#endif
203220220Sobrien
204220220Sobrien#ifndef NARROWCHAR
205220220Sobrien/* Encode a characted into the destination buffer, provided there is sufficent
206220220Sobrien * buffer space available. Returns the number of bytes used up (zero if the
207220220Sobrien * character cannot be encoded, -1 if there was not enough space available). */
208220220Sobrien
209220220Sobrien/* The maximum buffer size to hold the most unwieldly visual representation,
210220220Sobrien * in this case \U+nnnnn. */
211220220Sobrien#define VISUAL_WIDTH_MAX 8
212220220Sobrien
213220220Sobrien/* The terminal is thought of in terms of X columns by Y lines. In the cases
214220220Sobrien * where a wide character takes up more than one column, the adjacent
215220220Sobrien * occupied column entries will contain this faux character. */
216220220Sobrien#define MB_FILL_CHAR ((Char)-1)
217220220Sobrien
218220220Sobrien/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn
219220220Sobrien * style visual expansions. */
220220220Sobrienprotected int ct_visual_width(Char);
221220220Sobrien
222220220Sobrien/* Turn the given character into the appropriate visual format, matching
223220220Sobrien * the width given by ct_visual_width(). Returns the number of characters used
224220220Sobrien * up, or -1 if insufficient space. Buffer length is in count of Char's. */
225220220Sobrienprotected ssize_t ct_visual_char(Char *, size_t, Char);
226220220Sobrien
227220220Sobrien/* Convert the given string into visual format, using the ct_visual_char()
228220220Sobrien * function. Uses a static buffer, so not threadsafe. */
229220220Sobrienprotected const Char *ct_visual_string(const Char *);
230220220Sobrien
231220220Sobrien
232220220Sobrien/* printable character, use ct_visual_width() to find out display width */
233220220Sobrien#define CHTYPE_PRINT        ( 0)
234220220Sobrien/* control character found inside the ASCII portion of the charset */
235220220Sobrien#define CHTYPE_ASCIICTL     (-1)
236220220Sobrien/* a \t */
237220220Sobrien#define CHTYPE_TAB          (-2)
238220220Sobrien/* a \n */
239220220Sobrien#define CHTYPE_NL           (-3)
240220220Sobrien/* non-printable character */
241220220Sobrien#define CHTYPE_NONPRINT     (-4)
242220220Sobrien/* classification of character c, as one of the above defines */
243220220Sobrienprotected int ct_chr_class(Char c);
244220220Sobrien#endif
245220220Sobrien
246220220Sobrien
247220220Sobrien#endif /* _chartype_f */
248