mbutil.c 8.8 KB

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