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