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