summaryrefslogtreecommitdiff
blob: 1e74832b638afa0f6f7097004e687b32b0eef847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
From: Tim Waugh <twaugh@redhat.com>
To: bug-bash@gnu.org
Subject: [patch] multibyte IFS values
Date: Tue, 24 Aug 2004 13:34:59 +0100

Hi,

Here is a patch to address these problems:

http://lists.gnu.org/archive/html/bug-bash/2004-07/msg00294.html
http://lists.gnu.org/archive/html/bug-bash/2004-07/msg00296.html

It works well for me at least.

Tim.

--- bash-3.0/subst.c.multibyteifs	2004-08-20 15:22:48.366497771 +0100
+++ bash-3.0/subst.c	2004-08-20 18:13:30.833624616 +0100
@@ -124,7 +124,12 @@
 SHELL_VAR *ifs_var;
 char *ifs_value;
 unsigned char ifs_cmap[UCHAR_MAX + 1];
+#if defined (HANDLE_MULTIBYTE)
+unsigned char ifs_firstc[MB_LEN_MAX];
+size_t ifs_firstc_len;
+#else
 unsigned char ifs_firstc;
+#endif
 
 /* Extern functions and variables from different files. */
 extern int last_command_exit_value, last_command_exit_signal;
@@ -862,8 +867,14 @@
      char *charlist;
 {
   register int i = *sindex;
+  size_t slen;
+#if defined (HANDLE_MULTIBYTE)
+  size_t clen;
+  wchar_t *wcharlist = NULL;
+#endif
   int c;
   char *temp;
+  DECLARE_MBSTATE;
 
   if (charlist[0] == '\'' && charlist[1] == '\0')
     {
@@ -872,18 +883,65 @@
       return temp;
     }
 
-  for (i = *sindex; c = string[i]; i++)
+  slen = strlen (string + *sindex) + *sindex;
+  i = *sindex;
+#if defined (HANDLE_MULTIBYTE)
+  clen = strlen (charlist);
+#endif
+  while ((c = string[i]))
     {
+#if defined (HANDLE_MULTIBYTE)
+      size_t mblength;
+#endif
+
       if (c == CTLESC)
 	{
-	  i++;
+	  i += 2;
 	  continue;
 	}
 
+#if defined (HANDLE_MULTIBYTE)
+      mblength = mblen (string + i, slen - i);
+      if (mblength > 1)
+	{
+	  wchar_t wc;
+	  size_t mblength = mbtowc (&wc, string + i, slen - i);
+	  if (MB_INVALIDCH (mblength))
+	    {
+	      if (MEMBER (c, charlist))
+		break;
+	    }
+	  else
+	    {
+	      if (!wcharlist)
+		{
+		  size_t len = mbstowcs (wcharlist, charlist, 0);
+		  if (len == -1)
+		    len = 0;
+		  wcharlist = xmalloc (sizeof (wchar_t) * (len + 1));
+		  mbstowcs (wcharlist, charlist, 1 + len);
+		}
+
+	      if (wcschr (wcharlist, wc))
+		{
+		  break;
+		}
+	    }
+	}
+      else
+#endif
+
       if (MEMBER (c, charlist))
 	break;
+
+      ADVANCE_CHAR (string, slen, i);
     }
 
+#if defined (HANDLE_MULTIBYTE)
+  if (wcharlist)
+    free (wcharlist);
+#endif
+
   temp = substring (string, *sindex, i);
   *sindex = i;
 
@@ -1456,11 +1514,36 @@
   d2 = 0;
   if (delims)
     {
-      d2 = (char *)xmalloc (strlen (delims) + 1);
-      for (i = ts = 0; delims[i]; i++)
+      size_t slength = strlen (delims);
+#if defined (HANDLE_MULTIBYTE)
+      size_t mblength = 1;
+      DECLARE_MBSTATE;
+#endif
+
+      d2 = (char *)xmalloc (slength + 1);
+      i = ts = 0;
+      while (delims[i])
 	{
+#if defined (HANDLE_MULTIBYTE)
+	  mbstate_t state_bak = state;
+	  mblength = mbrlen (delims + i, slength, &state);
+
+	  if (MB_INVALIDCH (mblength))
+	    state = state_bak;
+	  else if (mblength != 1)
+	    {
+	      memcpy (d2 + ts, delims + i, mblength);
+	      ts += mblength;
+	      i += mblength;
+	      slength -= mblength;
+	      continue;
+	    }
+#endif
+
 	  if (whitespace(delims[i]) == 0)
 	    d2[ts++] = delims[i];
+	  i++;
+	  slength--;
 	}
       d2[ts] = '\0';
     }
@@ -1654,10 +1737,19 @@
 string_list_dollar_star (list)
      WORD_LIST *list;
 {
+#if defined (HANDLE_MULTIBYTE)
+  char sep[MB_CUR_MAX + 1];
+#else
   char sep[2];
+#endif
 
+#if defined (HANDLE_MULTIBYTE)
+  memcpy (sep, ifs_firstc, ifs_firstc_len);
+  sep[ifs_firstc_len] = '\0';
+#else
   sep[0] = ifs_firstc;
   sep[1] = '\0';
+#endif
 
   return (string_list_internal (list, sep));
 }
@@ -1676,14 +1768,41 @@
      WORD_LIST *list;
      int quoted;
 {
-  char *ifs, sep[2];
+  char *ifs;
+#if defined (HANDLE_MULTIBYTE)
+  char sep[MB_CUR_MAX + 1];
+#else
+  char sep[2];
+#endif
   WORD_LIST *tlist;
 
   /* XXX this could just be ifs = ifs_value; */
   ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
 
+#if defined (HANDLE_MULTIBYTE)
+  if (ifs && *ifs)
+    {
+      size_t mblength = mblen (ifs, strnlen (ifs, MB_CUR_MAX));
+      if (MB_INVALIDCH (mblength))
+	{
+	  sep[0] = *ifs;
+	  sep[1] = '\0';
+	}
+      else
+	{
+	  memcpy (sep, ifs, mblength);
+	  sep[mblength] = '\0';
+	}
+    }
+  else
+    {
+      sep[0] = ' ';
+      sep[1] = '\0';
+    }
+#else
   sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs;
   sep[1] = '\0';
+#endif
 
   tlist = ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (ifs && *ifs == 0))
 		? quote_list (list)
@@ -1732,6 +1851,7 @@
   WORD_DESC *t;
   char *current_word, *s;
   int sindex, sh_style_split, whitesep;
+  size_t slen = 0;
 
   if (!string || !*string)
     return ((WORD_LIST *)NULL);
@@ -1805,7 +1925,12 @@
 
       /* Move past the current separator character. */
       if (string[sindex])
-	sindex++;
+	{
+	  DECLARE_MBSTATE;
+	  if (!slen)
+	    slen = strlen (string);
+	  ADVANCE_CHAR (string, slen, sindex);
+	}
 
       /* Now skip sequences of space, tab, or newline characters if they are
 	 in the list of separators. */
@@ -6796,7 +6921,27 @@
       ifs_cmap[uc] = 1;
     }
 
