Rename fprintf_symbol_filtered
[binutils-gdb.git] / gdb / charset.c
index fcb24a488239b57ef022c1b0c5d6971b8476584d..74f742e0aa70d30765f5ea97136b79f00454365b 100644 (file)
@@ -1,6 +1,6 @@
 /* Character set conversion support for GDB.
 
-   Copyright (C) 2001-2018 Free Software Foundation, Inc.
+   Copyright (C) 2001-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "defs.h"
 #include "charset.h"
 #include "gdbcmd.h"
-#include "gdb_obstack.h"
-#include "gdb_wait.h"
+#include "gdbsupport/gdb_obstack.h"
+#include "gdbsupport/gdb_wait.h"
 #include "charset-list.h"
-#include "vec.h"
-#include "environ.h"
+#include "gdbsupport/environ.h"
 #include "arch-utils.h"
-#include "gdb_vecs.h"
+#include "gdbsupport/gdb_vecs.h"
 #include <ctype.h>
 
 #ifdef USE_WIN32API
@@ -233,11 +232,11 @@ show_host_charset_name (struct ui_file *file, int from_tty,
                        const char *value)
 {
   if (!strcmp (value, "auto"))
-    fprintf_filtered (file,
-                     _("The host character set is \"auto; currently %s\".\n"),
-                     auto_host_charset_name);
+    gdb_printf (file,
+               _("The host character set is \"auto; currently %s\".\n"),
+               auto_host_charset_name);
   else
-    fprintf_filtered (file, _("The host character set is \"%s\".\n"), value);
+    gdb_printf (file, _("The host character set is \"%s\".\n"), value);
 }
 
 static const char *target_charset_name = "auto";
@@ -246,13 +245,13 @@ show_target_charset_name (struct ui_file *file, int from_tty,
                          struct cmd_list_element *c, const char *value)
 {
   if (!strcmp (value, "auto"))
-    fprintf_filtered (file,
-                     _("The target character set is \"auto; "
-                       "currently %s\".\n"),
-                     gdbarch_auto_charset (get_current_arch ()));
+    gdb_printf (file,
+               _("The target character set is \"auto; "
+                 "currently %s\".\n"),
+               gdbarch_auto_charset (get_current_arch ()));
   else
-    fprintf_filtered (file, _("The target character set is \"%s\".\n"),
-                     value);
+    gdb_printf (file, _("The target character set is \"%s\".\n"),
+               value);
 }
 
 static const char *target_wide_charset_name = "auto";
@@ -263,22 +262,22 @@ show_target_wide_charset_name (struct ui_file *file,
                               const char *value)
 {
   if (!strcmp (value, "auto"))
-    fprintf_filtered (file,
-                     _("The target wide character set is \"auto; "
-                       "currently %s\".\n"),
-                     gdbarch_auto_wide_charset (get_current_arch ()));
+    gdb_printf (file,
+               _("The target wide character set is \"auto; "
+                 "currently %s\".\n"),
+               gdbarch_auto_wide_charset (get_current_arch ()));
   else
-    fprintf_filtered (file, _("The target wide character set is \"%s\".\n"),
-                     value);
+    gdb_printf (file, _("The target wide character set is \"%s\".\n"),
+               value);
 }
 
-static const char *default_charset_names[] =
+static const char * const default_charset_names[] =
 {
   DEFAULT_CHARSET_NAMES
   0
 };
 
-static const char **charset_enum;
+static const char * const *charset_enum;
 
 \f
 /* If the target wide character set has big- or little-endian
@@ -295,9 +294,6 @@ static struct gdbarch *be_le_arch;
 static void
 set_be_le_names (struct gdbarch *gdbarch)
 {
-  int i, len;
-  const char *target_wide;
-
   if (be_le_arch == gdbarch)
     return;
   be_le_arch = gdbarch;
@@ -307,6 +303,9 @@ set_be_le_names (struct gdbarch *gdbarch)
   target_wide_charset_le_name = "UTF-32LE";
   target_wide_charset_be_name = "UTF-32BE";
 #else
+  int i, len;
+  const char *target_wide;
+
   target_wide_charset_le_name = NULL;
   target_wide_charset_be_name = NULL;
 
@@ -464,20 +463,6 @@ host_letter_to_control_character (char c)
   return c & 0237;
 }
 
-/* Convert a host character, C, to its hex value.  C must already have
-   been validated using isxdigit.  */
-
-int
-host_hex_value (char c)
-{
-  if (isdigit (c))
-    return c - '0';
-  if (c >= 'a' && c <= 'f')
-    return 10 + c - 'a';
-  gdb_assert (c >= 'A' && c <= 'F');
-  return 10 + c - 'A';
-}
-
 \f
 /* Public character management functions.  */
 
