858842e627550024d7f2560380c9714c5e73e03e
[platform/upstream/bash.git] / lib / readline / mbutil.c
1 /* mbutil.c -- readline multibyte character utility functions */
2
3 /* Copyright (C) 2001-2009 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library (Readline), a library
6    for reading lines of text with interactive input and history editing.      
7
8    Readline is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12
13    Readline is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Readline.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #include "posixjmp.h"
31
32 #if defined (HAVE_UNISTD_H)
33 #  include <unistd.h>      /* for _POSIX_VERSION */
34 #endif /* HAVE_UNISTD_H */
35
36 #if defined (HAVE_STDLIB_H)
37 #  include <stdlib.h>
38 #else
39 #  include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
41
42 #include <stdio.h>
43 #include <ctype.h>
44
45 /* System-specific feature definitions and include files. */
46 #include "rldefs.h"
47 #include "rlmbutil.h"
48
49 #if defined (TIOCSTAT_IN_SYS_IOCTL)
50 #  include <sys/ioctl.h>
51 #endif /* TIOCSTAT_IN_SYS_IOCTL */
52
53 /* Some standard library routines. */
54 #include "readline.h"
55
56 #include "rlprivate.h"
57 #include "xmalloc.h"
58
59 /* Declared here so it can be shared between the readline and history
60    libraries. */
61 #if defined (HANDLE_MULTIBYTE)
62 int rl_byte_oriented = 0;
63 #else
64 int rl_byte_oriented = 1;
65 #endif
66
67 /* **************************************************************** */
68 /*                                                                  */
69 /*              Multibyte Character Utility Functions               */
70 /*                                                                  */
71 /* **************************************************************** */
72
73 #if defined(HANDLE_MULTIBYTE)
74
75 static int
76 _rl_find_next_mbchar_internal (string, seed, count, find_non_zero)
77      char *string;
78      int seed, count, find_non_zero;
79 {
80   size_t tmp;
81   mbstate_t ps;
82   int point;
83   wchar_t wc;
84
85   tmp = 0;
86
87   memset(&ps, 0, sizeof (mbstate_t));
88   if (seed < 0)
89     seed = 0;
90   if (count <= 0)
91     return seed;
92
93   point = seed + _rl_adjust_point (string, seed, &ps);
94   /* if this is true, means that seed was not pointing to a byte indicating
95      the beginning of a multibyte character.  Correct the point and consume
96      one char. */
97   if (seed < point)
98     count--;
99
100   while (count > 0)  
101     {
102       tmp = mbrtowc (&wc, string+point, strlen(string + point), &ps);
103       if (MB_INVALIDCH ((size_t)tmp))
104         {
105           /* invalid bytes. assume a byte represents a character */
106           point++;
107           count--;
108           /* reset states. */
109           memset(&ps, 0, sizeof(mbstate_t));
110         }
111       else if (MB_NULLWCH (tmp))
112         break;                  /* found wide '\0' */
113       else
114         {
115           /* valid bytes */
116           point += tmp;
117           if (find_non_zero)
118             {
119               if (wcwidth (wc) == 0)
120                 continue;
121               else
122                 count--;
123             }
124           else
125             count--;
126         }
127     }
128
129   if (find_non_zero)
130     {
131       tmp = mbrtowc (&wc, string + point, strlen (string + point), &ps);
132       while (MB_NULLWCH (tmp) == 0 && MB_INVALIDCH (tmp) == 0 && wcwidth (wc) == 0)
133         {
134           point += tmp;
135           tmp = mbrtowc (&wc, string + point, strlen (string + point), &ps);
136         }
137     }
138
139   return point;
140 }
141
142 static int
143 _rl_find_prev_mbchar_internal (string, seed, find_non_zero)
144      char *string;
145      int seed, find_non_zero;
146 {
147   mbstate_t ps;
148   int prev, non_zero_prev, point, length;
149   size_t tmp;
150   wchar_t wc;
151
152   memset(&ps, 0, sizeof(mbstate_t));
153   length = strlen(string);
154   
155   if (seed < 0)
156     return 0;
157   else if (length < seed)
158     return length;
159
160   prev = non_zero_prev = point = 0;
161   while (point < seed)
162     {
163       tmp = mbrtowc (&wc, string + point, length - point, &ps);
164       if (MB_INVALIDCH ((size_t)tmp))
165         {
166           /* in this case, bytes are invalid or shorted to compose
167              multibyte char, so assume that the first byte represents
168              a single character anyway. */
169           tmp = 1;
170           /* clear the state of the byte sequence, because
171              in this case effect of mbstate is undefined  */
172           memset(&ps, 0, sizeof (mbstate_t));
173
174           /* Since we're assuming that this byte represents a single
175              non-zero-width character, don't forget about it. */
176           prev = point;
177         }
178       else if (MB_NULLWCH (tmp))
179         break;                  /* Found '\0' char.  Can this happen? */
180       else
181         {
182           if (find_non_zero)
183             {
184               if (wcwidth (wc) != 0)
185                 prev = point;
186             }
187           else
188             prev = point;  
189         }
190
191       point += tmp;
192     }
193
194   return prev;
195 }
196
197 /* return the number of bytes parsed from the multibyte sequence starting
198    at src, if a non-L'\0' wide character was recognized. It returns 0, 
199    if a L'\0' wide character was recognized. It  returns (size_t)(-1), 
200    if an invalid multibyte sequence was encountered. It returns (size_t)(-2) 
201    if it couldn't parse a complete  multibyte character.  */
202 int
203 _rl_get_char_len (src, ps)
204      char *src;
205      mbstate_t *ps;
206 {
207   size_t tmp;
208
209   tmp = mbrlen((const char *)src, (size_t)strlen (src), ps);
210   if (tmp == (size_t)(-2))
211     {
212       /* shorted to compose multibyte char */
213       if (ps)
214         memset (ps, 0, sizeof(mbstate_t));
215       return -2;
216     }
217   else if (tmp == (size_t)(-1))
218     {
219       /* invalid to compose multibyte char */
220       /* initialize the conversion state */
221       if (ps)
222         memset (ps, 0, sizeof(mbstate_t));
223       return -1;
224     }
225   else if (tmp == (size_t)0)
226     return 0;
227   else
228     return (int)tmp;
229 }
230
231 /* compare the specified two characters. If the characters matched,
232    return 1. Otherwise return 0. */
233 int
234 _rl_compare_chars (buf1, pos1, ps1, buf2, pos2, ps2)
235      char *buf1;
236      int pos1;
237      mbstate_t *ps1;
238      char *buf2;
239      int pos2;
240      mbstate_t *ps2;
241 {
242   int i, w1, w2;
243
244   if ((w1 = _rl_get_char_len (&buf1[pos1], ps1)) <= 0 || 
245         (w2 = _rl_get_char_len (&buf2[pos2], ps2)) <= 0 ||
246         (w1 != w2) ||
247         (buf1[pos1] != buf2[pos2]))
248     return 0;
249
250   for (i = 1; i < w1; i++)
251     if (buf1[pos1+i] != buf2[pos2+i])
252       return 0;
253
254   return 1;
255 }
256
257 /* adjust pointed byte and find mbstate of the point of string.
258    adjusted point will be point <= adjusted_point, and returns
259    differences of the byte(adjusted_point - point).
260    if point is invalied (point < 0 || more than string length),
261    it returns -1 */
262 int
263 _rl_adjust_point(string, point, ps)
264      char *string;
265      int point;
266      mbstate_t *ps;
267 {
268   size_t tmp = 0;
269   int length;
270   int pos = 0;
271
272   length = strlen(string);
273   if (point < 0)
274     return -1;
275   if (length < point)
276     return -1;
277   
278   while (pos < point)
279     {
280       tmp = mbrlen (string + pos, length - pos, ps);
281       if (MB_INVALIDCH ((size_t)tmp))
282         {
283           /* in this case, bytes are invalid or shorted to compose
284              multibyte char, so assume that the first byte represents
285              a single character anyway. */
286           pos++;
287           /* clear the state of the byte sequence, because
288              in this case effect of mbstate is undefined  */
289           if (ps)
290             memset (ps, 0, sizeof (mbstate_t));
291         }
292       else if (MB_NULLWCH (tmp))
293         pos++;
294       else
295         pos += tmp;
296     }
297
298   return (pos - point);
299 }
300
301 int
302 _rl_is_mbchar_matched (string, seed, end, mbchar, length)
303      char *string;
304      int seed, end;
305      char *mbchar;
306      int length;
307 {
308   int i;
309
310   if ((end - seed) < length)
311     return 0;
312
313   for (i = 0; i < length; i++)
314     if (string[seed + i] != mbchar[i])
315       return 0;
316   return 1;
317 }
318
319 wchar_t
320 _rl_char_value (buf, ind)
321      char *buf;
322      int ind;
323 {
324   size_t tmp;
325   wchar_t wc;
326   mbstate_t ps;
327   int l;
328
329   if (MB_LEN_MAX == 1 || rl_byte_oriented)
330     return ((wchar_t) buf[ind]);
331   l = strlen (buf);
332   if (ind >= l - 1)
333     return ((wchar_t) buf[ind]);
334   memset (&ps, 0, sizeof (mbstate_t));
335   tmp = mbrtowc (&wc, buf + ind, l - ind, &ps);
336   if (MB_INVALIDCH (tmp) || MB_NULLWCH (tmp))  
337     return ((wchar_t) buf[ind]);
338   return wc;
339 }
340 #endif /* HANDLE_MULTIBYTE */
341
342 /* Find next `count' characters started byte point of the specified seed.
343    If flags is MB_FIND_NONZERO, we look for non-zero-width multibyte
344    characters. */
345 #undef _rl_find_next_mbchar
346 int
347 _rl_find_next_mbchar (string, seed, count, flags)
348      char *string;
349      int seed, count, flags;
350 {
351 #if defined (HANDLE_MULTIBYTE)
352   return _rl_find_next_mbchar_internal (string, seed, count, flags);
353 #else
354   return (seed + count);
355 #endif
356 }
357
358 /* Find previous character started byte point of the specified seed.
359    Returned point will be point <= seed.  If flags is MB_FIND_NONZERO,
360    we look for non-zero-width multibyte characters. */
361 #undef _rl_find_prev_mbchar
362 int
363 _rl_find_prev_mbchar (string, seed, flags)
364      char *string;
365      int seed, flags;
366 {
367 #if defined (HANDLE_MULTIBYTE)
368   return _rl_find_prev_mbchar_internal (string, seed, flags);
369 #else
370   return ((seed == 0) ? seed : seed - 1);
371 #endif
372 }