1331722Seadler/*
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.
21203964Simp * 3. Neither the name of the University nor the names of its contributors
221539Srgrimes *    may be used to endorse or promote products derived from this software
231539Srgrimes *    without specific prior written permission.
241539Srgrimes *
251539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351539Srgrimes * SUCH DAMAGE.
361539Srgrimes *
37133559Stjr * From @(#)ctype.h	8.4 (Berkeley) 1/21/94
38133559Stjr * From FreeBSD: src/include/ctype.h,v 1.27 2004/06/23 07:11:39 tjr Exp
39133559Stjr * $FreeBSD$
401539Srgrimes */
411539Srgrimes
42133559Stjr#ifndef __CTYPE_H_
43133559Stjr#define	__CTYPE_H_
441539Srgrimes
45103113Smike#include <sys/cdefs.h>
46103113Smike#include <sys/_types.h>
471539Srgrimes
4857035Sobrien#define	_CTYPE_A	0x00000100L		/* Alpha */
4957035Sobrien#define	_CTYPE_C	0x00000200L		/* Control */
5057035Sobrien#define	_CTYPE_D	0x00000400L		/* Digit */
5157035Sobrien#define	_CTYPE_G	0x00000800L		/* Graph */
5257035Sobrien#define	_CTYPE_L	0x00001000L		/* Lower */
5357035Sobrien#define	_CTYPE_P	0x00002000L		/* Punct */
5457035Sobrien#define	_CTYPE_S	0x00004000L		/* Space */
5557035Sobrien#define	_CTYPE_U	0x00008000L		/* Upper */
5657035Sobrien#define	_CTYPE_X	0x00010000L		/* X digit */
5757035Sobrien#define	_CTYPE_B	0x00020000L		/* Blank */
5857035Sobrien#define	_CTYPE_R	0x00040000L		/* Print */
5957035Sobrien#define	_CTYPE_I	0x00080000L		/* Ideogram */
6057035Sobrien#define	_CTYPE_T	0x00100000L		/* Special */
6157035Sobrien#define	_CTYPE_Q	0x00200000L		/* Phonogram */
62290494Sbapt#define	_CTYPE_N 	0x00400000L		/* Number (superset of digit) */
63101984Skeichii#define	_CTYPE_SW0	0x20000000L		/* 0 width character */
64102093Sache#define	_CTYPE_SW1	0x40000000L		/* 1 width character */
65101984Skeichii#define	_CTYPE_SW2	0x80000000L		/* 2 width character */
66101984Skeichii#define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
67133564Stjr#define	_CTYPE_SWM	0xe0000000L		/* Mask for screen width data */
68133564Stjr#define	_CTYPE_SWS	30			/* Bits to shift to get width */
691539Srgrimes
70102998Smike/* See comments in <sys/_types.h> about __ct_rune_t. */
711539Srgrimes__BEGIN_DECLS
72134101Stjrunsigned long	___runetype(__ct_rune_t) __pure;
73134101Stjr__ct_rune_t	___tolower(__ct_rune_t) __pure;
74134101Stjr__ct_rune_t	___toupper(__ct_rune_t) __pure;
751539Srgrimes__END_DECLS
761539Srgrimes
771539Srgrimes/*
787655Sbde * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
797655Sbde * to generate code for extern versions of all our inline functions.
801539Srgrimes */
817655Sbde#ifdef _EXTERNALIZE_CTYPE_INLINES_
827655Sbde#define	_USE_CTYPE_INLINE_
837655Sbde#define	static
847655Sbde#define	__inline
851539Srgrimes#endif
861539Srgrimes
87172619Sacheextern int __mb_sb_limit;
88172619Sache
897655Sbde/*
907655Sbde * Use inline functions if we are allowed to and the compiler supports them.
917655Sbde */
927655Sbde#if !defined(_DONT_USE_CTYPE_INLINE_) && \
937655Sbde    (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
94103113Smike
95103113Smike#include <runetype.h>
96103113Smike
971539Srgrimesstatic __inline int
98102227Smike__maskrune(__ct_rune_t _c, unsigned long _f)
991539Srgrimes{
10029855Sache	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
101130961Stjr		_CurrentRuneLocale->__runetype[_c]) & _f;
1021539Srgrimes}
1031539Srgrimes
1041539Srgrimesstatic __inline int
105172619Sache__sbmaskrune(__ct_rune_t _c, unsigned long _f)
106172619Sache{
107172619Sache	return (_c < 0 || _c >= __mb_sb_limit) ? 0 :
108172619Sache	       _CurrentRuneLocale->__runetype[_c] & _f;
109172619Sache}
110172619Sache
111172619Sachestatic __inline int
112102227Smike__istype(__ct_rune_t _c, unsigned long _f)
11390231Sbbraun{
11490231Sbbraun	return (!!__maskrune(_c, _f));
11590231Sbbraun}
11690231Sbbraun
11790231Sbbraunstatic __inline int
118172619Sache__sbistype(__ct_rune_t _c, unsigned long _f)
119172619Sache{
120172619Sache	return (!!__sbmaskrune(_c, _f));
121172619Sache}
122172619Sache
123172619Sachestatic __inline int
124102227Smike__isctype(__ct_rune_t _c, unsigned long _f)
1251539Srgrimes{
126173244Sache	return (_c < 0 || _c >= 128) ? 0 :
127130961Stjr	       !!(_DefaultRuneLocale.__runetype[_c] & _f);
1281539Srgrimes}
1291539Srgrimes
130102227Smikestatic __inline __ct_rune_t
131102227Smike__toupper(__ct_rune_t _c)
1321539Srgrimes{
13314813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
134130961Stjr	       _CurrentRuneLocale->__mapupper[_c];
1351539Srgrimes}
1361539Srgrimes
137102227Smikestatic __inline __ct_rune_t
138172619Sache__sbtoupper(__ct_rune_t _c)
139172619Sache{
140172619Sache	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
141172619Sache	       _CurrentRuneLocale->__mapupper[_c];
142172619Sache}
143172619Sache
144172619Sachestatic __inline __ct_rune_t
145102227Smike__tolower(__ct_rune_t _c)
1461539Srgrimes{
14714813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
148130961Stjr	       _CurrentRuneLocale->__maplower[_c];
1491539Srgrimes}
1501539Srgrimes
151172619Sachestatic __inline __ct_rune_t
152172619Sache__sbtolower(__ct_rune_t _c)
153172619Sache{
154172619Sache	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
155172619Sache	       _CurrentRuneLocale->__maplower[_c];
156172619Sache}
157172619Sache
158133564Stjrstatic __inline int
159133564Stjr__wcwidth(__ct_rune_t _c)
160133564Stjr{
161133564Stjr	unsigned int _x;
162133564Stjr
163133564Stjr	if (_c == 0)
164133564Stjr		return (0);
165133564Stjr	_x = (unsigned int)__maskrune(_c, _CTYPE_SWM|_CTYPE_R);
166133564Stjr	if ((_x & _CTYPE_SWM) != 0)
167133564Stjr		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
168133564Stjr	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
169133564Stjr}
170133564Stjr
1717655Sbde#else /* not using inlines */
1721539Srgrimes
1731539Srgrimes__BEGIN_DECLS
174102227Smikeint		__maskrune(__ct_rune_t, unsigned long);
175172619Sacheint		__sbmaskrune(__ct_rune_t, unsigned long);
176102227Smikeint		__istype(__ct_rune_t, unsigned long);
177172619Sacheint		__sbistype(__ct_rune_t, unsigned long);
178102227Smikeint		__isctype(__ct_rune_t, unsigned long);
179102227Smike__ct_rune_t	__toupper(__ct_rune_t);
180172619Sache__ct_rune_t	__sbtoupper(__ct_rune_t);
181102227Smike__ct_rune_t	__tolower(__ct_rune_t);
182172619Sache__ct_rune_t	__sbtolower(__ct_rune_t);
183133564Stjrint		__wcwidth(__ct_rune_t);
1841539Srgrimes__END_DECLS
1857655Sbde#endif /* using inlines */
1861539Srgrimes
187133559Stjr#endif /* !__CTYPE_H_ */
188