@@ -548,7 +533,7 @@ convert_between_encodings (const char *from, const char *to,
 
       /* Now make sure that the object on the obstack only includes
         bytes we have converted.  */
-      obstack_blank_fast (output, -outleft);
+      obstack_blank_fast (output, -(ssize_t) outleft);
 
       if (r == (size_t) -1)
        {
@@ -817,10 +802,10 @@ find_charset_names (void)
 
 #ifdef ICONV_BIN
   {
-    char *iconv_dir = relocate_gdb_directory (ICONV_BIN,
-                                             ICONV_BIN_RELOCATABLE);
-    iconv_program = concat (iconv_dir, SLASH_STRING, "iconv", NULL);
-    xfree (iconv_dir);
+    std::string iconv_dir = relocate_gdb_directory (ICONV_BIN,
+                                                   ICONV_BIN_RELOCATABLE);
+    iconv_program
+      = concat (iconv_dir.c_str(), SLASH_STRING, "iconv", (char *) NULL);
   }
 #else
   iconv_program = xstrdup ("iconv");
@@ -946,15 +931,9 @@ default_auto_wide_charset (void)
 #define ENDIAN_SUFFIX "LE"
 #endif
 
-/* The code below serves to generate a compile time error if
-   gdb_wchar_t type is not of size 2 nor 4, despite the fact that
-   macro __STDC_ISO_10646__ is defined.
-   This is better than a gdb_assert call, because GDB cannot handle
-   strings correctly if this size is different.  */
+/* GDB cannot handle strings correctly if this size is different.  */
 
-extern char your_gdb_wchar_t_is_bogus[(sizeof (gdb_wchar_t) == 2
-                                      || sizeof (gdb_wchar_t) == 4)
-                                     ? 1 : -1];
+gdb_static_assert (sizeof (gdb_wchar_t) == 2 || sizeof (gdb_wchar_t) == 4);
 
 /* intermediate_encoding returns the charset used internally by
    GDB to convert between target and host encodings. As the test above
@@ -967,50 +946,47 @@ intermediate_encoding (void)
 {
   iconv_t desc;
   static const char *stored_result = NULL;
-  char *result;
+  gdb::unique_xmalloc_ptr<char> result;
 
   if (stored_result)
     return stored_result;
   result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8),
                       ENDIAN_SUFFIX);
   /* Check that the name is supported by iconv_open.  */
-  desc = iconv_open (result, host_charset ());
+  desc = iconv_open (result.get (), host_charset ());
   if (desc != (iconv_t) -1)
     {
       iconv_close (desc);
-      stored_result = result;
-      return result;
+      stored_result = result.release ();
+      return stored_result;
     }
-  /* Not valid, free the allocated memory.  */
-  xfree (result);
   /* Second try, with UCS-2 type.  */
   result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t),
                       ENDIAN_SUFFIX);
   /* Check that the name is supported by iconv_open.  */
-  desc = iconv_open (result, host_charset ());
+  desc = iconv_open (result.get (), host_charset ());
   if (desc != (iconv_t) -1)
     {
       iconv_close (desc);
-      stored_result = result;
-      return result;
+      stored_result = result.release ();
+      return stored_result;
     }
-  /* Not valid, free the allocated memory.  */
-  xfree (result);
   /* No valid charset found, generate error here.  */
-  error (_("Unable to find a vaild charset for string conversions"));
+  error (_("Unable to find a valid charset for string conversions"));
 }
 
 #endif /* USE_INTERMEDIATE_ENCODING_FUNCTION */
 
+void _initialize_charset ();
 void
-_initialize_charset (void)
+_initialize_charset ()
 {
   /* The first element is always "auto".  */
   charsets.charsets.push_back (xstrdup ("auto"));
   find_charset_names ();
 
   if (charsets.charsets.size () > 1)
-    charset_enum = (const char **) charsets.charsets.data ();
+    charset_enum = (const char * const *) charsets.charsets.data ();
   else
     charset_enum = default_charset_names;
 
@@ -1039,6 +1015,9 @@ _initialize_charset (void)
 #endif
 #endif
 
+  /* Recall that the first element is always "auto".  */
+  host_charset_name = charset_enum[0];
+  gdb_assert (strcmp (host_charset_name, "auto") == 0);
   add_setshow_enum_cmd ("charset", class_support,
                        charset_enum, &host_charset_name, _("\
 Set the host and target character sets."), _("\
@@ -1067,6 +1046,9 @@ To see a list of the character sets GDB supports, type `set host-charset <TAB>'.
                        show_host_charset_name,
                        &setlist, &showlist);
 
+  /* Recall that the first element is always "auto".  */
+  target_charset_name = charset_enum[0];
+  gdb_assert (strcmp (target_charset_name, "auto") == 0);
   add_setshow_enum_cmd ("target-charset", class_support,
                        charset_enum, &target_charset_name, _("\
 Set the target character set."), _("\
@@ -1079,6 +1061,9 @@ To see a list of the character sets GDB supports, type `set target-charset'<TAB>
                        show_target_charset_name,
                        &setlist, &showlist);
 
+  /* Recall that the first element is always "auto".  */
+  target_wide_charset_name = charset_enum[0];
+  gdb_assert (strcmp (target_wide_charset_name, "auto") == 0);
   add_setshow_enum_cmd ("target-wide-charset", class_support,
                        charset_enum, &target_wide_charset_name,
                        _("\