/contrib/tcsh/sh.char.h

https://bitbucket.org/freebsd/freebsd-head/ · C Header · 313 lines · 251 code · 15 blank · 47 comment · 49 complexity · c31451599aff10e769834db71c5ccb61 MD5 · raw file

  1. /* $Header: /p/tcsh/cvsroot/tcsh/sh.char.h,v 3.36 2010/05/08 00:36:02 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. 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. #ifndef NLS
  71. extern tcshuc _cmap_lower[], _cmap_upper[];
  72. #endif
  73. #ifndef __QNXNTO__
  74. #undef _QF
  75. #define _QF 0x0001 /* '" (Forward quotes) */
  76. #undef _QB
  77. #define _QB 0x0002 /* ` (Backquote) */
  78. #undef _SP
  79. #define _SP 0x0004 /* space and tab */
  80. #else
  81. #undef _XD
  82. #define _XD 0x0001 /* As in <ctype.h> */
  83. #undef _UP
  84. #define _UP 0x0002 /* As in <ctype.h> */
  85. #undef _SP
  86. #define _SP 0x0004 /* As in <ctype.h> */
  87. #endif
  88. #undef _NL
  89. #define _NL 0x0008 /* \n */
  90. #undef _META
  91. #define _META 0x0010 /* lex meta characters, sp #'`";&<>()|\t\n */
  92. #undef _GLOB
  93. #define _GLOB 0x0020 /* glob characters, *?{[` */
  94. #undef _ESC
  95. #define _ESC 0x0040 /* \ */
  96. #undef _DOL
  97. #define _DOL 0x0080 /* $ */
  98. #undef _DIG
  99. #define _DIG 0x0100 /* 0-9 */
  100. #undef _LET
  101. #define _LET 0x0200 /* a-z, A-Z, _, or locale-specific */
  102. #ifndef __QNXNTO__
  103. #undef _UP
  104. #define _UP 0x0400 /* A-Z, or locale-specific */
  105. #else
  106. #undef _QF
  107. #define _QF 0x0400 /* '" (Forward quotes) */
  108. #endif
  109. #undef _DOW
  110. #define _DOW 0x0800 /* a-z, or locale-specific */
  111. #ifndef __QNXNTO__
  112. #undef _XD
  113. #define _XD 0x1000 /* 0-9, a-f, A-F */
  114. #else
  115. #undef _QB
  116. #define _QB 0x1000 /* 0-9, a-f, A-F */
  117. #endif
  118. #undef _CMD
  119. #define _CMD 0x2000 /* lex end of command chars, ;&(|` */
  120. #undef _CTR
  121. #define _CTR 0x4000 /* control */
  122. #undef _PUN
  123. #define _PUN 0x8000 /* punctuation */
  124. #ifdef IS_ASCII
  125. # define ASC(ch) (ch)
  126. # define CTL_ESC(ch) (ch)
  127. #else
  128. # ifdef _OSD_POSIX
  129. /* "BS2000 OSD" is a POSIX on a main frame using a EBCDIC char set */
  130. # include <ascii_ebcdic.h>
  131. # else
  132. /* "OS/390 USS" is a POSIX on a main frame using an IBM1047 char set */
  133. # endif
  134. extern unsigned short _toascii[256];
  135. extern unsigned short _toebcdic[256];
  136. /* mainly for comparisons if (ASC(ch)=='\177')... */
  137. # define ASC(ch) _toascii[(tcshuc)(ch)]
  138. /* Literal escapes ('\010') must be mapped to EBCDIC,
  139. * for C-Escapes ('\b'), the compiler already does it.
  140. */
  141. # define CTL_ESC(ch) _toebcdic[(tcshuc)(ch)]
  142. #endif /*IS_ASCII*/
  143. #ifdef WIDE_STRINGS
  144. # define cmap(c, bits) \
  145. (((c) & QUOTE) || (unsigned int)(c) >= 0x0080 ? 0 : \
  146. (_cmap[(tcshuc)ASC(c)] & (bits)))
  147. #elif defined(SHORT_STRINGS) && defined(KANJI)
  148. # define cmap(c, bits) \
  149. ((((c) & QUOTE) || ((tcshuc)(ASC(c) & 0x80) && adrof(STRnokanji))) ? \
  150. 0 : (_cmap[(tcshuc)ASC(c)] & (bits)))
  151. #else /* SHORT_STRINGS && KANJI */
  152. # define cmap(c, bits) \
  153. (((c) & QUOTE) ? 0 : (_cmap[(tcshuc)ASC(c)] & (bits)))
  154. #endif /* SHORT_STRINGS && KANJI */
  155. #define isglob(c) cmap((c), _GLOB)
  156. #define isspc(c) cmap((c), _SP)
  157. #define ismeta(c) cmap((c), _META)
  158. #define iscmdmeta(c) cmap((c), _CMD)
  159. #ifdef WIDE_STRINGS
  160. #define letter(c) (((c) & QUOTE) ? 0 : \
  161. (iswalpha((tcshuc) (c)) || (c) == '_'))
  162. #define alnum(c) (((c) & QUOTE) ? 0 : \
  163. (iswalnum((tcshuc) (c)) || (c) == '_'))
  164. #else
  165. #define letter(c) (((Char)(c) & QUOTE) ? 0 : \
  166. ((isalpha((tcshuc) (c)) && !(cmap((c), _PUN))) \
  167. || (c) == '_'))
  168. #define alnum(c) (((Char)(c) & QUOTE) ? 0 : \
  169. ((isalnum((tcshuc) (c)) && !(cmap((c), _PUN))) \
  170. || (c) == '_'))
  171. #endif
  172. #if defined(DSPMBYTE)
  173. # define IsmbyteU(c) (Ismbyte1((Char)(c))||(Ismbyte2((Char)(c))&&((c)&0200)))
  174. #endif
  175. #ifdef NLS
  176. # ifdef WIDE_STRINGS
  177. # define Isspace(c) (((c) & QUOTE) ? 0 : iswspace(c))
  178. # define Isdigit(c) (((c) & QUOTE) ? 0 : iswdigit(c))
  179. # define Isalpha(c) (((c) & QUOTE) ? 0 : iswalpha(c))
  180. # define Islower(c) (((c) & QUOTE) ? 0 : iswlower(c))
  181. # define Isupper(c) (((c) & QUOTE) ? 0 : iswupper(c))
  182. # define Tolower(c) (((c) & QUOTE) ? 0 : (wchar_t)towlower(c))
  183. # define Toupper(c) (((c) & QUOTE) ? 0 : (wchar_t)towupper(c))
  184. # define Isxdigit(c) (((c) & QUOTE) ? 0 : iswxdigit(c))
  185. # define Isalnum(c) (((c) & QUOTE) ? 0 : iswalnum(c))
  186. # define Iscntrl(c) (((c) & QUOTE) ? 0 : iswcntrl(c))
  187. # define Isprint(c) (((c) & QUOTE) ? 0 : iswprint(c))
  188. # define Ispunct(c) (((c) & QUOTE) ? 0 : iswpunct(c))
  189. # elif defined (NeXT)
  190. # define Isspace(c) (((Char)(c) & QUOTE) ? 0 : NXIsSpace((unsigned) (c)))
  191. # define Isdigit(c) (((Char)(c) & QUOTE) ? 0 : NXIsDigit((unsigned) (c)))
  192. # define Isalpha(c) (((Char)(c) & QUOTE) ? 0 : NXIsAlpha((unsigned) (c)))
  193. # define Islower(c) (((Char)(c) & QUOTE) ? 0 : NXIsLower((unsigned) (c)))
  194. # define Isupper(c) (((Char)(c) & QUOTE) ? 0 : NXIsUpper((unsigned) (c)))
  195. # define Tolower(c) (((Char)(c) & QUOTE) ? 0 : NXToLower((unsigned) (c)))
  196. # define Toupper(c) (((Char)(c) & QUOTE) ? 0 : NXToUpper((unsigned) (c)))
  197. # define Isxdigit(c) (((Char)(c) & QUOTE) ? 0 : NXIsXDigit((unsigned) (c)))
  198. #if defined(DSPMBYTE)
  199. # define IscntrlM(c) (((Char)(c) & QUOTE) ? 0 : NXIsCntrl((unsigned) (c)))
  200. # define Iscntrl(c) ( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
  201. # define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : NXIsPrint((unsigned) (c)))
  202. # define Isprint(c) ( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
  203. #else
  204. # define Isalnum(c) (((Char)(c) & QUOTE) ? 0 : NXIsAlNum((unsigned) (c)))
  205. # define Iscntrl(c) (((Char)(c) & QUOTE) ? 0 : NXIsCntrl((unsigned) (c)))
  206. # define Isprint(c) (((Char)(c) & QUOTE) ? 0 : NXIsPrint((unsigned) (c)))
  207. #endif /* !defined(DSPMBYTE) */
  208. # define Ispunct(c) (((Char)(c) & QUOTE) ? 0 : NXIsPunct((unsigned) (c)))
  209. # else /* !NeXT */
  210. # ifndef WINNT_NATIVE
  211. # define Isspace(c) (((Char)(c) & QUOTE) ? 0 : isspace((tcshuc) (c)))
  212. # define Isdigit(c) (((Char)(c) & QUOTE) ? 0 : isdigit((tcshuc) (c)))
  213. # define Isalpha(c) (((Char)(c) & QUOTE) ? 0 : isalpha((tcshuc) (c)))
  214. # define Islower(c) (((Char)(c) & QUOTE) ? 0 : islower((tcshuc) (c)))
  215. # define Isupper(c) (((Char)(c) & QUOTE) ? 0 : isupper((tcshuc) (c)))
  216. # define Tolower(c) (((Char)(c) & QUOTE) ? 0 : tolower((tcshuc) (c)))
  217. # define Toupper(c) (((Char)(c) & QUOTE) ? 0 : toupper((tcshuc) (c)))
  218. # define Isxdigit(c) (((Char)(c) & QUOTE) ? 0 : isxdigit((tcshuc) (c)))
  219. # define Isalnum(c) (((Char)(c) & QUOTE) ? 0 : isalnum((tcshuc) (c)))
  220. #if defined(DSPMBYTE)
  221. # define IscntrlM(c) (((Char)(c) & QUOTE) ? 0 : iscntrl((tcshuc) (c)))
  222. # define Iscntrl(c) ( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
  223. #else
  224. # define Iscntrl(c) (((Char)(c) & QUOTE) ? 0 : iscntrl((tcshuc) (c)))
  225. #endif /* !defined(DSPMBYTE) */
  226. # if SOLARIS2 == 24
  227. /*
  228. * From <casper@fwi.uva.nl> Casper Dik:
  229. * In Solaris 2.4, isprint('\t') returns true after setlocal(LC_ALL,"").
  230. * This breaks commandline editing when you include tabs.
  231. * (This is in the en_US locale).
  232. */
  233. #if defined(DSPMBYTE)
  234. # define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : \
  235. (isprint((tcshuc) (c)) && (c) != '\t'))
  236. #else
  237. # define Isprint(c) (((Char)(c) & QUOTE) ? 0 : \
  238. (isprint((tcshuc) (c)) && (c) != '\t'))
  239. #endif /* !defined(DSPMBYTE) */
  240. # else
  241. #if defined(DSPMBYTE)
  242. # define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : isprint((tcshuc) (c)))
  243. #else
  244. # define Isprint(c) (((Char)(c) & QUOTE) ? 0 : isprint((tcshuc) (c)))
  245. #endif /* !defined(DSPMBYTE) */
  246. # endif /* SOLARIS2 == 24 */
  247. #if defined(DSPMBYTE)
  248. # define Isprint(c) ( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
  249. #endif /* !defined(DSPMBYTE) */
  250. # define Ispunct(c) (((Char)(c) & QUOTE) ? 0 : ispunct((tcshuc) (c)))
  251. # else /* WINNT_NATIVE */
  252. # define Isspace(c) (((Char)(c) & QUOTE) ? 0 : isspace( oem_it((tcshuc)(c))))
  253. # define Isdigit(c) (((Char)(c) & QUOTE) ? 0 : isdigit( oem_it((tcshuc)(c))))
  254. # define Isalpha(c) (((Char)(c) & QUOTE) ? 0 : isalpha( oem_it((tcshuc)(c))))
  255. # define Islower(c) (((Char)(c) & QUOTE) ? 0 : islower( oem_it((tcshuc)(c))))
  256. # define Isupper(c) (((Char)(c) & QUOTE) ? 0 : isupper( oem_it((tcshuc)(c))))
  257. # define Tolower(c) (((Char)(c) & QUOTE) ? 0 : tolower( oem_it((tcshuc)(c))))
  258. # define Toupper(c) (((Char)(c) & QUOTE) ? 0 : toupper( oem_it((tcshuc)(c))))
  259. # define Isxdigit(c)(((Char)(c) & QUOTE) ? 0 : isxdigit(oem_it((tcshuc)(c))))
  260. # define Isalnum(c) (((Char)(c) & QUOTE) ? 0 : isalnum( oem_it((tcshuc)(c))))
  261. # define Ispunct(c) (((Char)(c) & QUOTE) ? 0 : ispunct( oem_it((tcshuc)(c))))
  262. #if defined(DSPMBYTE)
  263. # define IscntrlM(c) (((Char)(c) & QUOTE) ? 0 : iscntrl( oem_it((tcshuc)(c))))
  264. # define Iscntrl(c) ( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
  265. # define IsprintM(c) (((Char)(c) & QUOTE) ? 0 : isprint( oem_it((tcshuc)(c))))
  266. # define Isprint(c) ( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
  267. #else
  268. # define Iscntrl(c) (((Char)(c) & QUOTE) ? 0 : iscntrl( oem_it((tcshuc)(c))))
  269. # define Isprint(c) (((Char)(c) & QUOTE) ? 0 : isprint( oem_it((tcshuc)(c))))
  270. #endif /* !defined(DSPMBYTE) */
  271. # endif /* WINNT_NATIVE */
  272. # endif /* !NeXT */
  273. #else /* !NLS */
  274. # define Isspace(c) cmap((c), _SP|_NL)
  275. # define Isdigit(c) cmap((c), _DIG)
  276. # define Isalpha(c) (cmap((c),_LET) && !(((c) & META) && AsciiOnly))
  277. # define Islower(c) (cmap((c),_DOW) && !(((c) & META) && AsciiOnly))
  278. # define Isupper(c) (cmap((c), _UP) && !(((c) & META) && AsciiOnly))
  279. # define Tolower(c) (_cmap_lower[ASC(c)])
  280. # define Toupper(c) (_cmap_upper[ASC(c)])
  281. # define Isxdigit(c) cmap((c), _XD)
  282. # define Isalnum(c) (cmap((c), _DIG|_LET) && !(((Char)(c) & META) && AsciiOnly))
  283. #if defined(DSPMBYTE)
  284. # define IscntrlM(c) (cmap((c),_CTR) && !(((c) & META) && AsciiOnly))
  285. # define Iscntrl(c) ( (IscntrlM(c)) && !(_enable_mbdisp&&(IsmbyteU((c)))) )
  286. # define IsprintM(c) (!cmap((c),_CTR) && !(((c) & META) && AsciiOnly))
  287. # define Isprint(c) ( (IsprintM(c)) || (_enable_mbdisp&&(IsmbyteU((c)))) )
  288. #else
  289. # define Iscntrl(c) (cmap((c),_CTR) && !(((c) & META) && AsciiOnly))
  290. # define Isprint(c) (!cmap((c),_CTR) && !(((c) & META) && AsciiOnly))
  291. #endif /* !defined(DSPMBYTE) */
  292. # define Ispunct(c) (cmap((c),_PUN) && !(((c) & META) && AsciiOnly))
  293. #endif /* !NLS */
  294. #if defined(DSPMBYTE)
  295. # define Ismbyte1(c) ((_mbmap[(c) & 0377] & _MB1) ? 1 : 0)
  296. # define Ismbyte2(c) ((_mbmap[(c) & 0377] & _MB2) ? 1 : 0)
  297. #endif
  298. #endif /* _h_sh_char */