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)
32 extern size_t xmbsrtowcs __P((wchar_t *, const char **, size_t, mbstate_t *));
33 extern size_t xdupmbstowcs __P((wchar_t **, char ***, const char *));
35 extern size_t mbstrlen __P((const char *));
37 extern char *xstrchr __P((const char *, int));
39 extern int locale_mb_cur_max; /* XXX */
42 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
43 #define MB_NULLWCH(x) ((x) == 0)
46 #define MBSLEN(s) (((s) && (s)[0]) ? ((s)[1] ? mbstrlen (s) : 1) : 0)
47 #define MB_STRLEN(s) ((MB_CUR_MAX > 1) ? MBSLEN (s) : STRLEN (s))
49 #define MBLEN(s, n) ((MB_CUR_MAX > 1) ? mblen ((s), (n)) : 1)
50 #define MBRLEN(s, n, p) ((MB_CUR_MAX > 1) ? mbrlen ((s), (n), (p)) : 1)
52 #else /* !HANDLE_MULTIBYTE */
61 #define xstrchr(s, c) strchr(s, c)
64 #define MB_INVALIDCH(x) (0)
65 #define MB_NULLWCH(x) (0)
68 #define MB_STRLEN(s) (STRLEN(s))
71 #define MBRLEN(s, n, p) 1
77 #endif /* !HANDLE_MULTIBYTE */
79 /* Declare and initialize a multibyte state. Call must be terminated
81 #if defined (HANDLE_MULTIBYTE)
82 # define DECLARE_MBSTATE \
84 memset (&state, '\0', sizeof (mbstate_t))
86 # define DECLARE_MBSTATE
87 #endif /* !HANDLE_MULTIBYTE */
89 /* Initialize or reinitialize a multibyte state named `state'. Call must be
90 terminated with `;'. */
91 #if defined (HANDLE_MULTIBYTE)
92 # define INITIALIZE_MBSTATE memset (&state, '\0', sizeof (mbstate_t))
94 # define INITIALIZE_MBSTATE
95 #endif /* !HANDLE_MULTIBYTE */
97 /* Advance one (possibly multi-byte) character in string _STR of length
98 _STRSIZE, starting at index _I. STATE must have already been declared. */
99 #if defined (HANDLE_MULTIBYTE)
100 # define ADVANCE_CHAR(_str, _strsize, _i) \
103 if (locale_mb_cur_max > 1) \
105 mbstate_t state_bak; \
109 _f = is_basic ((_str)[_i]); \
115 mblength = mbrlen ((_str) + (_i), (_strsize) - (_i), &state); \
118 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
123 else if (mblength == 0) \
133 # define ADVANCE_CHAR(_str, _strsize, _i) (_i)++
134 #endif /* !HANDLE_MULTIBYTE */
136 /* Advance one (possibly multibyte) character in the string _STR of length
138 SPECIAL: assume that _STR will be incremented by 1 after this call. */
139 #if defined (HANDLE_MULTIBYTE)
140 # define ADVANCE_CHAR_P(_str, _strsize) \
143 if (locale_mb_cur_max > 1) \
145 mbstate_t state_bak; \
149 _f = is_basic (*(_str)); \
155 mblength = mbrlen ((_str), (_strsize), &state); \
158 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
164 (_str) += (mblength < 1) ? 0 : (mblength - 1); \
169 # define ADVANCE_CHAR_P(_str, _strsize)
170 #endif /* !HANDLE_MULTIBYTE */
172 /* Back up one (possibly multi-byte) character in string _STR of length
173 _STRSIZE, starting at index _I. STATE must have already been declared. */
174 #if defined (HANDLE_MULTIBYTE)
175 # define BACKUP_CHAR(_str, _strsize, _i) \
178 if (locale_mb_cur_max > 1) \
180 mbstate_t state_bak; \
182 int _x, _p; /* _x == temp index into string, _p == prev index */ \
188 mblength = mbrlen ((_str) + (_x), (_strsize) - (_x), &state); \
190 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
195 else if (mblength == 0) \
199 _p = _x; /* _p == start of prev mbchar */ \
210 # define BACKUP_CHAR(_str, _strsize, _i) (_i)--
211 #endif /* !HANDLE_MULTIBYTE */
213 /* Back up one (possibly multibyte) character in the string _BASE of length
214 _STRSIZE starting at _STR (_BASE <= _STR <= (_BASE + _STRSIZE) ).
215 SPECIAL: DO NOT assume that _STR will be decremented by 1 after this call. */
216 #if defined (HANDLE_MULTIBYTE)
217 # define BACKUP_CHAR_P(_base, _strsize, _str) \
220 if (locale_mb_cur_max > 1) \
222 mbstate_t state_bak; \
224 char *_x, _p; /* _x == temp pointer into string, _p == prev pointer */ \
227 while (_x < (_str)) \
230 mblength = mbrlen (_x, (_strsize) - _x, &state); \
232 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
237 else if (mblength == 0) \
241 _p = _x; /* _p == start of prev mbchar */ \
252 # define BACKUP_CHAR_P(_base, _strsize, _str) (_str)--
253 #endif /* !HANDLE_MULTIBYTE */
255 /* Copy a single character from the string _SRC to the string _DST.
256 _SRCEND is a pointer to the end of _SRC. */
257 #if defined (HANDLE_MULTIBYTE)
258 # define COPY_CHAR_P(_dst, _src, _srcend) \
261 if (locale_mb_cur_max > 1) \
263 mbstate_t state_bak; \
267 _k = is_basic (*(_src)); \
273 mblength = mbrlen ((_src), (_srcend) - (_src), &state); \
275 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
281 mblength = (mblength < 1) ? 1 : mblength; \
283 for (_k = 0; _k < mblength; _k++) \
284 *(_dst)++ = *(_src)++; \
287 *(_dst)++ = *(_src)++; \
291 # define COPY_CHAR_P(_dst, _src, _srcend) *(_dst)++ = *(_src)++
292 #endif /* !HANDLE_MULTIBYTE */
294 /* Copy a single character from the string _SRC at index _SI to the string
295 _DST at index _DI. _SRCEND is a pointer to the end of _SRC. */
296 #if defined (HANDLE_MULTIBYTE)
297 # define COPY_CHAR_I(_dst, _di, _src, _srcend, _si) \
300 if (locale_mb_cur_max > 1) \
302 mbstate_t state_bak; \
306 _k = is_basic (*((_src) + (_si))); \
312 mblength = mbrlen ((_src) + (_si), (_srcend) - ((_src)+(_si)), &state); \
314 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
320 mblength = (mblength < 1) ? 1 : mblength; \
322 for (_k = 0; _k < mblength; _k++) \
323 _dst[_di++] = _src[_si++]; \
326 _dst[_di++] = _src[_si++]; \
330 # define COPY_CHAR_I(_dst, _di, _src, _srcend, _si) _dst[_di++] = _src[_si++]
331 #endif /* !HANDLE_MULTIBYTE */
333 /****************************************************************
335 * The following are only guaranteed to work in subst.c *
337 ****************************************************************/
339 #if defined (HANDLE_MULTIBYTE)
340 # define SCOPY_CHAR_I(_dst, _escchar, _sc, _src, _si, _slen) \
343 if (locale_mb_cur_max > 1) \
345 mbstate_t state_bak; \
349 _i = is_basic (*((_src) + (_si))); \
355 mblength = mbrlen ((_src) + (_si), (_slen) - (_si), &state); \
357 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
363 mblength = (mblength < 1) ? 1 : mblength; \
365 temp = xmalloc (mblength + 2); \
366 temp[0] = _escchar; \
367 for (_i = 0; _i < mblength; _i++) \
368 temp[_i + 1] = _src[_si++]; \
369 temp[mblength + 1] = '\0'; \
375 _dst[0] = _escchar; \
381 # define SCOPY_CHAR_I(_dst, _escchar, _sc, _src, _si, _slen) \
382 _dst[0] = _escchar; \
384 #endif /* !HANDLE_MULTIBYTE */
386 #if defined (HANDLE_MULTIBYTE)
387 # define SCOPY_CHAR_M(_dst, _src, _srcend, _si) \
390 if (locale_mb_cur_max > 1) \
392 mbstate_t state_bak; \
396 _i = is_basic (*((_src) + (_si))); \
402 mblength = mbrlen ((_src) + (_si), (_srcend) - ((_src) + (_si)), &state); \
404 if (mblength == (size_t)-2 || mblength == (size_t)-1) \
410 mblength = (mblength < 1) ? 1 : mblength; \
412 FASTCOPY(((_src) + (_si)), (_dst), mblength); \
414 (_dst) += mblength; \
419 *(_dst)++ = _src[(_si)]; \
425 # define SCOPY_CHAR_M(_dst, _src, _srcend, _si) \
426 *(_dst)++ = _src[(_si)]; \
428 #endif /* !HANDLE_MULTIBYTE */
431 # define SADD_MBCHAR(_dst, _src, _si, _srcsize) \
434 if (locale_mb_cur_max > 1) \
437 mbstate_t state_bak; \
440 i = is_basic (*((_src) + (_si))); \
446 mblength = mbrlen ((_src) + (_si), (_srcsize) - (_si), &state); \
448 if (mblength == (size_t)-1 || mblength == (size_t)-2) \
456 _dst = (char *)xmalloc (mblength + 1); \
457 for (i = 0; i < mblength; i++) \
458 (_dst)[i] = (_src)[(_si)++]; \
459 (_dst)[mblength] = '\0'; \
467 # define SADD_MBCHAR(_dst, _src, _si, _srcsize)
470 /* Watch out when using this -- it's just straight textual substitution */
471 #if defined (HANDLE_MULTIBYTE)
472 # define SADD_MBQCHAR_BODY(_dst, _src, _si, _srcsize) \
475 mbstate_t state_bak; \
478 i = is_basic (*((_src) + (_si))); \
484 mblength = mbrlen ((_src) + (_si), (_srcsize) - (_si), &state); \
486 if (mblength == (size_t)-1 || mblength == (size_t)-2) \
494 (_dst) = (char *)xmalloc (mblength + 2); \
495 (_dst)[0] = CTLESC; \
496 for (i = 0; i < mblength; i++) \
497 (_dst)[i+1] = (_src)[(_si)++]; \
498 (_dst)[mblength+1] = '\0'; \
502 #endif /* HANDLE_MULTIBYTE */
503 #endif /* _SH_MBUTIL_H_ */