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