Automatic date update in version.in
[binutils-gdb.git] / gdb / charset.c
blob4c1debef61433bcf90b0d9436cc80aebb7fb1890
1 /* Character set conversion support for GDB.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "charset.h"
21 #include "gdbcmd.h"
22 #include "gdbsupport/gdb_obstack.h"
23 #include "gdbsupport/gdb_wait.h"
24 #include "charset-list.h"
25 #include "gdbsupport/environ.h"
26 #include "arch-utils.h"
27 #include "gdbsupport/gdb_vecs.h"
28 #include <ctype.h>
30 #ifdef USE_WIN32API
31 #include <windows.h>
32 #endif
34 /* How GDB's character set support works
36 GDB has three global settings:
38 - The `current host character set' is the character set GDB should
39 use in talking to the user, and which (hopefully) the user's
40 terminal knows how to display properly. Most users should not
41 change this.
43 - The `current target character set' is the character set the
44 program being debugged uses.
46 - The `current target wide character set' is the wide character set
47 the program being debugged uses, that is, the encoding used for
48 wchar_t.
50 There are commands to set each of these, and mechanisms for
51 choosing reasonable default values. GDB has a global list of
52 character sets that it can use as its host or target character
53 sets.
55 The header file `charset.h' declares various functions that
56 different pieces of GDB need to perform tasks like:
58 - printing target strings and characters to the user's terminal
59 (mostly target->host conversions),
61 - building target-appropriate representations of strings and
62 characters the user enters in expressions (mostly host->target
63 conversions),
65 and so on.
67 To avoid excessive code duplication and maintenance efforts,
68 GDB simply requires a capable iconv function. Users on platforms
69 without a suitable iconv can use the GNU iconv library. */
72 #ifdef PHONY_ICONV
74 /* Provide a phony iconv that does as little as possible. Also,
75 arrange for there to be a single available character set. */
77 #undef GDB_DEFAULT_HOST_CHARSET
78 #ifdef USE_WIN32API
79 # define GDB_DEFAULT_HOST_CHARSET "CP1252"
80 #else
81 # define GDB_DEFAULT_HOST_CHARSET "ISO-8859-1"
82 #endif
83 #define GDB_DEFAULT_TARGET_CHARSET GDB_DEFAULT_HOST_CHARSET
84 #define GDB_DEFAULT_TARGET_WIDE_CHARSET "UTF-32"
85 #undef DEFAULT_CHARSET_NAMES
86 #define DEFAULT_CHARSET_NAMES GDB_DEFAULT_HOST_CHARSET ,
88 #undef iconv_t
89 #define iconv_t int
90 #undef iconv_open
91 #define iconv_open phony_iconv_open
92 #undef iconv
93 #define iconv phony_iconv
94 #undef iconv_close
95 #define iconv_close phony_iconv_close
97 #undef ICONV_CONST
98 #define ICONV_CONST const
100 /* We allow conversions from UTF-32, wchar_t, and the host charset.
101 We allow conversions to wchar_t and the host charset.
102 Return 1 if we are converting from UTF-32BE, 2 if from UTF32-LE,
103 0 otherwise. This is used as a flag in calls to iconv. */
105 static iconv_t
106 phony_iconv_open (const char *to, const char *from)
108 if (strcmp (to, "wchar_t") && strcmp (to, GDB_DEFAULT_HOST_CHARSET))
109 return -1;
111 if (!strcmp (from, "UTF-32BE") || !strcmp (from, "UTF-32"))
112 return 1;
114 if (!strcmp (from, "UTF-32LE"))
115 return 2;
117 if (strcmp (from, "wchar_t") && strcmp (from, GDB_DEFAULT_HOST_CHARSET))
118 return -1;
120 return 0;
123 static int
124 phony_iconv_close (iconv_t arg)
126 return 0;
129 static size_t
130 phony_iconv (iconv_t utf_flag, const char **inbuf, size_t *inbytesleft,
131 char **outbuf, size_t *outbytesleft)
133 if (utf_flag)
135 enum bfd_endian endian
136 = utf_flag == 1 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
137 while (*inbytesleft >= 4)
139 unsigned long c
140 = extract_unsigned_integer ((const gdb_byte *)*inbuf, 4, endian);
142 if (c >= 256)
144 errno = EILSEQ;
145 return -1;
147 if (*outbytesleft < 1)
149 errno = E2BIG;
150 return -1;
152 **outbuf = c & 0xff;
153 ++*outbuf;
154 --*outbytesleft;
156 *inbuf += 4;
157 *inbytesleft -= 4;
159 if (*inbytesleft)
161 /* Partial sequence on input. */
162 errno = EINVAL;
163 return -1;
166 else
168 /* In all other cases we simply copy input bytes to the
169 output. */
170 size_t amt = *inbytesleft;
172 if (amt > *outbytesleft)
173 amt = *outbytesleft;
174 memcpy (*outbuf, *inbuf, amt);
175 *inbuf += amt;
176 *outbuf += amt;
177 *inbytesleft -= amt;
178 *outbytesleft -= amt;
179 if (*inbytesleft)
181 errno = E2BIG;
182 return -1;
186 /* The number of non-reversible conversions -- but they were all
187 reversible. */
188 return 0;
191 #else /* PHONY_ICONV */
193 /* On systems that don't have EILSEQ, GNU iconv's iconv.h defines it
194 to ENOENT, while gnulib defines it to a different value. Always
195 map ENOENT to gnulib's EILSEQ, leaving callers agnostic. */
197 static size_t
198 gdb_iconv (iconv_t utf_flag, ICONV_CONST char **inbuf, size_t *inbytesleft,
199 char **outbuf, size_t *outbytesleft)
201 size_t ret;
203 ret = iconv (utf_flag, inbuf, inbytesleft, outbuf, outbytesleft);
204 if (errno == ENOENT)
205 errno = EILSEQ;
206 return ret;
209 #undef iconv
210 #define iconv gdb_iconv
212 #endif /* PHONY_ICONV */
215 /* The global lists of character sets and translations. */
218 #ifndef GDB_DEFAULT_TARGET_CHARSET
219 #define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
220 #endif
222 #ifndef GDB_DEFAULT_TARGET_WIDE_CHARSET
223 #define GDB_DEFAULT_TARGET_WIDE_CHARSET "UTF-32"
224 #endif
226 static const char *auto_host_charset_name = GDB_DEFAULT_HOST_CHARSET;
227 static const char *host_charset_name = "auto";
228 static void
229 show_host_charset_name (struct ui_file *file, int from_tty,
230 struct cmd_list_element *c,
231 const char *value)
233 if (!strcmp (value, "auto"))
234 gdb_printf (file,
235 _("The host character set is \"auto; currently %s\".\n"),
236 auto_host_charset_name);
237 else
238 gdb_printf (file, _("The host character set is \"%s\".\n"), value);
241 static const char *target_charset_name = "auto";
242 static void
243 show_target_charset_name (struct ui_file *file, int from_tty,
244 struct cmd_list_element *c, const char *value)
246 if (!strcmp (value, "auto"))
247 gdb_printf (file,
248 _("The target character set is \"auto; "
249 "currently %s\".\n"),
250 gdbarch_auto_charset (get_current_arch ()));
251 else
252 gdb_printf (file, _("The target character set is \"%s\".\n"),
253 value);
256 static const char *target_wide_charset_name = "auto";
257 static void
258 show_target_wide_charset_name (struct ui_file *file,
259 int from_tty,
260 struct cmd_list_element *c,
261 const char *value)
263 if (!strcmp (value, "auto"))
264 gdb_printf (file,
265 _("The target wide character set is \"auto; "
266 "currently %s\".\n"),
267 gdbarch_auto_wide_charset (get_current_arch ()));
268 else
269 gdb_printf (file, _("The target wide character set is \"%s\".\n"),
270 value);
273 static const char * const default_charset_names[] =
275 DEFAULT_CHARSET_NAMES
279 static const char * const *charset_enum;
282 /* If the target wide character set has big- or little-endian
283 variants, these are the corresponding names. */
284 static const char *target_wide_charset_be_name;
285 static const char *target_wide_charset_le_name;
287 /* The architecture for which the BE- and LE-names are valid. */
288 static struct gdbarch *be_le_arch;
290 /* A helper function which sets the target wide big- and little-endian
291 character set names, if possible. */
293 static void
294 set_be_le_names (struct gdbarch *gdbarch)
296 if (be_le_arch == gdbarch)
297 return;
298 be_le_arch = gdbarch;
300 #ifdef PHONY_ICONV
301 /* Match the wide charset names recognized by phony_iconv_open. */
302 target_wide_charset_le_name = "UTF-32LE";
303 target_wide_charset_be_name = "UTF-32BE";
304 #else
305 int i, len;
306 const char *target_wide;
308 target_wide_charset_le_name = NULL;
309 target_wide_charset_be_name = NULL;
311 target_wide = target_wide_charset_name;
312 if (!strcmp (target_wide, "auto"))
313 target_wide = gdbarch_auto_wide_charset (gdbarch);
315 len = strlen (target_wide);
316 for (i = 0; charset_enum[i]; ++i)
318 if (strncmp (target_wide, charset_enum[i], len))
319 continue;
320 if ((charset_enum[i][len] == 'B'
321 || charset_enum[i][len] == 'L')
322 && charset_enum[i][len + 1] == 'E'
323 && charset_enum[i][len + 2] == '\0')
325 if (charset_enum[i][len] == 'B')
326 target_wide_charset_be_name = charset_enum[i];
327 else
328 target_wide_charset_le_name = charset_enum[i];
331 # endif /* PHONY_ICONV */
334 /* 'Set charset', 'set host-charset', 'set target-charset', 'set
335 target-wide-charset', 'set charset' sfunc's. */
337 static void
338 validate (struct gdbarch *gdbarch)
340 iconv_t desc;
341 const char *host_cset = host_charset ();
342 const char *target_cset = target_charset (gdbarch);
343 const char *target_wide_cset = target_wide_charset_name;
345 if (!strcmp (target_wide_cset, "auto"))
346 target_wide_cset = gdbarch_auto_wide_charset (gdbarch);
348 desc = iconv_open (target_wide_cset, host_cset);
349 if (desc == (iconv_t) -1)
350 error (_("Cannot convert between character sets `%s' and `%s'"),
351 target_wide_cset, host_cset);
352 iconv_close (desc);
354 desc = iconv_open (target_cset, host_cset);
355 if (desc == (iconv_t) -1)
356 error (_("Cannot convert between character sets `%s' and `%s'"),
357 target_cset, host_cset);
358 iconv_close (desc);
360 /* Clear the cache. */
361 be_le_arch = NULL;
364 /* This is the sfunc for the 'set charset' command. */
365 static void
366 set_charset_sfunc (const char *charset, int from_tty,
367 struct cmd_list_element *c)
369 /* CAREFUL: set the target charset here as well. */
370 target_charset_name = host_charset_name;
371 validate (get_current_arch ());
374 /* 'set host-charset' command sfunc. We need a wrapper here because
375 the function needs to have a specific signature. */
376 static void
377 set_host_charset_sfunc (const char *charset, int from_tty,
378 struct cmd_list_element *c)
380 validate (get_current_arch ());
383 /* Wrapper for the 'set target-charset' command. */
384 static void
385 set_target_charset_sfunc (const char *charset, int from_tty,
386 struct cmd_list_element *c)
388 validate (get_current_arch ());
391 /* Wrapper for the 'set target-wide-charset' command. */
392 static void
393 set_target_wide_charset_sfunc (const char *charset, int from_tty,
394 struct cmd_list_element *c)
396 validate (get_current_arch ());
399 /* sfunc for the 'show charset' command. */
400 static void
401 show_charset (struct ui_file *file, int from_tty,
402 struct cmd_list_element *c,
403 const char *name)
405 show_host_charset_name (file, from_tty, c, host_charset_name);
406 show_target_charset_name (file, from_tty, c, target_charset_name);
407 show_target_wide_charset_name (file, from_tty, c,
408 target_wide_charset_name);
412 /* Accessor functions. */
414 const char *
415 host_charset (void)
417 if (!strcmp (host_charset_name, "auto"))
418 return auto_host_charset_name;
419 return host_charset_name;
422 const char *
423 target_charset (struct gdbarch *gdbarch)
425 if (!strcmp (target_charset_name, "auto"))
426 return gdbarch_auto_charset (gdbarch);
427 return target_charset_name;
430 const char *
431 target_wide_charset (struct gdbarch *gdbarch)
433 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
435 set_be_le_names (gdbarch);
436 if (byte_order == BFD_ENDIAN_BIG)
438 if (target_wide_charset_be_name)
439 return target_wide_charset_be_name;
441 else
443 if (target_wide_charset_le_name)
444 return target_wide_charset_le_name;
447 if (!strcmp (target_wide_charset_name, "auto"))
448 return gdbarch_auto_wide_charset (gdbarch);
450 return target_wide_charset_name;
454 /* Host character set management. For the time being, we assume that
455 the host character set is some superset of ASCII. */
457 char
458 host_letter_to_control_character (char c)
460 if (c == '?')
461 return 0177;
462 return c & 0237;
466 /* Public character management functions. */
468 class iconv_wrapper
470 public:
472 iconv_wrapper (const char *to, const char *from)
474 m_desc = iconv_open (to, from);
475 if (m_desc == (iconv_t) -1)
476 perror_with_name (_("Converting character sets"));
479 ~iconv_wrapper ()
481 iconv_close (m_desc);
484 size_t convert (ICONV_CONST char **inp, size_t *inleft, char **outp,
485 size_t *outleft)
487 return iconv (m_desc, inp, inleft, outp, outleft);
490 private:
492 iconv_t m_desc;
495 void
496 convert_between_encodings (const char *from, const char *to,
497 const gdb_byte *bytes, unsigned int num_bytes,
498 int width, struct obstack *output,
499 enum transliterations translit)
501 size_t inleft;
502 ICONV_CONST char *inp;
503 unsigned int space_request;
505 /* Often, the host and target charsets will be the same. */
506 if (!strcmp (from, to))
508 obstack_grow (output, bytes, num_bytes);
509 return;
512 iconv_wrapper desc (to, from);
514 inleft = num_bytes;
515 inp = (ICONV_CONST char *) bytes;
517 space_request = num_bytes;
519 while (inleft > 0)
521 char *outp;
522 size_t outleft, r;
523 int old_size;
525 old_size = obstack_object_size (output);
526 obstack_blank (output, space_request);
528 outp = (char *) obstack_base (output) + old_size;
529 outleft = space_request;
531 r = desc.convert (&inp, &inleft, &outp, &outleft);
533 /* Now make sure that the object on the obstack only includes
534 bytes we have converted. */
535 obstack_blank_fast (output, -(ssize_t) outleft);
537 if (r == (size_t) -1)
539 switch (errno)
541 case EILSEQ:
543 int i;
545 /* Invalid input sequence. */
546 if (translit == translit_none)
547 error (_("Could not convert character "
548 "to `%s' character set"), to);
550 /* We emit escape sequence for the bytes, skip them,
551 and try again. */
552 for (i = 0; i < width; ++i)
554 char octal[5];
556 xsnprintf (octal, sizeof (octal), "\\%.3o", *inp & 0xff);
557 obstack_grow_str (output, octal);
559 ++inp;
560 --inleft;
563 break;
565 case E2BIG:
566 /* We ran out of space in the output buffer. Make it
567 bigger next time around. */
568 space_request *= 2;
569 break;
571 case EINVAL:
572 /* Incomplete input sequence. FIXME: ought to report this
573 to the caller somehow. */
574 inleft = 0;
575 break;
577 default:
578 perror_with_name (_("Internal error while "
579 "converting character sets"));
587 /* Create a new iterator. */
588 wchar_iterator::wchar_iterator (const gdb_byte *input, size_t bytes,
589 const char *charset, size_t width)
590 : m_input (input),
591 m_bytes (bytes),
592 m_width (width),
593 m_out (1)
595 m_desc = iconv_open (INTERMEDIATE_ENCODING, charset);
596 if (m_desc == (iconv_t) -1)
597 perror_with_name (_("Converting character sets"));
600 wchar_iterator::~wchar_iterator ()
602 if (m_desc != (iconv_t) -1)
603 iconv_close (m_desc);
607 wchar_iterator::iterate (enum wchar_iterate_result *out_result,
608 gdb_wchar_t **out_chars,
609 const gdb_byte **ptr,
610 size_t *len)
612 size_t out_request;
614 /* Try to convert some characters. At first we try to convert just
615 a single character. The reason for this is that iconv does not
616 necessarily update its outgoing arguments when it encounters an
617 invalid input sequence -- but we want to reliably report this to
618 our caller so it can emit an escape sequence. */
619 out_request = 1;
620 while (m_bytes > 0)
622 ICONV_CONST char *inptr = (ICONV_CONST char *) m_input;
623 char *outptr = (char *) m_out.data ();
624 const gdb_byte *orig_inptr = m_input;
625 size_t orig_in = m_bytes;
626 size_t out_avail = out_request * sizeof (gdb_wchar_t);
627 size_t num;
628 size_t r = iconv (m_desc, &inptr, &m_bytes, &outptr, &out_avail);
630 m_input = (gdb_byte *) inptr;
632 if (r == (size_t) -1)
634 switch (errno)
636 case EILSEQ:
637 /* Invalid input sequence. We still might have
638 converted a character; if so, return it. */
639 if (out_avail < out_request * sizeof (gdb_wchar_t))
640 break;
642 /* Otherwise skip the first invalid character, and let
643 the caller know about it. */
644 *out_result = wchar_iterate_invalid;
645 *ptr = m_input;
646 *len = m_width;
647 m_input += m_width;
648 m_bytes -= m_width;
649 return 0;
651 case E2BIG:
652 /* We ran out of space. We still might have converted a
653 character; if so, return it. Otherwise, grow the
654 buffer and try again. */
655 if (out_avail < out_request * sizeof (gdb_wchar_t))
656 break;
658 ++out_request;
659 if (out_request > m_out.size ())
660 m_out.resize (out_request);
661 continue;
663 case EINVAL:
664 /* Incomplete input sequence. Let the caller know, and
665 arrange for future calls to see EOF. */
666 *out_result = wchar_iterate_incomplete;
667 *ptr = m_input;
668 *len = m_bytes;
669 m_bytes = 0;
670 return 0;
672 default:
673 perror_with_name (_("Internal error while "
674 "converting character sets"));
678 /* We converted something. */
679 num = out_request - out_avail / sizeof (gdb_wchar_t);
680 *out_result = wchar_iterate_ok;
681 *out_chars = m_out.data ();
682 *ptr = orig_inptr;
683 *len = orig_in - m_bytes;
684 return num;
687 /* Really done. */
688 *out_result = wchar_iterate_eof;
689 return -1;
692 struct charset_vector
694 ~charset_vector ()
696 /* Note that we do not call charset_vector::clear, which would also xfree
697 the elements. This destructor is only called after exit, at which point
698 those will be freed anyway on process exit, so not freeing them now is
699 not classified as a memory leak. OTOH, freeing them now might be
700 classified as a data race, because some worker thread might still be
701 accessing them. */
702 charsets.clear ();
705 void clear ()
707 for (char *c : charsets)
708 xfree (c);
710 charsets.clear ();
713 std::vector<char *> charsets;
716 static charset_vector charsets;
718 #ifdef PHONY_ICONV
720 static void
721 find_charset_names (void)
723 charsets.charsets.push_back (xstrdup (GDB_DEFAULT_HOST_CHARSET));
724 charsets.charsets.push_back (NULL);
727 #else /* PHONY_ICONV */
729 /* Sometimes, libiconv redefines iconvlist as libiconvlist -- but
730 provides different symbols in the static and dynamic libraries.
731 So, configure may see libiconvlist but not iconvlist. But, calling
732 iconvlist is the right thing to do and will work. Hence we do a
733 check here but unconditionally call iconvlist below. */
734 #if defined (HAVE_ICONVLIST) || defined (HAVE_LIBICONVLIST)
736 /* A helper function that adds some character sets to the vector of
737 all character sets. This is a callback function for iconvlist. */
739 static int
740 add_one (unsigned int count, const char *const *names, void *data)
742 unsigned int i;
744 for (i = 0; i < count; ++i)
745 charsets.charsets.push_back (xstrdup (names[i]));
747 return 0;
750 static void
751 find_charset_names (void)
753 iconvlist (add_one, NULL);
755 charsets.charsets.push_back (NULL);
758 #else
760 /* Return non-zero if LINE (output from iconv) should be ignored.
761 Older iconv programs (e.g. 2.2.2) include the human readable
762 introduction even when stdout is not a tty. Newer versions omit
763 the intro if stdout is not a tty. */
765 static int
766 ignore_line_p (const char *line)
768 /* This table is used to filter the output. If this text appears
769 anywhere in the line, it is ignored (strstr is used). */
770 static const char * const ignore_lines[] =
772 "The following",
773 "not necessarily",
774 "the FROM and TO",
775 "listed with several",
776 NULL
778 int i;
780 for (i = 0; ignore_lines[i] != NULL; ++i)
782 if (strstr (line, ignore_lines[i]) != NULL)
783 return 1;
786 return 0;
789 static void
790 find_charset_names (void)
792 struct pex_obj *child;
793 const char *args[3];
794 int err, status;
795 int fail = 1;
796 int flags;
797 gdb_environ iconv_env = gdb_environ::from_host_environ ();
798 char *iconv_program;
800 /* Older iconvs, e.g. 2.2.2, don't omit the intro text if stdout is
801 not a tty. We need to recognize it and ignore it. This text is
802 subject to translation, so force LANGUAGE=C. */
803 iconv_env.set ("LANGUAGE", "C");
804 iconv_env.set ("LC_ALL", "C");
806 child = pex_init (PEX_USE_PIPES, "iconv", NULL);
808 #ifdef ICONV_BIN
810 std::string iconv_dir = relocate_gdb_directory (ICONV_BIN,
811 ICONV_BIN_RELOCATABLE);
812 iconv_program
813 = concat (iconv_dir.c_str(), SLASH_STRING, "iconv", (char *) NULL);
815 #else
816 iconv_program = xstrdup ("iconv");
817 #endif
818 args[0] = iconv_program;
819 args[1] = "-l";
820 args[2] = NULL;
821 flags = PEX_STDERR_TO_STDOUT;
822 #ifndef ICONV_BIN
823 flags |= PEX_SEARCH;
824 #endif
825 /* Note that we simply ignore errors here. */
826 if (!pex_run_in_environment (child, flags,
827 args[0], const_cast<char **> (args),
828 iconv_env.envp (),
829 NULL, NULL, &err))
831 FILE *in = pex_read_output (child, 0);
833 /* POSIX says that iconv -l uses an unspecified format. We
834 parse the glibc and libiconv formats; feel free to add others
835 as needed. */
837 while (in != NULL && !feof (in))
839 /* The size of buf is chosen arbitrarily. */
840 char buf[1024];
841 char *start, *r;
842 int len;
844 r = fgets (buf, sizeof (buf), in);
845 if (!r)
846 break;
847 len = strlen (r);
848 if (len <= 3)
849 continue;
850 if (ignore_line_p (r))
851 continue;
853 /* Strip off the newline. */
854 --len;
855 /* Strip off one or two '/'s. glibc will print lines like
856 "8859_7//", but also "10646-1:1993/UCS4/". */
857 if (buf[len - 1] == '/')
858 --len;
859 if (buf[len - 1] == '/')
860 --len;
861 buf[len] = '\0';
863 /* libiconv will print multiple entries per line, separated
864 by spaces. Older iconvs will print multiple entries per
865 line, indented by two spaces, and separated by ", "
866 (i.e. the human readable form). */
867 start = buf;
868 while (1)
870 int keep_going;
871 char *p;
873 /* Skip leading blanks. */
874 for (p = start; *p && *p == ' '; ++p)
876 start = p;
877 /* Find the next space, comma, or end-of-line. */
878 for ( ; *p && *p != ' ' && *p != ','; ++p)
880 /* Ignore an empty result. */
881 if (p == start)
882 break;
883 keep_going = *p;
884 *p = '\0';
885 charsets.charsets.push_back (xstrdup (start));
886 if (!keep_going)
887 break;
888 /* Skip any extra spaces. */
889 for (start = p + 1; *start && *start == ' '; ++start)
894 if (pex_get_status (child, 1, &status)
895 && WIFEXITED (status) && !WEXITSTATUS (status))
896 fail = 0;
900 xfree (iconv_program);
901 pex_free (child);
903 if (fail)
905 /* Some error occurred, so drop the vector. */
906 charsets.clear ();
908 else
909 charsets.charsets.push_back (NULL);
912 #endif /* HAVE_ICONVLIST || HAVE_LIBICONVLIST */
913 #endif /* PHONY_ICONV */
915 /* The "auto" target charset used by default_auto_charset. */
916 static const char *auto_target_charset_name = GDB_DEFAULT_TARGET_CHARSET;
918 const char *
919 default_auto_charset (void)
921 return auto_target_charset_name;
924 const char *
925 default_auto_wide_charset (void)
927 return GDB_DEFAULT_TARGET_WIDE_CHARSET;
931 #ifdef USE_INTERMEDIATE_ENCODING_FUNCTION
932 /* Macro used for UTF or UCS endianness suffix. */
933 #if WORDS_BIGENDIAN
934 #define ENDIAN_SUFFIX "BE"
935 #else
936 #define ENDIAN_SUFFIX "LE"
937 #endif
939 /* GDB cannot handle strings correctly if this size is different. */
941 static_assert (sizeof (gdb_wchar_t) == 2 || sizeof (gdb_wchar_t) == 4);
943 /* intermediate_encoding returns the charset used internally by
944 GDB to convert between target and host encodings. As the test above
945 compiled, sizeof (gdb_wchar_t) is either 2 or 4 bytes.
946 UTF-16/32 is tested first, UCS-2/4 is tested as a second option,
947 otherwise an error is generated. */
949 const char *
950 intermediate_encoding (void)
952 iconv_t desc;
953 static const char *stored_result = NULL;
954 gdb::unique_xmalloc_ptr<char> result;
956 if (stored_result)
957 return stored_result;
958 result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8),
959 ENDIAN_SUFFIX);
960 /* Check that the name is supported by iconv_open. */
961 desc = iconv_open (result.get (), host_charset ());
962 if (desc != (iconv_t) -1)
964 iconv_close (desc);
965 stored_result = result.release ();
966 return stored_result;
968 /* Second try, with UCS-2 type. */
969 result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t),
970 ENDIAN_SUFFIX);
971 /* Check that the name is supported by iconv_open. */
972 desc = iconv_open (result.get (), host_charset ());
973 if (desc != (iconv_t) -1)
975 iconv_close (desc);
976 stored_result = result.release ();
977 return stored_result;
979 /* No valid charset found, generate error here. */
980 error (_("Unable to find a valid charset for string conversions"));
983 #endif /* USE_INTERMEDIATE_ENCODING_FUNCTION */
985 void _initialize_charset ();
986 void
987 _initialize_charset ()
989 /* The first element is always "auto". */
990 charsets.charsets.push_back (xstrdup ("auto"));
991 find_charset_names ();
993 if (charsets.charsets.size () > 1)
994 charset_enum = (const char * const *) charsets.charsets.data ();
995 else
996 charset_enum = default_charset_names;
998 #ifndef PHONY_ICONV
999 #ifdef HAVE_LANGINFO_CODESET
1000 /* The result of nl_langinfo may be overwritten later. This may
1001 leak a little memory, if the user later changes the host charset,
1002 but that doesn't matter much. */
1003 auto_host_charset_name = xstrdup (nl_langinfo (CODESET));
1004 /* Solaris will return `646' here -- but the Solaris iconv then does
1005 not accept this. Darwin (and maybe FreeBSD) may return "" here,
1006 which GNU libiconv doesn't like (infinite loop). */
1007 if (!strcmp (auto_host_charset_name, "646") || !*auto_host_charset_name)
1008 auto_host_charset_name = "ASCII";
1009 auto_target_charset_name = auto_host_charset_name;
1010 #elif defined (USE_WIN32API)
1012 /* "CP" + x<=5 digits + paranoia. */
1013 static char w32_host_default_charset[16];
1015 snprintf (w32_host_default_charset, sizeof w32_host_default_charset,
1016 "CP%d", GetACP());
1017 auto_host_charset_name = w32_host_default_charset;
1018 auto_target_charset_name = auto_host_charset_name;
1020 #endif
1021 #endif
1023 /* Recall that the first element is always "auto". */
1024 host_charset_name = charset_enum[0];
1025 gdb_assert (strcmp (host_charset_name, "auto") == 0);
1026 add_setshow_enum_cmd ("charset", class_support,
1027 charset_enum, &host_charset_name, _("\
1028 Set the host and target character sets."), _("\
1029 Show the host and target character sets."), _("\
1030 The `host character set' is the one used by the system GDB is running on.\n\
1031 The `target character set' is the one used by the program being debugged.\n\
1032 You may only use supersets of ASCII for your host character set; GDB does\n\
1033 not support any others.\n\
1034 To see a list of the character sets GDB supports, type `set charset <TAB>'."),
1035 /* Note that the sfunc below needs to set
1036 target_charset_name, because the 'set
1037 charset' command sets two variables. */
1038 set_charset_sfunc,
1039 show_charset,
1040 &setlist, &showlist);
1042 add_setshow_enum_cmd ("host-charset", class_support,
1043 charset_enum, &host_charset_name, _("\
1044 Set the host character set."), _("\
1045 Show the host character set."), _("\
1046 The `host character set' is the one used by the system GDB is running on.\n\
1047 You may only use supersets of ASCII for your host character set; GDB does\n\
1048 not support any others.\n\
1049 To see a list of the character sets GDB supports, type `set host-charset <TAB>'."),
1050 set_host_charset_sfunc,
1051 show_host_charset_name,
1052 &setlist, &showlist);
1054 /* Recall that the first element is always "auto". */
1055 target_charset_name = charset_enum[0];
1056 gdb_assert (strcmp (target_charset_name, "auto") == 0);
1057 add_setshow_enum_cmd ("target-charset", class_support,
1058 charset_enum, &target_charset_name, _("\
1059 Set the target character set."), _("\
1060 Show the target character set."), _("\
1061 The `target character set' is the one used by the program being debugged.\n\
1062 GDB translates characters and strings between the host and target\n\
1063 character sets as needed.\n\
1064 To see a list of the character sets GDB supports, type `set target-charset'<TAB>"),
1065 set_target_charset_sfunc,
1066 show_target_charset_name,
1067 &setlist, &showlist);
1069 /* Recall that the first element is always "auto". */
1070 target_wide_charset_name = charset_enum[0];
1071 gdb_assert (strcmp (target_wide_charset_name, "auto") == 0);
1072 add_setshow_enum_cmd ("target-wide-charset", class_support,
1073 charset_enum, &target_wide_charset_name,
1074 _("\
1075 Set the target wide character set."), _("\
1076 Show the target wide character set."), _("\
1077 The `target wide character set' is the one used by the program being debugged.\
1078 \nIn particular it is the encoding used by `wchar_t'.\n\
1079 GDB translates characters and strings between the host and target\n\
1080 character sets as needed.\n\
1081 To see a list of the character sets GDB supports, type\n\
1082 `set target-wide-charset'<TAB>"),
1083 set_target_wide_charset_sfunc,
1084 show_target_wide_charset_name,
1085 &setlist, &showlist);