xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/utils.c (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* General utility routines for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2023 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 <ctype.h>
22 #include "gdbsupport/gdb_wait.h"
23 #include "event-top.h"
24 #include "gdbthread.h"
25 #include "fnmatch.h"
26 #include "gdb_bfd.h"
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
29 #endif /* HAVE_SYS_RESOURCE_H */
30 
31 #ifdef TUI
32 #include "tui/tui.h"		/* For tui_get_command_dimension.   */
33 #endif
34 
35 #ifdef __GO32__
36 #include <pc.h>
37 #endif
38 
39 #include <signal.h>
40 #include "gdbcmd.h"
41 #include "serial.h"
42 #include "bfd.h"
43 #include "target.h"
44 #include "gdb-demangle.h"
45 #include "expression.h"
46 #include "language.h"
47 #include "charset.h"
48 #include "annotate.h"
49 #include "filenames.h"
50 #include "symfile.h"
51 #include "gdbsupport/gdb_obstack.h"
52 #include "gdbcore.h"
53 #include "top.h"
54 #include "main.h"
55 #include "solist.h"
56 
57 #include "inferior.h"		/* for signed_pointer_to_address */
58 
59 #include "gdb_curses.h"
60 
61 #include "readline/readline.h"
62 
63 #include <chrono>
64 
65 #include "interps.h"
66 #include "gdbsupport/gdb_regex.h"
67 #include "gdbsupport/job-control.h"
68 #include "gdbsupport/selftest.h"
69 #include "gdbsupport/gdb_optional.h"
70 #include "cp-support.h"
71 #include <algorithm>
72 #include "gdbsupport/pathstuff.h"
73 #include "cli/cli-style.h"
74 #include "gdbsupport/scope-exit.h"
75 #include "gdbarch.h"
76 #include "cli-out.h"
77 #include "gdbsupport/gdb-safe-ctype.h"
78 #include "bt-utils.h"
79 #include "gdbsupport/buildargv.h"
80 #include "pager.h"
81 #include "run-on-main-thread.h"
82 
83 void (*deprecated_error_begin_hook) (void);
84 
85 /* Prototypes for local functions */
86 
87 static void set_screen_size (void);
88 static void set_width (void);
89 
90 /* Time spent in prompt_for_continue in the currently executing command
91    waiting for user to respond.
92    Initialized in make_command_stats_cleanup.
93    Modified in prompt_for_continue and defaulted_query.
94    Used in report_command_stats.  */
95 
96 static std::chrono::steady_clock::duration prompt_for_continue_wait_time;
97 
98 /* A flag indicating whether to timestamp debugging messages.  */
99 
100 bool debug_timestamp = false;
101 
102 /* True means that strings with character values >0x7F should be printed
103    as octal escapes.  False means just print the value (e.g. it's an
104    international character, and the terminal or window can cope.)  */
105 
106 bool sevenbit_strings = false;
107 static void
108 show_sevenbit_strings (struct ui_file *file, int from_tty,
109 		       struct cmd_list_element *c, const char *value)
110 {
111   gdb_printf (file, _("Printing of 8-bit characters "
112 		      "in strings as \\nnn is %s.\n"),
113 	      value);
114 }
115 
116 /* String to be printed before warning messages, if any.  */
117 
118 const char *warning_pre_print = "\nwarning: ";
119 
120 bool pagination_enabled = true;
121 static void
122 show_pagination_enabled (struct ui_file *file, int from_tty,
123 			 struct cmd_list_element *c, const char *value)
124 {
125   gdb_printf (file, _("State of pagination is %s.\n"), value);
126 }
127 
128 
129 
130 
131 /* Print a warning message.  The first argument STRING is the warning
132    message, used as an fprintf format string, the second is the
133    va_list of arguments for that string.  A warning is unfiltered (not
134    paginated) so that the user does not need to page through each
135    screen full of warnings when there are lots of them.  */
136 
137 void
138 vwarning (const char *string, va_list args)
139 {
140   if (deprecated_warning_hook)
141     (*deprecated_warning_hook) (string, args);
142   else
143     {
144       gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
145       if (target_supports_terminal_ours ())
146 	{
147 	  term_state.emplace ();
148 	  target_terminal::ours_for_output ();
149 	}
150       if (warning_pre_print)
151 	gdb_puts (warning_pre_print, gdb_stderr);
152       gdb_vprintf (gdb_stderr, string, args);
153       gdb_printf (gdb_stderr, "\n");
154     }
155 }
156 
157 /* Print an error message and return to command level.
158    The first argument STRING is the error message, used as a fprintf string,
159    and the remaining args are passed as arguments to it.  */
160 
161 void
162 verror (const char *string, va_list args)
163 {
164   throw_verror (GENERIC_ERROR, string, args);
165 }
166 
167 void
168 error_stream (const string_file &stream)
169 {
170   error (("%s"), stream.c_str ());
171 }
172 
173 /* Emit a message and abort.  */
174 
175 static void ATTRIBUTE_NORETURN
176 abort_with_message (const char *msg)
177 {
178   if (current_ui == NULL)
179     fputs (msg, stderr);
180   else
181     gdb_puts (msg, gdb_stderr);
182 
183   abort ();		/* ARI: abort */
184 }
185 
186 /* Dump core trying to increase the core soft limit to hard limit first.  */
187 
188 void
189 dump_core (void)
190 {
191 #ifdef HAVE_SETRLIMIT
192   struct rlimit rlim = { (rlim_t) RLIM_INFINITY, (rlim_t) RLIM_INFINITY };
193 
194   setrlimit (RLIMIT_CORE, &rlim);
195 #endif /* HAVE_SETRLIMIT */
196 
197   /* Ensure that the SIGABRT we're about to raise will immediately cause
198      GDB to exit and dump core, we don't want to trigger GDB's printing of
199      a backtrace to the console here.  */
200   signal (SIGABRT, SIG_DFL);
201 
202   abort ();		/* ARI: abort */
203 }
204 
205 /* Check whether GDB will be able to dump core using the dump_core
206    function.  Returns zero if GDB cannot or should not dump core.
207    If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
208    If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected.  */
209 
210 int
211 can_dump_core (enum resource_limit_kind limit_kind)
212 {
213 #ifdef HAVE_GETRLIMIT
214   struct rlimit rlim;
215 
216   /* Be quiet and assume we can dump if an error is returned.  */
217   if (getrlimit (RLIMIT_CORE, &rlim) != 0)
218     return 1;
219 
220   switch (limit_kind)
221     {
222     case LIMIT_CUR:
223       if (rlim.rlim_cur == 0)
224 	return 0;
225       /* Fall through.  */
226 
227     case LIMIT_MAX:
228       if (rlim.rlim_max == 0)
229 	return 0;
230     }
231 #endif /* HAVE_GETRLIMIT */
232 
233   return 1;
234 }
235 
236 /* Print a warning that we cannot dump core.  */
237 
238 void
239 warn_cant_dump_core (const char *reason)
240 {
241   gdb_printf (gdb_stderr,
242 	      _("%s\nUnable to dump core, use `ulimit -c"
243 		" unlimited' before executing GDB next time.\n"),
244 	      reason);
245 }
246 
247 /* Check whether GDB will be able to dump core using the dump_core
248    function, and print a warning if we cannot.  */
249 
250 static int
251 can_dump_core_warn (enum resource_limit_kind limit_kind,
252 		    const char *reason)
253 {
254   int core_dump_allowed = can_dump_core (limit_kind);
255 
256   if (!core_dump_allowed)
257     warn_cant_dump_core (reason);
258 
259   return core_dump_allowed;
260 }
261 
262 /* Allow the user to configure the debugger behavior with respect to
263    what to do when an internal problem is detected.  */
264 
265 const char internal_problem_ask[] = "ask";
266 const char internal_problem_yes[] = "yes";
267 const char internal_problem_no[] = "no";
268 static const char *const internal_problem_modes[] =
269 {
270   internal_problem_ask,
271   internal_problem_yes,
272   internal_problem_no,
273   NULL
274 };
275 
276 /* Data structure used to control how the internal_vproblem function
277    should behave.  An instance of this structure is created for each
278    problem type that GDB supports.  */
279 
280 struct internal_problem
281 {
282   /* The name of this problem type.  This must not contain white space as
283      this string is used to build command names.  */
284   const char *name;
285 
286   /* When this is true then a user command is created (based on NAME) that
287      allows the SHOULD_QUIT field to be modified, otherwise, SHOULD_QUIT
288      can't be changed from its default value by the user.  */
289   bool user_settable_should_quit;
290 
291   /* Reference a value from internal_problem_modes to indicate if GDB
292      should quit when it hits a problem of this type.  */
293   const char *should_quit;
294 
295   /* Like USER_SETTABLE_SHOULD_QUIT but for SHOULD_DUMP_CORE.  */
296   bool user_settable_should_dump_core;
297 
298   /* Like SHOULD_QUIT, but whether GDB should dump core.  */
299   const char *should_dump_core;
300 
301   /* Like USER_SETTABLE_SHOULD_QUIT but for SHOULD_PRINT_BACKTRACE.  */
302   bool user_settable_should_print_backtrace;
303 
304   /* When this is true GDB will print a backtrace when a problem of this
305      type is encountered.  */
306   bool should_print_backtrace;
307 };
308 
309 /* Return true if the readline callbacks have been initialized for UI.
310    This is always true once GDB is fully initialized, but during the early
311    startup phase this is initially false.  */
312 
313 static bool
314 readline_initialized (struct ui *ui)
315 {
316   return ui->call_readline != nullptr;
317 }
318 
319 /* Report a problem, internal to GDB, to the user.  Once the problem
320    has been reported, and assuming GDB didn't quit, the caller can
321    either allow execution to resume or throw an error.  */
322 
323 static void ATTRIBUTE_PRINTF (4, 0)
324 internal_vproblem (struct internal_problem *problem,
325 		   const char *file, int line, const char *fmt, va_list ap)
326 {
327   static int dejavu;
328   int quit_p;
329   int dump_core_p;
330   std::string reason;
331 
332   /* Don't allow infinite error/warning recursion.  */
333   {
334     static const char msg[] = "Recursive internal problem.\n";
335 
336     switch (dejavu)
337       {
338       case 0:
339 	dejavu = 1;
340 	break;
341       case 1:
342 	dejavu = 2;
343 	abort_with_message (msg);
344       default:
345 	dejavu = 3;
346 	/* Newer GLIBC versions put the warn_unused_result attribute
347 	   on write, but this is one of those rare cases where
348 	   ignoring the return value is correct.  Casting to (void)
349 	   does not fix this problem.  This is the solution suggested
350 	   at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509.  */
351 	if (write (STDERR_FILENO, msg, sizeof (msg)) != sizeof (msg))
352 	  abort (); /* ARI: abort */
353 	exit (1);
354       }
355   }
356 
357   /* Create a string containing the full error/warning message.  Need
358      to call query with this full string, as otherwize the reason
359      (error/warning) and question become separated.  Format using a
360      style similar to a compiler error message.  Include extra detail
361      so that the user knows that they are living on the edge.  */
362   {
363     std::string msg = string_vprintf (fmt, ap);
364     reason = string_printf ("%s:%d: %s: %s\n"
365 			    "A problem internal to GDB has been detected,\n"
366 			    "further debugging may prove unreliable.",
367 			    file, line, problem->name, msg.c_str ());
368   }
369 
370   /* Fall back to abort_with_message if gdb_stderr is not set up.  */
371   if (current_ui == NULL)
372     {
373       fputs (reason.c_str (), stderr);
374       abort_with_message ("\n");
375     }
376 
377   /* Try to get the message out and at the start of a new line.  */
378   gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
379   if (target_supports_terminal_ours ())
380     {
381       term_state.emplace ();
382       target_terminal::ours_for_output ();
383     }
384   if (filtered_printing_initialized ())
385     begin_line ();
386 
387   /* Emit the message unless query will emit it below.  */
388   if (problem->should_quit != internal_problem_ask
389       || !confirm
390       || !filtered_printing_initialized ()
391       || !readline_initialized (current_ui)
392       || problem->should_print_backtrace)
393     gdb_printf (gdb_stderr, "%s\n", reason.c_str ());
394 
395   if (problem->should_print_backtrace)
396     gdb_internal_backtrace ();
397 
398   if (problem->should_quit == internal_problem_ask)
399     {
400       /* Default (yes/batch case) is to quit GDB.  When in batch mode
401 	 this lessens the likelihood of GDB going into an infinite
402 	 loop.  */
403       if (!confirm || !filtered_printing_initialized ()
404 	  || !readline_initialized (current_ui))
405 	quit_p = 1;
406       else
407 	quit_p = query (_("%s\nQuit this debugging session? "),
408 			reason.c_str ());
409     }
410   else if (problem->should_quit == internal_problem_yes)
411     quit_p = 1;
412   else if (problem->should_quit == internal_problem_no)
413     quit_p = 0;
414   else
415     internal_error (_("bad switch"));
416 
417   gdb_puts (_("\nThis is a bug, please report it."), gdb_stderr);
418   if (REPORT_BUGS_TO[0])
419     gdb_printf (gdb_stderr, _("  For instructions, see:\n%ps."),
420 		styled_string (file_name_style.style (),
421 			       REPORT_BUGS_TO));
422   gdb_puts ("\n\n", gdb_stderr);
423 
424   if (problem->should_dump_core == internal_problem_ask)
425     {
426       if (!can_dump_core_warn (LIMIT_MAX, reason.c_str ()))
427 	dump_core_p = 0;
428       else if (!filtered_printing_initialized ()
429 	       || !readline_initialized (current_ui))
430 	dump_core_p = 1;
431       else
432 	{
433 	  /* Default (yes/batch case) is to dump core.  This leaves a GDB
434 	     `dropping' so that it is easier to see that something went
435 	     wrong in GDB.  */
436 	  dump_core_p = query (_("%s\nCreate a core file of GDB? "),
437 			       reason.c_str ());
438 	}
439     }
440   else if (problem->should_dump_core == internal_problem_yes)
441     dump_core_p = can_dump_core_warn (LIMIT_MAX, reason.c_str ());
442   else if (problem->should_dump_core == internal_problem_no)
443     dump_core_p = 0;
444   else
445     internal_error (_("bad switch"));
446 
447   if (quit_p)
448     {
449       if (dump_core_p)
450 	dump_core ();
451       else
452 	exit (1);
453     }
454   else
455     {
456       if (dump_core_p)
457 	{
458 #ifdef HAVE_WORKING_FORK
459 	  if (fork () == 0)
460 	    dump_core ();
461 #endif
462 	}
463     }
464 
465   dejavu = 0;
466 }
467 
468 static struct internal_problem internal_error_problem = {
469   "internal-error", true, internal_problem_ask, true, internal_problem_ask,
470   true, GDB_PRINT_INTERNAL_BACKTRACE_INIT_ON
471 };
472 
473 void
474 internal_verror (const char *file, int line, const char *fmt, va_list ap)
475 {
476   internal_vproblem (&internal_error_problem, file, line, fmt, ap);
477   throw_quit (_("Command aborted."));
478 }
479 
480 static struct internal_problem internal_warning_problem = {
481   "internal-warning", true, internal_problem_ask, true, internal_problem_ask,
482   true, false
483 };
484 
485 void
486 internal_vwarning (const char *file, int line, const char *fmt, va_list ap)
487 {
488   internal_vproblem (&internal_warning_problem, file, line, fmt, ap);
489 }
490 
491 static struct internal_problem demangler_warning_problem = {
492   "demangler-warning", true, internal_problem_ask, false, internal_problem_no,
493   false, false
494 };
495 
496 void
497 demangler_vwarning (const char *file, int line, const char *fmt, va_list ap)
498 {
499   internal_vproblem (&demangler_warning_problem, file, line, fmt, ap);
500 }
501 
502 void
503 demangler_warning (const char *file, int line, const char *string, ...)
504 {
505   va_list ap;
506 
507   va_start (ap, string);
508   demangler_vwarning (file, line, string, ap);
509   va_end (ap);
510 }
511 
512 /* When GDB reports an internal problem (error or warning) it gives
513    the user the opportunity to quit GDB and/or create a core file of
514    the current debug session.  This function registers a few commands
515    that make it possible to specify that GDB should always or never
516    quit or create a core file, without asking.  The commands look
517    like:
518 
519    maint set PROBLEM-NAME quit ask|yes|no
520    maint show PROBLEM-NAME quit
521    maint set PROBLEM-NAME corefile ask|yes|no
522    maint show PROBLEM-NAME corefile
523 
524    Where PROBLEM-NAME is currently "internal-error" or
525    "internal-warning".  */
526 
527 static void
528 add_internal_problem_command (struct internal_problem *problem)
529 {
530   struct cmd_list_element **set_cmd_list;
531   struct cmd_list_element **show_cmd_list;
532 
533   set_cmd_list = XNEW (struct cmd_list_element *);
534   show_cmd_list = XNEW (struct cmd_list_element *);
535   *set_cmd_list = NULL;
536   *show_cmd_list = NULL;
537 
538   /* The add_basic_prefix_cmd and add_show_prefix_cmd functions take
539      ownership of the string passed in, which is why we don't need to free
540      set_doc and show_doc in this function.  */
541   const char *set_doc
542     = xstrprintf (_("Configure what GDB does when %s is detected."),
543 		  problem->name).release ();
544   const char *show_doc
545     = xstrprintf (_("Show what GDB does when %s is detected."),
546 		  problem->name).release ();
547 
548   add_setshow_prefix_cmd (problem->name, class_maintenance,
549 			  set_doc, show_doc, set_cmd_list, show_cmd_list,
550 			  &maintenance_set_cmdlist, &maintenance_show_cmdlist);
551 
552   if (problem->user_settable_should_quit)
553     {
554       std::string set_quit_doc
555 	= string_printf (_("Set whether GDB should quit when an %s is "
556 			   "detected."), problem->name);
557       std::string show_quit_doc
558 	= string_printf (_("Show whether GDB will quit when an %s is "
559 			   "detected."), problem->name);
560       add_setshow_enum_cmd ("quit", class_maintenance,
561 			    internal_problem_modes,
562 			    &problem->should_quit,
563 			    set_quit_doc.c_str (),
564 			    show_quit_doc.c_str (),
565 			    NULL, /* help_doc */
566 			    NULL, /* setfunc */
567 			    NULL, /* showfunc */
568 			    set_cmd_list,
569 			    show_cmd_list);
570     }
571 
572   if (problem->user_settable_should_dump_core)
573     {
574       std::string set_core_doc
575 	= string_printf (_("Set whether GDB should create a core file of "
576 			   "GDB when %s is detected."), problem->name);
577       std::string show_core_doc
578 	= string_printf (_("Show whether GDB will create a core file of "
579 			   "GDB when %s is detected."), problem->name);
580       add_setshow_enum_cmd ("corefile", class_maintenance,
581 			    internal_problem_modes,
582 			    &problem->should_dump_core,
583 			    set_core_doc.c_str (),
584 			    show_core_doc.c_str (),
585 			    NULL, /* help_doc */
586 			    NULL, /* setfunc */
587 			    NULL, /* showfunc */
588 			    set_cmd_list,
589 			    show_cmd_list);
590     }
591 
592   if (problem->user_settable_should_print_backtrace)
593     {
594       std::string set_bt_doc
595 	= string_printf (_("Set whether GDB should print a backtrace of "
596 			   "GDB when %s is detected."), problem->name);
597       std::string show_bt_doc
598 	= string_printf (_("Show whether GDB will print a backtrace of "
599 			   "GDB when %s is detected."), problem->name);
600       add_setshow_boolean_cmd ("backtrace", class_maintenance,
601 			       &problem->should_print_backtrace,
602 			       set_bt_doc.c_str (),
603 			       show_bt_doc.c_str (),
604 			       NULL, /* help_doc */
605 			       gdb_internal_backtrace_set_cmd,
606 			       NULL, /* showfunc */
607 			       set_cmd_list,
608 			       show_cmd_list);
609     }
610 }
611 
612 /* Return a newly allocated string, containing the PREFIX followed
613    by the system error message for errno (separated by a colon).  */
614 
615 static std::string
616 perror_string (const char *prefix)
617 {
618   const char *err = safe_strerror (errno);
619   return std::string (prefix) + ": " + err;
620 }
621 
622 /* Print the system error message for errno, and also mention STRING
623    as the file name for which the error was encountered.  Use ERRCODE
624    for the thrown exception.  Then return to command level.  */
625 
626 static void ATTRIBUTE_NORETURN
627 throw_perror_with_name (enum errors errcode, const char *string)
628 {
629   std::string combined = perror_string (string);
630 
631   /* I understand setting these is a matter of taste.  Still, some people
632      may clear errno but not know about bfd_error.  Doing this here is not
633      unreasonable.  */
634   bfd_set_error (bfd_error_no_error);
635   errno = 0;
636 
637   throw_error (errcode, _("%s."), combined.c_str ());
638 }
639 
640 /* See throw_perror_with_name, ERRCODE defaults here to GENERIC_ERROR.  */
641 
642 void
643 perror_with_name (const char *string)
644 {
645   throw_perror_with_name (GENERIC_ERROR, string);
646 }
647 
648 /* Same as perror_with_name except that it prints a warning instead
649    of throwing an error.  */
650 
651 void
652 perror_warning_with_name (const char *string)
653 {
654   std::string combined = perror_string (string);
655   warning (_("%s"), combined.c_str ());
656 }
657 
658 /* Print the system error message for ERRCODE, and also mention STRING
659    as the file name for which the error was encountered.  */
660 
661 void
662 print_sys_errmsg (const char *string, int errcode)
663 {
664   const char *err = safe_strerror (errcode);
665   gdb_printf (gdb_stderr, "%s: %s.\n", string, err);
666 }
667 
668 /* Control C eventually causes this to be called, at a convenient time.  */
669 
670 void
671 quit (void)
672 {
673   if (sync_quit_force_run)
674     {
675       sync_quit_force_run = 0;
676       quit_force (NULL, 0);
677     }
678 
679 #ifdef __MSDOS__
680   /* No steenking SIGINT will ever be coming our way when the
681      program is resumed.  Don't lie.  */
682   throw_quit ("Quit");
683 #else
684   if (job_control
685       /* If there is no terminal switching for this target, then we can't
686 	 possibly get screwed by the lack of job control.  */
687       || !target_supports_terminal_ours ())
688     throw_quit ("Quit");
689   else
690     throw_quit ("Quit (expect signal SIGINT when the program is resumed)");
691 #endif
692 }
693 
694 /* See defs.h.  */
695 
696 void
697 maybe_quit (void)
698 {
699   if (!is_main_thread ())
700     return;
701 
702   if (sync_quit_force_run)
703     quit ();
704 
705   quit_handler ();
706 }
707 
708 
709 /* Called when a memory allocation fails, with the number of bytes of
710    memory requested in SIZE.  */
711 
712 void
713 malloc_failure (long size)
714 {
715   if (size > 0)
716     {
717       internal_error (_("virtual memory exhausted: can't allocate %ld bytes."),
718 		      size);
719     }
720   else
721     {
722       internal_error (_("virtual memory exhausted."));
723     }
724 }
725 
726 /* See common/errors.h.  */
727 
728 void
729 flush_streams ()
730 {
731   gdb_stdout->flush ();
732   gdb_stderr->flush ();
733 }
734 
735 /* My replacement for the read system call.
736    Used like `read' but keeps going if `read' returns too soon.  */
737 
738 int
739 myread (int desc, char *addr, int len)
740 {
741   int val;
742   int orglen = len;
743 
744   while (len > 0)
745     {
746       val = read (desc, addr, len);
747       if (val < 0)
748 	return val;
749       if (val == 0)
750 	return orglen - len;
751       len -= val;
752       addr += val;
753     }
754   return orglen;
755 }
756 
757 /* See utils.h.  */
758 
759 ULONGEST
760 uinteger_pow (ULONGEST v1, LONGEST v2)
761 {
762   if (v2 < 0)
763     {
764       if (v1 == 0)
765 	error (_("Attempt to raise 0 to negative power."));
766       else
767 	return 0;
768     }
769   else
770     {
771       /* The Russian Peasant's Algorithm.  */
772       ULONGEST v;
773 
774       v = 1;
775       for (;;)
776 	{
777 	  if (v2 & 1L)
778 	    v *= v1;
779 	  v2 >>= 1;
780 	  if (v2 == 0)
781 	    return v;
782 	  v1 *= v1;
783 	}
784     }
785 }
786 
787 
788 
789 /* An RAII class that sets up to handle input and then tears down
790    during destruction.  */
791 
792 class scoped_input_handler
793 {
794 public:
795 
796   scoped_input_handler ()
797     : m_quit_handler (&quit_handler, default_quit_handler),
798       m_ui (NULL)
799   {
800     target_terminal::ours ();
801     current_ui->register_file_handler ();
802     if (current_ui->prompt_state == PROMPT_BLOCKED)
803       m_ui = current_ui;
804   }
805 
806   ~scoped_input_handler ()
807   {
808     if (m_ui != NULL)
809       m_ui->unregister_file_handler ();
810   }
811 
812   DISABLE_COPY_AND_ASSIGN (scoped_input_handler);
813 
814 private:
815 
816   /* Save and restore the terminal state.  */
817   target_terminal::scoped_restore_terminal_state m_term_state;
818 
819   /* Save and restore the quit handler.  */
820   scoped_restore_tmpl<quit_handler_ftype *> m_quit_handler;
821 
822   /* The saved UI, if non-NULL.  */
823   struct ui *m_ui;
824 };
825 
826 
827 
828 /* This function supports the query, nquery, and yquery functions.
829    Ask user a y-or-n question and return 0 if answer is no, 1 if
830    answer is yes, or default the answer to the specified default
831    (for yquery or nquery).  DEFCHAR may be 'y' or 'n' to provide a
832    default answer, or '\0' for no default.
833    CTLSTR is the control string and should end in "? ".  It should
834    not say how to answer, because we do that.
835    ARGS are the arguments passed along with the CTLSTR argument to
836    printf.  */
837 
838 static int ATTRIBUTE_PRINTF (1, 0)
839 defaulted_query (const char *ctlstr, const char defchar, va_list args)
840 {
841   int retval;
842   int def_value;
843   char def_answer, not_def_answer;
844   const char *y_string, *n_string;
845 
846   /* Set up according to which answer is the default.  */
847   if (defchar == '\0')
848     {
849       def_value = 1;
850       def_answer = 'Y';
851       not_def_answer = 'N';
852       y_string = "y";
853       n_string = "n";
854     }
855   else if (defchar == 'y')
856     {
857       def_value = 1;
858       def_answer = 'Y';
859       not_def_answer = 'N';
860       y_string = "[y]";
861       n_string = "n";
862     }
863   else
864     {
865       def_value = 0;
866       def_answer = 'N';
867       not_def_answer = 'Y';
868       y_string = "y";
869       n_string = "[n]";
870     }
871 
872   /* Automatically answer the default value if the user did not want
873      prompts or the command was issued with the server prefix.  */
874   if (!confirm || server_command)
875     return def_value;
876 
877   /* If input isn't coming from the user directly, just say what
878      question we're asking, and then answer the default automatically.  This
879      way, important error messages don't get lost when talking to GDB
880      over a pipe.  */
881   if (current_ui->instream != current_ui->stdin_stream
882       || !current_ui->input_interactive_p ()
883       /* Restrict queries to the main UI.  */
884       || current_ui != main_ui)
885     {
886       target_terminal::scoped_restore_terminal_state term_state;
887       target_terminal::ours_for_output ();
888       gdb_stdout->wrap_here (0);
889       gdb_vprintf (gdb_stdout, ctlstr, args);
890 
891       gdb_printf (_("(%s or %s) [answered %c; "
892 		    "input not from terminal]\n"),
893 		  y_string, n_string, def_answer);
894 
895       return def_value;
896     }
897 
898   if (deprecated_query_hook)
899     {
900       target_terminal::scoped_restore_terminal_state term_state;
901       return deprecated_query_hook (ctlstr, args);
902     }
903 
904   /* Format the question outside of the loop, to avoid reusing args.  */
905   std::string question = string_vprintf (ctlstr, args);
906   std::string prompt
907     = string_printf (_("%s%s(%s or %s) %s"),
908 		     annotation_level > 1 ? "\n\032\032pre-query\n" : "",
909 		     question.c_str (), y_string, n_string,
910 		     annotation_level > 1 ? "\n\032\032query\n" : "");
911 
912   /* Used to add duration we waited for user to respond to
913      prompt_for_continue_wait_time.  */
914   using namespace std::chrono;
915   steady_clock::time_point prompt_started = steady_clock::now ();
916 
917   scoped_input_handler prepare_input;
918 
919   while (1)
920     {
921       char *response, answer;
922 
923       gdb_flush (gdb_stdout);
924       response = gdb_readline_wrapper (prompt.c_str ());
925 
926       if (response == NULL)	/* C-d  */
927 	{
928 	  gdb_printf ("EOF [assumed %c]\n", def_answer);
929 	  retval = def_value;
930 	  break;
931 	}
932 
933       answer = response[0];
934       xfree (response);
935 
936       if (answer >= 'a')
937 	answer -= 040;
938       /* Check answer.  For the non-default, the user must specify
939 	 the non-default explicitly.  */
940       if (answer == not_def_answer)
941 	{
942 	  retval = !def_value;
943 	  break;
944 	}
945       /* Otherwise, if a default was specified, the user may either
946 	 specify the required input or have it default by entering
947 	 nothing.  */
948       if (answer == def_answer
949 	  || (defchar != '\0' && answer == '\0'))
950 	{
951 	  retval = def_value;
952 	  break;
953 	}
954       /* Invalid entries are not defaulted and require another selection.  */
955       gdb_printf (_("Please answer %s or %s.\n"),
956 		  y_string, n_string);
957     }
958 
959   /* Add time spend in this routine to prompt_for_continue_wait_time.  */
960   prompt_for_continue_wait_time += steady_clock::now () - prompt_started;
961 
962   if (annotation_level > 1)
963     gdb_printf (("\n\032\032post-query\n"));
964   return retval;
965 }
966 
967 
968 /* Ask user a y-or-n question and return 0 if answer is no, 1 if
969    answer is yes, or 0 if answer is defaulted.
970    Takes three args which are given to printf to print the question.
971    The first, a control string, should end in "? ".
972    It should not say how to answer, because we do that.  */
973 
974 int
975 nquery (const char *ctlstr, ...)
976 {
977   va_list args;
978   int ret;
979 
980   va_start (args, ctlstr);
981   ret = defaulted_query (ctlstr, 'n', args);
982   va_end (args);
983   return ret;
984 }
985 
986 /* Ask user a y-or-n question and return 0 if answer is no, 1 if
987    answer is yes, or 1 if answer is defaulted.
988    Takes three args which are given to printf to print the question.
989    The first, a control string, should end in "? ".
990    It should not say how to answer, because we do that.  */
991 
992 int
993 yquery (const char *ctlstr, ...)
994 {
995   va_list args;
996   int ret;
997 
998   va_start (args, ctlstr);
999   ret = defaulted_query (ctlstr, 'y', args);
1000   va_end (args);
1001   return ret;
1002 }
1003 
1004 /* Ask user a y-or-n question and return 1 iff answer is yes.
1005    Takes three args which are given to printf to print the question.
1006    The first, a control string, should end in "? ".
1007    It should not say how to answer, because we do that.  */
1008 
1009 int
1010 query (const char *ctlstr, ...)
1011 {
1012   va_list args;
1013   int ret;
1014 
1015   va_start (args, ctlstr);
1016   ret = defaulted_query (ctlstr, '\0', args);
1017   va_end (args);
1018   return ret;
1019 }
1020 
1021 /* A helper for parse_escape that converts a host character to a
1022    target character.  C is the host character.  If conversion is
1023    possible, then the target character is stored in *TARGET_C and the
1024    function returns 1.  Otherwise, the function returns 0.  */
1025 
1026 static int
1027 host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c)
1028 {
1029   char the_char = c;
1030   int result = 0;
1031 
1032   auto_obstack host_data;
1033 
1034   convert_between_encodings (target_charset (gdbarch), host_charset (),
1035 			     (gdb_byte *) &the_char, 1, 1,
1036 			     &host_data, translit_none);
1037 
1038   if (obstack_object_size (&host_data) == 1)
1039     {
1040       result = 1;
1041       *target_c = *(char *) obstack_base (&host_data);
1042     }
1043 
1044   return result;
1045 }
1046 
1047 /* Parse a C escape sequence.  STRING_PTR points to a variable
1048    containing a pointer to the string to parse.  That pointer
1049    should point to the character after the \.  That pointer
1050    is updated past the characters we use.  The value of the
1051    escape sequence is returned.
1052 
1053    A negative value means the sequence \ newline was seen,
1054    which is supposed to be equivalent to nothing at all.
1055 
1056    If \ is followed by a null character, we return a negative
1057    value and leave the string pointer pointing at the null character.
1058 
1059    If \ is followed by 000, we return 0 and leave the string pointer
1060    after the zeros.  A value of 0 does not mean end of string.  */
1061 
1062 int
1063 parse_escape (struct gdbarch *gdbarch, const char **string_ptr)
1064 {
1065   int target_char = -2;	/* Initialize to avoid GCC warnings.  */
1066   int c = *(*string_ptr)++;
1067 
1068   switch (c)
1069     {
1070       case '\n':
1071 	return -2;
1072       case 0:
1073 	(*string_ptr)--;
1074 	return 0;
1075 
1076       case '0':
1077       case '1':
1078       case '2':
1079       case '3':
1080       case '4':
1081       case '5':
1082       case '6':
1083       case '7':
1084 	{
1085 	  int i = fromhex (c);
1086 	  int count = 0;
1087 	  while (++count < 3)
1088 	    {
1089 	      c = (**string_ptr);
1090 	      if (ISDIGIT (c) && c != '8' && c != '9')
1091 		{
1092 		  (*string_ptr)++;
1093 		  i *= 8;
1094 		  i += fromhex (c);
1095 		}
1096 	      else
1097 		{
1098 		  break;
1099 		}
1100 	    }
1101 	  return i;
1102 	}
1103 
1104     case 'a':
1105       c = '\a';
1106       break;
1107     case 'b':
1108       c = '\b';
1109       break;
1110     case 'f':
1111       c = '\f';
1112       break;
1113     case 'n':
1114       c = '\n';
1115       break;
1116     case 'r':
1117       c = '\r';
1118       break;
1119     case 't':
1120       c = '\t';
1121       break;
1122     case 'v':
1123       c = '\v';
1124       break;
1125 
1126     default:
1127       break;
1128     }
1129 
1130   if (!host_char_to_target (gdbarch, c, &target_char))
1131     error (_("The escape sequence `\\%c' is equivalent to plain `%c',"
1132 	     " which has no equivalent\nin the `%s' character set."),
1133 	   c, c, target_charset (gdbarch));
1134   return target_char;
1135 }
1136 
1137 
1138 /* Number of lines per page or UINT_MAX if paging is disabled.  */
1139 static unsigned int lines_per_page;
1140 static void
1141 show_lines_per_page (struct ui_file *file, int from_tty,
1142 		     struct cmd_list_element *c, const char *value)
1143 {
1144   gdb_printf (file,
1145 	      _("Number of lines gdb thinks are in a page is %s.\n"),
1146 	      value);
1147 }
1148 
1149 /* Number of chars per line or UINT_MAX if line folding is disabled.  */
1150 static unsigned int chars_per_line;
1151 static void
1152 show_chars_per_line (struct ui_file *file, int from_tty,
1153 		     struct cmd_list_element *c, const char *value)
1154 {
1155   gdb_printf (file,
1156 	      _("Number of characters gdb thinks "
1157 		"are in a line is %s.\n"),
1158 	      value);
1159 }
1160 
1161 /* Current count of lines printed on this page, chars on this line.  */
1162 static unsigned int lines_printed, chars_printed;
1163 
1164 /* True if pagination is disabled for just one command.  */
1165 
1166 static bool pagination_disabled_for_command;
1167 
1168 /* Buffer and start column of buffered text, for doing smarter word-
1169    wrapping.  When someone calls wrap_here(), we start buffering output
1170    that comes through gdb_puts().  If we see a newline, we just
1171    spit it out and forget about the wrap_here().  If we see another
1172    wrap_here(), we spit it out and remember the newer one.  If we see
1173    the end of the line, we spit out a newline, the indent, and then
1174    the buffered output.  */
1175 
1176 static bool filter_initialized = false;
1177 
1178 
1179 
1180 /* Initialize the number of lines per page and chars per line.  */
1181 
1182 void
1183 init_page_info (void)
1184 {
1185   if (batch_flag)
1186     {
1187       lines_per_page = UINT_MAX;
1188       chars_per_line = UINT_MAX;
1189     }
1190   else
1191 #if defined(TUI)
1192   if (!tui_get_command_dimension (&chars_per_line, &lines_per_page))
1193 #endif
1194     {
1195       int rows, cols;
1196 
1197 #if defined(__GO32__)
1198       rows = ScreenRows ();
1199       cols = ScreenCols ();
1200       lines_per_page = rows;
1201       chars_per_line = cols;
1202 #else
1203       /* Make sure Readline has initialized its terminal settings.  */
1204       rl_reset_terminal (NULL);
1205 
1206       /* Get the screen size from Readline.  */
1207       rl_get_screen_size (&rows, &cols);
1208       lines_per_page = rows;
1209       chars_per_line = cols;
1210 
1211       /* Readline should have fetched the termcap entry for us.
1212 	 Only try to use tgetnum function if rl_get_screen_size
1213 	 did not return a useful value. */
1214       if (((rows <= 0) && (tgetnum ((char *) "li") < 0))
1215 	/* Also disable paging if inside Emacs.  $EMACS was used
1216 	   before Emacs v25.1, $INSIDE_EMACS is used since then.  */
1217 	  || getenv ("EMACS") || getenv ("INSIDE_EMACS"))
1218 	{
1219 	  /* The number of lines per page is not mentioned in the terminal
1220 	     description or EMACS environment variable is set.  This probably
1221 	     means that paging is not useful, so disable paging.  */
1222 	  lines_per_page = UINT_MAX;
1223 	}
1224 
1225       /* If the output is not a terminal, don't paginate it.  */
1226       if (!gdb_stdout->isatty ())
1227 	lines_per_page = UINT_MAX;
1228 #endif
1229     }
1230 
1231   /* We handle SIGWINCH ourselves.  */
1232   rl_catch_sigwinch = 0;
1233 
1234   set_screen_size ();
1235   set_width ();
1236 }
1237 
1238 /* Return nonzero if filtered printing is initialized.  */
1239 int
1240 filtered_printing_initialized (void)
1241 {
1242   return filter_initialized;
1243 }
1244 
1245 set_batch_flag_and_restore_page_info::set_batch_flag_and_restore_page_info ()
1246   : m_save_lines_per_page (lines_per_page),
1247     m_save_chars_per_line (chars_per_line),
1248     m_save_batch_flag (batch_flag)
1249 {
1250   batch_flag = 1;
1251   init_page_info ();
1252 }
1253 
1254 set_batch_flag_and_restore_page_info::~set_batch_flag_and_restore_page_info ()
1255 {
1256   batch_flag = m_save_batch_flag;
1257   chars_per_line = m_save_chars_per_line;
1258   lines_per_page = m_save_lines_per_page;
1259 
1260   set_screen_size ();
1261   set_width ();
1262 }
1263 
1264 /* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE.  */
1265 
1266 static void
1267 set_screen_size (void)
1268 {
1269   int rows = lines_per_page;
1270   int cols = chars_per_line;
1271 
1272   /* If we get 0 or negative ROWS or COLS, treat as "infinite" size.
1273      A negative number can be seen here with the "set width/height"
1274      commands and either:
1275 
1276      - the user specified "unlimited", which maps to UINT_MAX, or
1277      - the user specified some number between INT_MAX and UINT_MAX.
1278 
1279      Cap "infinity" to approximately sqrt(INT_MAX) so that we don't
1280      overflow in rl_set_screen_size, which multiplies rows and columns
1281      to compute the number of characters on the screen.  */
1282 
1283   const int sqrt_int_max = INT_MAX >> (sizeof (int) * 8 / 2);
1284 
1285   if (rows <= 0 || rows > sqrt_int_max)
1286     {
1287       rows = sqrt_int_max;
1288       lines_per_page = UINT_MAX;
1289     }
1290 
1291   if (cols <= 0 || cols > sqrt_int_max)
1292     {
1293       cols = sqrt_int_max;
1294       chars_per_line = UINT_MAX;
1295     }
1296 
1297   /* Update Readline's idea of the terminal size.  */
1298   rl_set_screen_size (rows, cols);
1299 }
1300 
1301 /* Reinitialize WRAP_BUFFER.  */
1302 
1303 static void
1304 set_width (void)
1305 {
1306   if (chars_per_line == 0)
1307     init_page_info ();
1308 
1309   filter_initialized = true;
1310 }
1311 
1312 static void
1313 set_width_command (const char *args, int from_tty, struct cmd_list_element *c)
1314 {
1315   set_screen_size ();
1316   set_width ();
1317 }
1318 
1319 static void
1320 set_height_command (const char *args, int from_tty, struct cmd_list_element *c)
1321 {
1322   set_screen_size ();
1323 }
1324 
1325 /* See utils.h.  */
1326 
1327 void
1328 set_screen_width_and_height (int width, int height)
1329 {
1330   lines_per_page = height;
1331   chars_per_line = width;
1332 
1333   set_screen_size ();
1334   set_width ();
1335 }
1336 
1337 void
1338 pager_file::emit_style_escape (const ui_file_style &style)
1339 {
1340   if (can_emit_style_escape () && style != m_applied_style)
1341     {
1342       m_applied_style = style;
1343       if (m_paging)
1344 	m_stream->emit_style_escape (style);
1345       else
1346 	m_wrap_buffer.append (style.to_ansi ());
1347     }
1348 }
1349 
1350 /* See pager.h.  */
1351 
1352 void
1353 pager_file::reset_style ()
1354 {
1355   if (can_emit_style_escape ())
1356     {
1357       m_applied_style = ui_file_style ();
1358       m_wrap_buffer.append (m_applied_style.to_ansi ());
1359     }
1360 }
1361 
1362 /* Wait, so the user can read what's on the screen.  Prompt the user
1363    to continue by pressing RETURN.  'q' is also provided because
1364    telling users what to do in the prompt is more user-friendly than
1365    expecting them to think of Ctrl-C/SIGINT.  */
1366 
1367 void
1368 pager_file::prompt_for_continue ()
1369 {
1370   char cont_prompt[120];
1371   /* Used to add duration we waited for user to respond to
1372      prompt_for_continue_wait_time.  */
1373   using namespace std::chrono;
1374   steady_clock::time_point prompt_started = steady_clock::now ();
1375   bool disable_pagination = pagination_disabled_for_command;
1376 
1377   scoped_restore save_paging = make_scoped_restore (&m_paging, true);
1378 
1379   /* Clear the current styling.  */
1380   m_stream->emit_style_escape (ui_file_style ());
1381 
1382   if (annotation_level > 1)
1383     m_stream->puts (("\n\032\032pre-prompt-for-continue\n"));
1384 
1385   strcpy (cont_prompt,
1386 	  "--Type <RET> for more, q to quit, "
1387 	  "c to continue without paging--");
1388   if (annotation_level > 1)
1389     strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1390 
1391   /* We must do this *before* we call gdb_readline_wrapper, else it
1392      will eventually call us -- thinking that we're trying to print
1393      beyond the end of the screen.  */
1394   reinitialize_more_filter ();
1395 
1396   scoped_input_handler prepare_input;
1397 
1398   /* Call gdb_readline_wrapper, not readline, in order to keep an
1399      event loop running.  */
1400   gdb::unique_xmalloc_ptr<char> ignore (gdb_readline_wrapper (cont_prompt));
1401 
1402   /* Add time spend in this routine to prompt_for_continue_wait_time.  */
1403   prompt_for_continue_wait_time += steady_clock::now () - prompt_started;
1404 
1405   if (annotation_level > 1)
1406     m_stream->puts (("\n\032\032post-prompt-for-continue\n"));
1407 
1408   if (ignore != NULL)
1409     {
1410       char *p = ignore.get ();
1411 
1412       while (*p == ' ' || *p == '\t')
1413 	++p;
1414       if (p[0] == 'q')
1415 	/* Do not call quit here; there is no possibility of SIGINT.  */
1416 	throw_quit ("Quit");
1417       if (p[0] == 'c')
1418 	disable_pagination = true;
1419     }
1420 
1421   /* Now we have to do this again, so that GDB will know that it doesn't
1422      need to save the ---Type <return>--- line at the top of the screen.  */
1423   reinitialize_more_filter ();
1424   pagination_disabled_for_command = disable_pagination;
1425 
1426   dont_repeat ();		/* Forget prev cmd -- CR won't repeat it.  */
1427 }
1428 
1429 /* Initialize timer to keep track of how long we waited for the user.  */
1430 
1431 void
1432 reset_prompt_for_continue_wait_time (void)
1433 {
1434   using namespace std::chrono;
1435 
1436   prompt_for_continue_wait_time = steady_clock::duration::zero ();
1437 }
1438 
1439 /* Fetch the cumulative time spent in prompt_for_continue.  */
1440 
1441 std::chrono::steady_clock::duration
1442 get_prompt_for_continue_wait_time ()
1443 {
1444   return prompt_for_continue_wait_time;
1445 }
1446 
1447 /* Reinitialize filter; ie. tell it to reset to original values.  */
1448 
1449 void
1450 reinitialize_more_filter (void)
1451 {
1452   lines_printed = 0;
1453   chars_printed = 0;
1454   pagination_disabled_for_command = false;
1455 }
1456 
1457 void
1458 pager_file::flush_wrap_buffer ()
1459 {
1460   if (!m_paging && !m_wrap_buffer.empty ())
1461     {
1462       m_stream->puts (m_wrap_buffer.c_str ());
1463       m_wrap_buffer.clear ();
1464     }
1465 }
1466 
1467 void
1468 pager_file::flush ()
1469 {
1470   flush_wrap_buffer ();
1471   m_stream->flush ();
1472 }
1473 
1474 /* See utils.h.  */
1475 
1476 void
1477 gdb_flush (struct ui_file *stream)
1478 {
1479   stream->flush ();
1480 }
1481 
1482 /* See utils.h.  */
1483 
1484 int
1485 get_chars_per_line ()
1486 {
1487   return chars_per_line;
1488 }
1489 
1490 /* See ui-file.h.  */
1491 
1492 void
1493 pager_file::wrap_here (int indent)
1494 {
1495   /* This should have been allocated, but be paranoid anyway.  */
1496   gdb_assert (filter_initialized);
1497 
1498   flush_wrap_buffer ();
1499   if (chars_per_line == UINT_MAX)	/* No line overflow checking.  */
1500     {
1501       m_wrap_column = 0;
1502     }
1503   else if (chars_printed >= chars_per_line)
1504     {
1505       this->puts ("\n");
1506       if (indent != 0)
1507 	this->puts (n_spaces (indent));
1508       m_wrap_column = 0;
1509     }
1510   else
1511     {
1512       m_wrap_column = chars_printed;
1513       m_wrap_indent = indent;
1514       m_wrap_style = m_applied_style;
1515     }
1516 }
1517 
1518 /* Print input string to gdb_stdout arranging strings in columns of n
1519    chars.  String can be right or left justified in the column.  Never
1520    prints trailing spaces.  String should never be longer than width.
1521    FIXME: this could be useful for the EXAMINE command, which
1522    currently doesn't tabulate very well.  */
1523 
1524 void
1525 puts_tabular (char *string, int width, int right)
1526 {
1527   int spaces = 0;
1528   int stringlen;
1529   char *spacebuf;
1530 
1531   gdb_assert (chars_per_line > 0);
1532   if (chars_per_line == UINT_MAX)
1533     {
1534       gdb_puts (string);
1535       gdb_puts ("\n");
1536       return;
1537     }
1538 
1539   if (((chars_printed - 1) / width + 2) * width >= chars_per_line)
1540     gdb_puts ("\n");
1541 
1542   if (width >= chars_per_line)
1543     width = chars_per_line - 1;
1544 
1545   stringlen = strlen (string);
1546 
1547   if (chars_printed > 0)
1548     spaces = width - (chars_printed - 1) % width - 1;
1549   if (right)
1550     spaces += width - stringlen;
1551 
1552   spacebuf = (char *) alloca (spaces + 1);
1553   spacebuf[spaces] = '\0';
1554   while (spaces--)
1555     spacebuf[spaces] = ' ';
1556 
1557   gdb_puts (spacebuf);
1558   gdb_puts (string);
1559 }
1560 
1561 
1562 /* Ensure that whatever gets printed next, using the filtered output
1563    commands, starts at the beginning of the line.  I.e. if there is
1564    any pending output for the current line, flush it and start a new
1565    line.  Otherwise do nothing.  */
1566 
1567 void
1568 begin_line (void)
1569 {
1570   if (chars_printed > 0)
1571     {
1572       gdb_puts ("\n");
1573     }
1574 }
1575 
1576 void
1577 pager_file::puts (const char *linebuffer)
1578 {
1579   const char *lineptr;
1580 
1581   if (linebuffer == 0)
1582     return;
1583 
1584   /* Don't do any filtering or wrapping if both are disabled.  */
1585   if (batch_flag
1586       || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)
1587       || top_level_interpreter () == NULL
1588       || top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
1589     {
1590       flush_wrap_buffer ();
1591       m_stream->puts (linebuffer);
1592       return;
1593     }
1594 
1595   auto buffer_clearer
1596     = make_scope_exit ([&] ()
1597 		       {
1598 			 m_wrap_buffer.clear ();
1599 			 m_wrap_column = 0;
1600 			 m_wrap_indent = 0;
1601 		       });
1602 
1603   /* If the user does "set height 1" then the pager will exhibit weird
1604      behavior.  This is pathological, though, so don't allow it.  */
1605   const unsigned int lines_allowed = (lines_per_page > 1
1606 				      ? lines_per_page - 1
1607 				      : 1);
1608 
1609   /* Go through and output each character.  Show line extension
1610      when this is necessary; prompt user for new page when this is
1611      necessary.  */
1612 
1613   lineptr = linebuffer;
1614   while (*lineptr)
1615     {
1616       /* Possible new page.  Note that PAGINATION_DISABLED_FOR_COMMAND
1617 	 might be set during this loop, so we must continue to check
1618 	 it here.  */
1619       if (pagination_enabled
1620 	  && !pagination_disabled_for_command
1621 	  && lines_printed >= lines_allowed)
1622 	prompt_for_continue ();
1623 
1624       while (*lineptr && *lineptr != '\n')
1625 	{
1626 	  int skip_bytes;
1627 
1628 	  /* Print a single line.  */
1629 	  if (*lineptr == '\t')
1630 	    {
1631 	      m_wrap_buffer.push_back ('\t');
1632 	      /* Shifting right by 3 produces the number of tab stops
1633 		 we have already passed, and then adding one and
1634 		 shifting left 3 advances to the next tab stop.  */
1635 	      chars_printed = ((chars_printed >> 3) + 1) << 3;
1636 	      lineptr++;
1637 	    }
1638 	  else if (*lineptr == '\033'
1639 		   && skip_ansi_escape (lineptr, &skip_bytes))
1640 	    {
1641 	      m_wrap_buffer.append (lineptr, skip_bytes);
1642 	      /* Note that we don't consider this a character, so we
1643 		 don't increment chars_printed here.  */
1644 	      lineptr += skip_bytes;
1645 	    }
1646 	  else if (*lineptr == '\r')
1647 	    {
1648 	      m_wrap_buffer.push_back (*lineptr);
1649 	      chars_printed = 0;
1650 	      lineptr++;
1651 	    }
1652 	  else
1653 	    {
1654 	      m_wrap_buffer.push_back (*lineptr);
1655 	      chars_printed++;
1656 	      lineptr++;
1657 	    }
1658 
1659 	  if (chars_printed >= chars_per_line)
1660 	    {
1661 	      unsigned int save_chars = chars_printed;
1662 
1663 	      /* If we change the style, below, we'll want to reset it
1664 		 before continuing to print.  If there is no wrap
1665 		 column, then we'll only reset the style if the pager
1666 		 prompt is given; and to avoid emitting style
1667 		 sequences in the middle of a run of text, we track
1668 		 this as well.  */
1669 	      ui_file_style save_style = m_applied_style;
1670 	      bool did_paginate = false;
1671 
1672 	      chars_printed = 0;
1673 	      lines_printed++;
1674 	      if (m_wrap_column)
1675 		{
1676 		  /* We are about to insert a newline at an historic
1677 		     location in the WRAP_BUFFER.  Before we do we want to
1678 		     restore the default style.  To know if we actually
1679 		     need to insert an escape sequence we must restore the
1680 		     current applied style to how it was at the WRAP_COLUMN
1681 		     location.  */
1682 		  m_applied_style = m_wrap_style;
1683 		  m_stream->emit_style_escape (ui_file_style ());
1684 		  /* If we aren't actually wrapping, don't output
1685 		     newline -- if chars_per_line is right, we
1686 		     probably just overflowed anyway; if it's wrong,
1687 		     let us keep going.  */
1688 		  m_stream->puts ("\n");
1689 		}
1690 	      else
1691 		this->flush_wrap_buffer ();
1692 
1693 	      /* Possible new page.  Note that
1694 		 PAGINATION_DISABLED_FOR_COMMAND might be set during
1695 		 this loop, so we must continue to check it here.  */
1696 	      if (pagination_enabled
1697 		  && !pagination_disabled_for_command
1698 		  && lines_printed >= lines_allowed)
1699 		{
1700 		  prompt_for_continue ();
1701 		  did_paginate = true;
1702 		}
1703 
1704 	      /* Now output indentation and wrapped string.  */
1705 	      if (m_wrap_column)
1706 		{
1707 		  m_stream->puts (n_spaces (m_wrap_indent));
1708 
1709 		  /* Having finished inserting the wrapping we should
1710 		     restore the style as it was at the WRAP_COLUMN.  */
1711 		  m_stream->emit_style_escape (m_wrap_style);
1712 
1713 		  /* The WRAP_BUFFER will still contain content, and that
1714 		     content might set some alternative style.  Restore
1715 		     APPLIED_STYLE as it was before we started wrapping,
1716 		     this reflects the current style for the last character
1717 		     in WRAP_BUFFER.  */
1718 		  m_applied_style = save_style;
1719 
1720 		  /* Note that this can set chars_printed > chars_per_line
1721 		     if we are printing a long string.  */
1722 		  chars_printed = m_wrap_indent + (save_chars - m_wrap_column);
1723 		  m_wrap_column = 0;	/* And disable fancy wrap */
1724 		}
1725 	      else if (did_paginate)
1726 		m_stream->emit_style_escape (save_style);
1727 	    }
1728 	}
1729 
1730       if (*lineptr == '\n')
1731 	{
1732 	  chars_printed = 0;
1733 	  wrap_here (0); /* Spit out chars, cancel further wraps.  */
1734 	  lines_printed++;
1735 	  m_stream->puts ("\n");
1736 	  lineptr++;
1737 	}
1738     }
1739 
1740   buffer_clearer.release ();
1741 }
1742 
1743 void
1744 pager_file::write (const char *buf, long length_buf)
1745 {
1746   /* We have to make a string here because the pager uses
1747      skip_ansi_escape, which requires NUL-termination.  */
1748   std::string str (buf, length_buf);
1749   this->puts (str.c_str ());
1750 }
1751 
1752 #if GDB_SELF_TEST
1753 
1754 /* Test that disabling the pager does not also disable word
1755    wrapping.  */
1756 
1757 static void
1758 test_pager ()
1759 {
1760   string_file *strfile = new string_file ();
1761   pager_file pager (strfile);
1762 
1763   /* Make sure the pager is disabled.  */
1764   scoped_restore save_enabled
1765     = make_scoped_restore (&pagination_enabled, false);
1766   scoped_restore save_disabled
1767     = make_scoped_restore (&pagination_disabled_for_command, false);
1768   scoped_restore save_batch
1769     = make_scoped_restore (&batch_flag, false);
1770   scoped_restore save_lines
1771     = make_scoped_restore (&lines_per_page, 50);
1772   /* Make it easy to word wrap.  */
1773   scoped_restore save_chars
1774     = make_scoped_restore (&chars_per_line, 15);
1775   scoped_restore save_printed
1776     = make_scoped_restore (&chars_printed, 0);
1777 
1778   pager.puts ("aaaaaaaaaaaa");
1779   pager.wrap_here (2);
1780   pager.puts ("bbbbbbbbbbbb\n");
1781 
1782   SELF_CHECK (strfile->string () == "aaaaaaaaaaaa\n  bbbbbbbbbbbb\n");
1783 }
1784 
1785 #endif /* GDB_SELF_TEST */
1786 
1787 void
1788 gdb_puts (const char *linebuffer, struct ui_file *stream)
1789 {
1790   stream->puts (linebuffer);
1791 }
1792 
1793 /* See utils.h.  */
1794 
1795 void
1796 fputs_styled (const char *linebuffer, const ui_file_style &style,
1797 	      struct ui_file *stream)
1798 {
1799   stream->emit_style_escape (style);
1800   gdb_puts (linebuffer, stream);
1801   stream->emit_style_escape (ui_file_style ());
1802 }
1803 
1804 /* See utils.h.  */
1805 
1806 void
1807 fputs_highlighted (const char *str, const compiled_regex &highlight,
1808 		   struct ui_file *stream)
1809 {
1810   regmatch_t pmatch;
1811 
1812   while (*str && highlight.exec (str, 1, &pmatch, 0) == 0)
1813     {
1814       size_t n_highlight = pmatch.rm_eo - pmatch.rm_so;
1815 
1816       /* Output the part before pmatch with current style.  */
1817       while (pmatch.rm_so > 0)
1818 	{
1819 	  gdb_putc (*str, stream);
1820 	  pmatch.rm_so--;
1821 	  str++;
1822 	}
1823 
1824       /* Output pmatch with the highlight style.  */
1825       stream->emit_style_escape (highlight_style.style ());
1826       while (n_highlight > 0)
1827 	{
1828 	  gdb_putc (*str, stream);
1829 	  n_highlight--;
1830 	  str++;
1831 	}
1832       stream->emit_style_escape (ui_file_style ());
1833     }
1834 
1835   /* Output the trailing part of STR not matching HIGHLIGHT.  */
1836   if (*str)
1837     gdb_puts (str, stream);
1838 }
1839 
1840 void
1841 gdb_putc (int c)
1842 {
1843   return gdb_stdout->putc (c);
1844 }
1845 
1846 void
1847 gdb_putc (int c, struct ui_file *stream)
1848 {
1849   return stream->putc (c);
1850 }
1851 
1852 void
1853 gdb_vprintf (struct ui_file *stream, const char *format, va_list args)
1854 {
1855   stream->vprintf (format, args);
1856 }
1857 
1858 void
1859 gdb_vprintf (const char *format, va_list args)
1860 {
1861   gdb_stdout->vprintf (format, args);
1862 }
1863 
1864 void
1865 gdb_printf (struct ui_file *stream, const char *format, ...)
1866 {
1867   va_list args;
1868 
1869   va_start (args, format);
1870   gdb_vprintf (stream, format, args);
1871   va_end (args);
1872 }
1873 
1874 /* See utils.h.  */
1875 
1876 void
1877 fprintf_styled (struct ui_file *stream, const ui_file_style &style,
1878 		const char *format, ...)
1879 {
1880   va_list args;
1881 
1882   stream->emit_style_escape (style);
1883   va_start (args, format);
1884   gdb_vprintf (stream, format, args);
1885   va_end (args);
1886   stream->emit_style_escape (ui_file_style ());
1887 }
1888 
1889 void
1890 gdb_printf (const char *format, ...)
1891 {
1892   va_list args;
1893 
1894   va_start (args, format);
1895   gdb_vprintf (gdb_stdout, format, args);
1896   va_end (args);
1897 }
1898 
1899 
1900 void
1901 printf_unfiltered (const char *format, ...)
1902 {
1903   va_list args;
1904 
1905   va_start (args, format);
1906   string_file file (gdb_stdout->can_emit_style_escape ());
1907   file.vprintf (format, args);
1908   gdb_stdout->puts_unfiltered (file.string ().c_str ());
1909   va_end (args);
1910 }
1911 
1912 /* Easy -- but watch out!
1913 
1914    This routine is *not* a replacement for puts()!  puts() appends a newline.
1915    This one doesn't, and had better not!  */
1916 
1917 void
1918 gdb_puts (const char *string)
1919 {
1920   gdb_stdout->puts (string);
1921 }
1922 
1923 /* Return a pointer to N spaces and a null.  The pointer is good
1924    until the next call to here.  */
1925 const char *
1926 n_spaces (int n)
1927 {
1928   char *t;
1929   static char *spaces = 0;
1930   static int max_spaces = -1;
1931 
1932   if (n > max_spaces)
1933     {
1934       xfree (spaces);
1935       spaces = (char *) xmalloc (n + 1);
1936       for (t = spaces + n; t != spaces;)
1937 	*--t = ' ';
1938       spaces[n] = '\0';
1939       max_spaces = n;
1940     }
1941 
1942   return spaces + max_spaces - n;
1943 }
1944 
1945 /* Print N spaces.  */
1946 void
1947 print_spaces (int n, struct ui_file *stream)
1948 {
1949   gdb_puts (n_spaces (n), stream);
1950 }
1951 
1952 /* C++/ObjC demangler stuff.  */
1953 
1954 /* fprintf_symbol attempts to demangle NAME, a symbol in language
1955    LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1956    If the name is not mangled, or the language for the name is unknown, or
1957    demangling is off, the name is printed in its "raw" form.  */
1958 
1959 void
1960 fprintf_symbol (struct ui_file *stream, const char *name,
1961 		enum language lang, int arg_mode)
1962 {
1963   if (name != NULL)
1964     {
1965       /* If user wants to see raw output, no problem.  */
1966       if (!demangle)
1967 	{
1968 	  gdb_puts (name, stream);
1969 	}
1970       else
1971 	{
1972 	  gdb::unique_xmalloc_ptr<char> demangled
1973 	    = language_demangle (language_def (lang), name, arg_mode);
1974 	  gdb_puts (demangled ? demangled.get () : name, stream);
1975 	}
1976     }
1977 }
1978 
1979 /* True if CH is a character that can be part of a symbol name.  I.e.,
1980    either a number, a letter, or a '_'.  */
1981 
1982 static bool
1983 valid_identifier_name_char (int ch)
1984 {
1985   return (ISALNUM (ch) || ch == '_');
1986 }
1987 
1988 /* Skip to end of token, or to END, whatever comes first.  Input is
1989    assumed to be a C++ operator name.  */
1990 
1991 static const char *
1992 cp_skip_operator_token (const char *token, const char *end)
1993 {
1994   const char *p = token;
1995   while (p != end && !ISSPACE (*p) && *p != '(')
1996     {
1997       if (valid_identifier_name_char (*p))
1998 	{
1999 	  while (p != end && valid_identifier_name_char (*p))
2000 	    p++;
2001 	  return p;
2002 	}
2003       else
2004 	{
2005 	  /* Note, ordered such that among ops that share a prefix,
2006 	     longer comes first.  This is so that the loop below can
2007 	     bail on first match.  */
2008 	  static const char *ops[] =
2009 	    {
2010 	      "[",
2011 	      "]",
2012 	      "~",
2013 	      ",",
2014 	      "-=", "--", "->", "-",
2015 	      "+=", "++", "+",
2016 	      "*=", "*",
2017 	      "/=", "/",
2018 	      "%=", "%",
2019 	      "|=", "||", "|",
2020 	      "&=", "&&", "&",
2021 	      "^=", "^",
2022 	      "!=", "!",
2023 	      "<<=", "<=", "<<", "<",
2024 	      ">>=", ">=", ">>", ">",
2025 	      "==", "=",
2026 	    };
2027 
2028 	  for (const char *op : ops)
2029 	    {
2030 	      size_t oplen = strlen (op);
2031 	      size_t lencmp = std::min<size_t> (oplen, end - p);
2032 
2033 	      if (strncmp (p, op, lencmp) == 0)
2034 		return p + lencmp;
2035 	    }
2036 	  /* Some unidentified character.  Return it.  */
2037 	  return p + 1;
2038 	}
2039     }
2040 
2041   return p;
2042 }
2043 
2044 /* Advance STRING1/STRING2 past whitespace.  */
2045 
2046 static void
2047 skip_ws (const char *&string1, const char *&string2, const char *end_str2)
2048 {
2049   while (ISSPACE (*string1))
2050     string1++;
2051   while (string2 < end_str2 && ISSPACE (*string2))
2052     string2++;
2053 }
2054 
2055 /* True if STRING points at the start of a C++ operator name.  START
2056    is the start of the string that STRING points to, hence when
2057    reading backwards, we must not read any character before START.  */
2058 
2059 static bool
2060 cp_is_operator (const char *string, const char *start)
2061 {
2062   return ((string == start
2063 	   || !valid_identifier_name_char (string[-1]))
2064 	  && strncmp (string, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
2065 	  && !valid_identifier_name_char (string[CP_OPERATOR_LEN]));
2066 }
2067 
2068 /* If *NAME points at an ABI tag, skip it and return true.  Otherwise
2069    leave *NAME unmodified and return false.  (see GCC's abi_tag
2070    attribute), such names are demangled as e.g.,
2071    "function[abi:cxx11]()".  */
2072 
2073 static bool
2074 skip_abi_tag (const char **name)
2075 {
2076   const char *p = *name;
2077 
2078   if (startswith (p, "[abi:"))
2079     {
2080       p += 5;
2081 
2082       while (valid_identifier_name_char (*p))
2083 	p++;
2084 
2085       if (*p == ']')
2086 	{
2087 	  p++;
2088 	  *name = p;
2089 	  return true;
2090 	}
2091     }
2092   return false;
2093 }
2094 
2095 /* If *NAME points at a template parameter list, skip it and return true.
2096    Otherwise do nothing and return false.  */
2097 
2098 static bool
2099 skip_template_parameter_list (const char **name)
2100 {
2101   const char *p = *name;
2102 
2103   if (*p == '<')
2104     {
2105       const char *template_param_list_end = find_toplevel_char (p + 1, '>');
2106 
2107       if (template_param_list_end == NULL)
2108 	return false;
2109 
2110       p = template_param_list_end + 1;
2111 
2112       /* Skip any whitespace that might occur after the closing of the
2113 	 parameter list, but only if it is the end of parameter list.  */
2114       const char *q = p;
2115       while (ISSPACE (*q))
2116 	++q;
2117       if (*q == '>')
2118 	p = q;
2119       *name = p;
2120       return true;
2121     }
2122 
2123   return false;
2124 }
2125 
2126 /* See utils.h.  */
2127 
2128 int
2129 strncmp_iw_with_mode (const char *string1, const char *string2,
2130 		      size_t string2_len, strncmp_iw_mode mode,
2131 		      enum language language,
2132 		      completion_match_for_lcd *match_for_lcd,
2133 		      bool ignore_template_params)
2134 {
2135   const char *string1_start = string1;
2136   const char *end_str2 = string2 + string2_len;
2137   bool skip_spaces = true;
2138   bool have_colon_op = (language == language_cplus
2139 			|| language == language_rust
2140 			|| language == language_fortran);
2141 
2142   while (1)
2143     {
2144       if (skip_spaces
2145 	  || ((ISSPACE (*string1) && !valid_identifier_name_char (*string2))
2146 	      || (ISSPACE (*string2) && !valid_identifier_name_char (*string1))))
2147 	{
2148 	  skip_ws (string1, string2, end_str2);
2149 	  skip_spaces = false;
2150 	}
2151 
2152       /* Skip [abi:cxx11] tags in the symbol name if the lookup name
2153 	 doesn't include them.  E.g.:
2154 
2155 	 string1: function[abi:cxx1](int)
2156 	 string2: function
2157 
2158 	 string1: function[abi:cxx1](int)
2159 	 string2: function(int)
2160 
2161 	 string1: Struct[abi:cxx1]::function()
2162 	 string2: Struct::function()
2163 
2164 	 string1: function(Struct[abi:cxx1], int)
2165 	 string2: function(Struct, int)
2166       */
2167       if (string2 == end_str2
2168 	  || (*string2 != '[' && !valid_identifier_name_char (*string2)))
2169 	{
2170 	  const char *abi_start = string1;
2171 
2172 	  /* There can be more than one tag.  */
2173 	  while (*string1 == '[' && skip_abi_tag (&string1))
2174 	    ;
2175 
2176 	  if (match_for_lcd != NULL && abi_start != string1)
2177 	    match_for_lcd->mark_ignored_range (abi_start, string1);
2178 
2179 	  while (ISSPACE (*string1))
2180 	    string1++;
2181 	}
2182 
2183       /* Skip template parameters in STRING1 if STRING2 does not contain
2184 	 any.  E.g.:
2185 
2186 	 Case 1: User is looking for all functions named "foo".
2187 	 string1: foo <...> (...)
2188 	 string2: foo
2189 
2190 	 Case 2: User is looking for all methods named "foo" in all template
2191 	 class instantiations.
2192 	 string1: Foo<...>::foo <...> (...)
2193 	 string2: Foo::foo (...)
2194 
2195 	 Case 3: User is looking for a specific overload of a template
2196 	 function or method.
2197 	 string1: foo<...>
2198 	 string2: foo(...)
2199 
2200 	 Case 4: User is looking for a specific overload of a specific
2201 	 template instantiation.
2202 	 string1: foo<A> (...)
2203 	 string2: foo<B> (...)
2204 
2205 	 Case 5: User is looking wild parameter match.
2206 	 string1: foo<A<a<b<...> > > > (...)
2207 	 string2: foo<A
2208       */
2209       if (language == language_cplus && ignore_template_params
2210 	  && *string1 == '<' && *string2 != '<')
2211 	{
2212 	  /* Skip any parameter list in STRING1.  */
2213 	  const char *template_start = string1;
2214 
2215 	  if (skip_template_parameter_list (&string1))
2216 	    {
2217 	      /* Don't mark the parameter list ignored if the user didn't
2218 		 try to ignore it.  [Case #5 above]  */
2219 	      if (*string2 != '\0'
2220 		  && match_for_lcd != NULL && template_start != string1)
2221 		match_for_lcd->mark_ignored_range (template_start, string1);
2222 	    }
2223 	}
2224 
2225       if (*string1 == '\0' || string2 == end_str2)
2226 	break;
2227 
2228       /* Handle the :: operator.  */
2229       if (have_colon_op && string1[0] == ':' && string1[1] == ':')
2230 	{
2231 	  if (*string2 != ':')
2232 	    return 1;
2233 
2234 	  string1++;
2235 	  string2++;
2236 
2237 	  if (string2 == end_str2)
2238 	    break;
2239 
2240 	  if (*string2 != ':')
2241 	    return 1;
2242 
2243 	  string1++;
2244 	  string2++;
2245 
2246 	  while (ISSPACE (*string1))
2247 	    string1++;
2248 	  while (string2 < end_str2 && ISSPACE (*string2))
2249 	    string2++;
2250 	  continue;
2251 	}
2252 
2253       /* Handle C++ user-defined operators.  */
2254       else if (language == language_cplus
2255 	       && *string1 == 'o')
2256 	{
2257 	  if (cp_is_operator (string1, string1_start))
2258 	    {
2259 	      /* An operator name in STRING1.  Check STRING2.  */
2260 	      size_t cmplen
2261 		= std::min<size_t> (CP_OPERATOR_LEN, end_str2 - string2);
2262 	      if (strncmp (string1, string2, cmplen) != 0)
2263 		return 1;
2264 
2265 	      string1 += cmplen;
2266 	      string2 += cmplen;
2267 
2268 	      if (string2 != end_str2)
2269 		{
2270 		  /* Check for "operatorX" in STRING2.  */
2271 		  if (valid_identifier_name_char (*string2))
2272 		    return 1;
2273 
2274 		  skip_ws (string1, string2, end_str2);
2275 		}
2276 
2277 	      /* Handle operator().  */
2278 	      if (*string1 == '(')
2279 		{
2280 		  if (string2 == end_str2)
2281 		    {
2282 		      if (mode == strncmp_iw_mode::NORMAL)
2283 			return 0;
2284 		      else
2285 			{
2286 			  /* Don't break for the regular return at the
2287 			     bottom, because "operator" should not
2288 			     match "operator()", since this open
2289 			     parentheses is not the parameter list
2290 			     start.  */
2291 			  return *string1 != '\0';
2292 			}
2293 		    }
2294 
2295 		  if (*string1 != *string2)
2296 		    return 1;
2297 
2298 		  string1++;
2299 		  string2++;
2300 		}
2301 
2302 	      while (1)
2303 		{
2304 		  skip_ws (string1, string2, end_str2);
2305 
2306 		  /* Skip to end of token, or to END, whatever comes
2307 		     first.  */
2308 		  const char *end_str1 = string1 + strlen (string1);
2309 		  const char *p1 = cp_skip_operator_token (string1, end_str1);
2310 		  const char *p2 = cp_skip_operator_token (string2, end_str2);
2311 
2312 		  cmplen = std::min (p1 - string1, p2 - string2);
2313 		  if (p2 == end_str2)
2314 		    {
2315 		      if (strncmp (string1, string2, cmplen) != 0)
2316 			return 1;
2317 		    }
2318 		  else
2319 		    {
2320 		      if (p1 - string1 != p2 - string2)
2321 			return 1;
2322 		      if (strncmp (string1, string2, cmplen) != 0)
2323 			return 1;
2324 		    }
2325 
2326 		  string1 += cmplen;
2327 		  string2 += cmplen;
2328 
2329 		  if (*string1 == '\0' || string2 == end_str2)
2330 		    break;
2331 		  if (*string1 == '(' || *string2 == '(')
2332 		    break;
2333 
2334 		  /* If STRING1 or STRING2 starts with a template
2335 		     parameter list, break out of operator processing.  */
2336 		  skip_ws (string1, string2, end_str2);
2337 		  if (*string1 == '<' || *string2 == '<')
2338 		    break;
2339 		}
2340 
2341 	      continue;
2342 	    }
2343 	}
2344 
2345       if (case_sensitivity == case_sensitive_on && *string1 != *string2)
2346 	break;
2347       if (case_sensitivity == case_sensitive_off
2348 	  && (TOLOWER ((unsigned char) *string1)
2349 	      != TOLOWER ((unsigned char) *string2)))
2350 	break;
2351 
2352       /* If we see any non-whitespace, non-identifier-name character
2353 	 (any of "()<>*&" etc.), then skip spaces the next time
2354 	 around.  */
2355       if (!ISSPACE (*string1) && !valid_identifier_name_char (*string1))
2356 	skip_spaces = true;
2357 
2358       string1++;
2359       string2++;
2360     }
2361 
2362   if (string2 == end_str2)
2363     {
2364       if (mode == strncmp_iw_mode::NORMAL)
2365 	{
2366 	  /* Strip abi tag markers from the matched symbol name.
2367 	     Usually the ABI marker will be found on function name
2368 	     (automatically added because the function returns an
2369 	     object marked with an ABI tag).  However, it's also
2370 	     possible to see a marker in one of the function
2371 	     parameters, for example.
2372 
2373 	     string2 (lookup name):
2374 	       func
2375 	     symbol name:
2376 	       function(some_struct[abi:cxx11], int)
2377 
2378 	     and for completion LCD computation we want to say that
2379 	     the match was for:
2380 	       function(some_struct, int)
2381 	  */
2382 	  if (match_for_lcd != NULL)
2383 	    {
2384 	      while ((string1 = strstr (string1, "[abi:")) != NULL)
2385 		{
2386 		  const char *abi_start = string1;
2387 
2388 		  /* There can be more than one tag.  */
2389 		  while (skip_abi_tag (&string1) && *string1 == '[')
2390 		    ;
2391 
2392 		  if (abi_start != string1)
2393 		    match_for_lcd->mark_ignored_range (abi_start, string1);
2394 		}
2395 	    }
2396 
2397 	  return 0;
2398 	}
2399       else
2400 	return (*string1 != '\0' && *string1 != '(');
2401     }
2402   else
2403     return 1;
2404 }
2405 
2406 #if GDB_SELF_TEST
2407 
2408 /* Unit tests for strncmp_iw_with_mode.  */
2409 
2410 #define CHECK_MATCH_LM(S1, S2, MODE, LANG, LCD)			\
2411   SELF_CHECK (strncmp_iw_with_mode ((S1), (S2), strlen ((S2)),	\
2412 				    strncmp_iw_mode::MODE,				\
2413 				    (LANG), (LCD)) == 0)
2414 
2415 #define CHECK_MATCH_LANG(S1, S2, MODE, LANG)			\
2416   CHECK_MATCH_LM ((S1), (S2), MODE, (LANG), nullptr)
2417 
2418 #define CHECK_MATCH(S1, S2, MODE)						\
2419   CHECK_MATCH_LANG ((S1), (S2), MODE, language_minimal)
2420 
2421 #define CHECK_NO_MATCH_LM(S1, S2, MODE, LANG, LCD)		\
2422   SELF_CHECK (strncmp_iw_with_mode ((S1), (S2), strlen ((S2)),	\
2423 				    strncmp_iw_mode::MODE,				\
2424 				    (LANG)) != 0)
2425 
2426 #define CHECK_NO_MATCH_LANG(S1, S2, MODE, LANG)		\
2427   CHECK_NO_MATCH_LM ((S1), (S2), MODE, (LANG), nullptr)
2428 
2429 #define CHECK_NO_MATCH(S1, S2, MODE)				       \
2430   CHECK_NO_MATCH_LANG ((S1), (S2), MODE, language_minimal)
2431 
2432 static void
2433 check_scope_operator (enum language lang)
2434 {
2435   CHECK_MATCH_LANG ("::", "::", NORMAL, lang);
2436   CHECK_MATCH_LANG ("::foo", "::", NORMAL, lang);
2437   CHECK_MATCH_LANG ("::foo", "::foo", NORMAL, lang);
2438   CHECK_MATCH_LANG (" :: foo ", "::foo", NORMAL, lang);
2439   CHECK_MATCH_LANG ("a::b", "a ::b", NORMAL, lang);
2440   CHECK_MATCH_LANG ("a::b", "a\t::b", NORMAL, lang);
2441   CHECK_MATCH_LANG ("a::b", "a \t::b", NORMAL, lang);
2442   CHECK_MATCH_LANG ("a::b", "a\t ::b", NORMAL, lang);
2443   CHECK_MATCH_LANG ("a::b", "a:: b", NORMAL, lang);
2444   CHECK_MATCH_LANG ("a::b", "a::\tb", NORMAL, lang);
2445   CHECK_MATCH_LANG ("a::b", "a:: \tb", NORMAL, lang);
2446   CHECK_MATCH_LANG ("a::b", "a::\t b", NORMAL, lang);
2447   CHECK_MATCH_LANG ("a::b", "a :: b", NORMAL, lang);
2448   CHECK_MATCH_LANG ("a::b", "a ::\tb", NORMAL, lang);
2449   CHECK_MATCH_LANG ("a::b", "a\t:: b", NORMAL, lang);
2450   CHECK_MATCH_LANG ("a::b", "a \t::\t b", NORMAL, lang);
2451   CHECK_MATCH_LANG ("a ::b", "a::b", NORMAL, lang);
2452   CHECK_MATCH_LANG ("a\t::b", "a::b", NORMAL, lang);
2453   CHECK_MATCH_LANG ("a \t::b", "a::b", NORMAL, lang);
2454   CHECK_MATCH_LANG ("a\t ::b", "a::b", NORMAL, lang);
2455   CHECK_MATCH_LANG ("a:: b", "a::b", NORMAL, lang);
2456   CHECK_MATCH_LANG ("a::\tb", "a::b", NORMAL, lang);
2457   CHECK_MATCH_LANG ("a:: \tb", "a::b", NORMAL, lang);
2458   CHECK_MATCH_LANG ("a::\t b", "a::b", NORMAL, lang);
2459   CHECK_MATCH_LANG ("a :: b", "a::b", NORMAL, lang);
2460   CHECK_MATCH_LANG ("a ::\tb", "a::b", NORMAL, lang);
2461   CHECK_MATCH_LANG ("a\t:: b", "a::b", NORMAL, lang);
2462   CHECK_MATCH_LANG ("a \t::\t b", "a::b", NORMAL, lang);
2463   CHECK_MATCH_LANG ("a::b::c", "a::b::c", NORMAL, lang);
2464   CHECK_MATCH_LANG (" a:: b:: c", "a::b::c", NORMAL, lang);
2465   CHECK_MATCH_LANG ("a::b::c", " a:: b:: c", NORMAL, lang);
2466   CHECK_MATCH_LANG ("a ::b ::c", "a::b::c", NORMAL, lang);
2467   CHECK_MATCH_LANG ("a::b::c", "a :: b:: c", NORMAL, lang);
2468   CHECK_MATCH_LANG ("\ta::\tb::\tc", "\ta::\tb::\tc", NORMAL, lang);
2469   CHECK_MATCH_LANG ("a\t::b\t::c\t", "a\t::b\t::c\t", NORMAL, lang);
2470   CHECK_MATCH_LANG (" \ta:: \tb:: \tc", " \ta:: \tb:: \tc", NORMAL, lang);
2471   CHECK_MATCH_LANG ("\t a::\t b::\t c", "\t a::\t b::\t c", NORMAL, lang);
2472   CHECK_MATCH_LANG ("a::b::c", "\ta::\tb::\tc", NORMAL, lang);
2473   CHECK_MATCH_LANG ("a::b::c", "a\t::b\t::c\t", NORMAL, lang);
2474   CHECK_MATCH_LANG ("a::b::c", " \ta:: \tb:: \tc", NORMAL, lang);
2475   CHECK_MATCH_LANG ("a::b::c", "\t a::\t b::\t c", NORMAL, lang);
2476   CHECK_MATCH_LANG ("\ta::\tb::\tc", "a::b::c", NORMAL, lang);
2477   CHECK_MATCH_LANG ("a\t::b\t::c\t", "a::b::c", NORMAL, lang);
2478   CHECK_MATCH_LANG (" \ta:: \tb:: \tc", "a::b::c", NORMAL, lang);
2479   CHECK_MATCH_LANG ("\t a::\t b::\t c", "a::b::c", NORMAL, lang);
2480   CHECK_MATCH_LANG ("a :: b:: c\t", "\ta :: b\t::  c\t\t", NORMAL, lang);
2481   CHECK_MATCH_LANG ("  a::\t  \t    b::     c\t", "\ta ::b::  c\t\t",
2482 	      NORMAL, lang);
2483   CHECK_MATCH_LANG ("a      :: b               :: \t\t\tc\t",
2484 	      "\t\t\t\ta        ::   \t\t\t        b             \t\t::c",
2485 	      NORMAL, lang);
2486   CHECK_MATCH_LANG ("a::b()", "a", NORMAL, lang);
2487   CHECK_MATCH_LANG ("a::b()", "a::", NORMAL, lang);
2488   CHECK_MATCH_LANG ("a::b()", "a::b", NORMAL, lang);
2489   CHECK_MATCH_LANG ("a::b(a)", "a", NORMAL, lang);
2490   CHECK_MATCH_LANG ("a::b(a)", "a::", NORMAL, lang);
2491   CHECK_MATCH_LANG ("a::b(a)", "a::b", NORMAL, lang);
2492   CHECK_MATCH_LANG ("a::b(a,b)", "a", NORMAL, lang);
2493   CHECK_MATCH_LANG ("a::b(a,b)", "a::", NORMAL, lang);
2494   CHECK_MATCH_LANG ("a::b(a,b)", "a::b", NORMAL, lang);
2495   CHECK_MATCH_LANG ("a::b(a,b,c)", "a", NORMAL, lang);
2496   CHECK_MATCH_LANG ("a::b(a,b,c)", "a::", NORMAL, lang);
2497   CHECK_MATCH_LANG ("a::b(a,b,c)", "a::b", NORMAL, lang);
2498 
2499   CHECK_NO_MATCH_LANG ("a::", "::a", NORMAL, lang);
2500   CHECK_NO_MATCH_LANG ("::a", "::a()", NORMAL, lang);
2501   CHECK_NO_MATCH_LANG ("::", "::a", NORMAL, lang);
2502   CHECK_NO_MATCH_LANG ("a:::b", "a::b", NORMAL, lang);
2503   CHECK_NO_MATCH_LANG ("a::b()", "a::b(a)", NORMAL, lang);
2504   CHECK_NO_MATCH_LANG ("a::b(a)", "a::b()", NORMAL, lang);
2505   CHECK_NO_MATCH_LANG ("a::b(a,b)", "a::b(a,a)", NORMAL, lang);
2506   CHECK_NO_MATCH_LANG ("a::b", "a()", NORMAL, lang);
2507   CHECK_NO_MATCH_LANG ("a::b", "a::()", NORMAL, lang);
2508   CHECK_NO_MATCH_LANG ("a::b", "a::b()", NORMAL, lang);
2509   CHECK_NO_MATCH_LANG ("a::b", "a(a)", NORMAL, lang);
2510   CHECK_NO_MATCH_LANG ("a::b", "a::(a)", NORMAL, lang);
2511   CHECK_NO_MATCH_LANG ("a::b", "a::b()", NORMAL, lang);
2512   CHECK_NO_MATCH_LANG ("a::b", "a(a,b)", NORMAL, lang);
2513   CHECK_NO_MATCH_LANG ("a::b", "a::(a,b)", NORMAL, lang);
2514   CHECK_NO_MATCH_LANG ("a::b", "a::b(a,b)", NORMAL, lang);
2515   CHECK_NO_MATCH_LANG ("a::b", "a(a,b,c)", NORMAL, lang);
2516   CHECK_NO_MATCH_LANG ("a::b", "a::(a,b,c)", NORMAL, lang);
2517   CHECK_NO_MATCH_LANG ("a::b", "a::b(a,b,c)", NORMAL, lang);
2518 }
2519 
2520 /* Callback for strncmp_iw_with_mode unit tests.  */
2521 
2522 static void
2523 strncmp_iw_with_mode_tests ()
2524 {
2525   /* Some of the following tests are nonsensical, but could be input by a
2526      deranged script (or user).  */
2527 
2528   /* strncmp_iw_mode::NORMAL: strcmp()-like but ignore any whitespace...  */
2529 
2530   CHECK_MATCH ("", "", NORMAL);
2531   CHECK_MATCH ("foo", "foo", NORMAL);
2532   CHECK_MATCH (" foo", "foo", NORMAL);
2533   CHECK_MATCH ("foo ", "foo", NORMAL);
2534   CHECK_MATCH (" foo ", "foo", NORMAL);
2535   CHECK_MATCH ("  foo", "foo", NORMAL);
2536   CHECK_MATCH ("foo  ", "foo", NORMAL);
2537   CHECK_MATCH ("  foo  ", "foo", NORMAL);
2538   CHECK_MATCH ("\tfoo", "foo", NORMAL);
2539   CHECK_MATCH ("foo\t", "foo", NORMAL);
2540   CHECK_MATCH ("\tfoo\t", "foo", NORMAL);
2541   CHECK_MATCH (" \tfoo \t", "foo", NORMAL);
2542   CHECK_MATCH ("\t foo\t ", "foo", NORMAL);
2543   CHECK_MATCH ("\t \t     \t\t\t\t   foo\t\t\t  \t\t   \t   \t    \t  \t ",
2544 	       "foo", NORMAL);
2545   CHECK_MATCH ("foo",
2546 	       "\t \t     \t\t\t\t   foo\t\t\t  \t\t   \t   \t    \t  \t ",
2547 	       NORMAL);
2548   CHECK_MATCH ("foo bar", "foo", NORMAL);
2549   CHECK_NO_MATCH ("foo", "bar", NORMAL);
2550   CHECK_NO_MATCH ("foo bar", "foobar", NORMAL);
2551   CHECK_NO_MATCH (" foo ", "bar", NORMAL);
2552   CHECK_NO_MATCH ("foo", " bar ", NORMAL);
2553   CHECK_NO_MATCH (" \t\t    foo\t\t ", "\t    \t    \tbar\t", NORMAL);
2554   CHECK_NO_MATCH ("@!%&", "@!%&foo", NORMAL);
2555 
2556   /* ... and function parameters in STRING1.  */
2557   CHECK_MATCH ("foo()", "foo()", NORMAL);
2558   CHECK_MATCH ("foo ()", "foo()", NORMAL);
2559   CHECK_MATCH ("foo  ()", "foo()", NORMAL);
2560   CHECK_MATCH ("foo\t()", "foo()", NORMAL);
2561   CHECK_MATCH ("foo\t  ()", "foo()", NORMAL);
2562   CHECK_MATCH ("foo  \t()", "foo()", NORMAL);
2563   CHECK_MATCH ("foo()", "foo ()", NORMAL);
2564   CHECK_MATCH ("foo()", "foo  ()", NORMAL);
2565   CHECK_MATCH ("foo()", "foo\t()", NORMAL);
2566   CHECK_MATCH ("foo()", "foo\t ()", NORMAL);
2567   CHECK_MATCH ("foo()", "foo \t()", NORMAL);
2568   CHECK_MATCH ("foo()", "foo()", NORMAL);
2569   CHECK_MATCH ("foo ()", "foo ()", NORMAL);
2570   CHECK_MATCH ("foo  ()", "foo  ()", NORMAL);
2571   CHECK_MATCH ("foo\t()", "foo\t()", NORMAL);
2572   CHECK_MATCH ("foo\t  ()", "foo\t ()", NORMAL);
2573   CHECK_MATCH ("foo  \t()", "foo \t()", NORMAL);
2574   CHECK_MATCH ("foo(a)", "foo(a)", NORMAL);
2575   CHECK_MATCH ("foo( a)", "foo(a)", NORMAL);
2576   CHECK_MATCH ("foo(a )", "foo(a)", NORMAL);
2577   CHECK_MATCH ("foo(\ta)", "foo(a)", NORMAL);
2578   CHECK_MATCH ("foo(a\t)", "foo(a)", NORMAL);
2579   CHECK_MATCH ("foo(\t a)", "foo(a)", NORMAL);
2580   CHECK_MATCH ("foo( \ta)", "foo(a)", NORMAL);
2581   CHECK_MATCH ("foo(a\t )", "foo(a)", NORMAL);
2582   CHECK_MATCH ("foo(a \t)", "foo(a)", NORMAL);
2583   CHECK_MATCH ("foo( a )", "foo(a)", NORMAL);
2584   CHECK_MATCH ("foo(\ta\t)", "foo(a)", NORMAL);
2585   CHECK_MATCH ("foo(\t a\t )", "foo(a)", NORMAL);
2586   CHECK_MATCH ("foo( \ta \t)", "foo(a)", NORMAL);
2587   CHECK_MATCH ("foo(a)", "foo( a)", NORMAL);
2588   CHECK_MATCH ("foo(a)", "foo(a )", NORMAL);
2589   CHECK_MATCH ("foo(a)", "foo(\ta)", NORMAL);
2590   CHECK_MATCH ("foo(a)", "foo(a\t)", NORMAL);
2591   CHECK_MATCH ("foo(a)", "foo(\t a)", NORMAL);
2592   CHECK_MATCH ("foo(a)", "foo( \ta)", NORMAL);
2593   CHECK_MATCH ("foo(a)", "foo(a\t )", NORMAL);
2594   CHECK_MATCH ("foo(a)", "foo(a \t)", NORMAL);
2595   CHECK_MATCH ("foo(a)", "foo( a )", NORMAL);
2596   CHECK_MATCH ("foo(a)", "foo(\ta\t)", NORMAL);
2597   CHECK_MATCH ("foo(a)", "foo(\t a\t )", NORMAL);
2598   CHECK_MATCH ("foo(a)", "foo( \ta \t)", NORMAL);
2599   CHECK_MATCH ("foo(a,b)", "foo(a,b)", NORMAL);
2600   CHECK_MATCH ("foo(a ,b)", "foo(a,b)", NORMAL);
2601   CHECK_MATCH ("foo(a\t,b)", "foo(a,b)", NORMAL);
2602   CHECK_MATCH ("foo(a,\tb)", "foo(a,b)", NORMAL);
2603   CHECK_MATCH ("foo(a\t,\tb)", "foo(a,b)", NORMAL);
2604   CHECK_MATCH ("foo(a \t,b)", "foo(a,b)", NORMAL);
2605   CHECK_MATCH ("foo(a\t ,b)", "foo(a,b)", NORMAL);
2606   CHECK_MATCH ("foo(a,\tb)", "foo(a,b)", NORMAL);
2607   CHECK_MATCH ("foo(a, \tb)", "foo(a,b)", NORMAL);
2608   CHECK_MATCH ("foo(a,\t b)", "foo(a,b)", NORMAL);
2609   CHECK_MATCH ("foo(a,b)", "foo(a ,b)", NORMAL);
2610   CHECK_MATCH ("foo(a,b)", "foo(a\t,b)", NORMAL);
2611   CHECK_MATCH ("foo(a,b)", "foo(a,\tb)", NORMAL);
2612   CHECK_MATCH ("foo(a,b)", "foo(a\t,\tb)", NORMAL);
2613   CHECK_MATCH ("foo(a,b)", "foo(a \t,b)", NORMAL);
2614   CHECK_MATCH ("foo(a,b)", "foo(a\t ,b)", NORMAL);
2615   CHECK_MATCH ("foo(a,b)", "foo(a,\tb)", NORMAL);
2616   CHECK_MATCH ("foo(a,b)", "foo(a, \tb)", NORMAL);
2617   CHECK_MATCH ("foo(a,b)", "foo(a,\t b)", NORMAL);
2618   CHECK_MATCH ("foo(a,b,c,d)", "foo(a,b,c,d)", NORMAL);
2619   CHECK_MATCH (" foo ( a , b , c , d ) ", "foo(a,b,c,d)", NORMAL);
2620   CHECK_MATCH (" foo ( a , b , c , d ) ", "foo( a , b , c , d )", NORMAL);
2621   CHECK_MATCH ("foo &\t*(\ta b    *\t\t&)", "foo", NORMAL);
2622   CHECK_MATCH ("foo &\t*(\ta b    *\t\t&)", "foo&*(a b * &)", NORMAL);
2623   CHECK_MATCH ("foo(a) b", "foo(a)", NORMAL);
2624   CHECK_MATCH ("*foo(*a&)", "*foo", NORMAL);
2625   CHECK_MATCH ("*foo(*a&)", "*foo(*a&)", NORMAL);
2626   CHECK_MATCH ("*a&b#c/^d$foo(*a&)", "*a&b#c/^d$foo", NORMAL);
2627   CHECK_MATCH ("* foo", "*foo", NORMAL);
2628   CHECK_MATCH ("foo&", "foo", NORMAL);
2629   CHECK_MATCH ("foo*", "foo", NORMAL);
2630   CHECK_MATCH ("foo.", "foo", NORMAL);
2631   CHECK_MATCH ("foo->", "foo", NORMAL);
2632 
2633   CHECK_NO_MATCH ("foo", "foo(", NORMAL);
2634   CHECK_NO_MATCH ("foo", "foo()", NORMAL);
2635   CHECK_NO_MATCH ("foo", "foo(a)", NORMAL);
2636   CHECK_NO_MATCH ("foo", "foo(a)", NORMAL);
2637   CHECK_NO_MATCH ("foo", "foo*", NORMAL);
2638   CHECK_NO_MATCH ("foo", "foo (*", NORMAL);
2639   CHECK_NO_MATCH ("foo*", "foo (*", NORMAL);
2640   CHECK_NO_MATCH ("foo *", "foo (*", NORMAL);
2641   CHECK_NO_MATCH ("foo&", "foo (*", NORMAL);
2642   CHECK_NO_MATCH ("foo &", "foo (*", NORMAL);
2643   CHECK_NO_MATCH ("foo &*", "foo (&)", NORMAL);
2644   CHECK_NO_MATCH ("foo & \t    *\t", "foo (*", NORMAL);
2645   CHECK_NO_MATCH ("foo & \t    *\t", "foo (*", NORMAL);
2646   CHECK_NO_MATCH ("foo(a*) b", "foo(a) b", NORMAL);
2647   CHECK_NO_MATCH ("foo[aqi:A](a)", "foo(b)", NORMAL);
2648   CHECK_NO_MATCH ("*foo", "foo", NORMAL);
2649   CHECK_NO_MATCH ("*foo", "foo*", NORMAL);
2650   CHECK_NO_MATCH ("*foo*", "*foo&", NORMAL);
2651   CHECK_NO_MATCH ("*foo*", "foo *", NORMAL);
2652   CHECK_NO_MATCH ("&foo", "foo", NORMAL);
2653   CHECK_NO_MATCH ("&foo", "foo&", NORMAL);
2654   CHECK_NO_MATCH ("foo&", "&foo", NORMAL);
2655   CHECK_NO_MATCH ("foo", "foo&", NORMAL);
2656   CHECK_NO_MATCH ("foo", "foo*", NORMAL);
2657   CHECK_NO_MATCH ("foo", "foo.", NORMAL);
2658   CHECK_NO_MATCH ("foo", "foo->", NORMAL);
2659   CHECK_NO_MATCH ("foo bar", "foo()", NORMAL);
2660   CHECK_NO_MATCH ("foo bar", "foo bar()", NORMAL);
2661   CHECK_NO_MATCH ("foo()", "foo(a)", NORMAL);
2662   CHECK_NO_MATCH ("*(*)&", "*(*)*", NORMAL);
2663   CHECK_NO_MATCH ("foo(a)", "foo()", NORMAL);
2664   CHECK_NO_MATCH ("foo(a)", "foo(b)", NORMAL);
2665   CHECK_NO_MATCH ("foo(a,b)", "foo(a,b,c)", NORMAL);
2666   CHECK_NO_MATCH ("foo(a\\b)", "foo()", NORMAL);
2667   CHECK_NO_MATCH ("foo bar(a b c d)", "foobar", NORMAL);
2668   CHECK_NO_MATCH ("foo bar(a b c d)", "foobar ( a b   c \td\t)\t", NORMAL);
2669 
2670   /* Test scope operator.  */
2671   check_scope_operator (language_minimal);
2672   check_scope_operator (language_cplus);
2673   check_scope_operator (language_fortran);
2674   check_scope_operator (language_rust);
2675 
2676   /* Test C++ user-defined operators.  */
2677   CHECK_MATCH_LANG ("operator foo(int&)", "operator foo(int &)", NORMAL,
2678 		    language_cplus);
2679   CHECK_MATCH_LANG ("operator foo(int &)", "operator foo(int &)", NORMAL,
2680 		    language_cplus);
2681   CHECK_MATCH_LANG ("operator foo(int\t&)", "operator foo(int\t&)", NORMAL,
2682 		    language_cplus);
2683   CHECK_MATCH_LANG ("operator foo (int)", "operator foo(int)", NORMAL,
2684 		    language_cplus);
2685   CHECK_MATCH_LANG ("operator foo\t(int)", "operator foo(int)", NORMAL,
2686 		    language_cplus);
2687   CHECK_MATCH_LANG ("operator foo \t(int)", "operator foo(int)", NORMAL,
2688 		    language_cplus);
2689   CHECK_MATCH_LANG ("operator foo (int)", "operator foo \t(int)", NORMAL,
2690 		    language_cplus);
2691   CHECK_MATCH_LANG ("operator foo\t(int)", "operator foo \t(int)", NORMAL,
2692 		    language_cplus);
2693   CHECK_MATCH_LANG ("operator foo \t(int)", "operator foo \t(int)", NORMAL,
2694 		    language_cplus);
2695 
2696   CHECK_MATCH_LANG ("a::operator foo(int&)", "a::operator foo(int &)", NORMAL,
2697 		    language_cplus);
2698   CHECK_MATCH_LANG ("a :: operator foo(int &)", "a::operator foo(int &)", NORMAL,
2699 		    language_cplus);
2700   CHECK_MATCH_LANG ("a \t:: \toperator foo(int\t&)", "a::operator foo(int\t&)", NORMAL,
2701 		    language_cplus);
2702   CHECK_MATCH_LANG ("a::operator foo (int)", "a::operator foo(int)", NORMAL,
2703 		    language_cplus);
2704   CHECK_MATCH_LANG ("a::operator foo\t(int)", "a::operator foo(int)", NORMAL,
2705 		    language_cplus);
2706   CHECK_MATCH_LANG ("a::operator foo \t(int)", "a::operator foo(int)", NORMAL,
2707 		    language_cplus);
2708   CHECK_MATCH_LANG ("a::operator foo (int)", "a::operator foo \t(int)", NORMAL,
2709 		    language_cplus);
2710   CHECK_MATCH_LANG ("a::operator foo\t(int)", "a::operator foo \t(int)", NORMAL,
2711 		    language_cplus);
2712   CHECK_MATCH_LANG ("a::operator foo \t(int)", "a::operator foo \t(int)", NORMAL,
2713 		    language_cplus);
2714 
2715   CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(char)", NORMAL,
2716 		       language_cplus);
2717   CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int *)", NORMAL,
2718 		       language_cplus);
2719   CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int &)", NORMAL,
2720 		       language_cplus);
2721   CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int, char *)", NORMAL,
2722 		       language_cplus);
2723   CHECK_NO_MATCH_LANG ("operator foo(int)", "operator bar(int)", NORMAL,
2724 		       language_cplus);
2725 
2726   CHECK_NO_MATCH_LANG ("a::operator b::foo(int)", "a::operator a::foo(char)", NORMAL,
2727 		       language_cplus);
2728   CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int *)", NORMAL,
2729 		       language_cplus);
2730   CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int &)", NORMAL,
2731 		       language_cplus);
2732   CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int, char *)", NORMAL,
2733 		       language_cplus);
2734   CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator bar(int)", NORMAL,
2735 		       language_cplus);
2736 
2737   /* Skip "[abi:cxx11]" tags in the symbol name if the lookup name
2738      doesn't include them.  These are not language-specific in
2739      strncmp_iw_with_mode.  */
2740 
2741   CHECK_MATCH ("foo[abi:a]", "foo", NORMAL);
2742   CHECK_MATCH ("foo[abi:a]()", "foo", NORMAL);
2743   CHECK_MATCH ("foo[abi:a](a)", "foo", NORMAL);
2744   CHECK_MATCH ("foo[abi:a](a&,b*)", "foo", NORMAL);
2745   CHECK_MATCH ("foo[abi:a](a,b)", "foo(a,b)", NORMAL);
2746   CHECK_MATCH ("foo[abi:a](a,b) c", "foo(a,b) c", NORMAL);
2747   CHECK_MATCH ("foo[abi:a](a)", "foo(a)", NORMAL);
2748   CHECK_MATCH ("foo[abi:a](a,b)", "foo(a,b)", NORMAL);
2749   CHECK_MATCH ("foo[abi:a]", "foo[abi:a]", NORMAL);
2750   CHECK_MATCH ("foo[ abi:a]", "foo[abi:a]", NORMAL);
2751   CHECK_MATCH ("foo[\tabi:a]", "foo[abi:a]", NORMAL);
2752   CHECK_MATCH ("foo[ \tabi:a]", "foo[abi:a]", NORMAL);
2753   CHECK_MATCH ("foo[\t abi:a]", "foo[abi:a]", NORMAL);
2754   CHECK_MATCH ("foo[abi :a]", "foo[abi:a]", NORMAL);
2755   CHECK_MATCH ("foo[abi\t:a]", "foo[abi:a]", NORMAL);
2756   CHECK_MATCH ("foo[abi \t:a]", "foo[abi:a]", NORMAL);
2757   CHECK_MATCH ("foo[abi\t :a]", "foo[abi:a]", NORMAL);
2758   CHECK_MATCH ("foo[abi:a]", "foo[ abi:a]", NORMAL);
2759   CHECK_MATCH ("foo[abi:a]", "foo[\tabi:a]", NORMAL);
2760   CHECK_MATCH ("foo[abi:a]", "foo[ \tabi:a]", NORMAL);
2761   CHECK_MATCH ("foo[abi:a]", "foo[\t abi:a]", NORMAL);
2762   CHECK_MATCH ("foo[abi:a]", "foo[abi :a]", NORMAL);
2763   CHECK_MATCH ("foo[abi:a]", "foo[abi\t:a]", NORMAL);
2764   CHECK_MATCH ("foo[abi:a]", "foo[abi \t:a]", NORMAL);
2765   CHECK_MATCH ("foo[abi:a]", "foo[abi\t :a]", NORMAL);
2766   CHECK_MATCH ("foo[abi:a]", "foo[abi:a ]", NORMAL);
2767   CHECK_MATCH ("foo[abi:a]", "foo[abi:a\t]", NORMAL);
2768   CHECK_MATCH ("foo[abi:a]", "foo[abi:a \t]", NORMAL);
2769   CHECK_MATCH ("foo[abi:a]", "foo[abi:a\t ]", NORMAL);
2770   CHECK_MATCH ("foo[abi:a,b]", "foo[abi:a,b]", NORMAL);
2771   CHECK_MATCH ("foo[abi:::]", "foo[abi:::]", NORMAL);
2772   CHECK_MATCH ("foo[abi : : : ]", "foo[abi:::]", NORMAL);
2773   CHECK_MATCH ("foo[abi:::]", "foo[abi : : : ]", NORMAL);
2774   CHECK_MATCH ("foo[ \t abi  \t:\t:   :   \t]",
2775 	       "foo[   abi :                \t    ::]",
2776 	       NORMAL);
2777   CHECK_MATCH ("foo< bar< baz< quxi > > >(int)", "foo<bar<baz<quxi>>>(int)",
2778 	       NORMAL);
2779   CHECK_MATCH ("\tfoo<\tbar<\tbaz\t<\tquxi\t>\t>\t>(int)",
2780 	       "foo<bar<baz<quxi>>>(int)", NORMAL);
2781   CHECK_MATCH (" \tfoo \t< \tbar \t< \tbaz \t< \tquxi \t> \t> \t> \t( \tint \t)",
2782 	       "foo<bar<baz<quxi>>>(int)", NORMAL);
2783   CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
2784 	       "foo < bar < baz < quxi > > > (int)", NORMAL);
2785   CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
2786 	       "\tfoo\t<\tbar\t<\tbaz\t<\tquxi\t>\t>\t>\t(int)", NORMAL);
2787   CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
2788 	       " \tfoo \t< \tbar \t< \tbaz \t< \tquxi \t> \t> \t> \t( \tint \t)", NORMAL);
2789   CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "fo", NORMAL);
2790   CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo", NORMAL);
2791   CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo<bar<baz>>::", NORMAL);
2792   CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo<bar<baz> >::foo", NORMAL);
2793   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c][abi:d])",
2794 	       NORMAL);
2795   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo", NORMAL);
2796   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo(bar)", NORMAL);
2797   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a](bar)", NORMAL);
2798   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo(bar[abi:c])", NORMAL);
2799   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a](bar[abi:c])", NORMAL);
2800   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar)", NORMAL);
2801   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c])",
2802 	       NORMAL);
2803   CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo", NORMAL);
2804   CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo()", NORMAL);
2805   CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>", NORMAL);
2806   CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz)", NORMAL);
2807   CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz[abi:b])",
2808 	      NORMAL);
2809   CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz[abi:A])",
2810 	      NORMAL);
2811   CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:a]>(char*, baz)",
2812 	      NORMAL);
2813   CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:A]>(char*, baz)",
2814 	      NORMAL);
2815   CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:a]>(char*, baz[abi:b])",
2816 	      NORMAL);
2817   CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])",
2818 		 "foo<bar[abi:a]>(char*, baz[abi:B])", NORMAL);
2819 
2820   CHECK_NO_MATCH ("foo", "foo[", NORMAL);
2821   CHECK_NO_MATCH ("foo", "foo[]", NORMAL);
2822   CHECK_NO_MATCH ("foo", "foo[ a]", NORMAL);
2823   CHECK_NO_MATCH ("foo", "foo[a ]", NORMAL);
2824   CHECK_NO_MATCH ("foo", "foo[ a ]", NORMAL);
2825   CHECK_NO_MATCH ("foo", "foo[\ta]", NORMAL);
2826   CHECK_NO_MATCH ("foo", "foo[a \t]", NORMAL);
2827   CHECK_NO_MATCH ("foo", "foo[a\t ]", NORMAL);
2828   CHECK_NO_MATCH ("foo", "foo[ \ta]", NORMAL);
2829   CHECK_NO_MATCH ("foo", "foo[\t a]", NORMAL);
2830   CHECK_NO_MATCH ("foo", "foo[ \ta \t]", NORMAL);
2831   CHECK_NO_MATCH ("foo", "foo[\t a\t ]", NORMAL);
2832   CHECK_NO_MATCH ("foo", "foo[abi]", NORMAL);
2833   CHECK_NO_MATCH ("foo", "foo[ abi]", NORMAL);
2834   CHECK_NO_MATCH ("foo", "foo[abi ]", NORMAL);
2835   CHECK_NO_MATCH ("foo", "foo[\tabi]", NORMAL);
2836   CHECK_NO_MATCH ("foo", "foo[abi\t]", NORMAL);
2837   CHECK_NO_MATCH ("foo", "foo[ \tabi]", NORMAL);
2838   CHECK_NO_MATCH ("foo", "foo[\t abi]", NORMAL);
2839   CHECK_NO_MATCH ("foo", "foo[abi \t]", NORMAL);
2840   CHECK_NO_MATCH ("foo", "foo[abi\t ]", NORMAL);
2841   CHECK_NO_MATCH ("foo", "foo[abi :]", NORMAL);
2842   CHECK_NO_MATCH ("foo", "foo[abi\t:]", NORMAL);
2843   CHECK_NO_MATCH ("foo", "foo[abi \t:]", NORMAL);
2844   CHECK_NO_MATCH ("foo", "foo[abi\t :]", NORMAL);
2845   CHECK_NO_MATCH ("foo", "foo[abi: ]", NORMAL);
2846   CHECK_NO_MATCH ("foo", "foo[abi:\t]", NORMAL);
2847   CHECK_NO_MATCH ("foo", "foo[abi: \t]", NORMAL);
2848   CHECK_NO_MATCH ("foo", "foo[abi:\t ]", NORMAL);
2849   CHECK_NO_MATCH ("foo", "foo[abi: a]", NORMAL);
2850   CHECK_NO_MATCH ("foo", "foo[abi:\ta]", NORMAL);
2851   CHECK_NO_MATCH ("foo", "foo[abi: \ta]", NORMAL);
2852   CHECK_NO_MATCH ("foo", "foo[abi:\t a]", NORMAL);
2853   CHECK_NO_MATCH ("foo", "foo[abi:a ]", NORMAL);
2854   CHECK_NO_MATCH ("foo", "foo[abi:a\t]", NORMAL);
2855   CHECK_NO_MATCH ("foo", "foo[abi:a \t]", NORMAL);
2856   CHECK_NO_MATCH ("foo", "foo[abi:a\t ]", NORMAL);
2857   CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
2858   CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
2859   CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
2860   CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
2861   CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) c", NORMAL);
2862   CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) .", NORMAL);
2863   CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) *", NORMAL);
2864   CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) &", NORMAL);
2865   CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) c", NORMAL);
2866   CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) .", NORMAL);
2867   CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) *", NORMAL);
2868   CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) &", NORMAL);
2869   CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)c", NORMAL);
2870   CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b).", NORMAL);
2871   CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)*", NORMAL);
2872   CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)&", NORMAL);
2873   CHECK_NO_MATCH ("foo[abi:a](a,b) d", "foo(a,b) c", NORMAL);
2874   CHECK_NO_MATCH ("foo[abi:a](a)", "foo()", NORMAL);
2875   CHECK_NO_MATCH ("foo[abi:a](a)", "foo(b)", NORMAL);
2876   CHECK_NO_MATCH ("foo[abi:a](a)", "foo[abi:b](a)", NORMAL);
2877   CHECK_NO_MATCH ("foo[abi:a](a)", "foo[abi:a](b)", NORMAL);
2878   CHECK_NO_MATCH ("foo[abi:]", "foo[abi:a]", NORMAL);
2879   CHECK_NO_MATCH ("foo[abi:", "foo[abi:a]", NORMAL);
2880   CHECK_NO_MATCH ("foo[abi:]", "foo[abi:a", NORMAL);
2881   CHECK_NO_MATCH ("foo[abi:,]", "foo[abi:a]", NORMAL);
2882   CHECK_NO_MATCH ("foo[abi:a,b]", "foo[abi:a]", NORMAL);
2883   CHECK_NO_MATCH ("foo[abi::a]", "foo[abi:a]", NORMAL);
2884   CHECK_NO_MATCH ("foo[abi:,([a]", "foo[abi:a]", NORMAL);
2885 
2886   CHECK_MATCH ("foo <a, b [, c (",  "foo", NORMAL);
2887   CHECK_MATCH ("foo >a, b ], c )",  "foo", NORMAL);
2888   CHECK_MATCH ("@!%&\\*", "@!%&\\*", NORMAL);
2889   CHECK_MATCH ("()", "()", NORMAL);
2890   CHECK_MATCH ("*(*)*", "*(*)*", NORMAL);
2891   CHECK_MATCH ("[]", "[]", NORMAL);
2892   CHECK_MATCH ("<>", "<>", NORMAL);
2893 
2894   /* strncmp_iw_with_mode::MATCH_PARAMS: the "strcmp_iw hack."  */
2895   CHECK_MATCH ("foo2", "foo", NORMAL);
2896   CHECK_NO_MATCH ("foo2", "foo", MATCH_PARAMS);
2897   CHECK_NO_MATCH ("foo2", "foo ", MATCH_PARAMS);
2898   CHECK_NO_MATCH ("foo2", "foo\t", MATCH_PARAMS);
2899   CHECK_NO_MATCH ("foo2", "foo \t", MATCH_PARAMS);
2900   CHECK_NO_MATCH ("foo2", "foo\t ", MATCH_PARAMS);
2901   CHECK_NO_MATCH ("foo2", "foo \t", MATCH_PARAMS);
2902   CHECK_NO_MATCH ("foo2", " foo", MATCH_PARAMS);
2903   CHECK_NO_MATCH ("foo2", "\tfoo", MATCH_PARAMS);
2904   CHECK_NO_MATCH ("foo2", " \tfoo", MATCH_PARAMS);
2905   CHECK_NO_MATCH ("foo2", "\t foo", MATCH_PARAMS);
2906   CHECK_NO_MATCH (" foo2", "foo", MATCH_PARAMS);
2907   CHECK_NO_MATCH ("\tfoo2", "foo", MATCH_PARAMS);
2908   CHECK_NO_MATCH (" \tfoo2", "foo", MATCH_PARAMS);
2909   CHECK_NO_MATCH ("\t foo2", "foo", MATCH_PARAMS);
2910   CHECK_NO_MATCH (" foo2 ", " foo ", MATCH_PARAMS);
2911   CHECK_NO_MATCH ("\tfoo2\t", "\tfoo\t", MATCH_PARAMS);
2912   CHECK_NO_MATCH (" \tfoo2 \t", " \tfoo \t", MATCH_PARAMS);
2913   CHECK_NO_MATCH ("\t foo2\t ", "\t foo\t ", MATCH_PARAMS);
2914   CHECK_NO_MATCH ("foo2 ", "foo", MATCH_PARAMS);
2915   CHECK_NO_MATCH ("foo2\t", "foo", MATCH_PARAMS);
2916   CHECK_NO_MATCH ("foo2 ", "foo", MATCH_PARAMS);
2917   CHECK_NO_MATCH ("foo2 \t", "foo", MATCH_PARAMS);
2918   CHECK_NO_MATCH ("foo2\t ", "foo", MATCH_PARAMS);
2919   CHECK_NO_MATCH ("foo2 (args)", "foo", MATCH_PARAMS);
2920   CHECK_NO_MATCH ("foo2 (args)", "foo", MATCH_PARAMS);
2921   CHECK_NO_MATCH ("foo2\t(args)", "foo", MATCH_PARAMS);
2922   CHECK_NO_MATCH ("foo2 \t(args)", "foo", MATCH_PARAMS);
2923   CHECK_NO_MATCH ("foo2\t (args)", "foo", MATCH_PARAMS);
2924   CHECK_NO_MATCH ("foo2 ( args)", "foo", MATCH_PARAMS);
2925   CHECK_NO_MATCH ("foo2(args )", "foo", MATCH_PARAMS);
2926   CHECK_NO_MATCH ("foo2(args\t)", "foo", MATCH_PARAMS);
2927   CHECK_NO_MATCH ("foo2 (args \t)", "foo", MATCH_PARAMS);
2928   CHECK_NO_MATCH ("foo2 (args\t )", "foo", MATCH_PARAMS);
2929   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c][abi:d])",
2930 	       MATCH_PARAMS);
2931   CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo", MATCH_PARAMS);
2932 
2933   /* strncmp_iw_with_mode also supports case insensitivity.  */
2934   {
2935     CHECK_NO_MATCH ("FoO", "foo", NORMAL);
2936     CHECK_NO_MATCH ("FoO", "foo", MATCH_PARAMS);
2937 
2938     scoped_restore restore_case = make_scoped_restore (&case_sensitivity);
2939     case_sensitivity = case_sensitive_off;
2940 
2941     CHECK_MATCH ("FoO", "foo", NORMAL);
2942     CHECK_MATCH ("FoO", "foo", MATCH_PARAMS);
2943     CHECK_MATCH ("foo", "FoO", NORMAL);
2944     CHECK_MATCH ("foo", "FoO", MATCH_PARAMS);
2945 
2946     CHECK_MATCH ("FoO[AbI:abC]()", "foo", NORMAL);
2947     CHECK_NO_MATCH ("FoO[AbI:abC]()", "foo", MATCH_PARAMS);
2948     CHECK_MATCH ("FoO2[AbI:abC]()", "foo", NORMAL);
2949     CHECK_NO_MATCH ("FoO2[AbI:abC]()", "foo", MATCH_PARAMS);
2950 
2951     CHECK_MATCH ("foo[abi:abc]()", "FoO[AbI:abC]()", NORMAL);
2952     CHECK_MATCH ("foo[abi:abc]()", "FoO[AbI:AbC]()", MATCH_PARAMS);
2953     CHECK_MATCH ("foo[abi:abc](xyz)", "FoO[AbI:abC](XyZ)", NORMAL);
2954     CHECK_MATCH ("foo[abi:abc](xyz)", "FoO[AbI:abC](XyZ)", MATCH_PARAMS);
2955     CHECK_MATCH ("foo[abi:abc][abi:def](xyz)", "FoO[AbI:abC](XyZ)", NORMAL);
2956     CHECK_MATCH ("foo[abi:abc][abi:def](xyz)", "FoO[AbI:abC](XyZ)",
2957 		 MATCH_PARAMS);
2958     CHECK_MATCH ("foo<bar<baz>>(bar<baz>)", "FoO<bAr<BaZ>>(bAr<BaZ>)",
2959 		 NORMAL);
2960     CHECK_MATCH ("foo<bar<baz>>(bar<baz>)", "FoO<bAr<BaZ>>(bAr<BaZ>)",
2961 		 MATCH_PARAMS);
2962   }
2963 }
2964 
2965 #undef MATCH
2966 #undef NO_MATCH
2967 #endif
2968 
2969 /* See utils.h.  */
2970 
2971 int
2972 strncmp_iw (const char *string1, const char *string2, size_t string2_len)
2973 {
2974   return strncmp_iw_with_mode (string1, string2, string2_len,
2975 			       strncmp_iw_mode::NORMAL, language_minimal);
2976 }
2977 
2978 /* See utils.h.  */
2979 
2980 int
2981 strcmp_iw (const char *string1, const char *string2)
2982 {
2983   return strncmp_iw_with_mode (string1, string2, strlen (string2),
2984 			       strncmp_iw_mode::MATCH_PARAMS, language_minimal);
2985 }
2986 
2987 /* This is like strcmp except that it ignores whitespace and treats
2988    '(' as the first non-NULL character in terms of ordering.  Like
2989    strcmp (and unlike strcmp_iw), it returns negative if STRING1 <
2990    STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2
2991    according to that ordering.
2992 
2993    If a list is sorted according to this function and if you want to
2994    find names in the list that match some fixed NAME according to
2995    strcmp_iw(LIST_ELT, NAME), then the place to start looking is right
2996    where this function would put NAME.
2997 
2998    This function must be neutral to the CASE_SENSITIVITY setting as the user
2999    may choose it during later lookup.  Therefore this function always sorts
3000    primarily case-insensitively and secondarily case-sensitively.
3001 
3002    Here are some examples of why using strcmp to sort is a bad idea:
3003 
3004    Whitespace example:
3005 
3006    Say your partial symtab contains: "foo<char *>", "goo".  Then, if
3007    we try to do a search for "foo<char*>", strcmp will locate this
3008    after "foo<char *>" and before "goo".  Then lookup_partial_symbol
3009    will start looking at strings beginning with "goo", and will never
3010    see the correct match of "foo<char *>".
3011 
3012    Parenthesis example:
3013 
3014    In practice, this is less like to be an issue, but I'll give it a
3015    shot.  Let's assume that '$' is a legitimate character to occur in
3016    symbols.  (Which may well even be the case on some systems.)  Then
3017    say that the partial symbol table contains "foo$" and "foo(int)".
3018    strcmp will put them in this order, since '$' < '('.  Now, if the
3019    user searches for "foo", then strcmp will sort "foo" before "foo$".
3020    Then lookup_partial_symbol will notice that strcmp_iw("foo$",
3021    "foo") is false, so it won't proceed to the actual match of
3022    "foo(int)" with "foo".  */
3023 
3024 int
3025 strcmp_iw_ordered (const char *string1, const char *string2)
3026 {
3027   const char *saved_string1 = string1, *saved_string2 = string2;
3028   enum case_sensitivity case_pass = case_sensitive_off;
3029 
3030   for (;;)
3031     {
3032       /* C1 and C2 are valid only if *string1 != '\0' && *string2 != '\0'.
3033 	 Provide stub characters if we are already at the end of one of the
3034 	 strings.  */
3035       char c1 = 'X', c2 = 'X';
3036 
3037       while (*string1 != '\0' && *string2 != '\0')
3038 	{
3039 	  while (ISSPACE (*string1))
3040 	    string1++;
3041 	  while (ISSPACE (*string2))
3042 	    string2++;
3043 
3044 	  switch (case_pass)
3045 	  {
3046 	    case case_sensitive_off:
3047 	      c1 = TOLOWER ((unsigned char) *string1);
3048 	      c2 = TOLOWER ((unsigned char) *string2);
3049 	      break;
3050 	    case case_sensitive_on:
3051 	      c1 = *string1;
3052 	      c2 = *string2;
3053 	      break;
3054 	  }
3055 	  if (c1 != c2)
3056 	    break;
3057 
3058 	  if (*string1 != '\0')
3059 	    {
3060 	      string1++;
3061 	      string2++;
3062 	    }
3063 	}
3064 
3065       switch (*string1)
3066 	{
3067 	  /* Characters are non-equal unless they're both '\0'; we want to
3068 	     make sure we get the comparison right according to our
3069 	     comparison in the cases where one of them is '\0' or '('.  */
3070 	case '\0':
3071 	  if (*string2 == '\0')
3072 	    break;
3073 	  else
3074 	    return -1;
3075 	case '(':
3076 	  if (*string2 == '\0')
3077 	    return 1;
3078 	  else
3079 	    return -1;
3080 	default:
3081 	  if (*string2 == '\0' || *string2 == '(')
3082 	    return 1;
3083 	  else if (c1 > c2)
3084 	    return 1;
3085 	  else if (c1 < c2)
3086 	    return -1;
3087 	  /* PASSTHRU */
3088 	}
3089 
3090       if (case_pass == case_sensitive_on)
3091 	return 0;
3092 
3093       /* Otherwise the strings were equal in case insensitive way, make
3094 	 a more fine grained comparison in a case sensitive way.  */
3095 
3096       case_pass = case_sensitive_on;
3097       string1 = saved_string1;
3098       string2 = saved_string2;
3099     }
3100 }
3101 
3102 
3103 
3104 static void
3105 show_debug_timestamp (struct ui_file *file, int from_tty,
3106 		      struct cmd_list_element *c, const char *value)
3107 {
3108   gdb_printf (file, _("Timestamping debugging messages is %s.\n"),
3109 	      value);
3110 }
3111 
3112 
3113 const char *
3114 paddress (struct gdbarch *gdbarch, CORE_ADDR addr)
3115 {
3116   /* Truncate address to the size of a target address, avoiding shifts
3117      larger or equal than the width of a CORE_ADDR.  The local
3118      variable ADDR_BIT stops the compiler reporting a shift overflow
3119      when it won't occur.  */
3120   /* NOTE: This assumes that the significant address information is
3121      kept in the least significant bits of ADDR - the upper bits were
3122      either zero or sign extended.  Should gdbarch_address_to_pointer or
3123      some ADDRESS_TO_PRINTABLE() be used to do the conversion?  */
3124 
3125   int addr_bit = gdbarch_addr_bit (gdbarch);
3126 
3127   if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
3128     addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
3129   return hex_string (addr);
3130 }
3131 
3132 /* This function is described in "defs.h".  */
3133 
3134 const char *
3135 print_core_address (struct gdbarch *gdbarch, CORE_ADDR address)
3136 {
3137   int addr_bit = gdbarch_addr_bit (gdbarch);
3138 
3139   if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
3140     address &= ((CORE_ADDR) 1 << addr_bit) - 1;
3141 
3142   /* FIXME: cagney/2002-05-03: Need local_address_string() function
3143      that returns the language localized string formatted to a width
3144      based on gdbarch_addr_bit.  */
3145   if (addr_bit <= 32)
3146     return hex_string_custom (address, 8);
3147   else
3148     return hex_string_custom (address, 16);
3149 }
3150 
3151 /* Convert a string back into a CORE_ADDR.  */
3152 CORE_ADDR
3153 string_to_core_addr (const char *my_string)
3154 {
3155   CORE_ADDR addr = 0;
3156 
3157   if (my_string[0] == '0' && TOLOWER (my_string[1]) == 'x')
3158     {
3159       /* Assume that it is in hex.  */
3160       int i;
3161 
3162       for (i = 2; my_string[i] != '\0'; i++)
3163 	{
3164 	  if (ISDIGIT (my_string[i]))
3165 	    addr = (my_string[i] - '0') + (addr * 16);
3166 	  else if (ISXDIGIT (my_string[i]))
3167 	    addr = (TOLOWER (my_string[i]) - 'a' + 0xa) + (addr * 16);
3168 	  else
3169 	    error (_("invalid hex \"%s\""), my_string);
3170 	}
3171     }
3172   else
3173     {
3174       /* Assume that it is in decimal.  */
3175       int i;
3176 
3177       for (i = 0; my_string[i] != '\0'; i++)
3178 	{
3179 	  if (ISDIGIT (my_string[i]))
3180 	    addr = (my_string[i] - '0') + (addr * 10);
3181 	  else
3182 	    error (_("invalid decimal \"%s\""), my_string);
3183 	}
3184     }
3185 
3186   return addr;
3187 }
3188 
3189 #if GDB_SELF_TEST
3190 
3191 static void
3192 gdb_realpath_check_trailer (const char *input, const char *trailer)
3193 {
3194   gdb::unique_xmalloc_ptr<char> result = gdb_realpath (input);
3195 
3196   size_t len = strlen (result.get ());
3197   size_t trail_len = strlen (trailer);
3198 
3199   SELF_CHECK (len >= trail_len
3200 	      && strcmp (result.get () + len - trail_len, trailer) == 0);
3201 }
3202 
3203 static void
3204 gdb_realpath_tests ()
3205 {
3206   /* A file which contains a directory prefix.  */
3207   gdb_realpath_check_trailer ("./xfullpath.exp", "/xfullpath.exp");
3208   /* A file which contains a directory prefix.  */
3209   gdb_realpath_check_trailer ("../../defs.h", "/defs.h");
3210   /* A one-character filename.  */
3211   gdb_realpath_check_trailer ("./a", "/a");
3212   /* A file in the root directory.  */
3213   gdb_realpath_check_trailer ("/root_file_which_should_exist",
3214 			      "/root_file_which_should_exist");
3215   /* A file which does not have a directory prefix.  */
3216   gdb_realpath_check_trailer ("xfullpath.exp", "xfullpath.exp");
3217   /* A one-char filename without any directory prefix.  */
3218   gdb_realpath_check_trailer ("a", "a");
3219   /* An empty filename.  */
3220   gdb_realpath_check_trailer ("", "");
3221 }
3222 
3223 /* Test the gdb_argv::as_array_view method.  */
3224 
3225 static void
3226 gdb_argv_as_array_view_test ()
3227 {
3228   {
3229     gdb_argv argv;
3230 
3231     gdb::array_view<char *> view = argv.as_array_view ();
3232 
3233     SELF_CHECK (view.data () == nullptr);
3234     SELF_CHECK (view.size () == 0);
3235   }
3236   {
3237     gdb_argv argv ("une bonne 50");
3238 
3239     gdb::array_view<char *> view = argv.as_array_view ();
3240 
3241     SELF_CHECK (view.size () == 3);
3242     SELF_CHECK (strcmp (view[0], "une") == 0);
3243     SELF_CHECK (strcmp (view[1], "bonne") == 0);
3244     SELF_CHECK (strcmp (view[2], "50") == 0);
3245   }
3246 }
3247 
3248 #endif /* GDB_SELF_TEST */
3249 
3250 /* Simple, portable version of dirname that does not modify its
3251    argument.  */
3252 
3253 std::string
3254 ldirname (const char *filename)
3255 {
3256   std::string dirname;
3257   const char *base = lbasename (filename);
3258 
3259   while (base > filename && IS_DIR_SEPARATOR (base[-1]))
3260     --base;
3261 
3262   if (base == filename)
3263     return dirname;
3264 
3265   dirname = std::string (filename, base - filename);
3266 
3267   /* On DOS based file systems, convert "d:foo" to "d:.", so that we
3268      create "d:./bar" later instead of the (different) "d:/bar".  */
3269   if (base - filename == 2 && IS_ABSOLUTE_PATH (base)
3270       && !IS_DIR_SEPARATOR (filename[0]))
3271     dirname[base++ - filename] = '.';
3272 
3273   return dirname;
3274 }
3275 
3276 /* Return ARGS parsed as a valid pid, or throw an error.  */
3277 
3278 int
3279 parse_pid_to_attach (const char *args)
3280 {
3281   unsigned long pid;
3282   char *dummy;
3283 
3284   if (!args)
3285     error_no_arg (_("process-id to attach"));
3286 
3287   dummy = (char *) args;
3288   pid = strtoul (args, &dummy, 0);
3289   /* Some targets don't set errno on errors, grrr!  */
3290   if ((pid == 0 && dummy == args) || dummy != &args[strlen (args)])
3291     error (_("Illegal process-id: %s."), args);
3292 
3293   return pid;
3294 }
3295 
3296 /* Substitute all occurrences of string FROM by string TO in *STRINGP.  *STRINGP
3297    must come from xrealloc-compatible allocator and it may be updated.  FROM
3298    needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be
3299    located at the start or end of *STRINGP.  */
3300 
3301 void
3302 substitute_path_component (char **stringp, const char *from, const char *to)
3303 {
3304   char *string = *stringp, *s;
3305   const size_t from_len = strlen (from);
3306   const size_t to_len = strlen (to);
3307 
3308   for (s = string;;)
3309     {
3310       s = strstr (s, from);
3311       if (s == NULL)
3312 	break;
3313 
3314       if ((s == string || IS_DIR_SEPARATOR (s[-1])
3315 	   || s[-1] == DIRNAME_SEPARATOR)
3316 	  && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len])
3317 	      || s[from_len] == DIRNAME_SEPARATOR))
3318 	{
3319 	  char *string_new;
3320 
3321 	  string_new
3322 	    = (char *) xrealloc (string, (strlen (string) + to_len + 1));
3323 
3324 	  /* Relocate the current S pointer.  */
3325 	  s = s - string + string_new;
3326 	  string = string_new;
3327 
3328 	  /* Replace from by to.  */
3329 	  memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1);
3330 	  memcpy (s, to, to_len);
3331 
3332 	  s += to_len;
3333 	}
3334       else
3335 	s++;
3336     }
3337 
3338   *stringp = string;
3339 }
3340 
3341 #ifdef HAVE_WAITPID
3342 
3343 #ifdef SIGALRM
3344 
3345 /* SIGALRM handler for waitpid_with_timeout.  */
3346 
3347 static void
3348 sigalrm_handler (int signo)
3349 {
3350   /* Nothing to do.  */
3351 }
3352 
3353 #endif
3354 
3355 /* Wrapper to wait for child PID to die with TIMEOUT.
3356    TIMEOUT is the time to stop waiting in seconds.
3357    If TIMEOUT is zero, pass WNOHANG to waitpid.
3358    Returns PID if it was successfully waited for, otherwise -1.
3359 
3360    Timeouts are currently implemented with alarm and SIGALRM.
3361    If the host does not support them, this waits "forever".
3362    It would be odd though for a host to have waitpid and not SIGALRM.  */
3363 
3364 pid_t
3365 wait_to_die_with_timeout (pid_t pid, int *status, int timeout)
3366 {
3367   pid_t waitpid_result;
3368 
3369   gdb_assert (pid > 0);
3370   gdb_assert (timeout >= 0);
3371 
3372   if (timeout > 0)
3373     {
3374 #ifdef SIGALRM
3375 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
3376       struct sigaction sa, old_sa;
3377 
3378       sa.sa_handler = sigalrm_handler;
3379       sigemptyset (&sa.sa_mask);
3380       sa.sa_flags = 0;
3381       sigaction (SIGALRM, &sa, &old_sa);
3382 #else
3383       sighandler_t ofunc;
3384 
3385       ofunc = signal (SIGALRM, sigalrm_handler);
3386 #endif
3387 
3388       alarm (timeout);
3389 #endif
3390 
3391       waitpid_result = waitpid (pid, status, 0);
3392 
3393 #ifdef SIGALRM
3394       alarm (0);
3395 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
3396       sigaction (SIGALRM, &old_sa, NULL);
3397 #else
3398       signal (SIGALRM, ofunc);
3399 #endif
3400 #endif
3401     }
3402   else
3403     waitpid_result = waitpid (pid, status, WNOHANG);
3404 
3405   if (waitpid_result == pid)
3406     return pid;
3407   else
3408     return -1;
3409 }
3410 
3411 #endif /* HAVE_WAITPID */
3412 
3413 /* Provide fnmatch compatible function for FNM_FILE_NAME matching of host files.
3414    Both FNM_FILE_NAME and FNM_NOESCAPE must be set in FLAGS.
3415 
3416    It handles correctly HAVE_DOS_BASED_FILE_SYSTEM and
3417    HAVE_CASE_INSENSITIVE_FILE_SYSTEM.  */
3418 
3419 int
3420 gdb_filename_fnmatch (const char *pattern, const char *string, int flags)
3421 {
3422   gdb_assert ((flags & FNM_FILE_NAME) != 0);
3423 
3424   /* It is unclear how '\' escaping vs. directory separator should coexist.  */
3425   gdb_assert ((flags & FNM_NOESCAPE) != 0);
3426 
3427 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3428   {
3429     char *pattern_slash, *string_slash;
3430 
3431     /* Replace '\' by '/' in both strings.  */
3432 
3433     pattern_slash = (char *) alloca (strlen (pattern) + 1);
3434     strcpy (pattern_slash, pattern);
3435     pattern = pattern_slash;
3436     for (; *pattern_slash != 0; pattern_slash++)
3437       if (IS_DIR_SEPARATOR (*pattern_slash))
3438 	*pattern_slash = '/';
3439 
3440     string_slash = (char *) alloca (strlen (string) + 1);
3441     strcpy (string_slash, string);
3442     string = string_slash;
3443     for (; *string_slash != 0; string_slash++)
3444       if (IS_DIR_SEPARATOR (*string_slash))
3445 	*string_slash = '/';
3446   }
3447 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
3448 
3449 #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
3450   flags |= FNM_CASEFOLD;
3451 #endif /* HAVE_CASE_INSENSITIVE_FILE_SYSTEM */
3452 
3453   return fnmatch (pattern, string, flags);
3454 }
3455 
3456 /* Return the number of path elements in PATH.
3457    / = 1
3458    /foo = 2
3459    /foo/ = 2
3460    foo/bar = 2
3461    foo/ = 1  */
3462 
3463 int
3464 count_path_elements (const char *path)
3465 {
3466   int count = 0;
3467   const char *p = path;
3468 
3469   if (HAS_DRIVE_SPEC (p))
3470     {
3471       p = STRIP_DRIVE_SPEC (p);
3472       ++count;
3473     }
3474 
3475   while (*p != '\0')
3476     {
3477       if (IS_DIR_SEPARATOR (*p))
3478 	++count;
3479       ++p;
3480     }
3481 
3482   /* Backup one if last character is /, unless it's the only one.  */
3483   if (p > path + 1 && IS_DIR_SEPARATOR (p[-1]))
3484     --count;
3485 
3486   /* Add one for the file name, if present.  */
3487   if (p > path && !IS_DIR_SEPARATOR (p[-1]))
3488     ++count;
3489 
3490   return count;
3491 }
3492 
3493 /* Remove N leading path elements from PATH.
3494    N must be non-negative.
3495    If PATH has more than N path elements then return NULL.
3496    If PATH has exactly N path elements then return "".
3497    See count_path_elements for a description of how we do the counting.  */
3498 
3499 const char *
3500 strip_leading_path_elements (const char *path, int n)
3501 {
3502   int i = 0;
3503   const char *p = path;
3504 
3505   gdb_assert (n >= 0);
3506 
3507   if (n == 0)
3508     return p;
3509 
3510   if (HAS_DRIVE_SPEC (p))
3511     {
3512       p = STRIP_DRIVE_SPEC (p);
3513       ++i;
3514     }
3515 
3516   while (i < n)
3517     {
3518       while (*p != '\0' && !IS_DIR_SEPARATOR (*p))
3519 	++p;
3520       if (*p == '\0')
3521 	{
3522 	  if (i + 1 == n)
3523 	    return "";
3524 	  return NULL;
3525 	}
3526       ++p;
3527       ++i;
3528     }
3529 
3530   return p;
3531 }
3532 
3533 /* See utils.h.  */
3534 
3535 void
3536 copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
3537 	      const gdb_byte *source, ULONGEST source_offset,
3538 	      ULONGEST nbits, int bits_big_endian)
3539 {
3540   unsigned int buf, avail;
3541 
3542   if (nbits == 0)
3543     return;
3544 
3545   if (bits_big_endian)
3546     {
3547       /* Start from the end, then work backwards.  */
3548       dest_offset += nbits - 1;
3549       dest += dest_offset / 8;
3550       dest_offset = 7 - dest_offset % 8;
3551       source_offset += nbits - 1;
3552       source += source_offset / 8;
3553       source_offset = 7 - source_offset % 8;
3554     }
3555   else
3556     {
3557       dest += dest_offset / 8;
3558       dest_offset %= 8;
3559       source += source_offset / 8;
3560       source_offset %= 8;
3561     }
3562 
3563   /* Fill BUF with DEST_OFFSET bits from the destination and 8 -
3564      SOURCE_OFFSET bits from the source.  */
3565   buf = *(bits_big_endian ? source-- : source++) >> source_offset;
3566   buf <<= dest_offset;
3567   buf |= *dest & ((1 << dest_offset) - 1);
3568 
3569   /* NBITS: bits yet to be written; AVAIL: BUF's fill level.  */
3570   nbits += dest_offset;
3571   avail = dest_offset + 8 - source_offset;
3572 
3573   /* Flush 8 bits from BUF, if appropriate.  */
3574   if (nbits >= 8 && avail >= 8)
3575     {
3576       *(bits_big_endian ? dest-- : dest++) = buf;
3577       buf >>= 8;
3578       avail -= 8;
3579       nbits -= 8;
3580     }
3581 
3582   /* Copy the middle part.  */
3583   if (nbits >= 8)
3584     {
3585       size_t len = nbits / 8;
3586 
3587       /* Use a faster method for byte-aligned copies.  */
3588       if (avail == 0)
3589 	{
3590 	  if (bits_big_endian)
3591 	    {
3592 	      dest -= len;
3593 	      source -= len;
3594 	      memcpy (dest + 1, source + 1, len);
3595 	    }
3596 	  else
3597 	    {
3598 	      memcpy (dest, source, len);
3599 	      dest += len;
3600 	      source += len;
3601 	    }
3602 	}
3603       else
3604 	{
3605 	  while (len--)
3606 	    {
3607 	      buf |= *(bits_big_endian ? source-- : source++) << avail;
3608 	      *(bits_big_endian ? dest-- : dest++) = buf;
3609 	      buf >>= 8;
3610 	    }
3611 	}
3612       nbits %= 8;
3613     }
3614 
3615   /* Write the last byte.  */
3616   if (nbits)
3617     {
3618       if (avail < nbits)
3619 	buf |= *source << avail;
3620 
3621       buf &= (1 << nbits) - 1;
3622       *dest = (*dest & (~0U << nbits)) | buf;
3623     }
3624 }
3625 
3626 void _initialize_utils ();
3627 void
3628 _initialize_utils ()
3629 {
3630   add_setshow_uinteger_cmd ("width", class_support, &chars_per_line, _("\
3631 Set number of characters where GDB should wrap lines of its output."), _("\
3632 Show number of characters where GDB should wrap lines of its output."), _("\
3633 This affects where GDB wraps its output to fit the screen width.\n\
3634 Setting this to \"unlimited\" or zero prevents GDB from wrapping its output."),
3635 			    set_width_command,
3636 			    show_chars_per_line,
3637 			    &setlist, &showlist);
3638 
3639   add_setshow_uinteger_cmd ("height", class_support, &lines_per_page, _("\
3640 Set number of lines in a page for GDB output pagination."), _("\
3641 Show number of lines in a page for GDB output pagination."), _("\
3642 This affects the number of lines after which GDB will pause\n\
3643 its output and ask you whether to continue.\n\
3644 Setting this to \"unlimited\" or zero causes GDB never pause during output."),
3645 			    set_height_command,
3646 			    show_lines_per_page,
3647 			    &setlist, &showlist);
3648 
3649   add_setshow_boolean_cmd ("pagination", class_support,
3650 			   &pagination_enabled, _("\
3651 Set state of GDB output pagination."), _("\
3652 Show state of GDB output pagination."), _("\
3653 When pagination is ON, GDB pauses at end of each screenful of\n\
3654 its output and asks you whether to continue.\n\
3655 Turning pagination off is an alternative to \"set height unlimited\"."),
3656 			   NULL,
3657 			   show_pagination_enabled,
3658 			   &setlist, &showlist);
3659 
3660   add_setshow_boolean_cmd ("sevenbit-strings", class_support,
3661 			   &sevenbit_strings, _("\
3662 Set printing of 8-bit characters in strings as \\nnn."), _("\
3663 Show printing of 8-bit characters in strings as \\nnn."), NULL,
3664 			   NULL,
3665 			   show_sevenbit_strings,
3666 			   &setprintlist, &showprintlist);
3667 
3668   add_setshow_boolean_cmd ("timestamp", class_maintenance,
3669 			    &debug_timestamp, _("\
3670 Set timestamping of debugging messages."), _("\
3671 Show timestamping of debugging messages."), _("\
3672 When set, debugging messages will be marked with seconds and microseconds."),
3673 			   NULL,
3674 			   show_debug_timestamp,
3675 			   &setdebuglist, &showdebuglist);
3676 
3677   add_internal_problem_command (&internal_error_problem);
3678   add_internal_problem_command (&internal_warning_problem);
3679   add_internal_problem_command (&demangler_warning_problem);
3680 
3681 #if GDB_SELF_TEST
3682   selftests::register_test ("gdb_realpath", gdb_realpath_tests);
3683   selftests::register_test ("gdb_argv_array_view", gdb_argv_as_array_view_test);
3684   selftests::register_test ("strncmp_iw_with_mode",
3685 			    strncmp_iw_with_mode_tests);
3686   selftests::register_test ("pager", test_pager);
3687 #endif
3688 }
3689