_ctype.h revision 133564
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 133564 2004-08-12 12:19:11Z tjr $
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
75102227Smikeunsigned long	___runetype(__ct_rune_t);
76102227Smike__ct_rune_t	___tolower(__ct_rune_t);
77102227Smike__ct_rune_t	___toupper(__ct_rune_t);
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
907655Sbde/*
917655Sbde * Use inline functions if we are allowed to and the compiler supports them.
927655Sbde */
937655Sbde#if !defined(_DONT_USE_CTYPE_INLINE_) && \
947655Sbde    (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
95103113Smike
96103113Smike#include <runetype.h>
97103113Smike
981539Srgrimesstatic __inline int
99102227Smike__maskrune(__ct_rune_t _c, unsigned long _f)
1001539Srgrimes{
10129855Sache	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
102130961Stjr		_CurrentRuneLocale->__runetype[_c]) & _f;
1031539Srgrimes}
1041539Srgrimes
1051539Srgrimesstatic __inline int
106102227Smike__istype(__ct_rune_t _c, unsigned long _f)
10790231Sbbraun{
10890231Sbbraun	return (!!__maskrune(_c, _f));
10990231Sbbraun}
11090231Sbbraun
11190231Sbbraunstatic __inline int
112102227Smike__isctype(__ct_rune_t _c, unsigned long _f)
1131539Srgrimes{
11412028Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? 0 :
115130961Stjr	       !!(_DefaultRuneLocale.__runetype[_c] & _f);
1161539Srgrimes}
1171539Srgrimes
118102227Smikestatic __inline __ct_rune_t
119102227Smike__toupper(__ct_rune_t _c)
1201539Srgrimes{
12114813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
122130961Stjr	       _CurrentRuneLocale->__mapupper[_c];
1231539Srgrimes}
1241539Srgrimes
125102227Smikestatic __inline __ct_rune_t
126102227Smike__tolower(__ct_rune_t _c)
1271539Srgrimes{
12814813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
129130961Stjr	       _CurrentRuneLocale->__maplower[_c];
1301539Srgrimes}
1311539Srgrimes
132133564Stjrstatic __inline int
133133564Stjr__wcwidth(__ct_rune_t _c)
134133564Stjr{
135133564Stjr	unsigned int _x;
136133564Stjr
137133564Stjr	if (_c == 0)
138133564Stjr		return (0);
139133564Stjr	_x = (unsigned int)__maskrune(_c, _CTYPE_SWM|_CTYPE_R);
140133564Stjr	if ((_x & _CTYPE_SWM) != 0)
141133564Stjr		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
142133564Stjr	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
143133564Stjr}
144133564Stjr
1457655Sbde#else /* not using inlines */
1461539Srgrimes
1471539Srgrimes__BEGIN_DECLS
148102227Smikeint		__maskrune(__ct_rune_t, unsigned long);
149102227Smikeint		__istype(__ct_rune_t, unsigned long);
150102227Smikeint		__isctype(__ct_rune_t, unsigned long);
151102227Smike__ct_rune_t	__toupper(__ct_rune_t);
152102227Smike__ct_rune_t	__tolower(__ct_rune_t);
153133564Stjrint		__wcwidth(__ct_rune_t);
1541539Srgrimes__END_DECLS
1557655Sbde#endif /* using inlines */
1561539Srgrimes
157133559Stjr#endif /* !__CTYPE_H_ */
158