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