1/*
2 * Copyright (c) 2000, 2005, 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright (c) 1989, 1993
25 *	The Regents of the University of California.  All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * This code is derived from software contributed to Berkeley by
33 * Paul Borman at Krystal Technologies.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 *    notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 *    notice, this list of conditions and the following disclaimer in the
42 *    documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 *    must display the following acknowledgement:
45 *	This product includes software developed by the University of
46 *	California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 *    may be used to endorse or promote products derived from this software
49 *    without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 *	@(#)ctype.h	8.4 (Berkeley) 1/21/94
64 */
65
66//Begin-Libc
67#include "xlocale_private.h"
68//End-Libc
69#ifndef	_CTYPE_H_
70#define _CTYPE_H_
71
72#include <sys/cdefs.h>
73#include <runetype.h>
74
75#define	_CTYPE_A	0x00000100L		/* Alpha */
76#define	_CTYPE_C	0x00000200L		/* Control */
77#define	_CTYPE_D	0x00000400L		/* Digit */
78#define	_CTYPE_G	0x00000800L		/* Graph */
79#define	_CTYPE_L	0x00001000L		/* Lower */
80#define	_CTYPE_P	0x00002000L		/* Punct */
81#define	_CTYPE_S	0x00004000L		/* Space */
82#define	_CTYPE_U	0x00008000L		/* Upper */
83#define	_CTYPE_X	0x00010000L		/* X digit */
84#define	_CTYPE_B	0x00020000L		/* Blank */
85#define	_CTYPE_R	0x00040000L		/* Print */
86#define	_CTYPE_I	0x00080000L		/* Ideogram */
87#define	_CTYPE_T	0x00100000L		/* Special */
88#define	_CTYPE_Q	0x00200000L		/* Phonogram */
89#define	_CTYPE_SW0	0x20000000L		/* 0 width character */
90#define	_CTYPE_SW1	0x40000000L		/* 1 width character */
91#define	_CTYPE_SW2	0x80000000L		/* 2 width character */
92#define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
93#define	_CTYPE_SWM	0xe0000000L		/* Mask for screen width data */
94#define	_CTYPE_SWS	30			/* Bits to shift to get width */
95
96#ifdef _NONSTD_SOURCE
97/*
98 * Backward compatibility
99 */
100#define	_A		_CTYPE_A		/* Alpha */
101#define	_C		_CTYPE_C		/* Control */
102#define	_D		_CTYPE_D		/* Digit */
103#define	_G		_CTYPE_G		/* Graph */
104#define	_L		_CTYPE_L		/* Lower */
105#define	_P		_CTYPE_P		/* Punct */
106#define	_S		_CTYPE_S		/* Space */
107#define	_U		_CTYPE_U		/* Upper */
108#define	_X		_CTYPE_X		/* X digit */
109#define	_B		_CTYPE_B		/* Blank */
110#define	_R		_CTYPE_R		/* Print */
111#define	_I		_CTYPE_I		/* Ideogram */
112#define	_T		_CTYPE_T		/* Special */
113#define	_Q		_CTYPE_Q		/* Phonogram */
114#define	_SW0		_CTYPE_SW0		/* 0 width character */
115#define	_SW1		_CTYPE_SW1		/* 1 width character */
116#define	_SW2		_CTYPE_SW2		/* 2 width character */
117#define	_SW3		_CTYPE_SW3		/* 3 width character */
118#endif /* _NONSTD_SOURCE */
119
120//Begin-Libc
121/*
122 * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
123 * to generate code for extern versions of all intermediate inline functions.
124 */
125#ifdef _EXTERNALIZE_CTYPE_INLINES_
126#define _USE_CTYPE_INLINE_
127#define __DARWIN_CTYPE_inline
128#else /* !_EXTERNALIZE_CTYPE_INLINES_ */
129//End-Libc
130#define __DARWIN_CTYPE_inline		__header_inline
131//Begin-Libc
132#endif /* !_EXTERNALIZE_CTYPE_INLINES_ */
133//End-Libc
134
135//Begin-Libc
136/*
137 * _EXTERNALIZE_CTYPE_INLINES_TOP_ is defined in locale/isctype.c to tell us
138 * to generate code for extern versions of all top-level inline functions.
139 */
140#ifdef _EXTERNALIZE_CTYPE_INLINES_TOP_
141#define _USE_CTYPE_INLINE_
142#define __DARWIN_CTYPE_TOP_inline
143#else /* !_EXTERNALIZE_CTYPE_INLINES_TOP_ */
144//End-Libc
145#define __DARWIN_CTYPE_TOP_inline	__header_inline
146//Begin-Libc
147#endif /* _EXTERNALIZE_CTYPE_INLINES_TOP_ */
148//End-Libc
149
150/*
151 * Use inline functions if we are allowed to and the compiler supports them.
152 */
153#if !defined(_DONT_USE_CTYPE_INLINE_) && \
154    (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
155
156/* See comments in <machine/_type.h> about __darwin_ct_rune_t. */
157__BEGIN_DECLS
158unsigned long		___runetype(__darwin_ct_rune_t);
159__darwin_ct_rune_t	___tolower(__darwin_ct_rune_t);
160__darwin_ct_rune_t	___toupper(__darwin_ct_rune_t);
161__END_DECLS
162
163__DARWIN_CTYPE_TOP_inline int
164isascii(int _c)
165{
166	return ((_c & ~0x7F) == 0);
167}
168
169#ifdef USE_ASCII
170__DARWIN_CTYPE_inline int
171__maskrune(__darwin_ct_rune_t _c, unsigned long _f)
172{
173	return (int)_DefaultRuneLocale.__runetype[_c & 0xff] & (__uint32_t)_f;
174}
175//Begin-Libc
176#elif defined(__LIBC__)
177__DARWIN_CTYPE_inline int
178__maskrune(__darwin_ct_rune_t _c, unsigned long _f)
179{
180	/* _CurrentRuneLocale.__runetype[_c] is __uint32_t
181	 * _f is unsigned long
182	 * ___runetype(_c) is unsigned long
183	 * retval is int
184	 */
185	return (int)((_c < 0 || _c >= _CACHED_RUNES) ? (__uint32_t)___runetype(_c) :
186		__current_locale()->__lc_ctype->_CurrentRuneLocale.__runetype[_c]) & (__uint32_t)_f;
187}
188//End-Libc
189#else /* !USE_ASCII */
190__BEGIN_DECLS
191int             	__maskrune(__darwin_ct_rune_t, unsigned long);
192__END_DECLS
193#endif /* USE_ASCII */
194
195__DARWIN_CTYPE_inline int
196__istype(__darwin_ct_rune_t _c, unsigned long _f)
197{
198#ifdef USE_ASCII
199	return !!(__maskrune(_c, _f));
200#else /* USE_ASCII */
201	return (isascii(_c) ? !!(_DefaultRuneLocale.__runetype[_c] & _f)
202		: !!__maskrune(_c, _f));
203#endif /* USE_ASCII */
204}
205
206__DARWIN_CTYPE_inline __darwin_ct_rune_t
207__isctype(__darwin_ct_rune_t _c, unsigned long _f)
208{
209#ifdef USE_ASCII
210	return !!(__maskrune(_c, _f));
211#else /* USE_ASCII */
212	return (_c < 0 || _c >= _CACHED_RUNES) ? 0 :
213		!!(_DefaultRuneLocale.__runetype[_c] & _f);
214#endif /* USE_ASCII */
215}
216
217#ifdef USE_ASCII
218__DARWIN_CTYPE_inline __darwin_ct_rune_t
219__toupper(__darwin_ct_rune_t _c)
220{
221	return _DefaultRuneLocale.__mapupper[_c & 0xff];
222}
223
224__DARWIN_CTYPE_inline __darwin_ct_rune_t
225__tolower(__darwin_ct_rune_t _c)
226{
227	return _DefaultRuneLocale.__maplower[_c & 0xff];
228}
229//Begin-Libc
230#elif defined(__LIBC__)
231/*
232 * We can't do what we do for __toupper_l() (check for ASCII first, then call
233 * ___toupper_l() otherwise) because versions of ___toupper() before Tiger
234 * assume c >= _CACHED_RUNES.  So we are stuck making __toupper() a routine
235 * to hide the extended locale details, outside of Libc.
236 */
237__DARWIN_CTYPE_inline __darwin_ct_rune_t
238__toupper(__darwin_ct_rune_t _c)
239{
240	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
241		__current_locale()->__lc_ctype->_CurrentRuneLocale.__mapupper[_c];
242}
243
244__DARWIN_CTYPE_inline __darwin_ct_rune_t
245__tolower(__darwin_ct_rune_t _c)
246{
247	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
248		__current_locale()->__lc_ctype->_CurrentRuneLocale.__maplower[_c];
249}
250//End-Libc
251#else /* !USE_ASCII */
252__BEGIN_DECLS
253__darwin_ct_rune_t	__toupper(__darwin_ct_rune_t);
254__darwin_ct_rune_t	__tolower(__darwin_ct_rune_t);
255__END_DECLS
256#endif /* USE_ASCII */
257
258__DARWIN_CTYPE_inline int
259__wcwidth(__darwin_ct_rune_t _c)
260{
261	unsigned int _x;
262
263	if (_c == 0)
264		return (0);
265	_x = (unsigned int)__maskrune(_c, _CTYPE_SWM|_CTYPE_R);
266	if ((_x & _CTYPE_SWM) != 0)
267		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
268	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
269}
270
271#ifndef _EXTERNALIZE_CTYPE_INLINES_
272
273#define	_tolower(c)	__tolower(c)
274#define	_toupper(c)	__toupper(c)
275
276__DARWIN_CTYPE_TOP_inline int
277isalnum(int _c)
278{
279	return (__istype(_c, _CTYPE_A|_CTYPE_D));
280}
281
282__DARWIN_CTYPE_TOP_inline int
283isalpha(int _c)
284{
285	return (__istype(_c, _CTYPE_A));
286}
287
288__DARWIN_CTYPE_TOP_inline int
289isblank(int _c)
290{
291	return (__istype(_c, _CTYPE_B));
292}
293
294__DARWIN_CTYPE_TOP_inline int
295iscntrl(int _c)
296{
297	return (__istype(_c, _CTYPE_C));
298}
299
300/* ANSI -- locale independent */
301__DARWIN_CTYPE_TOP_inline int
302isdigit(int _c)
303{
304	return (__isctype(_c, _CTYPE_D));
305}
306
307__DARWIN_CTYPE_TOP_inline int
308isgraph(int _c)
309{
310	return (__istype(_c, _CTYPE_G));
311}
312
313__DARWIN_CTYPE_TOP_inline int
314islower(int _c)
315{
316	return (__istype(_c, _CTYPE_L));
317}
318
319__DARWIN_CTYPE_TOP_inline int
320isprint(int _c)
321{
322	return (__istype(_c, _CTYPE_R));
323}
324
325__DARWIN_CTYPE_TOP_inline int
326ispunct(int _c)
327{
328	return (__istype(_c, _CTYPE_P));
329}
330
331__DARWIN_CTYPE_TOP_inline int
332isspace(int _c)
333{
334	return (__istype(_c, _CTYPE_S));
335}
336
337__DARWIN_CTYPE_TOP_inline int
338isupper(int _c)
339{
340	return (__istype(_c, _CTYPE_U));
341}
342
343/* ANSI -- locale independent */
344__DARWIN_CTYPE_TOP_inline int
345isxdigit(int _c)
346{
347	return (__isctype(_c, _CTYPE_X));
348}
349
350__DARWIN_CTYPE_TOP_inline int
351toascii(int _c)
352{
353	return (_c & 0x7F);
354}
355
356__DARWIN_CTYPE_TOP_inline int
357tolower(int _c)
358{
359        return (__tolower(_c));
360}
361
362__DARWIN_CTYPE_TOP_inline int
363toupper(int _c)
364{
365        return (__toupper(_c));
366}
367
368#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
369__DARWIN_CTYPE_TOP_inline int
370digittoint(int _c)
371{
372	return (__maskrune(_c, 0x0F));
373}
374
375__DARWIN_CTYPE_TOP_inline int
376ishexnumber(int _c)
377{
378	return (__istype(_c, _CTYPE_X));
379}
380
381__DARWIN_CTYPE_TOP_inline int
382isideogram(int _c)
383{
384	return (__istype(_c, _CTYPE_I));
385}
386
387__DARWIN_CTYPE_TOP_inline int
388isnumber(int _c)
389{
390	return (__istype(_c, _CTYPE_D));
391}
392
393__DARWIN_CTYPE_TOP_inline int
394isphonogram(int _c)
395{
396	return (__istype(_c, _CTYPE_Q));
397}
398
399__DARWIN_CTYPE_TOP_inline int
400isrune(int _c)
401{
402	return (__istype(_c, 0xFFFFFFF0L));
403}
404
405__DARWIN_CTYPE_TOP_inline int
406isspecial(int _c)
407{
408	return (__istype(_c, _CTYPE_T));
409}
410#endif /* !_ANSI_SOURCE && (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
411#endif /* _EXTERNALIZE_CTYPE_INLINES_ */
412
413#else /* not using inlines */
414
415__BEGIN_DECLS
416int     isalnum(int);
417int     isalpha(int);
418int     isblank(int);
419int     iscntrl(int);
420int     isdigit(int);
421int     isgraph(int);
422int     islower(int);
423int     isprint(int);
424int     ispunct(int);
425int     isspace(int);
426int     isupper(int);
427int     isxdigit(int);
428int     tolower(int);
429int     toupper(int);
430int     isascii(int);
431int     toascii(int);
432
433#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
434int     _tolower(int);
435int     _toupper(int);
436int     digittoint(int);
437int     ishexnumber(int);
438int     isideogram(int);
439int     isnumber(int);
440int     isphonogram(int);
441int     isrune(int);
442int     isspecial(int);
443#endif
444__END_DECLS
445
446#endif /* using inlines */
447
448#ifdef _USE_EXTENDED_LOCALES_
449#include <xlocale/_ctype.h>
450#endif /* _USE_EXTENDED_LOCALES_ */
451
452#endif /* !_CTYPE_H_ */
453