1 /* Copyright (C) 1991,92,93,95,96,97,98,99,2001,02
2 	Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 /*
20  *	ISO C99 Standard 7.4: Character handling	<ctype.h>
21  */
22 
23 #ifndef	_CTYPE_H
24 #define	_CTYPE_H	1
25 
26 #include <features.h>
27 #include <bits/types.h>
28 
29 __BEGIN_DECLS
30 __BEGIN_NAMESPACE_STD
31 
32 /* The following names are all functions:
33      int isCHARACTERISTIC(int c);
34    which return nonzero iff C has CHARACTERISTIC.
35    For the meaning of the characteristic names, see the `enum' above.  */
36 extern int isalnum(int __c) __THROW;
37 libc_hidden_proto(isalnum)
38 extern int isalpha(int __c) __THROW;
39 libc_hidden_proto(isalpha)
40 extern int iscntrl(int __c) __THROW;
41 libc_hidden_proto(iscntrl)
42 extern int isdigit(int __c) __THROW;
43 libc_hidden_proto(isdigit)
44 extern int islower(int __c) __THROW;
45 libc_hidden_proto(islower)
46 extern int isgraph(int __c) __THROW;
47 libc_hidden_proto(isgraph)
48 extern int isprint(int __c) __THROW;
49 libc_hidden_proto(isprint)
50 extern int ispunct(int __c) __THROW;
51 libc_hidden_proto(ispunct)
52 extern int isspace(int __c) __THROW;
53 libc_hidden_proto(isspace)
54 extern int isupper(int __c) __THROW;
55 libc_hidden_proto(isupper)
56 extern int isxdigit(int __c) __THROW;
57 libc_hidden_proto(isxdigit)
58 
59 
60 /* Return the lowercase version of C.  */
61 extern int tolower(int __c) __THROW;
62 libc_hidden_proto(tolower)
63 
64 /* Return the uppercase version of C.  */
65 extern int toupper(int __c) __THROW;
66 libc_hidden_proto(toupper)
67 
68 #if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) && \
69 	defined __UCLIBC_SUSV4_LEGACY__
70 /* Return nonzero iff C is in the ASCII set
71    (i.e., is no more than 7 bits wide).  */
72 extern int isascii(int __c) __THROW;
73 libc_hidden_proto(isascii)
74 /* Return the part of C that is in the ASCII set
75    (i.e., the low-order 7 bits of C).  */
76 extern int toascii(int __c) __THROW;
77 #endif
78 
79 __END_NAMESPACE_STD
80 
81 
82 /* ISO C99 introduced one new function.  */
83 #ifdef	__USE_ISOC99
84 __BEGIN_NAMESPACE_C99
85 
86 extern int isblank(int __c) __THROW;
87 libc_hidden_proto(isblank)
88 
89 __END_NAMESPACE_C99
90 #endif
91 __END_DECLS
92 
93 #ifndef __UCLIBC_HAS_CTYPE_TABLES__
94 
95 /* "Stub locale": we are permanently in C/POSIX locale.
96  * Using simple(r) ctype.h machinery in this header instead: */
97 #include <bits/uClibc_ctype.h>
98 
99 #else
100 
101 __BEGIN_DECLS
102 
103 #ifndef _ISbit
104 /* These are all the characteristics of characters.
105    If there get to be more than 16 distinct characteristics,
106    __ctype_mask_t will need to be adjusted. */
107 
108 /* libstdc++ from gcc toolchain needs this macro. */
109 # define _ISbit(bit)	(1 << (bit))
110 
111 enum
112 {
113   _ISupper = _ISbit (0),	/* UPPERCASE.  */
114   _ISlower = _ISbit (1),	/* lowercase.  */
115   _ISalpha = _ISbit (2),	/* Alphabetic.  */
116   _ISdigit = _ISbit (3),	/* Numeric.  */
117   _ISxdigit = _ISbit (4),	/* Hexadecimal numeric.  */
118   _ISspace = _ISbit (5),	/* Whitespace.  */
119   _ISprint = _ISbit (6),	/* Printing.  */
120   _ISgraph = _ISbit (7),	/* Graphical.  */
121   _ISblank = _ISbit (8),	/* Blank (usually SPC and TAB).  */
122   _IScntrl = _ISbit (9),	/* Control character.  */
123   _ISpunct = _ISbit (10),	/* Punctuation.  */
124   _ISalnum = _ISbit (11)	/* Alphanumeric.  */
125 };
126 #else
127 #error _ISbit already defined!
128 #endif /* ! _ISbit  */
129 
130 /* __ctype_XXX_t types and __UCLIBC_CTYPE_x_TBL_OFFSET constants */
131 #include <bits/uClibc_touplow.h>
132 
133 #ifdef __UCLIBC_HAS_CTYPE_SIGNED__
134 # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)((c) + 128)) < 384)
135 #else
136 # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)(c)) < 256)
137 #endif
138 
139 /* In the thread-specific locale model (see `uselocale' in <locale.h>)
140    we cannot use global variables for these as was done in the past.
141    Instead, the following accessor functions return the address of
142    each variable, which is local to the current thread if multithreaded.
143 
144    These point into arrays of 384, so they can be indexed by any `unsigned
145    char' value [0,255]; by EOF (-1); or by any `signed char' value
146    [-128,-1).  ISO C requires that the ctype functions work for `unsigned
147    char' values and for EOF; we also support negative `signed char' values
148    for broken old programs.  The case conversion arrays are of `int's
149    rather than `unsigned char's because tolower (EOF) must be EOF, which
150    doesn't fit into an `unsigned char'.  But today more important is that
151    the arrays are also used for multi-byte character sets.  */
152 
153 /* uClibc differences:
154  *
155  * When __UCLIBC_HAS_CTYPE_SIGNED is defined,
156  *
157  *    The upper and lower mapping arrays are type int16_t, so that
158  *    they may store all char values plus EOF.  The glibc reasoning
159  *    given above for these being type int is questionable, as the
160  *    ctype mapping functions map from the set of (unsigned) char
161  *    and EOF back into the set.  They have no awareness of multi-byte
162  *    or wide characters.
163  *
164  * Otherwise,
165  *
166  *    The ctype array is defined for -1..255.
167  *    The upper and lower mapping arrays are defined for 0..255.
168  *    The upper and lower mapping arrays are type unsigned char.
169  */
170 
171 /* Pointers to the default C-locale data. */
172 extern const __ctype_mask_t *__C_ctype_b;
173 libc_hidden_proto(__C_ctype_b)
174 extern const __ctype_touplow_t *__C_ctype_toupper;
175 libc_hidden_proto(__C_ctype_toupper)
176 extern const __ctype_touplow_t *__C_ctype_tolower;
177 libc_hidden_proto(__C_ctype_tolower)
178 
179 #ifdef __UCLIBC_HAS_XLOCALE__
180 
181 const __ctype_mask_t **__ctype_b_loc(void) __attribute__ ((const));
182 libc_hidden_proto(__ctype_b_loc)
183 const __ctype_touplow_t **__ctype_tolower_loc(void) __attribute__ ((const));
184 libc_hidden_proto(__ctype_tolower_loc)
185 const __ctype_touplow_t **__ctype_toupper_loc(void) __attribute__ ((const));
186 libc_hidden_proto(__ctype_toupper_loc)
187 #define __UCLIBC_CTYPE_B	(*__ctype_b_loc())
188 #define __UCLIBC_CTYPE_TOLOWER	(*__ctype_tolower_loc())
189 #define __UCLIBC_CTYPE_TOUPPER	(*__ctype_toupper_loc())
190 
191 #else  /* __UCLIBC_HAS_XLOCALE__ */
192 
193 /* Pointers to the current global locale data in use. */
194 extern const __ctype_mask_t *__ctype_b;
195 libc_hidden_proto(__ctype_b)
196 extern const __ctype_touplow_t *__ctype_toupper;
197 libc_hidden_proto(__ctype_toupper)
198 extern const __ctype_touplow_t *__ctype_tolower;
199 libc_hidden_proto(__ctype_tolower)
200 #define __UCLIBC_CTYPE_B	(__ctype_b)
201 #define __UCLIBC_CTYPE_TOLOWER	(__ctype_tolower)
202 #define __UCLIBC_CTYPE_TOUPPER	(__ctype_toupper)
203 
204 #endif /* __UCLIBC_HAS_XLOCALE__ */
205 
206 #ifdef __UCLIBC_SUSV4_LEGACY__
207 #define	__isascii(c)	(((c) & ~0x7f) == 0)	/* If C is a 7 bit value.  */
208 #define	__toascii(c)	((c) & 0x7f)		/* Mask off high bits.  */
209 #endif
210 
211 #ifdef _LIBC
212 /* These are uClibc-specific. */
213 #define __isdigit_char(C)    (((unsigned char)((C) - '0')) <= 9)
214 #define __isdigit_int(C)     (((unsigned int)((C) - '0')) <= 9)
215 #endif
216 
217 #ifdef __USE_GNU
218 /* Test C for a set of character classes according to MASK.  */
219 extern int isctype(int __c, int __mask) __THROW;
220 #endif
221 
222 #if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
223 	&& defined __UCLIBC_SUSV4_LEGACY__
224 /* These are the same as `toupper' and `tolower' except that they do not
225    check the argument for being in the range of a `char'.  */
226 extern int _toupper(int __c) __THROW;
227 extern int _tolower(int __c) __THROW;
228 #endif /* Use SVID or use misc.  */
229 
230 /* This code is needed for the optimized mapping functions.  */
231 #define __tobody(c, f, table, args) \
232 (__extension__ ({ \
233 	int __res; \
234 	if (sizeof(c) > 1) { \
235 		if (__builtin_constant_p(c)) { \
236 			int __c = (c); \
237 			__res = __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (table)[__c] : __c; \
238 		} else \
239 			__res = f args; \
240 	} else \
241 		__res = (table)[(int) (c)]; \
242 	__res; \
243 }))
244 
245 #define __isctype(c, type) ((__UCLIBC_CTYPE_B)[(int)(c)] & (__ctype_mask_t)type)
246 /* Do not combine in one #if - unifdef tool is not that clever */
247 #ifndef __NO_CTYPE
248 #ifndef __cplusplus
249 # define isalnum(c)	__isctype((c), _ISalnum)
250 # define isalpha(c)	__isctype((c), _ISalpha)
251 # define iscntrl(c)	__isctype((c), _IScntrl)
252 # define isdigit(c)	__isctype((c), _ISdigit)
253 # define islower(c)	__isctype((c), _ISlower)
254 # define isgraph(c)	__isctype((c), _ISgraph)
255 # define isprint(c)	__isctype((c), _ISprint)
256 # define ispunct(c)	__isctype((c), _ISpunct)
257 # define isspace(c)	__isctype((c), _ISspace)
258 # define isupper(c)	__isctype((c), _ISupper)
259 # define isxdigit(c)	__isctype((c), _ISxdigit)
260 # ifdef __USE_ISOC99
261 #  define isblank(c)	__isctype((c), _ISblank)
262 # endif
263 
264 # ifdef __USE_EXTERN_INLINES
265 __extern_inline int
266 __NTH (tolower (int __c))
267 {
268   return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOLOWER)[__c] : __c;
269 }
270 __extern_inline int
271 __NTH (toupper (int __c))
272 {
273   return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOUPPER)[__c] : __c;
274 }
275 # endif
276 
277 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
278 #  define tolower(c)	__tobody(c, tolower, __UCLIBC_CTYPE_TOLOWER, (c))
279 #  define toupper(c)	__tobody(c, toupper, __UCLIBC_CTYPE_TOUPPER, (c))
280 # endif /* Optimizing gcc */
281 
282 # if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
283 	&& defined __UCLIBC_SUSV4_LEGACY__
284 #  define isascii(c)	__isascii (c)
285 #  define toascii(c)	__toascii (c)
286 #  define _tolower(c)	((int) (__UCLIBC_CTYPE_TOLOWER)[(int) (c)])
287 #  define _toupper(c)	((int) (__UCLIBC_CTYPE_TOUPPER)[(int) (c)])
288 # endif
289 
290 #endif /* not __cplusplus */
291 #endif /* not __NO_CTYPE */
292 
293 
294 #if defined __USE_GNU && defined __UCLIBC_HAS_XLOCALE__
295 /* The concept of one static locale per category is not very well
296    thought out.  Many applications will need to process its data using
297    information from several different locales.  Another application is
298    the implementation of the internationalization handling in the
299    upcoming ISO C++ standard library.  To support this another set of
300    the functions using locale data exist which have an additional
301    argument.
302 
303    Attention: all these functions are *not* standardized in any form.
304    This is a proof-of-concept implementation.  */
305 
306 /* Structure for reentrant locale using functions.  This is an
307    (almost) opaque type for the user level programs.  */
308 # include <xlocale.h>
309 
310 /* These definitions are similar to the ones above but all functions
311    take as an argument a handle for the locale which shall be used.  */
312 extern int isalnum_l(int, __locale_t) __THROW;
313 libc_hidden_proto(isalnum_l)
314 extern int isalpha_l(int, __locale_t) __THROW;
315 libc_hidden_proto(isalpha_l)
316 extern int iscntrl_l(int, __locale_t) __THROW;
317 libc_hidden_proto(iscntrl_l)
318 extern int isdigit_l(int, __locale_t) __THROW;
319 libc_hidden_proto(isdigit_l)
320 extern int islower_l(int, __locale_t) __THROW;
321 libc_hidden_proto(islower_l)
322 extern int isgraph_l(int, __locale_t) __THROW;
323 libc_hidden_proto(isgraph_l)
324 extern int isprint_l(int, __locale_t) __THROW;
325 libc_hidden_proto(isprint_l)
326 extern int ispunct_l(int, __locale_t) __THROW;
327 libc_hidden_proto(ispunct_l)
328 extern int isspace_l(int, __locale_t) __THROW;
329 libc_hidden_proto(isspace_l)
330 extern int isupper_l(int, __locale_t) __THROW;
331 libc_hidden_proto(isupper_l)
332 extern int isxdigit_l(int, __locale_t) __THROW;
333 libc_hidden_proto(isxdigit_l)
334 extern int isblank_l(int, __locale_t) __THROW;
335 libc_hidden_proto(isblank_l)
336 
337 # if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
338 	&& defined __UCLIBC_SUSV4_LEGACY__
339 /* Return nonzero iff C is in the ASCII set
340    (i.e., is no more than 7 bits wide).  */
341 extern int isascii_l (int __c) __THROW;
342 libc_hidden_proto(isascii_l)
343 
344 # endif
345 
346 /* Return the lowercase version of C in locale L.  */
347 extern int tolower_l (int __c, __locale_t __l) __THROW;
348 libc_hidden_proto(tolower_l)
349 
350 /* Return the uppercase version of C.  */
351 extern int toupper_l (int __c, __locale_t __l) __THROW;
352 
353 # define __isctype_l(c, type, locale) ((locale)->__ctype_b[(int) (c)] & (__ctype_mask_t) type)
354 # ifndef __NO_CTYPE
355 #  if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
356 	&& defined __UCLIBC_SUSV4_LEGACY__
357 #   define __isascii_l(c,l)	((l), __isascii (c))
358 #   define __toascii_l(c,l)	((l), __toascii (c))
359 #  endif
360 
361 #  if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
362 	&& defined __UCLIBC_SUSV4_LEGACY__
363 #   define isascii_l(c,l)	__isascii_l ((c), (l))
364 #   define toascii_l(c,l)	__toascii_l ((c), (l))
365 #  endif
366 
367 # endif /* Not __NO_CTYPE.  */
368 
369 #endif /* Use GNU.  */
370 
371 __END_DECLS
372 
373 #endif /* __UCLIBC_HAS_CTYPE_TABLES__ */
374 
375 #if defined _LIBC && !defined __UCLIBC_SUSV4_LEGACY__
376 /* We define {__,}isascii for internal use only */
377 # define __isascii(c) (((c) & ~0x7f) == 0)
378 # define isascii(c) __isascii (c)
379 #endif
380 
381 #endif /* ctype.h  */
382