d19b7f6ac303b0e3f704fdd5009201b971327384
[platform/upstream/bash.git] / include / chartypes.h
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 HEXVALUE(c) \
86   (((c) >= 'a' && (c) <= 'f') \
87         ? (c)-'a'+10 \
88         : (c) >= 'A' && (c) <= 'F' ? (c)-'A'+10 : (c)-'0')
89                   
90 #ifndef ISOCTAL
91 #  define ISOCTAL(c)    ((c) >= '0' && (c) <= '7')
92 #endif
93 #define OCTVALUE(c)     ((c) - '0')
94
95 #define TODIGIT(c)      ((c) - '0')
96 #define TOCHAR(c)       ((c) + '0')
97
98 #define TOLOWER(c)      (ISUPPER(c) ? tolower(c) : (c))
99 #define TOUPPER(c)      (ISLOWER(c) ? toupper(c) : (c))
100
101 #ifdef toascii
102 #  define TOASCII(c)    (toascii(c))
103 #else
104 #  define TOASCII(c)    ((c) & 0x7F)
105 #endif
106
107 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
108    since ours (we hope) works properly with all combinations of
109    machines, compilers, `char' and `unsigned char' argument types.
110    (Per Bothner suggested the basic approach.)  */
111 #undef SIGN_EXTEND_CHAR
112 #if __STDC__
113 #  define SIGN_EXTEND_CHAR(c) ((signed char) (c))
114 #else  /* not __STDC__ */
115    /* As in Harbison and Steele.  */
116 #  define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
117 #endif
118
119 #endif /* _SH_CHARTYPES_H */