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