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