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