]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/mbutil.c
dc62b4cc24dd55df745c1e623a24f3ca94f21059
1 /* mbutil.c -- readline multibyte character utility functions */
3 /* Copyright (C) 2001-2020 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
)
89 unsigned char c
, c1
, c2
, c3
;
92 return (0); /* no shift states */
96 c
= (unsigned char)*s
;
101 c1
= (unsigned char)s
[1];
106 if (n
>= 2 && (c1
^ 0x80) < 0x40)
113 if ((c1
^ 0x80) < 0x40
114 && (c
>= 0xe1 || c1
>= 0xa0)
115 && (c
!= 0xed || c1
< 0xa0))
119 c2
= (unsigned char)s
[2];
120 if ((c2
^ 0x80) < 0x40)
128 if (((c1
^ 0x80) < 0x40)
129 && (c
>= 0xf1 || c1
>= 0x90)
130 && (c
< 0xf4 || (c
== 0xf4 && c1
< 0x90)))
134 c2
= (unsigned char)s
[2];
135 if ((c2
^ 0x80) < 0x40)
139 c3
= (unsigned char)s
[3];
140 if ((c3
^ 0x80) < 0x40)
146 /* invalid or incomplete multibyte character */
151 _rl_find_next_mbchar_internal (char *string
, int seed
, int count
, int find_non_zero
)
160 memset(&ps
, 0, sizeof (mbstate_t));
166 point
= seed
+ _rl_adjust_point (string
, seed
, &ps
);
167 /* if _rl_adjust_point returns -1, the character or string is invalid.
169 if (point
== seed
- 1) /* invalid */
172 /* if this is true, means that seed was not pointing to a byte indicating
173 the beginning of a multibyte character. Correct the point and consume
180 len
= strlen (string
+ point
);
183 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(string
[point
]))
186 wc
= (wchar_t) string
[point
];
187 memset(&ps
, 0, sizeof(mbstate_t));
190 tmp
= mbrtowc (&wc
, string
+point
, len
, &ps
);
191 if (MB_INVALIDCH ((size_t)tmp
))
193 /* invalid bytes. assume a byte represents a character */
197 memset(&ps
, 0, sizeof(mbstate_t));
199 else if (MB_NULLWCH (tmp
))
200 break; /* found wide '\0' */
207 if (WCWIDTH (wc
) == 0)
219 tmp
= mbrtowc (&wc
, string
+ point
, strlen (string
+ point
), &ps
);
220 while (MB_NULLWCH (tmp
) == 0 && MB_INVALIDCH (tmp
) == 0 && WCWIDTH (wc
) == 0)
223 tmp
= mbrtowc (&wc
, string
+ point
, strlen (string
+ point
), &ps
);
231 _rl_test_nonzero (char *string
, int ind
, int len
)
237 memset (&ps
, 0, sizeof (mbstate_t));
238 tmp
= mbrtowc (&wc
, string
+ ind
, len
- ind
, &ps
);
239 /* treat invalid multibyte sequences as non-zero-width */
240 return (MB_INVALIDCH (tmp
) || MB_NULLWCH (tmp
) || WCWIDTH (wc
) > 0);
243 /* experimental -- needs to handle zero-width characters better */
245 _rl_find_prev_utf8char (char *string
, int seed
, int find_non_zero
)
253 len
= RL_STRLEN (string
);
258 b
= (unsigned char)string
[prev
];
259 if (UTF8_SINGLEBYTE (b
))
264 /* Move back until we're not in the middle of a multibyte char */
267 while (prev
> 0 && (b
= (unsigned char)string
[--prev
]) && UTF8_MBCHAR (b
))
271 if (UTF8_MBFIRSTCHAR (b
))
275 if (_rl_test_nonzero (string
, prev
, len
))
277 else /* valid but WCWIDTH (wc) == 0 */
284 return (save
); /* invalid utf-8 multibyte sequence */
287 return ((prev
< 0) ? 0 : prev
);
291 _rl_find_prev_mbchar_internal (char *string
, int seed
, int find_non_zero
)
294 int prev
, non_zero_prev
, point
, length
;
299 return (_rl_find_prev_utf8char (string
, seed
, find_non_zero
));
301 memset(&ps
, 0, sizeof(mbstate_t));
302 length
= strlen(string
);
306 else if (length
< seed
)
309 prev
= non_zero_prev
= point
= 0;
312 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(string
[point
]))
315 wc
= (wchar_t) string
[point
];
316 memset(&ps
, 0, sizeof(mbstate_t));
319 tmp
= mbrtowc (&wc
, string
+ point
, length
- point
, &ps
);
320 if (MB_INVALIDCH ((size_t)tmp
))
322 /* in this case, bytes are invalid or too short to compose
323 multibyte char, so assume that the first byte represents
324 a single character anyway. */
326 /* clear the state of the byte sequence, because
327 in this case effect of mbstate is undefined */
328 memset(&ps
, 0, sizeof (mbstate_t));
330 /* Since we're assuming that this byte represents a single
331 non-zero-width character, don't forget about it. */
334 else if (MB_NULLWCH (tmp
))
335 break; /* Found '\0' char. Can this happen? */
340 if (WCWIDTH (wc
) != 0)
353 /* return the number of bytes parsed from the multibyte sequence starting
354 at src, if a non-L'\0' wide character was recognized. It returns 0,
355 if a L'\0' wide character was recognized. It returns (size_t)(-1),
356 if an invalid multibyte sequence was encountered. It returns (size_t)(-2)
357 if it couldn't parse a complete multibyte character. */
359 _rl_get_char_len (char *src
, mbstate_t *ps
)
364 /* Look at no more than MB_CUR_MAX characters */
365 l
= (size_t)strlen (src
);
366 if (_rl_utf8locale
&& l
> 0 && UTF8_SINGLEBYTE(*src
))
367 tmp
= (*src
!= 0) ? 1 : 0;
370 mb_cur_max
= MB_CUR_MAX
;
371 tmp
= mbrlen((const char *)src
, (l
< mb_cur_max
) ? l
: mb_cur_max
, ps
);
373 if (tmp
== (size_t)(-2))
375 /* too short to compose multibyte char */
377 memset (ps
, 0, sizeof(mbstate_t));
380 else if (tmp
== (size_t)(-1))
382 /* invalid to compose multibyte char */
383 /* initialize the conversion state */
385 memset (ps
, 0, sizeof(mbstate_t));
388 else if (tmp
== (size_t)0)
394 /* compare the specified two characters. If the characters matched,
395 return 1. Otherwise return 0. */
397 _rl_compare_chars (char *buf1
, int pos1
, mbstate_t *ps1
, char *buf2
, int pos2
, mbstate_t *ps2
)
401 if ((w1
= _rl_get_char_len (&buf1
[pos1
], ps1
)) <= 0 ||
402 (w2
= _rl_get_char_len (&buf2
[pos2
], ps2
)) <= 0 ||
404 (buf1
[pos1
] != buf2
[pos2
]))
407 for (i
= 1; i
< w1
; i
++)
408 if (buf1
[pos1
+i
] != buf2
[pos2
+i
])
414 /* adjust pointed byte and find mbstate of the point of string.
415 adjusted point will be point <= adjusted_point, and returns
416 differences of the byte(adjusted_point - point).
417 if point is invalid (point < 0 || more than string length),
420 _rl_adjust_point (char *string
, int point
, mbstate_t *ps
)
427 length
= strlen(string
);
435 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(string
[pos
]))
438 tmp
= mbrlen (string
+ pos
, length
- pos
, ps
);
439 if (MB_INVALIDCH ((size_t)tmp
))
441 /* in this case, bytes are invalid or too short to compose
442 multibyte char, so assume that the first byte represents
443 a single character anyway. */
445 /* clear the state of the byte sequence, because
446 in this case effect of mbstate is undefined */
448 memset (ps
, 0, sizeof (mbstate_t));
450 else if (MB_NULLWCH (tmp
))
456 return (pos
- point
);
460 _rl_is_mbchar_matched (char *string
, int seed
, int end
, char *mbchar
, int length
)
464 if ((end
- seed
) < length
)
467 for (i
= 0; i
< length
; i
++)
468 if (string
[seed
+ i
] != mbchar
[i
])
474 _rl_char_value (char *buf
, int ind
)
481 if (MB_LEN_MAX
== 1 || rl_byte_oriented
)
482 return ((wchar_t) buf
[ind
]);
483 if (_rl_utf8locale
&& UTF8_SINGLEBYTE(buf
[ind
]))
484 return ((wchar_t) buf
[ind
]);
487 return ((wchar_t) buf
[ind
]);
488 if (l
< ind
) /* Sanity check */
489 l
= strlen (buf
+ind
);
490 memset (&ps
, 0, sizeof (mbstate_t));
491 tmp
= mbrtowc (&wc
, buf
+ ind
, l
- ind
, &ps
);
492 if (MB_INVALIDCH (tmp
) || MB_NULLWCH (tmp
))
493 return ((wchar_t) buf
[ind
]);
496 #endif /* HANDLE_MULTIBYTE */
498 /* Find next `count' characters started byte point of the specified seed.
499 If flags is MB_FIND_NONZERO, we look for non-zero-width multibyte
501 #undef _rl_find_next_mbchar
503 _rl_find_next_mbchar (char *string
, int seed
, int count
, int flags
)
505 #if defined (HANDLE_MULTIBYTE)
506 return _rl_find_next_mbchar_internal (string
, seed
, count
, flags
);
508 return (seed
+ count
);
512 /* Find previous character started byte point of the specified seed.
513 Returned point will be point <= seed. If flags is MB_FIND_NONZERO,
514 we look for non-zero-width multibyte characters. */
515 #undef _rl_find_prev_mbchar
517 _rl_find_prev_mbchar (char *string
, int seed
, int flags
)
519 #if defined (HANDLE_MULTIBYTE)
520 return _rl_find_prev_mbchar_internal (string
, seed
, flags
);
522 return ((seed
== 0) ? seed
: seed
- 1);