xref: /minix3/external/bsd/nvi/dist/common/multibyte.h (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: multibyte.h,v 1.2 2013/11/22 15:52:05 christos Exp $	*/
2*84d9c625SLionel Sambuc #ifndef MULTIBYTE_H
3*84d9c625SLionel Sambuc #define MULTIBYTE_H
4*84d9c625SLionel Sambuc 
5*84d9c625SLionel Sambuc /*
6*84d9c625SLionel Sambuc  * Ex/vi commands are generally separated by whitespace characters.  We
7*84d9c625SLionel Sambuc  * can't use the standard isspace(3) macro because it returns true for
8*84d9c625SLionel Sambuc  * characters like ^K in the ASCII character set.  The 4.4BSD isblank(3)
9*84d9c625SLionel Sambuc  * macro does exactly what we want, but it's not portable yet.
10*84d9c625SLionel Sambuc  *
11*84d9c625SLionel Sambuc  * XXX
12*84d9c625SLionel Sambuc  * Note side effect, ch is evaluated multiple times.
13*84d9c625SLionel Sambuc  */
14*84d9c625SLionel Sambuc #define ISBLANK(c)	((c) == ' ' || (c) == '\t')
15*84d9c625SLionel Sambuc 
16*84d9c625SLionel Sambuc #define ISDIGIT(c)	((c) >= '0' && (c) <= '9')
17*84d9c625SLionel Sambuc #define ISXDIGIT(c)	(ISDIGIT(c) || \
18*84d9c625SLionel Sambuc 			 ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
19*84d9c625SLionel Sambuc #define ISALPHA(c)	(((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
20*84d9c625SLionel Sambuc #define ISALNUM(c)	(ISALPHA(c) || ISDIGIT(c))
21*84d9c625SLionel Sambuc 
22*84d9c625SLionel Sambuc /*
23*84d9c625SLionel Sambuc  * Fundamental character types.
24*84d9c625SLionel Sambuc  *
25*84d9c625SLionel Sambuc  * CHAR_T       An integral type that can hold any character.
26*84d9c625SLionel Sambuc  * ARG_CHAR_T   The type of a CHAR_T when passed as an argument using
27*84d9c625SLionel Sambuc  *              traditional promotion rules.  It should also be able
28*84d9c625SLionel Sambuc  *              to be compared against any CHAR_T for equality without
29*84d9c625SLionel Sambuc  *              problems.
30*84d9c625SLionel Sambuc  *
31*84d9c625SLionel Sambuc  * If no integral type can hold a character, don't even try the port.
32*84d9c625SLionel Sambuc  */
33*84d9c625SLionel Sambuc 
34*84d9c625SLionel Sambuc #ifdef USE_WIDECHAR
35*84d9c625SLionel Sambuc #include <wchar.h>
36*84d9c625SLionel Sambuc #include <wctype.h>
37*84d9c625SLionel Sambuc 
38*84d9c625SLionel Sambuc typedef wchar_t		RCHAR_T;
39*84d9c625SLionel Sambuc #define REOF		WEOF
40*84d9c625SLionel Sambuc typedef wchar_t		CHAR_T;
41*84d9c625SLionel Sambuc typedef	wint_t		ARG_CHAR_T;
42*84d9c625SLionel Sambuc typedef wint_t		UCHAR_T;
43*84d9c625SLionel Sambuc 
44*84d9c625SLionel Sambuc #define ISUPPER		iswupper
45*84d9c625SLionel Sambuc #define STRLEN		wcslen
46*84d9c625SLionel Sambuc #define STRTOL		wcstol
47*84d9c625SLionel Sambuc #define STRTOUL		wcstoul
48*84d9c625SLionel Sambuc #define SPRINTF		swprintf
49*84d9c625SLionel Sambuc #define STRCHR		wcschr
50*84d9c625SLionel Sambuc #define STRCMP		wcscmp
51*84d9c625SLionel Sambuc #define STRPBRK		wcspbrk
52*84d9c625SLionel Sambuc #define ISBLANK2	iswblank
53*84d9c625SLionel Sambuc #define ISCNTRL		iswcntrl
54*84d9c625SLionel Sambuc #define ISGRAPH		iswgraph
55*84d9c625SLionel Sambuc #define ISLOWER		iswlower
56*84d9c625SLionel Sambuc #define ISPUNCT		iswpunct
57*84d9c625SLionel Sambuc #define ISSPACE		iswspace
58*84d9c625SLionel Sambuc #define ISUPPER		iswupper
59*84d9c625SLionel Sambuc #define TOLOWER		towlower
60*84d9c625SLionel Sambuc #define TOUPPER		towupper
61*84d9c625SLionel Sambuc #define STRSET		wmemset
62*84d9c625SLionel Sambuc #define STRCHR		wcschr
63*84d9c625SLionel Sambuc 
64*84d9c625SLionel Sambuc #define L(ch)		L ## ch
65*84d9c625SLionel Sambuc #define WS		"%ls"
66*84d9c625SLionel Sambuc #define WVS		"%*ls"
67*84d9c625SLionel Sambuc #define WC		"%lc"
68*84d9c625SLionel Sambuc 
69*84d9c625SLionel Sambuc #else
70*84d9c625SLionel Sambuc #include <stdio.h>
71*84d9c625SLionel Sambuc 
72*84d9c625SLionel Sambuc #define REOF		EOF
73*84d9c625SLionel Sambuc typedef	char		CHAR_T;
74*84d9c625SLionel Sambuc typedef	int		ARG_CHAR_T;
75*84d9c625SLionel Sambuc typedef	unsigned char	UCHAR_T;
76*84d9c625SLionel Sambuc 
77*84d9c625SLionel Sambuc #define ISUPPER		isupper
78*84d9c625SLionel Sambuc #define STRLEN		strlen
79*84d9c625SLionel Sambuc #define STRTOL		strtol
80*84d9c625SLionel Sambuc #define STRTOUL		strtoul
81*84d9c625SLionel Sambuc #define SPRINTF		snprintf
82*84d9c625SLionel Sambuc #define STRCHR		strchr
83*84d9c625SLionel Sambuc #define STRCMP		strcmp
84*84d9c625SLionel Sambuc #define STRPBRK		strpbrk
85*84d9c625SLionel Sambuc #define ISBLANK2	isblank
86*84d9c625SLionel Sambuc #define ISCNTRL		iscntrl
87*84d9c625SLionel Sambuc #define ISGRAPH		isgraph
88*84d9c625SLionel Sambuc #define ISLOWER		islower
89*84d9c625SLionel Sambuc #define ISPUNCT		ispunct
90*84d9c625SLionel Sambuc #define ISSPACE		isspace
91*84d9c625SLionel Sambuc #define ISUPPER		isupper
92*84d9c625SLionel Sambuc #define TOLOWER		tolower
93*84d9c625SLionel Sambuc #define TOUPPER		toupper
94*84d9c625SLionel Sambuc #define STRSET		memset
95*84d9c625SLionel Sambuc #define STRCHR		strchr
96*84d9c625SLionel Sambuc 
97*84d9c625SLionel Sambuc #define L(ch)		ch
98*84d9c625SLionel Sambuc #define WS		"%s"
99*84d9c625SLionel Sambuc #define WVS		"%*s"
100*84d9c625SLionel Sambuc #define WC		"%c"
101*84d9c625SLionel Sambuc 
102*84d9c625SLionel Sambuc #endif
103*84d9c625SLionel Sambuc 
104*84d9c625SLionel Sambuc #define MEMCMP(to, from, n) 						    \
105*84d9c625SLionel Sambuc 	memcmp(to, from, (n) * sizeof(*(to)))
106*84d9c625SLionel Sambuc #define	MEMMOVE(p, t, len)	memmove(p, t, (len) * sizeof(*(p)))
107*84d9c625SLionel Sambuc #define	MEMCPY(p, t, len)	memcpy(p, t, (len) * sizeof(*(p)))
108*84d9c625SLionel Sambuc /*
109*84d9c625SLionel Sambuc  * Like MEMCPY but return a pointer to the end of the copied bytes.
110*84d9c625SLionel Sambuc  * Glibc has a function mempcpy with the same purpose.
111*84d9c625SLionel Sambuc  */
112*84d9c625SLionel Sambuc #define MEMPCPY(p, t, len) \
113*84d9c625SLionel Sambuc 	((void *)((char *)MEMCPY(p, t, len) + (len) * sizeof(*(p))))
114*84d9c625SLionel Sambuc #define SIZE(w)		(sizeof(w)/sizeof(*w))
115*84d9c625SLionel Sambuc 
116*84d9c625SLionel Sambuc #endif
117