xref: /dflybsd-src/contrib/gdb-7/gdb/charset.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Character set conversion support for GDB.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 2001, 2003, 2007, 2008, 2009 Free Software Foundation, Inc.
4*5796c8dcSSimon Schubert 
5*5796c8dcSSimon Schubert    This file is part of GDB.
6*5796c8dcSSimon Schubert 
7*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
8*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
9*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
10*5796c8dcSSimon Schubert    (at your option) any later version.
11*5796c8dcSSimon Schubert 
12*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
13*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*5796c8dcSSimon Schubert    GNU General Public License for more details.
16*5796c8dcSSimon Schubert 
17*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
18*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*5796c8dcSSimon Schubert 
20*5796c8dcSSimon Schubert #include "defs.h"
21*5796c8dcSSimon Schubert #include "charset.h"
22*5796c8dcSSimon Schubert #include "gdbcmd.h"
23*5796c8dcSSimon Schubert #include "gdb_assert.h"
24*5796c8dcSSimon Schubert #include "gdb_obstack.h"
25*5796c8dcSSimon Schubert #include "gdb_wait.h"
26*5796c8dcSSimon Schubert #include "charset-list.h"
27*5796c8dcSSimon Schubert #include "vec.h"
28*5796c8dcSSimon Schubert 
29*5796c8dcSSimon Schubert #include <stddef.h>
30*5796c8dcSSimon Schubert #include "gdb_string.h"
31*5796c8dcSSimon Schubert #include <ctype.h>
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert 
34*5796c8dcSSimon Schubert /* How GDB's character set support works
35*5796c8dcSSimon Schubert 
36*5796c8dcSSimon Schubert    GDB has three global settings:
37*5796c8dcSSimon Schubert 
38*5796c8dcSSimon Schubert    - The `current host character set' is the character set GDB should
39*5796c8dcSSimon Schubert      use in talking to the user, and which (hopefully) the user's
40*5796c8dcSSimon Schubert      terminal knows how to display properly.  Most users should not
41*5796c8dcSSimon Schubert      change this.
42*5796c8dcSSimon Schubert 
43*5796c8dcSSimon Schubert    - The `current target character set' is the character set the
44*5796c8dcSSimon Schubert      program being debugged uses.
45*5796c8dcSSimon Schubert 
46*5796c8dcSSimon Schubert    - The `current target wide character set' is the wide character set
47*5796c8dcSSimon Schubert      the program being debugged uses, that is, the encoding used for
48*5796c8dcSSimon Schubert      wchar_t.
49*5796c8dcSSimon Schubert 
50*5796c8dcSSimon Schubert    There are commands to set each of these, and mechanisms for
51*5796c8dcSSimon Schubert    choosing reasonable default values.  GDB has a global list of
52*5796c8dcSSimon Schubert    character sets that it can use as its host or target character
53*5796c8dcSSimon Schubert    sets.
54*5796c8dcSSimon Schubert 
55*5796c8dcSSimon Schubert    The header file `charset.h' declares various functions that
56*5796c8dcSSimon Schubert    different pieces of GDB need to perform tasks like:
57*5796c8dcSSimon Schubert 
58*5796c8dcSSimon Schubert    - printing target strings and characters to the user's terminal
59*5796c8dcSSimon Schubert      (mostly target->host conversions),
60*5796c8dcSSimon Schubert 
61*5796c8dcSSimon Schubert    - building target-appropriate representations of strings and
62*5796c8dcSSimon Schubert      characters the user enters in expressions (mostly host->target
63*5796c8dcSSimon Schubert      conversions),
64*5796c8dcSSimon Schubert 
65*5796c8dcSSimon Schubert      and so on.
66*5796c8dcSSimon Schubert 
67*5796c8dcSSimon Schubert    To avoid excessive code duplication and maintenance efforts,
68*5796c8dcSSimon Schubert    GDB simply requires a capable iconv function.  Users on platforms
69*5796c8dcSSimon Schubert    without a suitable iconv can use the GNU iconv library.  */
70*5796c8dcSSimon Schubert 
71*5796c8dcSSimon Schubert 
72*5796c8dcSSimon Schubert #ifdef PHONY_ICONV
73*5796c8dcSSimon Schubert 
74*5796c8dcSSimon Schubert /* Provide a phony iconv that does as little as possible.  Also,
75*5796c8dcSSimon Schubert    arrange for there to be a single available character set.  */
76*5796c8dcSSimon Schubert 
77*5796c8dcSSimon Schubert #undef GDB_DEFAULT_HOST_CHARSET
78*5796c8dcSSimon Schubert #define GDB_DEFAULT_HOST_CHARSET "ISO-8859-1"
79*5796c8dcSSimon Schubert #define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
80*5796c8dcSSimon Schubert #define GDB_DEFAULT_TARGET_WIDE_CHARSET "ISO-8859-1"
81*5796c8dcSSimon Schubert #undef DEFAULT_CHARSET_NAMES
82*5796c8dcSSimon Schubert #define DEFAULT_CHARSET_NAMES GDB_DEFAULT_HOST_CHARSET ,
83*5796c8dcSSimon Schubert 
84*5796c8dcSSimon Schubert #undef iconv_t
85*5796c8dcSSimon Schubert #define iconv_t int
86*5796c8dcSSimon Schubert #undef iconv_open
87*5796c8dcSSimon Schubert #undef iconv
88*5796c8dcSSimon Schubert #undef iconv_close
89*5796c8dcSSimon Schubert 
90*5796c8dcSSimon Schubert #undef ICONV_CONST
91*5796c8dcSSimon Schubert #define ICONV_CONST const
92*5796c8dcSSimon Schubert 
93*5796c8dcSSimon Schubert /* Some systems don't have EILSEQ, so we define it here, but not as
94*5796c8dcSSimon Schubert    EINVAL, because callers of `iconv' want to distinguish EINVAL and
95*5796c8dcSSimon Schubert    EILSEQ.  This is what iconv.h from libiconv does as well.  Note
96*5796c8dcSSimon Schubert    that wchar.h may also define EILSEQ, so this needs to be after we
97*5796c8dcSSimon Schubert    include wchar.h, which happens in defs.h through gdb_wchar.h.  */
98*5796c8dcSSimon Schubert #ifndef EILSEQ
99*5796c8dcSSimon Schubert #define EILSEQ ENOENT
100*5796c8dcSSimon Schubert #endif
101*5796c8dcSSimon Schubert 
102*5796c8dcSSimon Schubert iconv_t
103*5796c8dcSSimon Schubert iconv_open (const char *to, const char *from)
104*5796c8dcSSimon Schubert {
105*5796c8dcSSimon Schubert   /* We allow conversions from UCS-4BE, wchar_t, and the host charset.
106*5796c8dcSSimon Schubert      We allow conversions to wchar_t and the host charset.  */
107*5796c8dcSSimon Schubert   if (strcmp (from, "UCS-4BE") && strcmp (from, "wchar_t")
108*5796c8dcSSimon Schubert       && strcmp (from, GDB_DEFAULT_HOST_CHARSET))
109*5796c8dcSSimon Schubert     return -1;
110*5796c8dcSSimon Schubert   if (strcmp (to, "wchar_t") && strcmp (to, GDB_DEFAULT_HOST_CHARSET))
111*5796c8dcSSimon Schubert     return -1;
112*5796c8dcSSimon Schubert 
113*5796c8dcSSimon Schubert   /* Return 1 if we are converting from UCS-4BE, 0 otherwise.  This is
114*5796c8dcSSimon Schubert      used as a flag in calls to iconv.  */
115*5796c8dcSSimon Schubert   return !strcmp (from, "UCS-4BE");
116*5796c8dcSSimon Schubert }
117*5796c8dcSSimon Schubert 
118*5796c8dcSSimon Schubert int
119*5796c8dcSSimon Schubert iconv_close (iconv_t arg)
120*5796c8dcSSimon Schubert {
121*5796c8dcSSimon Schubert   return 0;
122*5796c8dcSSimon Schubert }
123*5796c8dcSSimon Schubert 
124*5796c8dcSSimon Schubert size_t
125*5796c8dcSSimon Schubert iconv (iconv_t ucs_flag, const char **inbuf, size_t *inbytesleft,
126*5796c8dcSSimon Schubert        char **outbuf, size_t *outbytesleft)
127*5796c8dcSSimon Schubert {
128*5796c8dcSSimon Schubert   if (ucs_flag)
129*5796c8dcSSimon Schubert     {
130*5796c8dcSSimon Schubert       while (*inbytesleft >= 4)
131*5796c8dcSSimon Schubert 	{
132*5796c8dcSSimon Schubert 	  size_t j;
133*5796c8dcSSimon Schubert 	  unsigned long c = 0;
134*5796c8dcSSimon Schubert 
135*5796c8dcSSimon Schubert 	  for (j = 0; j < 4; ++j)
136*5796c8dcSSimon Schubert 	    {
137*5796c8dcSSimon Schubert 	      c <<= 8;
138*5796c8dcSSimon Schubert 	      c += (*inbuf)[j] & 0xff;
139*5796c8dcSSimon Schubert 	    }
140*5796c8dcSSimon Schubert 
141*5796c8dcSSimon Schubert 	  if (c >= 256)
142*5796c8dcSSimon Schubert 	    {
143*5796c8dcSSimon Schubert 	      errno = EILSEQ;
144*5796c8dcSSimon Schubert 	      return -1;
145*5796c8dcSSimon Schubert 	    }
146*5796c8dcSSimon Schubert 	  **outbuf = c & 0xff;
147*5796c8dcSSimon Schubert 	  ++*outbuf;
148*5796c8dcSSimon Schubert 	  --*outbytesleft;
149*5796c8dcSSimon Schubert 
150*5796c8dcSSimon Schubert 	  ++*inbuf;
151*5796c8dcSSimon Schubert 	  *inbytesleft -= 4;
152*5796c8dcSSimon Schubert 	}
153*5796c8dcSSimon Schubert       if (*inbytesleft < 4)
154*5796c8dcSSimon Schubert 	{
155*5796c8dcSSimon Schubert 	  errno = EINVAL;
156*5796c8dcSSimon Schubert 	  return -1;
157*5796c8dcSSimon Schubert 	}
158*5796c8dcSSimon Schubert     }
159*5796c8dcSSimon Schubert   else
160*5796c8dcSSimon Schubert     {
161*5796c8dcSSimon Schubert       /* In all other cases we simply copy input bytes to the
162*5796c8dcSSimon Schubert 	 output.  */
163*5796c8dcSSimon Schubert       size_t amt = *inbytesleft;
164*5796c8dcSSimon Schubert       if (amt > *outbytesleft)
165*5796c8dcSSimon Schubert 	amt = *outbytesleft;
166*5796c8dcSSimon Schubert       memcpy (*outbuf, *inbuf, amt);
167*5796c8dcSSimon Schubert       *inbuf += amt;
168*5796c8dcSSimon Schubert       *outbuf += amt;
169*5796c8dcSSimon Schubert       *inbytesleft -= amt;
170*5796c8dcSSimon Schubert       *outbytesleft -= amt;
171*5796c8dcSSimon Schubert     }
172*5796c8dcSSimon Schubert 
173*5796c8dcSSimon Schubert   if (*inbytesleft)
174*5796c8dcSSimon Schubert     {
175*5796c8dcSSimon Schubert       errno = E2BIG;
176*5796c8dcSSimon Schubert       return -1;
177*5796c8dcSSimon Schubert     }
178*5796c8dcSSimon Schubert 
179*5796c8dcSSimon Schubert   /* The number of non-reversible conversions -- but they were all
180*5796c8dcSSimon Schubert      reversible.  */
181*5796c8dcSSimon Schubert   return 0;
182*5796c8dcSSimon Schubert }
183*5796c8dcSSimon Schubert 
184*5796c8dcSSimon Schubert #endif
185*5796c8dcSSimon Schubert 
186*5796c8dcSSimon Schubert 
187*5796c8dcSSimon Schubert 
188*5796c8dcSSimon Schubert /* The global lists of character sets and translations.  */
189*5796c8dcSSimon Schubert 
190*5796c8dcSSimon Schubert 
191*5796c8dcSSimon Schubert #ifndef GDB_DEFAULT_TARGET_CHARSET
192*5796c8dcSSimon Schubert #define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
193*5796c8dcSSimon Schubert #endif
194*5796c8dcSSimon Schubert 
195*5796c8dcSSimon Schubert #ifndef GDB_DEFAULT_TARGET_WIDE_CHARSET
196*5796c8dcSSimon Schubert #define GDB_DEFAULT_TARGET_WIDE_CHARSET "UCS-4"
197*5796c8dcSSimon Schubert #endif
198*5796c8dcSSimon Schubert 
199*5796c8dcSSimon Schubert static const char *auto_host_charset_name = GDB_DEFAULT_HOST_CHARSET;
200*5796c8dcSSimon Schubert static const char *host_charset_name = "auto";
201*5796c8dcSSimon Schubert static void
202*5796c8dcSSimon Schubert show_host_charset_name (struct ui_file *file, int from_tty,
203*5796c8dcSSimon Schubert 			struct cmd_list_element *c,
204*5796c8dcSSimon Schubert 			const char *value)
205*5796c8dcSSimon Schubert {
206*5796c8dcSSimon Schubert   if (!strcmp (value, "auto"))
207*5796c8dcSSimon Schubert     fprintf_filtered (file,
208*5796c8dcSSimon Schubert 		      _("The host character set is \"auto; currently %s\".\n"),
209*5796c8dcSSimon Schubert 		      auto_host_charset_name);
210*5796c8dcSSimon Schubert   else
211*5796c8dcSSimon Schubert     fprintf_filtered (file, _("The host character set is \"%s\".\n"), value);
212*5796c8dcSSimon Schubert }
213*5796c8dcSSimon Schubert 
214*5796c8dcSSimon Schubert static const char *target_charset_name = GDB_DEFAULT_TARGET_CHARSET;
215*5796c8dcSSimon Schubert static void
216*5796c8dcSSimon Schubert show_target_charset_name (struct ui_file *file, int from_tty,
217*5796c8dcSSimon Schubert 			  struct cmd_list_element *c, const char *value)
218*5796c8dcSSimon Schubert {
219*5796c8dcSSimon Schubert   fprintf_filtered (file, _("The target character set is \"%s\".\n"),
220*5796c8dcSSimon Schubert 		    value);
221*5796c8dcSSimon Schubert }
222*5796c8dcSSimon Schubert 
223*5796c8dcSSimon Schubert static const char *target_wide_charset_name = GDB_DEFAULT_TARGET_WIDE_CHARSET;
224*5796c8dcSSimon Schubert static void
225*5796c8dcSSimon Schubert show_target_wide_charset_name (struct ui_file *file, int from_tty,
226*5796c8dcSSimon Schubert 			       struct cmd_list_element *c, const char *value)
227*5796c8dcSSimon Schubert {
228*5796c8dcSSimon Schubert   fprintf_filtered (file, _("The target wide character set is \"%s\".\n"),
229*5796c8dcSSimon Schubert 		    value);
230*5796c8dcSSimon Schubert }
231*5796c8dcSSimon Schubert 
232*5796c8dcSSimon Schubert static const char *default_charset_names[] =
233*5796c8dcSSimon Schubert {
234*5796c8dcSSimon Schubert   DEFAULT_CHARSET_NAMES
235*5796c8dcSSimon Schubert   0
236*5796c8dcSSimon Schubert };
237*5796c8dcSSimon Schubert 
238*5796c8dcSSimon Schubert static const char **charset_enum;
239*5796c8dcSSimon Schubert 
240*5796c8dcSSimon Schubert 
241*5796c8dcSSimon Schubert /* If the target wide character set has big- or little-endian
242*5796c8dcSSimon Schubert    variants, these are the corresponding names.  */
243*5796c8dcSSimon Schubert static const char *target_wide_charset_be_name;
244*5796c8dcSSimon Schubert static const char *target_wide_charset_le_name;
245*5796c8dcSSimon Schubert 
246*5796c8dcSSimon Schubert /* A helper function for validate which sets the target wide big- and
247*5796c8dcSSimon Schubert    little-endian character set names, if possible.  */
248*5796c8dcSSimon Schubert 
249*5796c8dcSSimon Schubert static void
250*5796c8dcSSimon Schubert set_be_le_names (void)
251*5796c8dcSSimon Schubert {
252*5796c8dcSSimon Schubert   int i, len;
253*5796c8dcSSimon Schubert 
254*5796c8dcSSimon Schubert   target_wide_charset_le_name = NULL;
255*5796c8dcSSimon Schubert   target_wide_charset_be_name = NULL;
256*5796c8dcSSimon Schubert 
257*5796c8dcSSimon Schubert   len = strlen (target_wide_charset_name);
258*5796c8dcSSimon Schubert   for (i = 0; charset_enum[i]; ++i)
259*5796c8dcSSimon Schubert     {
260*5796c8dcSSimon Schubert       if (strncmp (target_wide_charset_name, charset_enum[i], len))
261*5796c8dcSSimon Schubert 	continue;
262*5796c8dcSSimon Schubert       if ((charset_enum[i][len] == 'B'
263*5796c8dcSSimon Schubert 	   || charset_enum[i][len] == 'L')
264*5796c8dcSSimon Schubert 	  && charset_enum[i][len + 1] == 'E'
265*5796c8dcSSimon Schubert 	  && charset_enum[i][len + 2] == '\0')
266*5796c8dcSSimon Schubert 	{
267*5796c8dcSSimon Schubert 	  if (charset_enum[i][len] == 'B')
268*5796c8dcSSimon Schubert 	    target_wide_charset_be_name = charset_enum[i];
269*5796c8dcSSimon Schubert 	  else
270*5796c8dcSSimon Schubert 	    target_wide_charset_le_name = charset_enum[i];
271*5796c8dcSSimon Schubert 	}
272*5796c8dcSSimon Schubert     }
273*5796c8dcSSimon Schubert }
274*5796c8dcSSimon Schubert 
275*5796c8dcSSimon Schubert /* 'Set charset', 'set host-charset', 'set target-charset', 'set
276*5796c8dcSSimon Schubert    target-wide-charset', 'set charset' sfunc's.  */
277*5796c8dcSSimon Schubert 
278*5796c8dcSSimon Schubert static void
279*5796c8dcSSimon Schubert validate (void)
280*5796c8dcSSimon Schubert {
281*5796c8dcSSimon Schubert   iconv_t desc;
282*5796c8dcSSimon Schubert   const char *host_cset = host_charset ();
283*5796c8dcSSimon Schubert 
284*5796c8dcSSimon Schubert   desc = iconv_open (target_wide_charset_name, host_cset);
285*5796c8dcSSimon Schubert   if (desc == (iconv_t) -1)
286*5796c8dcSSimon Schubert     error ("Cannot convert between character sets `%s' and `%s'",
287*5796c8dcSSimon Schubert 	   target_wide_charset_name, host_cset);
288*5796c8dcSSimon Schubert   iconv_close (desc);
289*5796c8dcSSimon Schubert 
290*5796c8dcSSimon Schubert   desc = iconv_open (target_charset_name, host_cset);
291*5796c8dcSSimon Schubert   if (desc == (iconv_t) -1)
292*5796c8dcSSimon Schubert     error ("Cannot convert between character sets `%s' and `%s'",
293*5796c8dcSSimon Schubert 	   target_charset_name, host_cset);
294*5796c8dcSSimon Schubert   iconv_close (desc);
295*5796c8dcSSimon Schubert 
296*5796c8dcSSimon Schubert   set_be_le_names ();
297*5796c8dcSSimon Schubert }
298*5796c8dcSSimon Schubert 
299*5796c8dcSSimon Schubert /* This is the sfunc for the 'set charset' command.  */
300*5796c8dcSSimon Schubert static void
301*5796c8dcSSimon Schubert set_charset_sfunc (char *charset, int from_tty, struct cmd_list_element *c)
302*5796c8dcSSimon Schubert {
303*5796c8dcSSimon Schubert   /* CAREFUL: set the target charset here as well. */
304*5796c8dcSSimon Schubert   target_charset_name = host_charset_name;
305*5796c8dcSSimon Schubert   validate ();
306*5796c8dcSSimon Schubert }
307*5796c8dcSSimon Schubert 
308*5796c8dcSSimon Schubert /* 'set host-charset' command sfunc.  We need a wrapper here because
309*5796c8dcSSimon Schubert    the function needs to have a specific signature.  */
310*5796c8dcSSimon Schubert static void
311*5796c8dcSSimon Schubert set_host_charset_sfunc (char *charset, int from_tty,
312*5796c8dcSSimon Schubert 			struct cmd_list_element *c)
313*5796c8dcSSimon Schubert {
314*5796c8dcSSimon Schubert   validate ();
315*5796c8dcSSimon Schubert }
316*5796c8dcSSimon Schubert 
317*5796c8dcSSimon Schubert /* Wrapper for the 'set target-charset' command.  */
318*5796c8dcSSimon Schubert static void
319*5796c8dcSSimon Schubert set_target_charset_sfunc (char *charset, int from_tty,
320*5796c8dcSSimon Schubert 			  struct cmd_list_element *c)
321*5796c8dcSSimon Schubert {
322*5796c8dcSSimon Schubert   validate ();
323*5796c8dcSSimon Schubert }
324*5796c8dcSSimon Schubert 
325*5796c8dcSSimon Schubert /* Wrapper for the 'set target-wide-charset' command.  */
326*5796c8dcSSimon Schubert static void
327*5796c8dcSSimon Schubert set_target_wide_charset_sfunc (char *charset, int from_tty,
328*5796c8dcSSimon Schubert 			       struct cmd_list_element *c)
329*5796c8dcSSimon Schubert {
330*5796c8dcSSimon Schubert   validate ();
331*5796c8dcSSimon Schubert }
332*5796c8dcSSimon Schubert 
333*5796c8dcSSimon Schubert /* sfunc for the 'show charset' command.  */
334*5796c8dcSSimon Schubert static void
335*5796c8dcSSimon Schubert show_charset (struct ui_file *file, int from_tty, struct cmd_list_element *c,
336*5796c8dcSSimon Schubert 	      const char *name)
337*5796c8dcSSimon Schubert {
338*5796c8dcSSimon Schubert   show_host_charset_name (file, from_tty, c, host_charset_name);
339*5796c8dcSSimon Schubert   show_target_charset_name (file, from_tty, c, target_charset_name);
340*5796c8dcSSimon Schubert   show_target_wide_charset_name (file, from_tty, c, target_wide_charset_name);
341*5796c8dcSSimon Schubert }
342*5796c8dcSSimon Schubert 
343*5796c8dcSSimon Schubert 
344*5796c8dcSSimon Schubert /* Accessor functions.  */
345*5796c8dcSSimon Schubert 
346*5796c8dcSSimon Schubert const char *
347*5796c8dcSSimon Schubert host_charset (void)
348*5796c8dcSSimon Schubert {
349*5796c8dcSSimon Schubert   if (!strcmp (host_charset_name, "auto"))
350*5796c8dcSSimon Schubert     return auto_host_charset_name;
351*5796c8dcSSimon Schubert   return host_charset_name;
352*5796c8dcSSimon Schubert }
353*5796c8dcSSimon Schubert 
354*5796c8dcSSimon Schubert const char *
355*5796c8dcSSimon Schubert target_charset (void)
356*5796c8dcSSimon Schubert {
357*5796c8dcSSimon Schubert   return target_charset_name;
358*5796c8dcSSimon Schubert }
359*5796c8dcSSimon Schubert 
360*5796c8dcSSimon Schubert const char *
361*5796c8dcSSimon Schubert target_wide_charset (enum bfd_endian byte_order)
362*5796c8dcSSimon Schubert {
363*5796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
364*5796c8dcSSimon Schubert     {
365*5796c8dcSSimon Schubert       if (target_wide_charset_be_name)
366*5796c8dcSSimon Schubert 	return target_wide_charset_be_name;
367*5796c8dcSSimon Schubert     }
368*5796c8dcSSimon Schubert   else
369*5796c8dcSSimon Schubert     {
370*5796c8dcSSimon Schubert       if (target_wide_charset_le_name)
371*5796c8dcSSimon Schubert 	return target_wide_charset_le_name;
372*5796c8dcSSimon Schubert     }
373*5796c8dcSSimon Schubert 
374*5796c8dcSSimon Schubert   return target_wide_charset_name;
375*5796c8dcSSimon Schubert }
376*5796c8dcSSimon Schubert 
377*5796c8dcSSimon Schubert 
378*5796c8dcSSimon Schubert /* Host character set management.  For the time being, we assume that
379*5796c8dcSSimon Schubert    the host character set is some superset of ASCII.  */
380*5796c8dcSSimon Schubert 
381*5796c8dcSSimon Schubert char
382*5796c8dcSSimon Schubert host_letter_to_control_character (char c)
383*5796c8dcSSimon Schubert {
384*5796c8dcSSimon Schubert   if (c == '?')
385*5796c8dcSSimon Schubert     return 0177;
386*5796c8dcSSimon Schubert   return c & 0237;
387*5796c8dcSSimon Schubert }
388*5796c8dcSSimon Schubert 
389*5796c8dcSSimon Schubert /* Convert a host character, C, to its hex value.  C must already have
390*5796c8dcSSimon Schubert    been validated using isxdigit.  */
391*5796c8dcSSimon Schubert 
392*5796c8dcSSimon Schubert int
393*5796c8dcSSimon Schubert host_hex_value (char c)
394*5796c8dcSSimon Schubert {
395*5796c8dcSSimon Schubert   if (isdigit (c))
396*5796c8dcSSimon Schubert     return c - '0';
397*5796c8dcSSimon Schubert   if (c >= 'a' && c <= 'f')
398*5796c8dcSSimon Schubert     return 10 + c - 'a';
399*5796c8dcSSimon Schubert   gdb_assert (c >= 'A' && c <= 'F');
400*5796c8dcSSimon Schubert   return 10 + c - 'A';
401*5796c8dcSSimon Schubert }
402*5796c8dcSSimon Schubert 
403*5796c8dcSSimon Schubert 
404*5796c8dcSSimon Schubert /* Public character management functions.  */
405*5796c8dcSSimon Schubert 
406*5796c8dcSSimon Schubert /* A cleanup function which is run to close an iconv descriptor.  */
407*5796c8dcSSimon Schubert 
408*5796c8dcSSimon Schubert static void
409*5796c8dcSSimon Schubert cleanup_iconv (void *p)
410*5796c8dcSSimon Schubert {
411*5796c8dcSSimon Schubert   iconv_t *descp = p;
412*5796c8dcSSimon Schubert   iconv_close (*descp);
413*5796c8dcSSimon Schubert }
414*5796c8dcSSimon Schubert 
415*5796c8dcSSimon Schubert void
416*5796c8dcSSimon Schubert convert_between_encodings (const char *from, const char *to,
417*5796c8dcSSimon Schubert 			   const gdb_byte *bytes, unsigned int num_bytes,
418*5796c8dcSSimon Schubert 			   int width, struct obstack *output,
419*5796c8dcSSimon Schubert 			   enum transliterations translit)
420*5796c8dcSSimon Schubert {
421*5796c8dcSSimon Schubert   iconv_t desc;
422*5796c8dcSSimon Schubert   struct cleanup *cleanups;
423*5796c8dcSSimon Schubert   size_t inleft;
424*5796c8dcSSimon Schubert   char *inp;
425*5796c8dcSSimon Schubert   unsigned int space_request;
426*5796c8dcSSimon Schubert 
427*5796c8dcSSimon Schubert   /* Often, the host and target charsets will be the same.  */
428*5796c8dcSSimon Schubert   if (!strcmp (from, to))
429*5796c8dcSSimon Schubert     {
430*5796c8dcSSimon Schubert       obstack_grow (output, bytes, num_bytes);
431*5796c8dcSSimon Schubert       return;
432*5796c8dcSSimon Schubert     }
433*5796c8dcSSimon Schubert 
434*5796c8dcSSimon Schubert   desc = iconv_open (to, from);
435*5796c8dcSSimon Schubert   if (desc == (iconv_t) -1)
436*5796c8dcSSimon Schubert     perror_with_name ("Converting character sets");
437*5796c8dcSSimon Schubert   cleanups = make_cleanup (cleanup_iconv, &desc);
438*5796c8dcSSimon Schubert 
439*5796c8dcSSimon Schubert   inleft = num_bytes;
440*5796c8dcSSimon Schubert   inp = (char *) bytes;
441*5796c8dcSSimon Schubert 
442*5796c8dcSSimon Schubert   space_request = num_bytes;
443*5796c8dcSSimon Schubert 
444*5796c8dcSSimon Schubert   while (inleft > 0)
445*5796c8dcSSimon Schubert     {
446*5796c8dcSSimon Schubert       char *outp;
447*5796c8dcSSimon Schubert       size_t outleft, r;
448*5796c8dcSSimon Schubert       int old_size;
449*5796c8dcSSimon Schubert 
450*5796c8dcSSimon Schubert       old_size = obstack_object_size (output);
451*5796c8dcSSimon Schubert       obstack_blank (output, space_request);
452*5796c8dcSSimon Schubert 
453*5796c8dcSSimon Schubert       outp = obstack_base (output) + old_size;
454*5796c8dcSSimon Schubert       outleft = space_request;
455*5796c8dcSSimon Schubert 
456*5796c8dcSSimon Schubert       r = iconv (desc, (ICONV_CONST char **) &inp, &inleft, &outp, &outleft);
457*5796c8dcSSimon Schubert 
458*5796c8dcSSimon Schubert       /* Now make sure that the object on the obstack only includes
459*5796c8dcSSimon Schubert 	 bytes we have converted.  */
460*5796c8dcSSimon Schubert       obstack_blank (output, - (int) outleft);
461*5796c8dcSSimon Schubert 
462*5796c8dcSSimon Schubert       if (r == (size_t) -1)
463*5796c8dcSSimon Schubert 	{
464*5796c8dcSSimon Schubert 	  switch (errno)
465*5796c8dcSSimon Schubert 	    {
466*5796c8dcSSimon Schubert 	    case EILSEQ:
467*5796c8dcSSimon Schubert 	      {
468*5796c8dcSSimon Schubert 		int i;
469*5796c8dcSSimon Schubert 
470*5796c8dcSSimon Schubert 		/* Invalid input sequence.  */
471*5796c8dcSSimon Schubert 		if (translit == translit_none)
472*5796c8dcSSimon Schubert 		  error (_("Could not convert character to `%s' character set"),
473*5796c8dcSSimon Schubert 			 to);
474*5796c8dcSSimon Schubert 
475*5796c8dcSSimon Schubert 		/* We emit escape sequence for the bytes, skip them,
476*5796c8dcSSimon Schubert 		   and try again.  */
477*5796c8dcSSimon Schubert 		for (i = 0; i < width; ++i)
478*5796c8dcSSimon Schubert 		  {
479*5796c8dcSSimon Schubert 		    char octal[5];
480*5796c8dcSSimon Schubert 
481*5796c8dcSSimon Schubert 		    sprintf (octal, "\\%.3o", *inp & 0xff);
482*5796c8dcSSimon Schubert 		    obstack_grow_str (output, octal);
483*5796c8dcSSimon Schubert 
484*5796c8dcSSimon Schubert 		    ++inp;
485*5796c8dcSSimon Schubert 		    --inleft;
486*5796c8dcSSimon Schubert 		  }
487*5796c8dcSSimon Schubert 	      }
488*5796c8dcSSimon Schubert 	      break;
489*5796c8dcSSimon Schubert 
490*5796c8dcSSimon Schubert 	    case E2BIG:
491*5796c8dcSSimon Schubert 	      /* We ran out of space in the output buffer.  Make it
492*5796c8dcSSimon Schubert 		 bigger next time around.  */
493*5796c8dcSSimon Schubert 	      space_request *= 2;
494*5796c8dcSSimon Schubert 	      break;
495*5796c8dcSSimon Schubert 
496*5796c8dcSSimon Schubert 	    case EINVAL:
497*5796c8dcSSimon Schubert 	      /* Incomplete input sequence.  FIXME: ought to report this
498*5796c8dcSSimon Schubert 		 to the caller somehow.  */
499*5796c8dcSSimon Schubert 	      inleft = 0;
500*5796c8dcSSimon Schubert 	      break;
501*5796c8dcSSimon Schubert 
502*5796c8dcSSimon Schubert 	    default:
503*5796c8dcSSimon Schubert 	      perror_with_name ("Internal error while converting character sets");
504*5796c8dcSSimon Schubert 	    }
505*5796c8dcSSimon Schubert 	}
506*5796c8dcSSimon Schubert     }
507*5796c8dcSSimon Schubert 
508*5796c8dcSSimon Schubert   do_cleanups (cleanups);
509*5796c8dcSSimon Schubert }
510*5796c8dcSSimon Schubert 
511*5796c8dcSSimon Schubert 
512*5796c8dcSSimon Schubert 
513*5796c8dcSSimon Schubert /* An iterator that returns host wchar_t's from a target string.  */
514*5796c8dcSSimon Schubert struct wchar_iterator
515*5796c8dcSSimon Schubert {
516*5796c8dcSSimon Schubert   /* The underlying iconv descriptor.  */
517*5796c8dcSSimon Schubert   iconv_t desc;
518*5796c8dcSSimon Schubert 
519*5796c8dcSSimon Schubert   /* The input string.  This is updated as convert characters.  */
520*5796c8dcSSimon Schubert   char *input;
521*5796c8dcSSimon Schubert   /* The number of bytes remaining in the input.  */
522*5796c8dcSSimon Schubert   size_t bytes;
523*5796c8dcSSimon Schubert 
524*5796c8dcSSimon Schubert   /* The width of an input character.  */
525*5796c8dcSSimon Schubert   size_t width;
526*5796c8dcSSimon Schubert 
527*5796c8dcSSimon Schubert   /* The output buffer and its size.  */
528*5796c8dcSSimon Schubert   gdb_wchar_t *out;
529*5796c8dcSSimon Schubert   size_t out_size;
530*5796c8dcSSimon Schubert };
531*5796c8dcSSimon Schubert 
532*5796c8dcSSimon Schubert /* Create a new iterator.  */
533*5796c8dcSSimon Schubert struct wchar_iterator *
534*5796c8dcSSimon Schubert make_wchar_iterator (const gdb_byte *input, size_t bytes, const char *charset,
535*5796c8dcSSimon Schubert 		     size_t width)
536*5796c8dcSSimon Schubert {
537*5796c8dcSSimon Schubert   struct wchar_iterator *result;
538*5796c8dcSSimon Schubert   iconv_t desc;
539*5796c8dcSSimon Schubert 
540*5796c8dcSSimon Schubert   desc = iconv_open (INTERMEDIATE_ENCODING, charset);
541*5796c8dcSSimon Schubert   if (desc == (iconv_t) -1)
542*5796c8dcSSimon Schubert     perror_with_name ("Converting character sets");
543*5796c8dcSSimon Schubert 
544*5796c8dcSSimon Schubert   result = XNEW (struct wchar_iterator);
545*5796c8dcSSimon Schubert   result->desc = desc;
546*5796c8dcSSimon Schubert   result->input = (char *) input;
547*5796c8dcSSimon Schubert   result->bytes = bytes;
548*5796c8dcSSimon Schubert   result->width = width;
549*5796c8dcSSimon Schubert 
550*5796c8dcSSimon Schubert   result->out = XNEW (gdb_wchar_t);
551*5796c8dcSSimon Schubert   result->out_size = 1;
552*5796c8dcSSimon Schubert 
553*5796c8dcSSimon Schubert   return result;
554*5796c8dcSSimon Schubert }
555*5796c8dcSSimon Schubert 
556*5796c8dcSSimon Schubert static void
557*5796c8dcSSimon Schubert do_cleanup_iterator (void *p)
558*5796c8dcSSimon Schubert {
559*5796c8dcSSimon Schubert   struct wchar_iterator *iter = p;
560*5796c8dcSSimon Schubert 
561*5796c8dcSSimon Schubert   iconv_close (iter->desc);
562*5796c8dcSSimon Schubert   xfree (iter->out);
563*5796c8dcSSimon Schubert   xfree (iter);
564*5796c8dcSSimon Schubert }
565*5796c8dcSSimon Schubert 
566*5796c8dcSSimon Schubert struct cleanup *
567*5796c8dcSSimon Schubert make_cleanup_wchar_iterator (struct wchar_iterator *iter)
568*5796c8dcSSimon Schubert {
569*5796c8dcSSimon Schubert   return make_cleanup (do_cleanup_iterator, iter);
570*5796c8dcSSimon Schubert }
571*5796c8dcSSimon Schubert 
572*5796c8dcSSimon Schubert int
573*5796c8dcSSimon Schubert wchar_iterate (struct wchar_iterator *iter,
574*5796c8dcSSimon Schubert 	       enum wchar_iterate_result *out_result,
575*5796c8dcSSimon Schubert 	       gdb_wchar_t **out_chars,
576*5796c8dcSSimon Schubert 	       const gdb_byte **ptr,
577*5796c8dcSSimon Schubert 	       size_t *len)
578*5796c8dcSSimon Schubert {
579*5796c8dcSSimon Schubert   size_t out_request;
580*5796c8dcSSimon Schubert 
581*5796c8dcSSimon Schubert   /* Try to convert some characters.  At first we try to convert just
582*5796c8dcSSimon Schubert      a single character.  The reason for this is that iconv does not
583*5796c8dcSSimon Schubert      necessarily update its outgoing arguments when it encounters an
584*5796c8dcSSimon Schubert      invalid input sequence -- but we want to reliably report this to
585*5796c8dcSSimon Schubert      our caller so it can emit an escape sequence.  */
586*5796c8dcSSimon Schubert   out_request = 1;
587*5796c8dcSSimon Schubert   while (iter->bytes > 0)
588*5796c8dcSSimon Schubert     {
589*5796c8dcSSimon Schubert       char *outptr = (char *) &iter->out[0];
590*5796c8dcSSimon Schubert       char *orig_inptr = iter->input;
591*5796c8dcSSimon Schubert       size_t orig_in = iter->bytes;
592*5796c8dcSSimon Schubert       size_t out_avail = out_request * sizeof (gdb_wchar_t);
593*5796c8dcSSimon Schubert       size_t num;
594*5796c8dcSSimon Schubert       gdb_wchar_t result;
595*5796c8dcSSimon Schubert 
596*5796c8dcSSimon Schubert       size_t r = iconv (iter->desc,
597*5796c8dcSSimon Schubert 			(ICONV_CONST char **) &iter->input, &iter->bytes,
598*5796c8dcSSimon Schubert 			&outptr, &out_avail);
599*5796c8dcSSimon Schubert       if (r == (size_t) -1)
600*5796c8dcSSimon Schubert 	{
601*5796c8dcSSimon Schubert 	  switch (errno)
602*5796c8dcSSimon Schubert 	    {
603*5796c8dcSSimon Schubert 	    case EILSEQ:
604*5796c8dcSSimon Schubert 	      /* Invalid input sequence.  Skip it, and let the caller
605*5796c8dcSSimon Schubert 		 know about it.  */
606*5796c8dcSSimon Schubert 	      *out_result = wchar_iterate_invalid;
607*5796c8dcSSimon Schubert 	      *ptr = iter->input;
608*5796c8dcSSimon Schubert 	      *len = iter->width;
609*5796c8dcSSimon Schubert 	      iter->input += iter->width;
610*5796c8dcSSimon Schubert 	      iter->bytes -= iter->width;
611*5796c8dcSSimon Schubert 	      return 0;
612*5796c8dcSSimon Schubert 
613*5796c8dcSSimon Schubert 	    case E2BIG:
614*5796c8dcSSimon Schubert 	      /* We ran out of space.  We still might have converted a
615*5796c8dcSSimon Schubert 		 character; if so, return it.  Otherwise, grow the
616*5796c8dcSSimon Schubert 		 buffer and try again.  */
617*5796c8dcSSimon Schubert 	      if (out_avail < out_request * sizeof (gdb_wchar_t))
618*5796c8dcSSimon Schubert 		break;
619*5796c8dcSSimon Schubert 
620*5796c8dcSSimon Schubert 	      ++out_request;
621*5796c8dcSSimon Schubert 	      if (out_request > iter->out_size)
622*5796c8dcSSimon Schubert 		{
623*5796c8dcSSimon Schubert 		  iter->out_size = out_request;
624*5796c8dcSSimon Schubert 		  iter->out = xrealloc (iter->out,
625*5796c8dcSSimon Schubert 					out_request * sizeof (gdb_wchar_t));
626*5796c8dcSSimon Schubert 		}
627*5796c8dcSSimon Schubert 	      continue;
628*5796c8dcSSimon Schubert 
629*5796c8dcSSimon Schubert 	    case EINVAL:
630*5796c8dcSSimon Schubert 	      /* Incomplete input sequence.  Let the caller know, and
631*5796c8dcSSimon Schubert 		 arrange for future calls to see EOF.  */
632*5796c8dcSSimon Schubert 	      *out_result = wchar_iterate_incomplete;
633*5796c8dcSSimon Schubert 	      *ptr = iter->input;
634*5796c8dcSSimon Schubert 	      *len = iter->bytes;
635*5796c8dcSSimon Schubert 	      iter->bytes = 0;
636*5796c8dcSSimon Schubert 	      return 0;
637*5796c8dcSSimon Schubert 
638*5796c8dcSSimon Schubert 	    default:
639*5796c8dcSSimon Schubert 	      perror_with_name ("Internal error while converting character sets");
640*5796c8dcSSimon Schubert 	    }
641*5796c8dcSSimon Schubert 	}
642*5796c8dcSSimon Schubert 
643*5796c8dcSSimon Schubert       /* We converted something.  */
644*5796c8dcSSimon Schubert       num = out_request - out_avail / sizeof (gdb_wchar_t);
645*5796c8dcSSimon Schubert       *out_result = wchar_iterate_ok;
646*5796c8dcSSimon Schubert       *out_chars = iter->out;
647*5796c8dcSSimon Schubert       *ptr = orig_inptr;
648*5796c8dcSSimon Schubert       *len = orig_in - iter->bytes;
649*5796c8dcSSimon Schubert       return num;
650*5796c8dcSSimon Schubert     }
651*5796c8dcSSimon Schubert 
652*5796c8dcSSimon Schubert   /* Really done.  */
653*5796c8dcSSimon Schubert   *out_result = wchar_iterate_eof;
654*5796c8dcSSimon Schubert   return -1;
655*5796c8dcSSimon Schubert }
656*5796c8dcSSimon Schubert 
657*5796c8dcSSimon Schubert 
658*5796c8dcSSimon Schubert /* The charset.c module initialization function.  */
659*5796c8dcSSimon Schubert 
660*5796c8dcSSimon Schubert extern initialize_file_ftype _initialize_charset; /* -Wmissing-prototype */
661*5796c8dcSSimon Schubert 
662*5796c8dcSSimon Schubert typedef char *char_ptr;
663*5796c8dcSSimon Schubert DEF_VEC_P (char_ptr);
664*5796c8dcSSimon Schubert 
665*5796c8dcSSimon Schubert static VEC (char_ptr) *charsets;
666*5796c8dcSSimon Schubert 
667*5796c8dcSSimon Schubert #ifdef PHONY_ICONV
668*5796c8dcSSimon Schubert 
669*5796c8dcSSimon Schubert static void
670*5796c8dcSSimon Schubert find_charset_names (void)
671*5796c8dcSSimon Schubert {
672*5796c8dcSSimon Schubert   VEC_safe_push (char_ptr, charsets, GDB_DEFAULT_HOST_CHARSET);
673*5796c8dcSSimon Schubert   VEC_safe_push (char_ptr, charsets, NULL);
674*5796c8dcSSimon Schubert }
675*5796c8dcSSimon Schubert 
676*5796c8dcSSimon Schubert #else /* PHONY_ICONV */
677*5796c8dcSSimon Schubert 
678*5796c8dcSSimon Schubert /* Sometimes, libiconv redefines iconvlist as libiconvlist -- but
679*5796c8dcSSimon Schubert    provides different symbols in the static and dynamic libraries.
680*5796c8dcSSimon Schubert    So, configure may see libiconvlist but not iconvlist.  But, calling
681*5796c8dcSSimon Schubert    iconvlist is the right thing to do and will work.  Hence we do a
682*5796c8dcSSimon Schubert    check here but unconditionally call iconvlist below.  */
683*5796c8dcSSimon Schubert #if defined (HAVE_ICONVLIST) || defined (HAVE_LIBICONVLIST)
684*5796c8dcSSimon Schubert 
685*5796c8dcSSimon Schubert /* A helper function that adds some character sets to the vector of
686*5796c8dcSSimon Schubert    all character sets.  This is a callback function for iconvlist.  */
687*5796c8dcSSimon Schubert 
688*5796c8dcSSimon Schubert static int
689*5796c8dcSSimon Schubert add_one (unsigned int count, const char *const *names, void *data)
690*5796c8dcSSimon Schubert {
691*5796c8dcSSimon Schubert   unsigned int i;
692*5796c8dcSSimon Schubert 
693*5796c8dcSSimon Schubert   for (i = 0; i < count; ++i)
694*5796c8dcSSimon Schubert     VEC_safe_push (char_ptr, charsets, xstrdup (names[i]));
695*5796c8dcSSimon Schubert 
696*5796c8dcSSimon Schubert   return 0;
697*5796c8dcSSimon Schubert }
698*5796c8dcSSimon Schubert 
699*5796c8dcSSimon Schubert static void
700*5796c8dcSSimon Schubert find_charset_names (void)
701*5796c8dcSSimon Schubert {
702*5796c8dcSSimon Schubert   iconvlist (add_one, NULL);
703*5796c8dcSSimon Schubert   VEC_safe_push (char_ptr, charsets, NULL);
704*5796c8dcSSimon Schubert }
705*5796c8dcSSimon Schubert 
706*5796c8dcSSimon Schubert #else
707*5796c8dcSSimon Schubert 
708*5796c8dcSSimon Schubert static void
709*5796c8dcSSimon Schubert find_charset_names (void)
710*5796c8dcSSimon Schubert {
711*5796c8dcSSimon Schubert   struct pex_obj *child;
712*5796c8dcSSimon Schubert   char *args[3];
713*5796c8dcSSimon Schubert   int err, status;
714*5796c8dcSSimon Schubert   int fail = 1;
715*5796c8dcSSimon Schubert 
716*5796c8dcSSimon Schubert   child = pex_init (0, "iconv", NULL);
717*5796c8dcSSimon Schubert 
718*5796c8dcSSimon Schubert   args[0] = "iconv";
719*5796c8dcSSimon Schubert   args[1] = "-l";
720*5796c8dcSSimon Schubert   args[2] = NULL;
721*5796c8dcSSimon Schubert   /* Note that we simply ignore errors here.  */
722*5796c8dcSSimon Schubert   if (!pex_run (child, PEX_SEARCH | PEX_STDERR_TO_STDOUT, "iconv",
723*5796c8dcSSimon Schubert 		args, NULL, NULL, &err))
724*5796c8dcSSimon Schubert     {
725*5796c8dcSSimon Schubert       FILE *in = pex_read_output (child, 0);
726*5796c8dcSSimon Schubert 
727*5796c8dcSSimon Schubert       /* POSIX says that iconv -l uses an unspecified format.  We
728*5796c8dcSSimon Schubert 	 parse the glibc and libiconv formats; feel free to add others
729*5796c8dcSSimon Schubert 	 as needed.  */
730*5796c8dcSSimon Schubert       while (!feof (in))
731*5796c8dcSSimon Schubert 	{
732*5796c8dcSSimon Schubert 	  /* The size of buf is chosen arbitrarily.  */
733*5796c8dcSSimon Schubert 	  char buf[1024];
734*5796c8dcSSimon Schubert 	  char *start, *r;
735*5796c8dcSSimon Schubert 	  int len, keep_going;
736*5796c8dcSSimon Schubert 
737*5796c8dcSSimon Schubert 	  r = fgets (buf, sizeof (buf), in);
738*5796c8dcSSimon Schubert 	  if (!r)
739*5796c8dcSSimon Schubert 	    break;
740*5796c8dcSSimon Schubert 	  len = strlen (r);
741*5796c8dcSSimon Schubert 	  if (len <= 3)
742*5796c8dcSSimon Schubert 	    continue;
743*5796c8dcSSimon Schubert 	  /* Strip off the newline.  */
744*5796c8dcSSimon Schubert 	  --len;
745*5796c8dcSSimon Schubert 	  /* Strip off one or two '/'s.  glibc will print lines like
746*5796c8dcSSimon Schubert 	     "8859_7//", but also "10646-1:1993/UCS4/".  */
747*5796c8dcSSimon Schubert 	  if (buf[len - 1] == '/')
748*5796c8dcSSimon Schubert 	    --len;
749*5796c8dcSSimon Schubert 	  if (buf[len - 1] == '/')
750*5796c8dcSSimon Schubert 	    --len;
751*5796c8dcSSimon Schubert 	  buf[len] = '\0';
752*5796c8dcSSimon Schubert 
753*5796c8dcSSimon Schubert 	  /* libiconv will print multiple entries per line, separated
754*5796c8dcSSimon Schubert 	     by spaces.  */
755*5796c8dcSSimon Schubert 	  start = buf;
756*5796c8dcSSimon Schubert 	  while (1)
757*5796c8dcSSimon Schubert 	    {
758*5796c8dcSSimon Schubert 	      int keep_going;
759*5796c8dcSSimon Schubert 	      char *p;
760*5796c8dcSSimon Schubert 
761*5796c8dcSSimon Schubert 	      /* Find the next space, or end-of-line.  */
762*5796c8dcSSimon Schubert 	      for (p = start; *p && *p != ' '; ++p)
763*5796c8dcSSimon Schubert 		;
764*5796c8dcSSimon Schubert 	      /* Ignore an empty result.  */
765*5796c8dcSSimon Schubert 	      if (p == start)
766*5796c8dcSSimon Schubert 		break;
767*5796c8dcSSimon Schubert 	      keep_going = *p;
768*5796c8dcSSimon Schubert 	      *p = '\0';
769*5796c8dcSSimon Schubert 	      VEC_safe_push (char_ptr, charsets, xstrdup (start));
770*5796c8dcSSimon Schubert 	      if (!keep_going)
771*5796c8dcSSimon Schubert 		break;
772*5796c8dcSSimon Schubert 	      /* Skip any extra spaces.  */
773*5796c8dcSSimon Schubert 	      for (start = p + 1; *start && *start == ' '; ++start)
774*5796c8dcSSimon Schubert 		;
775*5796c8dcSSimon Schubert 	    }
776*5796c8dcSSimon Schubert 	}
777*5796c8dcSSimon Schubert 
778*5796c8dcSSimon Schubert       if (pex_get_status (child, 1, &status)
779*5796c8dcSSimon Schubert 	  && WIFEXITED (status) && !WEXITSTATUS (status))
780*5796c8dcSSimon Schubert 	fail = 0;
781*5796c8dcSSimon Schubert 
782*5796c8dcSSimon Schubert     }
783*5796c8dcSSimon Schubert 
784*5796c8dcSSimon Schubert   pex_free (child);
785*5796c8dcSSimon Schubert 
786*5796c8dcSSimon Schubert   if (fail)
787*5796c8dcSSimon Schubert     {
788*5796c8dcSSimon Schubert       /* Some error occurred, so drop the vector.  */
789*5796c8dcSSimon Schubert       int ix;
790*5796c8dcSSimon Schubert       char *elt;
791*5796c8dcSSimon Schubert       for (ix = 0; VEC_iterate (char_ptr, charsets, ix, elt); ++ix)
792*5796c8dcSSimon Schubert 	xfree (elt);
793*5796c8dcSSimon Schubert       VEC_truncate (char_ptr, charsets, 0);
794*5796c8dcSSimon Schubert     }
795*5796c8dcSSimon Schubert   else
796*5796c8dcSSimon Schubert     VEC_safe_push (char_ptr, charsets, NULL);
797*5796c8dcSSimon Schubert }
798*5796c8dcSSimon Schubert 
799*5796c8dcSSimon Schubert #endif /* HAVE_ICONVLIST || HAVE_LIBICONVLIST */
800*5796c8dcSSimon Schubert #endif /* PHONY_ICONV */
801*5796c8dcSSimon Schubert 
802*5796c8dcSSimon Schubert void
803*5796c8dcSSimon Schubert _initialize_charset (void)
804*5796c8dcSSimon Schubert {
805*5796c8dcSSimon Schubert   struct cmd_list_element *new_cmd;
806*5796c8dcSSimon Schubert 
807*5796c8dcSSimon Schubert   /* The first element is always "auto"; then we skip it for the
808*5796c8dcSSimon Schubert      commands where it is not allowed.  */
809*5796c8dcSSimon Schubert   VEC_safe_push (char_ptr, charsets, xstrdup ("auto"));
810*5796c8dcSSimon Schubert   find_charset_names ();
811*5796c8dcSSimon Schubert 
812*5796c8dcSSimon Schubert   if (VEC_length (char_ptr, charsets) > 1)
813*5796c8dcSSimon Schubert     charset_enum = (const char **) VEC_address (char_ptr, charsets);
814*5796c8dcSSimon Schubert   else
815*5796c8dcSSimon Schubert     charset_enum = default_charset_names;
816*5796c8dcSSimon Schubert 
817*5796c8dcSSimon Schubert #ifndef PHONY_ICONV
818*5796c8dcSSimon Schubert #ifdef HAVE_LANGINFO_CODESET
819*5796c8dcSSimon Schubert   auto_host_charset_name = nl_langinfo (CODESET);
820*5796c8dcSSimon Schubert   /* Solaris will return `646' here -- but the Solaris iconv then
821*5796c8dcSSimon Schubert      does not accept this.  */
822*5796c8dcSSimon Schubert   if (!strcmp (auto_host_charset_name, "646"))
823*5796c8dcSSimon Schubert     auto_host_charset_name = "ASCII";
824*5796c8dcSSimon Schubert   target_charset_name = auto_host_charset_name;
825*5796c8dcSSimon Schubert 
826*5796c8dcSSimon Schubert   set_be_le_names ();
827*5796c8dcSSimon Schubert #endif
828*5796c8dcSSimon Schubert #endif
829*5796c8dcSSimon Schubert 
830*5796c8dcSSimon Schubert   add_setshow_enum_cmd ("charset", class_support,
831*5796c8dcSSimon Schubert 			&charset_enum[1], &host_charset_name, _("\
832*5796c8dcSSimon Schubert Set the host and target character sets."), _("\
833*5796c8dcSSimon Schubert Show the host and target character sets."), _("\
834*5796c8dcSSimon Schubert The `host character set' is the one used by the system GDB is running on.\n\
835*5796c8dcSSimon Schubert The `target character set' is the one used by the program being debugged.\n\
836*5796c8dcSSimon Schubert You may only use supersets of ASCII for your host character set; GDB does\n\
837*5796c8dcSSimon Schubert not support any others.\n\
838*5796c8dcSSimon Schubert To see a list of the character sets GDB supports, type `set charset <TAB>'."),
839*5796c8dcSSimon Schubert 			/* Note that the sfunc below needs to set
840*5796c8dcSSimon Schubert 			   target_charset_name, because the 'set
841*5796c8dcSSimon Schubert 			   charset' command sets two variables.  */
842*5796c8dcSSimon Schubert 			set_charset_sfunc,
843*5796c8dcSSimon Schubert 			show_charset,
844*5796c8dcSSimon Schubert 			&setlist, &showlist);
845*5796c8dcSSimon Schubert 
846*5796c8dcSSimon Schubert   add_setshow_enum_cmd ("host-charset", class_support,
847*5796c8dcSSimon Schubert 			charset_enum, &host_charset_name, _("\
848*5796c8dcSSimon Schubert Set the host character set."), _("\
849*5796c8dcSSimon Schubert Show the host character set."), _("\
850*5796c8dcSSimon Schubert The `host character set' is the one used by the system GDB is running on.\n\
851*5796c8dcSSimon Schubert You may only use supersets of ASCII for your host character set; GDB does\n\
852*5796c8dcSSimon Schubert not support any others.\n\
853*5796c8dcSSimon Schubert To see a list of the character sets GDB supports, type `set host-charset <TAB>'."),
854*5796c8dcSSimon Schubert 			set_host_charset_sfunc,
855*5796c8dcSSimon Schubert 			show_host_charset_name,
856*5796c8dcSSimon Schubert 			&setlist, &showlist);
857*5796c8dcSSimon Schubert 
858*5796c8dcSSimon Schubert   add_setshow_enum_cmd ("target-charset", class_support,
859*5796c8dcSSimon Schubert 			&charset_enum[1], &target_charset_name, _("\
860*5796c8dcSSimon Schubert Set the target character set."), _("\
861*5796c8dcSSimon Schubert Show the target character set."), _("\
862*5796c8dcSSimon Schubert The `target character set' is the one used by the program being debugged.\n\
863*5796c8dcSSimon Schubert GDB translates characters and strings between the host and target\n\
864*5796c8dcSSimon Schubert character sets as needed.\n\
865*5796c8dcSSimon Schubert To see a list of the character sets GDB supports, type `set target-charset'<TAB>"),
866*5796c8dcSSimon Schubert 			set_target_charset_sfunc,
867*5796c8dcSSimon Schubert 			show_target_charset_name,
868*5796c8dcSSimon Schubert 			&setlist, &showlist);
869*5796c8dcSSimon Schubert 
870*5796c8dcSSimon Schubert   add_setshow_enum_cmd ("target-wide-charset", class_support,
871*5796c8dcSSimon Schubert 			&charset_enum[1], &target_wide_charset_name,
872*5796c8dcSSimon Schubert 			_("\
873*5796c8dcSSimon Schubert Set the target wide character set."), _("\
874*5796c8dcSSimon Schubert Show the target wide character set."), _("\
875*5796c8dcSSimon Schubert The `target wide character set' is the one used by the program being debugged.\n\
876*5796c8dcSSimon Schubert In particular it is the encoding used by `wchar_t'.\n\
877*5796c8dcSSimon Schubert GDB translates characters and strings between the host and target\n\
878*5796c8dcSSimon Schubert character sets as needed.\n\
879*5796c8dcSSimon Schubert To see a list of the character sets GDB supports, type\n\
880*5796c8dcSSimon Schubert `set target-wide-charset'<TAB>"),
881*5796c8dcSSimon Schubert 			set_target_wide_charset_sfunc,
882*5796c8dcSSimon Schubert 			show_target_wide_charset_name,
883*5796c8dcSSimon Schubert 			&setlist, &showlist);
884*5796c8dcSSimon Schubert }
885