xref: /minix3/lib/libedit/chartype.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: chartype.h,v 1.15 2015/05/17 13:14:41 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by the NetBSD
18  *        Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _h_chartype_f
37 #define _h_chartype_f
38 
39 
40 
41 #ifdef WIDECHAR
42 
43 /* Ideally we should also test the value of the define to see if it
44  * supports non-BMP code points without requiring UTF-16, but nothing
45  * seems to actually advertise this properly, despite Unicode 3.1 having
46  * been around since 2001... */
47 #if !defined(__minix)
48 #if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__)) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
49 #ifndef __STDC_ISO_10646__
50 /* In many places it is assumed that the first 127 code points are ASCII
51  * compatible, so ensure wchar_t indeed does ISO 10646 and not some other
52  * funky encoding that could break us in weird and wonderful ways. */
53 	#error wchar_t must store ISO 10646 characters
54 #endif
55 #endif
56 #endif /* !defined(__minix) */
57 
58 /* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it...
59  * ref: ISO/IEC DTR 19769
60  */
61 #if WCHAR_MAX < INT32_MAX
62 #warning Build environment does not support non-BMP characters
63 #endif
64 
65 #define ct_mbtowc            mbtowc
66 #define ct_mbtowc_reset      mbtowc(0,0,(size_t)0)
67 #define ct_wctomb            wctomb
68 #define ct_wctomb_reset      wctomb(0,0)
69 #define ct_wcstombs          wcstombs
70 #define ct_mbstowcs          mbstowcs
71 
72 #define Char			wchar_t
73 #define Int			wint_t
74 #define FUN(prefix,rest)	prefix ## _w ## rest
75 #define FUNW(type)		type ## _w
76 #define TYPE(type)		type ## W
77 #define FCHAR			"%lc"
78 #define FSTR			"%ls"
79 #define STR(x) 			L ## x
80 #define UC(c)			c
81 #define Isalpha(x)  iswalpha(x)
82 #define Isalnum(x)  iswalnum(x)
83 #define Isgraph(x)  iswgraph(x)
84 #define Isspace(x)  iswspace(x)
85 #define Isdigit(x)  iswdigit(x)
86 #define Iscntrl(x)  iswcntrl(x)
87 #define Isprint(x)  iswprint(x)
88 
89 #define Isupper(x)  iswupper(x)
90 #define Islower(x)  iswlower(x)
91 #define Toupper(x)  towupper(x)
92 #define Tolower(x)  towlower(x)
93 
94 #define IsASCII(x)  (x < 0x100)
95 
96 #define Strlen(x)       wcslen(x)
97 #define Strchr(s,c)     wcschr(s,c)
98 #define Strrchr(s,c)    wcsrchr(s,c)
99 #define Strstr(s,v)     wcsstr(s,v)
100 #define Strdup(x)       wcsdup(x)
101 #define Strcpy(d,s)     wcscpy(d,s)
102 #define Strncpy(d,s,n)  wcsncpy(d,s,n)
103 #define Strncat(d,s,n)  wcsncat(d,s,n)
104 
105 #define Strcmp(s,v)     wcscmp(s,v)
106 #define Strncmp(s,v,n)  wcsncmp(s,v,n)
107 #define Strcspn(s,r)    wcscspn(s,r)
108 
109 #define Strtol(p,e,b)   wcstol(p,e,b)
110 
111 static inline int
Width(wchar_t c)112 Width(wchar_t c)
113 {
114 	int w = wcwidth(c);
115 	return w < 0 ? 0 : w;
116 }
117 
118 #else /* NARROW */
119 
120 #define ct_mbtowc            error
121 #define ct_mbtowc_reset
122 #define ct_wctomb            error
123 #define ct_wctomb_reset
124 #define ct_wcstombs(a, b, c)    (strncpy(a, b, c), strlen(a))
125 #define ct_mbstowcs(a, b, c)    (strncpy(a, b, c), strlen(a))
126 
127 #define Char			char
128 #define Int			int
129 #define FUN(prefix,rest)	prefix ## _ ## rest
130 #define FUNW(type)		type
131 #define TYPE(type)		type
132 #define FCHAR			"%c"
133 #define FSTR			"%s"
134 #define STR(x) 			x
135 #define UC(c)			(unsigned char)(c)
136 
137 #define Isalpha(x)  isalpha((unsigned char)x)
138 #define Isalnum(x)  isalnum((unsigned char)x)
139 #define Isgraph(x)  isgraph((unsigned char)x)
140 #define Isspace(x)  isspace((unsigned char)x)
141 #define Isdigit(x)  isdigit((unsigned char)x)
142 #define Iscntrl(x)  iscntrl((unsigned char)x)
143 #define Isprint(x)  isprint((unsigned char)x)
144 
145 #define Isupper(x)  isupper((unsigned char)x)
146 #define Islower(x)  islower((unsigned char)x)
147 #define Toupper(x)  toupper((unsigned char)x)
148 #define Tolower(x)  tolower((unsigned char)x)
149 
150 #define IsASCII(x)  isascii((unsigned char)x)
151 
152 #define Strlen(x)       strlen(x)
153 #define Strchr(s,c)     strchr(s,c)
154 #define Strrchr(s,c)    strrchr(s,c)
155 #define Strstr(s,v)     strstr(s,v)
156 #define Strdup(x)       strdup(x)
157 #define Strcpy(d,s)     strcpy(d,s)
158 #define Strncpy(d,s,n)  strncpy(d,s,n)
159 #define Strncat(d,s,n)  strncat(d,s,n)
160 
161 #define Strcmp(s,v)     strcmp(s,v)
162 #define Strncmp(s,v,n)  strncmp(s,v,n)
163 #define Strcspn(s,r)    strcspn(s,r)
164 
165 #define Strtol(p,e,b)   strtol(p,e,b)
166 
167 #define Width(c)	1
168 
169 #endif
170 
171 
172 #ifdef WIDECHAR
173 /*
174  * Conversion buffer
175  */
176 typedef struct ct_buffer_t {
177         char    *cbuff;
178         size_t  csize;
179         Char *wbuff;
180         size_t  wsize;
181 } ct_buffer_t;
182 
183 #define ct_encode_string __ct_encode_string
184 /* Encode a wide-character string and return the UTF-8 encoded result. */
185 public char *ct_encode_string(const Char *, ct_buffer_t *);
186 
187 #define ct_decode_string __ct_decode_string
188 /* Decode a (multi)?byte string and return the wide-character string result. */
189 public Char *ct_decode_string(const char *, ct_buffer_t *);
190 
191 /* Decode a (multi)?byte argv string array.
192  * The pointer returned must be free()d when done. */
193 protected Char **ct_decode_argv(int, const char *[],  ct_buffer_t *);
194 
195 /* Resizes the conversion buffer(s) if needed. */
196 protected int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
197 protected int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
198 protected ssize_t ct_encode_char(char *, size_t, Char);
199 protected size_t ct_enc_width(Char);
200 
201 #define ct_free_argv(s)	el_free(s)
202 
203 #else
204 #define	ct_encode_string(s, b)	(s)
205 #define ct_decode_string(s, b)	(s)
206 #define ct_decode_argv(l, s, b)	(s)
207 #define ct_conv_cbuff_resize(b, s) ((s) == (0))
208 #define ct_conv_wbuff_resize(b, s) ((s) == (0))
209 #define ct_encode_char(d, l, s)	(*d = s, 1)
210 #define ct_free_argv(s)
211 #endif
212 
213 #ifndef NARROWCHAR
214 /* Encode a characted into the destination buffer, provided there is sufficent
215  * buffer space available. Returns the number of bytes used up (zero if the
216  * character cannot be encoded, -1 if there was not enough space available). */
217 
218 /* The maximum buffer size to hold the most unwieldly visual representation,
219  * in this case \U+nnnnn. */
220 #define VISUAL_WIDTH_MAX ((size_t)8)
221 
222 /* The terminal is thought of in terms of X columns by Y lines. In the cases
223  * where a wide character takes up more than one column, the adjacent
224  * occupied column entries will contain this faux character. */
225 #define MB_FILL_CHAR ((Char)-1)
226 
227 /* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn
228  * style visual expansions. */
229 protected int ct_visual_width(Char);
230 
231 /* Turn the given character into the appropriate visual format, matching
232  * the width given by ct_visual_width(). Returns the number of characters used
233  * up, or -1 if insufficient space. Buffer length is in count of Char's. */
234 protected ssize_t ct_visual_char(Char *, size_t, Char);
235 
236 /* Convert the given string into visual format, using the ct_visual_char()
237  * function. Uses a static buffer, so not threadsafe. */
238 protected const Char *ct_visual_string(const Char *);
239 
240 
241 /* printable character, use ct_visual_width() to find out display width */
242 #define CHTYPE_PRINT        ( 0)
243 /* control character found inside the ASCII portion of the charset */
244 #define CHTYPE_ASCIICTL     (-1)
245 /* a \t */
246 #define CHTYPE_TAB          (-2)
247 /* a \n */
248 #define CHTYPE_NL           (-3)
249 /* non-printable character */
250 #define CHTYPE_NONPRINT     (-4)
251 /* classification of character c, as one of the above defines */
252 protected int ct_chr_class(Char c);
253 #endif
254 
255 
256 #endif /* _chartype_f */
257