2003-05-31 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / charset.c
CommitLineData
234b45d4 1/* Character set conversion support for GDB.
1bac305b
AC
2
3 Copyright 2001, 2003 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
9 the Free Software Foundation; either version 2 of the License, or
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
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "charset.h"
24#include "gdbcmd.h"
25#include "gdb_assert.h"
26
27#include <stddef.h>
4ef3f3be 28#include "gdb_string.h"
234b45d4
KB
29#include <ctype.h>
30
31#ifdef HAVE_ICONV
32#include <iconv.h>
33#endif
34
35\f
36/* How GDB's character set support works
37
38 GDB has two global settings:
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
42 terminal knows how to display properly.
43
44 - The `current target character set' is the character set the
45 program being debugged uses.
46
47 There are commands to set each of these, and mechanisms for
48 choosing reasonable default values. GDB has a global list of
49 character sets that it can use as its host or target character
50 sets.
51
52 The header file `charset.h' declares various functions that
53 different pieces of GDB need to perform tasks like:
54
55 - printing target strings and characters to the user's terminal
56 (mostly target->host conversions),
57
58 - building target-appropriate representations of strings and
59 characters the user enters in expressions (mostly host->target
60 conversions),
61
62 and so on.
63
64 Now, many of these operations are specific to a particular
65 host/target character set pair. If GDB supports N character sets,
66 there are N^2 possible pairs. This means that, the larger GDB's
67 repertoire of character sets gets, the more expensive it gets to add
68 new character sets.
69
70 To make sure that GDB can do the right thing for every possible
71 pairing of host and target character set, while still allowing
72 GDB's repertoire to scale, we use a two-tiered approach:
73
74 - We maintain a global table of "translations" --- groups of
75 functions specific to a particular pair of character sets.
76
77 - However, a translation can be incomplete: some functions can be
78 omitted. Where there is not a translation to specify exactly
79 what function to use, we provide reasonable defaults. The
80 default behaviors try to use the "iconv" library functions, which
81 support a wide range of character sets. However, even if iconv
82 is not available, there are fallbacks to support trivial
83 translations: when the host and target character sets are the
84 same. */
85
86\f
87/* The character set and translation structures. */
88
89
90/* A character set GDB knows about. GDB only supports character sets
91 with stateless encodings, in which every character is one byte
92 long. */
93struct charset {
94
95 /* A singly-linked list of all known charsets. */
96 struct charset *next;
97
98 /* The name of the character set. Comparisons on character set
e33d66ec 99 names are case-sensitive. */
234b45d4
KB
100 const char *name;
101
102 /* Non-zero iff this character set can be used as a host character
103 set. At present, GDB basically assumes that the host character
104 set is a superset of ASCII. */
105 int valid_host_charset;
106
107 /* Pointers to charset-specific functions that depend only on a
108 single character set, and data pointers to pass to them. */
109 int (*host_char_print_literally) (void *baton,
110 int host_char);
111 void *host_char_print_literally_baton;
112
113 int (*target_char_to_control_char) (void *baton,
114 int target_char,
115 int *target_ctrl_char);
116 void *target_char_to_control_char_baton;
117};
118
119
120/* A translation from one character set to another. */
121struct translation {
122
123 /* A singly-linked list of all known translations. */
124 struct translation *next;
125
126 /* This structure describes functions going from the FROM character
127 set to the TO character set. Comparisons on character set names
e33d66ec 128 are case-sensitive. */
234b45d4
KB
129 const char *from, *to;
130
131 /* Pointers to translation-specific functions, and data pointers to
132 pass to them. These pointers can be zero, indicating that GDB
133 should fall back on the default behavior. We hope the default
134 behavior will be correct for many from/to pairs, reducing the
135 number of translations that need to be registered explicitly. */
136
137 /* TARGET_CHAR is in the `from' charset.
138 Returns a string in the `to' charset. */
139 const char *(*c_target_char_has_backslash_escape) (void *baton,
140 int target_char);
141 void *c_target_char_has_backslash_escape_baton;
142
143 /* HOST_CHAR is in the `from' charset.
144 TARGET_CHAR points to a char in the `to' charset. */
145 int (*c_parse_backslash) (void *baton, int host_char, int *target_char);
146 void *c_parse_backslash_baton;
147
148 /* This is used for the host_char_to_target and target_char_to_host
149 functions. */
150 int (*convert_char) (void *baton, int from, int *to);
151 void *convert_char_baton;
152};
153
154
155\f
156/* The global lists of character sets and translations. */
157
158
e33d66ec
EZ
159#ifndef GDB_DEFAULT_HOST_CHARSET
160#define GDB_DEFAULT_HOST_CHARSET "ISO-8859-1"
161#endif
234b45d4 162
e33d66ec
EZ
163#ifndef GDB_DEFAULT_TARGET_CHARSET
164#define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
165#endif
166
167static const char *host_charset_name = GDB_DEFAULT_HOST_CHARSET;
168static const char *target_charset_name = GDB_DEFAULT_TARGET_CHARSET;
234b45d4 169
e33d66ec
EZ
170static const char *host_charset_enum[] =
171{
172 "ASCII",
173 "ISO-8859-1",
174 0
175};
176
177static const char *target_charset_enum[] =
178{
179 "ASCII",
180 "ISO-8859-1",
181 "EBCDIC-US",
182 "IBM1047",
183 0
184};
234b45d4
KB
185
186/* The global list of all the charsets GDB knows about. */
187static struct charset *all_charsets;
188
189
190static void
191register_charset (struct charset *cs)
192{
193 struct charset **ptr;
194
195 /* Put the new charset on the end, so that the list ends up in the
196 same order as the registrations in the _initialize function. */
197 for (ptr = &all_charsets; *ptr; ptr = &(*ptr)->next)
198 ;
199
200 cs->next = 0;
201 *ptr = cs;
202}
203
204
205static struct charset *
206lookup_charset (const char *name)
207{
208 struct charset *cs;
209
210 for (cs = all_charsets; cs; cs = cs->next)
e33d66ec 211 if (! strcmp (name, cs->name))
234b45d4
KB
212 return cs;
213
214 return NULL;
215}
216
217
218/* The global list of translations. */
219static struct translation *all_translations;
220
221
222static void
223register_translation (struct translation *t)
224{
225 t->next = all_translations;
226 all_translations = t;
227}
228
229
230static struct translation *
231lookup_translation (const char *from, const char *to)
232{
233 struct translation *t;
234
235 for (t = all_translations; t; t = t->next)
e33d66ec
EZ
236 if (! strcmp (from, t->from)
237 && ! strcmp (to, t->to))
234b45d4
KB
238 return t;
239
240 return 0;
241}
242
243
244\f
245/* Constructing charsets. */
246
247/* Allocate, initialize and return a straightforward charset.
248 Use this function, rather than creating the structures yourself,
249 so that we can add new fields to the structure in the future without
250 having to tweak all the old charset descriptions. */
251static struct charset *
252simple_charset (const char *name,
253 int valid_host_charset,
254 int (*host_char_print_literally) (void *baton, int host_char),
255 void *host_char_print_literally_baton,
256 int (*target_char_to_control_char) (void *baton,
257 int target_char,
258 int *target_ctrl_char),
259 void *target_char_to_control_char_baton)
260{
261 struct charset *cs = xmalloc (sizeof (*cs));
262
263 memset (cs, 0, sizeof (*cs));
264 cs->name = name;
265 cs->valid_host_charset = valid_host_charset;
266 cs->host_char_print_literally = host_char_print_literally;
267 cs->host_char_print_literally_baton = host_char_print_literally_baton;
268 cs->target_char_to_control_char = target_char_to_control_char;
269 cs->target_char_to_control_char_baton = target_char_to_control_char_baton;
270
271 return cs;
272}
273
274
275\f
276/* ASCII functions. */
277
278static int
279ascii_print_literally (void *baton, int c)
280{
281 c &= 0xff;
282
283 return (0x20 <= c && c <= 0x7e);
284}
285
286
287static int
288ascii_to_control (void *baton, int c, int *ctrl_char)
289{
290 *ctrl_char = (c & 037);
291 return 1;
292}
293
294\f
295/* ISO-8859 family functions. */
296
297
298static int
299iso_8859_print_literally (void *baton, int c)
300{
301 c &= 0xff;
302
303 return ((0x20 <= c && c <= 0x7e) /* ascii printables */
304 || (! sevenbit_strings && 0xA0 <= c)); /* iso 8859 printables */
305}
306
307
308static int
309iso_8859_to_control (void *baton, int c, int *ctrl_char)
310{
311 *ctrl_char = (c & 0200) | (c & 037);
312 return 1;
313}
314
315
316/* Construct an ISO-8859-like character set. */
317static struct charset *
318iso_8859_family_charset (const char *name)
319{
320 return simple_charset (name, 1,
321 iso_8859_print_literally, 0,
322 iso_8859_to_control, 0);
323}
324
325
326\f
327/* EBCDIC family functions. */
328
329
330static int
331ebcdic_print_literally (void *baton, int c)
332{
333 c &= 0xff;
334
335 return (64 <= c && c <= 254);
336}
337
338
339static int
340ebcdic_to_control (void *baton, int c, int *ctrl_char)
341{
342 /* There are no control character equivalents in EBCDIC. Use
343 numeric escapes. */
344 return 0;
345}
346
347
348/* Construct an EBCDIC-like character set. */
349static struct charset *
350ebcdic_family_charset (const char *name)
351{
352 return simple_charset (name, 0,
353 ebcdic_print_literally, 0,
354 ebcdic_to_control, 0);
355}
356
357
358
359
360\f
361/* Fallback functions using iconv. */
362
363#if defined(HAVE_ICONV)
364
365struct cached_iconv {
366 struct charset *from, *to;
367 iconv_t i;
368};
369
370
371/* Make sure the iconv cache *CI contains an iconv descriptor
372 translating from FROM to TO. If it already does, fine; otherwise,
373 close any existing descriptor, and open up a new one. On success,
374 return zero; on failure, return -1 and set errno. */
375static int
376check_iconv_cache (struct cached_iconv *ci,
377 struct charset *from,
378 struct charset *to)
379{
380 iconv_t i;
381
382 /* Does the cached iconv descriptor match the conversion we're trying
383 to do now? */
384 if (ci->from == from
385 && ci->to == to
386 && ci->i != (iconv_t) 0)
387 return 0;
388
389 /* It doesn't. If we actually had any iconv descriptor open at
390 all, close it now. */
391 if (ci->i != (iconv_t) 0)
392 {
393 i = ci->i;
394 ci->i = (iconv_t) 0;
395
396 if (iconv_close (i) == -1)
397 error ("Error closing `iconv' descriptor for "
398 "`%s'-to-`%s' character conversion: %s",
399 ci->from->name, ci->to->name, safe_strerror (errno));
400 }
401
402 /* Open a new iconv descriptor for the required conversion. */
403 i = iconv_open (to->name, from->name);
404 if (i == (iconv_t) -1)
405 return -1;
406
407 ci->i = i;
408 ci->from = from;
409 ci->to = to;
410
411 return 0;
412}
413
414
415/* Convert FROM_CHAR using the cached iconv conversion *CI. Return
416 non-zero if the conversion was successful, zero otherwise. */
417static int
418cached_iconv_convert (struct cached_iconv *ci, int from_char, int *to_char)
419{
420 char from;
421 ICONV_CONST char *from_ptr = &from;
422 char to, *to_ptr = &to;
423 size_t from_left = sizeof (from), to_left = sizeof (to);
424
425 gdb_assert (ci->i != (iconv_t) 0);
426
427 from = from_char;
428 if (iconv (ci->i, &from_ptr, &from_left, &to_ptr, &to_left)
429 == (size_t) -1)
430 {
431 /* These all suggest that the input or output character sets
432 have multi-byte encodings of some characters, which means
433 it's unsuitable for use as a GDB character set. We should
434 never have selected it. */
435 gdb_assert (errno != E2BIG && errno != EINVAL);
436
437 /* This suggests a bug in the code managing *CI. */
438 gdb_assert (errno != EBADF);
439
440 /* This seems to mean that there is no equivalent character in
441 the `to' character set. */
442 if (errno == EILSEQ)
443 return 0;
444
445 /* Anything else is mysterious. */
446 internal_error ("Error converting character `%d' from `%s' to `%s' "
447 "character set: %s",
448 from_char, ci->from->name, ci->to->name,
449 safe_strerror (errno));
450 }
451
452 /* If the pointers weren't advanced across the input, that also
453 suggests something was wrong. */
454 gdb_assert (from_left == 0 && to_left == 0);
455
456 *to_char = (unsigned char) to;
457 return 1;
458}
459
460
461static void
4ef3f3be 462register_iconv_charsets (void)
234b45d4
KB
463{
464 /* Here we should check whether various character sets were
465 recognized by the local iconv implementation.
466
467 The first implementation registered a bunch of character sets
468 recognized by iconv, but then we discovered that iconv on Solaris
469 and iconv on GNU/Linux had no character sets in common. So we
470 replaced them with the hard-coded tables that appear later in the
471 file. */
472}
473
474#endif /* defined (HAVE_ICONV) */
475
476\f
477/* Fallback routines for systems without iconv. */
478
479#if ! defined (HAVE_ICONV)
480struct cached_iconv { char nothing; };
481
482static int
483check_iconv_cache (struct cached_iconv *ci,
484 struct charset *from,
485 struct charset *to)
486{
487 errno = EINVAL;
488 return -1;
489}
490
491static int
492cached_iconv_convert (struct cached_iconv *ci, int from_char, int *to_char)
493{
494 /* This function should never be called. */
495 gdb_assert (0);
496}
497
498static void
4ef3f3be 499register_iconv_charsets (void)
234b45d4
KB
500{
501}
502
503#endif /* ! defined(HAVE_ICONV) */
504
505\f
506/* Default trivial conversion functions. */
507
508static int
509identity_either_char_to_other (void *baton, int either_char, int *other_char)
510{
511 *other_char = either_char;
512 return 1;
513}
514
515
516\f
517/* Default non-trivial conversion functions. */
518
519
520static char backslashable[] = "abefnrtv";
521static char *backslashed[] = {"a", "b", "e", "f", "n", "r", "t", "v", "0"};
522static char represented[] = "\a\b\e\f\n\r\t\v";
523
524
525/* Translate TARGET_CHAR into the host character set, and see if it
526 matches any of our standard escape sequences. */
527static const char *
528default_c_target_char_has_backslash_escape (void *baton, int target_char)
529{
530 int host_char;
531 const char *ix;
532
533 /* If target_char has no equivalent in the host character set,
534 assume it doesn't have a backslashed form. */
535 if (! target_char_to_host (target_char, &host_char))
536 return NULL;
537
538 ix = strchr (represented, host_char);
539 if (ix)
540 return backslashed[ix - represented];
541 else
542 return NULL;
543}
544
545
546/* Translate the backslash the way we would in the host character set,
547 and then try to translate that into the target character set. */
548static int
549default_c_parse_backslash (void *baton, int host_char, int *target_char)
550{
551 const char *ix;
552
553 ix = strchr (backslashable, host_char);
554
555 if (! ix)
556 return 0;
557 else
558 return host_char_to_target (represented[ix - backslashable],
559 target_char);
560}
561
562
563/* Convert using a cached iconv descriptor. */
564static int
565iconv_convert (void *baton, int from_char, int *to_char)
566{
567 struct cached_iconv *ci = baton;
568 return cached_iconv_convert (ci, from_char, to_char);
569}
570
571
572\f
573/* Conversion tables. */
574
575
576/* I'd much rather fall back on iconv whenever possible. But the
577 character set names you use with iconv aren't standardized at all,
578 a lot of platforms have really meager character set coverage, etc.
579 I wanted to have at least something we could use to exercise the
580 test suite on all platforms.
581
582 In the long run, we should have a configure-time process explore
583 somehow which character sets the host platform supports, and some
584 arrangement that allows GDB users to use platform-indepedent names
585 for character sets. */
586
587
588/* We generated these tables using iconv on a GNU/Linux machine. */
589
590
591static int ascii_to_iso_8859_1_table[] = {
592 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 16 */
593 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 32 */
594 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 48 */
595 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 64 */
596 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 80 */
597 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 96 */
598 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 112 */
599 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 128 */
600 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 144 */
601 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 160 */
602 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 176 */
603 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
604 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 208 */
605 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 224 */
606 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 240 */
607 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 256 */
608};
609
610
611static int ascii_to_ebcdic_us_table[] = {
612 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15, /* 16 */
613 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31, /* 32 */
614 64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97, /* 48 */
615 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111, /* 64 */
616 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214, /* 80 */
617 215,216,217,226,227,228,229,230,231,232,233, -1,224, -1, -1,109, /* 96 */
618 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150, /* 112 */
619 151,152,153,162,163,164,165,166,167,168,169,192, 79,208,161, 7, /* 128 */
620 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 144 */
621 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 160 */
622 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 176 */
623 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
624 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 208 */
625 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 224 */
626 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 240 */
627 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 256 */
628};
629
630
631static int ascii_to_ibm1047_table[] = {
632 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15, /* 16 */
633 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31, /* 32 */
634 64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97, /* 48 */
635 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111, /* 64 */
636 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214, /* 80 */
637 215,216,217,226,227,228,229,230,231,232,233,173,224,189, 95,109, /* 96 */
638 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150, /* 112 */
639 151,152,153,162,163,164,165,166,167,168,169,192, 79,208,161, 7, /* 128 */
640 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 144 */
641 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 160 */
642 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 176 */
643 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
644 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 208 */
645 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 224 */
646 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 240 */
647 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 256 */
648};
649
650
651static int iso_8859_1_to_ascii_table[] = {
652 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 16 */
653 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 32 */
654 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 48 */
655 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 64 */
656 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 80 */
657 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 96 */
658 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 112 */
659 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 128 */
660 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 144 */
661 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 160 */
662 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 176 */
663 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
664 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 208 */
665 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 224 */
666 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 240 */
667 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 256 */
668};
669
670
671static int iso_8859_1_to_ebcdic_us_table[] = {
672 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15, /* 16 */
673 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31, /* 32 */
674 64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97, /* 48 */
675 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111, /* 64 */
676 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214, /* 80 */
677 215,216,217,226,227,228,229,230,231,232,233, -1,224, -1, -1,109, /* 96 */
678 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150, /* 112 */
679 151,152,153,162,163,164,165,166,167,168,169,192, 79,208,161, 7, /* 128 */
680 32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27, /* 144 */
681 48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,255, /* 160 */
682 -1, -1, 74, -1, -1, -1,106, -1, -1, -1, -1, -1, 95, -1, -1, -1, /* 176 */
683 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
684 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 208 */
685 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 224 */
686 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 240 */
687 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 256 */
688};
689
690
691static int iso_8859_1_to_ibm1047_table[] = {
692 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15, /* 16 */
693 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31, /* 32 */
694 64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97, /* 48 */
695 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111, /* 64 */
696 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214, /* 80 */
697 215,216,217,226,227,228,229,230,231,232,233,173,224,189, 95,109, /* 96 */
698 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150, /* 112 */
699 151,152,153,162,163,164,165,166,167,168,169,192, 79,208,161, 7, /* 128 */
700 32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27, /* 144 */
701 48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,255, /* 160 */
702 65,170, 74,177,159,178,106,181,187,180,154,138,176,202,175,188, /* 176 */
703 144,143,234,250,190,160,182,179,157,218,155,139,183,184,185,171, /* 192 */
704 100,101, 98,102, 99,103,158,104,116,113,114,115,120,117,118,119, /* 208 */
705 172,105,237,238,235,239,236,191,128,253,254,251,252,186,174, 89, /* 224 */
706 68, 69, 66, 70, 67, 71,156, 72, 84, 81, 82, 83, 88, 85, 86, 87, /* 240 */
707 140, 73,205,206,203,207,204,225,112,221,222,219,220,141,142,223 /* 256 */
708};
709
710
711static int ebcdic_us_to_ascii_table[] = {
712 0, 1, 2, 3, -1, 9, -1,127, -1, -1, -1, 11, 12, 13, 14, 15, /* 16 */
713 16, 17, 18, 19, -1, -1, 8, -1, 24, 25, -1, -1, 28, 29, 30, 31, /* 32 */
714 -1, -1, -1, -1, -1, 10, 23, 27, -1, -1, -1, -1, -1, 5, 6, 7, /* 48 */
715 -1, -1, 22, -1, -1, -1, -1, 4, -1, -1, -1, -1, 20, 21, -1, 26, /* 64 */
716 32, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 46, 60, 40, 43,124, /* 80 */
717 38, -1, -1, -1, -1, -1, -1, -1, -1, -1, 33, 36, 42, 41, 59, -1, /* 96 */
718 45, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, 44, 37, 95, 62, 63, /* 112 */
719 -1, -1, -1, -1, -1, -1, -1, -1, -1, 96, 58, 35, 64, 39, 61, 34, /* 128 */
720 -1, 97, 98, 99,100,101,102,103,104,105, -1, -1, -1, -1, -1, -1, /* 144 */
721 -1,106,107,108,109,110,111,112,113,114, -1, -1, -1, -1, -1, -1, /* 160 */
722 -1,126,115,116,117,118,119,120,121,122, -1, -1, -1, -1, -1, -1, /* 176 */
723 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
724 123, 65, 66, 67, 68, 69, 70, 71, 72, 73, -1, -1, -1, -1, -1, -1, /* 208 */
725 125, 74, 75, 76, 77, 78, 79, 80, 81, 82, -1, -1, -1, -1, -1, -1, /* 224 */
726 92, -1, 83, 84, 85, 86, 87, 88, 89, 90, -1, -1, -1, -1, -1, -1, /* 240 */
727 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1, -1 /* 256 */
728};
729
730
731static int ebcdic_us_to_iso_8859_1_table[] = {
732 0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15, /* 16 */
733 16, 17, 18, 19,157,133, 8,135, 24, 25,146,143, 28, 29, 30, 31, /* 32 */
734 128,129,130,131,132, 10, 23, 27,136,137,138,139,140, 5, 6, 7, /* 48 */
735 144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26, /* 64 */
736 32, -1, -1, -1, -1, -1, -1, -1, -1, -1,162, 46, 60, 40, 43,124, /* 80 */
737 38, -1, -1, -1, -1, -1, -1, -1, -1, -1, 33, 36, 42, 41, 59,172, /* 96 */
738 45, 47, -1, -1, -1, -1, -1, -1, -1, -1,166, 44, 37, 95, 62, 63, /* 112 */
739 -1, -1, -1, -1, -1, -1, -1, -1, -1, 96, 58, 35, 64, 39, 61, 34, /* 128 */
740 -1, 97, 98, 99,100,101,102,103,104,105, -1, -1, -1, -1, -1, -1, /* 144 */
741 -1,106,107,108,109,110,111,112,113,114, -1, -1, -1, -1, -1, -1, /* 160 */
742 -1,126,115,116,117,118,119,120,121,122, -1, -1, -1, -1, -1, -1, /* 176 */
743 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
744 123, 65, 66, 67, 68, 69, 70, 71, 72, 73, -1, -1, -1, -1, -1, -1, /* 208 */
745 125, 74, 75, 76, 77, 78, 79, 80, 81, 82, -1, -1, -1, -1, -1, -1, /* 224 */
746 92, -1, 83, 84, 85, 86, 87, 88, 89, 90, -1, -1, -1, -1, -1, -1, /* 240 */
747 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,159 /* 256 */
748};
749
750
751static int ebcdic_us_to_ibm1047_table[] = {
752 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 16 */
753 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 32 */
754 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 48 */
755 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 64 */
756 64, -1, -1, -1, -1, -1, -1, -1, -1, -1, 74, 75, 76, 77, 78, 79, /* 80 */
757 80, -1, -1, -1, -1, -1, -1, -1, -1, -1, 90, 91, 92, 93, 94,176, /* 96 */
758 96, 97, -1, -1, -1, -1, -1, -1, -1, -1,106,107,108,109,110,111, /* 112 */
759 -1, -1, -1, -1, -1, -1, -1, -1, -1,121,122,123,124,125,126,127, /* 128 */
760 -1,129,130,131,132,133,134,135,136,137, -1, -1, -1, -1, -1, -1, /* 144 */
761 -1,145,146,147,148,149,150,151,152,153, -1, -1, -1, -1, -1, -1, /* 160 */
762 -1,161,162,163,164,165,166,167,168,169, -1, -1, -1, -1, -1, -1, /* 176 */
763 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
764 192,193,194,195,196,197,198,199,200,201, -1, -1, -1, -1, -1, -1, /* 208 */
765 208,209,210,211,212,213,214,215,216,217, -1, -1, -1, -1, -1, -1, /* 224 */
766 224, -1,226,227,228,229,230,231,232,233, -1, -1, -1, -1, -1, -1, /* 240 */
767 240,241,242,243,244,245,246,247,248,249, -1, -1, -1, -1, -1,255 /* 256 */
768};
769
770
771static int ibm1047_to_ascii_table[] = {
772 0, 1, 2, 3, -1, 9, -1,127, -1, -1, -1, 11, 12, 13, 14, 15, /* 16 */
773 16, 17, 18, 19, -1, -1, 8, -1, 24, 25, -1, -1, 28, 29, 30, 31, /* 32 */
774 -1, -1, -1, -1, -1, 10, 23, 27, -1, -1, -1, -1, -1, 5, 6, 7, /* 48 */
775 -1, -1, 22, -1, -1, -1, -1, 4, -1, -1, -1, -1, 20, 21, -1, 26, /* 64 */
776 32, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 46, 60, 40, 43,124, /* 80 */
777 38, -1, -1, -1, -1, -1, -1, -1, -1, -1, 33, 36, 42, 41, 59, 94, /* 96 */
778 45, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, 44, 37, 95, 62, 63, /* 112 */
779 -1, -1, -1, -1, -1, -1, -1, -1, -1, 96, 58, 35, 64, 39, 61, 34, /* 128 */
780 -1, 97, 98, 99,100,101,102,103,104,105, -1, -1, -1, -1, -1, -1, /* 144 */
781 -1,106,107,108,109,110,111,112,113,114, -1, -1, -1, -1, -1, -1, /* 160 */
782 -1,126,115,116,117,118,119,120,121,122, -1, -1, -1, 91, -1, -1, /* 176 */
783 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, -1, -1, /* 192 */
784 123, 65, 66, 67, 68, 69, 70, 71, 72, 73, -1, -1, -1, -1, -1, -1, /* 208 */
785 125, 74, 75, 76, 77, 78, 79, 80, 81, 82, -1, -1, -1, -1, -1, -1, /* 224 */
786 92, -1, 83, 84, 85, 86, 87, 88, 89, 90, -1, -1, -1, -1, -1, -1, /* 240 */
787 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1, -1 /* 256 */
788};
789
790
791static int ibm1047_to_iso_8859_1_table[] = {
792 0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15, /* 16 */
793 16, 17, 18, 19,157,133, 8,135, 24, 25,146,143, 28, 29, 30, 31, /* 32 */
794 128,129,130,131,132, 10, 23, 27,136,137,138,139,140, 5, 6, 7, /* 48 */
795 144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26, /* 64 */
796 32,160,226,228,224,225,227,229,231,241,162, 46, 60, 40, 43,124, /* 80 */
797 38,233,234,235,232,237,238,239,236,223, 33, 36, 42, 41, 59, 94, /* 96 */
798 45, 47,194,196,192,193,195,197,199,209,166, 44, 37, 95, 62, 63, /* 112 */
799 248,201,202,203,200,205,206,207,204, 96, 58, 35, 64, 39, 61, 34, /* 128 */
800 216, 97, 98, 99,100,101,102,103,104,105,171,187,240,253,254,177, /* 144 */
801 176,106,107,108,109,110,111,112,113,114,170,186,230,184,198,164, /* 160 */
802 181,126,115,116,117,118,119,120,121,122,161,191,208, 91,222,174, /* 176 */
803 172,163,165,183,169,167,182,188,189,190,221,168,175, 93,180,215, /* 192 */
804 123, 65, 66, 67, 68, 69, 70, 71, 72, 73,173,244,246,242,243,245, /* 208 */
805 125, 74, 75, 76, 77, 78, 79, 80, 81, 82,185,251,252,249,250,255, /* 224 */
806 92,247, 83, 84, 85, 86, 87, 88, 89, 90,178,212,214,210,211,213, /* 240 */
807 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,179,219,220,217,218,159 /* 256 */
808};
809
810
811static int ibm1047_to_ebcdic_us_table[] = {
812 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 16 */
813 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 32 */
814 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 48 */
815 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 64 */
816 64, -1, -1, -1, -1, -1, -1, -1, -1, -1, 74, 75, 76, 77, 78, 79, /* 80 */
817 80, -1, -1, -1, -1, -1, -1, -1, -1, -1, 90, 91, 92, 93, 94, -1, /* 96 */
818 96, 97, -1, -1, -1, -1, -1, -1, -1, -1,106,107,108,109,110,111, /* 112 */
819 -1, -1, -1, -1, -1, -1, -1, -1, -1,121,122,123,124,125,126,127, /* 128 */
820 -1,129,130,131,132,133,134,135,136,137, -1, -1, -1, -1, -1, -1, /* 144 */
821 -1,145,146,147,148,149,150,151,152,153, -1, -1, -1, -1, -1, -1, /* 160 */
822 -1,161,162,163,164,165,166,167,168,169, -1, -1, -1, -1, -1, -1, /* 176 */
823 95, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 192 */
824 192,193,194,195,196,197,198,199,200,201, -1, -1, -1, -1, -1, -1, /* 208 */
825 208,209,210,211,212,213,214,215,216,217, -1, -1, -1, -1, -1, -1, /* 224 */
826 224, -1,226,227,228,229,230,231,232,233, -1, -1, -1, -1, -1, -1, /* 240 */
827 240,241,242,243,244,245,246,247,248,249, -1, -1, -1, -1, -1,255 /* 256 */
828};
829
830
831static int
832table_convert_char (void *baton, int from, int *to)
833{
834 int *table = (int *) baton;
835
836 if (0 <= from && from <= 255
837 && table[from] != -1)
838 {
839 *to = table[from];
840 return 1;
841 }
842 else
843 return 0;
844}
845
846
847static struct translation *
848table_translation (const char *from, const char *to, int *table,
849 const char *(*c_target_char_has_backslash_escape)
850 (void *baton, int target_char),
851 void *c_target_char_has_backslash_escape_baton,
852 int (*c_parse_backslash) (void *baton,
853 int host_char,
854 int *target_char),
855 void *c_parse_backslash_baton)
856{
857 struct translation *t = xmalloc (sizeof (*t));
858
859 memset (t, 0, sizeof (*t));
860 t->from = from;
861 t->to = to;
862 t->c_target_char_has_backslash_escape = c_target_char_has_backslash_escape;
863 t->c_target_char_has_backslash_escape_baton
864 = c_target_char_has_backslash_escape_baton;
865 t->c_parse_backslash = c_parse_backslash;
866 t->c_parse_backslash_baton = c_parse_backslash_baton;
867 t->convert_char = table_convert_char;
868 t->convert_char_baton = (void *) table;
869
870 return t;
871}
872
873
874static struct translation *
875simple_table_translation (const char *from, const char *to, int *table)
876{
877 return table_translation (from, to, table, 0, 0, 0, 0);
878}
879
880
881\f
882/* Setting and retrieving the host and target charsets. */
883
884
885/* The current host and target character sets. */
886static struct charset *current_host_charset, *current_target_charset;
887
888/* The current functions and batons we should use for the functions in
889 charset.h. */
890
891static const char *(*c_target_char_has_backslash_escape_func)
892 (void *baton, int target_char);
893static void *c_target_char_has_backslash_escape_baton;
894
895static int (*c_parse_backslash_func) (void *baton,
896 int host_char,
897 int *target_char);
898static void *c_parse_backslash_baton;
899
900static int (*host_char_to_target_func) (void *baton,
901 int host_char,
902 int *target_char);
903static void *host_char_to_target_baton;
904
905static int (*target_char_to_host_func) (void *baton,
906 int target_char,
907 int *host_char);
908static void *target_char_to_host_baton;
909
910
911/* Cached iconv conversions, that might be useful to fallback
912 routines. */
913static struct cached_iconv cached_iconv_host_to_target;
914static struct cached_iconv cached_iconv_target_to_host;
915
e33d66ec
EZ
916\f
917/* Charset structures manipulation functions. */
918
919static struct charset *
920lookup_charset_or_error (const char *name)
921{
922 struct charset *cs = lookup_charset (name);
923
924 if (! cs)
925 error ("GDB doesn't know of any character set named `%s'.", name);
926
927 return cs;
928}
929
930static void
931check_valid_host_charset (struct charset *cs)
932{
933 if (! cs->valid_host_charset)
934 error ("GDB can't use `%s' as its host character set.", cs->name);
935}
234b45d4
KB
936
937/* Set the host and target character sets to HOST and TARGET. */
938static void
939set_host_and_target_charsets (struct charset *host, struct charset *target)
940{
941 struct translation *h2t, *t2h;
942
943 /* If they're not both initialized yet, then just do nothing for
944 now. As soon as we're done running our initialize function,
945 everything will be initialized. */
946 if (! host || ! target)
947 {
948 current_host_charset = host;
949 current_target_charset = target;
950 return;
951 }
952
953 h2t = lookup_translation (host->name, target->name);
954 t2h = lookup_translation (target->name, host->name);
955
956 /* If the translations don't provide conversion functions, make sure
957 iconv can back them up. Do this *before* modifying any state. */
958 if (host != target)
959 {
960 if (! h2t || ! h2t->convert_char)
961 {
962 if (check_iconv_cache (&cached_iconv_host_to_target, host, target)
963 < 0)
964 error ("GDB can't convert from the `%s' character set to `%s'.",
965 host->name, target->name);
966 }
967 if (! t2h || ! t2h->convert_char)
968 {
969 if (check_iconv_cache (&cached_iconv_target_to_host, target, host)
970 < 0)
971 error ("GDB can't convert from the `%s' character set to `%s'.",
972 target->name, host->name);
973 }
974 }
975
976 if (t2h && t2h->c_target_char_has_backslash_escape)
977 {
978 c_target_char_has_backslash_escape_func
979 = t2h->c_target_char_has_backslash_escape;
980 c_target_char_has_backslash_escape_baton
981 = t2h->c_target_char_has_backslash_escape_baton;
982 }
983 else
984 c_target_char_has_backslash_escape_func
985 = default_c_target_char_has_backslash_escape;
986
987 if (h2t && h2t->c_parse_backslash)
988 {
989 c_parse_backslash_func = h2t->c_parse_backslash;
990 c_parse_backslash_baton = h2t->c_parse_backslash_baton;
991 }
992 else
993 c_parse_backslash_func = default_c_parse_backslash;
994
995 if (h2t && h2t->convert_char)
996 {
997 host_char_to_target_func = h2t->convert_char;
998 host_char_to_target_baton = h2t->convert_char_baton;
999 }
1000 else if (host == target)
1001 host_char_to_target_func = identity_either_char_to_other;
1002 else
1003 {
1004 host_char_to_target_func = iconv_convert;
1005 host_char_to_target_baton = &cached_iconv_host_to_target;
1006 }
1007
1008 if (t2h && t2h->convert_char)
1009 {
1010 target_char_to_host_func = t2h->convert_char;
1011 target_char_to_host_baton = t2h->convert_char_baton;
1012 }
1013 else if (host == target)
1014 target_char_to_host_func = identity_either_char_to_other;
1015 else
1016 {
1017 target_char_to_host_func = iconv_convert;
1018 target_char_to_host_baton = &cached_iconv_target_to_host;
1019 }
1020
1021 current_host_charset = host;
1022 current_target_charset = target;
1023}
1024
e33d66ec
EZ
1025/* Do the real work of setting the host charset. */
1026static void
1027set_host_charset (const char *charset)
1028{
1029 struct charset *cs = lookup_charset_or_error (charset);
1030 check_valid_host_charset (cs);
1031 set_host_and_target_charsets (cs, current_target_charset);
1032}
234b45d4 1033
e33d66ec
EZ
1034/* Do the real work of setting the target charset. */
1035static void
1036set_target_charset (const char *charset)
234b45d4 1037{
e33d66ec 1038 struct charset *cs = lookup_charset_or_error (charset);
234b45d4 1039
e33d66ec
EZ
1040 set_host_and_target_charsets (current_host_charset, cs);
1041}
234b45d4 1042
e33d66ec
EZ
1043\f
1044/* 'Set charset', 'set host-charset', 'set target-charset', 'show
1045 charset' sfunc's. */
1046
1047/* This is the sfunc for the 'set charset' command. */
1048static void
1049set_charset_sfunc (char *charset, int from_tty, struct cmd_list_element *c)
1050{
1051 struct charset *cs = lookup_charset_or_error (host_charset_name);
1052 check_valid_host_charset (cs);
1053 /* CAREFUL: set the target charset here as well. */
1054 target_charset_name = host_charset_name;
1055 set_host_and_target_charsets (cs, cs);
234b45d4 1056}
234b45d4 1057
e33d66ec
EZ
1058/* 'set host-charset' command sfunc. We need a wrapper here because
1059 the function needs to have a specific signature. */
234b45d4 1060static void
e33d66ec
EZ
1061set_host_charset_sfunc (char *charset, int from_tty,
1062 struct cmd_list_element *c)
234b45d4 1063{
e33d66ec 1064 set_host_charset (host_charset_name);
234b45d4
KB
1065}
1066
e33d66ec
EZ
1067/* Wrapper for the 'set target-charset' command. */
1068static void
1069set_target_charset_sfunc (char *charset, int from_tty,
1070 struct cmd_list_element *c)
1071{
1072 set_target_charset (target_charset_name);
1073}
234b45d4 1074
e33d66ec
EZ
1075/* sfunc for the 'show charset' command. */
1076static void
1077show_charset (char *arg, int from_tty)
234b45d4 1078{
e33d66ec
EZ
1079 if (current_host_charset == current_target_charset)
1080 {
1081 printf_filtered ("The current host and target character set is `%s'.\n",
1082 host_charset ());
1083 }
1084 else
1085 {
1086 printf_filtered ("The current host character set is `%s'.\n",
1087 host_charset ());
1088 printf_filtered ("The current target character set is `%s'.\n",
1089 target_charset ());
1090 }
234b45d4
KB
1091}
1092
e33d66ec
EZ
1093\f
1094/* Accessor functions. */
234b45d4
KB
1095
1096const char *
4ef3f3be 1097host_charset (void)
234b45d4
KB
1098{
1099 return current_host_charset->name;
1100}
1101
234b45d4 1102const char *
4ef3f3be 1103target_charset (void)
234b45d4
KB
1104{
1105 return current_target_charset->name;
1106}
1107
1108
1109\f
1110/* Public character management functions. */
1111
1112
1113const char *
1114c_target_char_has_backslash_escape (int target_char)
1115{
1116 return ((*c_target_char_has_backslash_escape_func)
1117 (c_target_char_has_backslash_escape_baton, target_char));
1118}
1119
1120
1121int
1122c_parse_backslash (int host_char, int *target_char)
1123{
1124 return (*c_parse_backslash_func) (c_parse_backslash_baton,
1125 host_char, target_char);
1126}
1127
1128
1129int
1130host_char_print_literally (int host_char)
1131{
1132 return ((*current_host_charset->host_char_print_literally)
1133 (current_host_charset->host_char_print_literally_baton,
1134 host_char));
1135}
1136
1137
1138int
1139target_char_to_control_char (int target_char, int *target_ctrl_char)
1140{
1141 return ((*current_target_charset->target_char_to_control_char)
1142 (current_target_charset->target_char_to_control_char_baton,
1143 target_char, target_ctrl_char));
1144}
1145
1146
1147int
1148host_char_to_target (int host_char, int *target_char)
1149{
1150 return ((*host_char_to_target_func)
1151 (host_char_to_target_baton, host_char, target_char));
1152}
1153
1154
1155int
1156target_char_to_host (int target_char, int *host_char)
1157{
1158 return ((*target_char_to_host_func)
1159 (target_char_to_host_baton, target_char, host_char));
1160}
1161
1162
234b45d4
KB
1163\f
1164/* The charset.c module initialization function. */
1165
234b45d4
KB
1166
1167void
1168_initialize_charset (void)
1169{
e33d66ec
EZ
1170 struct cmd_list_element *new_cmd;
1171
234b45d4
KB
1172 /* Register all the character set GDB knows about.
1173
1174 You should use the same names that iconv does, where possible, to
1175 take advantage of the iconv-based default behaviors.
1176
1177 CAUTION: if you register a character set, you must also register
1178 as many translations as are necessary to make that character set
1179 interoperate correctly with all the other character sets. We do
1180 provide default behaviors when no translation is available, or
1181 when a translation's function pointer for a particular operation
1182 is zero. Hopefully, these defaults will be correct often enough
1183 that we won't need to provide too many translations. */
e33d66ec 1184 register_charset (simple_charset ("ASCII", 1,
234b45d4
KB
1185 ascii_print_literally, 0,
1186 ascii_to_control, 0));
e33d66ec
EZ
1187 register_charset (iso_8859_family_charset ("ISO-8859-1"));
1188 register_charset (ebcdic_family_charset ("EBCDIC-US"));
1189 register_charset (ebcdic_family_charset ("IBM1047"));
234b45d4
KB
1190 register_iconv_charsets ();
1191
1192 {
1193 struct { char *from; char *to; int *table; } tlist[] = {
e33d66ec
EZ
1194 { "ASCII", "ISO-8859-1", ascii_to_iso_8859_1_table },
1195 { "ASCII", "EBCDIC-US", ascii_to_ebcdic_us_table },
1196 { "ASCII", "IBM1047", ascii_to_ibm1047_table },
1197 { "ISO-8859-1", "ASCII", iso_8859_1_to_ascii_table },
1198 { "ISO-8859-1", "EBCDIC-US", iso_8859_1_to_ebcdic_us_table },
1199 { "ISO-8859-1", "IBM1047", iso_8859_1_to_ibm1047_table },
1200 { "EBCDIC-US", "ASCII", ebcdic_us_to_ascii_table },
1201 { "EBCDIC-US", "ISO-8859-1", ebcdic_us_to_iso_8859_1_table },
1202 { "EBCDIC-US", "IBM1047", ebcdic_us_to_ibm1047_table },
1203 { "IBM1047", "ASCII", ibm1047_to_ascii_table },
1204 { "IBM1047", "ISO-8859-1", ibm1047_to_iso_8859_1_table },
1205 { "IBM1047", "EBCDIC-US", ibm1047_to_ebcdic_us_table }
234b45d4
KB
1206 };
1207
1208 int i;
1209
1210 for (i = 0; i < (sizeof (tlist) / sizeof (tlist[0])); i++)
1211 register_translation (simple_table_translation (tlist[i].from,
1212 tlist[i].to,
1213 tlist[i].table));
1214 }
1215
e33d66ec
EZ
1216 set_host_charset (host_charset_name);
1217 set_target_charset (target_charset_name);
1218
1219 new_cmd = add_set_enum_cmd ("charset",
1220 class_support,
1221 host_charset_enum,
1222 &host_charset_name,
1223 "Set the host and target character sets.\n"
1224 "The `host character set' is the one used by the system GDB is running on.\n"
1225 "The `target character set' is the one used by the program being debugged.\n"
1226 "You may only use supersets of ASCII for your host character set; GDB does\n"
1227 "not support any others.\n"
1228 "To see a list of the character sets GDB supports, type `set charset <TAB>'.",
1229 &setlist);
1230
1231 /* Note that the sfunc below needs to set target_charset_name, because
1232 the 'set charset' command sets two variables. */
1233 set_cmd_sfunc (new_cmd, set_charset_sfunc);
1234 /* Don't use set_from_show - need to print some extra info. */
1235 add_cmd ("charset", class_support, show_charset,
1236 "Show the host and target character sets.\n"
1237 "The `host character set' is the one used by the system GDB is running on.\n"
1238 "The `target character set' is the one used by the program being debugged.\n"
1239 "You may only use supersets of ASCII for your host character set; GDB does\n"
1240 "not support any others.\n"
1241 "To see a list of the character sets GDB supports, type `set charset <TAB>'.",
1242 &showlist);
1243
1244
1245 new_cmd = add_set_enum_cmd ("host-charset",
1246 class_support,
1247 host_charset_enum,
1248 &host_charset_name,
1249 "Set the host character set.\n"
1250 "The `host character set' is the one used by the system GDB is running on.\n"
1251 "You may only use supersets of ASCII for your host character set; GDB does\n"
1252 "not support any others.\n"
1253 "To see a list of the character sets GDB supports, type `set host-charset <TAB>'.",
1254 &setlist);
1255
1256 set_cmd_sfunc (new_cmd, set_host_charset_sfunc);
1257
1258 add_show_from_set (new_cmd, &showlist);
1259
1260
1261
1262 new_cmd = add_set_enum_cmd ("target-charset",
1263 class_support,
1264 target_charset_enum,
1265 &target_charset_name,
1266 "Set the target character set.\n"
1267 "The `target character set' is the one used by the program being debugged.\n"
1268 "GDB translates characters and strings between the host and target\n"
1269 "character sets as needed.\n"
1270 "To see a list of the character sets GDB supports, type `set target-charset'<TAB>",
1271 &setlist);
1272
1273 set_cmd_sfunc (new_cmd, set_target_charset_sfunc);
1274 add_show_from_set (new_cmd, &showlist);
234b45d4 1275}
This page took 0.1212 seconds and 4 git commands to generate.