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