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