12d08521Garrett D'Amore/*
22d08521Garrett D'Amore * This file and its contents are supplied under the terms of the
32d08521Garrett D'Amore * Common Development and Distribution License ("CDDL"), version 1.0.
42d08521Garrett D'Amore * You may only use this file in accordance with the terms of version
52d08521Garrett D'Amore * 1.0 of the CDDL.
62d08521Garrett D'Amore *
72d08521Garrett D'Amore * A full copy of the text of the CDDL should have accompanied this
82d08521Garrett D'Amore * source.  A copy of the CDDL is also available via the Internet at
92d08521Garrett D'Amore * http://www.illumos.org/license/CDDL.
102d08521Garrett D'Amore */
112d08521Garrett D'Amore
122d08521Garrett D'Amore/*
132d08521Garrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
144f9d8ecYuri Pankov * Copyright 2017 Nexenta Systems, Inc.
152d08521Garrett D'Amore */
162d08521Garrett D'Amore
172d08521Garrett D'Amore/*
182d08521Garrett D'Amore * This file contains the implementation of various functional forms
192d08521Garrett D'Amore * of the ctype tests, specifically the required by ISO C.  These are defined
202d08521Garrett D'Amore * in the "C" (POSIX) locale.
212d08521Garrett D'Amore */
222d08521Garrett D'Amore
232d08521Garrett D'Amore#include "lint.h"
242d08521Garrett D'Amore#include <ctype.h>
252d08521Garrett D'Amore#include <locale.h>
262d08521Garrett D'Amore#include "localeimpl.h"
272d08521Garrett D'Amore#include "_ctype.h"
282d08521Garrett D'Amore#include "lctype.h"
292d08521Garrett D'Amore
302d08521Garrett D'Amore/*
314f9d8ecYuri Pankov * As far as we know, *every* encoding we support is a strict superset of ASCII,
324f9d8ecYuri Pankov * so we can make things faster by trying ASCII first.  Next check if argument
334f9d8ecYuri Pankov * can be represented as unsigned char, and that locale is not multibyte - every
344f9d8ecYuri Pankov * multibyte encoding we support has non-ASCII code points undefined.  Finally,
354f9d8ecYuri Pankov * lookup the result in locale specific table.
362d08521Garrett D'Amore */
372d08521Garrett D'Amore#define	ISTYPE_L(c, mask, loc)	\
384f9d8ecYuri Pankov	(isascii(c) ? (__ctype_mask[c] & (mask)) : \
394f9d8ecYuri Pankov	((unsigned)c > 255 || loc->ctype->lc_max_mblen > 1) ? 0 : \
404f9d8ecYuri Pankov	(loc->ctype->lc_ctype_mask[c] & mask))
412d08521Garrett D'Amore
422d08521Garrett D'Amore#define	ISTYPE(c, mask)	ISTYPE_L(c, mask, uselocale(NULL))
432d08521Garrett D'Amore
442d08521Garrett D'Amore#define	DEFN_ISTYPE(type, mask)		\
452d08521Garrett D'Amoreint					\
462d08521Garrett D'Amoreis##type##_l(int c, locale_t l)	\
472d08521Garrett D'Amore{					\
482d08521Garrett D'Amore	return (ISTYPE_L(c, mask, l));	\
492d08521Garrett D'Amore}					\
502d08521Garrett D'Amore					\
512d08521Garrett D'Amoreint					\
522d08521Garrett D'Amoreis##type(int c)				\
532d08521Garrett D'Amore{					\
542d08521Garrett D'Amore	return (ISTYPE(c, mask));	\
552d08521Garrett D'Amore}
562d08521Garrett D'Amore
574f9d8ecYuri Pankov/*
584f9d8ecYuri Pankov * We are supplying functional forms, so make sure to suppress any macros
594f9d8ecYuri Pankov * we might have imported.
604f9d8ecYuri Pankov */
612d08521Garrett D'Amore#undef	isblank
622d08521Garrett D'Amore#undef	isupper
632d08521Garrett D'Amore#undef	islower
642d08521Garrett D'Amore#undef	isdigit
652d08521Garrett D'Amore#undef	isxdigit
662d08521Garrett D'Amore#undef	isalpha
672d08521Garrett D'Amore#undef	isalnum
682d08521Garrett D'Amore#undef	isspace
692d08521Garrett D'Amore#undef	iscntrl
702d08521Garrett D'Amore#undef	isgraph
712d08521Garrett D'Amore#undef	ispunct
722d08521Garrett D'Amore#undef	isprint
732d08521Garrett D'Amore
742d08521Garrett D'AmoreDEFN_ISTYPE(blank, _ISBLANK)
752d08521Garrett D'AmoreDEFN_ISTYPE(upper, _ISUPPER)
762d08521Garrett D'AmoreDEFN_ISTYPE(lower, _ISLOWER)
772d08521Garrett D'AmoreDEFN_ISTYPE(digit, _ISDIGIT)
782d08521Garrett D'AmoreDEFN_ISTYPE(xdigit, _ISXDIGIT)
792d08521Garrett D'AmoreDEFN_ISTYPE(alpha, _ISALPHA)
802d08521Garrett D'AmoreDEFN_ISTYPE(alnum, _ISALNUM)
812d08521Garrett D'AmoreDEFN_ISTYPE(space, _ISSPACE)
822d08521Garrett D'AmoreDEFN_ISTYPE(cntrl, _ISCNTRL)
832d08521Garrett D'AmoreDEFN_ISTYPE(graph, _ISGRAPH)
842d08521Garrett D'AmoreDEFN_ISTYPE(punct, _ISPUNCT)
852d08521Garrett D'AmoreDEFN_ISTYPE(print, _ISPRINT)
86