1/* chardefs.h -- Character definitions for readline. */
2
3/* Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
7
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#ifndef _CHARDEFS_H_
23#define _CHARDEFS_H_
24
25#include <ctype.h>
26
27#include <string.h>
28
29#ifndef whitespace
30#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
31#endif
32
33#ifdef CTRL
34# undef CTRL
35#endif
36#ifdef UNCTRL
37# undef UNCTRL
38#endif
39
40/* Some character stuff. */
41#define control_character_threshold 0x020 /* Smaller than this is control. */
42#define control_character_mask 0x1f /* 0x20 - 1 */
43#define meta_character_threshold 0x07f /* Larger than this is Meta. */
44#define control_character_bit 0x40 /* 0x000000, must be off. */
45#define meta_character_bit 0x080 /* x0000000, must be on. */
46#define largest_char 255 /* Largest character value. */
47
48#define CTRL_CHAR(c) ((c) < control_character_threshold && (((c) & 0x80) == 0))
49#define META_CHAR(c) ((c) > meta_character_threshold && (c) <= largest_char)
50
51#define CTRL(c) ((c) & control_character_mask)
52#define META(c) ((c) | meta_character_bit)
53
54#define UNMETA(c) ((c) & (~meta_character_bit))
55#define UNCTRL(c) _rl_to_upper(((c)|control_character_bit))
56
57#ifndef UCHAR_MAX
58# define UCHAR_MAX 255
59#endif
60#ifndef CHAR_MAX
61# define CHAR_MAX 127
62#endif
63
64/* use this as a proxy for C89 */
65#if defined (HAVE_STDLIB_H) && defined (HAVE_STRING_H)
66# define IN_CTYPE_DOMAIN(c) 1
67# define NON_NEGATIVE(c) 1
68#else
69# define IN_CTYPE_DOMAIN(c) ((c) >= 0 && (c) <= CHAR_MAX)
70# define NON_NEGATIVE(c) ((unsigned char)(c) == (c))
71#endif
72
73#if !defined (isxdigit) && !defined (HAVE_ISXDIGIT) && !defined (__cplusplus)
74# define isxdigit(c) (isdigit((unsigned char)(c)) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
75#endif
76
77/* Some systems define these; we want our definitions. */
78#undef ISPRINT
79
80/* Beware: these only work with single-byte ASCII characters. */
81
82#define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum ((unsigned char)c))
83#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha ((unsigned char)c))
84#define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit ((unsigned char)c))
85#define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower ((unsigned char)c))
86#define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint ((unsigned char)c))
87#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper ((unsigned char)c))
88#define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit ((unsigned char)c))
89
90#define _rl_lowercase_p(c) (NON_NEGATIVE(c) && ISLOWER(c))
91#define _rl_uppercase_p(c) (NON_NEGATIVE(c) && ISUPPER(c))
92#define _rl_digit_p(c) ((c) >= '0' && (c) <= '9')
93
94#define _rl_alphabetic_p(c) (NON_NEGATIVE(c) && ISALNUM(c))
95#define _rl_pure_alphabetic(c) (NON_NEGATIVE(c) && ISALPHA(c))
96
97#ifndef _rl_to_upper
98# define _rl_to_upper(c) (_rl_lowercase_p(c) ? toupper((unsigned char)(c)) : (c))
99# define _rl_to_lower(c) (_rl_uppercase_p(c) ? tolower((unsigned char)(c)) : (c))
100#endif
101
102#ifndef _rl_digit_value
103# define _rl_digit_value(x) ((x) - '0')
104#endif
105
106#ifndef _rl_isident
107# define _rl_isident(c) (ISALNUM(c) || (c) == '_')
108#endif
109
110#ifndef ISOCTAL
111# define ISOCTAL(c) ((c) >= '0' && (c) <= '7')
112#endif
113#define OCTVALUE(c) ((c) - '0')
114
115#define HEXVALUE(c) \
116 (((c) >= 'a' && (c) <= 'f') \
117 ? (c)-'a'+10 \
118 : (c) >= 'A' && (c) <= 'F' ? (c)-'A'+10 : (c)-'0')
119
120#ifndef NEWLINE
121#define NEWLINE '\n'
122#endif
123
124#ifndef RETURN
125#define RETURN CTRL('M')
126#endif
127
128#ifndef RUBOUT
129#define RUBOUT 0x7f
130#endif
131
132#ifndef TAB
133#define TAB '\t'
134#endif
135
136#ifdef ABORT_CHAR
137#undef ABORT_CHAR
138#endif
139#define ABORT_CHAR CTRL('G')
140
141#ifdef PAGE
142#undef PAGE
143#endif
144#define PAGE CTRL('L')
145
146#ifdef SPACE
147#undef SPACE
148#endif
149#define SPACE ' ' /* XXX - was 0x20 */
150
151#ifdef ESC
152#undef ESC
153#endif
154#define ESC CTRL('[')
155
156#endif /* _CHARDEFS_H_ */
157