xref: /freebsd/contrib/tcsh/sh.char.h (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*
2  * sh.char.h: Table for spotting special characters quickly
3  * 	      Makes for very obscure but efficient coding.
4  */
5 /*-
6  * Copyright (c) 1980, 1991 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 #ifndef _h_sh_char
34 #define _h_sh_char
35 #if defined(NeXT) && defined(NLS)
36 # include <appkit/NXCType.h>
37 #else
38 # include <ctype.h>
39 # ifdef WIDE_STRINGS
40 #  ifdef HAVE_WCTYPE_H
41 #   include <wctype.h>
42 #  else
43 #   include <wchar.h>
44 #  endif
45 # endif
46 #endif
47 
48 typedef unsigned char tcshuc;
49 #ifdef _MINIX
50 # undef _SP
51 #endif /* _MINIX */
52 extern unsigned short _cmap[];
53 #if defined(DSPMBYTE)
54 # define CHECK_MBYTEVAR	STRdspmbyte
55 #endif
56 extern unsigned short _cmap_c[];
57 extern unsigned short _cmap_mbyte[];
58 extern short _enable_mbdisp;
59 extern unsigned short _mbmap[];
60 extern unsigned short _mbmap_euc[];
61 extern unsigned short _mbmap_sjis[];
62 extern unsigned short _mbmap_big5[];
63 extern unsigned short _mbmap_utf8[];
64 /* VARIABLE Check str */
65 /* same compiler require #define even not define DSPMBYTE */
66 #undef	_MB1
67 #define _MB1	0x0001
68 #undef	_MB2
69 #define _MB2	0x0002
70 
71 #ifndef NLS
72 extern tcshuc _cmap_lower[], _cmap_upper[];
73 
74 #endif
75 
76 #ifndef __QNXNTO__
77 #undef	_QF
78 #define	_QF	0x0001		/* '" (Forward quotes) */
79 #undef	_QB
80 #define	_QB	0x0002		/* ` (Backquote) */
81 #undef	_SP
82 #define	_SP	0x0004		/* space and tab */
83 #else
84 #undef	_XD
85 #define	_XD	0x0001		/* As in <ctype.h> */
86 #undef	_UP
87 #define	_UP	0x0002		/* As in <ctype.h> */
88 #undef	_SP
89 #define	_SP	0x0004		/* As in <ctype.h> */
90 #endif
91 #undef	_NL
92 #define	_NL	0x0008		/* \n */
93 #undef	_META
94 #define	_META	0x0010		/* lex meta characters, sp #'`";&<>()|\t\n */
95 #undef	_GLOB
96 #define	_GLOB	0x0020		/* glob characters, *?{[` */
97 #undef	_ESC
98 #define	_ESC	0x0040		/* \ */
99 #undef	_DOL
100 #define	_DOL	0x0080		/* $ */
101 #undef	_DIG
102 #define	_DIG  	0x0100		/* 0-9 */
103 #undef	_LET
104 #define	_LET  	0x0200		/* a-z, A-Z, _, or locale-specific */
105 #ifndef __QNXNTO__
106 #undef	_UP
107 #define	_UP   	0x0400		/* A-Z, or locale-specific */
108 #else
109 #undef	_QF
110 #define	_QF	0x0400		/* '" (Forward quotes) */
111 #endif
112 #undef	_DOW
113 #define	_DOW  	0x0800		/* a-z, or locale-specific */
114 #ifndef __QNXNTO__
115 #undef	_XD
116 #define	_XD 	0x1000		/* 0-9, a-f, A-F */
117 #else
118 #undef	_QB
119 #define	_QB	0x1000		/* 0-9, a-f, A-F */
120 #endif
121 #undef	_CMD
122 #define	_CMD	0x2000		/* lex end of command chars, ;&(|` */
123 #undef	_CTR
124 #define _CTR	0x4000		/* control */
125 #undef	_PUN
126 #define _PUN	0x8000		/* punctuation */
127 
128 #ifdef IS_ASCII
129 # define ASC(ch) (ch)
130 # define CTL_ESC(ch) (ch)
131 #else
132 # ifdef _OSD_POSIX
133 /* "BS2000 OSD" is a POSIX on a main frame using a EBCDIC char set */
134 #   include <ascii_ebcdic.h>
135 # else
136 /* "OS/390 USS" is a POSIX on a main frame using an IBM1047 char set */
137 # endif
138   extern unsigned short _toascii[256];
139   extern unsigned short _toebcdic[256];
140 
141 /* mainly for comparisons if (ASC(ch)=='\177')... */
142 #  define ASC(ch)     _toascii[(tcshuc)(ch)]
143 
144 /* Literal escapes ('\010') must be mapped to EBCDIC,
145  * for C-Escapes   ('\b'), the compiler already does it.
146  */
147 #  define CTL_ESC(ch) _toebcdic[(tcshuc)(ch)]
148 #endif /*IS_ASCII*/
149 
150 #ifdef WIDE_STRINGS
151 # define cmap(c, bits)	\
152     (((c) & QUOTE) || (unsigned int)(c) >= 0x0080 ? 0 : \
153 	(_cmap[(tcshuc)ASC(c)] & (bits)))
154 #elif defined(SHORT_STRINGS) && defined(KANJI)
155 #  define cmap(c, bits)	\
156     ((((c) & QUOTE) || ((tcshuc)(ASC(c) & 0x80) && adrof(STRnokanji))) ? \
157 	0 : (_cmap[(tcshuc)ASC(c)] & (bits)))
158 #else /* SHORT_STRINGS && KANJI */
159 # define cmap(c, bits)	\
160     (((c) & QUOTE) ? 0 : (_cmap[(tcshuc)ASC(c)] & (bits)))
161 #endif /* SHORT_STRINGS && KANJI */
162 
163 #define isglob(c)	cmap((c), _GLOB)
164 #define isspc(c)	cmap((c), _SP)
165 #define ismeta(c)	cmap((c), _META)
166 #define iscmdmeta(c)	cmap((c), _CMD)
167 #ifdef WIDE_STRINGS
168 #define letter(c)	(((c) & QUOTE) ? 0 :  \
169 			 (iswalpha((c)) || (c) == '_'))
170 #define alnum(c)	(((c) & QUOTE) ? 0 :  \
171 		         (iswalnum((c)) || (c) == '_'))
172 #else
173 #define letter(c)	(((Char)(c) & QUOTE) ? 0 :  \
174 			 ((isalpha((c)) && !(cmap((c), _PUN))) \
175 			  || (c) == '_'))
176 #define alnum(c)	(((Char)(c) & QUOTE) ? 0 :  \
177 		         ((isalnum((c)) && !(cmap((c), _PUN))) \
178 			  || (c) == '_'))
179 
180 #endif
181 
182 #if defined(DSPMBYTE)
183 # define IsmbyteU(c)	(Ismbyte1((Char)(c))||(Ismbyte2((Char)(c))&&((c)&0200)))
184 #endif
185 
186 #ifdef NLS
187 # ifdef WIDE_STRINGS
188 #  define Isspace(c)	(((c) & QUOTE) ? 0 : iswspace(c))
189 #  define Isdigit(c)	(((c) & QUOTE) ? 0 : iswdigit(c))
190 #  define Isalpha(c)	(((c) & QUOTE) ? 0 : iswalpha(c))
191 #  define Islower(c)	(((c) & QUOTE) ? 0 : iswlower(c))
192 #  define Isupper(c)	(((c) & QUOTE) ? 0 : iswupper(c))
193 #  define Tolower(c) 	(((c) & QUOTE) ? 0 : (wchar_t)towlower(c))
194 #  define Toupper(c) 	(((c) & QUOTE) ? 0 : (wchar_t)towupper(c))
195 #  define Isxdigit(c)	(((c) & QUOTE) ? 0 : iswxdigit(c))
196 #  define Isalnum(c)	(((c) & QUOTE) ? 0 : iswalnum(c))
197 #  define Iscntrl(c) 	(((c) & QUOTE) ? 0 : iswcntrl(c))
198 #  define Isprint(c) 	(((c) & QUOTE) ? 0 : iswprint(c))
199 #  define Ispunct(c) 	(((c) & QUOTE) ? 0 : iswpunct(c))
200 # elif defined (NeXT)
201 #  define Isspace(c)	(((Char)(c) & QUOTE) ? 0 : NXIsSpace((unsigned) (c)))
202 #  define Isdigit(c)	(((Char)(c) & QUOTE) ? 0 : NXIsDigit((unsigned) (c)))
203 #  define Isalpha(c)	(((Char)(c) & QUOTE) ? 0 : NXIsAlpha((unsigned) (c)))
204 #  define Islower(c)	(((Char)(c) & QUOTE) ? 0 : NXIsLower((unsigned) (c)))
205 #  define Isupper(c)	(((Char)(c) & QUOTE) ? 0 : NXIsUpper((unsigned) (c)))
206 #  define Tolower(c) 	(((Char)(c) & QUOTE) ? 0 : NXToLower((unsigned) (c)))
207 #  define Toupper(c) 	(((Char)(c) & QUOTE) ? 0 : NXToUpper((unsigned) (c)))
208 #  define Isxdigit(c)	(((Char)(c) & QUOTE) ? 0 : NXIsXDigit((unsigned) (c)))
209 #if defined(DSPMBYTE)
210 #  define IscntrlM(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsCntrl((unsigned) (c)))
211 #  define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
212 #  define IsprintM(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPrint((unsigned) (c)))
213 #  define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
214 #else
215 #  define Isalnum(c)	(((Char)(c) & QUOTE) ? 0 : NXIsAlNum((unsigned) (c)))
216 #  define Iscntrl(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsCntrl((unsigned) (c)))
217 #  define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPrint((unsigned) (c)))
218 #endif /* !defined(DSPMBYTE) */
219 #  define Ispunct(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPunct((unsigned) (c)))
220 # else /* !NeXT */
221 #  ifndef WINNT_NATIVE
222 #   define Isspace(c)	(((Char)(c) & QUOTE) ? 0 : isspace((tcshuc) (c)))
223 #   define Isdigit(c)	(((Char)(c) & QUOTE) ? 0 : isdigit((tcshuc) (c)))
224 #   define Isalpha(c)	(((Char)(c) & QUOTE) ? 0 : isalpha((tcshuc) (c)))
225 #   define Islower(c)	(((Char)(c) & QUOTE) ? 0 : islower((tcshuc) (c)))
226 #   define Isupper(c)	(((Char)(c) & QUOTE) ? 0 : isupper((tcshuc) (c)))
227 #   define Tolower(c) 	(((Char)(c) & QUOTE) ? 0 : tolower((tcshuc) (c)))
228 #   define Toupper(c) 	(((Char)(c) & QUOTE) ? 0 : toupper((tcshuc) (c)))
229 #   define Isxdigit(c)	(((Char)(c) & QUOTE) ? 0 : isxdigit((tcshuc) (c)))
230 #   define Isalnum(c)	(((Char)(c) & QUOTE) ? 0 : isalnum((tcshuc) (c)))
231 #if defined(DSPMBYTE)
232 #   define IscntrlM(c) 	(((Char)(c) & QUOTE) ? 0 : iscntrl((tcshuc) (c)))
233 #   define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
234 #else
235 #   define Iscntrl(c) 	(((Char)(c) & QUOTE) ? 0 : iscntrl((tcshuc) (c)))
236 #endif /* !defined(DSPMBYTE) */
237 #   if SOLARIS2 == 24
238     /*
239      * From <casper@fwi.uva.nl> Casper Dik:
240      * In Solaris 2.4, isprint('\t') returns true after setlocal(LC_ALL,"").
241      * This breaks commandline editing when you include tabs.
242      * (This is in the en_US locale).
243      */
244 #if defined(DSPMBYTE)
245 #    define IsprintM(c) 	(((Char)(c) & QUOTE) ? 0 : \
246 				(isprint((tcshuc) (c)) && (c) != '\t'))
247 #else
248 #    define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : \
249 				(isprint((tcshuc) (c)) && (c) != '\t'))
250 #endif /* !defined(DSPMBYTE) */
251 #   else
252 #if defined(DSPMBYTE)
253 #    define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : isprint((tcshuc) (c)))
254 #else
255 #    define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : isprint((tcshuc) (c)))
256 #endif /* !defined(DSPMBYTE) */
257 #   endif /* SOLARIS2 == 24 */
258 #if defined(DSPMBYTE)
259 #   define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
260 #endif /* !defined(DSPMBYTE) */
261 #    define Ispunct(c) 	(((Char)(c) & QUOTE) ? 0 : ispunct((tcshuc) (c)))
262 #  else /* WINNT_NATIVE */
263 #   define Isspace(c) (((Char)(c) & QUOTE) ? 0 : isspace( oem_it((tcshuc)(c))))
264 #   define Isdigit(c) (((Char)(c) & QUOTE) ? 0 : isdigit( oem_it((tcshuc)(c))))
265 #   define Isalpha(c) (((Char)(c) & QUOTE) ? 0 : isalpha( oem_it((tcshuc)(c))))
266 #   define Islower(c) (((Char)(c) & QUOTE) ? 0 : islower( oem_it((tcshuc)(c))))
267 #   define Isupper(c) (((Char)(c) & QUOTE) ? 0 : isupper( oem_it((tcshuc)(c))))
268 #   define Tolower(c) (((Char)(c) & QUOTE) ? 0 : tolower( oem_it((tcshuc)(c))))
269 #   define Toupper(c) (((Char)(c) & QUOTE) ? 0 : toupper( oem_it((tcshuc)(c))))
270 #   define Isxdigit(c)(((Char)(c) & QUOTE) ? 0 : isxdigit(oem_it((tcshuc)(c))))
271 #   define Isalnum(c) (((Char)(c) & QUOTE) ? 0 : isalnum( oem_it((tcshuc)(c))))
272 #   define Ispunct(c) (((Char)(c) & QUOTE) ? 0 : ispunct( oem_it((tcshuc)(c))))
273 #if defined(DSPMBYTE)
274 #   define IscntrlM(c) (((Char)(c) & QUOTE) ? 0 : iscntrl( oem_it((tcshuc)(c))))
275 #   define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
276 #   define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : isprint( oem_it((tcshuc)(c))))
277 #   define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
278 #else
279 #   define Iscntrl(c) (((Char)(c) & QUOTE) ? 0 : iscntrl( oem_it((tcshuc)(c))))
280 #   define Isprint(c) (((Char)(c) & QUOTE) ? 0 : isprint( oem_it((tcshuc)(c))))
281 #endif /* !defined(DSPMBYTE) */
282 #  endif /* WINNT_NATIVE */
283 # endif /* !NeXT */
284 #else /* !NLS */
285 # define Isspace(c)	cmap((c), _SP|_NL)
286 # define Isdigit(c)	cmap((c), _DIG)
287 # define Isalpha(c)	(cmap((c),_LET) && !(((c) & META) && AsciiOnly))
288 # define Islower(c)	(cmap((c),_DOW) && !(((c) & META) && AsciiOnly))
289 # define Isupper(c)	(cmap((c), _UP) && !(((c) & META) && AsciiOnly))
290 # define Tolower(c)	(_cmap_lower[ASC(c)])
291 # define Toupper(c)	(_cmap_upper[ASC(c)])
292 # define Isxdigit(c)	cmap((c), _XD)
293 # define Isalnum(c)	(cmap((c), _DIG|_LET) && !(((Char)(c) & META) && AsciiOnly))
294 #if defined(DSPMBYTE)
295 # define IscntrlM(c)	(cmap((c),_CTR) && !(((c) & META) && AsciiOnly))
296 # define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
297 # define IsprintM(c)	(!cmap((c),_CTR) && !(((c) & META) && AsciiOnly))
298 # define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
299 #else
300 # define Iscntrl(c)	(cmap((c),_CTR) && !(((c) & META) && AsciiOnly))
301 # define Isprint(c)	(!cmap((c),_CTR) && !(((c) & META) && AsciiOnly))
302 #endif /* !defined(DSPMBYTE) */
303 # define Ispunct(c)	(cmap((c),_PUN) && !(((c) & META) && AsciiOnly))
304 
305 #endif /* !NLS */
306 
307 #if defined(DSPMBYTE)
308 # define Ismbyte1(c)	((_mbmap[(c) & 0377] & _MB1) ? 1 : 0)
309 # define Ismbyte2(c)	((_mbmap[(c) & 0377] & _MB2) ? 1 : 0)
310 #endif
311 
312 #endif /* _h_sh_char */
313