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