1 /* @(#)wchar.h	1.26 19/10/28 Copyright 2007-2019 J. Schilling */
2 /*
3  *	Abstraction from wchar.h
4  *
5  *	Copyright (c) 2007-2019 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #ifndef _SCHILY_WCHAR_H
22 #define	_SCHILY_WCHAR_H
23 
24 #ifndef	_SCHILY_MCONFIG_H
25 #include <schily/mconfig.h>
26 #endif
27 
28 #ifndef	_SCHILY_STDLIB_H
29 #include <schily/stdlib.h>	/* for MB_CUR_MAX, mbtowc()/wctomb() */
30 #endif
31 #ifndef	_SCHILY_TYPES_H
32 #include <schily/types.h>
33 #endif
34 #ifdef	HAVE_STDDEF_H
35 #ifndef	_INCL_STDDEF_H
36 #include <stddef.h>		/* Needed for e.g. size_t (POSIX)  */
37 #define	_INCL_STDDEF_H
38 #endif
39 #endif
40 #ifndef _SCHILY_STDIO_H
41 #include <schily/stdio.h>	/* Needed for e.g. FILE (POSIX)	   */
42 #endif
43 #ifndef	_SCHILY_VARARGS_H
44 #include <schily/varargs.h>	/* Needed for e.g. va_list (POSIX) */
45 #endif
46 
47 
48 #if	!defined(HAVE_MBTOWC) || !defined(HAVE_WCTOMB)
49 #if	defined(HAVE_MBRTOWC) && defined(HAVE_WCRTOMB)
50 #	define	mbtowc(wp, cp, len)	mbrtowc(wp, cp, len, (mbstate_t *)0)
51 #	define	wctomb(cp, wc)		wcrtomb(cp, wc, (mbstate_t *)0)
52 #else
53 #	define	NO_WCHAR
54 #endif
55 #endif
56 
57 #ifndef	HAVE_MBSINIT
58 #define	mbsinit(sp)			((int)((sp) == 0))
59 #endif
60 #ifndef	HAVE_MBRTOWC
61 #define	mbrtowc(wp, cp, len, sp)	mbtowc(wp, cp, len)
62 #endif
63 #ifndef	HAVE_WCRTOMB
64 #define	wcrtomb(cp, wc, sp)		wctomb(cp, wc)
65 #endif
66 
67 #ifdef	HAVE_WCHAR_H
68 #ifndef	_INCL_WCHAR_H
69 #include <wchar.h>
70 #define	_INCL_WCHAR_H
71 #endif
72 #endif	/* !HAVE_WCHAR_H */
73 
74 #ifndef	WEOF
75 #define	WEOF	((wint_t)(-1))
76 #endif	/* WEOF */
77 
78 #ifndef	USE_WCHAR
79 #define	USE_WCHAR
80 #endif
81 
82 /*
83  * HP-UX-10.20 and Ultrix do not have wchar.h but they have wchar_t in stdlib.h
84  * and the compiler understands L'a'. So disable USE_WCHAR / USE_WCTYPE only in
85  * case that wchar_t or L'a' support is missing.
86  */
87 #if	!defined(HAVE_WCTYPE_H) && (SIZEOF_WCHAR == 0 || SIZEOF_WCHAR_T == 0)
88 #undef	USE_WCHAR
89 #undef	USE_WCTYPE
90 #endif
91 
92 #ifdef	NO_WCTYPE
93 #undef	USE_WCHAR
94 #undef	USE_WCTYPE
95 #endif
96 
97 #ifdef	NO_WCHAR
98 #undef	USE_WCHAR
99 #undef	USE_WCTYPE
100 #endif
101 
102 #ifndef	USE_WCHAR
103 
104 /*
105  * We either don't have wide chars or we don't use them...
106  */
107 #undef	wchar_t
108 #define	wchar_t	char
109 #undef	wint_t
110 #define	wint_t	int
111 /*
112  * We cannot define wctype_t here because of a bug in Linux (missing xctype_t
113  * definition in wchar.h
114  */
115 #ifdef	__never__
116 #undef	wctype_t
117 #define	wctype_t	int
118 #endif
119 
120 #undef	WEOF
121 #define	WEOF	((wint_t)-1)
122 
123 #ifndef	_SCHILY_UTYPES_H
124 #include <schily/utypes.h>	/* For TYPE_MAXVAL() */
125 #endif
126 
127 #undef	WCHAR_MAX
128 #define	WCHAR_MAX	TYPE_MAXVAL(wchar_t)
129 #undef	WCHAR_MIN
130 #define	WCHAR_MIN	TYPE_MINVAL(wchar_t)
131 
132 #undef	WINT_MAX
133 #define	WINT_MAX	TYPE_MAXVAL(wint_t)
134 #undef	WINT_MIN
135 #define	WINT_MIN	TYPE_MINVAL(wint_t)
136 
137 #undef	WCTYPE_MAX
138 #define	WCTYPE_MAX	TYPE_MAXVAL(wctype_t)
139 #undef	WCTYPE_MIN
140 #define	WCTYPE_MIN	TYPE_MINVAL(wctype_t)
141 
142 #undef	SIZEOF_WCHAR_T
143 #define	SIZEOF_WCHAR_T	SIZEOF_CHAR
144 
145 #undef	MB_CUR_MAX
146 #define	MB_CUR_MAX	1
147 #undef	MB_LEN_MAX
148 #define	MB_LEN_MAX	1
149 
150 /*
151  * The mbtowc() for the non-multibyte case could be as simple as
152  *
153  * #define	mbtowc(wp, cp, len)	(*(wp) = *(cp), 1)
154  *
155  * but Mac OS X forces us to do many mbtowc(NULL, NULL, 0) calls in order
156  * to reset the internal state. On other platforms that do not support
157  * wide chars, NULL may be defined as (void *)0, so we need to check
158  * for "wp" != NULL and to cast "wp" and "cp" to their expected types.
159  */
160 #undef	mbtowc
161 #define	mbtowc(wp, cp, len)	((void)((wp) ? \
162 				*(wchar_t *)(wp) = *(char *)(cp) : 1), 1)
163 #undef	mbrtowc
164 #define	mbrtowc(wp, cp, len, sp) ((void)((wp) ? \
165 				*(wchar_t *)(wp) = *(char *)(cp) : 1), 1)
166 #undef	wctomb
167 #define	wctomb(cp, wc)		(*(cp) = wc, 1)
168 #undef	wcrtomb
169 #define	wcrtomb(cp, wc, sp)	(*(cp) = wc, 1)
170 
171 #undef	mbsinit
172 #define	mbsinit(sp)		((int)((sp) == 0))
173 
174 #undef	wcwidth
175 #define	wcwidth(wc)		(1)
176 
177 #else	/* USE_WCHAR */
178 
179 #ifndef	HAVE_WCWIDTH
180 #undef	wcwidth
181 #define	wcwidth(wc)		(1)
182 #endif
183 
184 #endif	/* USE_WCHAR */
185 
186 #endif	/* _SCHILY_WCHAR_H */
187