1*e4b17023SJohn Marino /* <ctype.h> replacement macros.
2*e4b17023SJohn Marino 
3*e4b17023SJohn Marino    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4*e4b17023SJohn Marino    Contributed by Zack Weinberg <zackw@stanford.edu>.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino This file is part of the libiberty library.
7*e4b17023SJohn Marino Libiberty is free software; you can redistribute it and/or
8*e4b17023SJohn Marino modify it under the terms of the GNU Library General Public
9*e4b17023SJohn Marino License as published by the Free Software Foundation; either
10*e4b17023SJohn Marino version 2 of the License, or (at your option) any later version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino Libiberty is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*e4b17023SJohn Marino Library General Public License for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino You should have received a copy of the GNU Library General Public
18*e4b17023SJohn Marino License along with libiberty; see the file COPYING.LIB.  If
19*e4b17023SJohn Marino not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20*e4b17023SJohn Marino Boston, MA 02110-1301, USA.  */
21*e4b17023SJohn Marino 
22*e4b17023SJohn Marino /* This is a compatible replacement of the standard C library's <ctype.h>
23*e4b17023SJohn Marino    with the following properties:
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino    - Implements all isxxx() macros required by C99.
26*e4b17023SJohn Marino    - Also implements some character classes useful when
27*e4b17023SJohn Marino      parsing C-like languages.
28*e4b17023SJohn Marino    - Does not change behavior depending on the current locale.
29*e4b17023SJohn Marino    - Behaves properly for all values in the range of a signed or
30*e4b17023SJohn Marino      unsigned char.
31*e4b17023SJohn Marino 
32*e4b17023SJohn Marino    To avoid conflicts, this header defines the isxxx functions in upper
33*e4b17023SJohn Marino    case, e.g. ISALPHA not isalpha.  */
34*e4b17023SJohn Marino 
35*e4b17023SJohn Marino #ifndef SAFE_CTYPE_H
36*e4b17023SJohn Marino #define SAFE_CTYPE_H
37*e4b17023SJohn Marino 
38*e4b17023SJohn Marino /* Determine host character set.  */
39*e4b17023SJohn Marino #define HOST_CHARSET_UNKNOWN 0
40*e4b17023SJohn Marino #define HOST_CHARSET_ASCII   1
41*e4b17023SJohn Marino #define HOST_CHARSET_EBCDIC  2
42*e4b17023SJohn Marino 
43*e4b17023SJohn Marino #if  '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
44*e4b17023SJohn Marino    && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
45*e4b17023SJohn Marino #  define HOST_CHARSET HOST_CHARSET_ASCII
46*e4b17023SJohn Marino #else
47*e4b17023SJohn Marino # if '\n' == 0x15 && ' ' == 0x40 && '0' == 0xF0 \
48*e4b17023SJohn Marino    && 'A' == 0xC1 && 'a' == 0x81 && '!' == 0x5A
49*e4b17023SJohn Marino #  define HOST_CHARSET HOST_CHARSET_EBCDIC
50*e4b17023SJohn Marino # else
51*e4b17023SJohn Marino #  define HOST_CHARSET HOST_CHARSET_UNKNOWN
52*e4b17023SJohn Marino # endif
53*e4b17023SJohn Marino #endif
54*e4b17023SJohn Marino 
55*e4b17023SJohn Marino /* Categories.  */
56*e4b17023SJohn Marino 
57*e4b17023SJohn Marino enum {
58*e4b17023SJohn Marino   /* In C99 */
59*e4b17023SJohn Marino   _sch_isblank  = 0x0001,	/* space \t */
60*e4b17023SJohn Marino   _sch_iscntrl  = 0x0002,	/* nonprinting characters */
61*e4b17023SJohn Marino   _sch_isdigit  = 0x0004,	/* 0-9 */
62*e4b17023SJohn Marino   _sch_islower  = 0x0008,	/* a-z */
63*e4b17023SJohn Marino   _sch_isprint  = 0x0010,	/* any printing character including ' ' */
64*e4b17023SJohn Marino   _sch_ispunct  = 0x0020,	/* all punctuation */
65*e4b17023SJohn Marino   _sch_isspace  = 0x0040,	/* space \t \n \r \f \v */
66*e4b17023SJohn Marino   _sch_isupper  = 0x0080,	/* A-Z */
67*e4b17023SJohn Marino   _sch_isxdigit = 0x0100,	/* 0-9A-Fa-f */
68*e4b17023SJohn Marino 
69*e4b17023SJohn Marino   /* Extra categories useful to cpplib.  */
70*e4b17023SJohn Marino   _sch_isidst	= 0x0200,	/* A-Za-z_ */
71*e4b17023SJohn Marino   _sch_isvsp    = 0x0400,	/* \n \r */
72*e4b17023SJohn Marino   _sch_isnvsp   = 0x0800,	/* space \t \f \v \0 */
73*e4b17023SJohn Marino 
74*e4b17023SJohn Marino   /* Combinations of the above.  */
75*e4b17023SJohn Marino   _sch_isalpha  = _sch_isupper|_sch_islower,	/* A-Za-z */
76*e4b17023SJohn Marino   _sch_isalnum  = _sch_isalpha|_sch_isdigit,	/* A-Za-z0-9 */
77*e4b17023SJohn Marino   _sch_isidnum  = _sch_isidst|_sch_isdigit,	/* A-Za-z0-9_ */
78*e4b17023SJohn Marino   _sch_isgraph  = _sch_isalnum|_sch_ispunct,	/* isprint and not space */
79*e4b17023SJohn Marino   _sch_iscppsp  = _sch_isvsp|_sch_isnvsp,	/* isspace + \0 */
80*e4b17023SJohn Marino   _sch_isbasic  = _sch_isprint|_sch_iscppsp     /* basic charset of ISO C
81*e4b17023SJohn Marino 						   (plus ` and @)  */
82*e4b17023SJohn Marino };
83*e4b17023SJohn Marino 
84*e4b17023SJohn Marino /* Character classification.  */
85*e4b17023SJohn Marino extern const unsigned short _sch_istable[256];
86*e4b17023SJohn Marino 
87*e4b17023SJohn Marino #define _sch_test(c, bit) (_sch_istable[(c) & 0xff] & (unsigned short)(bit))
88*e4b17023SJohn Marino 
89*e4b17023SJohn Marino #define ISALPHA(c)  _sch_test(c, _sch_isalpha)
90*e4b17023SJohn Marino #define ISALNUM(c)  _sch_test(c, _sch_isalnum)
91*e4b17023SJohn Marino #define ISBLANK(c)  _sch_test(c, _sch_isblank)
92*e4b17023SJohn Marino #define ISCNTRL(c)  _sch_test(c, _sch_iscntrl)
93*e4b17023SJohn Marino #define ISDIGIT(c)  _sch_test(c, _sch_isdigit)
94*e4b17023SJohn Marino #define ISGRAPH(c)  _sch_test(c, _sch_isgraph)
95*e4b17023SJohn Marino #define ISLOWER(c)  _sch_test(c, _sch_islower)
96*e4b17023SJohn Marino #define ISPRINT(c)  _sch_test(c, _sch_isprint)
97*e4b17023SJohn Marino #define ISPUNCT(c)  _sch_test(c, _sch_ispunct)
98*e4b17023SJohn Marino #define ISSPACE(c)  _sch_test(c, _sch_isspace)
99*e4b17023SJohn Marino #define ISUPPER(c)  _sch_test(c, _sch_isupper)
100*e4b17023SJohn Marino #define ISXDIGIT(c) _sch_test(c, _sch_isxdigit)
101*e4b17023SJohn Marino 
102*e4b17023SJohn Marino #define ISIDNUM(c)	_sch_test(c, _sch_isidnum)
103*e4b17023SJohn Marino #define ISIDST(c)	_sch_test(c, _sch_isidst)
104*e4b17023SJohn Marino #define IS_ISOBASIC(c)	_sch_test(c, _sch_isbasic)
105*e4b17023SJohn Marino #define IS_VSPACE(c)	_sch_test(c, _sch_isvsp)
106*e4b17023SJohn Marino #define IS_NVSPACE(c)	_sch_test(c, _sch_isnvsp)
107*e4b17023SJohn Marino #define IS_SPACE_OR_NUL(c)	_sch_test(c, _sch_iscppsp)
108*e4b17023SJohn Marino 
109*e4b17023SJohn Marino /* Character transformation.  */
110*e4b17023SJohn Marino extern const unsigned char  _sch_toupper[256];
111*e4b17023SJohn Marino extern const unsigned char  _sch_tolower[256];
112*e4b17023SJohn Marino #define TOUPPER(c) _sch_toupper[(c) & 0xff]
113*e4b17023SJohn Marino #define TOLOWER(c) _sch_tolower[(c) & 0xff]
114*e4b17023SJohn Marino 
115*e4b17023SJohn Marino /* Prevent the users of safe-ctype.h from accidently using the routines
116*e4b17023SJohn Marino    from ctype.h.  Initially, the approach was to produce an error when
117*e4b17023SJohn Marino    detecting that ctype.h has been included.  But this was causing
118*e4b17023SJohn Marino    trouble as ctype.h might get indirectly included as a result of
119*e4b17023SJohn Marino    including another system header (for instance gnulib's stdint.h).
120*e4b17023SJohn Marino    So we include ctype.h here and then immediately redefine its macros.  */
121*e4b17023SJohn Marino 
122*e4b17023SJohn Marino #include <ctype.h>
123*e4b17023SJohn Marino #undef isalpha
124*e4b17023SJohn Marino #define isalpha(c) do_not_use_isalpha_with_safe_ctype
125*e4b17023SJohn Marino #undef isalnum
126*e4b17023SJohn Marino #define isalnum(c) do_not_use_isalnum_with_safe_ctype
127*e4b17023SJohn Marino #undef iscntrl
128*e4b17023SJohn Marino #define iscntrl(c) do_not_use_iscntrl_with_safe_ctype
129*e4b17023SJohn Marino #undef isdigit
130*e4b17023SJohn Marino #define isdigit(c) do_not_use_isdigit_with_safe_ctype
131*e4b17023SJohn Marino #undef isgraph
132*e4b17023SJohn Marino #define isgraph(c) do_not_use_isgraph_with_safe_ctype
133*e4b17023SJohn Marino #undef islower
134*e4b17023SJohn Marino #define islower(c) do_not_use_islower_with_safe_ctype
135*e4b17023SJohn Marino #undef isprint
136*e4b17023SJohn Marino #define isprint(c) do_not_use_isprint_with_safe_ctype
137*e4b17023SJohn Marino #undef ispunct
138*e4b17023SJohn Marino #define ispunct(c) do_not_use_ispunct_with_safe_ctype
139*e4b17023SJohn Marino #undef isspace
140*e4b17023SJohn Marino #define isspace(c) do_not_use_isspace_with_safe_ctype
141*e4b17023SJohn Marino #undef isupper
142*e4b17023SJohn Marino #define isupper(c) do_not_use_isupper_with_safe_ctype
143*e4b17023SJohn Marino #undef isxdigit
144*e4b17023SJohn Marino #define isxdigit(c) do_not_use_isxdigit_with_safe_ctype
145*e4b17023SJohn Marino #undef toupper
146*e4b17023SJohn Marino #define toupper(c) do_not_use_toupper_with_safe_ctype
147*e4b17023SJohn Marino #undef tolower
148*e4b17023SJohn Marino #define tolower(c) do_not_use_tolower_with_safe_ctype
149*e4b17023SJohn Marino 
150*e4b17023SJohn Marino #endif /* SAFE_CTYPE_H */
151