_ctype.h revision 173244
11539Srgrimes/*
21539Srgrimes * Copyright (c) 1989, 1993
31539Srgrimes *	The Regents of the University of California.  All rights reserved.
41539Srgrimes * (c) UNIX System Laboratories, Inc.
51539Srgrimes * All or some portions of this file are derived from material licensed
61539Srgrimes * to the University of California by American Telephone and Telegraph
71539Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81539Srgrimes * the permission of UNIX System Laboratories, Inc.
91539Srgrimes *
101539Srgrimes * This code is derived from software contributed to Berkeley by
111539Srgrimes * Paul Borman at Krystal Technologies.
121539Srgrimes *
131539Srgrimes * Redistribution and use in source and binary forms, with or without
141539Srgrimes * modification, are permitted provided that the following conditions
151539Srgrimes * are met:
161539Srgrimes * 1. Redistributions of source code must retain the above copyright
171539Srgrimes *    notice, this list of conditions and the following disclaimer.
181539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
191539Srgrimes *    notice, this list of conditions and the following disclaimer in the
201539Srgrimes *    documentation and/or other materials provided with the distribution.
211539Srgrimes * 3. All advertising materials mentioning features or use of this software
221539Srgrimes *    must display the following acknowledgement:
231539Srgrimes *	This product includes software developed by the University of
241539Srgrimes *	California, Berkeley and its contributors.
251539Srgrimes * 4. Neither the name of the University nor the names of its contributors
261539Srgrimes *    may be used to endorse or promote products derived from this software
271539Srgrimes *    without specific prior written permission.
281539Srgrimes *
291539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
301539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
311539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
321539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
331539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
341539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
351539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
361539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
371539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
381539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
391539Srgrimes * SUCH DAMAGE.
401539Srgrimes *
41133559Stjr * From @(#)ctype.h	8.4 (Berkeley) 1/21/94
42133559Stjr * From FreeBSD: src/include/ctype.h,v 1.27 2004/06/23 07:11:39 tjr Exp
43133559Stjr * $FreeBSD: head/include/_ctype.h 173244 2007-11-01 03:18:02Z ache $
441539Srgrimes */
451539Srgrimes
46133559Stjr#ifndef __CTYPE_H_
47133559Stjr#define	__CTYPE_H_
481539Srgrimes
49103113Smike#include <sys/cdefs.h>
50103113Smike#include <sys/_types.h>
511539Srgrimes
5257035Sobrien#define	_CTYPE_A	0x00000100L		/* Alpha */
5357035Sobrien#define	_CTYPE_C	0x00000200L		/* Control */
5457035Sobrien#define	_CTYPE_D	0x00000400L		/* Digit */
5557035Sobrien#define	_CTYPE_G	0x00000800L		/* Graph */
5657035Sobrien#define	_CTYPE_L	0x00001000L		/* Lower */
5757035Sobrien#define	_CTYPE_P	0x00002000L		/* Punct */
5857035Sobrien#define	_CTYPE_S	0x00004000L		/* Space */
5957035Sobrien#define	_CTYPE_U	0x00008000L		/* Upper */
6057035Sobrien#define	_CTYPE_X	0x00010000L		/* X digit */
6157035Sobrien#define	_CTYPE_B	0x00020000L		/* Blank */
6257035Sobrien#define	_CTYPE_R	0x00040000L		/* Print */
6357035Sobrien#define	_CTYPE_I	0x00080000L		/* Ideogram */
6457035Sobrien#define	_CTYPE_T	0x00100000L		/* Special */
6557035Sobrien#define	_CTYPE_Q	0x00200000L		/* Phonogram */
66101984Skeichii#define	_CTYPE_SW0	0x20000000L		/* 0 width character */
67102093Sache#define	_CTYPE_SW1	0x40000000L		/* 1 width character */
68101984Skeichii#define	_CTYPE_SW2	0x80000000L		/* 2 width character */
69101984Skeichii#define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
70133564Stjr#define	_CTYPE_SWM	0xe0000000L		/* Mask for screen width data */
71133564Stjr#define	_CTYPE_SWS	30			/* Bits to shift to get width */
721539Srgrimes
73102998Smike/* See comments in <sys/_types.h> about __ct_rune_t. */
741539Srgrimes__BEGIN_DECLS
75134101Stjrunsigned long	___runetype(__ct_rune_t) __pure;
76134101Stjr__ct_rune_t	___tolower(__ct_rune_t) __pure;
77134101Stjr__ct_rune_t	___toupper(__ct_rune_t) __pure;
781539Srgrimes__END_DECLS
791539Srgrimes
801539Srgrimes/*
817655Sbde * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
827655Sbde * to generate code for extern versions of all our inline functions.
831539Srgrimes */
847655Sbde#ifdef _EXTERNALIZE_CTYPE_INLINES_
857655Sbde#define	_USE_CTYPE_INLINE_
867655Sbde#define	static
877655Sbde#define	__inline
881539Srgrimes#endif
891539Srgrimes
90172619Sacheextern int __mb_sb_limit;
91172619Sache
927655Sbde/*
937655Sbde * Use inline functions if we are allowed to and the compiler supports them.
947655Sbde */
957655Sbde#if !defined(_DONT_USE_CTYPE_INLINE_) && \
967655Sbde    (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
97103113Smike
98103113Smike#include <runetype.h>
99103113Smike
1001539Srgrimesstatic __inline int
101102227Smike__maskrune(__ct_rune_t _c, unsigned long _f)
1021539Srgrimes{
10329855Sache	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
104130961Stjr		_CurrentRuneLocale->__runetype[_c]) & _f;
1051539Srgrimes}
1061539Srgrimes
1071539Srgrimesstatic __inline int
108172619Sache__sbmaskrune(__ct_rune_t _c, unsigned long _f)
109172619Sache{
110172619Sache	return (_c < 0 || _c >= __mb_sb_limit) ? 0 :
111172619Sache	       _CurrentRuneLocale->__runetype[_c] & _f;
112172619Sache}
113172619Sache
114172619Sachestatic __inline int
115102227Smike__istype(__ct_rune_t _c, unsigned long _f)
11690231Sbbraun{
11790231Sbbraun	return (!!__maskrune(_c, _f));
11890231Sbbraun}
11990231Sbbraun
12090231Sbbraunstatic __inline int
121172619Sache__sbistype(__ct_rune_t _c, unsigned long _f)
122172619Sache{
123172619Sache	return (!!__sbmaskrune(_c, _f));
124172619Sache}
125172619Sache
126172619Sachestatic __inline int
127102227Smike__isctype(__ct_rune_t _c, unsigned long _f)
1281539Srgrimes{
129173244Sache	return (_c < 0 || _c >= 128) ? 0 :
130130961Stjr	       !!(_DefaultRuneLocale.__runetype[_c] & _f);
1311539Srgrimes}
1321539Srgrimes
133102227Smikestatic __inline __ct_rune_t
134102227Smike__toupper(__ct_rune_t _c)
1351539Srgrimes{
13614813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
137130961Stjr	       _CurrentRuneLocale->__mapupper[_c];
1381539Srgrimes}
1391539Srgrimes
140102227Smikestatic __inline __ct_rune_t
141172619Sache__sbtoupper(__ct_rune_t _c)
142172619Sache{
143172619Sache	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
144172619Sache	       _CurrentRuneLocale->__mapupper[_c];
145172619Sache}
146172619Sache
147172619Sachestatic __inline __ct_rune_t
148102227Smike__tolower(__ct_rune_t _c)
1491539Srgrimes{
15014813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
151130961Stjr	       _CurrentRuneLocale->__maplower[_c];
1521539Srgrimes}
1531539Srgrimes
154172619Sachestatic __inline __ct_rune_t
155172619Sache__sbtolower(__ct_rune_t _c)
156172619Sache{
157172619Sache	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
158172619Sache	       _CurrentRuneLocale->__maplower[_c];
159172619Sache}
160172619Sache
161133564Stjrstatic __inline int
162133564Stjr__wcwidth(__ct_rune_t _c)
163133564Stjr{
164133564Stjr	unsigned int _x;
165133564Stjr
166133564Stjr	if (_c == 0)
167133564Stjr		return (0);
168133564Stjr	_x = (unsigned int)__maskrune(_c, _CTYPE_SWM|_CTYPE_R);
169133564Stjr	if ((_x & _CTYPE_SWM) != 0)
170133564Stjr		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
171133564Stjr	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
172133564Stjr}
173133564Stjr
1747655Sbde#else /* not using inlines */
1751539Srgrimes
1761539Srgrimes__BEGIN_DECLS
177102227Smikeint		__maskrune(__ct_rune_t, unsigned long);
178172619Sacheint		__sbmaskrune(__ct_rune_t, unsigned long);
179102227Smikeint		__istype(__ct_rune_t, unsigned long);
180172619Sacheint		__sbistype(__ct_rune_t, unsigned long);
181102227Smikeint		__isctype(__ct_rune_t, unsigned long);
182102227Smike__ct_rune_t	__toupper(__ct_rune_t);
183172619Sache__ct_rune_t	__sbtoupper(__ct_rune_t);
184102227Smike__ct_rune_t	__tolower(__ct_rune_t);
185172619Sache__ct_rune_t	__sbtolower(__ct_rune_t);
186133564Stjrint		__wcwidth(__ct_rune_t);
1871539Srgrimes__END_DECLS
1887655Sbde#endif /* using inlines */
1891539Srgrimes
190133559Stjr#endif /* !__CTYPE_H_ */
191