1/*	$NetBSD: chartype.h,v 1.6 2010/04/20 02:01:13 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 * $FreeBSD$
36 */
37
38#ifndef _h_chartype_f
39#define _h_chartype_f
40
41
42
43#ifdef WIDECHAR
44
45/* Ideally we should also test the value of the define to see if it
46 * supports non-BMP code points without requiring UTF-16, but nothing
47 * seems to actually advertise this properly, despite Unicode 3.1 having
48 * been around since 2001... */
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
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,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 FSTR			"%ls"
78#define STR(x) 			L ## x
79#define UC(c)			c
80#define Isalpha(x)  iswalpha(x)
81#define Isalnum(x)  iswalnum(x)
82#define Isgraph(x)  iswgraph(x)
83#define Isspace(x)  iswspace(x)
84#define Isdigit(x)  iswdigit(x)
85#define Iscntrl(x)  iswcntrl(x)
86#define Isprint(x)  iswprint(x)
87
88#define Isupper(x)  iswupper(x)
89#define Islower(x)  iswlower(x)
90#define Toupper(x)  towupper(x)
91#define Tolower(x)  towlower(x)
92
93#define IsASCII(x)  (x < 0x100)
94
95#define Strlen(x)       wcslen(x)
96#define Strchr(s,c)     wcschr(s,c)
97#define Strrchr(s,c)    wcsrchr(s,c)
98#define Strstr(s,v)     wcsstr(s,v)
99#define Strdup(x)       wcsdup(x)
100#define Strcpy(d,s)     wcscpy(d,s)
101#define Strncpy(d,s,n)  wcsncpy(d,s,n)
102#define Strncat(d,s,n)  wcsncat(d,s,n)
103
104#define Strcmp(s,v)     wcscmp(s,v)
105#define Strncmp(s,v,n)  wcsncmp(s,v,n)
106#define Strcspn(s,r)    wcscspn(s,r)
107
108#define Strtol(p,e,b)   wcstol(p,e,b)
109
110#define Width(c)	wcwidth(c)
111
112#else /* NARROW */
113
114#define ct_mbtowc            error
115#define ct_mbtowc_reset
116#define ct_wctomb            error
117#define ct_wctomb_reset
118#define ct_wcstombs(a, b, c)    (strncpy(a, b, c), strlen(a))
119#define ct_mbstowcs(a, b, c)    (strncpy(a, b, c), strlen(a))
120
121#define Char			char
122#define Int			int
123#define FUN(prefix,rest)	prefix ## _ ## rest
124#define FUNW(type)		type
125#define TYPE(type)		type
126#define FSTR			"%s"
127#define STR(x) 			x
128#define UC(c)			(unsigned char)(c)
129
130#define Isalpha(x)  isalpha((unsigned char)x)
131#define Isalnum(x)  isalnum((unsigned char)x)
132#define Isgraph(x)  isgraph((unsigned char)x)
133#define Isspace(x)  isspace((unsigned char)x)
134#define Isdigit(x)  isdigit((unsigned char)x)
135#define Iscntrl(x)  iscntrl((unsigned char)x)
136#define Isprint(x)  isprint((unsigned char)x)
137
138#define Isupper(x)  isupper((unsigned char)x)
139#define Islower(x)  islower((unsigned char)x)
140#define Toupper(x)  toupper((unsigned char)x)
141#define Tolower(x)  tolower((unsigned char)x)
142
143#define IsASCII(x)  isascii((unsigned char)x)
144
145#define Strlen(x)       strlen(x)
146#define Strchr(s,c)     strchr(s,c)
147#define Strrchr(s,c)    strrchr(s,c)
148#define Strstr(s,v)     strstr(s,v)
149#define Strdup(x)       strdup(x)
150#define Strcpy(d,s)     strcpy(d,s)
151#define Strncpy(d,s,n)  strncpy(d,s,n)
152#define Strncat(d,s,n)  strncat(d,s,n)
153
154#define Strcmp(s,v)     strcmp(s,v)
155#define Strncmp(s,v,n)  strncmp(s,v,n)
156#define Strcspn(s,r)    strcspn(s,r)
157
158#define Strtol(p,e,b)   strtol(p,e,b)
159
160#define Width(c)	1
161
162#endif
163
164
165#ifdef WIDECHAR
166/*
167 * Conversion buffer
168 */
169typedef struct ct_buffer_t {
170        char    *cbuff;
171        size_t  csize;
172        Char *wbuff;
173        size_t  wsize;
174} ct_buffer_t;
175
176#define ct_encode_string __ct_encode_string
177/* Encode a wide character string and return the UTF-8 encoded result. */
178public char *ct_encode_string(const Char *, ct_buffer_t *);
179
180#define ct_decode_string __ct_decode_string
181/* Decode a (multi)?byte string and return the wide character string result. */
182public Char *ct_decode_string(const char *, ct_buffer_t *);
183
184/* Decode a (multi)?byte argv string array.
185 * The pointer returned must be free()d when done. */
186protected Char **ct_decode_argv(int, const char *[],  ct_buffer_t *);
187
188/* Resizes the conversion buffer(s) if needed. */
189protected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t);
190protected ssize_t ct_encode_char(char *, size_t, Char);
191protected size_t ct_enc_width(Char);
192
193#define ct_free_argv(s)	el_free(s)
194
195#else
196#define	ct_encode_string(s, b)	(s)
197#define ct_decode_string(s, b)	(s)
198#define ct_decode_argv(l, s, b)	(s)
199#define ct_conv_buff_resize(b, os, ns)
200#define ct_encode_char(d, l, s)	(*d = s, 1)
201#define ct_free_argv(s)
202#endif
203
204#ifndef NARROWCHAR
205/* Encode a characted into the destination buffer, provided there is sufficent
206 * buffer space available. Returns the number of bytes used up (zero if the
207 * character cannot be encoded, -1 if there was not enough space available). */
208
209/* The maximum buffer size to hold the most unwieldly visual representation,
210 * in this case \U+nnnnn. */
211#define VISUAL_WIDTH_MAX 8
212
213/* The terminal is thought of in terms of X columns by Y lines. In the cases
214 * where a wide character takes up more than one column, the adjacent
215 * occupied column entries will contain this faux character. */
216#define MB_FILL_CHAR ((Char)-1)
217
218/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn
219 * style visual expansions. */
220protected int ct_visual_width(Char);
221
222/* Turn the given character into the appropriate visual format, matching
223 * the width given by ct_visual_width(). Returns the number of characters used
224 * up, or -1 if insufficient space. Buffer length is in count of Char's. */
225protected ssize_t ct_visual_char(Char *, size_t, Char);
226
227/* Convert the given string into visual format, using the ct_visual_char()
228 * function. Uses a static buffer, so not threadsafe. */
229protected const Char *ct_visual_string(const Char *);
230
231
232/* printable character, use ct_visual_width() to find out display width */
233#define CHTYPE_PRINT        ( 0)
234/* control character found inside the ASCII portion of the charset */
235#define CHTYPE_ASCIICTL     (-1)
236/* a \t */
237#define CHTYPE_TAB          (-2)
238/* a \n */
239#define CHTYPE_NL           (-3)
240/* non-printable character */
241#define CHTYPE_NONPRINT     (-4)
242/* classification of character c, as one of the above defines */
243protected int ct_chr_class(Char c);
244#endif
245
246
247#endif /* _chartype_f */
248