Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / utils.c
index 3226656e2c32548f83e581195cb543fbae769e25..c59c63565eb1f93ce9c272aa9d893dffad9c8bc2 100644 (file)
@@ -1,6 +1,6 @@
 /* General utility routines for GDB, the GNU debugger.
 
-   Copyright (C) 1986-2020 Free Software Foundation, Inc.
+   Copyright (C) 1986-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -506,14 +506,10 @@ add_internal_problem_command (struct internal_problem *problem)
 
   add_basic_prefix_cmd (problem->name, class_maintenance, set_doc,
                        set_cmd_list,
-                       concat ("maintenance set ", problem->name, " ",
-                               (char *) NULL),
                        0/*allow-unknown*/, &maintenance_set_cmdlist);
 
   add_show_prefix_cmd (problem->name, class_maintenance, show_doc,
                       show_cmd_list,
-                      concat ("maintenance show ", problem->name, " ",
-                              (char *) NULL),
                       0/*allow-unknown*/, &maintenance_show_cmdlist);
 
   if (problem->user_settable_should_quit)
@@ -1426,12 +1422,15 @@ static void
 emit_style_escape (const ui_file_style &style,
                   struct ui_file *stream = nullptr)
 {
-  applied_style = style;
+  if (applied_style != style)
+    {
+      applied_style = style;
 
-  if (stream == nullptr)
-    wrap_buffer.append (style.to_ansi ());
-  else
-    stream->puts (style.to_ansi ().c_str ());
+      if (stream == nullptr)
+       wrap_buffer.append (style.to_ansi ());
+      else
+       stream->puts (style.to_ansi ().c_str ());
+    }
 }
 
 /* Set the current output style.  This will affect future uses of the
@@ -1579,6 +1578,14 @@ gdb_flush (struct ui_file *stream)
   stream->flush ();
 }
 
+/* See utils.h.  */
+
+int
+get_chars_per_line ()
+{
+  return chars_per_line;
+}
+
 /* Indicate that if the next sequence of characters overflows the line,
    a newline should be inserted here rather than when it hits the end.
    If INDENT is non-null, it is a string to be printed to indent the
@@ -1769,6 +1776,12 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
                 don't increment chars_printed here.  */
              lineptr += skip_bytes;
            }
