1/* chartypes.h -- extend ctype.h */
2
3/* Copyright (C) 2001 Free Software Foundation, Inc.
4
5   This file is part of GNU Bash, the Bourne Again SHell.
6
7   Bash is free software; you can redistribute it and/or modify it under
8   the terms of the GNU General Public License as published by the Free
9   Software Foundation; either version 2, or (at your option) any later
10   version.
11
12   Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13   WARRANTY; without even the implied warranty of MERCHANTABILITY or
14   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15   for more details.
16
17   You should have received a copy of the GNU General Public License along
18   with Bash; see the file COPYING.  If not, write to the Free Software
19   Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21#ifndef _SH_CHARTYPES_H
22#define _SH_CHARTYPES_H
23
24#include <ctype.h>
25
26/* Jim Meyering writes:
27
28   "... Some ctype macros are valid only for character codes that
29   isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
30   using /bin/cc or gcc but without giving an ansi option).  So, all
31   ctype uses should be through macros like ISPRINT...  If
32   STDC_HEADERS is defined, then autoconf has verified that the ctype
33   macros don't need to be guarded with references to isascii. ...
34   Defining IN_CTYPE_DOMAIN to 1 should let any compiler worth its salt
35   eliminate the && through constant folding."
36   Solaris defines some of these symbols so we must undefine them first.  */
37
38#if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
39#  define IN_CTYPE_DOMAIN(c) 1
40#else
41#  define IN_CTYPE_DOMAIN(c) isascii(c)
42#endif
43
44#if !defined (isspace) && !defined (HAVE_ISSPACE)
45#  define isspace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\f')
46#endif
47
48#if !defined (isprint) && !defined (HAVE_ISPRINT)
49#  define isprint(c) (isalpha(c) || isdigit(c) || ispunct(c))
50#endif
51
52#if defined (isblank) || defined (HAVE_ISBLANK)
53#  define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
54#else
55#  define ISBLANK(c) ((c) == ' ' || (c) == '\t')
56#endif
57
58#if defined (isgraph) || defined (HAVE_ISGRAPH)
59#  define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
60#else
61#  define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
62#endif
63
64#if !defined (isxdigit) && !defined (HAVE_ISXDIGIT)
65#  define isxdigit(c)	(((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
66#endif
67
68#undef ISPRINT
69
70#define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
71#define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
72#define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
73#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
74#define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
75#define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
76#define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
77#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
78#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
79#define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
80
81#define ISLETTER(c)	(ISALPHA(c))
82
83#define DIGIT(c)	((c) >= '0' && (c) <= '9')
84
85#define ISWORD(c)	(ISLETTER(c) || DIGIT(c) || ((c) == '_'))
86
87#define HEXVALUE(c) \
88  (((c) >= 'a' && (c) <= 'f') \
89	? (c)-'a'+10 \
90	: (c) >= 'A' && (c) <= 'F' ? (c)-'A'+10 : (c)-'0')
91
92#ifndef ISOCTAL
93#  define ISOCTAL(c)	((c) >= '0' && (c) <= '7')
94#endif
95#define OCTVALUE(c)	((c) - '0')
96
97#define TODIGIT(c)	((c) - '0')
98#define TOCHAR(c)	((c) + '0')
99
100#define TOLOWER(c)	(ISUPPER(c) ? tolower(c) : (c))
101#define TOUPPER(c)	(ISLOWER(c) ? toupper(c) : (c))
102
103#ifndef TOCTRL
104   /* letter to control char -- ASCII.  The TOUPPER is in there so \ce and
105      \cE will map to the same character in $'...' expansions. */
106#  define TOCTRL(x)	(TOUPPER(x) & 037)
107#endif
108#ifndef UNCTRL
109   /* control char to letter -- ASCII */
110#  define UNCTRL(x)	(TOUPPER((x) | 0x40))
111#endif
112
113#endif /* _SH_CHARTYPES_H */
114