sh.char.h revision 59243
11592Srgrimes/* $Header: /src/pub/tcsh/sh.char.h,v 3.15 1998/09/18 16:09:07 christos Exp $ */
21592Srgrimes/*
31592Srgrimes * sh.char.h: Table for spotting special characters quickly
41592Srgrimes * 	      Makes for very obscure but efficient coding.
51592Srgrimes */
61592Srgrimes/*-
71592Srgrimes * Copyright (c) 1980, 1991 The Regents of the University of California.
81592Srgrimes * All rights reserved.
91592Srgrimes *
101592Srgrimes * Redistribution and use in source and binary forms, with or without
111592Srgrimes * modification, are permitted provided that the following conditions
121592Srgrimes * are met:
131592Srgrimes * 1. Redistributions of source code must retain the above copyright
141592Srgrimes *    notice, this list of conditions and the following disclaimer.
151592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161592Srgrimes *    notice, this list of conditions and the following disclaimer in the
171592Srgrimes *    documentation and/or other materials provided with the distribution.
181592Srgrimes * 3. All advertising materials mentioning features or use of this software
191592Srgrimes *    must display the following acknowledgement:
201592Srgrimes *	This product includes software developed by the University of
211592Srgrimes *	California, Berkeley and its contributors.
221592Srgrimes * 4. Neither the name of the University nor the names of its contributors
231592Srgrimes *    may be used to endorse or promote products derived from this software
241592Srgrimes *    without specific prior written permission.
251592Srgrimes *
261592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3529916Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361592Srgrimes * SUCH DAMAGE.
371592Srgrimes */
381592Srgrimes#ifndef _h_sh_char
391592Srgrimes#define _h_sh_char
401592Srgrimes#if defined(NeXT) && defined(NLS)
4131405Scharnier# include <appkit/NXCType.h>
4229916Smarkm#else
4331405Scharnier# include <ctype.h>
4431405Scharnier#endif
4550476Speter
461592Srgrimestypedef unsigned char tcshuc;
471592Srgrimes#ifdef _MINIX
481592Srgrimes# undef _SP
491592Srgrimes#endif /* _MINIX */
501592Srgrimesextern unsigned short _cmap[];
511592Srgrimes#if defined(DSPMBYTE)
521592Srgrimesextern unsigned short _mbmap[];
531592Srgrimes# define CHECK_MBYTEVAR	STRdspmbyte
541592Srgrimes#endif
551592Srgrimesextern unsigned short _cmap_c[];
561592Srgrimesextern unsigned short _cmap_mbyte[];
571592Srgrimesextern short _enable_mbdisp;
5829916Smarkmextern unsigned short _mbmap[];
591592Srgrimesextern unsigned short _mbmap_euc[];
601592Srgrimesextern unsigned short _mbmap_sjis[];
611592Srgrimes/* VARIABLE Check str */
621592Srgrimes/* same compiler require #define even not define DSPMBYTE */
631592Srgrimes#define _MB1	0x0001
641592Srgrimes#define _MB2	0x0002
651592Srgrimes
661592Srgrimes#ifndef NLS
671592Srgrimesextern tcshuc _cmap_lower[], _cmap_upper[];
681592Srgrimes
6911486Sdg#endif
701592Srgrimes
711592Srgrimes#define	_QF	0x0001		/* '" (Forward quotes) */
721592Srgrimes#define	_QB	0x0002		/* ` (Backquote) */
7331405Scharnier#define	_SP	0x0004		/* space and tab */
7431405Scharnier#define	_NL	0x0008		/* \n */
751592Srgrimes#define	_META	0x0010		/* lex meta characters, sp #'`";&<>()|\t\n */
761592Srgrimes#define	_GLOB	0x0020		/* glob characters, *?{[` */
771592Srgrimes#define	_ESC	0x0040		/* \ */
781592Srgrimes#define	_DOL	0x0080		/* $ */
791592Srgrimes#define	_DIG  	0x0100		/* 0-9 */
8031405Scharnier#define	_LET  	0x0200		/* a-z, A-Z, _ */
811592Srgrimes#define	_UP   	0x0400		/* A-Z */
821592Srgrimes#define	_DOW  	0x0800		/* a-z */
8351433Smarkm#define	_XD 	0x1000		/* 0-9, a-f, A-F */
841592Srgrimes#define	_CMD	0x2000		/* lex end of command chars, ;&(|` */
851592Srgrimes#define _CTR	0x4000		/* control */
861592Srgrimes#define _PUN	0x8000		/* punctuation */
871592Srgrimes
8851433Smarkm#if defined(SHORT_STRINGS) && defined(KANJI)
891592Srgrimes#define ASC(ch) ch
9056590Sshin#define CTL_ESC(ch) ch
9156590Sshin#define cmap(c, bits)	\
9256590Sshin	((((c) & QUOTE) || ((c & 0x80) && adrof(STRnokanji))) ? \
9356590Sshin	0 : (_cmap[(tcshuc)(c)] & (bits)))
9456590Sshin#else
951592Srgrimes#ifndef _OSD_POSIX
961592Srgrimes#define ASC(ch) ch
971592Srgrimes#define CTL_ESC(ch) ch
981592Srgrimes#define cmap(c, bits)	\
991592Srgrimes	(((c) & QUOTE) ? 0 : (_cmap[(tcshuc)(c)] & (bits)))
1001592Srgrimes#else /*_OSD_POSIX*/
1011592Srgrimes/* "BS2000 OSD" is a POSIX on a main frame using a EBCDIC char set */
10211486Sdgextern unsigned short _toascii[256];
1031592Srgrimesextern unsigned short _toebcdic[256];
1041592Srgrimes
1051592Srgrimes/* mainly for comparisons if (ASC(ch)=='\177')... */
10656590Sshin#define ASC(ch)     _toascii[(tcshuc)ch]
10756590Sshin
10856590Sshin/* Literal escapes ('\010') must be mapped to EBCDIC,
10956590Sshin * for C-Escapes   ('\b'), the compiler already does it.
11056590Sshin */
11156590Sshin#define CTL_ESC(ch) _toebcdic[(tcshuc)ch]
11256590Sshin
11356590Sshin#define cmap(c, bits)	\
11456590Sshin	(((c) & QUOTE) ? 0 : (_cmap[_toascii[(tcshuc)(c)]] & (bits)))
11556590Sshin#endif /*_OSD_POSIX*/
11656590Sshin#endif
11756590Sshin
11856590Sshin#define isglob(c)	cmap(c, _GLOB)
11990377Simp#define isspc(c)	cmap(c, _SP)
12090377Simp#define ismeta(c)	cmap(c, _META)
12190377Simp#define iscmdmeta(c)	cmap(c, _CMD)
12290377Simp#define letter(c)	(((Char)(c) & QUOTE) ? 0 : \
12390377Simp			 (isalpha((tcshuc) (c)) || (c) == '_'))
12490377Simp#define alnum(c)	(((Char)(c) & QUOTE) ? 0 : \
12590377Simp		         (isalnum((tcshuc) (c)) || (c) == '_'))
12690377Simp
12790377Simp#if defined(DSPMBYTE)
12890377Simp# define IsmbyteU(c)	(Ismbyte1((Char)(c))||(Ismbyte2((Char)(c))&&((c)&0200)))
1291592Srgrimes#endif
13051433Smarkm
1311592Srgrimes#ifdef NLS
13290377Simp# ifdef NeXT
1331592Srgrimes#  define Isspace(c)	(((Char)(c) & QUOTE) ? 0 : NXIsSpace((unsigned) (c)))
1341592Srgrimes#  define Isdigit(c)	(((Char)(c) & QUOTE) ? 0 : NXIsDigit((unsigned) (c)))
13556590Sshin#  define Isalpha(c)	(((Char)(c) & QUOTE) ? 0 : NXIsAlpha((unsigned) (c)))
1361592Srgrimes#  define Islower(c)	(((Char)(c) & QUOTE) ? 0 : NXIsLower((unsigned) (c)))
1371592Srgrimes#  define Isupper(c)	(((Char)(c) & QUOTE) ? 0 : NXIsUpper((unsigned) (c)))
1381592Srgrimes#  define Tolower(c) 	(((Char)(c) & QUOTE) ? 0 : NXToLower((unsigned) (c)))
1391592Srgrimes#  define Toupper(c) 	(((Char)(c) & QUOTE) ? 0 : NXToUpper((unsigned) (c)))
1401592Srgrimes#  define Isxdigit(c)	(((Char)(c) & QUOTE) ? 0 : NXIsXDigit((unsigned) (c)))
14124349Simp#if defined(DSPMBYTE)
1421592Srgrimes#  define IscntrlM(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsCntrl((unsigned) (c)))
14311486Sdg#  define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
14411486Sdg#  define IsprintM(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPrint((unsigned) (c)))
14511486Sdg#  define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
1461592Srgrimes#else
1471592Srgrimes#  define Isalnum(c)	(((Char)(c) & QUOTE) ? 0 : NXIsAlNum((unsigned) (c)))
1481592Srgrimes#  define Iscntrl(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsCntrl((unsigned) (c)))
1491592Srgrimes#  define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPrint((unsigned) (c)))
1501592Srgrimes#endif /* !defined(DSPMBYTE) */
1511592Srgrimes#  define Ispunct(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPunct((unsigned) (c)))
1521592Srgrimes# else /* !NeXT */
1531592Srgrimes#  ifndef WINNT
1541592Srgrimes#   define Isspace(c)	(((Char)(c) & QUOTE) ? 0 : isspace((tcshuc) (c)))
1551592Srgrimes#   define Isdigit(c)	(((Char)(c) & QUOTE) ? 0 : isdigit((tcshuc) (c)))
1561592Srgrimes#   define Isalpha(c)	(((Char)(c) & QUOTE) ? 0 : isalpha((tcshuc) (c)))
1571592Srgrimes#   define Islower(c)	(((Char)(c) & QUOTE) ? 0 : islower((tcshuc) (c)))
1581592Srgrimes#   define Isupper(c)	(((Char)(c) & QUOTE) ? 0 : isupper((tcshuc) (c)))
1591592Srgrimes#   define Tolower(c) 	(((Char)(c) & QUOTE) ? 0 : tolower((tcshuc) (c)))
1601592Srgrimes#   define Toupper(c) 	(((Char)(c) & QUOTE) ? 0 : toupper((tcshuc) (c)))
1611592Srgrimes#   define Isxdigit(c)	(((Char)(c) & QUOTE) ? 0 : isxdigit((tcshuc) (c)))
1621592Srgrimes#   define Isalnum(c)	(((Char)(c) & QUOTE) ? 0 : isalnum((tcshuc) (c)))
1631592Srgrimes#if defined(DSPMBYTE)
1641592Srgrimes#   define IscntrlM(c) 	(((Char)(c) & QUOTE) ? 0 : iscntrl((tcshuc) (c)))
1651592Srgrimes#   define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
1661592Srgrimes#else
1671592Srgrimes#   define Iscntrl(c) 	(((Char)(c) & QUOTE) ? 0 : iscntrl((tcshuc) (c)))
1681592Srgrimes#endif /* !defined(DSPMBYTE) */
1691592Srgrimes#   if SOLARIS2 == 24
1701592Srgrimes    /*
1711592Srgrimes     * From <casper@fwi.uva.nl> Casper Dik:
1721592Srgrimes     * In Solaris 2.4, isprint('\t') returns true after setlocal(LC_ALL,"").
1731592Srgrimes     * This breaks commandline editing when you include tabs.
1741592Srgrimes     * (This is in the en_US locale).
1751592Srgrimes     */
1761592Srgrimes#if defined(DSPMBYTE)
17711486Sdg#    define IsprintM(c) 	(((Char)(c) & QUOTE) ? 0 : \
17811486Sdg				(isprint((tcshuc) (c)) && (c) != '\t'))
17911486Sdg#else
18056590Sshin#    define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : \
18156590Sshin				(isprint((tcshuc) (c)) && (c) != '\t'))
1821592Srgrimes#endif /* !defined(DSPMBYTE) */
1831592Srgrimes#   else
1841592Srgrimes#if defined(DSPMBYTE)
18556590Sshin#    define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : isprint((tcshuc) (c)))
18611486Sdg#else
1871592Srgrimes#    define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : isprint((tcshuc) (c)))
18829916Smarkm#endif /* !defined(DSPMBYTE) */
1891592Srgrimes#   endif /* SOLARIS2 == 24 */
1901592Srgrimes#if defined(DSPMBYTE)
1911592Srgrimes#   define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
1921592Srgrimes#endif /* !defined(DSPMBYTE) */
1931592Srgrimes#    define Ispunct(c) 	(((Char)(c) & QUOTE) ? 0 : ispunct((tcshuc) (c)))
1941592Srgrimes#  else /* WINNT */
1951592Srgrimes#   define Isspace(c) (((Char)(c) & QUOTE) ? 0 : isspace( oem_it((tcshuc)(c))))
1961592Srgrimes#   define Isdigit(c) (((Char)(c) & QUOTE) ? 0 : isdigit( oem_it((tcshuc)(c))))
1971592Srgrimes#   define Isalpha(c) (((Char)(c) & QUOTE) ? 0 : isalpha( oem_it((tcshuc)(c))))
1981592Srgrimes#   define Islower(c) (((Char)(c) & QUOTE) ? 0 : islower( oem_it((tcshuc)(c))))
1991592Srgrimes#   define Isupper(c) (((Char)(c) & QUOTE) ? 0 : isupper( oem_it((tcshuc)(c))))
20090377Simp#   define Tolower(c) (((Char)(c) & QUOTE) ? 0 : tolower( oem_it((tcshuc)(c))))
2011592Srgrimes#   define Toupper(c) (((Char)(c) & QUOTE) ? 0 : toupper( oem_it((tcshuc)(c))))
2021592Srgrimes#   define Isxdigit(c)(((Char)(c) & QUOTE) ? 0 : isxdigit(oem_it((tcshuc)(c))))
2031592Srgrimes#   define Isalnum(c) (((Char)(c) & QUOTE) ? 0 : isalnum( oem_it((tcshuc)(c))))
20456590Sshin#   define Ispunct(c) (((Char)(c) & QUOTE) ? 0 : ispunct( oem_it((tcshuc)(c))))
20556590Sshin#if defined(DSPMBYTE)
2061592Srgrimes#   define IscntrlM(c) (((Char)(c) & QUOTE) ? 0 : iscntrl( oem_it((tcshuc)(c))))
2071592Srgrimes#   define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
2081592Srgrimes#   define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : isprint( oem_it((tcshuc)(c))))
2091592Srgrimes#   define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
2101592Srgrimes#else
2111592Srgrimes#   define Iscntrl(c) (((Char)(c) & QUOTE) ? 0 : iscntrl( oem_it((tcshuc)(c))))
2121592Srgrimes#   define Isprint(c) (((Char)(c) & QUOTE) ? 0 : isprint( oem_it((tcshuc)(c))))
2131592Srgrimes#endif /* !defined(DSPMBYTE) */
2141592Srgrimes#  endif /* WINNT */
21556590Sshin# endif /* !NeXT */
21656590Sshin#else /* !NLS */
21756590Sshin# define Isspace(c)	cmap(c, _SP|_NL)
21856590Sshin# define Isdigit(c)	cmap(c, _DIG)
21956590Sshin# define Isalpha(c)	(cmap(c,_LET) && !(((c) & META) && AsciiOnly))
22024191Simp# define Islower(c)	(cmap(c,_DOW) && !(((c) & META) && AsciiOnly))
2211592Srgrimes# define Isupper(c)	(cmap(c, _UP) && !(((c) & META) && AsciiOnly))
2221592Srgrimes#ifndef _OSD_POSIX
22363959Sume# define Tolower(c)	(_cmap_lower[(tcshuc)(c)])
22456590Sshin# define Toupper(c)	(_cmap_upper[(tcshuc)(c)])
22563959Sume#else /*_OSD_POSIX*/
22656590Sshin/* "BS2000 OSD" is a POSIX on a main frame using a EBCDIC char set */
22756590Sshin# define Tolower(c)     (_cmap_lower[_toascii[(tcshuc)(c)]])
22856590Sshin# define Toupper(c)     (_cmap_upper[_toascii[(tcshuc)(c)]])
22956590Sshin#endif /*_OSD_POSIX*/
23056590Sshin# define Isxdigit(c)	cmap(c, _XD)
23156590Sshin# define Isalnum(c)	(cmap(c, _DIG|_LET) && !(((Char)(c) & META) && AsciiOnly))
23256590Sshin#if defined(DSPMBYTE)
23356590Sshin# define IscntrlM(c)	(cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
23456590Sshin# define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
2351592Srgrimes# define IsprintM(c)	(!cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
23656590Sshin# define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
2371592Srgrimes#else
2381592Srgrimes# define Iscntrl(c)	(cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
2391592Srgrimes# define Isprint(c)	(!cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
24056590Sshin#endif /* !defined(DSPMBYTE) */
24156590Sshin# define Ispunct(c)	(cmap(c,_PUN) && !(((c) & META) && AsciiOnly))
24222455Simp
24322455Simp#endif /* !NLS */
2441592Srgrimes
2451592Srgrimes#if defined(DSPMBYTE)
2461592Srgrimes# define Ismbyte1(c)	((_mbmap[(c) & 0377] & _MB1) ? 1 : 0)
2471592Srgrimes# define Ismbyte2(c)	((_mbmap[(c) & 0377] & _MB2) ? 1 : 0)
2481592Srgrimes#endif
2491592Srgrimes
2501592Srgrimes#endif /* _h_sh_char */
2511592Srgrimes