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