1 /* mbutil.c -- readline multibyte character utility functions */
3 /* Copyright (C) 2001-2017 Free Software Foundation, Inc.
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.
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.
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.
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/>.
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;
68 int _rl_utf8locale
= 0;
70 /* **************************************************************** */
72 /* Multibyte Character Utility Functions */
74 /* **************************************************************** */
76 #if defined(HANDLE_MULTIBYTE)
78 /* **************************************************************** */
80 /* UTF-8 specific Character Utility Functions */
82 /* **************************************************************** */
84 /* Return the length in bytes of the possibly-multibyte character beginning
85 at S. Encoding is UTF-8. */
87 _rl_utf8_mblen (const char *s
, size_t n
)
92 return (0); /* no shift states */
96 c
= (unsigned char)*s
;
101 c1
= (unsigned char)s
[1];
104 if (n
>= 2 && (s
[1] ^ 0x80) < 0x40)
110 && (s
[1] ^ 0x80) < 0x40 && (s
[2] ^ 0x80) < 0x40
111 && (c
>= 0xe1 || c1
>= 0xa0)
112 && (c
!= 0xed || c1
< 0xa0))
118 && (s
[1] ^ 0x80) < 0x40 && (s
[2] ^ 0x80) < 0x40
119 && (s
[3] ^ 0x80) < 0x40
120 && (c
>= 0xf1 || c1
>= 0x90)
121 && (c
< 0xf4 || (c
== 0xf4 && c1
< 0x90)))
125 /* invalid or incomplete multibyte character */
130 _rl_find_next_mbchar_internal (char *string
, int seed
, int count
, int find_non_zero
)
139 memset(&ps
, 0, sizeof (mbstate_t));
145 point
= seed
+ _rl_adjust_point (string
, seed
, &ps
);
146 /* if _rl_adjust_point returns -1, the character or string is invalid.
148 if (point
== seed
- 1) /* invalid */
151 /* if this is true, means that seed was not pointing to a byte indicating
152 the beginning of a multibyte character. Correct the point and consume
159 len
= strlen (string
+ point
);
162 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(string
[point
]))
165 wc
= (wchar_t) string
[point
];
166 memset(&ps
, 0, sizeof(mbstate_t));
169 tmp
= mbrtowc (&wc
, string
+point
, len
, &ps
);
170 if (MB_INVALIDCH ((size_t)tmp
))
172 /* invalid bytes. assume a byte represents a character */
176 memset(&ps
, 0, sizeof(mbstate_t));
178 else if (MB_NULLWCH (tmp
))
179 break; /* found wide '\0' */
186 if (WCWIDTH (wc
) == 0)
198 tmp
= mbrtowc (&wc
, string
+ point
, strlen (string
+ point
), &ps
);
199 while (MB_NULLWCH (tmp
) == 0 && MB_INVALIDCH (tmp
) == 0 && WCWIDTH (wc
) == 0)
202 tmp
= mbrtowc (&wc
, string
+ point
, strlen (string
+ point
), &ps
);
210 _rl_find_prev_mbchar_internal (char *string
, int seed
, int find_non_zero
)
213 int prev
, non_zero_prev
, point
, length
;
217 memset(&ps
, 0, sizeof(mbstate_t));
218 length
= strlen(string
);
222 else if (length
< seed
)
225 prev
= non_zero_prev
= point
= 0;
228 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(string
[point
]))
231 wc
= (wchar_t) string
[point
];
232 memset(&ps
, 0, sizeof(mbstate_t));
235 tmp
= mbrtowc (&wc
, string
+ point
, length
- point
, &ps
);
236 if (MB_INVALIDCH ((size_t)tmp
))
238 /* in this case, bytes are invalid or too short to compose
239 multibyte char, so assume that the first byte represents
240 a single character anyway. */
242 /* clear the state of the byte sequence, because
243 in this case effect of mbstate is undefined */
244 memset(&ps
, 0, sizeof (mbstate_t));
246 /* Since we're assuming that this byte represents a single
247 non-zero-width character, don't forget about it. */
250 else if (MB_NULLWCH (tmp
))
251 break; /* Found '\0' char. Can this happen? */
256 if (WCWIDTH (wc
) != 0)
269 /* return the number of bytes parsed from the multibyte sequence starting
270 at src, if a non-L'\0' wide character was recognized. It returns 0,
271 if a L'\0' wide character was recognized. It returns (size_t)(-1),
272 if an invalid multibyte sequence was encountered. It returns (size_t)(-2)
273 if it couldn't parse a complete multibyte character. */
275 _rl_get_char_len (char *src
, mbstate_t *ps
)
280 /* Look at no more than MB_CUR_MAX characters */
281 l
= (size_t)strlen (src
);
282 if (_rl_utf8locale
&& l
> 0 && UTF8_SINGLEBYTE(*src
))
283 tmp
= (*src
!= 0) ? 1 : 0;
286 mb_cur_max
= MB_CUR_MAX
;
287 tmp
= mbrlen((const char *)src
, (l
< mb_cur_max
) ? l
: mb_cur_max
, ps
);
289 if (tmp
== (size_t)(-2))
291 /* too short to compose multibyte char */
293 memset (ps
, 0, sizeof(mbstate_t));
296 else if (tmp
== (size_t)(-1))
298 /* invalid to compose multibyte char */
299 /* initialize the conversion state */
301 memset (ps
, 0, sizeof(mbstate_t));
304 else if (tmp
== (size_t)0)
310 /* compare the specified two characters. If the characters matched,
311 return 1. Otherwise return 0. */
313 _rl_compare_chars (char *buf1
, int pos1
, mbstate_t *ps1
, char *buf2
, int pos2
, mbstate_t *ps2
)
317 if ((w1
= _rl_get_char_len (&buf1
[pos1
], ps1
)) <= 0 ||
318 (w2
= _rl_get_char_len (&buf2
[pos2
], ps2
)) <= 0 ||
320 (buf1
[pos1
] != buf2
[pos2
]))
323 for (i
= 1; i
< w1
; i
++)
324 if (buf1
[pos1
+i
] != buf2
[pos2
+i
])
330 /* adjust pointed byte and find mbstate of the point of string.
331 adjusted point will be point <= adjusted_point, and returns
332 differences of the byte(adjusted_point - point).
333 if point is invalid (point < 0 || more than string length),
336 _rl_adjust_point (char *string
, int point
, mbstate_t *ps
)
343 length
= strlen(string
);
351 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(string
[pos
]))
354 tmp
= mbrlen (string
+ pos
, length
- pos
, ps
);
355 if (MB_INVALIDCH ((size_t)tmp
))
357 /* in this case, bytes are invalid or too short to compose
358 multibyte char, so assume that the first byte represents
359 a single character anyway. */
361 /* clear the state of the byte sequence, because
362 in this case effect of mbstate is undefined */
364 memset (ps
, 0, sizeof (mbstate_t));
366 else if (MB_NULLWCH (tmp
))
372 return (pos
- point
);
376 _rl_is_mbchar_matched (char *string
, int seed
, int end
, char *mbchar
, int length
)
380 if ((end
- seed
) < length
)
383 for (i
= 0; i
< length
; i
++)
384 if (string
[seed
+ i
] != mbchar
[i
])
390 _rl_char_value (char *buf
, int ind
)
397 if (MB_LEN_MAX
== 1 || rl_byte_oriented
)
398 return ((wchar_t) buf
[ind
]);
399 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(buf
[ind
]))
400 return ((wchar_t) buf
[ind
]);
403 return ((wchar_t) buf
[ind
]);
404 if (l
< ind
) /* Sanity check */
405 l
= strlen (buf
+ind
);
406 memset (&ps
, 0, sizeof (mbstate_t));
407 tmp
= mbrtowc (&wc
, buf
+ ind
, l
- ind
, &ps
);
408 if (MB_INVALIDCH (tmp
) || MB_NULLWCH (tmp
))
409 return ((wchar_t) buf
[ind
]);
412 #endif /* HANDLE_MULTIBYTE */
414 /* Find next `count' characters started byte point of the specified seed.
415 If flags is MB_FIND_NONZERO, we look for non-zero-width multibyte
417 #undef _rl_find_next_mbchar
419 _rl_find_next_mbchar (char *string
, int seed
, int count
, int flags
)
421 #if defined (HANDLE_MULTIBYTE)
422 return _rl_find_next_mbchar_internal (string
, seed
, count
, flags
);
424 return (seed
+ count
);
428 /* Find previous character started byte point of the specified seed.
429 Returned point will be point <= seed. If flags is MB_FIND_NONZERO,
430 we look for non-zero-width multibyte characters. */
431 #undef _rl_find_prev_mbchar
433 _rl_find_prev_mbchar (char *string
, int seed
, int flags
)
435 #if defined (HANDLE_MULTIBYTE)
436 return _rl_find_prev_mbchar_internal (string
, seed
, flags
);
438 return ((seed
== 0) ? seed
: seed
- 1);
This page took 0.0528729999999999 seconds and 4 git commands to generate.