1 /* mbutil.c -- readline multibyte character utility functions */
3 /* Copyright (C) 2001 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
33 # include <unistd.h> /* for _POSIX_VERSION */
34 #endif /* HAVE_UNISTD_H */
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
45 /* System-specific feature definitions and include files. */
49 #if defined (TIOCSTAT_IN_SYS_IOCTL)
50 # include <sys/ioctl.h>
51 #endif /* TIOCSTAT_IN_SYS_IOCTL */
53 /* Some standard library routines. */
56 #include "rlprivate.h"
59 /* Declared here so it can be shared between the readline and history
61 #if defined (HANDLE_MULTIBYTE)
62 int rl_byte_oriented = 0;
64 int rl_byte_oriented = 1;
67 /* **************************************************************** */
69 /* Multibyte Character Utility Functions */
71 /* **************************************************************** */
73 #if defined(HANDLE_MULTIBYTE)
76 _rl_find_next_mbchar_internal (string, seed, count, find_non_zero)
78 int seed, count, find_non_zero;
85 memset(&ps, 0, sizeof (mbstate_t));
91 point = seed + _rl_adjust_point(string, seed, &ps);
92 /* if this is true, means that seed was not pointed character
93 started byte. So correct the point and consume count */
99 tmp = mbrtowc (&wc, string+point, strlen(string + point), &ps);
100 if ((size_t)(tmp) == (size_t)-1 || (size_t)(tmp) == (size_t)-2)
102 /* invalid bytes. asume a byte represents a character */
106 memset(&ps, 0, sizeof(mbstate_t));
108 else if (tmp == (size_t)0)
109 /* found '\0' char */
117 if (wcwidth (wc) == 0)
129 tmp = mbrtowc (&wc, string + point, strlen (string + point), &ps);
130 while (wcwidth (wc) == 0)
133 tmp = mbrtowc (&wc, string + point, strlen (string + point), &ps);
134 if (tmp == (size_t)(0) || tmp == (size_t)(-1) || tmp == (size_t)(-2))
142 _rl_find_prev_mbchar_internal (string, seed, find_non_zero)
144 int seed, find_non_zero;
147 int prev, non_zero_prev, point, length;
151 memset(&ps, 0, sizeof(mbstate_t));
152 length = strlen(string);
156 else if (length < seed)
159 prev = non_zero_prev = point = 0;
162 tmp = mbrtowc (&wc, string + point, length - point, &ps);
163 if ((size_t)(tmp) == (size_t)-1 || (size_t)(tmp) == (size_t)-2)
165 /* in this case, bytes are invalid or shorted to compose
166 multibyte char, so assume that the first byte represents
167 a single character anyway. */
169 /* clear the state of the byte sequence, because
170 in this case effect of mbstate is undefined */
171 memset(&ps, 0, sizeof (mbstate_t));
174 break; /* Found '\0' char. Can this happen? */
179 if (wcwidth (wc) != 0)
192 /* return the number of bytes parsed from the multibyte sequence starting
193 at src, if a non-L'\0' wide character was recognized. It returns 0,
194 if a L'\0' wide character was recognized. It returns (size_t)(-1),
195 if an invalid multibyte sequence was encountered. It returns (size_t)(-2)
196 if it couldn't parse a complete multibyte character. */
198 _rl_get_char_len (src, ps)
204 tmp = mbrlen((const char *)src, (size_t)strlen (src), ps);
205 if (tmp == (size_t)(-2))
207 /* shorted to compose multibyte char */
209 memset (ps, 0, sizeof(mbstate_t));
212 else if (tmp == (size_t)(-1))
214 /* invalid to compose multibyte char */
215 /* initialize the conversion state */
217 memset (ps, 0, sizeof(mbstate_t));
220 else if (tmp == (size_t)0)
226 /* compare the specified two characters. If the characters matched,
227 return 1. Otherwise return 0. */
229 _rl_compare_chars (buf1, pos1, ps1, buf2, pos2, ps2)
239 if ((w1 = _rl_get_char_len (&buf1[pos1], ps1)) <= 0 ||
240 (w2 = _rl_get_char_len (&buf2[pos2], ps2)) <= 0 ||
242 (buf1[pos1] != buf2[pos2]))
245 for (i = 1; i < w1; i++)
246 if (buf1[pos1+i] != buf2[pos2+i])
252 /* adjust pointed byte and find mbstate of the point of string.
253 adjusted point will be point <= adjusted_point, and returns
254 differences of the byte(adjusted_point - point).
255 if point is invalied (point < 0 || more than string length),
258 _rl_adjust_point(string, point, ps)
267 length = strlen(string);
275 tmp = mbrlen (string + pos, length - pos, ps);
276 if((size_t)(tmp) == (size_t)-1 || (size_t)(tmp) == (size_t)-2)
278 /* in this case, bytes are invalid or shorted to compose
279 multibyte char, so assume that the first byte represents
280 a single character anyway. */
282 /* clear the state of the byte sequence, because
283 in this case effect of mbstate is undefined */
285 memset (ps, 0, sizeof (mbstate_t));
293 return (pos - point);
297 _rl_is_mbchar_matched (string, seed, end, mbchar, length)
305 if ((end - seed) < length)
308 for (i = 0; i < length; i++)
309 if (string[seed + i] != mbchar[i])
313 #endif /* HANDLE_MULTIBYTE */
315 /* Find next `count' characters started byte point of the specified seed.
316 If flags is MB_FIND_NONZERO, we look for non-zero-width multibyte
318 #undef _rl_find_next_mbchar
320 _rl_find_next_mbchar (string, seed, count, flags)
322 int seed, count, flags;
324 #if defined (HANDLE_MULTIBYTE)
325 return _rl_find_next_mbchar_internal (string, seed, count, flags);
327 return (seed + count);
331 /* Find previous character started byte point of the specified seed.
332 Returned point will be point <= seed. If flags is MB_FIND_NONZERO,
333 we look for non-zero-width multibyte characters. */
334 #undef _rl_find_prev_mbchar
336 _rl_find_prev_mbchar (string, seed, flags)
340 #if defined (HANDLE_MULTIBYTE)
341 return _rl_find_prev_mbchar_internal (string, seed, flags);
343 return ((seed == 0) ? seed : seed - 1);