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