sh.char.h revision 69408
169408Sache/* $Header: /src/pub/tcsh/sh.char.h,v 3.17 2000/11/11 23:03:35 christos Exp $ */
259243Sobrien/*
359243Sobrien * sh.char.h: Table for spotting special characters quickly
459243Sobrien * 	      Makes for very obscure but efficient coding.
559243Sobrien */
659243Sobrien/*-
759243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
859243Sobrien * All rights reserved.
959243Sobrien *
1059243Sobrien * Redistribution and use in source and binary forms, with or without
1159243Sobrien * modification, are permitted provided that the following conditions
1259243Sobrien * are met:
1359243Sobrien * 1. Redistributions of source code must retain the above copyright
1459243Sobrien *    notice, this list of conditions and the following disclaimer.
1559243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1659243Sobrien *    notice, this list of conditions and the following disclaimer in the
1759243Sobrien *    documentation and/or other materials provided with the distribution.
1859243Sobrien * 3. All advertising materials mentioning features or use of this software
1959243Sobrien *    must display the following acknowledgement:
2059243Sobrien *	This product includes software developed by the University of
2159243Sobrien *	California, Berkeley and its contributors.
2259243Sobrien * 4. Neither the name of the University nor the names of its contributors
2359243Sobrien *    may be used to endorse or promote products derived from this software
2459243Sobrien *    without specific prior written permission.
2559243Sobrien *
2659243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2759243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2859243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2959243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3059243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3159243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3259243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3359243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3459243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3559243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3659243Sobrien * SUCH DAMAGE.
3759243Sobrien */
3859243Sobrien#ifndef _h_sh_char
3959243Sobrien#define _h_sh_char
4059243Sobrien#if defined(NeXT) && defined(NLS)
4159243Sobrien# include <appkit/NXCType.h>
4259243Sobrien#else
4359243Sobrien# include <ctype.h>
4459243Sobrien#endif
4559243Sobrien
4659243Sobrientypedef unsigned char tcshuc;
4759243Sobrien#ifdef _MINIX
4859243Sobrien# undef _SP
4959243Sobrien#endif /* _MINIX */
5059243Sobrienextern unsigned short _cmap[];
5159243Sobrien#if defined(DSPMBYTE)
5259243Sobrienextern unsigned short _mbmap[];
5359243Sobrien# define CHECK_MBYTEVAR	STRdspmbyte
5459243Sobrien#endif
5559243Sobrienextern unsigned short _cmap_c[];
5659243Sobrienextern unsigned short _cmap_mbyte[];
5759243Sobrienextern short _enable_mbdisp;
5859243Sobrienextern unsigned short _mbmap[];
5959243Sobrienextern unsigned short _mbmap_euc[];
6059243Sobrienextern unsigned short _mbmap_sjis[];
6159243Sobrien/* VARIABLE Check str */
6259243Sobrien/* same compiler require #define even not define DSPMBYTE */
6359243Sobrien#define _MB1	0x0001
6459243Sobrien#define _MB2	0x0002
6559243Sobrien
6659243Sobrien#ifndef NLS
6759243Sobrienextern tcshuc _cmap_lower[], _cmap_upper[];
6859243Sobrien
6959243Sobrien#endif
7059243Sobrien
7159243Sobrien#define	_QF	0x0001		/* '" (Forward quotes) */
7259243Sobrien#define	_QB	0x0002		/* ` (Backquote) */
7359243Sobrien#define	_SP	0x0004		/* space and tab */
7459243Sobrien#define	_NL	0x0008		/* \n */
7559243Sobrien#define	_META	0x0010		/* lex meta characters, sp #'`";&<>()|\t\n */
7659243Sobrien#define	_GLOB	0x0020		/* glob characters, *?{[` */
7759243Sobrien#define	_ESC	0x0040		/* \ */
7859243Sobrien#define	_DOL	0x0080		/* $ */
7959243Sobrien#define	_DIG  	0x0100		/* 0-9 */
8059243Sobrien#define	_LET  	0x0200		/* a-z, A-Z, _ */
8159243Sobrien#define	_UP   	0x0400		/* A-Z */
8259243Sobrien#define	_DOW  	0x0800		/* a-z */
8359243Sobrien#define	_XD 	0x1000		/* 0-9, a-f, A-F */
8459243Sobrien#define	_CMD	0x2000		/* lex end of command chars, ;&(|` */
8559243Sobrien#define _CTR	0x4000		/* control */
8659243Sobrien#define _PUN	0x8000		/* punctuation */
8759243Sobrien
8859243Sobrien#if defined(SHORT_STRINGS) && defined(KANJI)
8969408Sache# define ASC(ch) ch
9069408Sache# define CTL_ESC(ch) ch
9169408Sache# define cmap(c, bits)	\
9259243Sobrien	((((c) & QUOTE) || ((c & 0x80) && adrof(STRnokanji))) ? \
9359243Sobrien	0 : (_cmap[(tcshuc)(c)] & (bits)))
9469408Sache#else /* SHORT_STRINGS && KANJI */
9569408Sache# ifdef IS_ASCII
9669408Sache#  define ASC(ch) ch
9769408Sache#  define CTL_ESC(ch) ch
9869408Sache#  define cmap(c, bits)	\
9959243Sobrien	(((c) & QUOTE) ? 0 : (_cmap[(tcshuc)(c)] & (bits)))
10069408Sache# else /* IS_ASCII */
10159243Sobrien/* "BS2000 OSD" is a POSIX on a main frame using a EBCDIC char set */
10269408Sache/* "OS/390 USS" is a POSIX on a main frame using an IBM1047 char set */
10359243Sobrienextern unsigned short _toascii[256];
10459243Sobrienextern unsigned short _toebcdic[256];
10559243Sobrien
10659243Sobrien/* mainly for comparisons if (ASC(ch)=='\177')... */
10769408Sache#  define ASC(ch)     _toascii[(tcshuc)ch]
10859243Sobrien
10959243Sobrien/* Literal escapes ('\010') must be mapped to EBCDIC,
11059243Sobrien * for C-Escapes   ('\b'), the compiler already does it.
11159243Sobrien */
11269408Sache#  define CTL_ESC(ch) _toebcdic[(tcshuc)ch]
11359243Sobrien
11469408Sache#  define cmap(c, bits)	\
11559243Sobrien	(((c) & QUOTE) ? 0 : (_cmap[_toascii[(tcshuc)(c)]] & (bits)))
11669408Sache# endif /* IS_ASCII */
11769408Sache#endif /* SHORT_STRINGS && KANJI */
11859243Sobrien
11959243Sobrien#define isglob(c)	cmap(c, _GLOB)
12059243Sobrien#define isspc(c)	cmap(c, _SP)
12159243Sobrien#define ismeta(c)	cmap(c, _META)
12259243Sobrien#define iscmdmeta(c)	cmap(c, _CMD)
12359243Sobrien#define letter(c)	(((Char)(c) & QUOTE) ? 0 : \
12459243Sobrien			 (isalpha((tcshuc) (c)) || (c) == '_'))
12559243Sobrien#define alnum(c)	(((Char)(c) & QUOTE) ? 0 : \
12659243Sobrien		         (isalnum((tcshuc) (c)) || (c) == '_'))
12759243Sobrien
12859243Sobrien#if defined(DSPMBYTE)
12959243Sobrien# define IsmbyteU(c)	(Ismbyte1((Char)(c))||(Ismbyte2((Char)(c))&&((c)&0200)))
13059243Sobrien#endif
13159243Sobrien
13259243Sobrien#ifdef NLS
13359243Sobrien# ifdef NeXT
13459243Sobrien#  define Isspace(c)	(((Char)(c) & QUOTE) ? 0 : NXIsSpace((unsigned) (c)))
13559243Sobrien#  define Isdigit(c)	(((Char)(c) & QUOTE) ? 0 : NXIsDigit((unsigned) (c)))
13659243Sobrien#  define Isalpha(c)	(((Char)(c) & QUOTE) ? 0 : NXIsAlpha((unsigned) (c)))
13759243Sobrien#  define Islower(c)	(((Char)(c) & QUOTE) ? 0 : NXIsLower((unsigned) (c)))
13859243Sobrien#  define Isupper(c)	(((Char)(c) & QUOTE) ? 0 : NXIsUpper((unsigned) (c)))
13959243Sobrien#  define Tolower(c) 	(((Char)(c) & QUOTE) ? 0 : NXToLower((unsigned) (c)))
14059243Sobrien#  define Toupper(c) 	(((Char)(c) & QUOTE) ? 0 : NXToUpper((unsigned) (c)))
14159243Sobrien#  define Isxdigit(c)	(((Char)(c) & QUOTE) ? 0 : NXIsXDigit((unsigned) (c)))
14259243Sobrien#if defined(DSPMBYTE)
14359243Sobrien#  define IscntrlM(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsCntrl((unsigned) (c)))
14459243Sobrien#  define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
14559243Sobrien#  define IsprintM(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPrint((unsigned) (c)))
14659243Sobrien#  define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
14759243Sobrien#else
14859243Sobrien#  define Isalnum(c)	(((Char)(c) & QUOTE) ? 0 : NXIsAlNum((unsigned) (c)))
14959243Sobrien#  define Iscntrl(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsCntrl((unsigned) (c)))
15059243Sobrien#  define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPrint((unsigned) (c)))
15159243Sobrien#endif /* !defined(DSPMBYTE) */
15259243Sobrien#  define Ispunct(c) 	(((Char)(c) & QUOTE) ? 0 : NXIsPunct((unsigned) (c)))
15359243Sobrien# else /* !NeXT */
15469408Sache#  ifndef WINNT_NATIVE
15559243Sobrien#   define Isspace(c)	(((Char)(c) & QUOTE) ? 0 : isspace((tcshuc) (c)))
15659243Sobrien#   define Isdigit(c)	(((Char)(c) & QUOTE) ? 0 : isdigit((tcshuc) (c)))
15759243Sobrien#   define Isalpha(c)	(((Char)(c) & QUOTE) ? 0 : isalpha((tcshuc) (c)))
15859243Sobrien#   define Islower(c)	(((Char)(c) & QUOTE) ? 0 : islower((tcshuc) (c)))
15959243Sobrien#   define Isupper(c)	(((Char)(c) & QUOTE) ? 0 : isupper((tcshuc) (c)))
16059243Sobrien#   define Tolower(c) 	(((Char)(c) & QUOTE) ? 0 : tolower((tcshuc) (c)))
16159243Sobrien#   define Toupper(c) 	(((Char)(c) & QUOTE) ? 0 : toupper((tcshuc) (c)))
16259243Sobrien#   define Isxdigit(c)	(((Char)(c) & QUOTE) ? 0 : isxdigit((tcshuc) (c)))
16359243Sobrien#   define Isalnum(c)	(((Char)(c) & QUOTE) ? 0 : isalnum((tcshuc) (c)))
16459243Sobrien#if defined(DSPMBYTE)
16559243Sobrien#   define IscntrlM(c) 	(((Char)(c) & QUOTE) ? 0 : iscntrl((tcshuc) (c)))
16659243Sobrien#   define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
16759243Sobrien#else
16859243Sobrien#   define Iscntrl(c) 	(((Char)(c) & QUOTE) ? 0 : iscntrl((tcshuc) (c)))
16959243Sobrien#endif /* !defined(DSPMBYTE) */
17059243Sobrien#   if SOLARIS2 == 24
17159243Sobrien    /*
17259243Sobrien     * From <casper@fwi.uva.nl> Casper Dik:
17359243Sobrien     * In Solaris 2.4, isprint('\t') returns true after setlocal(LC_ALL,"").
17459243Sobrien     * This breaks commandline editing when you include tabs.
17559243Sobrien     * (This is in the en_US locale).
17659243Sobrien     */
17759243Sobrien#if defined(DSPMBYTE)
17859243Sobrien#    define IsprintM(c) 	(((Char)(c) & QUOTE) ? 0 : \
17959243Sobrien				(isprint((tcshuc) (c)) && (c) != '\t'))
18059243Sobrien#else
18159243Sobrien#    define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : \
18259243Sobrien				(isprint((tcshuc) (c)) && (c) != '\t'))
18359243Sobrien#endif /* !defined(DSPMBYTE) */
18459243Sobrien#   else
18559243Sobrien#if defined(DSPMBYTE)
18659243Sobrien#    define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : isprint((tcshuc) (c)))
18759243Sobrien#else
18859243Sobrien#    define Isprint(c) 	(((Char)(c) & QUOTE) ? 0 : isprint((tcshuc) (c)))
18959243Sobrien#endif /* !defined(DSPMBYTE) */
19059243Sobrien#   endif /* SOLARIS2 == 24 */
19159243Sobrien#if defined(DSPMBYTE)
19259243Sobrien#   define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
19359243Sobrien#endif /* !defined(DSPMBYTE) */
19459243Sobrien#    define Ispunct(c) 	(((Char)(c) & QUOTE) ? 0 : ispunct((tcshuc) (c)))
19569408Sache#  else /* WINNT_NATIVE */
19659243Sobrien#   define Isspace(c) (((Char)(c) & QUOTE) ? 0 : isspace( oem_it((tcshuc)(c))))
19759243Sobrien#   define Isdigit(c) (((Char)(c) & QUOTE) ? 0 : isdigit( oem_it((tcshuc)(c))))
19859243Sobrien#   define Isalpha(c) (((Char)(c) & QUOTE) ? 0 : isalpha( oem_it((tcshuc)(c))))
19959243Sobrien#   define Islower(c) (((Char)(c) & QUOTE) ? 0 : islower( oem_it((tcshuc)(c))))
20059243Sobrien#   define Isupper(c) (((Char)(c) & QUOTE) ? 0 : isupper( oem_it((tcshuc)(c))))
20159243Sobrien#   define Tolower(c) (((Char)(c) & QUOTE) ? 0 : tolower( oem_it((tcshuc)(c))))
20259243Sobrien#   define Toupper(c) (((Char)(c) & QUOTE) ? 0 : toupper( oem_it((tcshuc)(c))))
20359243Sobrien#   define Isxdigit(c)(((Char)(c) & QUOTE) ? 0 : isxdigit(oem_it((tcshuc)(c))))
20459243Sobrien#   define Isalnum(c) (((Char)(c) & QUOTE) ? 0 : isalnum( oem_it((tcshuc)(c))))
20559243Sobrien#   define Ispunct(c) (((Char)(c) & QUOTE) ? 0 : ispunct( oem_it((tcshuc)(c))))
20659243Sobrien#if defined(DSPMBYTE)
20759243Sobrien#   define IscntrlM(c) (((Char)(c) & QUOTE) ? 0 : iscntrl( oem_it((tcshuc)(c))))
20859243Sobrien#   define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
20959243Sobrien#   define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : isprint( oem_it((tcshuc)(c))))
21059243Sobrien#   define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
21159243Sobrien#else
21259243Sobrien#   define Iscntrl(c) (((Char)(c) & QUOTE) ? 0 : iscntrl( oem_it((tcshuc)(c))))
21359243Sobrien#   define Isprint(c) (((Char)(c) & QUOTE) ? 0 : isprint( oem_it((tcshuc)(c))))
21459243Sobrien#endif /* !defined(DSPMBYTE) */
21569408Sache#  endif /* WINNT_NATIVE */
21659243Sobrien# endif /* !NeXT */
21759243Sobrien#else /* !NLS */
21859243Sobrien# define Isspace(c)	cmap(c, _SP|_NL)
21959243Sobrien# define Isdigit(c)	cmap(c, _DIG)
22059243Sobrien# define Isalpha(c)	(cmap(c,_LET) && !(((c) & META) && AsciiOnly))
22159243Sobrien# define Islower(c)	(cmap(c,_DOW) && !(((c) & META) && AsciiOnly))
22259243Sobrien# define Isupper(c)	(cmap(c, _UP) && !(((c) & META) && AsciiOnly))
22369408Sache# ifdef IS_ASCII
22469408Sache#  define Tolower(c)	(_cmap_lower[(tcshuc)(c)])
22569408Sache#  define Toupper(c)	(_cmap_upper[(tcshuc)(c)])
22669408Sache# else
22759243Sobrien/* "BS2000 OSD" is a POSIX on a main frame using a EBCDIC char set */
22869408Sache#  define Tolower(c)	(_cmap_lower[_toascii[(tcshuc)(c)]])
22969408Sache#  define Toupper(c)	(_cmap_upper[_toascii[(tcshuc)(c)]])
23069408Sache# endif
23159243Sobrien# define Isxdigit(c)	cmap(c, _XD)
23259243Sobrien# define Isalnum(c)	(cmap(c, _DIG|_LET) && !(((Char)(c) & META) && AsciiOnly))
23359243Sobrien#if defined(DSPMBYTE)
23459243Sobrien# define IscntrlM(c)	(cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
23559243Sobrien# define Iscntrl(c)	( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
23659243Sobrien# define IsprintM(c)	(!cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
23759243Sobrien# define Isprint(c)	( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
23859243Sobrien#else
23959243Sobrien# define Iscntrl(c)	(cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
24059243Sobrien# define Isprint(c)	(!cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
24159243Sobrien#endif /* !defined(DSPMBYTE) */
24259243Sobrien# define Ispunct(c)	(cmap(c,_PUN) && !(((c) & META) && AsciiOnly))
24359243Sobrien
24459243Sobrien#endif /* !NLS */
24559243Sobrien
24659243Sobrien#if defined(DSPMBYTE)
24759243Sobrien# define Ismbyte1(c)	((_mbmap[(c) & 0377] & _MB1) ? 1 : 0)
24859243Sobrien# define Ismbyte2(c)	((_mbmap[(c) & 0377] & _MB2) ? 1 : 0)
24959243Sobrien#endif
25059243Sobrien
25159243Sobrien#endif /* _h_sh_char */
252