| 1 | /* General utility routines for GDB, the GNU debugger. |
| 2 | Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #if !defined(__GO32__) |
| 22 | #include <sys/ioctl.h> |
| 23 | #include <sys/param.h> |
| 24 | #include <pwd.h> |
| 25 | #endif |
| 26 | #include <varargs.h> |
| 27 | #include <ctype.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "signals.h" |
| 31 | #include "gdbcmd.h" |
| 32 | #include "terminal.h" |
| 33 | #include "bfd.h" |
| 34 | #include "target.h" |
| 35 | #include "demangle.h" |
| 36 | #include "expression.h" |
| 37 | #include "language.h" |
| 38 | |
| 39 | /* Prototypes for local functions */ |
| 40 | |
| 41 | #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK) |
| 42 | #else |
| 43 | |
| 44 | static void |
| 45 | malloc_botch PARAMS ((void)); |
| 46 | |
| 47 | #endif /* NO_MMALLOC, etc */ |
| 48 | |
| 49 | static void |
| 50 | fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */ |
| 51 | |
| 52 | static void |
| 53 | prompt_for_continue PARAMS ((void)); |
| 54 | |
| 55 | static void |
| 56 | set_width_command PARAMS ((char *, int, struct cmd_list_element *)); |
| 57 | |
| 58 | /* If this definition isn't overridden by the header files, assume |
| 59 | that isatty and fileno exist on this system. */ |
| 60 | #ifndef ISATTY |
| 61 | #define ISATTY(FP) (isatty (fileno (FP))) |
| 62 | #endif |
| 63 | |
| 64 | /* Chain of cleanup actions established with make_cleanup, |
| 65 | to be executed if an error happens. */ |
| 66 | |
| 67 | static struct cleanup *cleanup_chain; |
| 68 | |
| 69 | /* Nonzero means a quit has been requested. */ |
| 70 | |
| 71 | int quit_flag; |
| 72 | |
| 73 | /* Nonzero means quit immediately if Control-C is typed now, |
| 74 | rather than waiting until QUIT is executed. */ |
| 75 | |
| 76 | int immediate_quit; |
| 77 | |
| 78 | /* Nonzero means that encoded C++ names should be printed out in their |
| 79 | C++ form rather than raw. */ |
| 80 | |
| 81 | int demangle = 1; |
| 82 | |
| 83 | /* Nonzero means that encoded C++ names should be printed out in their |
| 84 | C++ form even in assembler language displays. If this is set, but |
| 85 | DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ |
| 86 | |
| 87 | int asm_demangle = 0; |
| 88 | |
| 89 | /* Nonzero means that strings with character values >0x7F should be printed |
| 90 | as octal escapes. Zero means just print the value (e.g. it's an |
| 91 | international character, and the terminal or window can cope.) */ |
| 92 | |
| 93 | int sevenbit_strings = 0; |
| 94 | |
| 95 | /* String to be printed before error messages, if any. */ |
| 96 | |
| 97 | char *error_pre_print; |
| 98 | char *warning_pre_print = "\nwarning: "; |
| 99 | \f |
| 100 | /* Add a new cleanup to the cleanup_chain, |
| 101 | and return the previous chain pointer |
| 102 | to be passed later to do_cleanups or discard_cleanups. |
| 103 | Args are FUNCTION to clean up with, and ARG to pass to it. */ |
| 104 | |
| 105 | struct cleanup * |
| 106 | make_cleanup (function, arg) |
| 107 | void (*function) PARAMS ((PTR)); |
| 108 | PTR arg; |
| 109 | { |
| 110 | register struct cleanup *new |
| 111 | = (struct cleanup *) xmalloc (sizeof (struct cleanup)); |
| 112 | register struct cleanup *old_chain = cleanup_chain; |
| 113 | |
| 114 | new->next = cleanup_chain; |
| 115 | new->function = function; |
| 116 | new->arg = arg; |
| 117 | cleanup_chain = new; |
| 118 | |
| 119 | return old_chain; |
| 120 | } |
| 121 | |
| 122 | /* Discard cleanups and do the actions they describe |
| 123 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ |
| 124 | |
| 125 | void |
| 126 | do_cleanups (old_chain) |
| 127 | register struct cleanup *old_chain; |
| 128 | { |
| 129 | register struct cleanup *ptr; |
| 130 | while ((ptr = cleanup_chain) != old_chain) |
| 131 | { |
| 132 | cleanup_chain = ptr->next; /* Do this first incase recursion */ |
| 133 | (*ptr->function) (ptr->arg); |
| 134 | free (ptr); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* Discard cleanups, not doing the actions they describe, |
| 139 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ |
| 140 | |
| 141 | void |
| 142 | discard_cleanups (old_chain) |
| 143 | register struct cleanup *old_chain; |
| 144 | { |
| 145 | register struct cleanup *ptr; |
| 146 | while ((ptr = cleanup_chain) != old_chain) |
| 147 | { |
| 148 | cleanup_chain = ptr->next; |
| 149 | free ((PTR)ptr); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* Set the cleanup_chain to 0, and return the old cleanup chain. */ |
| 154 | struct cleanup * |
| 155 | save_cleanups () |
| 156 | { |
| 157 | struct cleanup *old_chain = cleanup_chain; |
| 158 | |
| 159 | cleanup_chain = 0; |
| 160 | return old_chain; |
| 161 | } |
| 162 | |
| 163 | /* Restore the cleanup chain from a previously saved chain. */ |
| 164 | void |
| 165 | restore_cleanups (chain) |
| 166 | struct cleanup *chain; |
| 167 | { |
| 168 | cleanup_chain = chain; |
| 169 | } |
| 170 | |
| 171 | /* This function is useful for cleanups. |
| 172 | Do |
| 173 | |
| 174 | foo = xmalloc (...); |
| 175 | old_chain = make_cleanup (free_current_contents, &foo); |
| 176 | |
| 177 | to arrange to free the object thus allocated. */ |
| 178 | |
| 179 | void |
| 180 | free_current_contents (location) |
| 181 | char **location; |
| 182 | { |
| 183 | free (*location); |
| 184 | } |
| 185 | |
| 186 | /* Provide a known function that does nothing, to use as a base for |
| 187 | for a possibly long chain of cleanups. This is useful where we |
| 188 | use the cleanup chain for handling normal cleanups as well as dealing |
| 189 | with cleanups that need to be done as a result of a call to error(). |
| 190 | In such cases, we may not be certain where the first cleanup is, unless |
| 191 | we have a do-nothing one to always use as the base. */ |
| 192 | |
| 193 | /* ARGSUSED */ |
| 194 | void |
| 195 | null_cleanup (arg) |
| 196 | char **arg; |
| 197 | { |
| 198 | } |
| 199 | |
| 200 | \f |
| 201 | /* Provide a hook for modules wishing to print their own warning messages |
| 202 | to set up the terminal state in a compatible way, without them having |
| 203 | to import all the target_<...> macros. */ |
| 204 | |
| 205 | void |
| 206 | warning_setup () |
| 207 | { |
| 208 | target_terminal_ours (); |
| 209 | wrap_here(""); /* Force out any buffered output */ |
| 210 | fflush (stdout); |
| 211 | } |
| 212 | |
| 213 | /* Print a warning message. |
| 214 | The first argument STRING is the warning message, used as a fprintf string, |
| 215 | and the remaining args are passed as arguments to it. |
| 216 | The primary difference between warnings and errors is that a warning |
| 217 | does not force the return to command level. */ |
| 218 | |
| 219 | /* VARARGS */ |
| 220 | void |
| 221 | warning (va_alist) |
| 222 | va_dcl |
| 223 | { |
| 224 | va_list args; |
| 225 | char *string; |
| 226 | |
| 227 | va_start (args); |
| 228 | target_terminal_ours (); |
| 229 | wrap_here(""); /* Force out any buffered output */ |
| 230 | fflush (stdout); |
| 231 | if (warning_pre_print) |
| 232 | fprintf (stderr, warning_pre_print); |
| 233 | string = va_arg (args, char *); |
| 234 | vfprintf (stderr, string, args); |
| 235 | fprintf (stderr, "\n"); |
| 236 | va_end (args); |
| 237 | } |
| 238 | |
| 239 | /* Print an error message and return to command level. |
| 240 | The first argument STRING is the error message, used as a fprintf string, |
| 241 | and the remaining args are passed as arguments to it. */ |
| 242 | |
| 243 | /* VARARGS */ |
| 244 | NORETURN void |
| 245 | error (va_alist) |
| 246 | va_dcl |
| 247 | { |
| 248 | va_list args; |
| 249 | char *string; |
| 250 | |
| 251 | va_start (args); |
| 252 | target_terminal_ours (); |
| 253 | wrap_here(""); /* Force out any buffered output */ |
| 254 | fflush (stdout); |
| 255 | if (error_pre_print) |
| 256 | fprintf_filtered (stderr, error_pre_print); |
| 257 | string = va_arg (args, char *); |
| 258 | vfprintf_filtered (stderr, string, args); |
| 259 | fprintf_filtered (stderr, "\n"); |
| 260 | va_end (args); |
| 261 | return_to_top_level (); |
| 262 | } |
| 263 | |
| 264 | /* Print an error message and exit reporting failure. |
| 265 | This is for a error that we cannot continue from. |
| 266 | The arguments are printed a la printf. |
| 267 | |
| 268 | This function cannot be declared volatile (NORETURN) in an |
| 269 | ANSI environment because exit() is not declared volatile. */ |
| 270 | |
| 271 | /* VARARGS */ |
| 272 | NORETURN void |
| 273 | fatal (va_alist) |
| 274 | va_dcl |
| 275 | { |
| 276 | va_list args; |
| 277 | char *string; |
| 278 | |
| 279 | va_start (args); |
| 280 | string = va_arg (args, char *); |
| 281 | fprintf (stderr, "\ngdb: "); |
| 282 | vfprintf (stderr, string, args); |
| 283 | fprintf (stderr, "\n"); |
| 284 | va_end (args); |
| 285 | exit (1); |
| 286 | } |
| 287 | |
| 288 | /* Print an error message and exit, dumping core. |
| 289 | The arguments are printed a la printf (). */ |
| 290 | |
| 291 | /* VARARGS */ |
| 292 | static void |
| 293 | fatal_dump_core (va_alist) |
| 294 | va_dcl |
| 295 | { |
| 296 | va_list args; |
| 297 | char *string; |
| 298 | |
| 299 | va_start (args); |
| 300 | string = va_arg (args, char *); |
| 301 | /* "internal error" is always correct, since GDB should never dump |
| 302 | core, no matter what the input. */ |
| 303 | fprintf (stderr, "\ngdb internal error: "); |
| 304 | vfprintf (stderr, string, args); |
| 305 | fprintf (stderr, "\n"); |
| 306 | va_end (args); |
| 307 | |
| 308 | signal (SIGQUIT, SIG_DFL); |
| 309 | kill (getpid (), SIGQUIT); |
| 310 | /* We should never get here, but just in case... */ |
| 311 | exit (1); |
| 312 | } |
| 313 | |
| 314 | /* The strerror() function can return NULL for errno values that are |
| 315 | out of range. Provide a "safe" version that always returns a |
| 316 | printable string. */ |
| 317 | |
| 318 | char * |
| 319 | safe_strerror (errnum) |
| 320 | int errnum; |
| 321 | { |
| 322 | char *msg; |
| 323 | static char buf[32]; |
| 324 | |
| 325 | if ((msg = strerror (errnum)) == NULL) |
| 326 | { |
| 327 | sprintf (buf, "(undocumented errno %d)", errnum); |
| 328 | msg = buf; |
| 329 | } |
| 330 | return (msg); |
| 331 | } |
| 332 | |
| 333 | /* The strsignal() function can return NULL for signal values that are |
| 334 | out of range. Provide a "safe" version that always returns a |
| 335 | printable string. */ |
| 336 | |
| 337 | char * |
| 338 | safe_strsignal (signo) |
| 339 | int signo; |
| 340 | { |
| 341 | char *msg; |
| 342 | static char buf[32]; |
| 343 | |
| 344 | if ((msg = strsignal (signo)) == NULL) |
| 345 | { |
| 346 | sprintf (buf, "(undocumented signal %d)", signo); |
| 347 | msg = buf; |
| 348 | } |
| 349 | return (msg); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* Print the system error message for errno, and also mention STRING |
| 354 | as the file name for which the error was encountered. |
| 355 | Then return to command level. */ |
| 356 | |
| 357 | void |
| 358 | perror_with_name (string) |
| 359 | char *string; |
| 360 | { |
| 361 | char *err; |
| 362 | char *combined; |
| 363 | |
| 364 | err = safe_strerror (errno); |
| 365 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); |
| 366 | strcpy (combined, string); |
| 367 | strcat (combined, ": "); |
| 368 | strcat (combined, err); |
| 369 | |
| 370 | /* I understand setting these is a matter of taste. Still, some people |
| 371 | may clear errno but not know about bfd_error. Doing this here is not |
| 372 | unreasonable. */ |
| 373 | bfd_error = no_error; |
| 374 | errno = 0; |
| 375 | |
| 376 | error ("%s.", combined); |
| 377 | } |
| 378 | |
| 379 | /* Print the system error message for ERRCODE, and also mention STRING |
| 380 | as the file name for which the error was encountered. */ |
| 381 | |
| 382 | void |
| 383 | print_sys_errmsg (string, errcode) |
| 384 | char *string; |
| 385 | int errcode; |
| 386 | { |
| 387 | char *err; |
| 388 | char *combined; |
| 389 | |
| 390 | err = safe_strerror (errcode); |
| 391 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); |
| 392 | strcpy (combined, string); |
| 393 | strcat (combined, ": "); |
| 394 | strcat (combined, err); |
| 395 | |
| 396 | fprintf (stderr, "%s.\n", combined); |
| 397 | } |
| 398 | |
| 399 | /* Control C eventually causes this to be called, at a convenient time. */ |
| 400 | |
| 401 | void |
| 402 | quit () |
| 403 | { |
| 404 | target_terminal_ours (); |
| 405 | wrap_here ((char *)0); /* Force out any pending output */ |
| 406 | #if !defined(__GO32__) |
| 407 | #ifdef HAVE_TERMIO |
| 408 | ioctl (fileno (stdout), TCFLSH, 1); |
| 409 | #else /* not HAVE_TERMIO */ |
| 410 | ioctl (fileno (stdout), TIOCFLUSH, 0); |
| 411 | #endif /* not HAVE_TERMIO */ |
| 412 | #ifdef TIOCGPGRP |
| 413 | error ("Quit"); |
| 414 | #else |
| 415 | error ("Quit (expect signal %d when inferior is resumed)", SIGINT); |
| 416 | #endif /* TIOCGPGRP */ |
| 417 | #else |
| 418 | error ("Quit"); |
| 419 | #endif |
| 420 | } |
| 421 | |
| 422 | |
| 423 | #ifdef __GO32__ |
| 424 | |
| 425 | /* In the absence of signals, poll keyboard for a quit. |
| 426 | Called from #define QUIT pollquit() in xm-go32.h. */ |
| 427 | |
| 428 | void |
| 429 | pollquit() |
| 430 | { |
| 431 | if (kbhit ()) |
| 432 | { |
| 433 | int k = getkey (); |
| 434 | if (k == 1) |
| 435 | quit_flag = 1; |
| 436 | else if (k == 2) |
| 437 | immediate_quit = 1; |
| 438 | quit (); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | #endif |
| 443 | |
| 444 | /* Control C comes here */ |
| 445 | |
| 446 | void |
| 447 | request_quit (signo) |
| 448 | int signo; |
| 449 | { |
| 450 | quit_flag = 1; |
| 451 | |
| 452 | #ifdef USG |
| 453 | /* Restore the signal handler. */ |
| 454 | signal (signo, request_quit); |
| 455 | #endif |
| 456 | |
| 457 | if (immediate_quit) |
| 458 | quit (); |
| 459 | } |
| 460 | |
| 461 | \f |
| 462 | /* Memory management stuff (malloc friends). */ |
| 463 | |
| 464 | #if defined (NO_MMALLOC) |
| 465 | |
| 466 | PTR |
| 467 | mmalloc (md, size) |
| 468 | PTR md; |
| 469 | long size; |
| 470 | { |
| 471 | return (malloc (size)); |
| 472 | } |
| 473 | |
| 474 | PTR |
| 475 | mrealloc (md, ptr, size) |
| 476 | PTR md; |
| 477 | PTR ptr; |
| 478 | long size; |
| 479 | { |
| 480 | if (ptr == 0) /* Guard against old realloc's */ |
| 481 | return malloc (size); |
| 482 | else |
| 483 | return realloc (ptr, size); |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | mfree (md, ptr) |
| 488 | PTR md; |
| 489 | PTR ptr; |
| 490 | { |
| 491 | free (ptr); |
| 492 | } |
| 493 | |
| 494 | #endif /* NO_MMALLOC */ |
| 495 | |
| 496 | #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK) |
| 497 | |
| 498 | void |
| 499 | init_malloc (md) |
| 500 | PTR md; |
| 501 | { |
| 502 | } |
| 503 | |
| 504 | #else /* have mmalloc and want corruption checking */ |
| 505 | |
| 506 | static void |
| 507 | malloc_botch () |
| 508 | { |
| 509 | fatal_dump_core ("Memory corruption"); |
| 510 | } |
| 511 | |
| 512 | /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified |
| 513 | by MD, to detect memory corruption. Note that MD may be NULL to specify |
| 514 | the default heap that grows via sbrk. |
| 515 | |
| 516 | Note that for freshly created regions, we must call mmcheck prior to any |
| 517 | mallocs in the region. Otherwise, any region which was allocated prior to |
| 518 | installing the checking hooks, which is later reallocated or freed, will |
| 519 | fail the checks! The mmcheck function only allows initial hooks to be |
| 520 | installed before the first mmalloc. However, anytime after we have called |
| 521 | mmcheck the first time to install the checking hooks, we can call it again |
| 522 | to update the function pointer to the memory corruption handler. |
| 523 | |
| 524 | Returns zero on failure, non-zero on success. */ |
| 525 | |
| 526 | void |
| 527 | init_malloc (md) |
| 528 | PTR md; |
| 529 | { |
| 530 | if (!mmcheck (md, malloc_botch)) |
| 531 | { |
| 532 | warning ("internal error: failed to install memory consistency checks"); |
| 533 | } |
| 534 | |
| 535 | mmtrace (); |
| 536 | } |
| 537 | |
| 538 | #endif /* Have mmalloc and want corruption checking */ |
| 539 | |
| 540 | /* Called when a memory allocation fails, with the number of bytes of |
| 541 | memory requested in SIZE. */ |
| 542 | |
| 543 | NORETURN void |
| 544 | nomem (size) |
| 545 | long size; |
| 546 | { |
| 547 | if (size > 0) |
| 548 | { |
| 549 | fatal ("virtual memory exhausted: can't allocate %ld bytes.", size); |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | fatal ("virtual memory exhausted."); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* Like mmalloc but get error if no storage available, and protect against |
| 558 | the caller wanting to allocate zero bytes. Whether to return NULL for |
| 559 | a zero byte request, or translate the request into a request for one |
| 560 | byte of zero'd storage, is a religious issue. */ |
| 561 | |
| 562 | PTR |
| 563 | xmmalloc (md, size) |
| 564 | PTR md; |
| 565 | long size; |
| 566 | { |
| 567 | register PTR val; |
| 568 | |
| 569 | if (size == 0) |
| 570 | { |
| 571 | val = NULL; |
| 572 | } |
| 573 | else if ((val = mmalloc (md, size)) == NULL) |
| 574 | { |
| 575 | nomem (size); |
| 576 | } |
| 577 | return (val); |
| 578 | } |
| 579 | |
| 580 | /* Like mrealloc but get error if no storage available. */ |
| 581 | |
| 582 | PTR |
| 583 | xmrealloc (md, ptr, size) |
| 584 | PTR md; |
| 585 | PTR ptr; |
| 586 | long size; |
| 587 | { |
| 588 | register PTR val; |
| 589 | |
| 590 | if (ptr != NULL) |
| 591 | { |
| 592 | val = mrealloc (md, ptr, size); |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | val = mmalloc (md, size); |
| 597 | } |
| 598 | if (val == NULL) |
| 599 | { |
| 600 | nomem (size); |
| 601 | } |
| 602 | return (val); |
| 603 | } |
| 604 | |
| 605 | /* Like malloc but get error if no storage available, and protect against |
| 606 | the caller wanting to allocate zero bytes. */ |
| 607 | |
| 608 | PTR |
| 609 | xmalloc (size) |
| 610 | long size; |
| 611 | { |
| 612 | return (xmmalloc ((void *) NULL, size)); |
| 613 | } |
| 614 | |
| 615 | /* Like mrealloc but get error if no storage available. */ |
| 616 | |
| 617 | PTR |
| 618 | xrealloc (ptr, size) |
| 619 | PTR ptr; |
| 620 | long size; |
| 621 | { |
| 622 | return (xmrealloc ((void *) NULL, ptr, size)); |
| 623 | } |
| 624 | |
| 625 | \f |
| 626 | /* My replacement for the read system call. |
| 627 | Used like `read' but keeps going if `read' returns too soon. */ |
| 628 | |
| 629 | int |
| 630 | myread (desc, addr, len) |
| 631 | int desc; |
| 632 | char *addr; |
| 633 | int len; |
| 634 | { |
| 635 | register int val; |
| 636 | int orglen = len; |
| 637 | |
| 638 | while (len > 0) |
| 639 | { |
| 640 | val = read (desc, addr, len); |
| 641 | if (val < 0) |
| 642 | return val; |
| 643 | if (val == 0) |
| 644 | return orglen - len; |
| 645 | len -= val; |
| 646 | addr += val; |
| 647 | } |
| 648 | return orglen; |
| 649 | } |
| 650 | \f |
| 651 | /* Make a copy of the string at PTR with SIZE characters |
| 652 | (and add a null character at the end in the copy). |
| 653 | Uses malloc to get the space. Returns the address of the copy. */ |
| 654 | |
| 655 | char * |
| 656 | savestring (ptr, size) |
| 657 | const char *ptr; |
| 658 | int size; |
| 659 | { |
| 660 | register char *p = (char *) xmalloc (size + 1); |
| 661 | memcpy (p, ptr, size); |
| 662 | p[size] = 0; |
| 663 | return p; |
| 664 | } |
| 665 | |
| 666 | char * |
| 667 | msavestring (md, ptr, size) |
| 668 | void *md; |
| 669 | const char *ptr; |
| 670 | int size; |
| 671 | { |
| 672 | register char *p = (char *) xmmalloc (md, size + 1); |
| 673 | memcpy (p, ptr, size); |
| 674 | p[size] = 0; |
| 675 | return p; |
| 676 | } |
| 677 | |
| 678 | /* The "const" is so it compiles under DGUX (which prototypes strsave |
| 679 | in <string.h>. FIXME: This should be named "xstrsave", shouldn't it? |
| 680 | Doesn't real strsave return NULL if out of memory? */ |
| 681 | char * |
| 682 | strsave (ptr) |
| 683 | const char *ptr; |
| 684 | { |
| 685 | return savestring (ptr, strlen (ptr)); |
| 686 | } |
| 687 | |
| 688 | char * |
| 689 | mstrsave (md, ptr) |
| 690 | void *md; |
| 691 | const char *ptr; |
| 692 | { |
| 693 | return (msavestring (md, ptr, strlen (ptr))); |
| 694 | } |
| 695 | |
| 696 | void |
| 697 | print_spaces (n, file) |
| 698 | register int n; |
| 699 | register FILE *file; |
| 700 | { |
| 701 | while (n-- > 0) |
| 702 | fputc (' ', file); |
| 703 | } |
| 704 | |
| 705 | /* Ask user a y-or-n question and return 1 iff answer is yes. |
| 706 | Takes three args which are given to printf to print the question. |
| 707 | The first, a control string, should end in "? ". |
| 708 | It should not say how to answer, because we do that. */ |
| 709 | |
| 710 | /* VARARGS */ |
| 711 | int |
| 712 | query (va_alist) |
| 713 | va_dcl |
| 714 | { |
| 715 | va_list args; |
| 716 | char *ctlstr; |
| 717 | register int answer; |
| 718 | register int ans2; |
| 719 | |
| 720 | /* Automatically answer "yes" if input is not from a terminal. */ |
| 721 | if (!input_from_terminal_p ()) |
| 722 | return 1; |
| 723 | |
| 724 | while (1) |
| 725 | { |
| 726 | wrap_here (""); /* Flush any buffered output */ |
| 727 | fflush (stdout); |
| 728 | va_start (args); |
| 729 | ctlstr = va_arg (args, char *); |
| 730 | vfprintf_filtered (stdout, ctlstr, args); |
| 731 | va_end (args); |
| 732 | printf_filtered ("(y or n) "); |
| 733 | fflush (stdout); |
| 734 | answer = fgetc (stdin); |
| 735 | clearerr (stdin); /* in case of C-d */ |
| 736 | if (answer == EOF) /* C-d */ |
| 737 | return 1; |
| 738 | if (answer != '\n') /* Eat rest of input line, to EOF or newline */ |
| 739 | do |
| 740 | { |
| 741 | ans2 = fgetc (stdin); |
| 742 | clearerr (stdin); |
| 743 | } |
| 744 | while (ans2 != EOF && ans2 != '\n'); |
| 745 | if (answer >= 'a') |
| 746 | answer -= 040; |
| 747 | if (answer == 'Y') |
| 748 | return 1; |
| 749 | if (answer == 'N') |
| 750 | return 0; |
| 751 | printf_filtered ("Please answer y or n.\n"); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | \f |
| 756 | /* Parse a C escape sequence. STRING_PTR points to a variable |
| 757 | containing a pointer to the string to parse. That pointer |
| 758 | should point to the character after the \. That pointer |
| 759 | is updated past the characters we use. The value of the |
| 760 | escape sequence is returned. |
| 761 | |
| 762 | A negative value means the sequence \ newline was seen, |
| 763 | which is supposed to be equivalent to nothing at all. |
| 764 | |
| 765 | If \ is followed by a null character, we return a negative |
| 766 | value and leave the string pointer pointing at the null character. |
| 767 | |
| 768 | If \ is followed by 000, we return 0 and leave the string pointer |
| 769 | after the zeros. A value of 0 does not mean end of string. */ |
| 770 | |
| 771 | int |
| 772 | parse_escape (string_ptr) |
| 773 | char **string_ptr; |
| 774 | { |
| 775 | register int c = *(*string_ptr)++; |
| 776 | switch (c) |
| 777 | { |
| 778 | case 'a': |
| 779 | return 007; /* Bell (alert) char */ |
| 780 | case 'b': |
| 781 | return '\b'; |
| 782 | case 'e': /* Escape character */ |
| 783 | return 033; |
| 784 | case 'f': |
| 785 | return '\f'; |
| 786 | case 'n': |
| 787 | return '\n'; |
| 788 | case 'r': |
| 789 | return '\r'; |
| 790 | case 't': |
| 791 | return '\t'; |
| 792 | case 'v': |
| 793 | return '\v'; |
| 794 | case '\n': |
| 795 | return -2; |
| 796 | case 0: |
| 797 | (*string_ptr)--; |
| 798 | return 0; |
| 799 | case '^': |
| 800 | c = *(*string_ptr)++; |
| 801 | if (c == '\\') |
| 802 | c = parse_escape (string_ptr); |
| 803 | if (c == '?') |
| 804 | return 0177; |
| 805 | return (c & 0200) | (c & 037); |
| 806 | |
| 807 | case '0': |
| 808 | case '1': |
| 809 | case '2': |
| 810 | case '3': |
| 811 | case '4': |
| 812 | case '5': |
| 813 | case '6': |
| 814 | case '7': |
| 815 | { |
| 816 | register int i = c - '0'; |
| 817 | register int count = 0; |
| 818 | while (++count < 3) |
| 819 | { |
| 820 | if ((c = *(*string_ptr)++) >= '0' && c <= '7') |
| 821 | { |
| 822 | i *= 8; |
| 823 | i += c - '0'; |
| 824 | } |
| 825 | else |
| 826 | { |
| 827 | (*string_ptr)--; |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | return i; |
| 832 | } |
| 833 | default: |
| 834 | return c; |
| 835 | } |
| 836 | } |
| 837 | \f |
| 838 | /* Print the character C on STREAM as part of the contents of a literal |
| 839 | string whose delimiter is QUOTER. Note that this routine should only |
| 840 | be call for printing things which are independent of the language |
| 841 | of the program being debugged. */ |
| 842 | |
| 843 | void |
| 844 | gdb_printchar (c, stream, quoter) |
| 845 | register int c; |
| 846 | FILE *stream; |
| 847 | int quoter; |
| 848 | { |
| 849 | |
| 850 | c &= 0xFF; /* Avoid sign bit follies */ |
| 851 | |
| 852 | if ( c < 0x20 || /* Low control chars */ |
| 853 | (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ |
| 854 | (sevenbit_strings && c >= 0x80)) { /* high order bit set */ |
| 855 | switch (c) |
| 856 | { |
| 857 | case '\n': |
| 858 | fputs_filtered ("\\n", stream); |
| 859 | break; |
| 860 | case '\b': |
| 861 | fputs_filtered ("\\b", stream); |
| 862 | break; |
| 863 | case '\t': |
| 864 | fputs_filtered ("\\t", stream); |
| 865 | break; |
| 866 | case '\f': |
| 867 | fputs_filtered ("\\f", stream); |
| 868 | break; |
| 869 | case '\r': |
| 870 | fputs_filtered ("\\r", stream); |
| 871 | break; |
| 872 | case '\033': |
| 873 | fputs_filtered ("\\e", stream); |
| 874 | break; |
| 875 | case '\007': |
| 876 | fputs_filtered ("\\a", stream); |
| 877 | break; |
| 878 | default: |
| 879 | fprintf_filtered (stream, "\\%.3o", (unsigned int) c); |
| 880 | break; |
| 881 | } |
| 882 | } else { |
| 883 | if (c == '\\' || c == quoter) |
| 884 | fputs_filtered ("\\", stream); |
| 885 | fprintf_filtered (stream, "%c", c); |
| 886 | } |
| 887 | } |
| 888 | \f |
| 889 | /* Number of lines per page or UINT_MAX if paging is disabled. */ |
| 890 | static unsigned int lines_per_page; |
| 891 | /* Number of chars per line or UNIT_MAX is line folding is disabled. */ |
| 892 | static unsigned int chars_per_line; |
| 893 | /* Current count of lines printed on this page, chars on this line. */ |
| 894 | static unsigned int lines_printed, chars_printed; |
| 895 | |
| 896 | /* Buffer and start column of buffered text, for doing smarter word- |
| 897 | wrapping. When someone calls wrap_here(), we start buffering output |
| 898 | that comes through fputs_filtered(). If we see a newline, we just |
| 899 | spit it out and forget about the wrap_here(). If we see another |
| 900 | wrap_here(), we spit it out and remember the newer one. If we see |
| 901 | the end of the line, we spit out a newline, the indent, and then |
| 902 | the buffered output. |
| 903 | |
| 904 | wrap_column is the column number on the screen where wrap_buffer begins. |
| 905 | When wrap_column is zero, wrapping is not in effect. |
| 906 | wrap_buffer is malloc'd with chars_per_line+2 bytes. |
| 907 | When wrap_buffer[0] is null, the buffer is empty. |
| 908 | wrap_pointer points into it at the next character to fill. |
| 909 | wrap_indent is the string that should be used as indentation if the |
| 910 | wrap occurs. */ |
| 911 | |
| 912 | static char *wrap_buffer, *wrap_pointer, *wrap_indent; |
| 913 | static int wrap_column; |
| 914 | |
| 915 | /* ARGSUSED */ |
| 916 | static void |
| 917 | set_width_command (args, from_tty, c) |
| 918 | char *args; |
| 919 | int from_tty; |
| 920 | struct cmd_list_element *c; |
| 921 | { |
| 922 | if (!wrap_buffer) |
| 923 | { |
| 924 | wrap_buffer = (char *) xmalloc (chars_per_line + 2); |
| 925 | wrap_buffer[0] = '\0'; |
| 926 | } |
| 927 | else |
| 928 | wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); |
| 929 | wrap_pointer = wrap_buffer; /* Start it at the beginning */ |
| 930 | } |
| 931 | |
| 932 | /* Wait, so the user can read what's on the screen. Prompt the user |
| 933 | to continue by pressing RETURN. */ |
| 934 | |
| 935 | static void |
| 936 | prompt_for_continue () |
| 937 | { |
| 938 | char *ignore; |
| 939 | |
| 940 | /* We must do this *before* we call gdb_readline, else it will eventually |
| 941 | call us -- thinking that we're trying to print beyond the end of the |
| 942 | screen. */ |
| 943 | reinitialize_more_filter (); |
| 944 | |
| 945 | immediate_quit++; |
| 946 | ignore = gdb_readline ("---Type <return> to continue---"); |
| 947 | if (ignore) |
| 948 | free (ignore); |
| 949 | immediate_quit--; |
| 950 | |
| 951 | /* Now we have to do this again, so that GDB will know that it doesn't |
| 952 | need to save the ---Type <return>--- line at the top of the screen. */ |
| 953 | reinitialize_more_filter (); |
| 954 | |
| 955 | dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ |
| 956 | } |
| 957 | |
| 958 | /* Reinitialize filter; ie. tell it to reset to original values. */ |
| 959 | |
| 960 | void |
| 961 | reinitialize_more_filter () |
| 962 | { |
| 963 | lines_printed = 0; |
| 964 | chars_printed = 0; |
| 965 | } |
| 966 | |
| 967 | /* Indicate that if the next sequence of characters overflows the line, |
| 968 | a newline should be inserted here rather than when it hits the end. |
| 969 | If INDENT is nonzero, it is a string to be printed to indent the |
| 970 | wrapped part on the next line. INDENT must remain accessible until |
| 971 | the next call to wrap_here() or until a newline is printed through |
| 972 | fputs_filtered(). |
| 973 | |
| 974 | If the line is already overfull, we immediately print a newline and |
| 975 | the indentation, and disable further wrapping. |
| 976 | |
| 977 | If we don't know the width of lines, but we know the page height, |
| 978 | we must not wrap words, but should still keep track of newlines |
| 979 | that were explicitly printed. |
| 980 | |
| 981 | INDENT should not contain tabs, as that |
| 982 | will mess up the char count on the next line. FIXME. */ |
| 983 | |
| 984 | void |
| 985 | wrap_here(indent) |
| 986 | char *indent; |
| 987 | { |
| 988 | if (wrap_buffer[0]) |
| 989 | { |
| 990 | *wrap_pointer = '\0'; |
| 991 | fputs (wrap_buffer, stdout); |
| 992 | } |
| 993 | wrap_pointer = wrap_buffer; |
| 994 | wrap_buffer[0] = '\0'; |
| 995 | if (chars_per_line == UINT_MAX) /* No line overflow checking */ |
| 996 | { |
| 997 | wrap_column = 0; |
| 998 | } |
| 999 | else if (chars_printed >= chars_per_line) |
| 1000 | { |
| 1001 | puts_filtered ("\n"); |
| 1002 | puts_filtered (indent); |
| 1003 | wrap_column = 0; |
| 1004 | } |
| 1005 | else |
| 1006 | { |
| 1007 | wrap_column = chars_printed; |
| 1008 | wrap_indent = indent; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | /* Ensure that whatever gets printed next, using the filtered output |
| 1013 | commands, starts at the beginning of the line. I.E. if there is |
| 1014 | any pending output for the current line, flush it and start a new |
| 1015 | line. Otherwise do nothing. */ |
| 1016 | |
| 1017 | void |
| 1018 | begin_line () |
| 1019 | { |
| 1020 | if (chars_printed > 0) |
| 1021 | { |
| 1022 | puts_filtered ("\n"); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /* Like fputs but pause after every screenful, and can wrap at points |
| 1027 | other than the final character of a line. |
| 1028 | Unlike fputs, fputs_filtered does not return a value. |
| 1029 | It is OK for LINEBUFFER to be NULL, in which case just don't print |
| 1030 | anything. |
| 1031 | |
| 1032 | Note that a longjmp to top level may occur in this routine |
| 1033 | (since prompt_for_continue may do so) so this routine should not be |
| 1034 | called when cleanups are not in place. */ |
| 1035 | |
| 1036 | void |
| 1037 | fputs_filtered (linebuffer, stream) |
| 1038 | const char *linebuffer; |
| 1039 | FILE *stream; |
| 1040 | { |
| 1041 | const char *lineptr; |
| 1042 | |
| 1043 | if (linebuffer == 0) |
| 1044 | return; |
| 1045 | |
| 1046 | /* Don't do any filtering if it is disabled. */ |
| 1047 | if (stream != stdout |
| 1048 | || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) |
| 1049 | { |
| 1050 | fputs (linebuffer, stream); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | /* Go through and output each character. Show line extension |
| 1055 | when this is necessary; prompt user for new page when this is |
| 1056 | necessary. */ |
| 1057 | |
| 1058 | lineptr = linebuffer; |
| 1059 | while (*lineptr) |
| 1060 | { |
| 1061 | /* Possible new page. */ |
| 1062 | if (lines_printed >= lines_per_page - 1) |
| 1063 | prompt_for_continue (); |
| 1064 | |
| 1065 | while (*lineptr && *lineptr != '\n') |
| 1066 | { |
| 1067 | /* Print a single line. */ |
| 1068 | if (*lineptr == '\t') |
| 1069 | { |
| 1070 | if (wrap_column) |
| 1071 | *wrap_pointer++ = '\t'; |
| 1072 | else |
| 1073 | putc ('\t', stream); |
| 1074 | /* Shifting right by 3 produces the number of tab stops |
| 1075 | we have already passed, and then adding one and |
| 1076 | shifting left 3 advances to the next tab stop. */ |
| 1077 | chars_printed = ((chars_printed >> 3) + 1) << 3; |
| 1078 | lineptr++; |
| 1079 | } |
| 1080 | else |
| 1081 | { |
| 1082 | if (wrap_column) |
| 1083 | *wrap_pointer++ = *lineptr; |
| 1084 | else |
| 1085 | putc (*lineptr, stream); |
| 1086 | chars_printed++; |
| 1087 | lineptr++; |
| 1088 | } |
| 1089 | |
| 1090 | if (chars_printed >= chars_per_line) |
| 1091 | { |
| 1092 | unsigned int save_chars = chars_printed; |
| 1093 | |
| 1094 | chars_printed = 0; |
| 1095 | lines_printed++; |
| 1096 | /* If we aren't actually wrapping, don't output newline -- |
| 1097 | if chars_per_line is right, we probably just overflowed |
| 1098 | anyway; if it's wrong, let us keep going. */ |
| 1099 | if (wrap_column) |
| 1100 | putc ('\n', stream); |
| 1101 | |
| 1102 | /* Possible new page. */ |
| 1103 | if (lines_printed >= lines_per_page - 1) |
| 1104 | prompt_for_continue (); |
| 1105 | |
| 1106 | /* Now output indentation and wrapped string */ |
| 1107 | if (wrap_column) |
| 1108 | { |
| 1109 | if (wrap_indent) |
| 1110 | fputs (wrap_indent, stream); |
| 1111 | *wrap_pointer = '\0'; /* Null-terminate saved stuff */ |
| 1112 | fputs (wrap_buffer, stream); /* and eject it */ |
| 1113 | /* FIXME, this strlen is what prevents wrap_indent from |
| 1114 | containing tabs. However, if we recurse to print it |
| 1115 | and count its chars, we risk trouble if wrap_indent is |
| 1116 | longer than (the user settable) chars_per_line. |
| 1117 | Note also that this can set chars_printed > chars_per_line |
| 1118 | if we are printing a long string. */ |
| 1119 | chars_printed = strlen (wrap_indent) |
| 1120 | + (save_chars - wrap_column); |
| 1121 | wrap_pointer = wrap_buffer; /* Reset buffer */ |
| 1122 | wrap_buffer[0] = '\0'; |
| 1123 | wrap_column = 0; /* And disable fancy wrap */ |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | if (*lineptr == '\n') |
| 1129 | { |
| 1130 | chars_printed = 0; |
| 1131 | wrap_here ((char *)0); /* Spit out chars, cancel further wraps */ |
| 1132 | lines_printed++; |
| 1133 | putc ('\n', stream); |
| 1134 | lineptr++; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* Print a variable number of ARGS using format FORMAT. If this |
| 1140 | information is going to put the amount written (since the last call |
| 1141 | to REINITIALIZE_MORE_FILTER or the last page break) over the page size, |
| 1142 | print out a pause message and do a gdb_readline to get the users |
| 1143 | permision to continue. |
| 1144 | |
| 1145 | Unlike fprintf, this function does not return a value. |
| 1146 | |
| 1147 | We implement three variants, vfprintf (takes a vararg list and stream), |
| 1148 | fprintf (takes a stream to write on), and printf (the usual). |
| 1149 | |
| 1150 | Note that this routine has a restriction that the length of the |
| 1151 | final output line must be less than 255 characters *or* it must be |
| 1152 | less than twice the size of the format string. This is a very |
| 1153 | arbitrary restriction, but it is an internal restriction, so I'll |
| 1154 | put it in. This means that the %s format specifier is almost |
| 1155 | useless; unless the caller can GUARANTEE that the string is short |
| 1156 | enough, fputs_filtered should be used instead. |
| 1157 | |
| 1158 | Note also that a longjmp to top level may occur in this routine |
| 1159 | (since prompt_for_continue may do so) so this routine should not be |
| 1160 | called when cleanups are not in place. */ |
| 1161 | |
| 1162 | #define MIN_LINEBUF 255 |
| 1163 | |
| 1164 | void |
| 1165 | vfprintf_filtered (stream, format, args) |
| 1166 | FILE *stream; |
| 1167 | char *format; |
| 1168 | va_list args; |
| 1169 | { |
| 1170 | char line_buf[MIN_LINEBUF+10]; |
| 1171 | char *linebuffer = line_buf; |
| 1172 | int format_length; |
| 1173 | |
| 1174 | format_length = strlen (format); |
| 1175 | |
| 1176 | /* Reallocate buffer to a larger size if this is necessary. */ |
| 1177 | if (format_length * 2 > MIN_LINEBUF) |
| 1178 | { |
| 1179 | linebuffer = alloca (10 + format_length * 2); |
| 1180 | } |
| 1181 | |
| 1182 | /* This won't blow up if the restrictions described above are |
| 1183 | followed. */ |
| 1184 | vsprintf (linebuffer, format, args); |
| 1185 | |
| 1186 | fputs_filtered (linebuffer, stream); |
| 1187 | } |
| 1188 | |
| 1189 | void |
| 1190 | vprintf_filtered (format, args) |
| 1191 | char *format; |
| 1192 | va_list args; |
| 1193 | { |
| 1194 | vfprintf_filtered (stdout, format, args); |
| 1195 | } |
| 1196 | |
| 1197 | /* VARARGS */ |
| 1198 | void |
| 1199 | fprintf_filtered (va_alist) |
| 1200 | va_dcl |
| 1201 | { |
| 1202 | va_list args; |
| 1203 | FILE *stream; |
| 1204 | char *format; |
| 1205 | |
| 1206 | va_start (args); |
| 1207 | stream = va_arg (args, FILE *); |
| 1208 | format = va_arg (args, char *); |
| 1209 | |
| 1210 | /* This won't blow up if the restrictions described above are |
| 1211 | followed. */ |
| 1212 | vfprintf_filtered (stream, format, args); |
| 1213 | va_end (args); |
| 1214 | } |
| 1215 | |
| 1216 | /* Like fprintf_filtered, but prints it's result indent. |
| 1217 | Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */ |
| 1218 | |
| 1219 | /* VARARGS */ |
| 1220 | void |
| 1221 | fprintfi_filtered (va_alist) |
| 1222 | va_dcl |
| 1223 | { |
| 1224 | va_list args; |
| 1225 | int spaces; |
| 1226 | FILE *stream; |
| 1227 | char *format; |
| 1228 | |
| 1229 | va_start (args); |
| 1230 | spaces = va_arg (args, int); |
| 1231 | stream = va_arg (args, FILE *); |
| 1232 | format = va_arg (args, char *); |
| 1233 | print_spaces_filtered (spaces, stream); |
| 1234 | |
| 1235 | /* This won't blow up if the restrictions described above are |
| 1236 | followed. */ |
| 1237 | vfprintf_filtered (stream, format, args); |
| 1238 | va_end (args); |
| 1239 | } |
| 1240 | |
| 1241 | /* VARARGS */ |
| 1242 | void |
| 1243 | printf_filtered (va_alist) |
| 1244 | va_dcl |
| 1245 | { |
| 1246 | va_list args; |
| 1247 | char *format; |
| 1248 | |
| 1249 | va_start (args); |
| 1250 | format = va_arg (args, char *); |
| 1251 | |
| 1252 | vfprintf_filtered (stdout, format, args); |
| 1253 | va_end (args); |
| 1254 | } |
| 1255 | |
| 1256 | /* Like printf_filtered, but prints it's result indented. |
| 1257 | Called as printfi_filtered (spaces, format, arg1, arg2, ...); */ |
| 1258 | |
| 1259 | /* VARARGS */ |
| 1260 | void |
| 1261 | printfi_filtered (va_alist) |
| 1262 | va_dcl |
| 1263 | { |
| 1264 | va_list args; |
| 1265 | int spaces; |
| 1266 | char *format; |
| 1267 | |
| 1268 | va_start (args); |
| 1269 | spaces = va_arg (args, int); |
| 1270 | format = va_arg (args, char *); |
| 1271 | print_spaces_filtered (spaces, stdout); |
| 1272 | vfprintf_filtered (stdout, format, args); |
| 1273 | va_end (args); |
| 1274 | } |
| 1275 | |
| 1276 | /* Easy -- but watch out! |
| 1277 | |
| 1278 | This routine is *not* a replacement for puts()! puts() appends a newline. |
| 1279 | This one doesn't, and had better not! */ |
| 1280 | |
| 1281 | void |
| 1282 | puts_filtered (string) |
| 1283 | char *string; |
| 1284 | { |
| 1285 | fputs_filtered (string, stdout); |
| 1286 | } |
| 1287 | |
| 1288 | /* Return a pointer to N spaces and a null. The pointer is good |
| 1289 | until the next call to here. */ |
| 1290 | char * |
| 1291 | n_spaces (n) |
| 1292 | int n; |
| 1293 | { |
| 1294 | register char *t; |
| 1295 | static char *spaces; |
| 1296 | static int max_spaces; |
| 1297 | |
| 1298 | if (n > max_spaces) |
| 1299 | { |
| 1300 | if (spaces) |
| 1301 | free (spaces); |
| 1302 | spaces = (char *) xmalloc (n+1); |
| 1303 | for (t = spaces+n; t != spaces;) |
| 1304 | *--t = ' '; |
| 1305 | spaces[n] = '\0'; |
| 1306 | max_spaces = n; |
| 1307 | } |
| 1308 | |
| 1309 | return spaces + max_spaces - n; |
| 1310 | } |
| 1311 | |
| 1312 | /* Print N spaces. */ |
| 1313 | void |
| 1314 | print_spaces_filtered (n, stream) |
| 1315 | int n; |
| 1316 | FILE *stream; |
| 1317 | { |
| 1318 | fputs_filtered (n_spaces (n), stream); |
| 1319 | } |
| 1320 | \f |
| 1321 | /* C++ demangler stuff. */ |
| 1322 | |
| 1323 | /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language |
| 1324 | LANG, using demangling args ARG_MODE, and print it filtered to STREAM. |
| 1325 | If the name is not mangled, or the language for the name is unknown, or |
| 1326 | demangling is off, the name is printed in its "raw" form. */ |
| 1327 | |
| 1328 | void |
| 1329 | fprintf_symbol_filtered (stream, name, lang, arg_mode) |
| 1330 | FILE *stream; |
| 1331 | char *name; |
| 1332 | enum language lang; |
| 1333 | int arg_mode; |
| 1334 | { |
| 1335 | char *demangled; |
| 1336 | |
| 1337 | if (name != NULL) |
| 1338 | { |
| 1339 | /* If user wants to see raw output, no problem. */ |
| 1340 | if (!demangle) |
| 1341 | { |
| 1342 | fputs_filtered (name, stream); |
| 1343 | } |
| 1344 | else |
| 1345 | { |
| 1346 | switch (lang) |
| 1347 | { |
| 1348 | case language_cplus: |
| 1349 | demangled = cplus_demangle (name, arg_mode); |
| 1350 | break; |
| 1351 | case language_chill: |
| 1352 | demangled = chill_demangle (name); |
| 1353 | break; |
| 1354 | default: |
| 1355 | demangled = NULL; |
| 1356 | break; |
| 1357 | } |
| 1358 | fputs_filtered (demangled ? demangled : name, stream); |
| 1359 | if (demangled != NULL) |
| 1360 | { |
| 1361 | free (demangled); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any |
| 1368 | differences in whitespace. Returns 0 if they match, non-zero if they |
| 1369 | don't (slightly different than strcmp()'s range of return values). |
| 1370 | |
| 1371 | As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". |
| 1372 | This "feature" is useful when searching for matching C++ function names |
| 1373 | (such as if the user types 'break FOO', where FOO is a mangled C++ |
| 1374 | function). */ |
| 1375 | |
| 1376 | int |
| 1377 | strcmp_iw (string1, string2) |
| 1378 | const char *string1; |
| 1379 | const char *string2; |
| 1380 | { |
| 1381 | while ((*string1 != '\0') && (*string2 != '\0')) |
| 1382 | { |
| 1383 | while (isspace (*string1)) |
| 1384 | { |
| 1385 | string1++; |
| 1386 | } |
| 1387 | while (isspace (*string2)) |
| 1388 | { |
| 1389 | string2++; |
| 1390 | } |
| 1391 | if (*string1 != *string2) |
| 1392 | { |
| 1393 | break; |
| 1394 | } |
| 1395 | if (*string1 != '\0') |
| 1396 | { |
| 1397 | string1++; |
| 1398 | string2++; |
| 1399 | } |
| 1400 | } |
| 1401 | return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); |
| 1402 | } |
| 1403 | |
| 1404 | \f |
| 1405 | void |
| 1406 | _initialize_utils () |
| 1407 | { |
| 1408 | struct cmd_list_element *c; |
| 1409 | |
| 1410 | c = add_set_cmd ("width", class_support, var_uinteger, |
| 1411 | (char *)&chars_per_line, |
| 1412 | "Set number of characters gdb thinks are in a line.", |
| 1413 | &setlist); |
| 1414 | add_show_from_set (c, &showlist); |
| 1415 | c->function.sfunc = set_width_command; |
| 1416 | |
| 1417 | add_show_from_set |
| 1418 | (add_set_cmd ("height", class_support, |
| 1419 | var_uinteger, (char *)&lines_per_page, |
| 1420 | "Set number of lines gdb thinks are in a page.", &setlist), |
| 1421 | &showlist); |
| 1422 | |
| 1423 | /* These defaults will be used if we are unable to get the correct |
| 1424 | values from termcap. */ |
| 1425 | #if defined(__GO32__) |
| 1426 | lines_per_page = ScreenRows(); |
| 1427 | chars_per_line = ScreenCols(); |
| 1428 | #else |
| 1429 | lines_per_page = 24; |
| 1430 | chars_per_line = 80; |
| 1431 | /* Initialize the screen height and width from termcap. */ |
| 1432 | { |
| 1433 | char *termtype = getenv ("TERM"); |
| 1434 | |
| 1435 | /* Positive means success, nonpositive means failure. */ |
| 1436 | int status; |
| 1437 | |
| 1438 | /* 2048 is large enough for all known terminals, according to the |
| 1439 | GNU termcap manual. */ |
| 1440 | char term_buffer[2048]; |
| 1441 | |
| 1442 | if (termtype) |
| 1443 | { |
| 1444 | status = tgetent (term_buffer, termtype); |
| 1445 | if (status > 0) |
| 1446 | { |
| 1447 | int val; |
| 1448 | |
| 1449 | val = tgetnum ("li"); |
| 1450 | if (val >= 0) |
| 1451 | lines_per_page = val; |
| 1452 | else |
| 1453 | /* The number of lines per page is not mentioned |
| 1454 | in the terminal description. This probably means |
| 1455 | that paging is not useful (e.g. emacs shell window), |
| 1456 | so disable paging. */ |
| 1457 | lines_per_page = UINT_MAX; |
| 1458 | |
| 1459 | val = tgetnum ("co"); |
| 1460 | if (val >= 0) |
| 1461 | chars_per_line = val; |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) |
| 1467 | |
| 1468 | /* If there is a better way to determine the window size, use it. */ |
| 1469 | SIGWINCH_HANDLER (); |
| 1470 | #endif |
| 1471 | #endif |
| 1472 | /* If the output is not a terminal, don't paginate it. */ |
| 1473 | if (!ISATTY (stdout)) |
| 1474 | lines_per_page = UINT_MAX; |
| 1475 | |
| 1476 | set_width_command ((char *)NULL, 0, c); |
| 1477 | |
| 1478 | add_show_from_set |
| 1479 | (add_set_cmd ("demangle", class_support, var_boolean, |
| 1480 | (char *)&demangle, |
| 1481 | "Set demangling of encoded C++ names when displaying symbols.", |
| 1482 | &setprintlist), |
| 1483 | &showprintlist); |
| 1484 | |
| 1485 | add_show_from_set |
| 1486 | (add_set_cmd ("sevenbit-strings", class_support, var_boolean, |
| 1487 | (char *)&sevenbit_strings, |
| 1488 | "Set printing of 8-bit characters in strings as \\nnn.", |
| 1489 | &setprintlist), |
| 1490 | &showprintlist); |
| 1491 | |
| 1492 | add_show_from_set |
| 1493 | (add_set_cmd ("asm-demangle", class_support, var_boolean, |
| 1494 | (char *)&asm_demangle, |
| 1495 | "Set demangling of C++ names in disassembly listings.", |
| 1496 | &setprintlist), |
| 1497 | &showprintlist); |
| 1498 | } |
| 1499 | |
| 1500 | /* Machine specific function to handle SIGWINCH signal. */ |
| 1501 | |
| 1502 | #ifdef SIGWINCH_HANDLER_BODY |
| 1503 | SIGWINCH_HANDLER_BODY |
| 1504 | #endif |
| 1505 | |