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