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