Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / charset.c
CommitLineData
234b45d4 1/* Character set conversion support for GDB.
1bac305b 2
42a4f53d 3 Copyright (C) 2001-2019 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
TT
21
22/* Standard C includes. */
23#include <ctype.h>
24
25/* Local non-gdb includes. */
26#include "arch-utils.h"
6c7a06a3 27#include "charset-list.h"
d55e5aa6 28#include "charset.h"
0747795c 29#include "common/environ.h"
0747795c 30#include "common/gdb_vecs.h"
d55e5aa6
TT
31#include "common/gdb_wait.h"
32#include "common/vec.h"
33#include "gdb_obstack.h"
34#include "gdbcmd.h"
234b45d4 35
43484f03
DJ
36#ifdef USE_WIN32API
37#include <windows.h>
38#endif
234b45d4
KB
39\f
40/* How GDB's character set support works
41
6c7a06a3 42 GDB has three global settings:
234b45d4
KB
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
6c7a06a3
TT
46 terminal knows how to display properly. Most users should not
47 change this.
234b45d4
KB
48
49 - The `current target character set' is the character set the
50 program being debugged uses.
51
6c7a06a3
TT
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
234b45d4
KB
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
6c7a06a3
TT
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. */
234b45d4
KB
76
77\f
6c7a06a3 78#ifdef PHONY_ICONV
234b45d4 79
6c7a06a3
TT
80/* Provide a phony iconv that does as little as possible. Also,
81 arrange for there to be a single available character set. */
234b45d4 82
6c7a06a3 83#undef GDB_DEFAULT_HOST_CHARSET
f74f61cb
SL
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"
6c7a06a3
TT
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
62234ccc 97#define iconv_open phony_iconv_open
6c7a06a3 98#undef iconv
62234ccc 99#define iconv phony_iconv
6c7a06a3 100#undef iconv_close
62234ccc 101#define iconv_close phony_iconv_close
6c7a06a3 102
0dd7fb99
TT
103#undef ICONV_CONST
104#define ICONV_CONST const
105
f74f61cb
SL
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
a95babbf 111static iconv_t
62234ccc 112phony_iconv_open (const char *to, const char *from)
6c7a06a3 113{
6c7a06a3
TT
114 if (strcmp (to, "wchar_t") && strcmp (to, GDB_DEFAULT_HOST_CHARSET))
115 return -1;
234b45d4 116
f74f61cb
SL
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;
6c7a06a3 127}
234b45d4 128
a95babbf 129static int
62234ccc 130phony_iconv_close (iconv_t arg)
6c7a06a3
TT
131{
132 return 0;
133}
234b45d4 134
a95babbf 135static size_t
62234ccc
TT
136phony_iconv (iconv_t utf_flag, const char **inbuf, size_t *inbytesleft,
137 char **outbuf, size_t *outbytesleft)
6c7a06a3 138{
b8899f2b 139 if (utf_flag)
6c7a06a3 140 {
f74f61cb
SL
141 enum bfd_endian endian
142 = utf_flag == 1 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
6c7a06a3
TT
143 while (*inbytesleft >= 4)
144 {
f74f61cb
SL
145 unsigned long c
146 = extract_unsigned_integer ((const gdb_byte *)*inbuf, 4, endian);
6c7a06a3
TT
147
148 if (c >= 256)
149 {
150 errno = EILSEQ;
151 return -1;
152 }
f74f61cb
SL
153 if (*outbytesleft < 1)
154 {
155 errno = E2BIG;
156 return -1;
157 }
6c7a06a3
TT
158 **outbuf = c & 0xff;
159 ++*outbuf;
160 --*outbytesleft;
161
f74f61cb 162 *inbuf += 4;
6c7a06a3
TT
163 *inbytesleft -= 4;
164 }
f74f61cb 165 if (*inbytesleft)
6c7a06a3 166 {
f74f61cb 167 /* Partial sequence on input. */
6c7a06a3
TT
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;
c5504eaf 177
6c7a06a3
TT
178 if (amt > *outbytesleft)
179 amt = *outbytesleft;
180 memcpy (*outbuf, *inbuf, amt);
181 *inbuf += amt;
182 *outbuf += amt;
183 *inbytesleft -= amt;
184 *outbytesleft -= amt;
f74f61cb
SL
185 if (*inbytesleft)
186 {
187 errno = E2BIG;
188 return -1;
189 }
6c7a06a3 190 }
234b45d4 191
6c7a06a3
TT
192 /* The number of non-reversible conversions -- but they were all
193 reversible. */
194 return 0;
195}
234b45d4 196
83030110
PA
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
234b45d4 217
83030110 218#endif /* PHONY_ICONV */
234b45d4
KB
219
220\f
221/* The global lists of character sets and translations. */
222
223
e33d66ec
EZ
224#ifndef GDB_DEFAULT_TARGET_CHARSET
225#define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
226#endif
227
6c7a06a3 228#ifndef GDB_DEFAULT_TARGET_WIDE_CHARSET
b8899f2b 229#define GDB_DEFAULT_TARGET_WIDE_CHARSET "UTF-32"
6c7a06a3
TT
230#endif
231
232static const char *auto_host_charset_name = GDB_DEFAULT_HOST_CHARSET;
233static const char *host_charset_name = "auto";
920d2a44
AC
234static void
235show_host_charset_name (struct ui_file *file, int from_tty,
236 struct cmd_list_element *c,
237 const char *value)
238{
6c7a06a3
TT
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);
920d2a44
AC
245}
246
f870a310 247static const char *target_charset_name = "auto";
920d2a44
AC
248static void
249show_target_charset_name (struct ui_file *file, int from_tty,
250 struct cmd_list_element *c, const char *value)
251{
f870a310
TT
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);
920d2a44
AC
260}
261
f870a310 262static const char *target_wide_charset_name = "auto";
6c7a06a3 263static void
aff410f1
MS
264show_target_wide_charset_name (struct ui_file *file,
265 int from_tty,
266 struct cmd_list_element *c,
267 const char *value)
e33d66ec 268{
f870a310
TT
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);
6c7a06a3 277}
e33d66ec 278
6c7a06a3 279static const char *default_charset_names[] =
e33d66ec 280{
6c7a06a3 281 DEFAULT_CHARSET_NAMES
e33d66ec
EZ
282 0
283};
234b45d4 284
6c7a06a3 285static const char **charset_enum;
234b45d4 286
6c7a06a3
TT
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;
234b45d4 292
f870a310
TT
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. */
234b45d4 298
6c7a06a3 299static void
f870a310 300set_be_le_names (struct gdbarch *gdbarch)
234b45d4 301{
f870a310
TT
302 if (be_le_arch == gdbarch)
303 return;
304 be_le_arch = gdbarch;
234b45d4 305
f74f61cb
SL
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
15766370
TT
311 int i, len;
312 const char *target_wide;
313
6c7a06a3
TT
314 target_wide_charset_le_name = NULL;
315 target_wide_charset_be_name = NULL;
234b45d4 316
f870a310
TT
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);
6c7a06a3
TT
322 for (i = 0; charset_enum[i]; ++i)
323 {
f870a310 324 if (strncmp (target_wide, charset_enum[i], len))
6c7a06a3
TT
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 }
f74f61cb 337# endif /* PHONY_ICONV */
234b45d4
KB
338}
339
6c7a06a3
TT
340/* 'Set charset', 'set host-charset', 'set target-charset', 'set
341 target-wide-charset', 'set charset' sfunc's. */
234b45d4
KB
342
343static void
f870a310 344validate (struct gdbarch *gdbarch)
234b45d4 345{
6c7a06a3
TT
346 iconv_t desc;
347 const char *host_cset = host_charset ();
f870a310
TT
348 const char *target_cset = target_charset (gdbarch);
349 const char *target_wide_cset = target_wide_charset_name;
c5504eaf 350
f870a310
TT
351 if (!strcmp (target_wide_cset, "auto"))
352 target_wide_cset = gdbarch_auto_wide_charset (gdbarch);
234b45d4 353
f870a310 354 desc = iconv_open (target_wide_cset, host_cset);
6c7a06a3 355 if (desc == (iconv_t) -1)
a73c6dcd 356 error (_("Cannot convert between character sets `%s' and `%s'"),
f870a310 357 target_wide_cset, host_cset);
6c7a06a3 358 iconv_close (desc);
234b45d4 359
f870a310 360 desc = iconv_open (target_cset, host_cset);
6c7a06a3 361 if (desc == (iconv_t) -1)
a73c6dcd 362 error (_("Cannot convert between character sets `%s' and `%s'"),
f870a310 363 target_cset, host_cset);
6c7a06a3 364 iconv_close (desc);
234b45d4 365
f870a310
TT
366 /* Clear the cache. */
367 be_le_arch = NULL;
234b45d4
KB
368}
369
6c7a06a3
TT
370/* This is the sfunc for the 'set charset' command. */
371static void
eb4c3f4a 372set_charset_sfunc (const char *charset, int from_tty,
aff410f1 373 struct cmd_list_element *c)
234b45d4 374{
aff410f1 375 /* CAREFUL: set the target charset here as well. */
6c7a06a3 376 target_charset_name = host_charset_name;
f870a310 377 validate (get_current_arch ());
234b45d4
KB
378}
379
6c7a06a3
TT
380/* 'set host-charset' command sfunc. We need a wrapper here because
381 the function needs to have a specific signature. */
382static void
eb4c3f4a 383set_host_charset_sfunc (const char *charset, int from_tty,
6c7a06a3 384 struct cmd_list_element *c)
234b45d4 385{
f870a310 386 validate (get_current_arch ());
234b45d4
KB
387}
388
6c7a06a3
TT
389/* Wrapper for the 'set target-charset' command. */
390static void
eb4c3f4a 391set_target_charset_sfunc (const char *charset, int from_tty,
6c7a06a3 392 struct cmd_list_element *c)
234b45d4 393{
f870a310 394 validate (get_current_arch ());
234b45d4
KB
395}
396
6c7a06a3
TT
397/* Wrapper for the 'set target-wide-charset' command. */
398static void
eb4c3f4a 399set_target_wide_charset_sfunc (const char *charset, int from_tty,
6c7a06a3 400 struct cmd_list_element *c)
234b45d4 401{
f870a310 402 validate (get_current_arch ());
234b45d4
KB
403}
404
6c7a06a3
TT
405/* sfunc for the 'show charset' command. */
406static void
aff410f1
MS
407show_charset (struct ui_file *file, int from_tty,
408 struct cmd_list_element *c,
6c7a06a3 409 const char *name)
234b45d4 410{
6c7a06a3
TT
411 show_host_charset_name (file, from_tty, c, host_charset_name);
412 show_target_charset_name (file, from_tty, c, target_charset_name);
aff410f1
MS
413 show_target_wide_charset_name (file, from_tty, c,
414 target_wide_charset_name);
234b45d4
KB
415}
416
234b45d4 417\f
6c7a06a3 418/* Accessor functions. */
234b45d4 419
6c7a06a3
TT
420const char *
421host_charset (void)
234b45d4 422{
6c7a06a3
TT
423 if (!strcmp (host_charset_name, "auto"))
424 return auto_host_charset_name;
425 return host_charset_name;
234b45d4
KB
426}
427
6c7a06a3 428const char *
f870a310 429target_charset (struct gdbarch *gdbarch)
234b45d4 430{
f870a310
TT
431 if (!strcmp (target_charset_name, "auto"))
432 return gdbarch_auto_charset (gdbarch);
6c7a06a3 433 return target_charset_name;
234b45d4 434}
234b45d4 435
6c7a06a3 436const char *
f870a310 437target_wide_charset (struct gdbarch *gdbarch)
234b45d4 438{
f870a310
TT
439 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
440
441 set_be_le_names (gdbarch);
e17a4113 442 if (byte_order == BFD_ENDIAN_BIG)
234b45d4 443 {
6c7a06a3
TT
444 if (target_wide_charset_be_name)
445 return target_wide_charset_be_name;
234b45d4 446 }
6c7a06a3 447 else
234b45d4 448 {
6c7a06a3
TT
449 if (target_wide_charset_le_name)
450 return target_wide_charset_le_name;
234b45d4
KB
451 }
452
f870a310
TT
453 if (!strcmp (target_wide_charset_name, "auto"))
454 return gdbarch_auto_wide_charset (gdbarch);
455
6c7a06a3 456 return target_wide_charset_name;
234b45d4
KB
457}
458
234b45d4 459\f
6c7a06a3
TT
460/* Host character set management. For the time being, we assume that
461 the host character set is some superset of ASCII. */
234b45d4 462
6c7a06a3
TT
463char
464host_letter_to_control_character (char c)
234b45d4 465{
6c7a06a3
TT
466 if (c == '?')
467 return 0177;
468 return c & 0237;
234b45d4
KB
469}
470
6c7a06a3
TT
471/* Convert a host character, C, to its hex value. C must already have
472 been validated using isxdigit. */
234b45d4 473
6c7a06a3
TT
474int
475host_hex_value (char c)
234b45d4 476{
6c7a06a3
TT
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';
234b45d4
KB
483}
484
234b45d4 485\f
6c7a06a3 486/* Public character management functions. */
234b45d4 487
80a3b8c5 488class iconv_wrapper
234b45d4 489{
80a3b8c5
TT
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};
234b45d4 514
6c7a06a3
TT
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{
6c7a06a3 521 size_t inleft;
39086a0e 522 ICONV_CONST char *inp;
6c7a06a3
TT
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 }
234b45d4 531
80a3b8c5 532 iconv_wrapper desc (to, from);
234b45d4 533
6c7a06a3 534 inleft = num_bytes;
39086a0e 535 inp = (ICONV_CONST char *) bytes;
234b45d4 536
6c7a06a3 537 space_request = num_bytes;
234b45d4 538
6c7a06a3 539 while (inleft > 0)
234b45d4 540 {
6c7a06a3
TT
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
241fd515 548 outp = (char *) obstack_base (output) + old_size;
6c7a06a3
TT
549 outleft = space_request;
550
80a3b8c5 551 r = desc.convert (&inp, &inleft, &outp, &outleft);
6c7a06a3
TT
552
553 /* Now make sure that the object on the obstack only includes
554 bytes we have converted. */
89eb3c54 555 obstack_blank_fast (output, -(ssize_t) outleft);
6c7a06a3
TT
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)
3e43a32a
MS
567 error (_("Could not convert character "
568 "to `%s' character set"), to);
6c7a06a3
TT
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
08850b56 576 xsnprintf (octal, sizeof (octal), "\\%.3o", *inp & 0xff);
6c7a06a3
TT
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:
9b20d036
MS
598 perror_with_name (_("Internal error while "
599 "converting character sets"));
6c7a06a3
TT
600 }
601 }
234b45d4 602 }
234b45d4
KB
603}
604
e33d66ec 605\f
e33d66ec 606
6c7a06a3 607/* Create a new iterator. */
cda6c55b
TT
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)
234b45d4 614{
cda6c55b
TT
615 m_desc = iconv_open (INTERMEDIATE_ENCODING, charset);
616 if (m_desc == (iconv_t) -1)
9b20d036 617 perror_with_name (_("Converting character sets"));
234b45d4
KB
618}
619
cda6c55b 620wchar_iterator::~wchar_iterator ()
e33d66ec 621{
cda6c55b
TT
622 if (m_desc != (iconv_t) -1)
623 iconv_close (m_desc);
e33d66ec 624}
234b45d4 625
6c7a06a3 626int
cda6c55b
TT
627wchar_iterator::iterate (enum wchar_iterate_result *out_result,
628 gdb_wchar_t **out_chars,
629 const gdb_byte **ptr,
630 size_t *len)
6c7a06a3
TT
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;
cda6c55b 640 while (m_bytes > 0)
e33d66ec 641 {
cda6c55b
TT
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;
6c7a06a3
TT
646 size_t out_avail = out_request * sizeof (gdb_wchar_t);
647 size_t num;
cda6c55b 648 size_t r = iconv (m_desc, &inptr, &m_bytes, &outptr, &out_avail);
39086a0e 649
cda6c55b 650 m_input = (gdb_byte *) inptr;
c5504eaf 651
6c7a06a3
TT
652 if (r == (size_t) -1)
653 {
654 switch (errno)
655 {
656 case EILSEQ:
aff410f1
MS
657 /* Invalid input sequence. We still might have
658 converted a character; if so, return it. */
1558ab4c
JK
659 if (out_avail < out_request * sizeof (gdb_wchar_t))
660 break;
661
aff410f1
MS
662 /* Otherwise skip the first invalid character, and let
663 the caller know about it. */
6c7a06a3 664 *out_result = wchar_iterate_invalid;
cda6c55b
TT
665 *ptr = m_input;
666 *len = m_width;
667 m_input += m_width;
668 m_bytes -= m_width;
6c7a06a3
TT
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;
cda6c55b 679 if (out_request > m_out.size ())
d5722aa2 680 m_out.resize (out_request);
6c7a06a3
TT
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;
cda6c55b
TT
687 *ptr = m_input;
688 *len = m_bytes;
689 m_bytes = 0;
6c7a06a3
TT
690 return 0;
691
692 default:
9b20d036
MS
693 perror_with_name (_("Internal error while "
694 "converting character sets"));
6c7a06a3
TT
695 }
696 }
697
698 /* We converted something. */
699 num = out_request - out_avail / sizeof (gdb_wchar_t);
700 *out_result = wchar_iterate_ok;
cda6c55b 701 *out_chars = m_out.data ();
6c7a06a3 702 *ptr = orig_inptr;
cda6c55b 703 *len = orig_in - m_bytes;
6c7a06a3 704 return num;
e33d66ec 705 }
6c7a06a3
TT
706
707 /* Really done. */
708 *out_result = wchar_iterate_eof;
709 return -1;
234b45d4
KB
710}
711
ccb2231c
SM
712struct charset_vector
713{
714 ~charset_vector ()
715 {
716 clear ();
717 }
718
719 void clear ()
720 {
721 for (char *c : charsets)
722 xfree (c);
234b45d4 723
ccb2231c
SM
724 charsets.clear ();
725 }
726
727 std::vector<char *> charsets;
728};
729
730static charset_vector charsets;
234b45d4 731
6c7a06a3 732#ifdef PHONY_ICONV
234b45d4 733
6c7a06a3
TT
734static void
735find_charset_names (void)
234b45d4 736{
ccb2231c
SM
737 charsets.charsets.push_back (xstrdup (GDB_DEFAULT_HOST_CHARSET));
738 charsets.charsets.push_back (NULL);
234b45d4
KB
739}
740
6c7a06a3 741#else /* PHONY_ICONV */
fc3b640d
TT
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)
234b45d4 749
6c7a06a3
TT
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)
234b45d4 755{
6c7a06a3 756 unsigned int i;
234b45d4 757
6c7a06a3 758 for (i = 0; i < count; ++i)
ccb2231c 759 charsets.charsets.push_back (xstrdup (names[i]));
234b45d4 760
6c7a06a3 761 return 0;
234b45d4
KB
762}
763
6c7a06a3
TT
764static void
765find_charset_names (void)
234b45d4 766{
6c7a06a3 767 iconvlist (add_one, NULL);
ccb2231c
SM
768
769 charsets.charsets.push_back (NULL);
234b45d4
KB
770}
771
6c7a06a3 772#else
234b45d4 773
40b5c9fb
DE
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
6c7a06a3
TT
803static void
804find_charset_names (void)
234b45d4 805{
732f6a93 806 struct pex_obj *child;
a121b7c1 807 const char *args[3];
732f6a93
TT
808 int err, status;
809 int fail = 1;
478aac75 810 int flags;
9a6c7d9c 811 gdb_environ iconv_env = gdb_environ::from_host_environ ();
478aac75 812 char *iconv_program;
40b5c9fb 813
aff410f1
MS
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. */
9a6c7d9c
SDJ
817 iconv_env.set ("LANGUAGE", "C");
818 iconv_env.set ("LC_ALL", "C");
732f6a93 819
40618926 820 child = pex_init (PEX_USE_PIPES, "iconv", NULL);
732f6a93 821
478aac75
DE
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;
732f6a93
TT
833 args[1] = "-l";
834 args[2] = NULL;
478aac75
DE
835 flags = PEX_STDERR_TO_STDOUT;
836#ifndef ICONV_BIN
837 flags |= PEX_SEARCH;
838#endif
732f6a93 839 /* Note that we simply ignore errors here. */
478aac75 840 if (!pex_run_in_environment (child, flags,
a121b7c1 841 args[0], const_cast<char **> (args),
9a6c7d9c 842 iconv_env.envp (),
40b5c9fb 843 NULL, NULL, &err))
732f6a93
TT
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. */
40b5c9fb 850
1d6b2d2b 851 while (in != NULL && !feof (in))
732f6a93
TT
852 {
853 /* The size of buf is chosen arbitrarily. */
854 char buf[1024];
855 char *start, *r;
8ea13695 856 int len;
732f6a93
TT
857
858 r = fgets (buf, sizeof (buf), in);
859 if (!r)
860 break;
861 len = strlen (r);
862 if (len <= 3)
863 continue;
40b5c9fb
DE
864 if (ignore_line_p (r))
865 continue;
866
732f6a93
TT
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
aff410f1
MS
878 by spaces. Older iconvs will print multiple entries per
879 line, indented by two spaces, and separated by ", "
40b5c9fb 880 (i.e. the human readable form). */
732f6a93
TT
881 start = buf;
882 while (1)
883 {
884 int keep_going;
885 char *p;
886
40b5c9fb
DE
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)
732f6a93
TT
893 ;
894 /* Ignore an empty result. */
895 if (p == start)
896 break;
897 keep_going = *p;
898 *p = '\0';
ccb2231c 899 charsets.charsets.push_back (xstrdup (start));
732f6a93
TT
900 if (!keep_going)
901 break;
902 /* Skip any extra spaces. */
903 for (start = p + 1; *start && *start == ' '; ++start)
904 ;
905 }
906 }
234b45d4 907
732f6a93
TT
908 if (pex_get_status (child, 1, &status)
909 && WIFEXITED (status) && !WEXITSTATUS (status))
910 fail = 0;
234b45d4 911
6c7a06a3 912 }
234b45d4 913
478aac75 914 xfree (iconv_program);
732f6a93 915 pex_free (child);
234b45d4 916
732f6a93
TT
917 if (fail)
918 {
919 /* Some error occurred, so drop the vector. */
ccb2231c 920 charsets.clear ();
732f6a93
TT
921 }
922 else
ccb2231c 923 charsets.charsets.push_back (NULL);
6c7a06a3 924}
234b45d4 925
fc3b640d 926#endif /* HAVE_ICONVLIST || HAVE_LIBICONVLIST */
6c7a06a3 927#endif /* PHONY_ICONV */
234b45d4 928
f870a310
TT
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
bcb28afc
PM
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
ee34b3f9 963/* intermediate_encoding returns the charset used internally by
bcb28afc
PM
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;
bcb28afc
PM
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
234b45d4
KB
1009void
1010_initialize_charset (void)
1011{
f870a310 1012 /* The first element is always "auto". */
ccb2231c 1013 charsets.charsets.push_back (xstrdup ("auto"));
6c7a06a3
TT
1014 find_charset_names ();
1015
ccb2231c
SM
1016 if (charsets.charsets.size () > 1)
1017 charset_enum = (const char **) charsets.charsets.data ();
6c7a06a3
TT
1018 else
1019 charset_enum = default_charset_names;
1020
1021#ifndef PHONY_ICONV
1022#ifdef HAVE_LANGINFO_CODESET
f870a310
TT
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));
aff410f1
MS
1027 /* Solaris will return `646' here -- but the Solaris iconv then does
1028 not accept this. Darwin (and maybe FreeBSD) may return "" here,
06be6983
TG
1029 which GNU libiconv doesn't like (infinite loop). */
1030 if (!strcmp (auto_host_charset_name, "646") || !*auto_host_charset_name)
58720494 1031 auto_host_charset_name = "ASCII";
f870a310
TT
1032 auto_target_charset_name = auto_host_charset_name;
1033#elif defined (USE_WIN32API)
1034 {
3e43a32a
MS
1035 /* "CP" + x<=5 digits + paranoia. */
1036 static char w32_host_default_charset[16];
f870a310
TT
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 }
6c7a06a3
TT
1043#endif
1044#endif
e33d66ec 1045
7ab04401 1046 add_setshow_enum_cmd ("charset", class_support,
f870a310 1047 charset_enum, &host_charset_name, _("\
7ab04401
AC
1048Set the host and target character sets."), _("\
1049Show the host and target character sets."), _("\
3d263c1d
BI
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>'."),
7ab04401
AC
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,
6c7a06a3 1063 charset_enum, &host_charset_name, _("\
7ab04401
AC
1064Set the host character set."), _("\
1065Show the host character set."), _("\
3d263c1d
BI
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\
ac74f770
MS
1068not support any others.\n\
1069To see a list of the character sets GDB supports, type `set host-charset <TAB>'."),
7ab04401 1070 set_host_charset_sfunc,
920d2a44 1071 show_host_charset_name,
7ab04401
AC
1072 &setlist, &showlist);
1073
1074 add_setshow_enum_cmd ("target-charset", class_support,
f870a310 1075 charset_enum, &target_charset_name, _("\
7ab04401
AC
1076Set the target character set."), _("\
1077Show the target character set."), _("\
3d263c1d
BI
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\
b670013c 1080character sets as needed.\n\
ac74f770 1081To see a list of the character sets GDB supports, type `set target-charset'<TAB>"),
7ab04401 1082 set_target_charset_sfunc,
920d2a44 1083 show_target_charset_name,
7ab04401 1084 &setlist, &showlist);
6c7a06a3
TT
1085
1086 add_setshow_enum_cmd ("target-wide-charset", class_support,
f870a310 1087 charset_enum, &target_wide_charset_name,
6c7a06a3
TT
1088 _("\
1089Set the target wide character set."), _("\
1090Show the target wide character set."), _("\
3e43a32a
MS
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\
6c7a06a3
TT
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);
234b45d4 1100}
This page took 1.418946 seconds and 4 git commands to generate.