]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/utils.c
(is_end_of_line): Add three missing lines of 0 initializers.
[thirdparty/binutils-gdb.git] / gdb / utils.c
CommitLineData
c906108c 1/* General utility routines for GDB, the GNU debugger.
d9fcf2fb
JM
2 Copyright 1986, 1989, 1990-1992, 1995, 1996, 1998, 2000
3 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
9 the Free Software Foundation; either version 2 of the License, or
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
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include <ctype.h>
24#include "gdb_string.h"
c2c6d25f 25#include "event-top.h"
c906108c
SS
26
27#ifdef HAVE_CURSES_H
28#include <curses.h>
29#endif
30#ifdef HAVE_TERM_H
31#include <term.h>
32#endif
33
9d271fd8
AC
34#ifdef __GO32__
35#include <pc.h>
36#endif
37
c906108c
SS
38/* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */
39#ifdef reg
40#undef reg
41#endif
42
43#include "signals.h"
44#include "gdbcmd.h"
45#include "serial.h"
46#include "bfd.h"
47#include "target.h"
48#include "demangle.h"
49#include "expression.h"
50#include "language.h"
51#include "annotate.h"
52
53#include <readline/readline.h>
54
917317f4
JM
55#undef XMALLOC
56#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
57
c906108c
SS
58/* readline defines this. */
59#undef savestring
60
507f3c78 61void (*error_begin_hook) (void);
c906108c 62
2acceee2
JM
63/* Holds the last error message issued by gdb */
64
d9fcf2fb 65static struct ui_file *gdb_lasterr;
2acceee2 66
c906108c
SS
67/* Prototypes for local functions */
68
d9fcf2fb
JM
69static void vfprintf_maybe_filtered (struct ui_file *, const char *,
70 va_list, int);
c906108c 71
d9fcf2fb 72static void fputs_maybe_filtered (const char *, struct ui_file *, int);
c906108c
SS
73
74#if defined (USE_MMALLOC) && !defined (NO_MMCHECK)
a14ed312 75static void malloc_botch (void);
c906108c
SS
76#endif
77
a14ed312 78static void prompt_for_continue (void);
c906108c 79
a14ed312 80static void set_width_command (char *, int, struct cmd_list_element *);
c906108c 81
a14ed312 82static void set_width (void);
c906108c 83
c906108c
SS
84/* Chain of cleanup actions established with make_cleanup,
85 to be executed if an error happens. */
86
c5aa993b
JM
87static struct cleanup *cleanup_chain; /* cleaned up after a failed command */
88static struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */
89static struct cleanup *run_cleanup_chain; /* cleaned up on each 'run' */
90static struct cleanup *exec_cleanup_chain; /* cleaned up on each execution command */
6426a772
JM
91/* cleaned up on each error from within an execution command */
92static struct cleanup *exec_error_cleanup_chain;
43ff13b4
JM
93
94/* Pointer to what is left to do for an execution command after the
95 target stops. Used only in asynchronous mode, by targets that
96 support async execution. The finish and until commands use it. So
97 does the target extended-remote command. */
98struct continuation *cmd_continuation;
c2d11a7d 99struct continuation *intermediate_continuation;
c906108c
SS
100
101/* Nonzero if we have job control. */
102
103int job_control;
104
105/* Nonzero means a quit has been requested. */
106
107int quit_flag;
108
109/* Nonzero means quit immediately if Control-C is typed now, rather
110 than waiting until QUIT is executed. Be careful in setting this;
111 code which executes with immediate_quit set has to be very careful
112 about being able to deal with being interrupted at any time. It is
113 almost always better to use QUIT; the only exception I can think of
114 is being able to quit out of a system call (using EINTR loses if
115 the SIGINT happens between the previous QUIT and the system call).
116 To immediately quit in the case in which a SIGINT happens between
117 the previous QUIT and setting immediate_quit (desirable anytime we
118 expect to block), call QUIT after setting immediate_quit. */
119
120int immediate_quit;
121
122/* Nonzero means that encoded C++ names should be printed out in their
123 C++ form rather than raw. */
124
125int demangle = 1;
126
127/* Nonzero means that encoded C++ names should be printed out in their
128 C++ form even in assembler language displays. If this is set, but
129 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
130
131int asm_demangle = 0;
132
133/* Nonzero means that strings with character values >0x7F should be printed
134 as octal escapes. Zero means just print the value (e.g. it's an
135 international character, and the terminal or window can cope.) */
136
137int sevenbit_strings = 0;
138
139/* String to be printed before error messages, if any. */
140
141char *error_pre_print;
142
143/* String to be printed before quit messages, if any. */
144
145char *quit_pre_print;
146
147/* String to be printed before warning messages, if any. */
148
149char *warning_pre_print = "\nwarning: ";
150
151int pagination_enabled = 1;
c906108c 152\f
c5aa993b 153
c906108c
SS
154/* Add a new cleanup to the cleanup_chain,
155 and return the previous chain pointer
156 to be passed later to do_cleanups or discard_cleanups.
157 Args are FUNCTION to clean up with, and ARG to pass to it. */
158
159struct cleanup *
e4005526 160make_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 161{
c5aa993b 162 return make_my_cleanup (&cleanup_chain, function, arg);
c906108c
SS
163}
164
165struct cleanup *
e4005526 166make_final_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 167{
c5aa993b 168 return make_my_cleanup (&final_cleanup_chain, function, arg);
c906108c 169}
7a292a7a 170
c906108c 171struct cleanup *
e4005526 172make_run_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 173{
c5aa993b 174 return make_my_cleanup (&run_cleanup_chain, function, arg);
c906108c 175}
7a292a7a 176
43ff13b4 177struct cleanup *
e4005526 178make_exec_cleanup (make_cleanup_ftype *function, void *arg)
43ff13b4 179{
c5aa993b 180 return make_my_cleanup (&exec_cleanup_chain, function, arg);
43ff13b4
JM
181}
182
6426a772 183struct cleanup *
e4005526 184make_exec_error_cleanup (make_cleanup_ftype *function, void *arg)
6426a772
JM
185{
186 return make_my_cleanup (&exec_error_cleanup_chain, function, arg);
187}
188
7a292a7a
SS
189static void
190do_freeargv (arg)
191 void *arg;
192{
c5aa993b 193 freeargv ((char **) arg);
7a292a7a
SS
194}
195
196struct cleanup *
197make_cleanup_freeargv (arg)
198 char **arg;
199{
200 return make_my_cleanup (&cleanup_chain, do_freeargv, arg);
201}
202
5c65bbb6
AC
203static void
204do_bfd_close_cleanup (void *arg)
205{
206 bfd_close (arg);
207}
208
209struct cleanup *
210make_cleanup_bfd_close (bfd *abfd)
211{
212 return make_cleanup (do_bfd_close_cleanup, abfd);
213}
214
f5ff8c83
AC
215static void
216do_close_cleanup (void *arg)
217{
218 close ((int) arg);
219}
220
221struct cleanup *
222make_cleanup_close (int fd)
223{
224 /* int into void*. Outch!! */
225 return make_cleanup (do_close_cleanup, (void *) fd);
226}
227
11cf8741 228static void
d9fcf2fb 229do_ui_file_delete (void *arg)
11cf8741 230{
d9fcf2fb 231 ui_file_delete (arg);
11cf8741
JM
232}
233
234struct cleanup *
d9fcf2fb 235make_cleanup_ui_file_delete (struct ui_file *arg)
11cf8741 236{
d9fcf2fb 237 return make_my_cleanup (&cleanup_chain, do_ui_file_delete, arg);
11cf8741
JM
238}
239
c906108c 240struct cleanup *
e4005526
AC
241make_my_cleanup (struct cleanup **pmy_chain, make_cleanup_ftype *function,
242 void *arg)
c906108c
SS
243{
244 register struct cleanup *new
c5aa993b 245 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
c906108c
SS
246 register struct cleanup *old_chain = *pmy_chain;
247
248 new->next = *pmy_chain;
249 new->function = function;
250 new->arg = arg;
251 *pmy_chain = new;
252
253 return old_chain;
254}
255
256/* Discard cleanups and do the actions they describe
257 until we get back to the point OLD_CHAIN in the cleanup_chain. */
258
259void
260do_cleanups (old_chain)
261 register struct cleanup *old_chain;
262{
c5aa993b 263 do_my_cleanups (&cleanup_chain, old_chain);
c906108c
SS
264}
265
266void
267do_final_cleanups (old_chain)
268 register struct cleanup *old_chain;
269{
c5aa993b 270 do_my_cleanups (&final_cleanup_chain, old_chain);
c906108c
SS
271}
272
273void
274do_run_cleanups (old_chain)
275 register struct cleanup *old_chain;
276{
c5aa993b 277 do_my_cleanups (&run_cleanup_chain, old_chain);
c906108c
SS
278}
279
43ff13b4
JM
280void
281do_exec_cleanups (old_chain)
282 register struct cleanup *old_chain;
283{
c5aa993b 284 do_my_cleanups (&exec_cleanup_chain, old_chain);
43ff13b4
JM
285}
286
6426a772
JM
287void
288do_exec_error_cleanups (old_chain)
289 register struct cleanup *old_chain;
290{
291 do_my_cleanups (&exec_error_cleanup_chain, old_chain);
292}
293
c906108c
SS
294void
295do_my_cleanups (pmy_chain, old_chain)
296 register struct cleanup **pmy_chain;
297 register struct cleanup *old_chain;
298{
299 register struct cleanup *ptr;
300 while ((ptr = *pmy_chain) != old_chain)
301 {
302 *pmy_chain = ptr->next; /* Do this first incase recursion */
303 (*ptr->function) (ptr->arg);
304 free (ptr);
305 }
306}
307
308/* Discard cleanups, not doing the actions they describe,
309 until we get back to the point OLD_CHAIN in the cleanup_chain. */
310
311void
312discard_cleanups (old_chain)
313 register struct cleanup *old_chain;
314{
c5aa993b 315 discard_my_cleanups (&cleanup_chain, old_chain);
c906108c
SS
316}
317
318void
319discard_final_cleanups (old_chain)
320 register struct cleanup *old_chain;
321{
c5aa993b 322 discard_my_cleanups (&final_cleanup_chain, old_chain);
c906108c
SS
323}
324
6426a772
JM
325void
326discard_exec_error_cleanups (old_chain)
327 register struct cleanup *old_chain;
328{
329 discard_my_cleanups (&exec_error_cleanup_chain, old_chain);
330}
331
c906108c
SS
332void
333discard_my_cleanups (pmy_chain, old_chain)
334 register struct cleanup **pmy_chain;
335 register struct cleanup *old_chain;
336{
337 register struct cleanup *ptr;
338 while ((ptr = *pmy_chain) != old_chain)
339 {
340 *pmy_chain = ptr->next;
e4005526 341 free (ptr);
c906108c
SS
342 }
343}
344
345/* Set the cleanup_chain to 0, and return the old cleanup chain. */
346struct cleanup *
347save_cleanups ()
348{
c5aa993b 349 return save_my_cleanups (&cleanup_chain);
c906108c
SS
350}
351
352struct cleanup *
353save_final_cleanups ()
354{
c5aa993b 355 return save_my_cleanups (&final_cleanup_chain);
c906108c
SS
356}
357
358struct cleanup *
359save_my_cleanups (pmy_chain)
c5aa993b 360 struct cleanup **pmy_chain;
c906108c
SS
361{
362 struct cleanup *old_chain = *pmy_chain;
363
364 *pmy_chain = 0;
365 return old_chain;
366}
367
368/* Restore the cleanup chain from a previously saved chain. */
369void
370restore_cleanups (chain)
371 struct cleanup *chain;
372{
c5aa993b 373 restore_my_cleanups (&cleanup_chain, chain);
c906108c
SS
374}
375
376void
377restore_final_cleanups (chain)
378 struct cleanup *chain;
379{
c5aa993b 380 restore_my_cleanups (&final_cleanup_chain, chain);
c906108c
SS
381}
382
383void
384restore_my_cleanups (pmy_chain, chain)
385 struct cleanup **pmy_chain;
386 struct cleanup *chain;
387{
388 *pmy_chain = chain;
389}
390
391/* This function is useful for cleanups.
392 Do
393
c5aa993b
JM
394 foo = xmalloc (...);
395 old_chain = make_cleanup (free_current_contents, &foo);
c906108c
SS
396
397 to arrange to free the object thus allocated. */
398
399void
2f9429ae 400free_current_contents (void *ptr)
c906108c 401{
2f9429ae 402 void **location = ptr;
e2f9c474
AC
403 if (location == NULL)
404 internal_error ("free_current_contents: NULL pointer");
2f9429ae 405 if (*location != NULL)
e2f9c474
AC
406 {
407 free (*location);
408 *location = NULL;
409 }
c906108c
SS
410}
411
412/* Provide a known function that does nothing, to use as a base for
413 for a possibly long chain of cleanups. This is useful where we
414 use the cleanup chain for handling normal cleanups as well as dealing
415 with cleanups that need to be done as a result of a call to error().
416 In such cases, we may not be certain where the first cleanup is, unless
417 we have a do-nothing one to always use as the base. */
418
419/* ARGSUSED */
420void
e4005526 421null_cleanup (void *arg)
c906108c
SS
422{
423}
424
43ff13b4 425/* Add a continuation to the continuation list, the gloabl list
c2d11a7d 426 cmd_continuation. The new continuation will be added at the front.*/
43ff13b4
JM
427void
428add_continuation (continuation_hook, arg_list)
507f3c78 429 void (*continuation_hook) (struct continuation_arg *);
c5aa993b 430 struct continuation_arg *arg_list;
43ff13b4 431{
c5aa993b 432 struct continuation *continuation_ptr;
43ff13b4 433
c5aa993b
JM
434 continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation));
435 continuation_ptr->continuation_hook = continuation_hook;
436 continuation_ptr->arg_list = arg_list;
437 continuation_ptr->next = cmd_continuation;
438 cmd_continuation = continuation_ptr;
43ff13b4
JM
439}
440
441/* Walk down the cmd_continuation list, and execute all the
c2d11a7d
JM
442 continuations. There is a problem though. In some cases new
443 continuations may be added while we are in the middle of this
444 loop. If this happens they will be added in the front, and done
445 before we have a chance of exhausting those that were already
446 there. We need to then save the beginning of the list in a pointer
447 and do the continuations from there on, instead of using the
448 global beginning of list as our iteration pointer.*/
c5aa993b 449void
43ff13b4 450do_all_continuations ()
c2d11a7d
JM
451{
452 struct continuation *continuation_ptr;
453 struct continuation *saved_continuation;
454
455 /* Copy the list header into another pointer, and set the global
456 list header to null, so that the global list can change as a side
457 effect of invoking the continuations and the processing of
458 the preexisting continuations will not be affected. */
459 continuation_ptr = cmd_continuation;
460 cmd_continuation = NULL;
461
462 /* Work now on the list we have set aside. */
463 while (continuation_ptr)
464 {
465 (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
466 saved_continuation = continuation_ptr;
467 continuation_ptr = continuation_ptr->next;
468 free (saved_continuation);
469 }
470}
471
472/* Walk down the cmd_continuation list, and get rid of all the
473 continuations. */
474void
475discard_all_continuations ()
43ff13b4 476{
c5aa993b 477 struct continuation *continuation_ptr;
43ff13b4 478
c5aa993b
JM
479 while (cmd_continuation)
480 {
c5aa993b
JM
481 continuation_ptr = cmd_continuation;
482 cmd_continuation = continuation_ptr->next;
483 free (continuation_ptr);
484 }
43ff13b4 485}
c2c6d25f 486
57e687d9 487/* Add a continuation to the continuation list, the global list
c2d11a7d
JM
488 intermediate_continuation. The new continuation will be added at the front.*/
489void
490add_intermediate_continuation (continuation_hook, arg_list)
507f3c78 491 void (*continuation_hook) (struct continuation_arg *);
c2d11a7d
JM
492 struct continuation_arg *arg_list;
493{
494 struct continuation *continuation_ptr;
495
496 continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation));
497 continuation_ptr->continuation_hook = continuation_hook;
498 continuation_ptr->arg_list = arg_list;
499 continuation_ptr->next = intermediate_continuation;
500 intermediate_continuation = continuation_ptr;
501}
502
503/* Walk down the cmd_continuation list, and execute all the
504 continuations. There is a problem though. In some cases new
505 continuations may be added while we are in the middle of this
506 loop. If this happens they will be added in the front, and done
507 before we have a chance of exhausting those that were already
508 there. We need to then save the beginning of the list in a pointer
509 and do the continuations from there on, instead of using the
510 global beginning of list as our iteration pointer.*/
511void
512do_all_intermediate_continuations ()
513{
514 struct continuation *continuation_ptr;
515 struct continuation *saved_continuation;
516
517 /* Copy the list header into another pointer, and set the global
518 list header to null, so that the global list can change as a side
519 effect of invoking the continuations and the processing of
520 the preexisting continuations will not be affected. */
521 continuation_ptr = intermediate_continuation;
522 intermediate_continuation = NULL;
523
524 /* Work now on the list we have set aside. */
525 while (continuation_ptr)
526 {
527 (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
528 saved_continuation = continuation_ptr;
529 continuation_ptr = continuation_ptr->next;
530 free (saved_continuation);
531 }
532}
533
c2c6d25f
JM
534/* Walk down the cmd_continuation list, and get rid of all the
535 continuations. */
536void
c2d11a7d 537discard_all_intermediate_continuations ()
c2c6d25f
JM
538{
539 struct continuation *continuation_ptr;
540
c2d11a7d 541 while (intermediate_continuation)
c2c6d25f 542 {
c2d11a7d
JM
543 continuation_ptr = intermediate_continuation;
544 intermediate_continuation = continuation_ptr->next;
c2c6d25f
JM
545 free (continuation_ptr);
546 }
547}
548
c906108c 549\f
c5aa993b 550
c906108c
SS
551/* Print a warning message. Way to use this is to call warning_begin,
552 output the warning message (use unfiltered output to gdb_stderr),
553 ending in a newline. There is not currently a warning_end that you
554 call afterwards, but such a thing might be added if it is useful
555 for a GUI to separate warning messages from other output.
556
557 FIXME: Why do warnings use unfiltered output and errors filtered?
558 Is this anything other than a historical accident? */
559
560void
561warning_begin ()
562{
563 target_terminal_ours ();
c5aa993b 564 wrap_here (""); /* Force out any buffered output */
c906108c
SS
565 gdb_flush (gdb_stdout);
566 if (warning_pre_print)
567 fprintf_unfiltered (gdb_stderr, warning_pre_print);
568}
569
570/* Print a warning message.
571 The first argument STRING is the warning message, used as a fprintf string,
572 and the remaining args are passed as arguments to it.
573 The primary difference between warnings and errors is that a warning
574 does not force the return to command level. */
575
c906108c 576void
c5aa993b 577warning (const char *string,...)
c906108c
SS
578{
579 va_list args;
c906108c 580 va_start (args, string);
c906108c
SS
581 if (warning_hook)
582 (*warning_hook) (string, args);
583 else
c5aa993b
JM
584 {
585 warning_begin ();
586 vfprintf_unfiltered (gdb_stderr, string, args);
587 fprintf_unfiltered (gdb_stderr, "\n");
588 va_end (args);
589 }
c906108c
SS
590}
591
592/* Start the printing of an error message. Way to use this is to call
593 this, output the error message (use filtered output to gdb_stderr
594 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
595 in a newline, and then call return_to_top_level (RETURN_ERROR).
596 error() provides a convenient way to do this for the special case
597 that the error message can be formatted with a single printf call,
598 but this is more general. */
599void
600error_begin ()
601{
602 if (error_begin_hook)
603 error_begin_hook ();
604
605 target_terminal_ours ();
c5aa993b 606 wrap_here (""); /* Force out any buffered output */
c906108c
SS
607 gdb_flush (gdb_stdout);
608
609 annotate_error_begin ();
610
611 if (error_pre_print)
612 fprintf_filtered (gdb_stderr, error_pre_print);
613}
614
615/* Print an error message and return to command level.
616 The first argument STRING is the error message, used as a fprintf string,
617 and the remaining args are passed as arguments to it. */
618
4ce44c66
JM
619NORETURN void
620verror (const char *string, va_list args)
621{
c2d11a7d
JM
622 char *err_string;
623 struct cleanup *err_string_cleanup;
4ce44c66
JM
624 /* FIXME: cagney/1999-11-10: All error calls should come here.
625 Unfortunatly some code uses the sequence: error_begin(); print
626 error message; return_to_top_level. That code should be
627 flushed. */
628 error_begin ();
c2d11a7d
JM
629 /* NOTE: It's tempting to just do the following...
630 vfprintf_filtered (gdb_stderr, string, args);
631 and then follow with a similar looking statement to cause the message
632 to also go to gdb_lasterr. But if we do this, we'll be traversing the
633 va_list twice which works on some platforms and fails miserably on
634 others. */
635 /* Save it as the last error */
d9fcf2fb 636 ui_file_rewind (gdb_lasterr);
4ce44c66 637 vfprintf_filtered (gdb_lasterr, string, args);
c2d11a7d
JM
638 /* Retrieve the last error and print it to gdb_stderr */
639 err_string = error_last_message ();
640 err_string_cleanup = make_cleanup (free, err_string);
641 fputs_filtered (err_string, gdb_stderr);
642 fprintf_filtered (gdb_stderr, "\n");
643 do_cleanups (err_string_cleanup);
4ce44c66
JM
644 return_to_top_level (RETURN_ERROR);
645}
646
c906108c 647NORETURN void
c5aa993b 648error (const char *string,...)
c906108c
SS
649{
650 va_list args;
c906108c 651 va_start (args, string);
4ce44c66
JM
652 verror (string, args);
653 va_end (args);
c906108c
SS
654}
655
2acceee2 656NORETURN void
d9fcf2fb 657error_stream (struct ui_file *stream)
2acceee2 658{
4ce44c66 659 long size;
d9fcf2fb 660 char *msg = ui_file_xstrdup (stream, &size);
4ce44c66
JM
661 make_cleanup (free, msg);
662 error ("%s", msg);
2acceee2
JM
663}
664
665/* Get the last error message issued by gdb */
666
667char *
668error_last_message (void)
669{
4ce44c66 670 long len;
d9fcf2fb 671 return ui_file_xstrdup (gdb_lasterr, &len);
2acceee2 672}
4ce44c66 673
2acceee2
JM
674/* This is to be called by main() at the very beginning */
675
676void
677error_init (void)
678{
4ce44c66 679 gdb_lasterr = mem_fileopen ();
2acceee2 680}
c906108c 681
96baa820
JM
682/* Print a message reporting an internal error. Ask the user if they
683 want to continue, dump core, or just exit. */
c906108c 684
c906108c 685NORETURN void
4ce44c66 686internal_verror (const char *fmt, va_list ap)
c906108c 687{
96baa820
JM
688 static char msg[] = "Internal GDB error: recursive internal error.\n";
689 static int dejavu = 0;
7be570e7
JM
690 int continue_p;
691 int dump_core_p;
c906108c 692
96baa820
JM
693 /* don't allow infinite error recursion. */
694 switch (dejavu)
695 {
696 case 0:
697 dejavu = 1;
698 break;
699 case 1:
700 dejavu = 2;
701 fputs_unfiltered (msg, gdb_stderr);
702 abort ();
703 default:
704 dejavu = 3;
705 write (STDERR_FILENO, msg, sizeof (msg));
706 exit (1);
707 }
c906108c 708
96baa820 709 /* Try to get the message out */
4261bedc 710 target_terminal_ours ();
7be570e7 711 fputs_unfiltered ("gdb-internal-error: ", gdb_stderr);
4ce44c66 712 vfprintf_unfiltered (gdb_stderr, fmt, ap);
96baa820 713 fputs_unfiltered ("\n", gdb_stderr);
c906108c 714
7be570e7
JM
715 /* Default (no case) is to quit GDB. When in batch mode this
716 lessens the likelhood of GDB going into an infinate loop. */
717 continue_p = query ("\
718An internal GDB error was detected. This may make make further\n\
719debugging unreliable. Continue this debugging session? ");
720
721 /* Default (no case) is to not dump core. Lessen the chance of GDB
722 leaving random core files around. */
723 dump_core_p = query ("\
724Create a core file containing the current state of GDB? ");
725
726 if (continue_p)
727 {
728 if (dump_core_p)
729 {
730 if (fork () == 0)
731 abort ();
732 }
733 }
734 else
735 {
736 if (dump_core_p)
737 abort ();
738 else
739 exit (1);
740 }
96baa820
JM
741
742 dejavu = 0;
743 return_to_top_level (RETURN_ERROR);
c906108c
SS
744}
745
4ce44c66
JM
746NORETURN void
747internal_error (char *string, ...)
748{
749 va_list ap;
750 va_start (ap, string);
4261bedc 751
4ce44c66
JM
752 internal_verror (string, ap);
753 va_end (ap);
754}
755
c906108c
SS
756/* The strerror() function can return NULL for errno values that are
757 out of range. Provide a "safe" version that always returns a
758 printable string. */
759
760char *
761safe_strerror (errnum)
762 int errnum;
763{
764 char *msg;
765 static char buf[32];
766
767 if ((msg = strerror (errnum)) == NULL)
768 {
769 sprintf (buf, "(undocumented errno %d)", errnum);
770 msg = buf;
771 }
772 return (msg);
773}
774
c906108c
SS
775/* Print the system error message for errno, and also mention STRING
776 as the file name for which the error was encountered.
777 Then return to command level. */
778
779NORETURN void
780perror_with_name (string)
781 char *string;
782{
783 char *err;
784 char *combined;
785
786 err = safe_strerror (errno);
787 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
788 strcpy (combined, string);
789 strcat (combined, ": ");
790 strcat (combined, err);
791
792 /* I understand setting these is a matter of taste. Still, some people
793 may clear errno but not know about bfd_error. Doing this here is not
794 unreasonable. */
795 bfd_set_error (bfd_error_no_error);
796 errno = 0;
797
c5aa993b 798 error ("%s.", combined);
c906108c
SS
799}
800
801/* Print the system error message for ERRCODE, and also mention STRING
802 as the file name for which the error was encountered. */
803
804void
805print_sys_errmsg (string, errcode)
806 char *string;
807 int errcode;
808{
809 char *err;
810 char *combined;
811
812 err = safe_strerror (errcode);
813 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
814 strcpy (combined, string);
815 strcat (combined, ": ");
816 strcat (combined, err);
817
818 /* We want anything which was printed on stdout to come out first, before
819 this message. */
820 gdb_flush (gdb_stdout);
821 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
822}
823
824/* Control C eventually causes this to be called, at a convenient time. */
825
826void
827quit ()
828{
829 serial_t gdb_stdout_serial = serial_fdopen (1);
830
831 target_terminal_ours ();
832
833 /* We want all output to appear now, before we print "Quit". We
834 have 3 levels of buffering we have to flush (it's possible that
835 some of these should be changed to flush the lower-level ones
836 too): */
837
838 /* 1. The _filtered buffer. */
c5aa993b 839 wrap_here ((char *) 0);
c906108c
SS
840
841 /* 2. The stdio buffer. */
842 gdb_flush (gdb_stdout);
843 gdb_flush (gdb_stderr);
844
845 /* 3. The system-level buffer. */
846 SERIAL_DRAIN_OUTPUT (gdb_stdout_serial);
847 SERIAL_UN_FDOPEN (gdb_stdout_serial);
848
849 annotate_error_begin ();
850
851 /* Don't use *_filtered; we don't want to prompt the user to continue. */
852 if (quit_pre_print)
853 fprintf_unfiltered (gdb_stderr, quit_pre_print);
854
7be570e7
JM
855#ifdef __MSDOS__
856 /* No steenking SIGINT will ever be coming our way when the
857 program is resumed. Don't lie. */
858 fprintf_unfiltered (gdb_stderr, "Quit\n");
859#else
c906108c 860 if (job_control
c5aa993b
JM
861 /* If there is no terminal switching for this target, then we can't
862 possibly get screwed by the lack of job control. */
c906108c
SS
863 || current_target.to_terminal_ours == NULL)
864 fprintf_unfiltered (gdb_stderr, "Quit\n");
865 else
866 fprintf_unfiltered (gdb_stderr,
c5aa993b 867 "Quit (expect signal SIGINT when the program is resumed)\n");
7be570e7 868#endif
c906108c
SS
869 return_to_top_level (RETURN_QUIT);
870}
871
872
7be570e7 873#if defined(_MSC_VER) /* should test for wingdb instead? */
c906108c
SS
874
875/*
876 * Windows translates all keyboard and mouse events
877 * into a message which is appended to the message
878 * queue for the process.
879 */
880
c5aa993b
JM
881void
882notice_quit ()
c906108c 883{
c5aa993b 884 int k = win32pollquit ();
c906108c
SS
885 if (k == 1)
886 quit_flag = 1;
887 else if (k == 2)
888 immediate_quit = 1;
889}
890
4ce44c66 891#else /* !defined(_MSC_VER) */
c906108c 892
c5aa993b
JM
893void
894notice_quit ()
c906108c
SS
895{
896 /* Done by signals */
897}
898
4ce44c66 899#endif /* !defined(_MSC_VER) */
c906108c 900
c906108c 901/* Control C comes here */
c906108c
SS
902void
903request_quit (signo)
904 int signo;
905{
906 quit_flag = 1;
907 /* Restore the signal handler. Harmless with BSD-style signals, needed
908 for System V-style signals. So just always do it, rather than worrying
909 about USG defines and stuff like that. */
910 signal (signo, request_quit);
911
912#ifdef REQUEST_QUIT
913 REQUEST_QUIT;
914#else
c5aa993b 915 if (immediate_quit)
c906108c
SS
916 quit ();
917#endif
918}
c906108c
SS
919\f
920/* Memory management stuff (malloc friends). */
921
922/* Make a substitute size_t for non-ANSI compilers. */
923
924#ifndef HAVE_STDDEF_H
925#ifndef size_t
926#define size_t unsigned int
927#endif
928#endif
929
930#if !defined (USE_MMALLOC)
931
082faf24
AC
932PTR
933mcalloc (PTR md, size_t number, size_t size)
ed9a39eb
JM
934{
935 return calloc (number, size);
936}
937
c906108c
SS
938PTR
939mmalloc (md, size)
940 PTR md;
941 size_t size;
942{
943 return malloc (size);
944}
945
946PTR
947mrealloc (md, ptr, size)
948 PTR md;
949 PTR ptr;
950 size_t size;
951{
c5aa993b 952 if (ptr == 0) /* Guard against old realloc's */
c906108c
SS
953 return malloc (size);
954 else
955 return realloc (ptr, size);
956}
957
958void
959mfree (md, ptr)
960 PTR md;
961 PTR ptr;
962{
963 free (ptr);
964}
965
c5aa993b 966#endif /* USE_MMALLOC */
c906108c
SS
967
968#if !defined (USE_MMALLOC) || defined (NO_MMCHECK)
969
970void
082faf24 971init_malloc (void *md)
c906108c
SS
972{
973}
974
975#else /* Have mmalloc and want corruption checking */
976
977static void
978malloc_botch ()
979{
96baa820
JM
980 fprintf_unfiltered (gdb_stderr, "Memory corruption\n");
981 abort ();
c906108c
SS
982}
983
984/* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
985 by MD, to detect memory corruption. Note that MD may be NULL to specify
986 the default heap that grows via sbrk.
987
988 Note that for freshly created regions, we must call mmcheckf prior to any
989 mallocs in the region. Otherwise, any region which was allocated prior to
990 installing the checking hooks, which is later reallocated or freed, will
991 fail the checks! The mmcheck function only allows initial hooks to be
992 installed before the first mmalloc. However, anytime after we have called
993 mmcheck the first time to install the checking hooks, we can call it again
994 to update the function pointer to the memory corruption handler.
995
996 Returns zero on failure, non-zero on success. */
997
998#ifndef MMCHECK_FORCE
999#define MMCHECK_FORCE 0
1000#endif
1001
1002void
082faf24 1003init_malloc (void *md)
c906108c
SS
1004{
1005 if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE))
1006 {
1007 /* Don't use warning(), which relies on current_target being set
c5aa993b
JM
1008 to something other than dummy_target, until after
1009 initialize_all_files(). */
c906108c
SS
1010
1011 fprintf_unfiltered
1012 (gdb_stderr, "warning: failed to install memory consistency checks; ");
1013 fprintf_unfiltered
1014 (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n");
1015 }
1016
1017 mmtrace ();
1018}
1019
1020#endif /* Have mmalloc and want corruption checking */
1021
1022/* Called when a memory allocation fails, with the number of bytes of
1023 memory requested in SIZE. */
1024
1025NORETURN void
1026nomem (size)
1027 long size;
1028{
1029 if (size > 0)
1030 {
96baa820 1031 internal_error ("virtual memory exhausted: can't allocate %ld bytes.", size);
c906108c
SS
1032 }
1033 else
1034 {
96baa820 1035 internal_error ("virtual memory exhausted.");
c906108c
SS
1036 }
1037}
1038
1039/* Like mmalloc but get error if no storage available, and protect against
1040 the caller wanting to allocate zero bytes. Whether to return NULL for
1041 a zero byte request, or translate the request into a request for one
1042 byte of zero'd storage, is a religious issue. */
1043
1044PTR
1045xmmalloc (md, size)
1046 PTR md;
1047 long size;
1048{
1049 register PTR val;
1050
1051 if (size == 0)
1052 {
1053 val = NULL;
1054 }
1055 else if ((val = mmalloc (md, size)) == NULL)
1056 {
1057 nomem (size);
1058 }
1059 return (val);
1060}
1061
1062/* Like mrealloc but get error if no storage available. */
1063
1064PTR
1065xmrealloc (md, ptr, size)
1066 PTR md;
1067 PTR ptr;
1068 long size;
1069{
1070 register PTR val;
1071
1072 if (ptr != NULL)
1073 {
1074 val = mrealloc (md, ptr, size);
1075 }
1076 else
1077 {
1078 val = mmalloc (md, size);
1079 }
1080 if (val == NULL)
1081 {
1082 nomem (size);
1083 }
1084 return (val);
1085}
1086
1087/* Like malloc but get error if no storage available, and protect against
1088 the caller wanting to allocate zero bytes. */
1089
1090PTR
1091xmalloc (size)
1092 size_t size;
1093{
1094 return (xmmalloc ((PTR) NULL, size));
1095}
1096
ed9a39eb
JM
1097/* Like calloc but get error if no storage available */
1098
1099PTR
1100xcalloc (size_t number, size_t size)
1101{
1102 void *mem = mcalloc (NULL, number, size);
1103 if (mem == NULL)
1104 nomem (number * size);
1105 return mem;
1106}
1107
c906108c
SS
1108/* Like mrealloc but get error if no storage available. */
1109
1110PTR
1111xrealloc (ptr, size)
1112 PTR ptr;
1113 size_t size;
1114{
1115 return (xmrealloc ((PTR) NULL, ptr, size));
1116}
c906108c 1117\f
c5aa993b 1118
c906108c
SS
1119/* My replacement for the read system call.
1120 Used like `read' but keeps going if `read' returns too soon. */
1121
1122int
1123myread (desc, addr, len)
1124 int desc;
1125 char *addr;
1126 int len;
1127{
1128 register int val;
1129 int orglen = len;
1130
1131 while (len > 0)
1132 {
1133 val = read (desc, addr, len);
1134 if (val < 0)
1135 return val;
1136 if (val == 0)
1137 return orglen - len;
1138 len -= val;
1139 addr += val;
1140 }
1141 return orglen;
1142}
1143\f
1144/* Make a copy of the string at PTR with SIZE characters
1145 (and add a null character at the end in the copy).
1146 Uses malloc to get the space. Returns the address of the copy. */
1147
1148char *
1149savestring (ptr, size)
1150 const char *ptr;
1151 int size;
1152{
1153 register char *p = (char *) xmalloc (size + 1);
1154 memcpy (p, ptr, size);
1155 p[size] = 0;
1156 return p;
1157}
1158
1159char *
082faf24 1160msavestring (void *md, const char *ptr, int size)
c906108c
SS
1161{
1162 register char *p = (char *) xmmalloc (md, size + 1);
1163 memcpy (p, ptr, size);
1164 p[size] = 0;
1165 return p;
1166}
1167
1168/* The "const" is so it compiles under DGUX (which prototypes strsave
1169 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
1170 Doesn't real strsave return NULL if out of memory? */
1171char *
1172strsave (ptr)
1173 const char *ptr;
1174{
1175 return savestring (ptr, strlen (ptr));
1176}
1177
1178char *
082faf24 1179mstrsave (void *md, const char *ptr)
c906108c
SS
1180{
1181 return (msavestring (md, ptr, strlen (ptr)));
1182}
1183
1184void
1185print_spaces (n, file)
1186 register int n;
d9fcf2fb 1187 register struct ui_file *file;
c906108c 1188{
392a587b 1189 fputs_unfiltered (n_spaces (n), file);
c906108c
SS
1190}
1191
1192/* Print a host address. */
1193
1194void
d9fcf2fb 1195gdb_print_host_address (void *addr, struct ui_file *stream)
c906108c
SS
1196{
1197
1198 /* We could use the %p conversion specifier to fprintf if we had any
1199 way of knowing whether this host supports it. But the following
1200 should work on the Alpha and on 32 bit machines. */
1201
c5aa993b 1202 fprintf_filtered (stream, "0x%lx", (unsigned long) addr);
c906108c
SS
1203}
1204
1205/* Ask user a y-or-n question and return 1 iff answer is yes.
1206 Takes three args which are given to printf to print the question.
1207 The first, a control string, should end in "? ".
1208 It should not say how to answer, because we do that. */
1209
1210/* VARARGS */
1211int
c5aa993b 1212query (char *ctlstr,...)
c906108c
SS
1213{
1214 va_list args;
1215 register int answer;
1216 register int ans2;
1217 int retval;
1218
c906108c 1219 va_start (args, ctlstr);
c906108c
SS
1220
1221 if (query_hook)
1222 {
1223 return query_hook (ctlstr, args);
1224 }
1225
1226 /* Automatically answer "yes" if input is not from a terminal. */
1227 if (!input_from_terminal_p ())
1228 return 1;
1229#ifdef MPW
1230 /* FIXME Automatically answer "yes" if called from MacGDB. */
1231 if (mac_app)
1232 return 1;
1233#endif /* MPW */
1234
1235 while (1)
1236 {
1237 wrap_here (""); /* Flush any buffered output */
1238 gdb_flush (gdb_stdout);
1239
1240 if (annotation_level > 1)
1241 printf_filtered ("\n\032\032pre-query\n");
1242
1243 vfprintf_filtered (gdb_stdout, ctlstr, args);
1244 printf_filtered ("(y or n) ");
1245
1246 if (annotation_level > 1)
1247 printf_filtered ("\n\032\032query\n");
1248
1249#ifdef MPW
1250 /* If not in MacGDB, move to a new line so the entered line doesn't
c5aa993b 1251 have a prompt on the front of it. */
c906108c
SS
1252 if (!mac_app)
1253 fputs_unfiltered ("\n", gdb_stdout);
1254#endif /* MPW */
1255
c5aa993b 1256 wrap_here ("");
c906108c
SS
1257 gdb_flush (gdb_stdout);
1258
1259#if defined(TUI)
c5aa993b 1260 if (!tui_version || cmdWin == tuiWinWithFocus ())
c906108c
SS
1261#endif
1262 answer = fgetc (stdin);
1263#if defined(TUI)
1264 else
c5aa993b 1265 answer = (unsigned char) tuiBufferGetc ();
c906108c
SS
1266
1267#endif
1268 clearerr (stdin); /* in case of C-d */
1269 if (answer == EOF) /* C-d */
c5aa993b 1270 {
c906108c
SS
1271 retval = 1;
1272 break;
1273 }
1274 /* Eat rest of input line, to EOF or newline */
1275 if ((answer != '\n') || (tui_version && answer != '\r'))
c5aa993b 1276 do
c906108c
SS
1277 {
1278#if defined(TUI)
c5aa993b 1279 if (!tui_version || cmdWin == tuiWinWithFocus ())
c906108c
SS
1280#endif
1281 ans2 = fgetc (stdin);
1282#if defined(TUI)
1283 else
c5aa993b 1284 ans2 = (unsigned char) tuiBufferGetc ();
c906108c
SS
1285#endif
1286 clearerr (stdin);
1287 }
c5aa993b
JM
1288 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
1289 TUIDO (((TuiOpaqueFuncPtr) tui_vStartNewLines, 1));
c906108c
SS
1290
1291 if (answer >= 'a')
1292 answer -= 040;
1293 if (answer == 'Y')
1294 {
1295 retval = 1;
1296 break;
1297 }
1298 if (answer == 'N')
1299 {
1300 retval = 0;
1301 break;
1302 }
1303 printf_filtered ("Please answer y or n.\n");
1304 }
1305
1306 if (annotation_level > 1)
1307 printf_filtered ("\n\032\032post-query\n");
1308 return retval;
1309}
c906108c 1310\f
c5aa993b 1311
c906108c
SS
1312/* Parse a C escape sequence. STRING_PTR points to a variable
1313 containing a pointer to the string to parse. That pointer
1314 should point to the character after the \. That pointer
1315 is updated past the characters we use. The value of the
1316 escape sequence is returned.
1317
1318 A negative value means the sequence \ newline was seen,
1319 which is supposed to be equivalent to nothing at all.
1320
1321 If \ is followed by a null character, we return a negative
1322 value and leave the string pointer pointing at the null character.
1323
1324 If \ is followed by 000, we return 0 and leave the string pointer
1325 after the zeros. A value of 0 does not mean end of string. */
1326
1327int
1328parse_escape (string_ptr)
1329 char **string_ptr;
1330{
1331 register int c = *(*string_ptr)++;
1332 switch (c)
1333 {
1334 case 'a':
1335 return 007; /* Bell (alert) char */
1336 case 'b':
1337 return '\b';
1338 case 'e': /* Escape character */
1339 return 033;
1340 case 'f':
1341 return '\f';
1342 case 'n':
1343 return '\n';
1344 case 'r':
1345 return '\r';
1346 case 't':
1347 return '\t';
1348 case 'v':
1349 return '\v';
1350 case '\n':
1351 return -2;
1352 case 0:
1353 (*string_ptr)--;
1354 return 0;
1355 case '^':
1356 c = *(*string_ptr)++;
1357 if (c == '\\')
1358 c = parse_escape (string_ptr);
1359 if (c == '?')
1360 return 0177;
1361 return (c & 0200) | (c & 037);
c5aa993b 1362
c906108c
SS
1363 case '0':
1364 case '1':
1365 case '2':
1366 case '3':
1367 case '4':
1368 case '5':
1369 case '6':
1370 case '7':
1371 {
1372 register int i = c - '0';
1373 register int count = 0;
1374 while (++count < 3)
1375 {
1376 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
1377 {
1378 i *= 8;
1379 i += c - '0';
1380 }
1381 else
1382 {
1383 (*string_ptr)--;
1384 break;
1385 }
1386 }
1387 return i;
1388 }
1389 default:
1390 return c;
1391 }
1392}
1393\f
1394/* Print the character C on STREAM as part of the contents of a literal
1395 string whose delimiter is QUOTER. Note that this routine should only
1396 be call for printing things which are independent of the language
1397 of the program being debugged. */
1398
d9fcf2fb 1399static void printchar (int c, void (*do_fputs) (const char *, struct ui_file*), void (*do_fprintf) (struct ui_file*, const char *, ...), struct ui_file *stream, int quoter);
43e526b9
JM
1400
1401static void
1402printchar (c, do_fputs, do_fprintf, stream, quoter)
1403 int c;
507f3c78
KB
1404 void (*do_fputs) (const char *, struct ui_file *);
1405 void (*do_fprintf) (struct ui_file *, const char *, ...);
d9fcf2fb 1406 struct ui_file *stream;
c906108c
SS
1407 int quoter;
1408{
1409
1410 c &= 0xFF; /* Avoid sign bit follies */
1411
c5aa993b
JM
1412 if (c < 0x20 || /* Low control chars */
1413 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1414 (sevenbit_strings && c >= 0x80))
1415 { /* high order bit set */
1416 switch (c)
1417 {
1418 case '\n':
43e526b9 1419 do_fputs ("\\n", stream);
c5aa993b
JM
1420 break;
1421 case '\b':
43e526b9 1422 do_fputs ("\\b", stream);
c5aa993b
JM
1423 break;
1424 case '\t':
43e526b9 1425 do_fputs ("\\t", stream);
c5aa993b
JM
1426 break;
1427 case '\f':
43e526b9 1428 do_fputs ("\\f", stream);
c5aa993b
JM
1429 break;
1430 case '\r':
43e526b9 1431 do_fputs ("\\r", stream);
c5aa993b
JM
1432 break;
1433 case '\033':
43e526b9 1434 do_fputs ("\\e", stream);
c5aa993b
JM
1435 break;
1436 case '\007':
43e526b9 1437 do_fputs ("\\a", stream);
c5aa993b
JM
1438 break;
1439 default:
43e526b9 1440 do_fprintf (stream, "\\%.3o", (unsigned int) c);
c5aa993b
JM
1441 break;
1442 }
1443 }
1444 else
1445 {
1446 if (c == '\\' || c == quoter)
43e526b9
JM
1447 do_fputs ("\\", stream);
1448 do_fprintf (stream, "%c", c);
c5aa993b 1449 }
c906108c 1450}
43e526b9
JM
1451
1452/* Print the character C on STREAM as part of the contents of a
1453 literal string whose delimiter is QUOTER. Note that these routines
1454 should only be call for printing things which are independent of
1455 the language of the program being debugged. */
1456
1457void
1458fputstr_filtered (str, quoter, stream)
1459 const char *str;
1460 int quoter;
d9fcf2fb 1461 struct ui_file *stream;
43e526b9
JM
1462{
1463 while (*str)
1464 printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter);
1465}
1466
1467void
1468fputstr_unfiltered (str, quoter, stream)
1469 const char *str;
1470 int quoter;
d9fcf2fb 1471 struct ui_file *stream;
43e526b9
JM
1472{
1473 while (*str)
1474 printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1475}
1476
1477void
1478fputstrn_unfiltered (str, n, quoter, stream)
1479 const char *str;
1480 int n;
1481 int quoter;
d9fcf2fb 1482 struct ui_file *stream;
43e526b9
JM
1483{
1484 int i;
1485 for (i = 0; i < n; i++)
1486 printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1487}
1488
c906108c 1489\f
c5aa993b 1490
c906108c
SS
1491/* Number of lines per page or UINT_MAX if paging is disabled. */
1492static unsigned int lines_per_page;
e514a9d6 1493/* Number of chars per line or UNIT_MAX if line folding is disabled. */
c906108c
SS
1494static unsigned int chars_per_line;
1495/* Current count of lines printed on this page, chars on this line. */
1496static unsigned int lines_printed, chars_printed;
1497
1498/* Buffer and start column of buffered text, for doing smarter word-
1499 wrapping. When someone calls wrap_here(), we start buffering output
1500 that comes through fputs_filtered(). If we see a newline, we just
1501 spit it out and forget about the wrap_here(). If we see another
1502 wrap_here(), we spit it out and remember the newer one. If we see
1503 the end of the line, we spit out a newline, the indent, and then
1504 the buffered output. */
1505
1506/* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1507 are waiting to be output (they have already been counted in chars_printed).
1508 When wrap_buffer[0] is null, the buffer is empty. */
1509static char *wrap_buffer;
1510
1511/* Pointer in wrap_buffer to the next character to fill. */
1512static char *wrap_pointer;
1513
1514/* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1515 is non-zero. */
1516static char *wrap_indent;
1517
1518/* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1519 is not in effect. */
1520static int wrap_column;
c906108c 1521\f
c5aa993b 1522
c906108c
SS
1523/* Inialize the lines and chars per page */
1524void
c5aa993b 1525init_page_info ()
c906108c
SS
1526{
1527#if defined(TUI)
c5aa993b 1528 if (tui_version && m_winPtrNotNull (cmdWin))
c906108c
SS
1529 {
1530 lines_per_page = cmdWin->generic.height;
1531 chars_per_line = cmdWin->generic.width;
1532 }
1533 else
1534#endif
1535 {
1536 /* These defaults will be used if we are unable to get the correct
1537 values from termcap. */
1538#if defined(__GO32__)
c5aa993b
JM
1539 lines_per_page = ScreenRows ();
1540 chars_per_line = ScreenCols ();
1541#else
c906108c
SS
1542 lines_per_page = 24;
1543 chars_per_line = 80;
1544
1545#if !defined (MPW) && !defined (_WIN32)
1546 /* No termcap under MPW, although might be cool to do something
1547 by looking at worksheet or console window sizes. */
1548 /* Initialize the screen height and width from termcap. */
1549 {
c5aa993b 1550 char *termtype = getenv ("TERM");
c906108c 1551
c5aa993b
JM
1552 /* Positive means success, nonpositive means failure. */
1553 int status;
c906108c 1554
c5aa993b
JM
1555 /* 2048 is large enough for all known terminals, according to the
1556 GNU termcap manual. */
1557 char term_buffer[2048];
c906108c 1558
c5aa993b
JM
1559 if (termtype)
1560 {
c906108c
SS
1561 status = tgetent (term_buffer, termtype);
1562 if (status > 0)
1563 {
c5aa993b 1564 int val;
c906108c 1565 int running_in_emacs = getenv ("EMACS") != NULL;
c5aa993b
JM
1566
1567 val = tgetnum ("li");
1568 if (val >= 0 && !running_in_emacs)
1569 lines_per_page = val;
1570 else
1571 /* The number of lines per page is not mentioned
c906108c
SS
1572 in the terminal description. This probably means
1573 that paging is not useful (e.g. emacs shell window),
1574 so disable paging. */
c5aa993b
JM
1575 lines_per_page = UINT_MAX;
1576
1577 val = tgetnum ("co");
1578 if (val >= 0)
1579 chars_per_line = val;
c906108c 1580 }
c5aa993b 1581 }
c906108c
SS
1582 }
1583#endif /* MPW */
1584
1585#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1586
1587 /* If there is a better way to determine the window size, use it. */
1588 SIGWINCH_HANDLER (SIGWINCH);
1589#endif
1590#endif
1591 /* If the output is not a terminal, don't paginate it. */
d9fcf2fb 1592 if (!ui_file_isatty (gdb_stdout))
c5aa993b
JM
1593 lines_per_page = UINT_MAX;
1594 } /* the command_line_version */
1595 set_width ();
c906108c
SS
1596}
1597
1598static void
c5aa993b 1599set_width ()
c906108c
SS
1600{
1601 if (chars_per_line == 0)
c5aa993b 1602 init_page_info ();
c906108c
SS
1603
1604 if (!wrap_buffer)
1605 {
1606 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1607 wrap_buffer[0] = '\0';
1608 }
1609 else
1610 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
c5aa993b 1611 wrap_pointer = wrap_buffer; /* Start it at the beginning */
c906108c
SS
1612}
1613
1614/* ARGSUSED */
c5aa993b 1615static void
c906108c
SS
1616set_width_command (args, from_tty, c)
1617 char *args;
1618 int from_tty;
1619 struct cmd_list_element *c;
1620{
1621 set_width ();
1622}
1623
1624/* Wait, so the user can read what's on the screen. Prompt the user
1625 to continue by pressing RETURN. */
1626
1627static void
1628prompt_for_continue ()
1629{
1630 char *ignore;
1631 char cont_prompt[120];
1632
1633 if (annotation_level > 1)
1634 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1635
1636 strcpy (cont_prompt,
1637 "---Type <return> to continue, or q <return> to quit---");
1638 if (annotation_level > 1)
1639 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1640
1641 /* We must do this *before* we call gdb_readline, else it will eventually
1642 call us -- thinking that we're trying to print beyond the end of the
1643 screen. */
1644 reinitialize_more_filter ();
1645
1646 immediate_quit++;
1647 /* On a real operating system, the user can quit with SIGINT.
1648 But not on GO32.
1649
1650 'q' is provided on all systems so users don't have to change habits
1651 from system to system, and because telling them what to do in
1652 the prompt is more user-friendly than expecting them to think of
1653 SIGINT. */
1654 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1655 whereas control-C to gdb_readline will cause the user to get dumped
1656 out to DOS. */
1657 ignore = readline (cont_prompt);
1658
1659 if (annotation_level > 1)
1660 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1661
1662 if (ignore)
1663 {
1664 char *p = ignore;
1665 while (*p == ' ' || *p == '\t')
1666 ++p;
1667 if (p[0] == 'q')
0f71a2f6 1668 {
6426a772 1669 if (!event_loop_p)
0f71a2f6
JM
1670 request_quit (SIGINT);
1671 else
c5aa993b 1672 async_request_quit (0);
0f71a2f6 1673 }
c906108c
SS
1674 free (ignore);
1675 }
1676 immediate_quit--;
1677
1678 /* Now we have to do this again, so that GDB will know that it doesn't
1679 need to save the ---Type <return>--- line at the top of the screen. */
1680 reinitialize_more_filter ();
1681
1682 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1683}
1684
1685/* Reinitialize filter; ie. tell it to reset to original values. */
1686
1687void
1688reinitialize_more_filter ()
1689{
1690 lines_printed = 0;
1691 chars_printed = 0;
1692}
1693
1694/* Indicate that if the next sequence of characters overflows the line,
1695 a newline should be inserted here rather than when it hits the end.
1696 If INDENT is non-null, it is a string to be printed to indent the
1697 wrapped part on the next line. INDENT must remain accessible until
1698 the next call to wrap_here() or until a newline is printed through
1699 fputs_filtered().
1700
1701 If the line is already overfull, we immediately print a newline and
1702 the indentation, and disable further wrapping.
1703
1704 If we don't know the width of lines, but we know the page height,
1705 we must not wrap words, but should still keep track of newlines
1706 that were explicitly printed.
1707
1708 INDENT should not contain tabs, as that will mess up the char count
1709 on the next line. FIXME.
1710
1711 This routine is guaranteed to force out any output which has been
1712 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1713 used to force out output from the wrap_buffer. */
1714
1715void
c5aa993b 1716wrap_here (indent)
c906108c
SS
1717 char *indent;
1718{
1719 /* This should have been allocated, but be paranoid anyway. */
1720 if (!wrap_buffer)
1721 abort ();
1722
1723 if (wrap_buffer[0])
1724 {
1725 *wrap_pointer = '\0';
1726 fputs_unfiltered (wrap_buffer, gdb_stdout);
1727 }
1728 wrap_pointer = wrap_buffer;
1729 wrap_buffer[0] = '\0';
c5aa993b 1730 if (chars_per_line == UINT_MAX) /* No line overflow checking */
c906108c
SS
1731 {
1732 wrap_column = 0;
1733 }
1734 else if (chars_printed >= chars_per_line)
1735 {
1736 puts_filtered ("\n");
1737 if (indent != NULL)
1738 puts_filtered (indent);
1739 wrap_column = 0;
1740 }
1741 else
1742 {
1743 wrap_column = chars_printed;
1744 if (indent == NULL)
1745 wrap_indent = "";
1746 else
1747 wrap_indent = indent;
1748 }
1749}
1750
1751/* Ensure that whatever gets printed next, using the filtered output
1752 commands, starts at the beginning of the line. I.E. if there is
1753 any pending output for the current line, flush it and start a new
1754 line. Otherwise do nothing. */
1755
1756void
1757begin_line ()
1758{
1759 if (chars_printed > 0)
1760 {
1761 puts_filtered ("\n");
1762 }
1763}
1764
ac9a91a7 1765
c906108c
SS
1766/* Like fputs but if FILTER is true, pause after every screenful.
1767
1768 Regardless of FILTER can wrap at points other than the final
1769 character of a line.
1770
1771 Unlike fputs, fputs_maybe_filtered does not return a value.
1772 It is OK for LINEBUFFER to be NULL, in which case just don't print
1773 anything.
1774
1775 Note that a longjmp to top level may occur in this routine (only if
1776 FILTER is true) (since prompt_for_continue may do so) so this
1777 routine should not be called when cleanups are not in place. */
1778
1779static void
1780fputs_maybe_filtered (linebuffer, stream, filter)
1781 const char *linebuffer;
d9fcf2fb 1782 struct ui_file *stream;
c906108c
SS
1783 int filter;
1784{
1785 const char *lineptr;
1786
1787 if (linebuffer == 0)
1788 return;
1789
1790 /* Don't do any filtering if it is disabled. */
7a292a7a 1791 if ((stream != gdb_stdout) || !pagination_enabled
c5aa993b 1792 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
c906108c
SS
1793 {
1794 fputs_unfiltered (linebuffer, stream);
1795 return;
1796 }
1797
1798 /* Go through and output each character. Show line extension
1799 when this is necessary; prompt user for new page when this is
1800 necessary. */
c5aa993b 1801
c906108c
SS
1802 lineptr = linebuffer;
1803 while (*lineptr)
1804 {
1805 /* Possible new page. */
1806 if (filter &&
1807 (lines_printed >= lines_per_page - 1))
1808 prompt_for_continue ();
1809
1810 while (*lineptr && *lineptr != '\n')
1811 {
1812 /* Print a single line. */
1813 if (*lineptr == '\t')
1814 {
1815 if (wrap_column)
1816 *wrap_pointer++ = '\t';
1817 else
1818 fputc_unfiltered ('\t', stream);
1819 /* Shifting right by 3 produces the number of tab stops
1820 we have already passed, and then adding one and
c5aa993b 1821 shifting left 3 advances to the next tab stop. */
c906108c
SS
1822 chars_printed = ((chars_printed >> 3) + 1) << 3;
1823 lineptr++;
1824 }
1825 else
1826 {
1827 if (wrap_column)
1828 *wrap_pointer++ = *lineptr;
1829 else
c5aa993b 1830 fputc_unfiltered (*lineptr, stream);
c906108c
SS
1831 chars_printed++;
1832 lineptr++;
1833 }
c5aa993b 1834
c906108c
SS
1835 if (chars_printed >= chars_per_line)
1836 {
1837 unsigned int save_chars = chars_printed;
1838
1839 chars_printed = 0;
1840 lines_printed++;
1841 /* If we aren't actually wrapping, don't output newline --
c5aa993b
JM
1842 if chars_per_line is right, we probably just overflowed
1843 anyway; if it's wrong, let us keep going. */
c906108c
SS
1844 if (wrap_column)
1845 fputc_unfiltered ('\n', stream);
1846
1847 /* Possible new page. */
1848 if (lines_printed >= lines_per_page - 1)
1849 prompt_for_continue ();
1850
1851 /* Now output indentation and wrapped string */
1852 if (wrap_column)
1853 {
1854 fputs_unfiltered (wrap_indent, stream);
c5aa993b
JM
1855 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1856 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
c906108c
SS
1857 /* FIXME, this strlen is what prevents wrap_indent from
1858 containing tabs. However, if we recurse to print it
1859 and count its chars, we risk trouble if wrap_indent is
1860 longer than (the user settable) chars_per_line.
1861 Note also that this can set chars_printed > chars_per_line
1862 if we are printing a long string. */
1863 chars_printed = strlen (wrap_indent)
c5aa993b 1864 + (save_chars - wrap_column);
c906108c
SS
1865 wrap_pointer = wrap_buffer; /* Reset buffer */
1866 wrap_buffer[0] = '\0';
c5aa993b
JM
1867 wrap_column = 0; /* And disable fancy wrap */
1868 }
c906108c
SS
1869 }
1870 }
1871
1872 if (*lineptr == '\n')
1873 {
1874 chars_printed = 0;
c5aa993b 1875 wrap_here ((char *) 0); /* Spit out chars, cancel further wraps */
c906108c
SS
1876 lines_printed++;
1877 fputc_unfiltered ('\n', stream);
1878 lineptr++;
1879 }
1880 }
1881}
1882
1883void
1884fputs_filtered (linebuffer, stream)
1885 const char *linebuffer;
d9fcf2fb 1886 struct ui_file *stream;
c906108c
SS
1887{
1888 fputs_maybe_filtered (linebuffer, stream, 1);
1889}
1890
1891int
1892putchar_unfiltered (c)
1893 int c;
1894{
11cf8741 1895 char buf = c;
d9fcf2fb 1896 ui_file_write (gdb_stdout, &buf, 1);
c906108c
SS
1897 return c;
1898}
1899
1900int
1901fputc_unfiltered (c, stream)
1902 int c;
d9fcf2fb 1903 struct ui_file *stream;
c906108c 1904{
11cf8741 1905 char buf = c;
d9fcf2fb 1906 ui_file_write (stream, &buf, 1);
c906108c
SS
1907 return c;
1908}
1909
1910int
1911fputc_filtered (c, stream)
1912 int c;
d9fcf2fb 1913 struct ui_file *stream;
c906108c
SS
1914{
1915 char buf[2];
1916
1917 buf[0] = c;
1918 buf[1] = 0;
1919 fputs_filtered (buf, stream);
1920 return c;
1921}
1922
1923/* puts_debug is like fputs_unfiltered, except it prints special
1924 characters in printable fashion. */
1925
1926void
1927puts_debug (prefix, string, suffix)
1928 char *prefix;
1929 char *string;
1930 char *suffix;
1931{
1932 int ch;
1933
1934 /* Print prefix and suffix after each line. */
1935 static int new_line = 1;
1936 static int return_p = 0;
1937 static char *prev_prefix = "";
1938 static char *prev_suffix = "";
1939
1940 if (*string == '\n')
1941 return_p = 0;
1942
1943 /* If the prefix is changing, print the previous suffix, a new line,
1944 and the new prefix. */
c5aa993b 1945 if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line)
c906108c 1946 {
9846de1b
JM
1947 fputs_unfiltered (prev_suffix, gdb_stdlog);
1948 fputs_unfiltered ("\n", gdb_stdlog);
1949 fputs_unfiltered (prefix, gdb_stdlog);
c906108c
SS
1950 }
1951
1952 /* Print prefix if we printed a newline during the previous call. */
1953 if (new_line)
1954 {
1955 new_line = 0;
9846de1b 1956 fputs_unfiltered (prefix, gdb_stdlog);
c906108c
SS
1957 }
1958
1959 prev_prefix = prefix;
1960 prev_suffix = suffix;
1961
1962 /* Output characters in a printable format. */
1963 while ((ch = *string++) != '\0')
1964 {
1965 switch (ch)
c5aa993b 1966 {
c906108c
SS
1967 default:
1968 if (isprint (ch))
9846de1b 1969 fputc_unfiltered (ch, gdb_stdlog);
c906108c
SS
1970
1971 else
9846de1b 1972 fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff);
c906108c
SS
1973 break;
1974
c5aa993b
JM
1975 case '\\':
1976 fputs_unfiltered ("\\\\", gdb_stdlog);
1977 break;
1978 case '\b':
1979 fputs_unfiltered ("\\b", gdb_stdlog);
1980 break;
1981 case '\f':
1982 fputs_unfiltered ("\\f", gdb_stdlog);
1983 break;
1984 case '\n':
1985 new_line = 1;
1986 fputs_unfiltered ("\\n", gdb_stdlog);
1987 break;
1988 case '\r':
1989 fputs_unfiltered ("\\r", gdb_stdlog);
1990 break;
1991 case '\t':
1992 fputs_unfiltered ("\\t", gdb_stdlog);
1993 break;
1994 case '\v':
1995 fputs_unfiltered ("\\v", gdb_stdlog);
1996 break;
1997 }
c906108c
SS
1998
1999 return_p = ch == '\r';
2000 }
2001
2002 /* Print suffix if we printed a newline. */
2003 if (new_line)
2004 {
9846de1b
JM
2005 fputs_unfiltered (suffix, gdb_stdlog);
2006 fputs_unfiltered ("\n", gdb_stdlog);
c906108c
SS
2007 }
2008}
2009
2010
2011/* Print a variable number of ARGS using format FORMAT. If this
2012 information is going to put the amount written (since the last call
2013 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
2014 call prompt_for_continue to get the users permision to continue.
2015
2016 Unlike fprintf, this function does not return a value.
2017
2018 We implement three variants, vfprintf (takes a vararg list and stream),
2019 fprintf (takes a stream to write on), and printf (the usual).
2020
2021 Note also that a longjmp to top level may occur in this routine
2022 (since prompt_for_continue may do so) so this routine should not be
2023 called when cleanups are not in place. */
2024
2025static void
2026vfprintf_maybe_filtered (stream, format, args, filter)
d9fcf2fb 2027 struct ui_file *stream;
c906108c
SS
2028 const char *format;
2029 va_list args;
2030 int filter;
2031{
2032 char *linebuffer;
2033 struct cleanup *old_cleanups;
2034
2035 vasprintf (&linebuffer, format, args);
2036 if (linebuffer == NULL)
2037 {
2038 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
2039 exit (1);
2040 }
2041 old_cleanups = make_cleanup (free, linebuffer);
2042 fputs_maybe_filtered (linebuffer, stream, filter);
2043 do_cleanups (old_cleanups);
2044}
2045
2046
2047void
2048vfprintf_filtered (stream, format, args)
d9fcf2fb 2049 struct ui_file *stream;
c906108c
SS
2050 const char *format;
2051 va_list args;
2052{
2053 vfprintf_maybe_filtered (stream, format, args, 1);
2054}
2055
2056void
2057vfprintf_unfiltered (stream, format, args)
d9fcf2fb 2058 struct ui_file *stream;
c906108c
SS
2059 const char *format;
2060 va_list args;
2061{
2062 char *linebuffer;
2063 struct cleanup *old_cleanups;
2064
2065 vasprintf (&linebuffer, format, args);
2066 if (linebuffer == NULL)
2067 {
2068 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
2069 exit (1);
2070 }
2071 old_cleanups = make_cleanup (free, linebuffer);
2072 fputs_unfiltered (linebuffer, stream);
2073 do_cleanups (old_cleanups);
2074}
2075
2076void
2077vprintf_filtered (format, args)
2078 const char *format;
2079 va_list args;
2080{
2081 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
2082}
2083
2084void
2085vprintf_unfiltered (format, args)
2086 const char *format;
2087 va_list args;
2088{
2089 vfprintf_unfiltered (gdb_stdout, format, args);
2090}
2091
c906108c 2092void
d9fcf2fb 2093fprintf_filtered (struct ui_file * stream, const char *format,...)
c906108c
SS
2094{
2095 va_list args;
c906108c 2096 va_start (args, format);
c906108c
SS
2097 vfprintf_filtered (stream, format, args);
2098 va_end (args);
2099}
2100
c906108c 2101void
d9fcf2fb 2102fprintf_unfiltered (struct ui_file * stream, const char *format,...)
c906108c
SS
2103{
2104 va_list args;
c906108c 2105 va_start (args, format);
c906108c
SS
2106 vfprintf_unfiltered (stream, format, args);
2107 va_end (args);
2108}
2109
2110/* Like fprintf_filtered, but prints its result indented.
2111 Called as fprintfi_filtered (spaces, stream, format, ...); */
2112
c906108c 2113void
d9fcf2fb 2114fprintfi_filtered (int spaces, struct ui_file * stream, const char *format,...)
c906108c
SS
2115{
2116 va_list args;
c906108c 2117 va_start (args, format);
c906108c
SS
2118 print_spaces_filtered (spaces, stream);
2119
2120 vfprintf_filtered (stream, format, args);
2121 va_end (args);
2122}
2123
2124
c906108c 2125void
c5aa993b 2126printf_filtered (const char *format,...)
c906108c
SS
2127{
2128 va_list args;
c906108c 2129 va_start (args, format);
c906108c
SS
2130 vfprintf_filtered (gdb_stdout, format, args);
2131 va_end (args);
2132}
2133
2134
c906108c 2135void
c5aa993b 2136printf_unfiltered (const char *format,...)
c906108c
SS
2137{
2138 va_list args;
c906108c 2139 va_start (args, format);
c906108c
SS
2140 vfprintf_unfiltered (gdb_stdout, format, args);
2141 va_end (args);
2142}
2143
2144/* Like printf_filtered, but prints it's result indented.
2145 Called as printfi_filtered (spaces, format, ...); */
2146
c906108c 2147void
c5aa993b 2148printfi_filtered (int spaces, const char *format,...)
c906108c
SS
2149{
2150 va_list args;
c906108c 2151 va_start (args, format);
c906108c
SS
2152 print_spaces_filtered (spaces, gdb_stdout);
2153 vfprintf_filtered (gdb_stdout, format, args);
2154 va_end (args);
2155}
2156
2157/* Easy -- but watch out!
2158
2159 This routine is *not* a replacement for puts()! puts() appends a newline.
2160 This one doesn't, and had better not! */
2161
2162void
2163puts_filtered (string)
2164 const char *string;
2165{
2166 fputs_filtered (string, gdb_stdout);
2167}
2168
2169void
2170puts_unfiltered (string)
2171 const char *string;
2172{
2173 fputs_unfiltered (string, gdb_stdout);
2174}
2175
2176/* Return a pointer to N spaces and a null. The pointer is good
2177 until the next call to here. */
2178char *
2179n_spaces (n)
2180 int n;
2181{
392a587b
JM
2182 char *t;
2183 static char *spaces = 0;
2184 static int max_spaces = -1;
c906108c
SS
2185
2186 if (n > max_spaces)
2187 {
2188 if (spaces)
2189 free (spaces);
c5aa993b
JM
2190 spaces = (char *) xmalloc (n + 1);
2191 for (t = spaces + n; t != spaces;)
c906108c
SS
2192 *--t = ' ';
2193 spaces[n] = '\0';
2194 max_spaces = n;
2195 }
2196
2197 return spaces + max_spaces - n;
2198}
2199
2200/* Print N spaces. */
2201void
2202print_spaces_filtered (n, stream)
2203 int n;
d9fcf2fb 2204 struct ui_file *stream;
c906108c
SS
2205{
2206 fputs_filtered (n_spaces (n), stream);
2207}
2208\f
2209/* C++ demangler stuff. */
2210
2211/* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
2212 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
2213 If the name is not mangled, or the language for the name is unknown, or
2214 demangling is off, the name is printed in its "raw" form. */
2215
2216void
2217fprintf_symbol_filtered (stream, name, lang, arg_mode)
d9fcf2fb 2218 struct ui_file *stream;
c906108c
SS
2219 char *name;
2220 enum language lang;
2221 int arg_mode;
2222{
2223 char *demangled;
2224
2225 if (name != NULL)
2226 {
2227 /* If user wants to see raw output, no problem. */
2228 if (!demangle)
2229 {
2230 fputs_filtered (name, stream);
2231 }
2232 else
2233 {
2234 switch (lang)
2235 {
2236 case language_cplus:
2237 demangled = cplus_demangle (name, arg_mode);
2238 break;
2239 case language_java:
2240 demangled = cplus_demangle (name, arg_mode | DMGL_JAVA);
2241 break;
2242 case language_chill:
2243 demangled = chill_demangle (name);
2244 break;
2245 default:
2246 demangled = NULL;
2247 break;
2248 }
2249 fputs_filtered (demangled ? demangled : name, stream);
2250 if (demangled != NULL)
2251 {
2252 free (demangled);
2253 }
2254 }
2255 }
2256}
2257
2258/* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
2259 differences in whitespace. Returns 0 if they match, non-zero if they
2260 don't (slightly different than strcmp()'s range of return values).
c5aa993b 2261
c906108c
SS
2262 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2263 This "feature" is useful when searching for matching C++ function names
2264 (such as if the user types 'break FOO', where FOO is a mangled C++
2265 function). */
2266
2267int
2268strcmp_iw (string1, string2)
2269 const char *string1;
2270 const char *string2;
2271{
2272 while ((*string1 != '\0') && (*string2 != '\0'))
2273 {
2274 while (isspace (*string1))
2275 {
2276 string1++;
2277 }
2278 while (isspace (*string2))
2279 {
2280 string2++;
2281 }
2282 if (*string1 != *string2)
2283 {
2284 break;
2285 }
2286 if (*string1 != '\0')
2287 {
2288 string1++;
2289 string2++;
2290 }
2291 }
2292 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
2293}
c906108c 2294\f
c5aa993b 2295
c906108c 2296/*
c5aa993b
JM
2297 ** subset_compare()
2298 ** Answer whether string_to_compare is a full or partial match to
2299 ** template_string. The partial match must be in sequence starting
2300 ** at index 0.
2301 */
c906108c 2302int
7a292a7a 2303subset_compare (string_to_compare, template_string)
c5aa993b
JM
2304 char *string_to_compare;
2305 char *template_string;
7a292a7a
SS
2306{
2307 int match;
c5aa993b
JM
2308 if (template_string != (char *) NULL && string_to_compare != (char *) NULL &&
2309 strlen (string_to_compare) <= strlen (template_string))
2310 match = (strncmp (template_string,
2311 string_to_compare,
2312 strlen (string_to_compare)) == 0);
7a292a7a
SS
2313 else
2314 match = 0;
2315 return match;
2316}
c906108c
SS
2317
2318
a14ed312 2319static void pagination_on_command (char *arg, int from_tty);
7a292a7a
SS
2320static void
2321pagination_on_command (arg, from_tty)
c5aa993b
JM
2322 char *arg;
2323 int from_tty;
c906108c
SS
2324{
2325 pagination_enabled = 1;
2326}
2327
a14ed312 2328static void pagination_on_command (char *arg, int from_tty);
7a292a7a
SS
2329static void
2330pagination_off_command (arg, from_tty)
c5aa993b
JM
2331 char *arg;
2332 int from_tty;
c906108c
SS
2333{
2334 pagination_enabled = 0;
2335}
c906108c 2336\f
c5aa993b 2337
c906108c
SS
2338void
2339initialize_utils ()
2340{
2341 struct cmd_list_element *c;
2342
c5aa993b
JM
2343 c = add_set_cmd ("width", class_support, var_uinteger,
2344 (char *) &chars_per_line,
2345 "Set number of characters gdb thinks are in a line.",
2346 &setlist);
c906108c
SS
2347 add_show_from_set (c, &showlist);
2348 c->function.sfunc = set_width_command;
2349
2350 add_show_from_set
2351 (add_set_cmd ("height", class_support,
c5aa993b 2352 var_uinteger, (char *) &lines_per_page,
c906108c
SS
2353 "Set number of lines gdb thinks are in a page.", &setlist),
2354 &showlist);
c5aa993b 2355
c906108c
SS
2356 init_page_info ();
2357
2358 /* If the output is not a terminal, don't paginate it. */
d9fcf2fb 2359 if (!ui_file_isatty (gdb_stdout))
c906108c
SS
2360 lines_per_page = UINT_MAX;
2361
c5aa993b 2362 set_width_command ((char *) NULL, 0, c);
c906108c
SS
2363
2364 add_show_from_set
c5aa993b
JM
2365 (add_set_cmd ("demangle", class_support, var_boolean,
2366 (char *) &demangle,
2367 "Set demangling of encoded C++ names when displaying symbols.",
c906108c
SS
2368 &setprintlist),
2369 &showprintlist);
2370
2371 add_show_from_set
2372 (add_set_cmd ("pagination", class_support,
c5aa993b 2373 var_boolean, (char *) &pagination_enabled,
c906108c
SS
2374 "Set state of pagination.", &setlist),
2375 &showlist);
4261bedc 2376
c906108c
SS
2377 if (xdb_commands)
2378 {
c5aa993b
JM
2379 add_com ("am", class_support, pagination_on_command,
2380 "Enable pagination");
2381 add_com ("sm", class_support, pagination_off_command,
2382 "Disable pagination");
c906108c
SS
2383 }
2384
2385 add_show_from_set
c5aa993b
JM
2386 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
2387 (char *) &sevenbit_strings,
2388 "Set printing of 8-bit characters in strings as \\nnn.",
c906108c
SS
2389 &setprintlist),
2390 &showprintlist);
2391
2392 add_show_from_set
c5aa993b
JM
2393 (add_set_cmd ("asm-demangle", class_support, var_boolean,
2394 (char *) &asm_demangle,
2395 "Set demangling of C++ names in disassembly listings.",
c906108c
SS
2396 &setprintlist),
2397 &showprintlist);
2398}
2399
2400/* Machine specific function to handle SIGWINCH signal. */
2401
2402#ifdef SIGWINCH_HANDLER_BODY
c5aa993b 2403SIGWINCH_HANDLER_BODY
c906108c
SS
2404#endif
2405\f
2406/* Support for converting target fp numbers into host DOUBLEST format. */
2407
2408/* XXX - This code should really be in libiberty/floatformat.c, however
2409 configuration issues with libiberty made this very difficult to do in the
2410 available time. */
2411
2412#include "floatformat.h"
2413#include <math.h> /* ldexp */
2414
2415/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
2416 going to bother with trying to muck around with whether it is defined in
2417 a system header, what we do if not, etc. */
2418#define FLOATFORMAT_CHAR_BIT 8
2419
a14ed312
KB
2420static unsigned long get_field (unsigned char *,
2421 enum floatformat_byteorders,
2422 unsigned int, unsigned int, unsigned int);
c906108c
SS
2423
2424/* Extract a field which starts at START and is LEN bytes long. DATA and
2425 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2426static unsigned long
2427get_field (data, order, total_len, start, len)
2428 unsigned char *data;
2429 enum floatformat_byteorders order;
2430 unsigned int total_len;
2431 unsigned int start;
2432 unsigned int len;
2433{
2434 unsigned long result;
2435 unsigned int cur_byte;
2436 int cur_bitshift;
2437
2438 /* Start at the least significant part of the field. */
c906108c 2439 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
0fda6bd2
JM
2440 {
2441 /* We start counting from the other end (i.e, from the high bytes
2442 rather than the low bytes). As such, we need to be concerned
2443 with what happens if bit 0 doesn't start on a byte boundary.
2444 I.e, we need to properly handle the case where total_len is
2445 not evenly divisible by 8. So we compute ``excess'' which
2446 represents the number of bits from the end of our starting
2447 byte needed to get to bit 0. */
2448 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
2449 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
2450 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
2451 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
2452 - FLOATFORMAT_CHAR_BIT;
2453 }
2454 else
2455 {
2456 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
2457 cur_bitshift =
2458 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
2459 }
2460 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
2461 result = *(data + cur_byte) >> (-cur_bitshift);
2462 else
2463 result = 0;
c906108c
SS
2464 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2465 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2466 ++cur_byte;
2467 else
2468 --cur_byte;
2469
2470 /* Move towards the most significant part of the field. */
2471 while (cur_bitshift < len)
2472 {
0fda6bd2 2473 result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
c906108c
SS
2474 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2475 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2476 ++cur_byte;
2477 else
2478 --cur_byte;
2479 }
0fda6bd2
JM
2480 if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
2481 /* Mask out bits which are not part of the field */
2482 result &= ((1UL << len) - 1);
c906108c
SS
2483 return result;
2484}
c5aa993b 2485
c906108c
SS
2486/* Convert from FMT to a DOUBLEST.
2487 FROM is the address of the extended float.
2488 Store the DOUBLEST in *TO. */
2489
2490void
2491floatformat_to_doublest (fmt, from, to)
2492 const struct floatformat *fmt;
2493 char *from;
2494 DOUBLEST *to;
2495{
c5aa993b 2496 unsigned char *ufrom = (unsigned char *) from;
c906108c
SS
2497 DOUBLEST dto;
2498 long exponent;
2499 unsigned long mant;
2500 unsigned int mant_bits, mant_off;
2501 int mant_bits_left;
2502 int special_exponent; /* It's a NaN, denorm or zero */
2503
2504 /* If the mantissa bits are not contiguous from one end of the
2505 mantissa to the other, we need to make a private copy of the
2506 source bytes that is in the right order since the unpacking
2507 algorithm assumes that the bits are contiguous.
2508
2509 Swap the bytes individually rather than accessing them through
2510 "long *" since we have no guarantee that they start on a long
2511 alignment, and also sizeof(long) for the host could be different
2512 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
2513 for the target is 4. */
2514
c5aa993b 2515 if (fmt->byteorder == floatformat_littlebyte_bigword)
c906108c
SS
2516 {
2517 static unsigned char *newfrom;
2518 unsigned char *swapin, *swapout;
2519 int longswaps;
2520
c5aa993b 2521 longswaps = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
c906108c 2522 longswaps >>= 3;
c5aa993b 2523
c906108c
SS
2524 if (newfrom == NULL)
2525 {
c5aa993b 2526 newfrom = (unsigned char *) xmalloc (fmt->totalsize);
c906108c
SS
2527 }
2528 swapout = newfrom;
2529 swapin = ufrom;
2530 ufrom = newfrom;
2531 while (longswaps-- > 0)
2532 {
2533 /* This is ugly, but efficient */
2534 *swapout++ = swapin[4];
2535 *swapout++ = swapin[5];
2536 *swapout++ = swapin[6];
2537 *swapout++ = swapin[7];
2538 *swapout++ = swapin[0];
2539 *swapout++ = swapin[1];
2540 *swapout++ = swapin[2];
2541 *swapout++ = swapin[3];
2542 swapin += 8;
2543 }
2544 }
2545
2546 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
2547 fmt->exp_start, fmt->exp_len);
2548 /* Note that if exponent indicates a NaN, we can't really do anything useful
2549 (not knowing if the host has NaN's, or how to build one). So it will
2550 end up as an infinity or something close; that is OK. */
2551
2552 mant_bits_left = fmt->man_len;
2553 mant_off = fmt->man_start;
2554 dto = 0.0;
2555
2556 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
2557
11cf8741
JM
2558/* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
2559 we don't check for zero as the exponent doesn't matter. */
c906108c
SS
2560 if (!special_exponent)
2561 exponent -= fmt->exp_bias;
11cf8741
JM
2562 else if (exponent == 0)
2563 exponent = 1 - fmt->exp_bias;
c906108c
SS
2564
2565 /* Build the result algebraically. Might go infinite, underflow, etc;
2566 who cares. */
2567
2568/* If this format uses a hidden bit, explicitly add it in now. Otherwise,
2569 increment the exponent by one to account for the integer bit. */
2570
2571 if (!special_exponent)
7a292a7a
SS
2572 {
2573 if (fmt->intbit == floatformat_intbit_no)
2574 dto = ldexp (1.0, exponent);
2575 else
2576 exponent++;
2577 }
c906108c
SS
2578
2579 while (mant_bits_left > 0)
2580 {
2581 mant_bits = min (mant_bits_left, 32);
2582
2583 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
c5aa993b 2584 mant_off, mant_bits);
c906108c 2585
c5aa993b 2586 dto += ldexp ((double) mant, exponent - mant_bits);
c906108c
SS
2587 exponent -= mant_bits;
2588 mant_off += mant_bits;
2589 mant_bits_left -= mant_bits;
2590 }
2591
2592 /* Negate it if negative. */
2593 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
2594 dto = -dto;
2595 *to = dto;
2596}
2597\f
a14ed312
KB
2598static void put_field (unsigned char *, enum floatformat_byteorders,
2599 unsigned int,
2600 unsigned int, unsigned int, unsigned long);
c906108c
SS
2601
2602/* Set a field which starts at START and is LEN bytes long. DATA and
2603 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2604static void
2605put_field (data, order, total_len, start, len, stuff_to_put)
2606 unsigned char *data;
2607 enum floatformat_byteorders order;
2608 unsigned int total_len;
2609 unsigned int start;
2610 unsigned int len;
2611 unsigned long stuff_to_put;
2612{
2613 unsigned int cur_byte;
2614 int cur_bitshift;
2615
2616 /* Start at the least significant part of the field. */
c906108c 2617 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
0fda6bd2
JM
2618 {
2619 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
2620 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
2621 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
2622 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
2623 - FLOATFORMAT_CHAR_BIT;
2624 }
2625 else
2626 {
2627 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
2628 cur_bitshift =
2629 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
2630 }
2631 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
2632 {
2633 *(data + cur_byte) &=
2634 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
2635 << (-cur_bitshift));
2636 *(data + cur_byte) |=
2637 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
2638 }
c906108c
SS
2639 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2640 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2641 ++cur_byte;
2642 else
2643 --cur_byte;
2644
2645 /* Move towards the most significant part of the field. */
2646 while (cur_bitshift < len)
2647 {
2648 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
2649 {
2650 /* This is the last byte. */
2651 *(data + cur_byte) &=
2652 ~((1 << (len - cur_bitshift)) - 1);
2653 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
2654 }
2655 else
2656 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
2657 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
2658 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2659 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2660 ++cur_byte;
2661 else
2662 --cur_byte;
2663 }
2664}
2665
2666#ifdef HAVE_LONG_DOUBLE
2667/* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
2668 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
2669 frexp, but operates on the long double data type. */
2670
a14ed312 2671static long double ldfrexp (long double value, int *eptr);
c906108c
SS
2672
2673static long double
2674ldfrexp (value, eptr)
2675 long double value;
2676 int *eptr;
2677{
2678 long double tmp;
2679 int exp;
2680
2681 /* Unfortunately, there are no portable functions for extracting the exponent
2682 of a long double, so we have to do it iteratively by multiplying or dividing
2683 by two until the fraction is between 0.5 and 1.0. */
2684
2685 if (value < 0.0l)
2686 value = -value;
2687
2688 tmp = 1.0l;
2689 exp = 0;
2690
2691 if (value >= tmp) /* Value >= 1.0 */
2692 while (value >= tmp)
2693 {
2694 tmp *= 2.0l;
2695 exp++;
2696 }
2697 else if (value != 0.0l) /* Value < 1.0 and > 0.0 */
2698 {
2699 while (value < tmp)
2700 {
2701 tmp /= 2.0l;
2702 exp--;
2703 }
2704 tmp *= 2.0l;
2705 exp++;
2706 }
2707
2708 *eptr = exp;
c5aa993b 2709 return value / tmp;
c906108c
SS
2710}
2711#endif /* HAVE_LONG_DOUBLE */
2712
2713
2714/* The converse: convert the DOUBLEST *FROM to an extended float
2715 and store where TO points. Neither FROM nor TO have any alignment
2716 restrictions. */
2717
2718void
2719floatformat_from_doublest (fmt, from, to)
2720 CONST struct floatformat *fmt;
2721 DOUBLEST *from;
2722 char *to;
2723{
2724 DOUBLEST dfrom;
2725 int exponent;
2726 DOUBLEST mant;
2727 unsigned int mant_bits, mant_off;
2728 int mant_bits_left;
c5aa993b 2729 unsigned char *uto = (unsigned char *) to;
c906108c
SS
2730
2731 memcpy (&dfrom, from, sizeof (dfrom));
ba8966d6
KB
2732 memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
2733 / FLOATFORMAT_CHAR_BIT);
c906108c
SS
2734 if (dfrom == 0)
2735 return; /* Result is zero */
2736 if (dfrom != dfrom) /* Result is NaN */
2737 {
2738 /* From is NaN */
2739 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
2740 fmt->exp_len, fmt->exp_nan);
2741 /* Be sure it's not infinity, but NaN value is irrel */
2742 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
2743 32, 1);
2744 return;
2745 }
2746
2747 /* If negative, set the sign bit. */
2748 if (dfrom < 0)
2749 {
2750 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
2751 dfrom = -dfrom;
2752 }
2753
2754 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity */
2755 {
2756 /* Infinity exponent is same as NaN's. */
2757 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
2758 fmt->exp_len, fmt->exp_nan);
2759 /* Infinity mantissa is all zeroes. */
2760 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
2761 fmt->man_len, 0);
2762 return;
2763 }
2764
2765#ifdef HAVE_LONG_DOUBLE
2766 mant = ldfrexp (dfrom, &exponent);
2767#else
2768 mant = frexp (dfrom, &exponent);
2769#endif
2770
2771 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
2772 exponent + fmt->exp_bias - 1);
2773
2774 mant_bits_left = fmt->man_len;
2775 mant_off = fmt->man_start;
2776 while (mant_bits_left > 0)
2777 {
2778 unsigned long mant_long;
2779 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
2780
2781 mant *= 4294967296.0;
ba8966d6 2782 mant_long = ((unsigned long) mant) & 0xffffffffL;
c906108c
SS
2783 mant -= mant_long;
2784
2785 /* If the integer bit is implicit, then we need to discard it.
c5aa993b
JM
2786 If we are discarding a zero, we should be (but are not) creating
2787 a denormalized number which means adjusting the exponent
2788 (I think). */
c906108c
SS
2789 if (mant_bits_left == fmt->man_len
2790 && fmt->intbit == floatformat_intbit_no)
2791 {
2792 mant_long <<= 1;
ba8966d6 2793 mant_long &= 0xffffffffL;
c906108c
SS
2794 mant_bits -= 1;
2795 }
2796
2797 if (mant_bits < 32)
2798 {
2799 /* The bits we want are in the most significant MANT_BITS bits of
2800 mant_long. Move them to the least significant. */
2801 mant_long >>= 32 - mant_bits;
2802 }
2803
2804 put_field (uto, fmt->byteorder, fmt->totalsize,
2805 mant_off, mant_bits, mant_long);
2806 mant_off += mant_bits;
2807 mant_bits_left -= mant_bits;
2808 }
c5aa993b 2809 if (fmt->byteorder == floatformat_littlebyte_bigword)
c906108c
SS
2810 {
2811 int count;
2812 unsigned char *swaplow = uto;
2813 unsigned char *swaphigh = uto + 4;
2814 unsigned char tmp;
2815
2816 for (count = 0; count < 4; count++)
2817 {
2818 tmp = *swaplow;
2819 *swaplow++ = *swaphigh;
2820 *swaphigh++ = tmp;
2821 }
2822 }
2823}
2824
5683e87a
AC
2825/* print routines to handle variable size regs, etc. */
2826
c906108c
SS
2827/* temporary storage using circular buffer */
2828#define NUMCELLS 16
2829#define CELLSIZE 32
c5aa993b
JM
2830static char *
2831get_cell ()
c906108c
SS
2832{
2833 static char buf[NUMCELLS][CELLSIZE];
c5aa993b
JM
2834 static int cell = 0;
2835 if (++cell >= NUMCELLS)
2836 cell = 0;
c906108c
SS
2837 return buf[cell];
2838}
2839
d4f3574e
SS
2840int
2841strlen_paddr (void)
2842{
2843 return (TARGET_PTR_BIT / 8 * 2);
2844}
2845
c5aa993b 2846char *
104c1213 2847paddr (CORE_ADDR addr)
c906108c 2848{
5683e87a 2849 return phex (addr, TARGET_PTR_BIT / 8);
c906108c
SS
2850}
2851
c5aa993b 2852char *
104c1213 2853paddr_nz (CORE_ADDR addr)
c906108c 2854{
5683e87a 2855 return phex_nz (addr, TARGET_PTR_BIT / 8);
c906108c
SS
2856}
2857
104c1213
JM
2858static void
2859decimal2str (char *paddr_str, char *sign, ULONGEST addr)
2860{
2861 /* steal code from valprint.c:print_decimal(). Should this worry
2862 about the real size of addr as the above does? */
2863 unsigned long temp[3];
2864 int i = 0;
2865 do
2866 {
2867 temp[i] = addr % (1000 * 1000 * 1000);
2868 addr /= (1000 * 1000 * 1000);
2869 i++;
2870 }
2871 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
2872 switch (i)
2873 {
2874 case 1:
2875 sprintf (paddr_str, "%s%lu",
2876 sign, temp[0]);
2877 break;
2878 case 2:
2879 sprintf (paddr_str, "%s%lu%09lu",
2880 sign, temp[1], temp[0]);
2881 break;
2882 case 3:
2883 sprintf (paddr_str, "%s%lu%09lu%09lu",
2884 sign, temp[2], temp[1], temp[0]);
2885 break;
2886 default:
2887 abort ();
2888 }
2889}
2890
2891char *
2892paddr_u (CORE_ADDR addr)
2893{
2894 char *paddr_str = get_cell ();
2895 decimal2str (paddr_str, "", addr);
2896 return paddr_str;
2897}
2898
2899char *
2900paddr_d (LONGEST addr)
2901{
2902 char *paddr_str = get_cell ();
2903 if (addr < 0)
2904 decimal2str (paddr_str, "-", -addr);
2905 else
2906 decimal2str (paddr_str, "", addr);
2907 return paddr_str;
2908}
2909
5683e87a
AC
2910/* eliminate warning from compiler on 32-bit systems */
2911static int thirty_two = 32;
2912
104c1213 2913char *
5683e87a 2914phex (ULONGEST l, int sizeof_l)
104c1213 2915{
5683e87a
AC
2916 char *str = get_cell ();
2917 switch (sizeof_l)
104c1213
JM
2918 {
2919 case 8:
5683e87a
AC
2920 sprintf (str, "%08lx%08lx",
2921 (unsigned long) (l >> thirty_two),
2922 (unsigned long) (l & 0xffffffff));
104c1213
JM
2923 break;
2924 case 4:
5683e87a 2925 sprintf (str, "%08lx", (unsigned long) l);
104c1213
JM
2926 break;
2927 case 2:
5683e87a 2928 sprintf (str, "%04x", (unsigned short) (l & 0xffff));
104c1213
JM
2929 break;
2930 default:
5683e87a
AC
2931 phex (l, sizeof (l));
2932 break;
104c1213 2933 }
5683e87a 2934 return str;
104c1213
JM
2935}
2936
c5aa993b 2937char *
5683e87a 2938phex_nz (ULONGEST l, int sizeof_l)
c906108c 2939{
5683e87a
AC
2940 char *str = get_cell ();
2941 switch (sizeof_l)
c906108c 2942 {
c5aa993b
JM
2943 case 8:
2944 {
5683e87a 2945 unsigned long high = (unsigned long) (l >> thirty_two);
c5aa993b 2946 if (high == 0)
5683e87a 2947 sprintf (str, "%lx", (unsigned long) (l & 0xffffffff));
c5aa993b 2948 else
5683e87a
AC
2949 sprintf (str, "%lx%08lx",
2950 high, (unsigned long) (l & 0xffffffff));
c906108c 2951 break;
c5aa993b
JM
2952 }
2953 case 4:
5683e87a 2954 sprintf (str, "%lx", (unsigned long) l);
c5aa993b
JM
2955 break;
2956 case 2:
5683e87a 2957 sprintf (str, "%x", (unsigned short) (l & 0xffff));
c5aa993b
JM
2958 break;
2959 default:
5683e87a
AC
2960 phex_nz (l, sizeof (l));
2961 break;
c906108c 2962 }
5683e87a 2963 return str;
c906108c 2964}