+#if defined (HANDLE_MULTIBYTE)
+  if (!ifs_value)
+    {
+      ifs_firstc[0] = '\0';
+      ifs_firstc_len = 1;
+    }
+  else
+    {
+      size_t ifs_len = strnlen (ifs_value, MB_CUR_MAX);
+      ifs_firstc_len = mblen (ifs_value, ifs_len);
+      if (MB_INVALIDCH (ifs_firstc_len))
+        {
+          ifs_firstc[0] = '\0';
+          ifs_firstc_len = 1;
+        }
+      else
+        memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
+    }
+#else
   ifs_firstc = ifs_value ? *ifs_value : 0;
+#endif
 }
 
 char *
--- bash-3.0/subst.h.multibyteifs	2004-08-20 15:51:08.301074583 +0100
+++ bash-3.0/subst.h	2004-08-20 15:51:39.070206473 +0100
@@ -231,7 +231,12 @@
 extern SHELL_VAR *ifs_var;
 extern char *ifs_value;
 extern unsigned char ifs_cmap[];
+#if defined (HANDLE_MULTIBYTE)
+extern unsigned char ifs_firstc[];
+extern size_t ifs_firstc_len;
+#else
 extern unsigned char ifs_firstc;
+#endif
 
 /* Evaluates to 1 if C is a character in $IFS. */
 #define isifs(c)	(ifs_cmap[(unsigned char)(c)] != 0)