rlmbutil.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* rlmbutil.h -- utility functions for multibyte characters. */
  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. #if !defined (_RL_MBUTIL_H_)
  17. #define _RL_MBUTIL_H_
  18. #include "rlstdc.h"
  19. /************************************************/
  20. /* check multibyte capability for I18N code */
  21. /************************************************/
  22. /* For platforms which support the ISO C amendement 1 functionality we
  23. support user defined character classes. */
  24. /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
  25. #if defined (HAVE_WCTYPE_H) && defined (HAVE_WCHAR_H) && defined (HAVE_LOCALE_H)
  26. # include <wchar.h>
  27. # include <wctype.h>
  28. # if defined (HAVE_ISWCTYPE) && \
  29. defined (HAVE_ISWLOWER) && \
  30. defined (HAVE_ISWUPPER) && \
  31. defined (HAVE_MBSRTOWCS) && \
  32. defined (HAVE_MBRTOWC) && \
  33. defined (HAVE_MBRLEN) && \
  34. defined (HAVE_TOWLOWER) && \
  35. defined (HAVE_TOWUPPER) && \
  36. defined (HAVE_WCHAR_T) && \
  37. defined (HAVE_WCWIDTH)
  38. /* system is supposed to support XPG5 */
  39. # define HANDLE_MULTIBYTE 1
  40. # endif
  41. #endif
  42. /* If we don't want multibyte chars even on a system that supports them, let
  43. the configuring user turn multibyte support off. */
  44. #if defined (NO_MULTIBYTE_SUPPORT)
  45. # undef HANDLE_MULTIBYTE
  46. #endif
  47. /* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
  48. #if HANDLE_MULTIBYTE && !defined (HAVE_MBSTATE_T)
  49. # define wcsrtombs(dest, src, len, ps) (wcsrtombs) (dest, src, len, 0)
  50. # define mbsrtowcs(dest, src, len, ps) (mbsrtowcs) (dest, src, len, 0)
  51. # define wcrtomb(s, wc, ps) (wcrtomb) (s, wc, 0)
  52. # define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
  53. # define mbrlen(s, n, ps) (mbrlen) (s, n, 0)
  54. # define mbstate_t int
  55. #endif
  56. /* Make sure MB_LEN_MAX is at least 16 on systems that claim to be able to
  57. handle multibyte chars (some systems define MB_LEN_MAX as 1) */
  58. #ifdef HANDLE_MULTIBYTE
  59. # include <limits.h>
  60. # if defined(MB_LEN_MAX) && (MB_LEN_MAX < 16)
  61. # undef MB_LEN_MAX
  62. # endif
  63. # if !defined (MB_LEN_MAX)
  64. # define MB_LEN_MAX 16
  65. # endif
  66. #endif
  67. /************************************************/
  68. /* end of multibyte capability checks for I18N */
  69. /************************************************/
  70. /*
  71. * Flags for _rl_find_prev_mbchar and _rl_find_next_mbchar:
  72. *
  73. * MB_FIND_ANY find any multibyte character
  74. * MB_FIND_NONZERO find a non-zero-width multibyte character
  75. */
  76. #define MB_FIND_ANY 0x00
  77. #define MB_FIND_NONZERO 0x01
  78. extern int _rl_find_prev_mbchar PARAMS((char *, int, int));
  79. extern int _rl_find_next_mbchar PARAMS((char *, int, int, int));
  80. #ifdef HANDLE_MULTIBYTE
  81. extern int _rl_compare_chars PARAMS((char *, int, mbstate_t *, char *, int, mbstate_t *));
  82. extern int _rl_get_char_len PARAMS((char *, mbstate_t *));
  83. extern int _rl_adjust_point PARAMS((char *, int, mbstate_t *));
  84. extern int _rl_read_mbchar PARAMS((char *, int));
  85. extern int _rl_read_mbstring PARAMS((int, char *, int));
  86. extern int _rl_is_mbchar_matched PARAMS((char *, int, int, char *, int));
  87. extern wchar_t _rl_char_value PARAMS((char *, int));
  88. extern int _rl_walphabetic PARAMS((wchar_t));
  89. #define _rl_to_wupper(wc) (iswlower (wc) ? towupper (wc) : (wc))
  90. #define _rl_to_wlower(wc) (iswupper (wc) ? towlower (wc) : (wc))
  91. #define MB_NEXTCHAR(b,s,c,f) \
  92. ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) \
  93. ? _rl_find_next_mbchar ((b), (s), (c), (f)) \
  94. : ((s) + (c)))
  95. #define MB_PREVCHAR(b,s,f) \
  96. ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) \
  97. ? _rl_find_prev_mbchar ((b), (s), (f)) \
  98. : ((s) - 1))
  99. #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
  100. #define MB_NULLWCH(x) ((x) == 0)
  101. #else /* !HANDLE_MULTIBYTE */
  102. #undef MB_LEN_MAX
  103. #undef MB_CUR_MAX
  104. #define MB_LEN_MAX 1
  105. #define MB_CUR_MAX 1
  106. #define _rl_find_prev_mbchar(b, i, f) (((i) == 0) ? (i) : ((i) - 1))
  107. #define _rl_find_next_mbchar(b, i1, i2, f) ((i1) + (i2))
  108. #define _rl_char_value(buf,ind) ((buf)[(ind)])
  109. #define _rl_walphabetic(c) (rl_alphabetic (c))
  110. #define _rl_to_wupper(c) (_rl_to_upper (c))
  111. #define _rl_to_wlower(c) (_rl_to_lower (c))
  112. #define MB_NEXTCHAR(b,s,c,f) ((s) + (c))
  113. #define MB_PREVCHAR(b,s,f) ((s) - 1)
  114. #define MB_INVALIDCH(x) (0)
  115. #define MB_NULLWCH(x) (0)
  116. #endif /* !HANDLE_MULTIBYTE */
  117. extern int rl_byte_oriented;
  118. #endif /* _RL_MBUTIL_H_ */