ctype.h revision 102093
197403Sobrien/*
297403Sobrien * Copyright (c) 1989, 1993
3132720Skan *	The Regents of the University of California.  All rights reserved.
497403Sobrien * (c) UNIX System Laboratories, Inc.
597403Sobrien * All or some portions of this file are derived from material licensed
697403Sobrien * to the University of California by American Telephone and Telegraph
797403Sobrien * Co. or Unix System Laboratories, Inc. and are reproduced herein with
897403Sobrien * the permission of UNIX System Laboratories, Inc.
997403Sobrien *
1097403Sobrien * This code is derived from software contributed to Berkeley by
1197403Sobrien * Paul Borman at Krystal Technologies.
1297403Sobrien *
1397403Sobrien * Redistribution and use in source and binary forms, with or without
1497403Sobrien * modification, are permitted provided that the following conditions
1597403Sobrien * are met:
1697403Sobrien * 1. Redistributions of source code must retain the above copyright
1797403Sobrien *    notice, this list of conditions and the following disclaimer.
18169691Skan * 2. Redistributions in binary form must reproduce the above copyright
1997403Sobrien *    notice, this list of conditions and the following disclaimer in the
2097403Sobrien *    documentation and/or other materials provided with the distribution.
2197403Sobrien * 3. All advertising materials mentioning features or use of this software
2297403Sobrien *    must display the following acknowledgement:
2397403Sobrien *	This product includes software developed by the University of
2497403Sobrien *	California, Berkeley and its contributors.
2597403Sobrien * 4. Neither the name of the University nor the names of its contributors
2697403Sobrien *    may be used to endorse or promote products derived from this software
2797403Sobrien *    without specific prior written permission.
2897403Sobrien *
2997403Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3097403Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3197403Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3297403Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3397403Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34132720Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35132720Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36132720Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3797403Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3897403Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39102782Skan * SUCH DAMAGE.
40102782Skan *
4197403Sobrien *	@(#)ctype.h	8.4 (Berkeley) 1/21/94
4297403Sobrien *      $FreeBSD: head/include/ctype.h 102093 2002-08-19 08:50:41Z ache $
43102782Skan */
44102782Skan
45102782Skan#ifndef _CTYPE_H_
46102782Skan#define	_CTYPE_H_
47102782Skan
48102782Skan/*
49102782Skan * XXX <runetype.h> brings massive namespace pollution (rune_t and struct
50102782Skan * member names).
51102782Skan */
52102782Skan#include <runetype.h>
53102782Skan
54102782Skan#define	_CTYPE_A	0x00000100L		/* Alpha */
55102782Skan#define	_CTYPE_C	0x00000200L		/* Control */
56102782Skan#define	_CTYPE_D	0x00000400L		/* Digit */
57102782Skan#define	_CTYPE_G	0x00000800L		/* Graph */
58102782Skan#define	_CTYPE_L	0x00001000L		/* Lower */
59102782Skan#define	_CTYPE_P	0x00002000L		/* Punct */
60102782Skan#define	_CTYPE_S	0x00004000L		/* Space */
61102782Skan#define	_CTYPE_U	0x00008000L		/* Upper */
62102782Skan#define	_CTYPE_X	0x00010000L		/* X digit */
63102782Skan#define	_CTYPE_B	0x00020000L		/* Blank */
64102782Skan#define	_CTYPE_R	0x00040000L		/* Print */
65102782Skan#define	_CTYPE_I	0x00080000L		/* Ideogram */
66102782Skan#define	_CTYPE_T	0x00100000L		/* Special */
67102782Skan#define	_CTYPE_Q	0x00200000L		/* Phonogram */
68102782Skan#define	_CTYPE_SWM	0xe0000000L		/* Mask to get screen width data */
69102782Skan#define	_CTYPE_SWS	30			/* Bits to shift to get width */
70102782Skan#define	_CTYPE_SWA	0x00000000L		/* autowidth / default */
71102782Skan#define	_CTYPE_SW0	0x20000000L		/* 0 width character */
72102782Skan#define	_CTYPE_SW1	0x40000000L		/* 1 width character */
73102782Skan#define	_CTYPE_SW2	0x80000000L		/* 2 width character */
74102782Skan#define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
75102782Skan
76102782Skan__BEGIN_DECLS
77102782Skanint	isalnum(int);
78102782Skanint	isalpha(int);
79102782Skanint	iscntrl(int);
80102782Skanint	isdigit(int);
81102782Skanint	isgraph(int);
82132720Skanint	islower(int);
83102782Skanint	isprint(int);
84102782Skanint	ispunct(int);
85102782Skanint	isspace(int);
86102782Skanint	isupper(int);
87102782Skanint	isxdigit(int);
88102782Skanint	tolower(int);
89102782Skanint	toupper(int);
90102782Skan
91102782Skan#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
92102782Skanint	digittoint(int);
93102782Skanint	isascii(int);
94102782Skanint	isblank(int);
95102782Skanint	ishexnumber(int);
96132720Skanint	isideogram(int);
97132720Skanint	isnumber(int);
98102782Skanint	isphonogram(int);
99102782Skanint	isrune(int);
100132720Skanint	isspecial(int);
101102782Skanint	toascii(int);
102102782Skan#endif
103102782Skan__END_DECLS
104102782Skan
105102782Skan#define	isalnum(c)	__istype((c), _CTYPE_A|_CTYPE_D)
106102782Skan#define	isalpha(c)	__istype((c), _CTYPE_A)
107102782Skan#define	iscntrl(c)	__istype((c), _CTYPE_C)
10897403Sobrien#define	isdigit(c)	__isctype((c), _CTYPE_D) /* ANSI -- locale independent */
109102782Skan#define	isgraph(c)	__istype((c), _CTYPE_G)
110132720Skan#define	islower(c)	__istype((c), _CTYPE_L)
111132720Skan#define	isprint(c)	__istype((c), _CTYPE_R)
112102782Skan#define	ispunct(c)	__istype((c), _CTYPE_P)
113102782Skan#define	isspace(c)	__istype((c), _CTYPE_S)
114132720Skan#define	isupper(c)	__istype((c), _CTYPE_U)
115132720Skan#define	isxdigit(c)	__isctype((c), _CTYPE_X) /* ANSI -- locale independent */
116132720Skan#define	tolower(c)	__tolower(c)
117102782Skan#define	toupper(c)	__toupper(c)
118102782Skan
119102782Skan#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
120102782Skan#define	digittoint(c)	__maskrune((c), 0xFF)
121102782Skan#define	isascii(c)	(((c) & ~0x7F) == 0)
122102782Skan#define	isblank(c)	__istype((c), _CTYPE_B)
123102782Skan#define	ishexnumber(c)	__istype((c), _CTYPE_X)
124102782Skan#define	isideogram(c)	__istype((c), _CTYPE_I)
125#define	isnumber(c)	__istype((c), _CTYPE_D)
126#define	isphonogram(c)	__istype((c), _CTYPE_Q)
127#define	isrune(c)	__istype((c), 0xFFFFFF00L)
128#define	isspecial(c)	__istype((c), _CTYPE_T)
129#define	toascii(c)	((c) & 0x7F)
130#endif
131
132/* See comments in <machine/ansi.h> about _BSD_CT_RUNE_T_. */
133__BEGIN_DECLS
134unsigned long	___runetype(_BSD_CT_RUNE_T_);
135_BSD_CT_RUNE_T_	___tolower(_BSD_CT_RUNE_T_);
136_BSD_CT_RUNE_T_	___toupper(_BSD_CT_RUNE_T_);
137__END_DECLS
138
139/*
140 * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
141 * to generate code for extern versions of all our inline functions.
142 */
143#ifdef _EXTERNALIZE_CTYPE_INLINES_
144#define	_USE_CTYPE_INLINE_
145#define	static
146#define	__inline
147#endif
148
149/*
150 * Use inline functions if we are allowed to and the compiler supports them.
151 */
152#if !defined(_DONT_USE_CTYPE_INLINE_) && \
153    (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
154static __inline int
155__maskrune(_BSD_CT_RUNE_T_ _c, unsigned long _f)
156{
157	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
158		_CurrentRuneLocale->runetype[_c]) & _f;
159}
160
161static __inline int
162__istype(_BSD_CT_RUNE_T_ _c, unsigned long _f)
163{
164	return (!!__maskrune(_c, _f));
165}
166
167static __inline int
168__isctype(_BSD_CT_RUNE_T_ _c, unsigned long _f)
169{
170	return (_c < 0 || _c >= _CACHED_RUNES) ? 0 :
171	       !!(_DefaultRuneLocale.runetype[_c] & _f);
172}
173
174static __inline _BSD_CT_RUNE_T_
175__toupper(_BSD_CT_RUNE_T_ _c)
176{
177	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
178	       _CurrentRuneLocale->mapupper[_c];
179}
180
181static __inline _BSD_CT_RUNE_T_
182__tolower(_BSD_CT_RUNE_T_ _c)
183{
184	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
185	       _CurrentRuneLocale->maplower[_c];
186}
187
188#else /* not using inlines */
189
190__BEGIN_DECLS
191int		__maskrune(_BSD_CT_RUNE_T_, unsigned long);
192int		__istype(_BSD_CT_RUNE_T_, unsigned long);
193int		__isctype(_BSD_CT_RUNE_T_, unsigned long);
194_BSD_CT_RUNE_T_	__toupper(_BSD_CT_RUNE_T_);
195_BSD_CT_RUNE_T_	__tolower(_BSD_CT_RUNE_T_);
196__END_DECLS
197#endif /* using inlines */
198
199#endif /* !_CTYPE_H_ */
200