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