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