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