Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / lib / glob / xmbsrtowcs.c
1 /* xmbsrtowcs.c -- replacement function for mbsrtowcs */
2
3 /* Copyright (C) 2002 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) + 1;
58
59   if (dest == NULL)
60     {
61       wchar_t *wsbuf;
62       char *mbsbuf, *mbsbuf_top;
63       mbstate_t psbuf;
64
65       wsbuf = (wchar_t *) malloc ((n + 1) * sizeof(wchar_t));
66       mbsbuf_top = mbsbuf = (char *) malloc (n + 1);
67       memcpy(mbsbuf, *src, n + 1);
68       psbuf = *ps;
69
70       wclength = mbsrtowcs (wsbuf, (const char **)&mbsbuf, n, &psbuf);
71
72       free (wsbuf);
73       free (mbsbuf_top);
74       return wclength;
75     }
76       
77   for(wclength = 0; wclength < len; wclength++, dest++)
78     {
79       if(mbsinit(ps))
80         {
81           if (**src == '\0')
82             {
83               *dest = L'\0';
84               *src = NULL;
85               return (wclength);
86             }
87           else if (**src == '\\')
88             {
89               *dest = L'\\';
90               mblength = 1;
91             }
92           else
93             mblength = mbrtowc(dest, *src, n, ps);
94         }
95       else
96         mblength = mbrtowc(dest, *src, n, ps);
97
98       /* Cannot convert multibyte character to wide character. */
99       if (mblength == (size_t)-1 || mblength == (size_t)-2)
100         return (size_t)-1;
101
102       *src += mblength;
103       n -= mblength;
104
105       /* The multibyte string  has  been  completely  converted,
106          including  the terminating '\0'. */
107       if (*dest == L'\0')
108         {
109           *src = NULL;
110           break;
111         }
112     }
113
114     return (wclength);
115 }
116 #endif /* HANDLE_MULTIBYTE */