Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / charset.c
... / ...
CommitLineData
1/* Character set conversion support for GDB.
2
3 Copyright (C) 2001-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21
22/* Standard C includes. */
23#include <ctype.h>
24
25/* Local non-gdb includes. */
26#include "arch-utils.h"
27#include "charset-list.h"
28#include "charset.h"
29#include "common/environ.h"
30#include "common/gdb_vecs.h"
31#include "common/gdb_wait.h"
32#include "common/vec.h"
33#include "gdb_obstack.h"
34#include "gdbcmd.h"
35
36#ifdef USE_WIN32API
37#include <windows.h>
38#endif
39\f
40/* How GDB's character set support works
41
42 GDB has three global settings:
43
44 - The `current host character set' is the character set GDB should
45 use in talking to the user, and which (hopefully) the user's
46 terminal knows how to display properly. Most users should not
47 change this.
48
49 - The `current target character set' is the character set the
50 program being debugged uses.
51
52 - The `current target wide character set' is the wide character set
53 the program being debugged uses, that is, the encoding used for
54 wchar_t.
55
56 There are commands to set each of these, and mechanisms for
57 choosing reasonable default values. GDB has a global list of
58 character sets that it can use as its host or target character
59 sets.
60
61 The header file `charset.h' declares various functions that
62 different pieces of GDB need to perform tasks like:
63
64 - printing target strings and characters to the user's terminal
65 (mostly target->host conversions),
66
67 - building target-appropriate representations of strings and
68 characters the user enters in expressions (mostly host->target
69 conversions),
70
71 and so on.
72
73 To avoid excessive code duplication and maintenance efforts,
74 GDB simply requires a capable iconv function. Users on platforms
75 without a suitable iconv can use the GNU iconv library. */
76
77\f
78#ifdef PHONY_ICONV
79
80/* Provide a phony iconv that does as little as possible. Also,
81 arrange for there to be a single available character set. */
82
83#undef GDB_DEFAULT_HOST_CHARSET
84#ifdef USE_WIN32API
85# define GDB_DEFAULT_HOST_CHARSET "CP1252"
86#else
87# define GDB_DEFAULT_HOST_CHARSET "ISO-8859-1"
88#endif
89#define GDB_DEFAULT_TARGET_CHARSET GDB_DEFAULT_HOST_CHARSET
90#define GDB_DEFAULT_TARGET_WIDE_CHARSET "UTF-32"
91#undef DEFAULT_CHARSET_NAMES
92#define DEFAULT_CHARSET_NAMES GDB_DEFAULT_HOST_CHARSET ,
93
94#undef iconv_t
95#define iconv_t int
96#undef iconv_open
97#define iconv_open phony_iconv_open
98#undef iconv
99#define iconv phony_iconv
100#undef iconv_close
101#define iconv_close phony_iconv_close
102
103#undef ICONV_CONST
104#define ICONV_CONST const
105
106/* We allow conversions from UTF-32, wchar_t, and the host charset.
107 We allow conversions to wchar_t and the host charset.
108 Return 1 if we are converting from UTF-32BE, 2 if from UTF32-LE,
109 0 otherwise. This is used as a flag in calls to iconv. */
110
111static iconv_t
112phony_iconv_open (const char *to, const char *from)
113{
114 if (strcmp (to, "wchar_t") && strcmp (to, GDB_DEFAULT_HOST_CHARSET))
115 return -1;
116
117 if (!strcmp (from, "UTF-32BE") || !strcmp (from, "UTF-32"))
118 return 1;
119
120 if (!strcmp (from, "UTF-32LE"))
121 return 2;
122
123 if (strcmp (from, "wchar_t") && strcmp (from, GDB_DEFAULT_HOST_CHARSET))
124 return -1;
125
126 return 0;
127}
128
129static int
130phony_iconv_close (iconv_t arg)
131{
132 return 0;
133}
134
135static size_t
136phony_iconv (iconv_t utf_flag, const char **inbuf, size_t *inbytesleft,
137 char **outbuf, size_t *outbytesleft)
138{
139 if (utf_flag)
140 {
141 enum bfd_endian endian
142 = utf_flag == 1 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
143 while (*inbytesleft >= 4)
144 {
145 unsigned long c
146 = extract_unsigned_integer ((const gdb_byte *)*inbuf, 4, endian);
147
148 if (c >= 256)
149 {
150 errno = EILSEQ;
151 return -1;
152 }
153 if (*outbytesleft < 1)
154 {
155 errno = E2BIG;
156 return -1;
157 }
158 **outbuf = c & 0xff;
159 ++*outbuf;
160 --*outbytesleft;
161
162 *inbuf += 4;
163 *inbytesleft -= 4;
164 }
165 if (*inbytesleft)
166 {
167 /* Partial sequence on input. */
168 errno = EINVAL;
169 return -1;
170 }
171 }
172 else
173 {
174 /* In all other cases we simply copy input bytes to the
175 output. */
176 size_t amt = *inbytesleft;
177
178 if (amt > *outbytesleft)
179 amt = *outbytesleft;
180 memcpy (*outbuf, *inbuf, amt);
181 *inbuf += amt;
182 *outbuf += amt;
183 *inbytesleft -= amt;
184 *outbytesleft -= amt;
185 if (*inbytesleft)
186 {
187 errno = E2BIG;
188 return -1;
189 }
190 }
191
192 /* The number of non-reversible conversions -- but they were all
193 reversible. */
194 return 0;
195}
196
197#else /* PHONY_ICONV */
198
199/* On systems that don't have EILSEQ, GNU iconv's iconv.h defines it
200 to ENOENT, while gnulib defines it to a different value. Always
201 map ENOENT to gnulib's EILSEQ, leaving callers agnostic. */
202
203static size_t
204gdb_iconv (iconv_t utf_flag, ICONV_CONST char **inbuf, size_t *inbytesleft,
205 char **outbuf, size_t *outbytesleft)
206{
207 size_t ret;
208
209 ret = iconv (utf_flag, inbuf, inbytesleft, outbuf, outbytesleft);
210 if (errno == ENOENT)
211 errno = EILSEQ;
212 return ret;
213}
214
215#undef iconv
216#define iconv gdb_iconv
217
218#endif /* PHONY_ICONV */
219
220\f
221/* The global lists of character sets and translations. */
222
223
224#ifndef GDB_DEFAULT_TARGET_CHARSET
225#define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
226#endif
227
228#ifndef GDB_DEFAULT_TARGET_WIDE_CHARSET
229#define GDB_DEFAULT_TARGET_WIDE_CHARSET "UTF-32"
230#endif
231
232static const char *auto_host_charset_name = GDB_DEFAULT_HOST_CHARSET;
233static const char *host_charset_name = "auto";
234static void
235show_host_charset_name (struct ui_file *file, int from_tty,
236 struct cmd_list_element *c,
237 const char *value)
238{
239 if (!strcmp (value, "auto"))
240 fprintf_filtered (file,
241 _("The host character set is \"auto; currently %s\".\n"),
242 auto_host_charset_name);
243 else
244 fprintf_filtered (file, _("The host character set is \"%s\".\n"), value);
245}
246
247static const char *target_charset_name = "auto";
248static void
249show_target_charset_name (struct ui_file *file, int from_tty,
250 struct cmd_list_element *c, const char *value)
251{
252 if (!strcmp (value, "auto"))
253 fprintf_filtered (file,
254 _("The target character set is \"auto; "
255 "currently %s\".\n"),
256 gdbarch_auto_charset (get_current_arch ()));
257 else
258 fprintf_filtered (file, _("The target character set is \"%s\".\n"),
259 value);
260}
261
262static const char *target_wide_charset_name = "auto";
263static void
264show_target_wide_charset_name (struct ui_file *file,
265 int from_tty,
266 struct cmd_list_element *c,
267 const char *value)
268{
269 if (!strcmp (value, "auto"))
270 fprintf_filtered (file,
271 _("The target wide character set is \"auto; "
272 "currently %s\".\n"),
273 gdbarch_auto_wide_charset (get_current_arch ()));
274 else
275 fprintf_filtered (file, _("The target wide character set is \"%s\".\n"),
276 value);
277}
278
279static const char *default_charset_names[] =
280{
281 DEFAULT_CHARSET_NAMES
282 0
283};
284
285static const char **charset_enum;
286
287\f
288/* If the target wide character set has big- or little-endian
289 variants, these are the corresponding names. */
290static const char *target_wide_charset_be_name;
291static const char *target_wide_charset_le_name;
292
293/* The architecture for which the BE- and LE-names are valid. */
294static struct gdbarch *be_le_arch;
295
296/* A helper function which sets the target wide big- and little-endian
297 character set names, if possible. */
298
299static void
300set_be_le_names (struct gdbarch *gdbarch)
301{
302 if (be_le_arch == gdbarch)
303 return;
304 be_le_arch = gdbarch;
305
306#ifdef PHONY_ICONV
307 /* Match the wide charset names recognized by phony_iconv_open. */
308 target_wide_charset_le_name = "UTF-32LE";
309 target_wide_charset_be_name = "UTF-32BE";
310#else
311 int i, len;
312 const char *target_wide;
313
314 target_wide_charset_le_name = NULL;
315 target_wide_charset_be_name = NULL;
316
317 target_wide = target_wide_charset_name;
318 if (!strcmp (target_wide, "auto"))
319 target_wide = gdbarch_auto_wide_charset (gdbarch);
320
321 len = strlen (target_wide);
322 for (i = 0; charset_enum[i]; ++i)
323 {
324 if (strncmp (target_wide, charset_enum[i], len))
325 continue;
326 if ((charset_enum[i][len] == 'B'
327 || charset_enum[i][len] == 'L')
328 && charset_enum[i][len + 1] == 'E'
329 && charset_enum[i][len + 2] == '\0')
330 {
331 if (charset_enum[i][len] == 'B')
332 target_wide_charset_be_name = charset_enum[i];
333 else
334 target_wide_charset_le_name = charset_enum[i];
335 }
336 }
337# endif /* PHONY_ICONV */
338}
339
340/* 'Set charset', 'set host-charset', 'set target-charset', 'set
341 target-wide-charset', 'set charset' sfunc's. */
342
343static void
344validate (struct gdbarch *gdbarch)
345{
346 iconv_t desc;
347 const char *host_cset = host_charset ();
348 const char *target_cset = target_charset (gdbarch);
349 const char *target_wide_cset = target_wide_charset_name;
350
351 if (!strcmp (target_wide_cset, "auto"))
352 target_wide_cset = gdbarch_auto_wide_charset (gdbarch);
353
354 desc = iconv_open (target_wide_cset, host_cset);
355 if (desc == (iconv_t) -1)
356 error (_("Cannot convert between character sets `%s' and `%s'"),
357 target_wide_cset, host_cset);
358 iconv_close (desc);
359
360 desc = iconv_open (target_cset, host_cset);
361 if (desc == (iconv_t) -1)
362 error (_("Cannot convert between character sets `%s' and `%s'"),
363 target_cset, host_cset);
364 iconv_close (desc);
365
366 /* Clear the cache. */
367 be_le_arch = NULL;
368}
369
370/* This is the sfunc for the 'set charset' command. */
371static void
372set_charset_sfunc (const char *charset, int from_tty,
373 struct cmd_list_element *c)
374{
375 /* CAREFUL: set the target charset here as well. */
376 target_charset_name = host_charset_name;
377 validate (get_current_arch ());
378}
379
380/* 'set host-charset' command sfunc. We need a wrapper here because
381 the function needs to have a specific signature. */
382static void
383set_host_charset_sfunc (const char *charset, int from_tty,
384 struct cmd_list_element *c)
385{
386 validate (get_current_arch ());
387}
388
389/* Wrapper for the 'set target-charset' command. */
390static void
391set_target_charset_sfunc (const char *charset, int from_tty,
392 struct cmd_list_element *c)
393{
394 validate (get_current_arch ());
395}
396
397/* Wrapper for the 'set target-wide-charset' command. */
398static void
399set_target_wide_charset_sfunc (const char *charset, int from_tty,
400 struct cmd_list_element *c)
401{
402 validate (get_current_arch ());
403}
404
405/* sfunc for the 'show charset' command. */
406static void
407show_charset (struct ui_file *file, int from_tty,
408 struct cmd_list_element *c,
409 const char *name)
410{
411 show_host_charset_name (file, from_tty, c, host_charset_name);
412 show_target_charset_name (file, from_tty, c, target_charset_name);
413 show_target_wide_charset_name (file, from_tty, c,
414 target_wide_charset_name);
415}
416
417\f
418/* Accessor functions. */
419
420const char *
421host_charset (void)
422{
423 if (!strcmp (host_charset_name, "auto"))
424 return auto_host_charset_name;
425 return host_charset_name;
426}
427
428const char *
429target_charset (struct gdbarch *gdbarch)
430{
431 if (!strcmp (target_charset_name, "auto"))
432 return gdbarch_auto_charset (gdbarch);
433 return target_charset_name;
434}
435
436const char *
437target_wide_charset (struct gdbarch *gdbarch)
438{
439 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
440
441 set_be_le_names (gdbarch);
442 if (byte_order == BFD_ENDIAN_BIG)
443 {
444 if (target_wide_charset_be_name)
445 return target_wide_charset_be_name;
446 }
447 else
448 {
449 if (target_wide_charset_le_name)
450 return target_wide_charset_le_name;
451 }
452
453 if (!strcmp (target_wide_charset_name, "auto"))
454 return gdbarch_auto_wide_charset (gdbarch);
455
456 return target_wide_charset_name;
457}
458
459\f
460/* Host character set management. For the time being, we assume that
461 the host character set is some superset of ASCII. */
462
463char
464host_letter_to_control_character (char c)
465{
466 if (c == '?')
467 return 0177;
468 return c & 0237;
469}
470
471/* Convert a host character, C, to its hex value. C must already have
472 been validated using isxdigit. */
473
474int
475host_hex_value (char c)
476{
477 if (isdigit (c))
478 return c - '0';
479 if (c >= 'a' && c <= 'f')
480 return 10 + c - 'a';
481 gdb_assert (c >= 'A' && c <= 'F');
482 return 10 + c - 'A';
483}
484
485\f
486/* Public character management functions. */
487
488class iconv_wrapper
489{
490public:
491
492 iconv_wrapper (const char *to, const char *from)
493 {
494 m_desc = iconv_open (to, from);
495 if (m_desc == (iconv_t) -1)
496 perror_with_name (_("Converting character sets"));
497 }
498
499 ~iconv_wrapper ()
500 {
501 iconv_close (m_desc);
502 }
503
504 size_t convert (ICONV_CONST char **inp, size_t *inleft, char **outp,
505 size_t *outleft)
506 {
507 return iconv (m_desc, inp, inleft, outp, outleft);
508 }
509
510private:
511
512 iconv_t m_desc;
513};
514
515void
516convert_between_encodings (const char *from, const char *to,
517 const gdb_byte *bytes, unsigned int num_bytes,
518 int width, struct obstack *output,
519 enum transliterations translit)
520{
521 size_t inleft;
522 ICONV_CONST char *inp;
523 unsigned int space_request;
524
525 /* Often, the host and target charsets will be the same. */
526 if (!strcmp (from, to))
527 {
528 obstack_grow (output, bytes, num_bytes);
529 return;
530 }
531
532 iconv_wrapper desc (to, from);
533
534 inleft = num_bytes;
535 inp = (ICONV_CONST char *) bytes;
536
537 space_request = num_bytes;
538
539 while (inleft > 0)
540 {
541 char *outp;
542 size_t outleft, r;
543 int old_size;
544
545 old_size = obstack_object_size (output);
546 obstack_blank (output, space_request);
547
548 outp = (char *) obstack_base (output) + old_size;
549 outleft = space_request;
550
551 r = desc.convert (&inp, &inleft, &outp, &outleft);
552
553 /* Now make sure that the object on the obstack only includes
554 bytes we have converted. */
555 obstack_blank_fast (output, -(ssize_t) outleft);
556
557 if (r == (size_t) -1)
558 {
559 switch (errno)
560 {
561 case EILSEQ:
562 {
563 int i;
564
565 /* Invalid input sequence. */
566 if (translit == translit_none)
567 error (_("Could not convert character "
568 "to `%s' character set"), to);
569
570 /* We emit escape sequence for the bytes, skip them,
571 and try again. */
572 for (i = 0; i < width; ++i)
573 {
574 char octal[5];
575
576 xsnprintf (octal, sizeof (octal), "\\%.3o", *inp & 0xff);
577 obstack_grow_str (output, octal);
578
579 ++inp;
580 --inleft;
581 }
582 }
583 break;
584
585 case E2BIG:
586 /* We ran out of space in the output buffer. Make it
587 bigger next time around. */
588 space_request *= 2;
589 break;
590
591 case EINVAL:
592 /* Incomplete input sequence. FIXME: ought to report this
593 to the caller somehow. */
594 inleft = 0;
595 break;
596
597 default:
598 perror_with_name (_("Internal error while "
599 "converting character sets"));
600 }
601 }
602 }
603}
604
605\f
606
607/* Create a new iterator. */
608wchar_iterator::wchar_iterator (const gdb_byte *input, size_t bytes,
609 const char *charset, size_t width)
610: m_input (input),
611 m_bytes (bytes),
612 m_width (width),
613 m_out (1)
614{
615 m_desc = iconv_open (INTERMEDIATE_ENCODING, charset);
616 if (m_desc == (iconv_t) -1)
617 perror_with_name (_("Converting character sets"));
618}
619
620wchar_iterator::~wchar_iterator ()
621{
622 if (m_desc != (iconv_t) -1)
623 iconv_close (m_desc);
624}
625
626int
627wchar_iterator::iterate (enum wchar_iterate_result *out_result,
628 gdb_wchar_t **out_chars,
629 const gdb_byte **ptr,
630 size_t *len)
631{
632 size_t out_request;
633
634 /* Try to convert some characters. At first we try to convert just
635 a single character. The reason for this is that iconv does not
636 necessarily update its outgoing arguments when it encounters an
637 invalid input sequence -- but we want to reliably report this to
638 our caller so it can emit an escape sequence. */
639 out_request = 1;
640 while (m_bytes > 0)
641 {
642 ICONV_CONST char *inptr = (ICONV_CONST char *) m_input;
643 char *outptr = (char *) m_out.data ();
644 const gdb_byte *orig_inptr = m_input;
645 size_t orig_in = m_bytes;
646 size_t out_avail = out_request * sizeof (gdb_wchar_t);
647 size_t num;
648 size_t r = iconv (m_desc, &inptr, &m_bytes, &outptr, &out_avail);
649
650 m_input = (gdb_byte *) inptr;
651
652 if (r == (size_t) -1)
653 {
654 switch (errno)
655 {
656 case EILSEQ:
657 /* Invalid input sequence. We still might have
658 converted a character; if so, return it. */
659 if (out_avail < out_request * sizeof (gdb_wchar_t))
660 break;
661
662 /* Otherwise skip the first invalid character, and let
663 the caller know about it. */
664 *out_result = wchar_iterate_invalid;
665 *ptr = m_input;
666 *len = m_width;
667 m_input += m_width;
668 m_bytes -= m_width;
669 return 0;
670
671 case E2BIG:
672 /* We ran out of space. We still might have converted a
673 character; if so, return it. Otherwise, grow the
674 buffer and try again. */
675 if (out_avail < out_request * sizeof (gdb_wchar_t))
676 break;
677
678 ++out_request;
679 if (out_request > m_out.size ())
680 m_out.resize (out_request);
681 continue;
682
683 case EINVAL:
684 /* Incomplete input sequence. Let the caller know, and
685 arrange for future calls to see EOF. */
686 *out_result = wchar_iterate_incomplete;
687 *ptr = m_input;
688 *len = m_bytes;
689 m_bytes = 0;
690 return 0;
691
692 default:
693 perror_with_name (_("Internal error while "
694 "converting character sets"));
695 }
696 }
697
698 /* We converted something. */
699 num = out_request - out_avail / sizeof (gdb_wchar_t);
700 *out_result = wchar_iterate_ok;
701 *out_chars = m_out.data ();
702 *ptr = orig_inptr;
703 *len = orig_in - m_bytes;
704 return num;
705 }
706
707 /* Really done. */
708 *out_result = wchar_iterate_eof;
709 return -1;
710}
711
712struct charset_vector
713{
714 ~charset_vector ()
715 {
716 clear ();
717 }
718
719 void clear ()
720 {
721 for (char *c : charsets)
722 xfree (c);
723
724 charsets.clear ();
725 }
726
727 std::vector<char *> charsets;
728};
729
730static charset_vector charsets;
731
732#ifdef PHONY_ICONV
733
734static void
735find_charset_names (void)
736{
737 charsets.charsets.push_back (xstrdup (GDB_DEFAULT_HOST_CHARSET));
738 charsets.charsets.push_back (NULL);
739}
740
741#else /* PHONY_ICONV */
742
743/* Sometimes, libiconv redefines iconvlist as libiconvlist -- but
744 provides different symbols in the static and dynamic libraries.
745 So, configure may see libiconvlist but not iconvlist. But, calling
746 iconvlist is the right thing to do and will work. Hence we do a
747 check here but unconditionally call iconvlist below. */
748#if defined (HAVE_ICONVLIST) || defined (HAVE_LIBICONVLIST)
749
750/* A helper function that adds some character sets to the vector of
751 all character sets. This is a callback function for iconvlist. */
752
753static int
754add_one (unsigned int count, const char *const *names, void *data)
755{
756 unsigned int i;
757
758 for (i = 0; i < count; ++i)
759 charsets.charsets.push_back (xstrdup (names[i]));
760
761 return 0;
762}
763
764static void
765find_charset_names (void)
766{
767 iconvlist (add_one, NULL);
768
769 charsets.charsets.push_back (NULL);
770}
771
772#else
773
774/* Return non-zero if LINE (output from iconv) should be ignored.
775 Older iconv programs (e.g. 2.2.2) include the human readable
776 introduction even when stdout is not a tty. Newer versions omit
777 the intro if stdout is not a tty. */
778
779static int
780ignore_line_p (const char *line)
781{
782 /* This table is used to filter the output. If this text appears
783 anywhere in the line, it is ignored (strstr is used). */
784 static const char * const ignore_lines[] =
785 {
786 "The following",
787 "not necessarily",
788 "the FROM and TO",
789 "listed with several",
790 NULL
791 };
792 int i;
793
794 for (i = 0; ignore_lines[i] != NULL; ++i)
795 {
796 if (strstr (line, ignore_lines[i]) != NULL)
797 return 1;
798 }
799
800 return 0;
801}
802
803static void
804find_charset_names (void)
805{
806 struct pex_obj *child;
807 const char *args[3];
808 int err, status;
809 int fail = 1;
810 int flags;
811 gdb_environ iconv_env = gdb_environ::from_host_environ ();
812 char *iconv_program;
813
814 /* Older iconvs, e.g. 2.2.2, don't omit the intro text if stdout is
815 not a tty. We need to recognize it and ignore it. This text is
816 subject to translation, so force LANGUAGE=C. */
817 iconv_env.set ("LANGUAGE", "C");
818 iconv_env.set ("LC_ALL", "C");
819
820 child = pex_init (PEX_USE_PIPES, "iconv", NULL);
821
822#ifdef ICONV_BIN
823 {
824 char *iconv_dir = relocate_gdb_directory (ICONV_BIN,
825 ICONV_BIN_RELOCATABLE);
826 iconv_program = concat (iconv_dir, SLASH_STRING, "iconv", NULL);
827 xfree (iconv_dir);
828 }
829#else
830 iconv_program = xstrdup ("iconv");
831#endif
832 args[0] = iconv_program;
833 args[1] = "-l";
834 args[2] = NULL;
835 flags = PEX_STDERR_TO_STDOUT;
836#ifndef ICONV_BIN
837 flags |= PEX_SEARCH;
838#endif
839 /* Note that we simply ignore errors here. */
840 if (!pex_run_in_environment (child, flags,
841 args[0], const_cast<char **> (args),
842 iconv_env.envp (),
843 NULL, NULL, &err))
844 {
845 FILE *in = pex_read_output (child, 0);
846
847 /* POSIX says that iconv -l uses an unspecified format. We
848 parse the glibc and libiconv formats; feel free to add others
849 as needed. */
850
851 while (in != NULL && !feof (in))
852 {
853 /* The size of buf is chosen arbitrarily. */
854 char buf[1024];
855 char *start, *r;
856 int len;
857
858 r = fgets (buf, sizeof (buf), in);
859 if (!r)
860 break;
861 len = strlen (r);
862 if (len <= 3)
863 continue;
864 if (ignore_line_p (r))
865 continue;
866
867 /* Strip off the newline. */
868 --len;
869 /* Strip off one or two '/'s. glibc will print lines like
870 "8859_7//", but also "10646-1:1993/UCS4/". */
871 if (buf[len - 1] == '/')
872 --len;
873 if (buf[len - 1] == '/')
874 --len;
875 buf[len] = '\0';
876
877 /* libiconv will print multiple entries per line, separated
878 by spaces. Older iconvs will print multiple entries per
879 line, indented by two spaces, and separated by ", "
880 (i.e. the human readable form). */
881 start = buf;
882 while (1)
883 {
884 int keep_going;
885 char *p;
886
887 /* Skip leading blanks. */
888 for (p = start; *p && *p == ' '; ++p)
889 ;
890 start = p;
891 /* Find the next space, comma, or end-of-line. */
892 for ( ; *p && *p != ' ' && *p != ','; ++p)
893 ;
894 /* Ignore an empty result. */
895 if (p == start)
896 break;
897 keep_going = *p;
898 *p = '\0';
899 charsets.charsets.push_back (xstrdup (start));
900 if (!keep_going)
901 break;
902 /* Skip any extra spaces. */
903 for (start = p + 1; *start && *start == ' '; ++start)
904 ;
905 }
906 }
907
908 if (pex_get_status (child, 1, &status)
909 && WIFEXITED (status) && !WEXITSTATUS (status))
910 fail = 0;
911
912 }
913
914 xfree (iconv_program);
915 pex_free (child);
916
917 if (fail)
918 {
919 /* Some error occurred, so drop the vector. */
920 charsets.clear ();
921 }
922 else
923 charsets.charsets.push_back (NULL);
924}
925
926#endif /* HAVE_ICONVLIST || HAVE_LIBICONVLIST */
927#endif /* PHONY_ICONV */
928
929/* The "auto" target charset used by default_auto_charset. */
930static const char *auto_target_charset_name = GDB_DEFAULT_TARGET_CHARSET;
931
932const char *
933default_auto_charset (void)
934{
935 return auto_target_charset_name;
936}
937
938const char *
939default_auto_wide_charset (void)
940{
941 return GDB_DEFAULT_TARGET_WIDE_CHARSET;
942}
943
944
945#ifdef USE_INTERMEDIATE_ENCODING_FUNCTION
946/* Macro used for UTF or UCS endianness suffix. */
947#if WORDS_BIGENDIAN
948#define ENDIAN_SUFFIX "BE"
949#else
950#define ENDIAN_SUFFIX "LE"
951#endif
952
953/* The code below serves to generate a compile time error if
954 gdb_wchar_t type is not of size 2 nor 4, despite the fact that
955 macro __STDC_ISO_10646__ is defined.
956 This is better than a gdb_assert call, because GDB cannot handle
957 strings correctly if this size is different. */
958
959extern char your_gdb_wchar_t_is_bogus[(sizeof (gdb_wchar_t) == 2
960 || sizeof (gdb_wchar_t) == 4)
961 ? 1 : -1];
962
963/* intermediate_encoding returns the charset used internally by
964 GDB to convert between target and host encodings. As the test above
965 compiled, sizeof (gdb_wchar_t) is either 2 or 4 bytes.
966 UTF-16/32 is tested first, UCS-2/4 is tested as a second option,
967 otherwise an error is generated. */
968
969const char *
970intermediate_encoding (void)
971{
972 iconv_t desc;
973 static const char *stored_result = NULL;
974 char *result;
975
976 if (stored_result)
977 return stored_result;
978 result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8),
979 ENDIAN_SUFFIX);
980 /* Check that the name is supported by iconv_open. */
981 desc = iconv_open (result, host_charset ());
982 if (desc != (iconv_t) -1)
983 {
984 iconv_close (desc);
985 stored_result = result;
986 return result;
987 }
988 /* Not valid, free the allocated memory. */
989 xfree (result);
990 /* Second try, with UCS-2 type. */
991 result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t),
992 ENDIAN_SUFFIX);
993 /* Check that the name is supported by iconv_open. */
994 desc = iconv_open (result, host_charset ());
995 if (desc != (iconv_t) -1)
996 {
997 iconv_close (desc);
998 stored_result = result;
999 return result;
1000 }
1001 /* Not valid, free the allocated memory. */
1002 xfree (result);
1003 /* No valid charset found, generate error here. */
1004 error (_("Unable to find a vaild charset for string conversions"));
1005}
1006
1007#endif /* USE_INTERMEDIATE_ENCODING_FUNCTION */
1008
1009void
1010_initialize_charset (void)
1011{
1012 /* The first element is always "auto". */
1013 charsets.charsets.push_back (xstrdup ("auto"));
1014 find_charset_names ();
1015
1016 if (charsets.charsets.size () > 1)
1017 charset_enum = (const char **) charsets.charsets.data ();
1018 else
1019 charset_enum = default_charset_names;
1020
1021#ifndef PHONY_ICONV
1022#ifdef HAVE_LANGINFO_CODESET
1023 /* The result of nl_langinfo may be overwritten later. This may
1024 leak a little memory, if the user later changes the host charset,
1025 but that doesn't matter much. */
1026 auto_host_charset_name = xstrdup (nl_langinfo (CODESET));
1027 /* Solaris will return `646' here -- but the Solaris iconv then does
1028 not accept this. Darwin (and maybe FreeBSD) may return "" here,
1029 which GNU libiconv doesn't like (infinite loop). */
1030 if (!strcmp (auto_host_charset_name, "646") || !*auto_host_charset_name)
1031 auto_host_charset_name = "ASCII";
1032 auto_target_charset_name = auto_host_charset_name;
1033#elif defined (USE_WIN32API)
1034 {
1035 /* "CP" + x<=5 digits + paranoia. */
1036 static char w32_host_default_charset[16];
1037
1038 snprintf (w32_host_default_charset, sizeof w32_host_default_charset,
1039 "CP%d", GetACP());
1040 auto_host_charset_name = w32_host_default_charset;
1041 auto_target_charset_name = auto_host_charset_name;
1042 }
1043#endif
1044#endif
1045
1046 add_setshow_enum_cmd ("charset", class_support,
1047 charset_enum, &host_charset_name, _("\
1048Set the host and target character sets."), _("\
1049Show the host and target character sets."), _("\
1050The `host character set' is the one used by the system GDB is running on.\n\
1051The `target character set' is the one used by the program being debugged.\n\
1052You may only use supersets of ASCII for your host character set; GDB does\n\
1053not support any others.\n\
1054To see a list of the character sets GDB supports, type `set charset <TAB>'."),
1055 /* Note that the sfunc below needs to set
1056 target_charset_name, because the 'set
1057 charset' command sets two variables. */
1058 set_charset_sfunc,
1059 show_charset,
1060 &setlist, &showlist);
1061
1062 add_setshow_enum_cmd ("host-charset", class_support,
1063 charset_enum, &host_charset_name, _("\
1064Set the host character set."), _("\
1065Show the host character set."), _("\
1066The `host character set' is the one used by the system GDB is running on.\n\
1067You may only use supersets of ASCII for your host character set; GDB does\n\
1068not support any others.\n\
1069To see a list of the character sets GDB supports, type `set host-charset <TAB>'."),
1070 set_host_charset_sfunc,
1071 show_host_charset_name,
1072 &setlist, &showlist);
1073
1074 add_setshow_enum_cmd ("target-charset", class_support,
1075 charset_enum, &target_charset_name, _("\
1076Set the target character set."), _("\
1077Show the target character set."), _("\
1078The `target character set' is the one used by the program being debugged.\n\
1079GDB translates characters and strings between the host and target\n\
1080character sets as needed.\n\
1081To see a list of the character sets GDB supports, type `set target-charset'<TAB>"),
1082 set_target_charset_sfunc,
1083 show_target_charset_name,
1084 &setlist, &showlist);
1085
1086 add_setshow_enum_cmd ("target-wide-charset", class_support,
1087 charset_enum, &target_wide_charset_name,
1088 _("\
1089Set the target wide character set."), _("\
1090Show the target wide character set."), _("\
1091The `target wide character set' is the one used by the program being debugged.\
1092\nIn particular it is the encoding used by `wchar_t'.\n\
1093GDB translates characters and strings between the host and target\n\
1094character sets as needed.\n\
1095To see a list of the character sets GDB supports, type\n\
1096`set target-wide-charset'<TAB>"),
1097 set_target_wide_charset_sfunc,
1098 show_target_wide_charset_name,
1099 &setlist, &showlist);
1100}
This page took 0.140185 seconds and 4 git commands to generate.