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