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