xref: /dflybsd-src/contrib/gdb-7/gdb/gdb_wchar.h (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Wide characters for gdb
2*ef5ccd6cSJohn Marino    Copyright (C) 2009-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert 
45796c8dcSSimon Schubert    This file is part of GDB.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert    (at your option) any later version.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145796c8dcSSimon Schubert    GNU General Public License for more details.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert #ifndef GDB_WCHAR_H
205796c8dcSSimon Schubert #define GDB_WCHAR_H
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert /* We handle three different modes here.
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert    Capable systems have the full suite: wchar_t support and iconv
255796c8dcSSimon Schubert    (perhaps via GNU libiconv).  On these machines, full functionality
26c50c785cSJohn Marino    is available.  Note that full functionality is dependent on us
27c50c785cSJohn Marino    being able to convert from an arbitrary encoding to wchar_t.  In
28c50c785cSJohn Marino    practice this means we look for __STDC_ISO_10646__ (where we know
29c50c785cSJohn Marino    the name of the wchar_t encoding) or GNU libiconv, where we can use
30c50c785cSJohn Marino    "wchar_t".
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert    DJGPP is known to have libiconv but not wchar_t support.  On
335796c8dcSSimon Schubert    systems like this, we use the narrow character functions.  The full
345796c8dcSSimon Schubert    functionality is available to the user, but many characters (those
355796c8dcSSimon Schubert    outside the narrow range) will be displayed as escapes.
365796c8dcSSimon Schubert 
37c50c785cSJohn Marino    Finally, some systems do not have iconv, or are really broken
38c50c785cSJohn Marino    (e.g., Solaris, which almost has all of this working, but where
39c50c785cSJohn Marino    just enough is broken to make it too hard to use).  Here we provide
40c50c785cSJohn Marino    a phony iconv which only handles a single character set, and we
41c50c785cSJohn Marino    provide wrappers for the wchar_t functionality we use.  */
425796c8dcSSimon Schubert 
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert #if defined (HAVE_ICONV)
455796c8dcSSimon Schubert #include <iconv.h>
465796c8dcSSimon Schubert #else
475796c8dcSSimon Schubert /* This define is used elsewhere so we don't need to duplicate the
485796c8dcSSimon Schubert    same checking logic in multiple places.  */
495796c8dcSSimon Schubert #define PHONY_ICONV
505796c8dcSSimon Schubert #endif
515796c8dcSSimon Schubert 
52c50c785cSJohn Marino /* We use "btowc" as a sentinel to detect functioning wchar_t support.
53c50c785cSJohn Marino    We check for either __STDC_ISO_10646__ or a new-enough libiconv in
54c50c785cSJohn Marino    order to ensure we can convert to and from wchar_t.  We choose
55c50c785cSJohn Marino    libiconv version 0x108 because it is the first version with
56c50c785cSJohn Marino    iconvlist.  */
57c50c785cSJohn Marino #if defined (HAVE_ICONV) && defined (HAVE_WCHAR_H) && defined (HAVE_BTOWC) \
58c50c785cSJohn Marino   && (defined (__STDC_ISO_10646__) \
59c50c785cSJohn Marino       || (defined (_LIBICONV_VERSION) && _LIBICONV_VERSION >= 0x108))
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert #include <wchar.h>
625796c8dcSSimon Schubert #include <wctype.h>
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert typedef wchar_t gdb_wchar_t;
655796c8dcSSimon Schubert typedef wint_t gdb_wint_t;
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert #define gdb_wcslen wcslen
685796c8dcSSimon Schubert #define gdb_iswprint iswprint
695796c8dcSSimon Schubert #define gdb_iswdigit iswdigit
705796c8dcSSimon Schubert #define gdb_btowc btowc
715796c8dcSSimon Schubert #define gdb_WEOF WEOF
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert #define LCST(X) L ## X
745796c8dcSSimon Schubert 
75c50c785cSJohn Marino /* If __STDC_ISO_10646__ is defined, then the host wchar_t is UCS-4.
76c50c785cSJohn Marino    We exploit this fact in the hope that there are hosts that define
77c50c785cSJohn Marino    this but which do not support "wchar_t" as an encoding argument to
78c50c785cSJohn Marino    iconv_open.  We put the endianness into the encoding name to avoid
79c50c785cSJohn Marino    hosts that emit a BOM when the unadorned name is used.  */
80c50c785cSJohn Marino #if defined (__STDC_ISO_10646__)
81c50c785cSJohn Marino #define USE_INTERMEDIATE_ENCODING_FUNCTION
82c50c785cSJohn Marino #define INTERMEDIATE_ENCODING intermediate_encoding ()
83c50c785cSJohn Marino const char *intermediate_encoding (void);
84c50c785cSJohn Marino 
85c50c785cSJohn Marino #elif defined (_LIBICONV_VERSION) && _LIBICONV_VERSION >= 0x108
86c50c785cSJohn Marino #define INTERMEDIATE_ENCODING "wchar_t"
875796c8dcSSimon Schubert #else
88c50c785cSJohn Marino /* This shouldn't happen, because the earlier #if should have filtered
89c50c785cSJohn Marino    out this case.  */
90c50c785cSJohn Marino #error "Neither __STDC_ISO_10646__ nor _LIBICONV_VERSION defined"
91c50c785cSJohn Marino #endif
92c50c785cSJohn Marino 
93c50c785cSJohn Marino #else
94c50c785cSJohn Marino 
95c50c785cSJohn Marino /* If we got here and have wchar_t support, we might be on a system
96c50c785cSJohn Marino    with some problem.  So, we just disable everything.  */
97c50c785cSJohn Marino #if defined (HAVE_WCHAR_H) && defined (HAVE_BTOWC)
98c50c785cSJohn Marino #define PHONY_ICONV
99c50c785cSJohn Marino #endif
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert typedef char gdb_wchar_t;
1025796c8dcSSimon Schubert typedef int gdb_wint_t;
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert #define gdb_wcslen strlen
1055796c8dcSSimon Schubert #define gdb_iswprint isprint
1065796c8dcSSimon Schubert #define gdb_iswdigit isdigit
1075796c8dcSSimon Schubert #define gdb_btowc /* empty */
1085796c8dcSSimon Schubert #define gdb_WEOF EOF
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert #define LCST(X) X
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert /* If we are using the narrow character set, we want to use the host
1135796c8dcSSimon Schubert    narrow encoding as our intermediate encoding.  However, if we are
1145796c8dcSSimon Schubert    also providing a phony iconv, we might as well just stick with
1155796c8dcSSimon Schubert    "wchar_t".  */
116c50c785cSJohn Marino #ifdef PHONY_ICONV
117c50c785cSJohn Marino #define INTERMEDIATE_ENCODING "wchar_t"
118c50c785cSJohn Marino #else
1195796c8dcSSimon Schubert #define INTERMEDIATE_ENCODING host_charset ()
1205796c8dcSSimon Schubert #endif
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert #endif
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert #endif /* GDB_WCHAR_H */
125