+         else if (*lineptr == '\r')
+           {
+             wrap_buffer.push_back (*lineptr);
+             chars_printed = 0;
+             lineptr++;
+           }
          else
            {
              wrap_buffer.push_back (*lineptr);
@@ -1786,14 +1799,20 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
                 prompt is given; and to avoid emitting style
                 sequences in the middle of a run of text, we track
                 this as well.  */
-             ui_file_style save_style;
+             ui_file_style save_style = applied_style;
              bool did_paginate = false;
 
              chars_printed = 0;
              lines_printed++;
              if (wrap_column)
                {
-                 save_style = wrap_style;
+                 /* We are about to insert a newline at an historic
+                    location in the WRAP_BUFFER.  Before we do we want to
+                    restore the default style.  To know if we actually
+                    need to insert an escape sequence we must restore the
+                    current applied style to how it was at the WRAP_COLUMN
+                    location.  */
+                 applied_style = wrap_style;
                  if (stream->can_emit_style_escape ())
                    emit_style_escape (ui_file_style (), stream);
                  /* If we aren't actually wrapping, don't output
@@ -1808,10 +1827,7 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
                  stream->puts ("\n");
                }
              else
-               {
-                 save_style = applied_style;
-                 flush_wrap_buffer (stream);
-               }
+               flush_wrap_buffer (stream);
 
              /* Possible new page.  Note that
                 PAGINATION_DISABLED_FOR_COMMAND might be set during
@@ -1827,8 +1843,19 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
              if (wrap_column)
                {
                  stream->puts (wrap_indent);
+
+                 /* Having finished inserting the wrapping we should
+                    restore the style as it was at the WRAP_COLUMN.  */
                  if (stream->can_emit_style_escape ())
-                   emit_style_escape (save_style, stream);
+                   emit_style_escape (wrap_style, stream);
+
+                 /* The WRAP_BUFFER will still contain content, and that
+                    content might set some alternative style.  Restore
+                    APPLIED_STYLE as it was before we started wrapping,
+                    this reflects the current style for the last character
+                    in WRAP_BUFFER.  */
+                 applied_style = save_style;
+
                  /* FIXME, this strlen is what prevents wrap_indent from
                     containing tabs.  However, if we recurse to print it
                     and count its chars, we risk trouble if wrap_indent is
@@ -1881,16 +1908,9 @@ void
 fputs_styled (const char *linebuffer, const ui_file_style &style,
              struct ui_file *stream)
 {
-  /* This just makes it so we emit somewhat fewer escape
-     sequences.  */
-  if (style.is_default ())
-    fputs_maybe_filtered (linebuffer, stream, 1);
-  else
-    {
-      set_output_style (stream, style);
-      fputs_maybe_filtered (linebuffer, stream, 1);
-      set_output_style (stream, ui_file_style ());
-    }
+  set_output_style (stream, style);
+  fputs_maybe_filtered (linebuffer, stream, 1);
+  set_output_style (stream, ui_file_style ());
 }
 
 /* See utils.h.  */
@@ -1899,16 +1919,9 @@ void
 fputs_styled_unfiltered (const char *linebuffer, const ui_file_style &style,
                         struct ui_file *stream)
 {
-  /* This just makes it so we emit somewhat fewer escape
-     sequences.  */
-  if (style.is_default ())
-    fputs_maybe_filtered (linebuffer, stream, 0);
-  else
-    {
-      set_output_style (stream, style);
-      fputs_maybe_filtered (linebuffer, stream, 0);
-      set_output_style (stream, ui_file_style ());
-    }
+  set_output_style (stream, style);
+  fputs_maybe_filtered (linebuffer, stream, 0);
+  set_output_style (stream, ui_file_style ());
 }
 
 /* See utils.h.  */
@@ -2112,26 +2125,30 @@ vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
 {
   if (debug_timestamp && stream == gdb_stdlog)
     {
-      using namespace std::chrono;
-      int len, need_nl;
+      static bool needs_timestamp = true;
 
+      /* Print timestamp if previous print ended with a \n.  */
+      if (needs_timestamp)
+       {
+         using namespace std::chrono;
+
+         steady_clock::time_point now = steady_clock::now ();
+         seconds s = duration_cast<seconds> (now.time_since_epoch ());
+         microseconds us = duration_cast<microseconds> (now.time_since_epoch () - s);
+         std::string timestamp = string_printf ("%ld.%06ld ",
+                                                (long) s.count (),
+                                                (long) us.count ());
+         fputs_unfiltered (timestamp.c_str (), stream);
+       }
+
+      /* Print the message.  */
       string_file sfile;
       cli_ui_out (&sfile, 0).vmessage (ui_file_style (), format, args);
       std::string linebuffer = std::move (sfile.string ());
+      fputs_unfiltered (linebuffer.c_str (), stream);
 
-      steady_clock::time_point now = steady_clock::now ();
-      seconds s = duration_cast<seconds> (now.time_since_epoch ());
-      microseconds us = duration_cast<microseconds> (now.time_since_epoch () - s);
-
-      len = linebuffer.size ();
-      need_nl = (len > 0 && linebuffer[len - 1] != '\n');
-
-      std::string timestamp = string_printf ("%ld.%06ld %s%s",
-                                            (long) s.count (),
-                                            (long) us.count (),
-                                            linebuffer.c_str (),
-                                            need_nl ? "\n": "");
-      fputs_unfiltered (timestamp.c_str (), stream);
+      size_t len = linebuffer.length ();
+      needs_timestamp = (len > 0 && linebuffer[len - 1] == '\n');
     }
   else
     vfprintf_maybe_filtered (stream, format, args, false, true);
@@ -2169,22 +2186,6 @@ fprintf_unfiltered (struct ui_file *stream, const char *format, ...)
   va_end (args);
 }
 
-/* Like fprintf_filtered, but prints its result indented.
-   Called as fprintfi_filtered (spaces, stream, format, ...);  */
-
-void
-fprintfi_filtered (int spaces, struct ui_file *stream, const char *format,
-                  ...)
-{
-  va_list args;
-
-  va_start (args, format);
-  print_spaces_filtered (spaces, stream);
-
-  vfprintf_filtered (stream, format, args);
-  va_end (args);
-}
-
 /* See utils.h.  */
 
 void
@@ -2220,11 +2221,9 @@ vfprintf_styled_no_gdbfmt (struct ui_file *stream, const ui_file_style &style,
   std::string str = string_vprintf (format, args);
   if (!str.empty ())
     {
-      if (!style.is_default ())
-       set_output_style (stream, style);
+      set_output_style (stream, style);
       fputs_maybe_filtered (str.c_str (), stream, filter);
-      if (!style.is_default ())
-       set_output_style (stream, ui_file_style ());
+      set_output_style (stream, ui_file_style ());
     }
 }
 
@@ -2249,20 +2248,6 @@ printf_unfiltered (const char *format, ...)
   va_end (args);
 }
 
-/* Like printf_filtered, but prints it's result indented.
-   Called as printfi_filtered (spaces, format, ...);  */
-
-void
-printfi_filtered (int spaces, const char *format, ...)
-{
-  va_list args;
-
-  va_start (args, format);
-  print_spaces_filtered (spaces, gdb_stdout);
-  vfprintf_filtered (gdb_stdout, format, args);
-  va_end (args);
-}
-
 /* Easy -- but watch out!
 
    This routine is *not* a replacement for puts()!  puts() appends a newline.
@@ -2829,14 +2814,6 @@ streq (const char *lhs, const char *rhs)
   return !strcmp (lhs, rhs);
 }
 
-/* See utils.h.  */
-
-int
-streq_hash (const void *lhs, const void *rhs)
-{
-  return streq ((const char *) lhs, (const char *) rhs);
-}
-
 \f
 
 /*
This page took 0.026593 seconds and 4 git commands to generate.