xref: /dragonfly/contrib/libedit/src/chartype.h (revision 82730a9c)
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 /* 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 
48 #if 0 /* DragonFly wchar supports ISO 10646 */
49 #if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__))
50 #ifndef __STDC_ISO_10646__
51 /* In many places it is assumed that the first 127 code points are ASCII
52  * compatible, so ensure wchar_t indeed does ISO 10646 and not some other
53  * funky encoding that could break us in weird and wonderful ways. */
54 	#error wchar_t must store ISO 10646 characters
55 #endif
56 #endif
57 #endif
58 
59 /* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it...
60  * ref: ISO/IEC DTR 19769
61  */
62 #if WCHAR_MAX < INT32_MAX
63 #warning Build environment does not support non-BMP characters
64 #endif
65 
66 #ifndef HAVE_WCSDUP
67 wchar_t *wcsdup(const wchar_t *s);
68 #endif
69 
70 #define ct_mbtowc            mbtowc
71 #define ct_mbtowc_reset      mbtowc(0,0,(size_t)0)
72 #define ct_wctomb            wctomb
73 #define ct_wctomb_reset      wctomb(0,0)
74 #define ct_wcstombs          wcstombs
75 #define ct_mbstowcs          mbstowcs
76 
77 #define Char			wchar_t
78 #define Int			wint_t
79 #define FUN(prefix,rest)	prefix ## _w ## rest
80 #define FUNW(type)		type ## _w
81 #define TYPE(type)		type ## W
82 #define FSTR			"%ls"
83 #define STR(x) 			L ## x
84 #define UC(c)			c
85 #define Isalpha(x)  iswalpha(x)
86 #define Isalnum(x)  iswalnum(x)
87 #define Isgraph(x)  iswgraph(x)
88 #define Isspace(x)  iswspace(x)
89 #define Isdigit(x)  iswdigit(x)
90 #define Iscntrl(x)  iswcntrl(x)
91 #define Isprint(x)  iswprint(x)
92 
93 #define Isupper(x)  iswupper(x)
94 #define Islower(x)  iswlower(x)
95 #define Toupper(x)  towupper(x)
96 #define Tolower(x)  towlower(x)
97 
98 #define IsASCII(x)  (x < 0x100)
99 
100 #define Strlen(x)       wcslen(x)
101 #define Strchr(s,c)     wcschr(s,c)
102 #define Strrchr(s,c)    wcsrchr(s,c)
103 #define Strstr(s,v)     wcsstr(s,v)
104 #define Strdup(x)       wcsdup(x)
105 #define Strcpy(d,s)     wcscpy(d,s)
106 #define Strncpy(d,s,n)  wcsncpy(d,s,n)
107 #define Strncat(d,s,n)  wcsncat(d,s,n)
108 
109 #define Strcmp(s,v)     wcscmp(s,v)
110 #define Strncmp(s,v,n)  wcsncmp(s,v,n)
111 #define Strcspn(s,r)    wcscspn(s,r)
112 
113 #define Strtol(p,e,b)   wcstol(p,e,b)
114 
115 static inline int
116 Width(wchar_t c)
117 {
118 	int w = wcwidth(c);
119 	return w < 0 ? 0 : w;
120 }
121 
122 #else /* NARROW */
123 
124 #define ct_mbtowc            error
125 #define ct_mbtowc_reset
126 #define ct_wctomb            error
127 #define ct_wctomb_reset
128 #define ct_wcstombs(a, b, c)    (strncpy(a, b, c), strlen(a))
129 #define ct_mbstowcs(a, b, c)    (strncpy(a, b, c), strlen(a))
130 
131 #define Char			char
132 #define Int			int
133 #define FUN(prefix,rest)	prefix ## _ ## rest
134 #define FUNW(type)		type
135 #define TYPE(type)		type
136 #define FSTR			"%s"
137 #define STR(x) 			x
138 #define UC(c)			(unsigned char)(c)
139 
140 #define Isalpha(x)  isalpha((unsigned char)x)
141 #define Isalnum(x)  isalnum((unsigned char)x)
142 #define Isgraph(x)  isgraph((unsigned char)x)
143 #define Isspace(x)  isspace((unsigned char)x)
144 #define Isdigit(x)  isdigit((unsigned char)x)
145 #define Iscntrl(x)  iscntrl((unsigned char)x)
146 #define Isprint(x)  isprint((unsigned char)x)
147 
148 #define Isupper(x)  isupper((unsigned char)x)
149 #define Islower(x)  islower((unsigned char)x)
150 #define Toupper(x)  toupper((unsigned char)x)
151 #define Tolower(x)  tolower((unsigned char)x)
152 
153 #define IsASCII(x)  isascii((unsigned char)x)
154 
155 #define Strlen(x)       strlen(x)
156 #define Strchr(s,c)     strchr(s,c)
157 #define Strrchr(s,c)    strrchr(s,c)
158 #define Strstr(s,v)     strstr(s,v)
159 #define Strdup(x)       strdup(x)
160 #define Strcpy(d,s)     strcpy(d,s)
161 #define Strncpy(d,s,n)  strncpy(d,s,n)
162 #define Strncat(d,s,n)  strncat(d,s,n)
163 
164 #define Strcmp(s,v)     strcmp(s,v)
165 #define Strncmp(s,v,n)  strncmp(s,v,n)
166 #define Strcspn(s,r)    strcspn(s,r)
167 
168 #define Strtol(p,e,b)   strtol(p,e,b)
169 
170 #define Width(c)	1
171 
172 #endif
173 
174 
175 #ifdef WIDECHAR
176 /*
177  * Conversion buffer
178  */
179 typedef struct ct_buffer_t {
180         char    *cbuff;
181         size_t  csize;
182         Char *wbuff;
183         size_t  wsize;
184 } ct_buffer_t;
185 
186 #define ct_encode_string __ct_encode_string
187 /* Encode a wide-character string and return the UTF-8 encoded result. */
188 public char *ct_encode_string(const Char *, ct_buffer_t *);
189 
190 #define ct_decode_string __ct_decode_string
191 /* Decode a (multi)?byte string and return the wide-character string result. */
192 public Char *ct_decode_string(const char *, ct_buffer_t *);
193 
194 /* Decode a (multi)?byte argv string array.
195  * The pointer returned must be free()d when done. */
196 protected Char **ct_decode_argv(int, const char *[],  ct_buffer_t *);
197 
198 /* Resizes the conversion buffer(s) if needed. */
199 protected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t);
200 protected ssize_t ct_encode_char(char *, size_t, Char);
201 protected size_t ct_enc_width(Char);
202 
203 #define ct_free_argv(s)	el_free(s)
204 
205 #else
206 #define	ct_encode_string(s, b)	(s)
207 #define ct_decode_string(s, b)	(s)
208 #define ct_decode_argv(l, s, b)	(s)
209 #define ct_conv_buff_resize(b, os, ns)
210 #define ct_encode_char(d, l, s)	(*d = s, 1)
211 #define ct_free_argv(s)
212 #endif
213 
214 #ifndef NARROWCHAR
215 /* Encode a characted into the destination buffer, provided there is sufficent
216  * buffer space available. Returns the number of bytes used up (zero if the
217  * character cannot be encoded, -1 if there was not enough space available). */
218 
219 /* The maximum buffer size to hold the most unwieldly visual representation,
220  * in this case \U+nnnnn. */
221 #define VISUAL_WIDTH_MAX ((size_t)8)
222 
223 /* The terminal is thought of in terms of X columns by Y lines. In the cases
224  * where a wide character takes up more than one column, the adjacent
225  * occupied column entries will contain this faux character. */
226 #define MB_FILL_CHAR ((Char)-1)
227 
228 /* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn
229  * style visual expansions. */
230 protected int ct_visual_width(Char);
231 
232 /* Turn the given character into the appropriate visual format, matching
233  * the width given by ct_visual_width(). Returns the number of characters used
234  * up, or -1 if insufficient space. Buffer length is in count of Char's. */
235 protected ssize_t ct_visual_char(Char *, size_t, Char);
236 
237 /* Convert the given string into visual format, using the ct_visual_char()
238  * function. Uses a static buffer, so not threadsafe. */
239 protected const Char *ct_visual_string(const Char *);
240 
241 
242 /* printable character, use ct_visual_width() to find out display width */
243 #define CHTYPE_PRINT        ( 0)
244 /* control character found inside the ASCII portion of the charset */
245 #define CHTYPE_ASCIICTL     (-1)
246 /* a \t */
247 #define CHTYPE_TAB          (-2)
248 /* a \n */
249 #define CHTYPE_NL           (-3)
250 /* non-printable character */
251 #define CHTYPE_NONPRINT     (-4)
252 /* classification of character c, as one of the above defines */
253 protected int ct_chr_class(Char c);
254 #endif
255 
256 
257 #endif /* _chartype_f */
258