1 /* shmbutil.h -- utility functions for multibyte characters. */
3 /* Copyright (C) 2002-2004 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 #if !defined (_SH_MBUTIL_H_)
26 /* Include config.h for HANDLE_MULTIBYTE */
29 #if defined (HANDLE_MULTIBYTE)
31 extern size_t xmbsrtowcs __P((wchar_t *, const char **, size_t, mbstate_t *));
32 extern size_t xdupmbstowcs __P((wchar_t **, char ***, const char *));
34 extern size_t mbstrlen __P((const char *));
36 extern char *xstrchr __P((const char *, int));
39 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
40 #define MB_NULLWCH(x) ((x) == 0)
43 #define MBSLEN(s) (((s) && (s)[0]) ? ((s)[1] ? mbstrlen (s) : 1) : 0)
44 #define MB_STRLEN(s) ((MB_CUR_MAX > 1) ? MBSLEN (s) : STRLEN (s))
46 #define MBLEN(s, n) ((MB_CUR_MAX > 1) ? mblen ((s), (n)) : 1)
47 #define MBRLEN(s, n, p) ((MB_CUR_MAX > 1) ? mbrlen ((s), (n), (p)) : 1)
49 #else /* !HANDLE_MULTIBYTE */
58 #define xstrchr(s, c) strchr(s, c)
61 #define MB_INVALIDCH(x) (0)
62 #define MB_NULLWCH(x) (0)
65 #define MB_STRLEN(s) (STRLEN(s))
68 #define MBRLEN(s, n, p) 1
74 #endif /* !HANDLE_MULTIBYTE */
76 /* Declare and initialize a multibyte state. Call must be terminated
78 #if defined (HANDLE_MULTIBYTE)
79 # define DECLARE_MBSTATE \
81 memset (&state, '\0', sizeof (mbstate_t))
83 # define DECLARE_MBSTATE
84 #endif /* !HANDLE_MULTIBYTE */
86 /* Initialize or reinitialize a multibyte state named `state'. Call must be
87 terminated with `;'. */
88 #if defined (HANDLE_MULTIBYTE)
89 # define INITIALIZE_MBSTATE memset (&state, '\0', sizeof (mbstate_t))
91 # define INITIALIZE_MBSTATE
92 #endif /* !HANDLE_MULTIBYTE */
94 /* Advance one (possibly multi-byte) character in string _STR of length
95 _STRSIZE, starting at index _I. STATE must have already been declared. */
96 #if defined (HANDLE_MULTIBYTE)
97 # define ADVANCE_CHAR(_str, _strsize, _i) \
100 if (MB_CUR_MAX > 1) \
102 mbstate_t state_bak; \
106 mblength = mbrlen ((_str) + (_i), (_strsize) - (_i), &state); \
108 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
113 else if (mblength == 0) \
123 # define ADVANCE_CHAR(_str, _strsize, _i) (_i)++
124 #endif /* !HANDLE_MULTIBYTE */
126 /* Advance one (possibly multibyte) character in the string _STR of length
128 SPECIAL: assume that _STR will be incremented by 1 after this call. */
129 #if defined (HANDLE_MULTIBYTE)
130 # define ADVANCE_CHAR_P(_str, _strsize) \
133 if (MB_CUR_MAX > 1) \
135 mbstate_t state_bak; \
139 mblength = mbrlen ((_str), (_strsize), &state); \
141 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
147 (_str) += (mblength < 1) ? 0 : (mblength - 1); \
152 # define ADVANCE_CHAR_P(_str, _strsize)
153 #endif /* !HANDLE_MULTIBYTE */
155 /* Back up one (possibly multi-byte) character in string _STR of length
156 _STRSIZE, starting at index _I. STATE must have already been declared. */
157 #if defined (HANDLE_MULTIBYTE)
158 # define BACKUP_CHAR(_str, _strsize, _i) \
161 if (MB_CUR_MAX > 1) \
163 mbstate_t state_bak; \
165 int _x, _p; /* _x == temp index into string, _p == prev index */ \
171 mblength = mbrlen ((_str) + (_x), (_strsize) - (_x), &state); \
173 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
178 else if (mblength == 0) \
182 _p = _x; /* _p == start of prev mbchar */ \
193 # define BACKUP_CHAR(_str, _strsize, _i) (_i)--
194 #endif /* !HANDLE_MULTIBYTE */
196 /* Back up one (possibly multibyte) character in the string _BASE of length
197 _STRSIZE starting at _STR (_BASE <= _STR <= (_BASE + _STRSIZE) ).
198 SPECIAL: DO NOT assume that _STR will be decremented by 1 after this call. */
199 #if defined (HANDLE_MULTIBYTE)
200 # define BACKUP_CHAR_P(_base, _strsize, _str) \
203 if (MB_CUR_MAX > 1) \
205 mbstate_t state_bak; \
207 char *_x, _p; /* _x == temp pointer into string, _p == prev pointer */ \
210 while (_x < (_str)) \
213 mblength = mbrlen (_x, (_strsize) - _x, &state); \
215 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
220 else if (mblength == 0) \
224 _p = _x; /* _p == start of prev mbchar */ \
235 # define BACKUP_CHAR_P(_base, _strsize, _str) (_str)--
236 #endif /* !HANDLE_MULTIBYTE */
238 /* Copy a single character from the string _SRC to the string _DST.
239 _SRCEND is a pointer to the end of _SRC. */
240 #if defined (HANDLE_MULTIBYTE)
241 # define COPY_CHAR_P(_dst, _src, _srcend) \
244 if (MB_CUR_MAX > 1) \
246 mbstate_t state_bak; \
251 mblength = mbrlen ((_src), (_srcend) - (_src), &state); \
252 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
258 mblength = (mblength < 1) ? 1 : mblength; \
260 for (_k = 0; _k < mblength; _k++) \
261 *(_dst)++ = *(_src)++; \
264 *(_dst)++ = *(_src)++; \
268 # define COPY_CHAR_P(_dst, _src, _srcend) *(_dst)++ = *(_src)++
269 #endif /* !HANDLE_MULTIBYTE */
271 /* Copy a single character from the string _SRC at index _SI to the string
272 _DST at index _DI. _SRCEND is a pointer to the end of _SRC. */
273 #if defined (HANDLE_MULTIBYTE)
274 # define COPY_CHAR_I(_dst, _di, _src, _srcend, _si) \
277 if (MB_CUR_MAX > 1) \
279 mbstate_t state_bak; \
284 mblength = mbrlen ((_src) + (_si), (_srcend) - ((_src)+(_si)), &state); \
285 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
291 mblength = (mblength < 1) ? 1 : mblength; \
293 for (_k = 0; _k < mblength; _k++) \
294 _dst[_di++] = _src[_si++]; \
297 _dst[_di++] = _src[_si++]; \
301 # define COPY_CHAR_I(_dst, _di, _src, _srcend, _si) _dst[_di++] = _src[_si++]
302 #endif /* !HANDLE_MULTIBYTE */
304 /****************************************************************
306 * The following are only guaranteed to work in subst.c *
308 ****************************************************************/
310 #if defined (HANDLE_MULTIBYTE)
311 # define SCOPY_CHAR_I(_dst, _escchar, _sc, _src, _si, _slen) \
314 if (MB_CUR_MAX > 1) \
316 mbstate_t state_bak; \
321 mblength = mbrlen ((_src) + (_si), (_slen) - (_si), &state); \
322 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
328 mblength = (mblength < 1) ? 1 : mblength; \
330 temp = xmalloc (mblength + 2); \
331 temp[0] = _escchar; \
332 for (_i = 0; _i < mblength; _i++) \
333 temp[_i + 1] = _src[_si++]; \
334 temp[mblength + 1] = '\0'; \
340 _dst[0] = _escchar; \
346 # define SCOPY_CHAR_I(_dst, _escchar, _sc, _src, _si, _slen) \
347 _dst[0] = _escchar; \
349 #endif /* !HANDLE_MULTIBYTE */
351 #if defined (HANDLE_MULTIBYTE)
352 # define SCOPY_CHAR_M(_dst, _src, _srcend, _si) \
355 if (MB_CUR_MAX > 1) \
357 mbstate_t state_bak; \
361 mblength = mbrlen ((_src) + (_si), (_srcend) - ((_src) + (_si)), &state); \
362 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
368 mblength = (mblength < 1) ? 1 : mblength; \
370 FASTCOPY(((_src) + (_si)), (_dst), mblength); \
372 (_dst) += mblength; \
377 *(_dst)++ = _src[(_si)]; \
383 # define SCOPY_CHAR_M(_dst, _src, _srcend, _si) \
384 *(_dst)++ = _src[(_si)]; \
386 #endif /* !HANDLE_MULTIBYTE */
389 # define SADD_MBCHAR(_dst, _src, _si, _srcsize) \
392 if (MB_CUR_MAX > 1) \
395 mbstate_t state_bak; \
399 mblength = mbrlen ((_src) + (_si), (_srcsize) - (_si), &state); \
400 if (mblength == (size_t)-1 || mblength == (size_t)-2) \
408 _dst = (char *)xmalloc (mblength + 1); \
409 for (i = 0; i < mblength; i++) \
410 (_dst)[i] = (_src)[(_si)++]; \
411 (_dst)[mblength] = '\0'; \
419 # define SADD_MBCHAR(_dst, _src, _si, _srcsize)
422 /* Watch out when using this -- it's just straight textual subsitution */
423 #if defined (HANDLE_MULTIBYTE)
424 # define SADD_MBQCHAR_BODY(_dst, _src, _si, _srcsize) \
427 mbstate_t state_bak; \
431 mblength = mbrlen ((_src) + (_si), (_srcsize) - (_si), &state); \
432 if (mblength == (size_t)-1 || mblength == (size_t)-2) \
440 (_dst) = (char *)xmalloc (mblength + 2); \
441 (_dst)[0] = CTLESC; \
442 for (i = 0; i < mblength; i++) \
443 (_dst)[i+1] = (_src)[(_si)++]; \
444 (_dst)[mblength+1] = '\0'; \
448 #endif /* HANDLE_MULTIBYTE */
449 #endif /* _SH_MBUTIL_H_ */