| 1 | /* shmbutil.h -- utility functions for multibyte characters. */
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 2002-2004 Free Software Foundation, Inc.
|
|---|
| 4 |
|
|---|
| 5 | This file is part of GNU Bash, the Bourne Again SHell.
|
|---|
| 6 |
|
|---|
| 7 | Bash is free software; you can redistribute it and/or modify it under
|
|---|
| 8 | the terms of the GNU General Public License as published by the Free
|
|---|
| 9 | Software Foundation; either version 2, or (at your option) any later
|
|---|
| 10 | version.
|
|---|
| 11 |
|
|---|
| 12 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|---|
| 14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|---|
| 15 | for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License along
|
|---|
| 18 | with Bash; see the file COPYING. If not, write to the Free Software
|
|---|
| 19 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
|---|
| 20 |
|
|---|
| 21 | #if !defined (_SH_MBUTIL_H_)
|
|---|
| 22 | #define _SH_MBUTIL_H_
|
|---|
| 23 |
|
|---|
| 24 | #include "stdc.h"
|
|---|
| 25 |
|
|---|
| 26 | /* Include config.h for HANDLE_MULTIBYTE */
|
|---|
| 27 | #include <config.h>
|
|---|
| 28 |
|
|---|
| 29 | #if defined (HANDLE_MULTIBYTE)
|
|---|
| 30 |
|
|---|
| 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 *));
|
|---|
| 33 |
|
|---|
| 34 | extern size_t mbstrlen __P((const char *));
|
|---|
| 35 |
|
|---|
| 36 | extern char *xstrchr __P((const char *, int));
|
|---|
| 37 |
|
|---|
| 38 | #ifndef MB_INVALIDCH
|
|---|
| 39 | #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
|
|---|
| 40 | #define MB_NULLWCH(x) ((x) == 0)
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 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))
|
|---|
| 45 |
|
|---|
| 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)
|
|---|
| 48 |
|
|---|
| 49 | #else /* !HANDLE_MULTIBYTE */
|
|---|
| 50 |
|
|---|
| 51 | #undef MB_LEN_MAX
|
|---|
| 52 | #undef MB_CUR_MAX
|
|---|
| 53 |
|
|---|
| 54 | #define MB_LEN_MAX 1
|
|---|
| 55 | #define MB_CUR_MAX 1
|
|---|
| 56 |
|
|---|
| 57 | #undef xstrchr
|
|---|
| 58 | #define xstrchr(s, c) strchr(s, c)
|
|---|
| 59 |
|
|---|
| 60 | #ifndef MB_INVALIDCH
|
|---|
| 61 | #define MB_INVALIDCH(x) (0)
|
|---|
| 62 | #define MB_NULLWCH(x) (0)
|
|---|
| 63 | #endif
|
|---|
| 64 |
|
|---|
| 65 | #define MB_STRLEN(s) (STRLEN(s))
|
|---|
| 66 |
|
|---|
| 67 | #define MBLEN(s, n) 1
|
|---|
| 68 | #define MBRLEN(s, n, p) 1
|
|---|
| 69 |
|
|---|
| 70 | #endif /* !HANDLE_MULTIBYTE */
|
|---|
| 71 |
|
|---|
| 72 | /* Declare and initialize a multibyte state. Call must be terminated
|
|---|
| 73 | with `;'. */
|
|---|
| 74 | #if defined (HANDLE_MULTIBYTE)
|
|---|
| 75 | # define DECLARE_MBSTATE \
|
|---|
| 76 | mbstate_t state; \
|
|---|
| 77 | memset (&state, '\0', sizeof (mbstate_t))
|
|---|
| 78 | #else
|
|---|
| 79 | # define DECLARE_MBSTATE
|
|---|
| 80 | #endif /* !HANDLE_MULTIBYTE */
|
|---|
| 81 |
|
|---|
| 82 | /* Initialize or reinitialize a multibyte state named `state'. Call must be
|
|---|
| 83 | terminated with `;'. */
|
|---|
| 84 | #if defined (HANDLE_MULTIBYTE)
|
|---|
| 85 | # define INITIALIZE_MBSTATE memset (&state, '\0', sizeof (mbstate_t))
|
|---|
| 86 | #else
|
|---|
| 87 | # define INITIALIZE_MBSTATE
|
|---|
| 88 | #endif /* !HANDLE_MULTIBYTE */
|
|---|
| 89 |
|
|---|
| 90 | /* Advance one (possibly multi-byte) character in string _STR of length
|
|---|
| 91 | _STRSIZE, starting at index _I. STATE must have already been declared. */
|
|---|
| 92 | #if defined (HANDLE_MULTIBYTE)
|
|---|
| 93 | # define ADVANCE_CHAR(_str, _strsize, _i) \
|
|---|
| 94 | do \
|
|---|
| 95 | { \
|
|---|
| 96 | if (MB_CUR_MAX > 1) \
|
|---|
| 97 | { \
|
|---|
| 98 | mbstate_t state_bak; \
|
|---|
| 99 | size_t mblength; \
|
|---|
| 100 | \
|
|---|
| 101 | state_bak = state; \
|
|---|
| 102 | mblength = mbrlen ((_str) + (_i), (_strsize) - (_i), &state); \
|
|---|
| 103 | \
|
|---|
| 104 | if (mblength == (size_t)-2 || mblength == (size_t)-1) \
|
|---|
| 105 | { \
|
|---|
| 106 | state = state_bak; \
|
|---|
| 107 | (_i)++; \
|
|---|
| 108 | } \
|
|---|
| 109 | else if (mblength == 0) \
|
|---|
| 110 | (_i)++; \
|
|---|
| 111 | else \
|
|---|
| 112 | (_i) += mblength; \
|
|---|
| 113 | } \
|
|---|
| 114 | else \
|
|---|
| 115 | (_i)++; \
|
|---|
| 116 | } \
|
|---|
| 117 | while (0)
|
|---|
| 118 | #else
|
|---|
| 119 | # define ADVANCE_CHAR(_str, _strsize, _i) (_i)++
|
|---|
| 120 | #endif /* !HANDLE_MULTIBYTE */
|
|---|
| 121 |
|
|---|
| 122 | /* Advance one (possibly multibyte) character in the string _STR of length
|
|---|
| 123 | _STRSIZE.
|
|---|
| 124 | SPECIAL: assume that _STR will be incremented by 1 after this call. */
|
|---|
| 125 | #if defined (HANDLE_MULTIBYTE)
|
|---|
| 126 | # define ADVANCE_CHAR_P(_str, _strsize) \
|
|---|
| 127 | do \
|
|---|
| 128 | { \
|
|---|
| 129 | if (MB_CUR_MAX > 1) \
|
|---|
| 130 | { \
|
|---|
| 131 | mbstate_t state_bak; \
|
|---|
| 132 | size_t mblength; \
|
|---|
| 133 | \
|
|---|
| 134 | state_bak = state; \
|
|---|
| 135 | mblength = mbrlen ((_str), (_strsize), &state); \
|
|---|
| 136 | \
|
|---|
| 137 | if (mblength == (size_t)-2 || mblength == (size_t)-1) \
|
|---|
| 138 | { \
|
|---|
| 139 | state = state_bak; \
|
|---|
| 140 | mblength = 1; \
|
|---|
| 141 | } \
|
|---|
| 142 | else \
|
|---|
| 143 | (_str) += (mblength < 1) ? 0 : (mblength - 1); \
|
|---|
| 144 | } \
|
|---|
| 145 | } \
|
|---|
| 146 | while (0)
|
|---|
| 147 | #else
|
|---|
| 148 | # define ADVANCE_CHAR_P(_str, _strsize)
|
|---|
| 149 | #endif /* !HANDLE_MULTIBYTE */
|
|---|
| 150 |
|
|---|
| 151 | /* Back up one (possibly multi-byte) character in string _STR of length
|
|---|
| 152 | _STRSIZE, starting at index _I. STATE must have already been declared. */
|
|---|
| 153 | #if defined (HANDLE_MULTIBYTE)
|
|---|
| 154 | # define BACKUP_CHAR(_str, _strsize, _i) \
|
|---|
| 155 | do \
|
|---|
| 156 | { \
|
|---|
|
|---|