xref: /dflybsd-src/contrib/gdb-7/readline/rlmbutil.h (revision 16003dcfd2baa152f5dd24794ec9f36e139eaeb8)
1*6b445a62SJohn Marino /* rlmbutil.h -- utility functions for multibyte characters. */
2*6b445a62SJohn Marino 
3*6b445a62SJohn Marino /* Copyright (C) 2001-2009 Free Software Foundation, Inc.
4*6b445a62SJohn Marino 
5*6b445a62SJohn Marino    This file is part of the GNU Readline Library (Readline), a library
6*6b445a62SJohn Marino    for reading lines of text with interactive input and history editing.
7*6b445a62SJohn Marino 
8*6b445a62SJohn Marino    Readline is free software: you can redistribute it and/or modify
9*6b445a62SJohn Marino    it under the terms of the GNU General Public License as published by
10*6b445a62SJohn Marino    the Free Software Foundation, either version 3 of the License, or
11*6b445a62SJohn Marino    (at your option) any later version.
12*6b445a62SJohn Marino 
13*6b445a62SJohn Marino    Readline is distributed in the hope that it will be useful,
14*6b445a62SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*6b445a62SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*6b445a62SJohn Marino    GNU General Public License for more details.
17*6b445a62SJohn Marino 
18*6b445a62SJohn Marino    You should have received a copy of the GNU General Public License
19*6b445a62SJohn Marino    along with Readline.  If not, see <http://www.gnu.org/licenses/>.
20*6b445a62SJohn Marino */
21*6b445a62SJohn Marino 
22*6b445a62SJohn Marino #if !defined (_RL_MBUTIL_H_)
23*6b445a62SJohn Marino #define _RL_MBUTIL_H_
24*6b445a62SJohn Marino 
25*6b445a62SJohn Marino #include "rlstdc.h"
26*6b445a62SJohn Marino 
27*6b445a62SJohn Marino /************************************************/
28*6b445a62SJohn Marino /* check multibyte capability for I18N code     */
29*6b445a62SJohn Marino /************************************************/
30*6b445a62SJohn Marino 
31*6b445a62SJohn Marino /* For platforms which support the ISO C amendement 1 functionality we
32*6b445a62SJohn Marino    support user defined character classes.  */
33*6b445a62SJohn Marino    /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
34*6b445a62SJohn Marino #if defined (HAVE_WCTYPE_H) && defined (HAVE_WCHAR_H) && defined (HAVE_LOCALE_H)
35*6b445a62SJohn Marino #  include <wchar.h>
36*6b445a62SJohn Marino #  include <wctype.h>
37*6b445a62SJohn Marino #  if defined (HAVE_ISWCTYPE) && \
38*6b445a62SJohn Marino       defined (HAVE_ISWLOWER) && \
39*6b445a62SJohn Marino       defined (HAVE_ISWUPPER) && \
40*6b445a62SJohn Marino       defined (HAVE_MBSRTOWCS) && \
41*6b445a62SJohn Marino       defined (HAVE_MBRTOWC) && \
42*6b445a62SJohn Marino       defined (HAVE_MBRLEN) && \
43*6b445a62SJohn Marino       defined (HAVE_TOWLOWER) && \
44*6b445a62SJohn Marino       defined (HAVE_TOWUPPER) && \
45*6b445a62SJohn Marino       defined (HAVE_WCHAR_T) && \
46*6b445a62SJohn Marino       defined (HAVE_WCWIDTH)
47*6b445a62SJohn Marino      /* system is supposed to support XPG5 */
48*6b445a62SJohn Marino #    define HANDLE_MULTIBYTE      1
49*6b445a62SJohn Marino #  endif
50*6b445a62SJohn Marino #endif
51*6b445a62SJohn Marino 
52*6b445a62SJohn Marino /* If we don't want multibyte chars even on a system that supports them, let
53*6b445a62SJohn Marino    the configuring user turn multibyte support off. */
54*6b445a62SJohn Marino #if defined (NO_MULTIBYTE_SUPPORT)
55*6b445a62SJohn Marino #  undef HANDLE_MULTIBYTE
56*6b445a62SJohn Marino #endif
57*6b445a62SJohn Marino 
58*6b445a62SJohn Marino /* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
59*6b445a62SJohn Marino #if HANDLE_MULTIBYTE && !defined (HAVE_MBSTATE_T)
60*6b445a62SJohn Marino #  define wcsrtombs(dest, src, len, ps) (wcsrtombs) (dest, src, len, 0)
61*6b445a62SJohn Marino #  define mbsrtowcs(dest, src, len, ps) (mbsrtowcs) (dest, src, len, 0)
62*6b445a62SJohn Marino #  define wcrtomb(s, wc, ps) (wcrtomb) (s, wc, 0)
63*6b445a62SJohn Marino #  define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
64*6b445a62SJohn Marino #  define mbrlen(s, n, ps) (mbrlen) (s, n, 0)
65*6b445a62SJohn Marino #  define mbstate_t int
66*6b445a62SJohn Marino #endif
67*6b445a62SJohn Marino 
68*6b445a62SJohn Marino /* Make sure MB_LEN_MAX is at least 16 on systems that claim to be able to
69*6b445a62SJohn Marino    handle multibyte chars (some systems define MB_LEN_MAX as 1) */
70*6b445a62SJohn Marino #ifdef HANDLE_MULTIBYTE
71*6b445a62SJohn Marino #  include <limits.h>
72*6b445a62SJohn Marino #  if defined(MB_LEN_MAX) && (MB_LEN_MAX < 16)
73*6b445a62SJohn Marino #    undef MB_LEN_MAX
74*6b445a62SJohn Marino #  endif
75*6b445a62SJohn Marino #  if !defined (MB_LEN_MAX)
76*6b445a62SJohn Marino #    define MB_LEN_MAX 16
77*6b445a62SJohn Marino #  endif
78*6b445a62SJohn Marino #endif
79*6b445a62SJohn Marino 
80*6b445a62SJohn Marino /************************************************/
81*6b445a62SJohn Marino /* end of multibyte capability checks for I18N  */
82*6b445a62SJohn Marino /************************************************/
83*6b445a62SJohn Marino 
84*6b445a62SJohn Marino /*
85*6b445a62SJohn Marino  * Flags for _rl_find_prev_mbchar and _rl_find_next_mbchar:
86*6b445a62SJohn Marino  *
87*6b445a62SJohn Marino  * MB_FIND_ANY		find any multibyte character
88*6b445a62SJohn Marino  * MB_FIND_NONZERO	find a non-zero-width multibyte character
89*6b445a62SJohn Marino  */
90*6b445a62SJohn Marino 
91*6b445a62SJohn Marino #define MB_FIND_ANY	0x00
92*6b445a62SJohn Marino #define MB_FIND_NONZERO	0x01
93*6b445a62SJohn Marino 
94*6b445a62SJohn Marino extern int _rl_find_prev_mbchar PARAMS((char *, int, int));
95*6b445a62SJohn Marino extern int _rl_find_next_mbchar PARAMS((char *, int, int, int));
96*6b445a62SJohn Marino 
97*6b445a62SJohn Marino #ifdef HANDLE_MULTIBYTE
98*6b445a62SJohn Marino 
99*6b445a62SJohn Marino extern int _rl_compare_chars PARAMS((char *, int, mbstate_t *, char *, int, mbstate_t *));
100*6b445a62SJohn Marino extern int _rl_get_char_len PARAMS((char *, mbstate_t *));
101*6b445a62SJohn Marino extern int _rl_adjust_point PARAMS((char *, int, mbstate_t *));
102*6b445a62SJohn Marino 
103*6b445a62SJohn Marino extern int _rl_read_mbchar PARAMS((char *, int));
104*6b445a62SJohn Marino extern int _rl_read_mbstring PARAMS((int, char *, int));
105*6b445a62SJohn Marino 
106*6b445a62SJohn Marino extern int _rl_is_mbchar_matched PARAMS((char *, int, int, char *, int));
107*6b445a62SJohn Marino 
108*6b445a62SJohn Marino extern wchar_t _rl_char_value PARAMS((char *, int));
109*6b445a62SJohn Marino extern int _rl_walphabetic PARAMS((wchar_t));
110*6b445a62SJohn Marino 
111*6b445a62SJohn Marino #define _rl_to_wupper(wc)	(iswlower (wc) ? towupper (wc) : (wc))
112*6b445a62SJohn Marino #define _rl_to_wlower(wc)	(iswupper (wc) ? towlower (wc) : (wc))
113*6b445a62SJohn Marino 
114*6b445a62SJohn Marino #define MB_NEXTCHAR(b,s,c,f) \
115*6b445a62SJohn Marino 	((MB_CUR_MAX > 1 && rl_byte_oriented == 0) \
116*6b445a62SJohn Marino 		? _rl_find_next_mbchar ((b), (s), (c), (f)) \
117*6b445a62SJohn Marino 		: ((s) + (c)))
118*6b445a62SJohn Marino #define MB_PREVCHAR(b,s,f) \
119*6b445a62SJohn Marino 	((MB_CUR_MAX > 1 && rl_byte_oriented == 0) \
120*6b445a62SJohn Marino 		? _rl_find_prev_mbchar ((b), (s), (f)) \
121*6b445a62SJohn Marino 		: ((s) - 1))
122*6b445a62SJohn Marino 
123*6b445a62SJohn Marino #define MB_INVALIDCH(x)		((x) == (size_t)-1 || (x) == (size_t)-2)
124*6b445a62SJohn Marino #define MB_NULLWCH(x)		((x) == 0)
125*6b445a62SJohn Marino 
126*6b445a62SJohn Marino #else /* !HANDLE_MULTIBYTE */
127*6b445a62SJohn Marino 
128*6b445a62SJohn Marino #undef MB_LEN_MAX
129*6b445a62SJohn Marino #undef MB_CUR_MAX
130*6b445a62SJohn Marino 
131*6b445a62SJohn Marino #define MB_LEN_MAX	1
132*6b445a62SJohn Marino #define MB_CUR_MAX	1
133*6b445a62SJohn Marino 
134*6b445a62SJohn Marino #define _rl_find_prev_mbchar(b, i, f)		(((i) == 0) ? (i) : ((i) - 1))
135*6b445a62SJohn Marino #define _rl_find_next_mbchar(b, i1, i2, f)	((i1) + (i2))
136*6b445a62SJohn Marino 
137*6b445a62SJohn Marino #define _rl_char_value(buf,ind)	((buf)[(ind)])
138*6b445a62SJohn Marino 
139*6b445a62SJohn Marino #define _rl_walphabetic(c)	(rl_alphabetic (c))
140*6b445a62SJohn Marino 
141*6b445a62SJohn Marino #define _rl_to_wupper(c)	(_rl_to_upper (c))
142*6b445a62SJohn Marino #define _rl_to_wlower(c)	(_rl_to_lower (c))
143*6b445a62SJohn Marino 
144*6b445a62SJohn Marino #define MB_NEXTCHAR(b,s,c,f)	((s) + (c))
145*6b445a62SJohn Marino #define MB_PREVCHAR(b,s,f)	((s) - 1)
146*6b445a62SJohn Marino 
147*6b445a62SJohn Marino #define MB_INVALIDCH(x)		(0)
148*6b445a62SJohn Marino #define MB_NULLWCH(x)		(0)
149*6b445a62SJohn Marino 
150*6b445a62SJohn Marino #endif /* !HANDLE_MULTIBYTE */
151*6b445a62SJohn Marino 
152*6b445a62SJohn Marino extern int rl_byte_oriented;
153*6b445a62SJohn Marino 
154*6b445a62SJohn Marino #endif /* _RL_MBUTIL_H_ */
155