daily update
[deliverable/binutils-gdb.git] / gdb / charset.c
CommitLineData
234b45d4 1/* Character set conversion support for GDB.
1bac305b 2
0fb0cc75 3 Copyright (C) 2001, 2003, 2007, 2008, 2009 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
TT
24#include "gdb_obstack.h"
25#include "charset-list.h"
26#include "vec.h"
234b45d4
KB
27
28#include <stddef.h>
4ef3f3be 29#include "gdb_string.h"
234b45d4
KB
30#include <ctype.h>
31
234b45d4
KB
32\f
33/* How GDB's character set support works
34
6c7a06a3 35 GDB has three global settings:
234b45d4
KB
36
37 - The `current host character set' is the character set GDB should
38 use in talking to the user, and which (hopefully) the user's
6c7a06a3
TT
39 terminal knows how to display properly. Most users should not
40 change this.
234b45d4
KB
41
42 - The `current target character set' is the character set the
43 program being debugged uses.
44
6c7a06a3
TT
45 - The `current target wide character set' is the wide character set
46 the program being debugged uses, that is, the encoding used for
47 wchar_t.
48
234b45d4
KB
49 There are commands to set each of these, and mechanisms for
50 choosing reasonable default values. GDB has a global list of
51 character sets that it can use as its host or target character
52 sets.
53
54 The header file `charset.h' declares various functions that
55 different pieces of GDB need to perform tasks like:
56
57 - printing target strings and characters to the user's terminal
58 (mostly target->host conversions),
59
60 - building target-appropriate representations of strings and
61 characters the user enters in expressions (mostly host->target
62 conversions),
63
6c7a06a3
TT
64 and so on.
65
66 To avoid excessive code duplication and maintenance efforts,
67 GDB simply requires a capable iconv function. Users on platforms
68 without a suitable iconv can use the GNU iconv library. */
234b45d4
KB
69
70\f
6c7a06a3 71#ifdef PHONY_ICONV
234b45d4 72
6c7a06a3
TT
73/* Provide a phony iconv that does as little as possible. Also,
74 arrange for there to be a single available character set. */
234b45d4 75
6c7a06a3
TT
76#undef GDB_DEFAULT_HOST_CHARSET
77#define GDB_DEFAULT_HOST_CHARSET "ISO-8859-1"
78#define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
79#define GDB_DEFAULT_TARGET_WIDE_CHARSET "ISO-8859-1"
80#undef DEFAULT_CHARSET_NAMES
81#define DEFAULT_CHARSET_NAMES GDB_DEFAULT_HOST_CHARSET ,
82
83#undef iconv_t
84#define iconv_t int
85#undef iconv_open
86#undef iconv
87#undef iconv_close
88
0dd7fb99
TT
89#undef ICONV_CONST
90#define ICONV_CONST const
91
6c7a06a3
TT
92iconv_t
93iconv_open (const char *to, const char *from)
94{
95 /* We allow conversions from UCS-4BE, wchar_t, and the host charset.
96 We allow conversions to wchar_t and the host charset. */
97 if (strcmp (from, "UCS-4BE") && strcmp (from, "wchar_t")
98 && strcmp (from, GDB_DEFAULT_HOST_CHARSET))
99 return -1;
100 if (strcmp (to, "wchar_t") && strcmp (to, GDB_DEFAULT_HOST_CHARSET))
101 return -1;
234b45d4 102
6c7a06a3
TT
103 /* Return 1 if we are converting from UCS-4BE, 0 otherwise. This is
104 used as a flag in calls to iconv. */
105 return !strcmp (from, "UCS-4BE");
106}
234b45d4 107
6c7a06a3
TT
108int
109iconv_close (iconv_t arg)
110{
111 return 0;
112}
234b45d4 113
6c7a06a3 114size_t
0dd7fb99 115iconv (iconv_t ucs_flag, const char **inbuf, size_t *inbytesleft,
6c7a06a3
TT
116 char **outbuf, size_t *outbytesleft)
117{
118 if (ucs_flag)
119 {
120 while (*inbytesleft >= 4)
121 {
122 size_t j;
123 unsigned long c = 0;
124
125 for (j = 0; j < 4; ++j)
126 {
127 c <<= 8;
128 c += (*inbuf)[j] & 0xff;
129 }
130
131 if (c >= 256)
132 {
133 errno = EILSEQ;
134 return -1;
135 }
136 **outbuf = c & 0xff;
137 ++*outbuf;
138 --*outbytesleft;
139
140 ++*inbuf;
141 *inbytesleft -= 4;
142 }
143 if (*inbytesleft < 4)
144 {
145 errno = EINVAL;
146 return -1;
147 }
148 }
149 else
150 {
151 /* In all other cases we simply copy input bytes to the
152 output. */
153 size_t amt = *inbytesleft;
154 if (amt > *outbytesleft)
155 amt = *outbytesleft;
156 memcpy (*outbuf, *inbuf, amt);
157 *inbuf += amt;
158 *outbuf += amt;
159 *inbytesleft -= amt;
160 *outbytesleft -= amt;
161 }
234b45d4 162
6c7a06a3
TT
163 if (*inbytesleft)
164 {
165 errno = E2BIG;
166 return -1;
167 }
234b45d4 168
6c7a06a3
TT
169 /* The number of non-reversible conversions -- but they were all
170 reversible. */
171 return 0;
172}
234b45d4 173
6c7a06a3 174#endif
234b45d4
KB
175
176
177\f
178/* The global lists of character sets and translations. */
179
180
e33d66ec
EZ
181#ifndef GDB_DEFAULT_TARGET_CHARSET
182#define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
183#endif
184
6c7a06a3
TT
185#ifndef GDB_DEFAULT_TARGET_WIDE_CHARSET
186#define GDB_DEFAULT_TARGET_WIDE_CHARSET "UCS-4"
187#endif
188
189static const char *auto_host_charset_name = GDB_DEFAULT_HOST_CHARSET;
190static const char *host_charset_name = "auto";
920d2a44
AC
191static void
192show_host_charset_name (struct ui_file *file, int from_tty,
193 struct cmd_list_element *c,
194 const char *value)
195{
6c7a06a3
TT
196 if (!strcmp (value, "auto"))
197 fprintf_filtered (file,
198 _("The host character set is \"auto; currently %s\".\n"),
199 auto_host_charset_name);
200 else
201 fprintf_filtered (file, _("The host character set is \"%s\".\n"), value);
920d2a44
AC
202}
203
e33d66ec 204static const char *target_charset_name = GDB_DEFAULT_TARGET_CHARSET;
920d2a44
AC
205static void
206show_target_charset_name (struct ui_file *file, int from_tty,
207 struct cmd_list_element *c, const char *value)
208{
209 fprintf_filtered (file, _("The target character set is \"%s\".\n"),
210 value);
211}
212
6c7a06a3
TT
213static const char *target_wide_charset_name = GDB_DEFAULT_TARGET_WIDE_CHARSET;
214static void
215show_target_wide_charset_name (struct ui_file *file, int from_tty,
216 struct cmd_list_element *c, const char *value)
e33d66ec 217{
6c7a06a3
TT
218 fprintf_filtered (file, _("The target wide character set is \"%s\".\n"),
219 value);
220}
e33d66ec 221
6c7a06a3 222static const char *default_charset_names[] =
e33d66ec 223{
6c7a06a3 224 DEFAULT_CHARSET_NAMES
e33d66ec
EZ
225 0
226};
234b45d4 227
6c7a06a3 228static const char **charset_enum;
234b45d4 229
6c7a06a3
TT
230\f
231/* If the target wide character set has big- or little-endian
232 variants, these are the corresponding names. */
233static const char *target_wide_charset_be_name;
234static const char *target_wide_charset_le_name;
234b45d4 235
6c7a06a3
TT
236/* A helper function for validate which sets the target wide big- and
237 little-endian character set names, if possible. */
234b45d4 238
6c7a06a3
TT
239static void
240set_be_le_names (void)
234b45d4 241{
6c7a06a3 242 int i, len;
234b45d4 243
6c7a06a3
TT
244 target_wide_charset_le_name = NULL;
245 target_wide_charset_be_name = NULL;
234b45d4 246
6c7a06a3
TT
247 len = strlen (target_wide_charset_name);
248 for (i = 0; charset_enum[i]; ++i)
249 {
250 if (strncmp (target_wide_charset_name, charset_enum[i], len))
251 continue;
252 if ((charset_enum[i][len] == 'B'
253 || charset_enum[i][len] == 'L')
254 && charset_enum[i][len + 1] == 'E'
255 && charset_enum[i][len + 2] == '\0')
256 {
257 if (charset_enum[i][len] == 'B')
258 target_wide_charset_be_name = charset_enum[i];
259 else
260 target_wide_charset_le_name = charset_enum[i];
261 }
262 }
234b45d4
KB
263}
264
6c7a06a3
TT
265/* 'Set charset', 'set host-charset', 'set target-charset', 'set
266 target-wide-charset', 'set charset' sfunc's. */
234b45d4
KB
267
268static void
6c7a06a3 269validate (void)
234b45d4 270{
6c7a06a3
TT
271 iconv_t desc;
272 const char *host_cset = host_charset ();
234b45d4 273
6c7a06a3
TT
274 desc = iconv_open (target_wide_charset_name, host_cset);
275 if (desc == (iconv_t) -1)
276 error ("Cannot convert between character sets `%s' and `%s'",
277 target_wide_charset_name, host_cset);
278 iconv_close (desc);
234b45d4 279
6c7a06a3
TT
280 desc = iconv_open (target_charset_name, host_cset);
281 if (desc == (iconv_t) -1)
282 error ("Cannot convert between character sets `%s' and `%s'",
283 target_charset_name, host_cset);
284 iconv_close (desc);
234b45d4 285
6c7a06a3 286 set_be_le_names ();
234b45d4
KB
287}
288
6c7a06a3
TT
289/* This is the sfunc for the 'set charset' command. */
290static void
291set_charset_sfunc (char *charset, int from_tty, struct cmd_list_element *c)
234b45d4 292{
6c7a06a3
TT
293 /* CAREFUL: set the target charset here as well. */
294 target_charset_name = host_charset_name;
295 validate ();
234b45d4
KB
296}
297
6c7a06a3
TT
298/* 'set host-charset' command sfunc. We need a wrapper here because
299 the function needs to have a specific signature. */
300static void
301set_host_charset_sfunc (char *charset, int from_tty,
302 struct cmd_list_element *c)
234b45d4 303{
6c7a06a3 304 validate ();
234b45d4
KB
305}
306
6c7a06a3
TT
307/* Wrapper for the 'set target-charset' command. */
308static void
309set_target_charset_sfunc (char *charset, int from_tty,
310 struct cmd_list_element *c)
234b45d4 311{
6c7a06a3 312 validate ();
234b45d4
KB
313}
314
6c7a06a3
TT
315/* Wrapper for the 'set target-wide-charset' command. */
316static void
317set_target_wide_charset_sfunc (char *charset, int from_tty,
318 struct cmd_list_element *c)
234b45d4 319{
6c7a06a3 320 validate ();
234b45d4
KB
321}
322
6c7a06a3
TT
323/* sfunc for the 'show charset' command. */
324static void
325show_charset (struct ui_file *file, int from_tty, struct cmd_list_element *c,
326 const char *name)
234b45d4 327{
6c7a06a3
TT
328 show_host_charset_name (file, from_tty, c, host_charset_name);
329 show_target_charset_name (file, from_tty, c, target_charset_name);
330 show_target_wide_charset_name (file, from_tty, c, target_wide_charset_name);
234b45d4
KB
331}
332
234b45d4 333\f
6c7a06a3 334/* Accessor functions. */
234b45d4 335
6c7a06a3
TT
336const char *
337host_charset (void)
234b45d4 338{
6c7a06a3
TT
339 if (!strcmp (host_charset_name, "auto"))
340 return auto_host_charset_name;
341 return host_charset_name;
234b45d4
KB
342}
343
6c7a06a3
TT
344const char *
345target_charset (void)
234b45d4 346{
6c7a06a3 347 return target_charset_name;
234b45d4 348}
234b45d4 349
6c7a06a3
TT
350const char *
351target_wide_charset (void)
234b45d4 352{
6c7a06a3 353 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
234b45d4 354 {
6c7a06a3
TT
355 if (target_wide_charset_be_name)
356 return target_wide_charset_be_name;
234b45d4 357 }
6c7a06a3 358 else
234b45d4 359 {
6c7a06a3
TT
360 if (target_wide_charset_le_name)
361 return target_wide_charset_le_name;
234b45d4
KB
362 }
363
6c7a06a3 364 return target_wide_charset_name;
234b45d4
KB
365}
366
234b45d4 367\f
6c7a06a3
TT
368/* Host character set management. For the time being, we assume that
369 the host character set is some superset of ASCII. */
234b45d4 370
6c7a06a3
TT
371char
372host_letter_to_control_character (char c)
234b45d4 373{
6c7a06a3
TT
374 if (c == '?')
375 return 0177;
376 return c & 0237;
234b45d4
KB
377}
378
6c7a06a3
TT
379/* Convert a host character, C, to its hex value. C must already have
380 been validated using isxdigit. */
234b45d4 381
6c7a06a3
TT
382int
383host_hex_value (char c)
234b45d4 384{
6c7a06a3
TT
385 if (isdigit (c))
386 return c - '0';
387 if (c >= 'a' && c <= 'f')
388 return 10 + c - 'a';
389 gdb_assert (c >= 'A' && c <= 'F');
390 return 10 + c - 'A';
234b45d4
KB
391}
392
234b45d4 393\f
6c7a06a3 394/* Public character management functions. */
234b45d4 395
6c7a06a3 396/* A cleanup function which is run to close an iconv descriptor. */
234b45d4 397
6c7a06a3
TT
398static void
399cleanup_iconv (void *p)
234b45d4 400{
6c7a06a3
TT
401 iconv_t *descp = p;
402 iconv_close (*descp);
234b45d4
KB
403}
404
6c7a06a3
TT
405void
406convert_between_encodings (const char *from, const char *to,
407 const gdb_byte *bytes, unsigned int num_bytes,
408 int width, struct obstack *output,
409 enum transliterations translit)
410{
411 iconv_t desc;
412 struct cleanup *cleanups;
413 size_t inleft;
414 char *inp;
415 unsigned int space_request;
416
417 /* Often, the host and target charsets will be the same. */
418 if (!strcmp (from, to))
419 {
420 obstack_grow (output, bytes, num_bytes);
421 return;
422 }
234b45d4 423
6c7a06a3
TT
424 desc = iconv_open (to, from);
425 if (desc == (iconv_t) -1)
426 perror_with_name ("Converting character sets");
427 cleanups = make_cleanup (cleanup_iconv, &desc);
234b45d4 428
6c7a06a3
TT
429 inleft = num_bytes;
430 inp = (char *) bytes;
234b45d4 431
6c7a06a3 432 space_request = num_bytes;
234b45d4 433
6c7a06a3 434 while (inleft > 0)
234b45d4 435 {
6c7a06a3
TT
436 char *outp;
437 size_t outleft, r;
438 int old_size;
439
440 old_size = obstack_object_size (output);
441 obstack_blank (output, space_request);
442
443 outp = obstack_base (output) + old_size;
444 outleft = space_request;
445
0dd7fb99 446 r = iconv (desc, (ICONV_CONST char **) &inp, &inleft, &outp, &outleft);
6c7a06a3
TT
447
448 /* Now make sure that the object on the obstack only includes
449 bytes we have converted. */
450 obstack_blank (output, - (int) outleft);
451
452 if (r == (size_t) -1)
453 {
454 switch (errno)
455 {
456 case EILSEQ:
457 {
458 int i;
459
460 /* Invalid input sequence. */
461 if (translit == translit_none)
462 error (_("Could not convert character to `%s' character set"),
463 to);
464
465 /* We emit escape sequence for the bytes, skip them,
466 and try again. */
467 for (i = 0; i < width; ++i)
468 {
469 char octal[5];
470
471 sprintf (octal, "\\%.3o", *inp & 0xff);
472 obstack_grow_str (output, octal);
473
474 ++inp;
475 --inleft;
476 }
477 }
478 break;
479
480 case E2BIG:
481 /* We ran out of space in the output buffer. Make it
482 bigger next time around. */
483 space_request *= 2;
484 break;
485
486 case EINVAL:
487 /* Incomplete input sequence. FIXME: ought to report this
488 to the caller somehow. */
489 inleft = 0;
490 break;
491
492 default:
493 perror_with_name ("Internal error while converting character sets");
494 }
495 }
234b45d4 496 }
234b45d4 497
6c7a06a3 498 do_cleanups (cleanups);
234b45d4
KB
499}
500
e33d66ec 501\f
e33d66ec 502
6c7a06a3
TT
503/* An iterator that returns host wchar_t's from a target string. */
504struct wchar_iterator
e33d66ec 505{
6c7a06a3
TT
506 /* The underlying iconv descriptor. */
507 iconv_t desc;
e33d66ec 508
6c7a06a3
TT
509 /* The input string. This is updated as convert characters. */
510 char *input;
511 /* The number of bytes remaining in the input. */
512 size_t bytes;
e33d66ec 513
6c7a06a3
TT
514 /* The width of an input character. */
515 size_t width;
e33d66ec 516
6c7a06a3
TT
517 /* The output buffer and its size. */
518 gdb_wchar_t *out;
519 size_t out_size;
520};
234b45d4 521
6c7a06a3
TT
522/* Create a new iterator. */
523struct wchar_iterator *
524make_wchar_iterator (const gdb_byte *input, size_t bytes, const char *charset,
525 size_t width)
234b45d4 526{
6c7a06a3
TT
527 struct wchar_iterator *result;
528 iconv_t desc;
234b45d4 529
6c7a06a3
TT
530 desc = iconv_open ("wchar_t", charset);
531 if (desc == (iconv_t) -1)
532 perror_with_name ("Converting character sets");
234b45d4 533
6c7a06a3
TT
534 result = XNEW (struct wchar_iterator);
535 result->desc = desc;
536 result->input = (char *) input;
537 result->bytes = bytes;
538 result->width = width;
234b45d4 539
6c7a06a3
TT
540 result->out = XNEW (gdb_wchar_t);
541 result->out_size = 1;
234b45d4 542
6c7a06a3 543 return result;
e33d66ec 544}
234b45d4 545
e33d66ec 546static void
6c7a06a3 547do_cleanup_iterator (void *p)
e33d66ec 548{
6c7a06a3 549 struct wchar_iterator *iter = p;
234b45d4 550
6c7a06a3
TT
551 iconv_close (iter->desc);
552 xfree (iter->out);
553 xfree (iter);
234b45d4
KB
554}
555
6c7a06a3
TT
556struct cleanup *
557make_cleanup_wchar_iterator (struct wchar_iterator *iter)
e33d66ec 558{
6c7a06a3 559 return make_cleanup (do_cleanup_iterator, iter);
e33d66ec 560}
234b45d4 561
6c7a06a3
TT
562int
563wchar_iterate (struct wchar_iterator *iter,
564 enum wchar_iterate_result *out_result,
565 gdb_wchar_t **out_chars,
566 const gdb_byte **ptr,
567 size_t *len)
568{
569 size_t out_request;
570
571 /* Try to convert some characters. At first we try to convert just
572 a single character. The reason for this is that iconv does not
573 necessarily update its outgoing arguments when it encounters an
574 invalid input sequence -- but we want to reliably report this to
575 our caller so it can emit an escape sequence. */
576 out_request = 1;
577 while (iter->bytes > 0)
e33d66ec 578 {
6c7a06a3
TT
579 char *outptr = (char *) &iter->out[0];
580 char *orig_inptr = iter->input;
581 size_t orig_in = iter->bytes;
582 size_t out_avail = out_request * sizeof (gdb_wchar_t);
583 size_t num;
584 gdb_wchar_t result;
585
0dd7fb99
TT
586 size_t r = iconv (iter->desc,
587 (ICONV_CONST char **) &iter->input, &iter->bytes,
6c7a06a3
TT
588 &outptr, &out_avail);
589 if (r == (size_t) -1)
590 {
591 switch (errno)
592 {
593 case EILSEQ:
594 /* Invalid input sequence. Skip it, and let the caller
595 know about it. */
596 *out_result = wchar_iterate_invalid;
597 *ptr = iter->input;
598 *len = iter->width;
599 iter->input += iter->width;
600 iter->bytes -= iter->width;
601 return 0;
602
603 case E2BIG:
604 /* We ran out of space. We still might have converted a
605 character; if so, return it. Otherwise, grow the
606 buffer and try again. */
607 if (out_avail < out_request * sizeof (gdb_wchar_t))
608 break;
609
610 ++out_request;
611 if (out_request > iter->out_size)
612 {
613 iter->out_size = out_request;
614 iter->out = xrealloc (iter->out,
615 out_request * sizeof (gdb_wchar_t));
616 }
617 continue;
618
619 case EINVAL:
620 /* Incomplete input sequence. Let the caller know, and
621 arrange for future calls to see EOF. */
622 *out_result = wchar_iterate_incomplete;
623 *ptr = iter->input;
624 *len = iter->bytes;
625 iter->bytes = 0;
626 return 0;
627
628 default:
629 perror_with_name ("Internal error while converting character sets");
630 }
631 }
632
633 /* We converted something. */
634 num = out_request - out_avail / sizeof (gdb_wchar_t);
635 *out_result = wchar_iterate_ok;
636 *out_chars = iter->out;
637 *ptr = orig_inptr;
638 *len = orig_in - iter->bytes;
639 return num;
e33d66ec 640 }
6c7a06a3
TT
641
642 /* Really done. */
643 *out_result = wchar_iterate_eof;
644 return -1;
234b45d4
KB
645}
646
e33d66ec 647\f
6c7a06a3 648/* The charset.c module initialization function. */
234b45d4 649
6c7a06a3 650extern initialize_file_ftype _initialize_charset; /* -Wmissing-prototype */
234b45d4 651
6c7a06a3
TT
652typedef char *char_ptr;
653DEF_VEC_P (char_ptr);
234b45d4 654
6c7a06a3 655static VEC (char_ptr) *charsets;
234b45d4 656
6c7a06a3 657#ifdef PHONY_ICONV
234b45d4 658
6c7a06a3
TT
659static void
660find_charset_names (void)
234b45d4 661{
6c7a06a3
TT
662 VEC_safe_push (char_ptr, charsets, GDB_DEFAULT_HOST_CHARSET);
663 VEC_safe_push (char_ptr, charsets, NULL);
234b45d4
KB
664}
665
6c7a06a3
TT
666#else /* PHONY_ICONV */
667#ifdef HAVE_ICONVLIST
234b45d4 668
6c7a06a3
TT
669/* A helper function that adds some character sets to the vector of
670 all character sets. This is a callback function for iconvlist. */
671
672static int
673add_one (unsigned int count, const char *const *names, void *data)
234b45d4 674{
6c7a06a3 675 unsigned int i;
234b45d4 676
6c7a06a3
TT
677 for (i = 0; i < count; ++i)
678 VEC_safe_push (char_ptr, charsets, xstrdup (names[i]));
234b45d4 679
6c7a06a3 680 return 0;
234b45d4
KB
681}
682
6c7a06a3
TT
683static void
684find_charset_names (void)
234b45d4 685{
6c7a06a3
TT
686 iconvlist (add_one, NULL);
687 VEC_safe_push (char_ptr, charsets, NULL);
234b45d4
KB
688}
689
6c7a06a3 690#else
234b45d4 691
6c7a06a3
TT
692static void
693find_charset_names (void)
234b45d4 694{
6c7a06a3 695 FILE *in;
234b45d4 696
6c7a06a3
TT
697 in = popen ("iconv -l", "r");
698 /* It is ok to ignore errors; we'll fall back on a default. */
699 if (!in)
700 return;
234b45d4 701
6c7a06a3
TT
702 /* POSIX says that iconv -l uses an unspecified format. We parse
703 the glibc format; feel free to add others as needed. */
704 while (!feof (in))
705 {
706 /* The size of buf is chosen arbitrarily. A character set name
707 longer than this would not be very nice. */
708 char buf[80];
709 int len;
710 char *r = fgets (buf, sizeof (buf), in);
711 if (!r)
712 break;
713 len = strlen (r);
714 if (len <= 3)
715 continue;
716 if (buf[len - 2] == '/' && buf[len - 3] == '/')
717 buf[len - 3] = '\0';
718 VEC_safe_push (char_ptr, charsets, xstrdup (buf));
719 }
234b45d4 720
6c7a06a3 721 pclose (in);
234b45d4 722
6c7a06a3
TT
723 VEC_safe_push (char_ptr, charsets, NULL);
724}
234b45d4 725
6c7a06a3
TT
726#endif /* HAVE_ICONVLIST */
727#endif /* PHONY_ICONV */
234b45d4
KB
728
729void
730_initialize_charset (void)
731{
e33d66ec
EZ
732 struct cmd_list_element *new_cmd;
733
6c7a06a3
TT
734 /* The first element is always "auto"; then we skip it for the
735 commands where it is not allowed. */
736 VEC_safe_push (char_ptr, charsets, "auto");
737 find_charset_names ();
738
739 if (VEC_length (char_ptr, charsets) > 1)
740 charset_enum = (const char **) VEC_address (char_ptr, charsets);
741 else
742 charset_enum = default_charset_names;
743
744#ifndef PHONY_ICONV
745#ifdef HAVE_LANGINFO_CODESET
746 auto_host_charset_name = nl_langinfo (CODESET);
747 target_charset_name = auto_host_charset_name;
748
749 set_be_le_names ();
750#endif
751#endif
e33d66ec 752
7ab04401 753 add_setshow_enum_cmd ("charset", class_support,
6c7a06a3 754 &charset_enum[1], &host_charset_name, _("\
7ab04401
AC
755Set the host and target character sets."), _("\
756Show the host and target character sets."), _("\
3d263c1d
BI
757The `host character set' is the one used by the system GDB is running on.\n\
758The `target character set' is the one used by the program being debugged.\n\
759You may only use supersets of ASCII for your host character set; GDB does\n\
760not support any others.\n\
761To see a list of the character sets GDB supports, type `set charset <TAB>'."),
7ab04401
AC
762 /* Note that the sfunc below needs to set
763 target_charset_name, because the 'set
764 charset' command sets two variables. */
765 set_charset_sfunc,
766 show_charset,
767 &setlist, &showlist);
768
769 add_setshow_enum_cmd ("host-charset", class_support,
6c7a06a3 770 charset_enum, &host_charset_name, _("\
7ab04401
AC
771Set the host character set."), _("\
772Show the host character set."), _("\
3d263c1d
BI
773The `host character set' is the one used by the system GDB is running on.\n\
774You may only use supersets of ASCII for your host character set; GDB does\n\
775not support any others.\n\
776To see a list of the character sets GDB supports, type `set host-charset <TAB>'."),
7ab04401 777 set_host_charset_sfunc,
920d2a44 778 show_host_charset_name,
7ab04401
AC
779 &setlist, &showlist);
780
781 add_setshow_enum_cmd ("target-charset", class_support,
6c7a06a3 782 &charset_enum[1], &target_charset_name, _("\
7ab04401
AC
783Set the target character set."), _("\
784Show the target character set."), _("\
3d263c1d
BI
785The `target character set' is the one used by the program being debugged.\n\
786GDB translates characters and strings between the host and target\n\
787character sets as needed.\n\
788To see a list of the character sets GDB supports, type `set target-charset'<TAB>"),
7ab04401 789 set_target_charset_sfunc,
920d2a44 790 show_target_charset_name,
7ab04401 791 &setlist, &showlist);
6c7a06a3
TT
792
793 add_setshow_enum_cmd ("target-wide-charset", class_support,
794 &charset_enum[1], &target_wide_charset_name,
795 _("\
796Set the target wide character set."), _("\
797Show the target wide character set."), _("\
798The `target wide character set' is the one used by the program being debugged.\n\
799In particular it is the encoding used by `wchar_t'.\n\
800GDB translates characters and strings between the host and target\n\
801character sets as needed.\n\
802To see a list of the character sets GDB supports, type\n\
803`set target-wide-charset'<TAB>"),
804 set_target_wide_charset_sfunc,
805 show_target_wide_charset_name,
806 &setlist, &showlist);
234b45d4 807}
This page took 0.436868 seconds and 4 git commands to generate.