1 | /* xmbsrtowcs.c -- replacement function for mbsrtowcs */
|
---|
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 | #include <config.h>
|
---|
21 |
|
---|
22 | #include <bashansi.h>
|
---|
23 |
|
---|
24 | /* <wchar.h>, <wctype.h> and <stdlib.h> are included in "shmbutil.h".
|
---|
25 | If <wchar.h>, <wctype.h>, mbsrtowcs(), exist, HANDLE_MULTIBYTE
|
---|
26 | is defined as 1. */
|
---|
27 | #include <shmbutil.h>
|
---|
28 |
|
---|
29 | #if HANDLE_MULTIBYTE
|
---|
30 | /* On some locales (ex. ja_JP.sjis), mbsrtowc doesn't convert 0x5c to U<0x5c>.
|
---|
31 | So, this function is made for converting 0x5c to U<0x5c>. */
|
---|
32 |
|
---|
33 | static mbstate_t local_state;
|
---|
34 | static int local_state_use = 0;
|
---|
35 |
|
---|
36 | size_t
|
---|
37 | xmbsrtowcs (dest, src, len, pstate)
|
---|
38 | wchar_t *dest;
|
---|
39 | const char **src;
|
---|
40 | size_t len;
|
---|
41 | mbstate_t *pstate;
|
---|
42 | {
|
---|
43 | mbstate_t *ps;
|
---|
44 | size_t mblength, wclength, n;
|
---|
45 |
|
---|
46 | ps = pstate;
|
---|
47 | if (pstate == NULL)
|
---|
48 | {
|
---|
49 | if (!local_state_use)
|
---|
50 | {
|
---|
51 | memset (&local_state, '\0', sizeof(mbstate_t));
|
---|
52 | local_state_use = 1;
|
---|
53 | }
|
---|
54 | ps = &local_state;
|
---|
55 | }
|
---|
56 |
|
---|
57 | n = strlen (*src);
|
---|
58 |
|
---|
59 | if (dest == NULL)
|
---|
60 | {
|
---|
61 | wchar_t *wsbuf;
|
---|
62 | const char *mbs;
|
---|
63 | mbstate_t psbuf;
|
---|
64 |
|
---|
65 | /* It doesn't matter if malloc fails here, since mbsrtowcs should do
|
---|
66 | the right thing with a NULL first argument. */
|
---|
67 | wsbuf = (wchar_t *) malloc ((n + 1) * sizeof(wchar_t));
|
---|
68 | mbs = *src;
|
---|
69 | psbuf = *ps;
|
---|
70 |
|
---|
71 | wclength = mbsrtowcs (wsbuf, &mbs, n, &psbuf);
|
---|
72 |
|
---|
73 | if (wsbuf)
|
---|
74 | free (wsbuf);
|
---|
75 | return wclength;
|
---|
76 | }
|
---|
77 |
|
---|
78 | for (wclength = 0; wclength < len; wclength++, dest++)
|
---|
79 | {
|
---|
80 | if (mbsinit(ps))
|
---|
81 | {
|
---|
82 | if (**src == '\0')
|
---|
83 | {
|
---|
84 | *dest = L'\0';
|
---|
85 | *src = NULL;
|
---|
86 | return (wclength);
|
---|
87 | }
|
---|
88 | else if (**src == '\\')
|
---|
89 | {
|
---|
90 | *dest = L'\\';
|
---|
91 | mblength = 1;
|
---|
92 | }
|
---|
93 | else
|
---|
94 | mblength = mbrtowc(dest, *src, n, ps);
|
---|
95 | }
|
---|
96 | else
|
---|
97 | mblength = mbrtowc(dest, *src, n, ps);
|
---|
98 |
|
---|
99 | /* Cannot convert multibyte character to wide character. */
|
---|
100 | if (mblength == (size_t)-1 || mblength == (size_t)-2)
|
---|
101 | return (size_t)-1;
|
---|
102 |
|
---|
103 | *src += mblength;
|
---|
104 | n -= mblength;
|
---|
105 |
|
---|
106 | /* The multibyte string has been completely converted,
|
---|
107 | including the terminating '\0'. */
|
---|
108 | if (*dest == L'\0')
|
---|
109 | {
|
---|
110 | *src = NULL;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | return (wclength);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* Convert a multibyte string to a wide character string. Memory for the
|
---|
119 | new wide character string is obtained with malloc.
|
---|
120 |
|
---|
121 | The return value is the length of the wide character string. Returns a
|
---|
122 | pointer to the wide character string in DESTP. If INDICESP is not NULL,
|
---|
123 | INDICESP stores the pointer to the pointer array. Each pointer is to
|
---|
124 | the first byte of each multibyte character. Memory for the pointer array
|
---|
125 | is obtained with malloc, too.
|
---|
126 | If conversion is failed, the return value is (size_t)-1 and the values
|
---|
127 | of DESTP and INDICESP are NULL. */
|
---|
128 |
|
---|
129 | #define WSBUF_INC 32
|
---|
130 |
|
---|
131 | size_t
|
---|
132 | xdupmbstowcs (destp, indicesp, src)
|
---|
133 | wchar_t **destp; /* Store the pointer to the wide character string */
|
---|
134 | char ***indicesp; /* Store the pointer to the pointer array. */
|
---|
135 | const char *src; /* Multibyte character string */
|
---|
136 | {
|
---|
137 | const char *p; /* Conversion start position of src */
|
---|
138 | wchar_t wc; /* Created wide character by conversion */
|
---|
139 | wchar_t *wsbuf; /* Buffer for wide characters. */
|
---|
140 | char **indices; /* Buffer for indices. */
|
---|
141 | size_t wsbuf_size; /* Size of WSBUF */
|
---|
142 | size_t wcnum; /* Number of wide characters in WSBUF */
|
---|
143 | mbstate_t state; /* Conversion State */
|
---|
144 |
|
---|
145 | /* In case SRC or DESP is NULL, conversion doesn't take place. */
|
---|
146 | if (src == NULL || destp == NULL)
|
---|
147 | {
|
---|
148 | *destp = NULL;
|
---|
149 | return (size_t)-1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | memset (&state, '\0', sizeof(mbstate_t));
|
---|
153 | wsbuf_size = WSBUF_INC;
|
---|
154 |
|
---|
155 | wsbuf = (wchar_t *) malloc (wsbuf_size * sizeof(wchar_t));
|
---|
156 | if (wsbuf == NULL)
|
---|
157 | {
|
---|
158 | *destp = NULL;
|
---|
159 | return (size_t)-1;
|
---|
160 | }
|
---|
161 |
|
---|
162 | indices = (char **) malloc (wsbuf_size * sizeof(char *));
|
---|
163 | if (indices == NULL)
|
---|
164 | {
|
---|
165 | free (wsbuf);
|
---|
166 | *destp = NULL;
|
---|
167 | return (size_t)-1;
|
---|
168 | }
|
---|
169 |
|
---|
170 | p = src;
|
---|
171 | wcnum = 0;
|
---|
172 | do
|
---|
173 | {
|
---|
174 | size_t mblength; /* Byte length of one multibyte character. */
|
---|
175 |
|
---|
176 | if (mbsinit (&state))
|
---|
177 | {
|
---|
178 | if (*p == '\0')
|
---|
179 | {
|
---|
180 | wc = L'\0';
|
---|
181 | mblength = 1;
|
---|
182 | }
|
---|
183 | else if (*p == '\\')
|
---|
184 | {
|
---|
185 | wc = L'\\';
|
---|
186 | mblength = 1;
|
---|
187 | }
|
---|
188 | else
|
---|
189 | mblength = mbrtowc(&wc, p, MB_LEN_MAX, &state);
|
---|
190 | }
|
---|
191 | else
|
---|
192 | mblength = mbrtowc(&wc, p, MB_LEN_MAX, &state);
|
---|
193 |
|
---|
194 | /* Conversion failed. */
|
---|
195 | if (MB_INVALIDCH (mblength))
|
---|
196 | {
|
---|
197 | free (wsbuf);
|
---|
198 | free (indices);
|
---|
199 | *destp = NULL;
|
---|
200 | return (size_t)-1;
|
---|
201 | }
|
---|
202 |
|
---|
203 | ++wcnum;
|
---|
204 |
|
---|
205 | /* Resize buffers when they are not large enough. */
|
---|
206 | if (wsbuf_size < wcnum)
|
---|
207 | {
|
---|
208 | wchar_t *wstmp;
|
---|
209 | char **idxtmp;
|
---|
210 |
|
---|
211 | wsbuf_size += WSBUF_INC;
|
---|
212 |
|
---|
213 | wstmp = (wchar_t *) realloc (wsbuf, wsbuf_size * sizeof (wchar_t));
|
---|
214 | if (wstmp == NULL)
|
---|
215 | {
|
---|
216 | free (wsbuf);
|
---|
217 | free (indices);
|
---|
218 | *destp = NULL;
|
---|
219 | return (size_t)-1;
|
---|
220 | }
|
---|
221 | wsbuf = wstmp;
|
---|
222 |
|
---|
223 | idxtmp = (char **) realloc (indices, wsbuf_size * sizeof (char **));
|
---|
224 | if (idxtmp == NULL)
|
---|
225 | {
|
---|
226 | free (wsbuf);
|
---|
227 | free (indices);
|
---|
228 | *destp = NULL;
|
---|
229 | return (size_t)-1;
|
---|
230 | }
|
---|
231 | indices = idxtmp;
|
---|
232 | }
|
---|
233 |
|
---|
234 | wsbuf[wcnum - 1] = wc;
|
---|
235 | indices[wcnum - 1] = (char *)p;
|
---|
236 | p += mblength;
|
---|
237 | }
|
---|
238 | while (MB_NULLWCH (wc) == 0);
|
---|
239 |
|
---|
240 | /* Return the length of the wide character string, not including `\0'. */
|
---|
241 | *destp = wsbuf;
|
---|
242 | if (indicesp != NULL)
|
---|
243 | *indicesp = indices;
|
---|
244 | else
|
---|
245 | free (indices);
|
---|
246 |
|
---|
247 | return (wcnum - 1);
|
---|
248 | }
|
---|
249 |
|
---|
250 | #endif /* HANDLE_MULTIBYTE */
|
---|