1 /*	$NetBSD: chartype.h,v 1.10 2011/11/16 01:45:10 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  * The test here in the original version of the source had a white-list
44  * of platforms and gave an "#error" if there wa an attempt to build
45  * otherwise. The list did not include FreeBSD and so for utterly
46  * frivolous reasons the code would not compile there. I have changed the
47  * error to a warning. If __GNUC__ is defined there should be a "#warning"
48  * directive that can be used, otherwise I hope that use of a (probably)
49  * undefined #pragma will cause the compiler to say something.
50  * At present I find it hard to see the possibility of modern machines that
51  * do not use the first 127 codepoints as expected. A LONG while in the past
52  * one might have had EBCDIC.   ACN May 2016
53  *
54  * Aha - I have done some more research. In some cases especially in CJK
55  * markets wide strings might be encoded with each wchar_t holding just
56  * a concatenation of bytes from an earlier multi-byte extended representation,
57  * and that could perhaps lead to the presence of bytes in the range 0-127
58  * not standing for ASCII characters. That would relate to ISO 2022. The
59  * code here in libedit really wants wchar_t to be a 32-bit type so that
60  * *ALL* Unicode characters can be represented. Where that is the case it
61  * will be able to accept UTF-8 input. On platforms where wchar_t is only
62  * a 16-bit type it will only be possible to cope with characters in the
63  * basic multilingual plane. The most common symbols NOT present there are
64  * perhaps some mathematical symbols and alphabets (including Gothic) and
65  * a load of Emojis. So those who use them and try (for instance) using copy
66  * and paste to insert them input input should be aware that on some platforms
67  * they will not be well supported.
68  */
69 
70 /* Ideally we should also test the value of the define to see if it
71  * supports non-BMP code points without requiring UTF-16, but nothing
72  * seems to actually advertise this properly, despite Unicode 3.1 having
73  * been around since 2001... */
74 #if !defined(__NetBSD__) && !defined(__sun) && \
75     !(defined(__APPLE__) && defined(__MACH__)) && \
76     !(defined(__FreeBSD__) || defined(__DragonFly__)) && !defined(__CYGWIN__)
77 #ifndef __STDC_ISO_10646__
78 /* In many places it is assumed that the first 127 code points are ASCII
79  * compatible, so ensure wchar_t indeed does ISO 10646 and not some other
80  * funky encoding that could break us in weird and wonderful ways. */
81 #ifdef __GNUC__
82 	#warning wchar_t must store ISO 10646 characters
83 #else
84 	#pragra warn_the_user wchar_t must store ISO 10646 characters
85 #endif
86 #endif
87 #endif
88 
89 /* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it...
90  * ref: ISO/IEC DTR 19769
91  */
92 #if WCHAR_MAX < INT32_MAX
93 #ifdef __GNUC__
94 /*
95  * Note that the "#warning" directive is not part of standard C. However
96  * it is supported by gcc and clang (and clang, at least on a Macintosh,
97  * predefined __GNUC__). So I will enable this just in cases where I am
98  * confident that it will be a warning and not an error.
99  */
100 #warning Build environment does not support non-BMP characters
101 #else
102 /*
103  * On other compilers I am going to hope that there is no built-in
104  * pragma called "warn_the_user" and that the compiler will report
105  * when an undefined pragma is encountered. I might have hoped that
106  * "#pragma warning ..." would be the idiom to use, but there are cases
107  * where that is used to control when the compiler generates warnings rather
108  * than to generate one instantly.
109  */
110 #pragma warn_the_user "Build environment does not support non-BMP characters"
111 #endif
112 #endif
113 
114 #ifndef HAVE_WCSDUP
115 wchar_t *wcsdup(const wchar_t *s);
116 #endif
117 
118 #define ct_mbtowc            mbtowc
119 #define ct_mbtowc_reset      mbtowc(0,0,(size_t)0)
120 #define ct_wctomb            wctomb
121 #define ct_wctomb_reset      wctomb(0,0)
122 #define ct_wcstombs          wcstombs
123 #define ct_mbstowcs          mbstowcs
124 
125 #define Char			wchar_t
126 #define Int			wint_t
127 #define FUN(prefix,rest)	prefix ## _w ## rest
128 #define FUNW(type)		type ## _w
129 #define TYPE(type)		type ## W
130 #define FSTR			"%ls"
131 #define STR(x) 			L ## x
132 #define UC(c)			c
133 #define Isalpha(x)  iswalpha(x)
134 #define Isalnum(x)  iswalnum(x)
135 #define Isgraph(x)  iswgraph(x)
136 #define Isspace(x)  iswspace(x)
137 #define Isdigit(x)  iswdigit(x)
138 #define Iscntrl(x)  iswcntrl(x)
139 #define Isprint(x)  iswprint(x)
140 
141 #define Isupper(x)  iswupper(x)
142 #define Islower(x)  iswlower(x)
143 #define Toupper(x)  towupper(x)
144 #define Tolower(x)  towlower(x)
145 
146 #define IsASCII(x)  (x < 0x100)
147 
148 #define Strlen(x)       wcslen(x)
149 #define Strchr(s,c)     wcschr(s,c)
150 #define Strrchr(s,c)    wcsrchr(s,c)
151 #define Strstr(s,v)     wcsstr(s,v)
152 #define Strdup(x)       wcsdup(x)
153 #define Strcpy(d,s)     wcscpy(d,s)
154 #define Strncpy(d,s,n)  wcsncpy(d,s,n)
155 #define Strncat(d,s,n)  wcsncat(d,s,n)
156 
157 #define Strcmp(s,v)     wcscmp(s,v)
158 #define Strncmp(s,v,n)  wcsncmp(s,v,n)
159 #define Strcspn(s,r)    wcscspn(s,r)
160 
161 #define Strtol(p,e,b)   wcstol(p,e,b)
162 
163 static inline int
Width(wchar_t c)164 Width(wchar_t c)
165 {
166 	int w = wcwidth(c);
167 	return w < 0 ? 0 : w;
168 }
169 
170 #else /* NARROW */
171 
172 #define ct_mbtowc            error
173 #define ct_mbtowc_reset
174 #define ct_wctomb            error
175 #define ct_wctomb_reset
176 #define ct_wcstombs(a, b, c)    (strncpy(a, b, c), strlen(a))
177 #define ct_mbstowcs(a, b, c)    (strncpy(a, b, c), strlen(a))
178 
179 #define Char			char
180 #define Int			int
181 #define FUN(prefix,rest)	prefix ## _ ## rest
182 #define FUNW(type)		type
183 #define TYPE(type)		type
184 #define FSTR			"%s"
185 #define STR(x) 			x
186 #define UC(c)			(unsigned char)(c)
187 
188 #define Isalpha(x)  isalpha((unsigned char)x)
189 #define Isalnum(x)  isalnum((unsigned char)x)
190 #define Isgraph(x)  isgraph((unsigned char)x)
191 #define Isspace(x)  isspace((unsigned char)x)
192 #define Isdigit(x)  isdigit((unsigned char)x)
193 #define Iscntrl(x)  iscntrl((unsigned char)x)
194 #define Isprint(x)  isprint((unsigned char)x)
195 
196 #define Isupper(x)  isupper((unsigned char)x)
197 #define Islower(x)  islower((unsigned char)x)
198 #define Toupper(x)  toupper((unsigned char)x)
199 #define Tolower(x)  tolower((unsigned char)x)
200 
201 #define IsASCII(x)  isascii((unsigned char)x)
202 
203 #define Strlen(x)       strlen(x)
204 #define Strchr(s,c)     strchr(s,c)
205 #define Strrchr(s,c)    strrchr(s,c)
206 #define Strstr(s,v)     strstr(s,v)
207 #define Strdup(x)       strdup(x)
208 #define Strcpy(d,s)     strcpy(d,s)
209 #define Strncpy(d,s,n)  strncpy(d,s,n)
210 #define Strncat(d,s,n)  strncat(d,s,n)
211 
212 #define Strcmp(s,v)     strcmp(s,v)
213 #define Strncmp(s,v,n)  strncmp(s,v,n)
214 #define Strcspn(s,r)    strcspn(s,r)
215 
216 #define Strtol(p,e,b)   strtol(p,e,b)
217 
218 #define Width(c)	1
219 
220 #endif
221 
222 
223 #ifdef WIDECHAR
224 /*
225  * Conversion buffer
226  */
227 typedef struct ct_buffer_t {
228         char    *cbuff;
229         size_t  csize;
230         Char *wbuff;
231         size_t  wsize;
232 } ct_buffer_t;
233 
234 #define ct_encode_string __ct_encode_string
235 /* Encode a wide-character string and return the UTF-8 encoded result. */
236 public char *ct_encode_string(const Char *, ct_buffer_t *);
237 
238 #define ct_decode_string __ct_decode_string
239 /* Decode a (multi)?byte string and return the wide-character string result. */
240 public Char *ct_decode_string(const char *, ct_buffer_t *);
241 
242 /* Decode a (multi)?byte argv string array.
243  * The pointer returned must be free()d when done. */
244 protected Char **ct_decode_argv(int, const char *[],  ct_buffer_t *);
245 
246 /* Resizes the conversion buffer(s) if needed. */
247 protected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t);
248 protected ssize_t ct_encode_char(char *, size_t, Char);
249 protected size_t ct_enc_width(Char);
250 
251 #define ct_free_argv(s)	el_free(s)
252 
253 #else
254 #define	ct_encode_string(s, b)	(s)
255 #define ct_decode_string(s, b)	(s)
256 #define ct_decode_argv(l, s, b)	(s)
257 #define ct_conv_buff_resize(b, os, ns)
258 #define ct_encode_char(d, l, s)	(*d = s, 1)
259 #define ct_free_argv(s)
260 #endif
261 
262 #ifndef NARROWCHAR
263 /* Encode a characted into the destination buffer, provided there is sufficent
264  * buffer space available. Returns the number of bytes used up (zero if the
265  * character cannot be encoded, -1 if there was not enough space available). */
266 
267 /* The maximum buffer size to hold the most unwieldly visual representation,
268  * in this case \U+nnnnn. */
269 #define VISUAL_WIDTH_MAX ((size_t)8)
270 
271 /* The terminal is thought of in terms of X columns by Y lines. In the cases
272  * where a wide character takes up more than one column, the adjacent
273  * occupied column entries will contain this faux character. */
274 #define MB_FILL_CHAR ((Char)-1)
275 
276 /* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn
277  * style visual expansions. */
278 protected int ct_visual_width(Char);
279 
280 /* Turn the given character into the appropriate visual format, matching
281  * the width given by ct_visual_width(). Returns the number of characters used
282  * up, or -1 if insufficient space. Buffer length is in count of Char's. */
283 protected ssize_t ct_visual_char(Char *, size_t, Char);
284 
285 /* Convert the given string into visual format, using the ct_visual_char()
286  * function. Uses a static buffer, so not threadsafe. */
287 protected const Char *ct_visual_string(const Char *);
288 
289 
290 /* printable character, use ct_visual_width() to find out display width */
291 #define CHTYPE_PRINT        ( 0)
292 /* control character found inside the ASCII portion of the charset */
293 #define CHTYPE_ASCIICTL     (-1)
294 /* a \t */
295 #define CHTYPE_TAB          (-2)
296 /* a \n */
297 #define CHTYPE_NL           (-3)
298 /* non-printable character */
299 #define CHTYPE_NONPRINT     (-4)
300 /* classification of character c, as one of the above defines */
301 protected int ct_chr_class(Char c);
302 #endif
303 
304 
305 #endif /* _chartype_f */
306