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