1*dded96e1Sbostic /* General utility routines for GDB, the GNU debugger.
2*dded96e1Sbostic Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3*dded96e1Sbostic
4*dded96e1Sbostic This file is part of GDB.
5*dded96e1Sbostic
6*dded96e1Sbostic This program is free software; you can redistribute it and/or modify
7*dded96e1Sbostic it under the terms of the GNU General Public License as published by
8*dded96e1Sbostic the Free Software Foundation; either version 2 of the License, or
9*dded96e1Sbostic (at your option) any later version.
10*dded96e1Sbostic
11*dded96e1Sbostic This program is distributed in the hope that it will be useful,
12*dded96e1Sbostic but WITHOUT ANY WARRANTY; without even the implied warranty of
13*dded96e1Sbostic MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*dded96e1Sbostic GNU General Public License for more details.
15*dded96e1Sbostic
16*dded96e1Sbostic You should have received a copy of the GNU General Public License
17*dded96e1Sbostic along with this program; if not, write to the Free Software
18*dded96e1Sbostic Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19*dded96e1Sbostic
20*dded96e1Sbostic #include "defs.h"
21*dded96e1Sbostic #if !defined(__GO32__)
22*dded96e1Sbostic #include <sys/ioctl.h>
23*dded96e1Sbostic #include <sys/param.h>
24*dded96e1Sbostic #include <pwd.h>
25*dded96e1Sbostic #endif
26*dded96e1Sbostic #include <varargs.h>
27*dded96e1Sbostic #include <ctype.h>
28*dded96e1Sbostic #include <string.h>
29*dded96e1Sbostic
30*dded96e1Sbostic #include "signals.h"
31*dded96e1Sbostic #include "gdbcmd.h"
32*dded96e1Sbostic #include "terminal.h"
33*dded96e1Sbostic #include "bfd.h"
34*dded96e1Sbostic #include "target.h"
35*dded96e1Sbostic #include "demangle.h"
36*dded96e1Sbostic
37*dded96e1Sbostic /* Prototypes for local functions */
38*dded96e1Sbostic
39*dded96e1Sbostic #if !defined (NO_MALLOC_CHECK)
40*dded96e1Sbostic
41*dded96e1Sbostic static void
42*dded96e1Sbostic malloc_botch PARAMS ((void));
43*dded96e1Sbostic
44*dded96e1Sbostic #endif /* NO_MALLOC_CHECK */
45*dded96e1Sbostic
46*dded96e1Sbostic static void
47*dded96e1Sbostic fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
48*dded96e1Sbostic
49*dded96e1Sbostic static void
50*dded96e1Sbostic prompt_for_continue PARAMS ((void));
51*dded96e1Sbostic
52*dded96e1Sbostic static void
53*dded96e1Sbostic set_width_command PARAMS ((char *, int, struct cmd_list_element *));
54*dded96e1Sbostic
55*dded96e1Sbostic /* If this definition isn't overridden by the header files, assume
56*dded96e1Sbostic that isatty and fileno exist on this system. */
57*dded96e1Sbostic #ifndef ISATTY
58*dded96e1Sbostic #define ISATTY(FP) (isatty (fileno (FP)))
59*dded96e1Sbostic #endif
60*dded96e1Sbostic
61*dded96e1Sbostic /* Chain of cleanup actions established with make_cleanup,
62*dded96e1Sbostic to be executed if an error happens. */
63*dded96e1Sbostic
64*dded96e1Sbostic static struct cleanup *cleanup_chain;
65*dded96e1Sbostic
66*dded96e1Sbostic /* Nonzero means a quit has been requested. */
67*dded96e1Sbostic
68*dded96e1Sbostic int quit_flag;
69*dded96e1Sbostic
70*dded96e1Sbostic /* Nonzero means quit immediately if Control-C is typed now,
71*dded96e1Sbostic rather than waiting until QUIT is executed. */
72*dded96e1Sbostic
73*dded96e1Sbostic int immediate_quit;
74*dded96e1Sbostic
75*dded96e1Sbostic /* Nonzero means that encoded C++ names should be printed out in their
76*dded96e1Sbostic C++ form rather than raw. */
77*dded96e1Sbostic
78*dded96e1Sbostic int demangle = 1;
79*dded96e1Sbostic
80*dded96e1Sbostic /* Nonzero means that encoded C++ names should be printed out in their
81*dded96e1Sbostic C++ form even in assembler language displays. If this is set, but
82*dded96e1Sbostic DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
83*dded96e1Sbostic
84*dded96e1Sbostic int asm_demangle = 0;
85*dded96e1Sbostic
86*dded96e1Sbostic /* Nonzero means that strings with character values >0x7F should be printed
87*dded96e1Sbostic as octal escapes. Zero means just print the value (e.g. it's an
88*dded96e1Sbostic international character, and the terminal or window can cope.) */
89*dded96e1Sbostic
90*dded96e1Sbostic int sevenbit_strings = 0;
91*dded96e1Sbostic
92*dded96e1Sbostic /* String to be printed before error messages, if any. */
93*dded96e1Sbostic
94*dded96e1Sbostic char *error_pre_print;
95*dded96e1Sbostic char *warning_pre_print = "\nwarning: ";
96*dded96e1Sbostic
97*dded96e1Sbostic /* Add a new cleanup to the cleanup_chain,
98*dded96e1Sbostic and return the previous chain pointer
99*dded96e1Sbostic to be passed later to do_cleanups or discard_cleanups.
100*dded96e1Sbostic Args are FUNCTION to clean up with, and ARG to pass to it. */
101*dded96e1Sbostic
102*dded96e1Sbostic struct cleanup *
103*dded96e1Sbostic make_cleanup (function, arg)
104*dded96e1Sbostic void (*function) PARAMS ((PTR));
105*dded96e1Sbostic PTR arg;
106*dded96e1Sbostic {
107*dded96e1Sbostic register struct cleanup *new
108*dded96e1Sbostic = (struct cleanup *) xmalloc (sizeof (struct cleanup));
109*dded96e1Sbostic register struct cleanup *old_chain = cleanup_chain;
110*dded96e1Sbostic
111*dded96e1Sbostic new->next = cleanup_chain;
112*dded96e1Sbostic new->function = function;
113*dded96e1Sbostic new->arg = arg;
114*dded96e1Sbostic cleanup_chain = new;
115*dded96e1Sbostic
116*dded96e1Sbostic return old_chain;
117*dded96e1Sbostic }
118*dded96e1Sbostic
119*dded96e1Sbostic /* Discard cleanups and do the actions they describe
120*dded96e1Sbostic until we get back to the point OLD_CHAIN in the cleanup_chain. */
121*dded96e1Sbostic
122*dded96e1Sbostic void
do_cleanups(old_chain)123*dded96e1Sbostic do_cleanups (old_chain)
124*dded96e1Sbostic register struct cleanup *old_chain;
125*dded96e1Sbostic {
126*dded96e1Sbostic register struct cleanup *ptr;
127*dded96e1Sbostic while ((ptr = cleanup_chain) != old_chain)
128*dded96e1Sbostic {
129*dded96e1Sbostic cleanup_chain = ptr->next; /* Do this first incase recursion */
130*dded96e1Sbostic (*ptr->function) (ptr->arg);
131*dded96e1Sbostic free (ptr);
132*dded96e1Sbostic }
133*dded96e1Sbostic }
134*dded96e1Sbostic
135*dded96e1Sbostic /* Discard cleanups, not doing the actions they describe,
136*dded96e1Sbostic until we get back to the point OLD_CHAIN in the cleanup_chain. */
137*dded96e1Sbostic
138*dded96e1Sbostic void
discard_cleanups(old_chain)139*dded96e1Sbostic discard_cleanups (old_chain)
140*dded96e1Sbostic register struct cleanup *old_chain;
141*dded96e1Sbostic {
142*dded96e1Sbostic register struct cleanup *ptr;
143*dded96e1Sbostic while ((ptr = cleanup_chain) != old_chain)
144*dded96e1Sbostic {
145*dded96e1Sbostic cleanup_chain = ptr->next;
146*dded96e1Sbostic free ((PTR)ptr);
147*dded96e1Sbostic }
148*dded96e1Sbostic }
149*dded96e1Sbostic
150*dded96e1Sbostic /* Set the cleanup_chain to 0, and return the old cleanup chain. */
151*dded96e1Sbostic struct cleanup *
save_cleanups()152*dded96e1Sbostic save_cleanups ()
153*dded96e1Sbostic {
154*dded96e1Sbostic struct cleanup *old_chain = cleanup_chain;
155*dded96e1Sbostic
156*dded96e1Sbostic cleanup_chain = 0;
157*dded96e1Sbostic return old_chain;
158*dded96e1Sbostic }
159*dded96e1Sbostic
160*dded96e1Sbostic /* Restore the cleanup chain from a previously saved chain. */
161*dded96e1Sbostic void
restore_cleanups(chain)162*dded96e1Sbostic restore_cleanups (chain)
163*dded96e1Sbostic struct cleanup *chain;
164*dded96e1Sbostic {
165*dded96e1Sbostic cleanup_chain = chain;
166*dded96e1Sbostic }
167*dded96e1Sbostic
168*dded96e1Sbostic /* This function is useful for cleanups.
169*dded96e1Sbostic Do
170*dded96e1Sbostic
171*dded96e1Sbostic foo = xmalloc (...);
172*dded96e1Sbostic old_chain = make_cleanup (free_current_contents, &foo);
173*dded96e1Sbostic
174*dded96e1Sbostic to arrange to free the object thus allocated. */
175*dded96e1Sbostic
176*dded96e1Sbostic void
free_current_contents(location)177*dded96e1Sbostic free_current_contents (location)
178*dded96e1Sbostic char **location;
179*dded96e1Sbostic {
180*dded96e1Sbostic free (*location);
181*dded96e1Sbostic }
182*dded96e1Sbostic
183*dded96e1Sbostic /* Provide a known function that does nothing, to use as a base for
184*dded96e1Sbostic for a possibly long chain of cleanups. This is useful where we
185*dded96e1Sbostic use the cleanup chain for handling normal cleanups as well as dealing
186*dded96e1Sbostic with cleanups that need to be done as a result of a call to error().
187*dded96e1Sbostic In such cases, we may not be certain where the first cleanup is, unless
188*dded96e1Sbostic we have a do-nothing one to always use as the base. */
189*dded96e1Sbostic
190*dded96e1Sbostic /* ARGSUSED */
191*dded96e1Sbostic void
null_cleanup(arg)192*dded96e1Sbostic null_cleanup (arg)
193*dded96e1Sbostic char **arg;
194*dded96e1Sbostic {
195*dded96e1Sbostic }
196*dded96e1Sbostic
197*dded96e1Sbostic
198*dded96e1Sbostic /* Provide a hook for modules wishing to print their own warning messages
199*dded96e1Sbostic to set up the terminal state in a compatible way, without them having
200*dded96e1Sbostic to import all the target_<...> macros. */
201*dded96e1Sbostic
202*dded96e1Sbostic void
warning_setup()203*dded96e1Sbostic warning_setup ()
204*dded96e1Sbostic {
205*dded96e1Sbostic target_terminal_ours ();
206*dded96e1Sbostic wrap_here(""); /* Force out any buffered output */
207*dded96e1Sbostic fflush (stdout);
208*dded96e1Sbostic }
209*dded96e1Sbostic
210*dded96e1Sbostic /* Print a warning message.
211*dded96e1Sbostic The first argument STRING is the warning message, used as a fprintf string,
212*dded96e1Sbostic and the remaining args are passed as arguments to it.
213*dded96e1Sbostic The primary difference between warnings and errors is that a warning
214*dded96e1Sbostic does not force the return to command level. */
215*dded96e1Sbostic
216*dded96e1Sbostic /* VARARGS */
217*dded96e1Sbostic void
warning(va_alist)218*dded96e1Sbostic warning (va_alist)
219*dded96e1Sbostic va_dcl
220*dded96e1Sbostic {
221*dded96e1Sbostic va_list args;
222*dded96e1Sbostic char *string;
223*dded96e1Sbostic
224*dded96e1Sbostic va_start (args);
225*dded96e1Sbostic target_terminal_ours ();
226*dded96e1Sbostic wrap_here(""); /* Force out any buffered output */
227*dded96e1Sbostic fflush (stdout);
228*dded96e1Sbostic if (warning_pre_print)
229*dded96e1Sbostic fprintf (stderr, warning_pre_print);
230*dded96e1Sbostic string = va_arg (args, char *);
231*dded96e1Sbostic vfprintf (stderr, string, args);
232*dded96e1Sbostic fprintf (stderr, "\n");
233*dded96e1Sbostic va_end (args);
234*dded96e1Sbostic }
235*dded96e1Sbostic
236*dded96e1Sbostic /* Print an error message and return to command level.
237*dded96e1Sbostic The first argument STRING is the error message, used as a fprintf string,
238*dded96e1Sbostic and the remaining args are passed as arguments to it. */
239*dded96e1Sbostic
240*dded96e1Sbostic /* VARARGS */
241*dded96e1Sbostic NORETURN void
error(va_alist)242*dded96e1Sbostic error (va_alist)
243*dded96e1Sbostic va_dcl
244*dded96e1Sbostic {
245*dded96e1Sbostic va_list args;
246*dded96e1Sbostic char *string;
247*dded96e1Sbostic
248*dded96e1Sbostic va_start (args);
249*dded96e1Sbostic target_terminal_ours ();
250*dded96e1Sbostic wrap_here(""); /* Force out any buffered output */
251*dded96e1Sbostic fflush (stdout);
252*dded96e1Sbostic if (error_pre_print)
253*dded96e1Sbostic fprintf_filtered (stderr, error_pre_print);
254*dded96e1Sbostic string = va_arg (args, char *);
255*dded96e1Sbostic vfprintf_filtered (stderr, string, args);
256*dded96e1Sbostic fprintf_filtered (stderr, "\n");
257*dded96e1Sbostic va_end (args);
258*dded96e1Sbostic return_to_top_level ();
259*dded96e1Sbostic }
260*dded96e1Sbostic
261*dded96e1Sbostic /* Print an error message and exit reporting failure.
262*dded96e1Sbostic This is for a error that we cannot continue from.
263*dded96e1Sbostic The arguments are printed a la printf.
264*dded96e1Sbostic
265*dded96e1Sbostic This function cannot be declared volatile (NORETURN) in an
266*dded96e1Sbostic ANSI environment because exit() is not declared volatile. */
267*dded96e1Sbostic
268*dded96e1Sbostic /* VARARGS */
269*dded96e1Sbostic NORETURN void
fatal(va_alist)270*dded96e1Sbostic fatal (va_alist)
271*dded96e1Sbostic va_dcl
272*dded96e1Sbostic {
273*dded96e1Sbostic va_list args;
274*dded96e1Sbostic char *string;
275*dded96e1Sbostic
276*dded96e1Sbostic va_start (args);
277*dded96e1Sbostic string = va_arg (args, char *);
278*dded96e1Sbostic fprintf (stderr, "\ngdb: ");
279*dded96e1Sbostic vfprintf (stderr, string, args);
280*dded96e1Sbostic fprintf (stderr, "\n");
281*dded96e1Sbostic va_end (args);
282*dded96e1Sbostic exit (1);
283*dded96e1Sbostic }
284*dded96e1Sbostic
285*dded96e1Sbostic /* Print an error message and exit, dumping core.
286*dded96e1Sbostic The arguments are printed a la printf (). */
287*dded96e1Sbostic
288*dded96e1Sbostic /* VARARGS */
289*dded96e1Sbostic static void
fatal_dump_core(va_alist)290*dded96e1Sbostic fatal_dump_core (va_alist)
291*dded96e1Sbostic va_dcl
292*dded96e1Sbostic {
293*dded96e1Sbostic va_list args;
294*dded96e1Sbostic char *string;
295*dded96e1Sbostic
296*dded96e1Sbostic va_start (args);
297*dded96e1Sbostic string = va_arg (args, char *);
298*dded96e1Sbostic /* "internal error" is always correct, since GDB should never dump
299*dded96e1Sbostic core, no matter what the input. */
300*dded96e1Sbostic fprintf (stderr, "\ngdb internal error: ");
301*dded96e1Sbostic vfprintf (stderr, string, args);
302*dded96e1Sbostic fprintf (stderr, "\n");
303*dded96e1Sbostic va_end (args);
304*dded96e1Sbostic
305*dded96e1Sbostic signal (SIGQUIT, SIG_DFL);
306*dded96e1Sbostic kill (getpid (), SIGQUIT);
307*dded96e1Sbostic /* We should never get here, but just in case... */
308*dded96e1Sbostic exit (1);
309*dded96e1Sbostic }
310*dded96e1Sbostic
311*dded96e1Sbostic /* The strerror() function can return NULL for errno values that are
312*dded96e1Sbostic out of range. Provide a "safe" version that always returns a
313*dded96e1Sbostic printable string. */
314*dded96e1Sbostic
315*dded96e1Sbostic char *
safe_strerror(errnum)316*dded96e1Sbostic safe_strerror (errnum)
317*dded96e1Sbostic int errnum;
318*dded96e1Sbostic {
319*dded96e1Sbostic char *msg;
320*dded96e1Sbostic static char buf[32];
321*dded96e1Sbostic
322*dded96e1Sbostic if ((msg = strerror (errnum)) == NULL)
323*dded96e1Sbostic {
324*dded96e1Sbostic sprintf (buf, "(undocumented errno %d)", errnum);
325*dded96e1Sbostic msg = buf;
326*dded96e1Sbostic }
327*dded96e1Sbostic return (msg);
328*dded96e1Sbostic }
329*dded96e1Sbostic
330*dded96e1Sbostic /* The strsignal() function can return NULL for signal values that are
331*dded96e1Sbostic out of range. Provide a "safe" version that always returns a
332*dded96e1Sbostic printable string. */
333*dded96e1Sbostic
334*dded96e1Sbostic char *
safe_strsignal(signo)335*dded96e1Sbostic safe_strsignal (signo)
336*dded96e1Sbostic int signo;
337*dded96e1Sbostic {
338*dded96e1Sbostic char *msg;
339*dded96e1Sbostic static char buf[32];
340*dded96e1Sbostic
341*dded96e1Sbostic if ((msg = strsignal (signo)) == NULL)
342*dded96e1Sbostic {
343*dded96e1Sbostic sprintf (buf, "(undocumented signal %d)", signo);
344*dded96e1Sbostic msg = buf;
345*dded96e1Sbostic }
346*dded96e1Sbostic return (msg);
347*dded96e1Sbostic }
348*dded96e1Sbostic
349*dded96e1Sbostic
350*dded96e1Sbostic /* Print the system error message for errno, and also mention STRING
351*dded96e1Sbostic as the file name for which the error was encountered.
352*dded96e1Sbostic Then return to command level. */
353*dded96e1Sbostic
354*dded96e1Sbostic void
perror_with_name(string)355*dded96e1Sbostic perror_with_name (string)
356*dded96e1Sbostic char *string;
357*dded96e1Sbostic {
358*dded96e1Sbostic char *err;
359*dded96e1Sbostic char *combined;
360*dded96e1Sbostic
361*dded96e1Sbostic err = safe_strerror (errno);
362*dded96e1Sbostic combined = (char *) alloca (strlen (err) + strlen (string) + 3);
363*dded96e1Sbostic strcpy (combined, string);
364*dded96e1Sbostic strcat (combined, ": ");
365*dded96e1Sbostic strcat (combined, err);
366*dded96e1Sbostic
367*dded96e1Sbostic /* I understand setting these is a matter of taste. Still, some people
368*dded96e1Sbostic may clear errno but not know about bfd_error. Doing this here is not
369*dded96e1Sbostic unreasonable. */
370*dded96e1Sbostic bfd_error = no_error;
371*dded96e1Sbostic errno = 0;
372*dded96e1Sbostic
373*dded96e1Sbostic error ("%s.", combined);
374*dded96e1Sbostic }
375*dded96e1Sbostic
376*dded96e1Sbostic /* Print the system error message for ERRCODE, and also mention STRING
377*dded96e1Sbostic as the file name for which the error was encountered. */
378*dded96e1Sbostic
379*dded96e1Sbostic void
print_sys_errmsg(string,errcode)380*dded96e1Sbostic print_sys_errmsg (string, errcode)
381*dded96e1Sbostic char *string;
382*dded96e1Sbostic int errcode;
383*dded96e1Sbostic {
384*dded96e1Sbostic char *err;
385*dded96e1Sbostic char *combined;
386*dded96e1Sbostic
387*dded96e1Sbostic err = safe_strerror (errcode);
388*dded96e1Sbostic combined = (char *) alloca (strlen (err) + strlen (string) + 3);
389*dded96e1Sbostic strcpy (combined, string);
390*dded96e1Sbostic strcat (combined, ": ");
391*dded96e1Sbostic strcat (combined, err);
392*dded96e1Sbostic
393*dded96e1Sbostic fprintf (stderr, "%s.\n", combined);
394*dded96e1Sbostic }
395*dded96e1Sbostic
396*dded96e1Sbostic /* Control C eventually causes this to be called, at a convenient time. */
397*dded96e1Sbostic
398*dded96e1Sbostic void
quit()399*dded96e1Sbostic quit ()
400*dded96e1Sbostic {
401*dded96e1Sbostic target_terminal_ours ();
402*dded96e1Sbostic wrap_here ((char *)0); /* Force out any pending output */
403*dded96e1Sbostic #if !defined(__GO32__)
404*dded96e1Sbostic #ifdef HAVE_TERMIO
405*dded96e1Sbostic ioctl (fileno (stdout), TCFLSH, 1);
406*dded96e1Sbostic #else /* not HAVE_TERMIO */
407*dded96e1Sbostic ioctl (fileno (stdout), TIOCFLUSH, 0);
408*dded96e1Sbostic #endif /* not HAVE_TERMIO */
409*dded96e1Sbostic #ifdef TIOCGPGRP
410*dded96e1Sbostic error ("Quit");
411*dded96e1Sbostic #else
412*dded96e1Sbostic error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
413*dded96e1Sbostic #endif /* TIOCGPGRP */
414*dded96e1Sbostic #endif
415*dded96e1Sbostic }
416*dded96e1Sbostic
417*dded96e1Sbostic /* Control C comes here */
418*dded96e1Sbostic
419*dded96e1Sbostic void
request_quit(signo)420*dded96e1Sbostic request_quit (signo)
421*dded96e1Sbostic int signo;
422*dded96e1Sbostic {
423*dded96e1Sbostic quit_flag = 1;
424*dded96e1Sbostic
425*dded96e1Sbostic #ifdef USG
426*dded96e1Sbostic /* Restore the signal handler. */
427*dded96e1Sbostic signal (signo, request_quit);
428*dded96e1Sbostic #endif
429*dded96e1Sbostic
430*dded96e1Sbostic if (immediate_quit)
431*dded96e1Sbostic quit ();
432*dded96e1Sbostic }
433*dded96e1Sbostic
434*dded96e1Sbostic
435*dded96e1Sbostic /* Memory management stuff (malloc friends). */
436*dded96e1Sbostic
437*dded96e1Sbostic #if defined (NO_MMALLOC)
438*dded96e1Sbostic
439*dded96e1Sbostic PTR
mmalloc(md,size)440*dded96e1Sbostic mmalloc (md, size)
441*dded96e1Sbostic PTR md;
442*dded96e1Sbostic long size;
443*dded96e1Sbostic {
444*dded96e1Sbostic return (malloc (size));
445*dded96e1Sbostic }
446*dded96e1Sbostic
447*dded96e1Sbostic PTR
mrealloc(md,ptr,size)448*dded96e1Sbostic mrealloc (md, ptr, size)
449*dded96e1Sbostic PTR md;
450*dded96e1Sbostic PTR ptr;
451*dded96e1Sbostic long size;
452*dded96e1Sbostic {
453*dded96e1Sbostic if (ptr == 0) /* Guard against old realloc's */
454*dded96e1Sbostic return malloc (size);
455*dded96e1Sbostic else
456*dded96e1Sbostic return realloc (ptr, size);
457*dded96e1Sbostic }
458*dded96e1Sbostic
459*dded96e1Sbostic void
mfree(md,ptr)460*dded96e1Sbostic mfree (md, ptr)
461*dded96e1Sbostic PTR md;
462*dded96e1Sbostic PTR ptr;
463*dded96e1Sbostic {
464*dded96e1Sbostic free (ptr);
465*dded96e1Sbostic }
466*dded96e1Sbostic
467*dded96e1Sbostic #endif /* NO_MMALLOC */
468*dded96e1Sbostic
469*dded96e1Sbostic #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
470*dded96e1Sbostic
471*dded96e1Sbostic void
init_malloc(md)472*dded96e1Sbostic init_malloc (md)
473*dded96e1Sbostic PTR md;
474*dded96e1Sbostic {
475*dded96e1Sbostic }
476*dded96e1Sbostic
477*dded96e1Sbostic #else /* have mmalloc and want corruption checking */
478*dded96e1Sbostic
479*dded96e1Sbostic static void
malloc_botch()480*dded96e1Sbostic malloc_botch ()
481*dded96e1Sbostic {
482*dded96e1Sbostic fatal_dump_core ("Memory corruption");
483*dded96e1Sbostic }
484*dded96e1Sbostic
485*dded96e1Sbostic /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
486*dded96e1Sbostic by MD, to detect memory corruption. Note that MD may be NULL to specify
487*dded96e1Sbostic the default heap that grows via sbrk.
488*dded96e1Sbostic
489*dded96e1Sbostic Note that for freshly created regions, we must call mmcheck prior to any
490*dded96e1Sbostic mallocs in the region. Otherwise, any region which was allocated prior to
491*dded96e1Sbostic installing the checking hooks, which is later reallocated or freed, will
492*dded96e1Sbostic fail the checks! The mmcheck function only allows initial hooks to be
493*dded96e1Sbostic installed before the first mmalloc. However, anytime after we have called
494*dded96e1Sbostic mmcheck the first time to install the checking hooks, we can call it again
495*dded96e1Sbostic to update the function pointer to the memory corruption handler.
496*dded96e1Sbostic
497*dded96e1Sbostic Returns zero on failure, non-zero on success. */
498*dded96e1Sbostic
499*dded96e1Sbostic void
init_malloc(md)500*dded96e1Sbostic init_malloc (md)
501*dded96e1Sbostic PTR md;
502*dded96e1Sbostic {
503*dded96e1Sbostic if (!mmcheck (md, malloc_botch))
504*dded96e1Sbostic {
505*dded96e1Sbostic warning ("internal error: failed to install memory consistency checks");
506*dded96e1Sbostic }
507*dded96e1Sbostic
508*dded96e1Sbostic mmtrace ();
509*dded96e1Sbostic }
510*dded96e1Sbostic
511*dded96e1Sbostic #endif /* Have mmalloc and want corruption checking */
512*dded96e1Sbostic
513*dded96e1Sbostic /* Called when a memory allocation fails, with the number of bytes of
514*dded96e1Sbostic memory requested in SIZE. */
515*dded96e1Sbostic
516*dded96e1Sbostic NORETURN void
nomem(size)517*dded96e1Sbostic nomem (size)
518*dded96e1Sbostic long size;
519*dded96e1Sbostic {
520*dded96e1Sbostic if (size > 0)
521*dded96e1Sbostic {
522*dded96e1Sbostic fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
523*dded96e1Sbostic }
524*dded96e1Sbostic else
525*dded96e1Sbostic {
526*dded96e1Sbostic fatal ("virtual memory exhausted.");
527*dded96e1Sbostic }
528*dded96e1Sbostic }
529*dded96e1Sbostic
530*dded96e1Sbostic /* Like mmalloc but get error if no storage available, and protect against
531*dded96e1Sbostic the caller wanting to allocate zero bytes. Whether to return NULL for
532*dded96e1Sbostic a zero byte request, or translate the request into a request for one
533*dded96e1Sbostic byte of zero'd storage, is a religious issue. */
534*dded96e1Sbostic
535*dded96e1Sbostic PTR
xmmalloc(md,size)536*dded96e1Sbostic xmmalloc (md, size)
537*dded96e1Sbostic PTR md;
538*dded96e1Sbostic long size;
539*dded96e1Sbostic {
540*dded96e1Sbostic register PTR val;
541*dded96e1Sbostic
542*dded96e1Sbostic if (size == 0)
543*dded96e1Sbostic {
544*dded96e1Sbostic val = NULL;
545*dded96e1Sbostic }
546*dded96e1Sbostic else if ((val = mmalloc (md, size)) == NULL)
547*dded96e1Sbostic {
548*dded96e1Sbostic nomem (size);
549*dded96e1Sbostic }
550*dded96e1Sbostic return (val);
551*dded96e1Sbostic }
552*dded96e1Sbostic
553*dded96e1Sbostic /* Like mrealloc but get error if no storage available. */
554*dded96e1Sbostic
555*dded96e1Sbostic PTR
xmrealloc(md,ptr,size)556*dded96e1Sbostic xmrealloc (md, ptr, size)
557*dded96e1Sbostic PTR md;
558*dded96e1Sbostic PTR ptr;
559*dded96e1Sbostic long size;
560*dded96e1Sbostic {
561*dded96e1Sbostic register PTR val;
562*dded96e1Sbostic
563*dded96e1Sbostic if (ptr != NULL)
564*dded96e1Sbostic {
565*dded96e1Sbostic val = mrealloc (md, ptr, size);
566*dded96e1Sbostic }
567*dded96e1Sbostic else
568*dded96e1Sbostic {
569*dded96e1Sbostic val = mmalloc (md, size);
570*dded96e1Sbostic }
571*dded96e1Sbostic if (val == NULL)
572*dded96e1Sbostic {
573*dded96e1Sbostic nomem (size);
574*dded96e1Sbostic }
575*dded96e1Sbostic return (val);
576*dded96e1Sbostic }
577*dded96e1Sbostic
578*dded96e1Sbostic /* Like malloc but get error if no storage available, and protect against
579*dded96e1Sbostic the caller wanting to allocate zero bytes. */
580*dded96e1Sbostic
581*dded96e1Sbostic PTR
xmalloc(size)582*dded96e1Sbostic xmalloc (size)
583*dded96e1Sbostic long size;
584*dded96e1Sbostic {
585*dded96e1Sbostic return (xmmalloc ((void *) NULL, size));
586*dded96e1Sbostic }
587*dded96e1Sbostic
588*dded96e1Sbostic /* Like mrealloc but get error if no storage available. */
589*dded96e1Sbostic
590*dded96e1Sbostic PTR
xrealloc(ptr,size)591*dded96e1Sbostic xrealloc (ptr, size)
592*dded96e1Sbostic PTR ptr;
593*dded96e1Sbostic long size;
594*dded96e1Sbostic {
595*dded96e1Sbostic return (xmrealloc ((void *) NULL, ptr, size));
596*dded96e1Sbostic }
597*dded96e1Sbostic
598*dded96e1Sbostic
599*dded96e1Sbostic /* My replacement for the read system call.
600*dded96e1Sbostic Used like `read' but keeps going if `read' returns too soon. */
601*dded96e1Sbostic
602*dded96e1Sbostic int
myread(desc,addr,len)603*dded96e1Sbostic myread (desc, addr, len)
604*dded96e1Sbostic int desc;
605*dded96e1Sbostic char *addr;
606*dded96e1Sbostic int len;
607*dded96e1Sbostic {
608*dded96e1Sbostic register int val;
609*dded96e1Sbostic int orglen = len;
610*dded96e1Sbostic
611*dded96e1Sbostic while (len > 0)
612*dded96e1Sbostic {
613*dded96e1Sbostic val = read (desc, addr, len);
614*dded96e1Sbostic if (val < 0)
615*dded96e1Sbostic return val;
616*dded96e1Sbostic if (val == 0)
617*dded96e1Sbostic return orglen - len;
618*dded96e1Sbostic len -= val;
619*dded96e1Sbostic addr += val;
620*dded96e1Sbostic }
621*dded96e1Sbostic return orglen;
622*dded96e1Sbostic }
623*dded96e1Sbostic
624*dded96e1Sbostic /* Make a copy of the string at PTR with SIZE characters
625*dded96e1Sbostic (and add a null character at the end in the copy).
626*dded96e1Sbostic Uses malloc to get the space. Returns the address of the copy. */
627*dded96e1Sbostic
628*dded96e1Sbostic char *
savestring(ptr,size)629*dded96e1Sbostic savestring (ptr, size)
630*dded96e1Sbostic const char *ptr;
631*dded96e1Sbostic int size;
632*dded96e1Sbostic {
633*dded96e1Sbostic register char *p = (char *) xmalloc (size + 1);
634*dded96e1Sbostic memcpy (p, ptr, size);
635*dded96e1Sbostic p[size] = 0;
636*dded96e1Sbostic return p;
637*dded96e1Sbostic }
638*dded96e1Sbostic
639*dded96e1Sbostic char *
msavestring(md,ptr,size)640*dded96e1Sbostic msavestring (md, ptr, size)
641*dded96e1Sbostic void *md;
642*dded96e1Sbostic const char *ptr;
643*dded96e1Sbostic int size;
644*dded96e1Sbostic {
645*dded96e1Sbostic register char *p = (char *) xmmalloc (md, size + 1);
646*dded96e1Sbostic memcpy (p, ptr, size);
647*dded96e1Sbostic p[size] = 0;
648*dded96e1Sbostic return p;
649*dded96e1Sbostic }
650*dded96e1Sbostic
651*dded96e1Sbostic /* The "const" is so it compiles under DGUX (which prototypes strsave
652*dded96e1Sbostic in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
653*dded96e1Sbostic Doesn't real strsave return NULL if out of memory? */
654*dded96e1Sbostic char *
strsave(ptr)655*dded96e1Sbostic strsave (ptr)
656*dded96e1Sbostic const char *ptr;
657*dded96e1Sbostic {
658*dded96e1Sbostic return savestring (ptr, strlen (ptr));
659*dded96e1Sbostic }
660*dded96e1Sbostic
661*dded96e1Sbostic char *
mstrsave(md,ptr)662*dded96e1Sbostic mstrsave (md, ptr)
663*dded96e1Sbostic void *md;
664*dded96e1Sbostic const char *ptr;
665*dded96e1Sbostic {
666*dded96e1Sbostic return (msavestring (md, ptr, strlen (ptr)));
667*dded96e1Sbostic }
668*dded96e1Sbostic
669*dded96e1Sbostic void
print_spaces(n,file)670*dded96e1Sbostic print_spaces (n, file)
671*dded96e1Sbostic register int n;
672*dded96e1Sbostic register FILE *file;
673*dded96e1Sbostic {
674*dded96e1Sbostic while (n-- > 0)
675*dded96e1Sbostic fputc (' ', file);
676*dded96e1Sbostic }
677*dded96e1Sbostic
678*dded96e1Sbostic /* Ask user a y-or-n question and return 1 iff answer is yes.
679*dded96e1Sbostic Takes three args which are given to printf to print the question.
680*dded96e1Sbostic The first, a control string, should end in "? ".
681*dded96e1Sbostic It should not say how to answer, because we do that. */
682*dded96e1Sbostic
683*dded96e1Sbostic /* VARARGS */
684*dded96e1Sbostic int
query(va_alist)685*dded96e1Sbostic query (va_alist)
686*dded96e1Sbostic va_dcl
687*dded96e1Sbostic {
688*dded96e1Sbostic va_list args;
689*dded96e1Sbostic char *ctlstr;
690*dded96e1Sbostic register int answer;
691*dded96e1Sbostic register int ans2;
692*dded96e1Sbostic
693*dded96e1Sbostic /* Automatically answer "yes" if input is not from a terminal. */
694*dded96e1Sbostic if (!input_from_terminal_p ())
695*dded96e1Sbostic return 1;
696*dded96e1Sbostic
697*dded96e1Sbostic while (1)
698*dded96e1Sbostic {
699*dded96e1Sbostic wrap_here (""); /* Flush any buffered output */
700*dded96e1Sbostic fflush (stdout);
701*dded96e1Sbostic va_start (args);
702*dded96e1Sbostic ctlstr = va_arg (args, char *);
703*dded96e1Sbostic vfprintf_filtered (stdout, ctlstr, args);
704*dded96e1Sbostic va_end (args);
705*dded96e1Sbostic printf_filtered ("(y or n) ");
706*dded96e1Sbostic fflush (stdout);
707*dded96e1Sbostic answer = fgetc (stdin);
708*dded96e1Sbostic clearerr (stdin); /* in case of C-d */
709*dded96e1Sbostic if (answer == EOF) /* C-d */
710*dded96e1Sbostic return 1;
711*dded96e1Sbostic if (answer != '\n') /* Eat rest of input line, to EOF or newline */
712*dded96e1Sbostic do
713*dded96e1Sbostic {
714*dded96e1Sbostic ans2 = fgetc (stdin);
715*dded96e1Sbostic clearerr (stdin);
716*dded96e1Sbostic }
717*dded96e1Sbostic while (ans2 != EOF && ans2 != '\n');
718*dded96e1Sbostic if (answer >= 'a')
719*dded96e1Sbostic answer -= 040;
720*dded96e1Sbostic if (answer == 'Y')
721*dded96e1Sbostic return 1;
722*dded96e1Sbostic if (answer == 'N')
723*dded96e1Sbostic return 0;
724*dded96e1Sbostic printf_filtered ("Please answer y or n.\n");
725*dded96e1Sbostic }
726*dded96e1Sbostic }
727*dded96e1Sbostic
728*dded96e1Sbostic
729*dded96e1Sbostic /* Parse a C escape sequence. STRING_PTR points to a variable
730*dded96e1Sbostic containing a pointer to the string to parse. That pointer
731*dded96e1Sbostic should point to the character after the \. That pointer
732*dded96e1Sbostic is updated past the characters we use. The value of the
733*dded96e1Sbostic escape sequence is returned.
734*dded96e1Sbostic
735*dded96e1Sbostic A negative value means the sequence \ newline was seen,
736*dded96e1Sbostic which is supposed to be equivalent to nothing at all.
737*dded96e1Sbostic
738*dded96e1Sbostic If \ is followed by a null character, we return a negative
739*dded96e1Sbostic value and leave the string pointer pointing at the null character.
740*dded96e1Sbostic
741*dded96e1Sbostic If \ is followed by 000, we return 0 and leave the string pointer
742*dded96e1Sbostic after the zeros. A value of 0 does not mean end of string. */
743*dded96e1Sbostic
744*dded96e1Sbostic int
parse_escape(string_ptr)745*dded96e1Sbostic parse_escape (string_ptr)
746*dded96e1Sbostic char **string_ptr;
747*dded96e1Sbostic {
748*dded96e1Sbostic register int c = *(*string_ptr)++;
749*dded96e1Sbostic switch (c)
750*dded96e1Sbostic {
751*dded96e1Sbostic case 'a':
752*dded96e1Sbostic return 007; /* Bell (alert) char */
753*dded96e1Sbostic case 'b':
754*dded96e1Sbostic return '\b';
755*dded96e1Sbostic case 'e': /* Escape character */
756*dded96e1Sbostic return 033;
757*dded96e1Sbostic case 'f':
758*dded96e1Sbostic return '\f';
759*dded96e1Sbostic case 'n':
760*dded96e1Sbostic return '\n';
761*dded96e1Sbostic case 'r':
762*dded96e1Sbostic return '\r';
763*dded96e1Sbostic case 't':
764*dded96e1Sbostic return '\t';
765*dded96e1Sbostic case 'v':
766*dded96e1Sbostic return '\v';
767*dded96e1Sbostic case '\n':
768*dded96e1Sbostic return -2;
769*dded96e1Sbostic case 0:
770*dded96e1Sbostic (*string_ptr)--;
771*dded96e1Sbostic return 0;
772*dded96e1Sbostic case '^':
773*dded96e1Sbostic c = *(*string_ptr)++;
774*dded96e1Sbostic if (c == '\\')
775*dded96e1Sbostic c = parse_escape (string_ptr);
776*dded96e1Sbostic if (c == '?')
777*dded96e1Sbostic return 0177;
778*dded96e1Sbostic return (c & 0200) | (c & 037);
779*dded96e1Sbostic
780*dded96e1Sbostic case '0':
781*dded96e1Sbostic case '1':
782*dded96e1Sbostic case '2':
783*dded96e1Sbostic case '3':
784*dded96e1Sbostic case '4':
785*dded96e1Sbostic case '5':
786*dded96e1Sbostic case '6':
787*dded96e1Sbostic case '7':
788*dded96e1Sbostic {
789*dded96e1Sbostic register int i = c - '0';
790*dded96e1Sbostic register int count = 0;
791*dded96e1Sbostic while (++count < 3)
792*dded96e1Sbostic {
793*dded96e1Sbostic if ((c = *(*string_ptr)++) >= '0' && c <= '7')
794*dded96e1Sbostic {
795*dded96e1Sbostic i *= 8;
796*dded96e1Sbostic i += c - '0';
797*dded96e1Sbostic }
798*dded96e1Sbostic else
799*dded96e1Sbostic {
800*dded96e1Sbostic (*string_ptr)--;
801*dded96e1Sbostic break;
802*dded96e1Sbostic }
803*dded96e1Sbostic }
804*dded96e1Sbostic return i;
805*dded96e1Sbostic }
806*dded96e1Sbostic default:
807*dded96e1Sbostic return c;
808*dded96e1Sbostic }
809*dded96e1Sbostic }
810*dded96e1Sbostic
811*dded96e1Sbostic /* Print the character C on STREAM as part of the contents
812*dded96e1Sbostic of a literal string whose delimiter is QUOTER. */
813*dded96e1Sbostic
814*dded96e1Sbostic void
printchar(c,stream,quoter)815*dded96e1Sbostic printchar (c, stream, quoter)
816*dded96e1Sbostic register int c;
817*dded96e1Sbostic FILE *stream;
818*dded96e1Sbostic int quoter;
819*dded96e1Sbostic {
820*dded96e1Sbostic
821*dded96e1Sbostic c &= 0xFF; /* Avoid sign bit follies */
822*dded96e1Sbostic
823*dded96e1Sbostic if ( c < 0x20 || /* Low control chars */
824*dded96e1Sbostic (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
825*dded96e1Sbostic (sevenbit_strings && c >= 0x80)) { /* high order bit set */
826*dded96e1Sbostic switch (c)
827*dded96e1Sbostic {
828*dded96e1Sbostic case '\n':
829*dded96e1Sbostic fputs_filtered ("\\n", stream);
830*dded96e1Sbostic break;
831*dded96e1Sbostic case '\b':
832*dded96e1Sbostic fputs_filtered ("\\b", stream);
833*dded96e1Sbostic break;
834*dded96e1Sbostic case '\t':
835*dded96e1Sbostic fputs_filtered ("\\t", stream);
836*dded96e1Sbostic break;
837*dded96e1Sbostic case '\f':
838*dded96e1Sbostic fputs_filtered ("\\f", stream);
839*dded96e1Sbostic break;
840*dded96e1Sbostic case '\r':
841*dded96e1Sbostic fputs_filtered ("\\r", stream);
842*dded96e1Sbostic break;
843*dded96e1Sbostic case '\033':
844*dded96e1Sbostic fputs_filtered ("\\e", stream);
845*dded96e1Sbostic break;
846*dded96e1Sbostic case '\007':
847*dded96e1Sbostic fputs_filtered ("\\a", stream);
848*dded96e1Sbostic break;
849*dded96e1Sbostic default:
850*dded96e1Sbostic fprintf_filtered (stream, "\\%X", (unsigned int) c);
851*dded96e1Sbostic break;
852*dded96e1Sbostic }
853*dded96e1Sbostic } else {
854*dded96e1Sbostic if (c == '\\' || c == quoter)
855*dded96e1Sbostic fputs_filtered ("\\", stream);
856*dded96e1Sbostic fprintf_filtered (stream, "%c", c);
857*dded96e1Sbostic }
858*dded96e1Sbostic }
859*dded96e1Sbostic
860*dded96e1Sbostic /* Number of lines per page or UINT_MAX if paging is disabled. */
861*dded96e1Sbostic static unsigned int lines_per_page;
862*dded96e1Sbostic /* Number of chars per line or UNIT_MAX is line folding is disabled. */
863*dded96e1Sbostic static unsigned int chars_per_line;
864*dded96e1Sbostic /* Current count of lines printed on this page, chars on this line. */
865*dded96e1Sbostic static unsigned int lines_printed, chars_printed;
866*dded96e1Sbostic
867*dded96e1Sbostic /* Buffer and start column of buffered text, for doing smarter word-
868*dded96e1Sbostic wrapping. When someone calls wrap_here(), we start buffering output
869*dded96e1Sbostic that comes through fputs_filtered(). If we see a newline, we just
870*dded96e1Sbostic spit it out and forget about the wrap_here(). If we see another
871*dded96e1Sbostic wrap_here(), we spit it out and remember the newer one. If we see
872*dded96e1Sbostic the end of the line, we spit out a newline, the indent, and then
873*dded96e1Sbostic the buffered output.
874*dded96e1Sbostic
875*dded96e1Sbostic wrap_column is the column number on the screen where wrap_buffer begins.
876*dded96e1Sbostic When wrap_column is zero, wrapping is not in effect.
877*dded96e1Sbostic wrap_buffer is malloc'd with chars_per_line+2 bytes.
878*dded96e1Sbostic When wrap_buffer[0] is null, the buffer is empty.
879*dded96e1Sbostic wrap_pointer points into it at the next character to fill.
880*dded96e1Sbostic wrap_indent is the string that should be used as indentation if the
881*dded96e1Sbostic wrap occurs. */
882*dded96e1Sbostic
883*dded96e1Sbostic static char *wrap_buffer, *wrap_pointer, *wrap_indent;
884*dded96e1Sbostic static int wrap_column;
885*dded96e1Sbostic
886*dded96e1Sbostic /* ARGSUSED */
887*dded96e1Sbostic static void
set_width_command(args,from_tty,c)888*dded96e1Sbostic set_width_command (args, from_tty, c)
889*dded96e1Sbostic char *args;
890*dded96e1Sbostic int from_tty;
891*dded96e1Sbostic struct cmd_list_element *c;
892*dded96e1Sbostic {
893*dded96e1Sbostic if (!wrap_buffer)
894*dded96e1Sbostic {
895*dded96e1Sbostic wrap_buffer = (char *) xmalloc (chars_per_line + 2);
896*dded96e1Sbostic wrap_buffer[0] = '\0';
897*dded96e1Sbostic }
898*dded96e1Sbostic else
899*dded96e1Sbostic wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
900*dded96e1Sbostic wrap_pointer = wrap_buffer; /* Start it at the beginning */
901*dded96e1Sbostic }
902*dded96e1Sbostic
903*dded96e1Sbostic extern FILE *instream;
904*dded96e1Sbostic
905*dded96e1Sbostic static void
instream_cleanup(stream)906*dded96e1Sbostic instream_cleanup(stream)
907*dded96e1Sbostic FILE *stream;
908*dded96e1Sbostic {
909*dded96e1Sbostic instream = stream;
910*dded96e1Sbostic }
911*dded96e1Sbostic
912*dded96e1Sbostic static void
prompt_for_continue()913*dded96e1Sbostic prompt_for_continue ()
914*dded96e1Sbostic {
915*dded96e1Sbostic if (ISATTY(stdin) && ISATTY(stdout))
916*dded96e1Sbostic {
917*dded96e1Sbostic struct cleanup *old_chain = make_cleanup(instream_cleanup, instream);
918*dded96e1Sbostic char *cp;
919*dded96e1Sbostic
920*dded96e1Sbostic instream = stdin;
921*dded96e1Sbostic immediate_quit++;
922*dded96e1Sbostic cp = gdb_readline ("---Type <return> to continue---");
923*dded96e1Sbostic if (cp)
924*dded96e1Sbostic free (cp);
925*dded96e1Sbostic chars_printed = lines_printed = 0;
926*dded96e1Sbostic immediate_quit--;
927*dded96e1Sbostic do_cleanups(old_chain);
928*dded96e1Sbostic dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
929*dded96e1Sbostic }
930*dded96e1Sbostic }
931*dded96e1Sbostic
932*dded96e1Sbostic /* Reinitialize filter; ie. tell it to reset to original values. */
933*dded96e1Sbostic
934*dded96e1Sbostic void
reinitialize_more_filter()935*dded96e1Sbostic reinitialize_more_filter ()
936*dded96e1Sbostic {
937*dded96e1Sbostic lines_printed = 0;
938*dded96e1Sbostic chars_printed = 0;
939*dded96e1Sbostic }
940*dded96e1Sbostic
941*dded96e1Sbostic /* Indicate that if the next sequence of characters overflows the line,
942*dded96e1Sbostic a newline should be inserted here rather than when it hits the end.
943*dded96e1Sbostic If INDENT is nonzero, it is a string to be printed to indent the
944*dded96e1Sbostic wrapped part on the next line. INDENT must remain accessible until
945*dded96e1Sbostic the next call to wrap_here() or until a newline is printed through
946*dded96e1Sbostic fputs_filtered().
947*dded96e1Sbostic
948*dded96e1Sbostic If the line is already overfull, we immediately print a newline and
949*dded96e1Sbostic the indentation, and disable further wrapping.
950*dded96e1Sbostic
951*dded96e1Sbostic If we don't know the width of lines, but we know the page height,
952*dded96e1Sbostic we must not wrap words, but should still keep track of newlines
953*dded96e1Sbostic that were explicitly printed.
954*dded96e1Sbostic
955*dded96e1Sbostic INDENT should not contain tabs, as that
956*dded96e1Sbostic will mess up the char count on the next line. FIXME. */
957*dded96e1Sbostic
958*dded96e1Sbostic void
wrap_here(indent)959*dded96e1Sbostic wrap_here(indent)
960*dded96e1Sbostic char *indent;
961*dded96e1Sbostic {
962*dded96e1Sbostic if (wrap_buffer[0])
963*dded96e1Sbostic {
964*dded96e1Sbostic *wrap_pointer = '\0';
965*dded96e1Sbostic fputs (wrap_buffer, stdout);
966*dded96e1Sbostic }
967*dded96e1Sbostic wrap_pointer = wrap_buffer;
968*dded96e1Sbostic wrap_buffer[0] = '\0';
969*dded96e1Sbostic if (chars_per_line == UINT_MAX) /* No line overflow checking */
970*dded96e1Sbostic {
971*dded96e1Sbostic wrap_column = 0;
972*dded96e1Sbostic }
973*dded96e1Sbostic else if (chars_printed >= chars_per_line)
974*dded96e1Sbostic {
975*dded96e1Sbostic puts_filtered ("\n");
976*dded96e1Sbostic puts_filtered (indent);
977*dded96e1Sbostic wrap_column = 0;
978*dded96e1Sbostic }
979*dded96e1Sbostic else
980*dded96e1Sbostic {
981*dded96e1Sbostic wrap_column = chars_printed;
982*dded96e1Sbostic wrap_indent = indent;
983*dded96e1Sbostic }
984*dded96e1Sbostic }
985*dded96e1Sbostic
986*dded96e1Sbostic /* Like fputs but pause after every screenful, and can wrap at points
987*dded96e1Sbostic other than the final character of a line.
988*dded96e1Sbostic Unlike fputs, fputs_filtered does not return a value.
989*dded96e1Sbostic It is OK for LINEBUFFER to be NULL, in which case just don't print
990*dded96e1Sbostic anything.
991*dded96e1Sbostic
992*dded96e1Sbostic Note that a longjmp to top level may occur in this routine
993*dded96e1Sbostic (since prompt_for_continue may do so) so this routine should not be
994*dded96e1Sbostic called when cleanups are not in place. */
995*dded96e1Sbostic
996*dded96e1Sbostic void
fputs_filtered(linebuffer,stream)997*dded96e1Sbostic fputs_filtered (linebuffer, stream)
998*dded96e1Sbostic const char *linebuffer;
999*dded96e1Sbostic FILE *stream;
1000*dded96e1Sbostic {
1001*dded96e1Sbostic const char *lineptr;
1002*dded96e1Sbostic
1003*dded96e1Sbostic if (linebuffer == 0)
1004*dded96e1Sbostic return;
1005*dded96e1Sbostic
1006*dded96e1Sbostic /* Don't do any filtering if it is disabled. */
1007*dded96e1Sbostic if (stream != stdout
1008*dded96e1Sbostic || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1009*dded96e1Sbostic {
1010*dded96e1Sbostic fputs (linebuffer, stream);
1011*dded96e1Sbostic return;
1012*dded96e1Sbostic }
1013*dded96e1Sbostic
1014*dded96e1Sbostic /* Go through and output each character. Show line extension
1015*dded96e1Sbostic when this is necessary; prompt user for new page when this is
1016*dded96e1Sbostic necessary. */
1017*dded96e1Sbostic
1018*dded96e1Sbostic lineptr = linebuffer;
1019*dded96e1Sbostic while (*lineptr)
1020*dded96e1Sbostic {
1021*dded96e1Sbostic /* Possible new page. */
1022*dded96e1Sbostic if (lines_printed >= lines_per_page - 1)
1023*dded96e1Sbostic prompt_for_continue ();
1024*dded96e1Sbostic
1025*dded96e1Sbostic while (*lineptr && *lineptr != '\n')
1026*dded96e1Sbostic {
1027*dded96e1Sbostic /* Print a single line. */
1028*dded96e1Sbostic if (*lineptr == '\t')
1029*dded96e1Sbostic {
1030*dded96e1Sbostic if (wrap_column)
1031*dded96e1Sbostic *wrap_pointer++ = '\t';
1032*dded96e1Sbostic else
1033*dded96e1Sbostic putc ('\t', stream);
1034*dded96e1Sbostic /* Shifting right by 3 produces the number of tab stops
1035*dded96e1Sbostic we have already passed, and then adding one and
1036*dded96e1Sbostic shifting left 3 advances to the next tab stop. */
1037*dded96e1Sbostic chars_printed = ((chars_printed >> 3) + 1) << 3;
1038*dded96e1Sbostic lineptr++;
1039*dded96e1Sbostic }
1040*dded96e1Sbostic else
1041*dded96e1Sbostic {
1042*dded96e1Sbostic if (wrap_column)
1043*dded96e1Sbostic *wrap_pointer++ = *lineptr;
1044*dded96e1Sbostic else
1045*dded96e1Sbostic putc (*lineptr, stream);
1046*dded96e1Sbostic chars_printed++;
1047*dded96e1Sbostic lineptr++;
1048*dded96e1Sbostic }
1049*dded96e1Sbostic
1050*dded96e1Sbostic if (chars_printed >= chars_per_line)
1051*dded96e1Sbostic {
1052*dded96e1Sbostic unsigned int save_chars = chars_printed;
1053*dded96e1Sbostic
1054*dded96e1Sbostic chars_printed = 0;
1055*dded96e1Sbostic lines_printed++;
1056*dded96e1Sbostic /* If we aren't actually wrapping, don't output newline --
1057*dded96e1Sbostic if chars_per_line is right, we probably just overflowed
1058*dded96e1Sbostic anyway; if it's wrong, let us keep going. */
1059*dded96e1Sbostic if (wrap_column)
1060*dded96e1Sbostic putc ('\n', stream);
1061*dded96e1Sbostic
1062*dded96e1Sbostic /* Possible new page. */
1063*dded96e1Sbostic if (lines_printed >= lines_per_page - 1)
1064*dded96e1Sbostic prompt_for_continue ();
1065*dded96e1Sbostic
1066*dded96e1Sbostic /* Now output indentation and wrapped string */
1067*dded96e1Sbostic if (wrap_column)
1068*dded96e1Sbostic {
1069*dded96e1Sbostic if (wrap_indent)
1070*dded96e1Sbostic fputs (wrap_indent, stream);
1071*dded96e1Sbostic *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1072*dded96e1Sbostic fputs (wrap_buffer, stream); /* and eject it */
1073*dded96e1Sbostic /* FIXME, this strlen is what prevents wrap_indent from
1074*dded96e1Sbostic containing tabs. However, if we recurse to print it
1075*dded96e1Sbostic and count its chars, we risk trouble if wrap_indent is
1076*dded96e1Sbostic longer than (the user settable) chars_per_line.
1077*dded96e1Sbostic Note also that this can set chars_printed > chars_per_line
1078*dded96e1Sbostic if we are printing a long string. */
1079*dded96e1Sbostic chars_printed = strlen (wrap_indent)
1080*dded96e1Sbostic + (save_chars - wrap_column);
1081*dded96e1Sbostic wrap_pointer = wrap_buffer; /* Reset buffer */
1082*dded96e1Sbostic wrap_buffer[0] = '\0';
1083*dded96e1Sbostic wrap_column = 0; /* And disable fancy wrap */
1084*dded96e1Sbostic }
1085*dded96e1Sbostic }
1086*dded96e1Sbostic }
1087*dded96e1Sbostic
1088*dded96e1Sbostic if (*lineptr == '\n')
1089*dded96e1Sbostic {
1090*dded96e1Sbostic chars_printed = 0;
1091*dded96e1Sbostic wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1092*dded96e1Sbostic lines_printed++;
1093*dded96e1Sbostic putc ('\n', stream);
1094*dded96e1Sbostic lineptr++;
1095*dded96e1Sbostic }
1096*dded96e1Sbostic }
1097*dded96e1Sbostic }
1098*dded96e1Sbostic
1099*dded96e1Sbostic
1100*dded96e1Sbostic /* fputs_demangled is a variant of fputs_filtered that
1101*dded96e1Sbostic demangles g++ names.*/
1102*dded96e1Sbostic
1103*dded96e1Sbostic void
fputs_demangled(linebuffer,stream,arg_mode)1104*dded96e1Sbostic fputs_demangled (linebuffer, stream, arg_mode)
1105*dded96e1Sbostic char *linebuffer;
1106*dded96e1Sbostic FILE *stream;
1107*dded96e1Sbostic int arg_mode;
1108*dded96e1Sbostic {
1109*dded96e1Sbostic #define SYMBOL_MAX 1024
1110*dded96e1Sbostic
1111*dded96e1Sbostic #define SYMBOL_CHAR(c) (isascii(c) \
1112*dded96e1Sbostic && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
1113*dded96e1Sbostic
1114*dded96e1Sbostic char buf[SYMBOL_MAX+1];
1115*dded96e1Sbostic # define DMSLOP 5 /* How much room to leave in buf */
1116*dded96e1Sbostic char *p;
1117*dded96e1Sbostic
1118*dded96e1Sbostic if (linebuffer == NULL)
1119*dded96e1Sbostic return;
1120*dded96e1Sbostic
1121*dded96e1Sbostic /* If user wants to see raw output, no problem. */
1122*dded96e1Sbostic if (!demangle) {
1123*dded96e1Sbostic fputs_filtered (linebuffer, stream);
1124*dded96e1Sbostic return;
1125*dded96e1Sbostic }
1126*dded96e1Sbostic
1127*dded96e1Sbostic p = linebuffer;
1128*dded96e1Sbostic
1129*dded96e1Sbostic while ( *p != (char) 0 ) {
1130*dded96e1Sbostic int i = 0;
1131*dded96e1Sbostic
1132*dded96e1Sbostic /* collect non-interesting characters into buf */
1133*dded96e1Sbostic while (*p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-DMSLOP ) {
1134*dded96e1Sbostic buf[i++] = *p;
1135*dded96e1Sbostic p++;
1136*dded96e1Sbostic }
1137*dded96e1Sbostic if (i > 0) {
1138*dded96e1Sbostic /* output the non-interesting characters without demangling */
1139*dded96e1Sbostic buf[i] = (char) 0;
1140*dded96e1Sbostic fputs_filtered(buf, stream);
1141*dded96e1Sbostic i = 0; /* reset buf */
1142*dded96e1Sbostic }
1143*dded96e1Sbostic
1144*dded96e1Sbostic /* and now the interesting characters */
1145*dded96e1Sbostic while (i < SYMBOL_MAX
1146*dded96e1Sbostic && *p != (char) 0
1147*dded96e1Sbostic && SYMBOL_CHAR(*p)
1148*dded96e1Sbostic && i < (int)sizeof(buf) - DMSLOP) {
1149*dded96e1Sbostic buf[i++] = *p;
1150*dded96e1Sbostic p++;
1151*dded96e1Sbostic }
1152*dded96e1Sbostic buf[i] = (char) 0;
1153*dded96e1Sbostic if (i > 0) {
1154*dded96e1Sbostic char * result;
1155*dded96e1Sbostic
1156*dded96e1Sbostic if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
1157*dded96e1Sbostic fputs_filtered(result, stream);
1158*dded96e1Sbostic free(result);
1159*dded96e1Sbostic }
1160*dded96e1Sbostic else {
1161*dded96e1Sbostic fputs_filtered(buf, stream);
1162*dded96e1Sbostic }
1163*dded96e1Sbostic }
1164*dded96e1Sbostic }
1165*dded96e1Sbostic }
1166*dded96e1Sbostic
1167*dded96e1Sbostic /* Print a variable number of ARGS using format FORMAT. If this
1168*dded96e1Sbostic information is going to put the amount written (since the last call
1169*dded96e1Sbostic to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1170*dded96e1Sbostic print out a pause message and do a gdb_readline to get the users
1171*dded96e1Sbostic permision to continue.
1172*dded96e1Sbostic
1173*dded96e1Sbostic Unlike fprintf, this function does not return a value.
1174*dded96e1Sbostic
1175*dded96e1Sbostic We implement three variants, vfprintf (takes a vararg list and stream),
1176*dded96e1Sbostic fprintf (takes a stream to write on), and printf (the usual).
1177*dded96e1Sbostic
1178*dded96e1Sbostic Note that this routine has a restriction that the length of the
1179*dded96e1Sbostic final output line must be less than 255 characters *or* it must be
1180*dded96e1Sbostic less than twice the size of the format string. This is a very
1181*dded96e1Sbostic arbitrary restriction, but it is an internal restriction, so I'll
1182*dded96e1Sbostic put it in. This means that the %s format specifier is almost
1183*dded96e1Sbostic useless; unless the caller can GUARANTEE that the string is short
1184*dded96e1Sbostic enough, fputs_filtered should be used instead.
1185*dded96e1Sbostic
1186*dded96e1Sbostic Note also that a longjmp to top level may occur in this routine
1187*dded96e1Sbostic (since prompt_for_continue may do so) so this routine should not be
1188*dded96e1Sbostic called when cleanups are not in place. */
1189*dded96e1Sbostic
1190*dded96e1Sbostic #define MIN_LINEBUF 255
1191*dded96e1Sbostic
1192*dded96e1Sbostic void
vfprintf_filtered(stream,format,args)1193*dded96e1Sbostic vfprintf_filtered (stream, format, args)
1194*dded96e1Sbostic FILE *stream;
1195*dded96e1Sbostic char *format;
1196*dded96e1Sbostic va_list args;
1197*dded96e1Sbostic {
1198*dded96e1Sbostic char line_buf[MIN_LINEBUF+10];
1199*dded96e1Sbostic char *linebuffer = line_buf;
1200*dded96e1Sbostic int format_length;
1201*dded96e1Sbostic
1202*dded96e1Sbostic format_length = strlen (format);
1203*dded96e1Sbostic
1204*dded96e1Sbostic /* Reallocate buffer to a larger size if this is necessary. */
1205*dded96e1Sbostic if (format_length * 2 > MIN_LINEBUF)
1206*dded96e1Sbostic {
1207*dded96e1Sbostic linebuffer = alloca (10 + format_length * 2);
1208*dded96e1Sbostic }
1209*dded96e1Sbostic
1210*dded96e1Sbostic /* This won't blow up if the restrictions described above are
1211*dded96e1Sbostic followed. */
1212*dded96e1Sbostic vsprintf (linebuffer, format, args);
1213*dded96e1Sbostic
1214*dded96e1Sbostic fputs_filtered (linebuffer, stream);
1215*dded96e1Sbostic }
1216*dded96e1Sbostic
1217*dded96e1Sbostic /* VARARGS */
1218*dded96e1Sbostic void
fprintf_filtered(va_alist)1219*dded96e1Sbostic fprintf_filtered (va_alist)
1220*dded96e1Sbostic va_dcl
1221*dded96e1Sbostic {
1222*dded96e1Sbostic va_list args;
1223*dded96e1Sbostic FILE *stream;
1224*dded96e1Sbostic char *format;
1225*dded96e1Sbostic
1226*dded96e1Sbostic va_start (args);
1227*dded96e1Sbostic stream = va_arg (args, FILE *);
1228*dded96e1Sbostic format = va_arg (args, char *);
1229*dded96e1Sbostic
1230*dded96e1Sbostic /* This won't blow up if the restrictions described above are
1231*dded96e1Sbostic followed. */
1232*dded96e1Sbostic vfprintf_filtered (stream, format, args);
1233*dded96e1Sbostic va_end (args);
1234*dded96e1Sbostic }
1235*dded96e1Sbostic
1236*dded96e1Sbostic /* Like fprintf_filtered, but prints it's result indent.
1237*dded96e1Sbostic Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */
1238*dded96e1Sbostic
1239*dded96e1Sbostic /* VARARGS */
1240*dded96e1Sbostic void
fprintfi_filtered(va_alist)1241*dded96e1Sbostic fprintfi_filtered (va_alist)
1242*dded96e1Sbostic va_dcl
1243*dded96e1Sbostic {
1244*dded96e1Sbostic va_list args;
1245*dded96e1Sbostic int spaces;
1246*dded96e1Sbostic FILE *stream;
1247*dded96e1Sbostic char *format;
1248*dded96e1Sbostic
1249*dded96e1Sbostic va_start (args);
1250*dded96e1Sbostic spaces = va_arg (args, int);
1251*dded96e1Sbostic stream = va_arg (args, FILE *);
1252*dded96e1Sbostic format = va_arg (args, char *);
1253*dded96e1Sbostic print_spaces_filtered (spaces, stream);
1254*dded96e1Sbostic
1255*dded96e1Sbostic /* This won't blow up if the restrictions described above are
1256*dded96e1Sbostic followed. */
1257*dded96e1Sbostic vfprintf_filtered (stream, format, args);
1258*dded96e1Sbostic va_end (args);
1259*dded96e1Sbostic }
1260*dded96e1Sbostic
1261*dded96e1Sbostic /* VARARGS */
1262*dded96e1Sbostic void
printf_filtered(va_alist)1263*dded96e1Sbostic printf_filtered (va_alist)
1264*dded96e1Sbostic va_dcl
1265*dded96e1Sbostic {
1266*dded96e1Sbostic va_list args;
1267*dded96e1Sbostic char *format;
1268*dded96e1Sbostic
1269*dded96e1Sbostic va_start (args);
1270*dded96e1Sbostic format = va_arg (args, char *);
1271*dded96e1Sbostic
1272*dded96e1Sbostic vfprintf_filtered (stdout, format, args);
1273*dded96e1Sbostic va_end (args);
1274*dded96e1Sbostic }
1275*dded96e1Sbostic
1276*dded96e1Sbostic /* Like printf_filtered, but prints it's result indented.
1277*dded96e1Sbostic Called as printfi_filtered (spaces, format, arg1, arg2, ...); */
1278*dded96e1Sbostic
1279*dded96e1Sbostic /* VARARGS */
1280*dded96e1Sbostic void
printfi_filtered(va_alist)1281*dded96e1Sbostic printfi_filtered (va_alist)
1282*dded96e1Sbostic va_dcl
1283*dded96e1Sbostic {
1284*dded96e1Sbostic va_list args;
1285*dded96e1Sbostic int spaces;
1286*dded96e1Sbostic char *format;
1287*dded96e1Sbostic
1288*dded96e1Sbostic va_start (args);
1289*dded96e1Sbostic spaces = va_arg (args, int);
1290*dded96e1Sbostic format = va_arg (args, char *);
1291*dded96e1Sbostic print_spaces_filtered (spaces, stdout);
1292*dded96e1Sbostic vfprintf_filtered (stdout, format, args);
1293*dded96e1Sbostic va_end (args);
1294*dded96e1Sbostic }
1295*dded96e1Sbostic
1296*dded96e1Sbostic /* Easy -- but watch out!
1297*dded96e1Sbostic
1298*dded96e1Sbostic This routine is *not* a replacement for puts()! puts() appends a newline.
1299*dded96e1Sbostic This one doesn't, and had better not! */
1300*dded96e1Sbostic
1301*dded96e1Sbostic void
puts_filtered(string)1302*dded96e1Sbostic puts_filtered (string)
1303*dded96e1Sbostic char *string;
1304*dded96e1Sbostic {
1305*dded96e1Sbostic fputs_filtered (string, stdout);
1306*dded96e1Sbostic }
1307*dded96e1Sbostic
1308*dded96e1Sbostic /* Return a pointer to N spaces and a null. The pointer is good
1309*dded96e1Sbostic until the next call to here. */
1310*dded96e1Sbostic char *
n_spaces(n)1311*dded96e1Sbostic n_spaces (n)
1312*dded96e1Sbostic int n;
1313*dded96e1Sbostic {
1314*dded96e1Sbostic register char *t;
1315*dded96e1Sbostic static char *spaces;
1316*dded96e1Sbostic static int max_spaces;
1317*dded96e1Sbostic
1318*dded96e1Sbostic if (n > max_spaces)
1319*dded96e1Sbostic {
1320*dded96e1Sbostic if (spaces)
1321*dded96e1Sbostic free (spaces);
1322*dded96e1Sbostic spaces = (char *) xmalloc (n+1);
1323*dded96e1Sbostic for (t = spaces+n; t != spaces;)
1324*dded96e1Sbostic *--t = ' ';
1325*dded96e1Sbostic spaces[n] = '\0';
1326*dded96e1Sbostic max_spaces = n;
1327*dded96e1Sbostic }
1328*dded96e1Sbostic
1329*dded96e1Sbostic return spaces + max_spaces - n;
1330*dded96e1Sbostic }
1331*dded96e1Sbostic
1332*dded96e1Sbostic /* Print N spaces. */
1333*dded96e1Sbostic void
print_spaces_filtered(n,stream)1334*dded96e1Sbostic print_spaces_filtered (n, stream)
1335*dded96e1Sbostic int n;
1336*dded96e1Sbostic FILE *stream;
1337*dded96e1Sbostic {
1338*dded96e1Sbostic fputs_filtered (n_spaces (n), stream);
1339*dded96e1Sbostic }
1340*dded96e1Sbostic
1341*dded96e1Sbostic /* C++ demangler stuff. */
1342*dded96e1Sbostic
1343*dded96e1Sbostic /* Make a copy of a symbol, applying C++ demangling if demangling is enabled
1344*dded96e1Sbostic and a demangled version exists. Note that the value returned from
1345*dded96e1Sbostic cplus_demangle is already allocated in malloc'd memory. */
1346*dded96e1Sbostic
1347*dded96e1Sbostic char *
strdup_demangled(name)1348*dded96e1Sbostic strdup_demangled (name)
1349*dded96e1Sbostic const char *name;
1350*dded96e1Sbostic {
1351*dded96e1Sbostic char *demangled = NULL;
1352*dded96e1Sbostic
1353*dded96e1Sbostic if (demangle)
1354*dded96e1Sbostic {
1355*dded96e1Sbostic demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI);
1356*dded96e1Sbostic }
1357*dded96e1Sbostic return ((demangled != NULL) ? demangled : strdup (name));
1358*dded96e1Sbostic }
1359*dded96e1Sbostic
1360*dded96e1Sbostic
1361*dded96e1Sbostic /* Print NAME on STREAM, demangling if necessary. */
1362*dded96e1Sbostic void
fprint_symbol(stream,name)1363*dded96e1Sbostic fprint_symbol (stream, name)
1364*dded96e1Sbostic FILE *stream;
1365*dded96e1Sbostic char *name;
1366*dded96e1Sbostic {
1367*dded96e1Sbostic char *demangled;
1368*dded96e1Sbostic if ((!demangle)
1369*dded96e1Sbostic || NULL == (demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI)))
1370*dded96e1Sbostic fputs_filtered (name, stream);
1371*dded96e1Sbostic else
1372*dded96e1Sbostic {
1373*dded96e1Sbostic fputs_filtered (demangled, stream);
1374*dded96e1Sbostic free (demangled);
1375*dded96e1Sbostic }
1376*dded96e1Sbostic }
1377*dded96e1Sbostic
1378*dded96e1Sbostic /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1379*dded96e1Sbostic differences in whitespace. Returns 0 if they match, non-zero if they
1380*dded96e1Sbostic don't (slightly different than strcmp()'s range of return values).
1381*dded96e1Sbostic
1382*dded96e1Sbostic As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1383*dded96e1Sbostic This "feature" is useful for demangle_and_match(), which is used
1384*dded96e1Sbostic when searching for matching C++ function names (such as if the
1385*dded96e1Sbostic user types 'break FOO', where FOO is a mangled C++ function). */
1386*dded96e1Sbostic
1387*dded96e1Sbostic int
strcmp_iw(string1,string2)1388*dded96e1Sbostic strcmp_iw (string1, string2)
1389*dded96e1Sbostic const char *string1;
1390*dded96e1Sbostic const char *string2;
1391*dded96e1Sbostic {
1392*dded96e1Sbostic while ((*string1 != '\0') && (*string2 != '\0'))
1393*dded96e1Sbostic {
1394*dded96e1Sbostic while (isspace (*string1))
1395*dded96e1Sbostic {
1396*dded96e1Sbostic string1++;
1397*dded96e1Sbostic }
1398*dded96e1Sbostic while (isspace (*string2))
1399*dded96e1Sbostic {
1400*dded96e1Sbostic string2++;
1401*dded96e1Sbostic }
1402*dded96e1Sbostic if (*string1 != *string2)
1403*dded96e1Sbostic {
1404*dded96e1Sbostic break;
1405*dded96e1Sbostic }
1406*dded96e1Sbostic if (*string1 != '\0')
1407*dded96e1Sbostic {
1408*dded96e1Sbostic string1++;
1409*dded96e1Sbostic string2++;
1410*dded96e1Sbostic }
1411*dded96e1Sbostic }
1412*dded96e1Sbostic return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1413*dded96e1Sbostic }
1414*dded96e1Sbostic
1415*dded96e1Sbostic /* Demangle NAME and compare the result with LOOKFOR, ignoring any differences
1416*dded96e1Sbostic in whitespace.
1417*dded96e1Sbostic
1418*dded96e1Sbostic If a match is found, returns a pointer to the demangled version of NAME
1419*dded96e1Sbostic in malloc'd memory, which needs to be freed by the caller after use.
1420*dded96e1Sbostic If a match is not found, returns NULL.
1421*dded96e1Sbostic
1422*dded96e1Sbostic OPTIONS is a flags word that controls the demangling process and is just
1423*dded96e1Sbostic passed on to the demangler.
1424*dded96e1Sbostic
1425*dded96e1Sbostic When the caller sees a non-NULL result, it knows that NAME is the mangled
1426*dded96e1Sbostic equivalent of LOOKFOR, and it can use either NAME, the "official demangled"
1427*dded96e1Sbostic version of NAME (the return value) or the "unofficial demangled" version
1428*dded96e1Sbostic of NAME (LOOKFOR, which it already knows). */
1429*dded96e1Sbostic
1430*dded96e1Sbostic char *
demangle_and_match(name,lookfor,options)1431*dded96e1Sbostic demangle_and_match (name, lookfor, options)
1432*dded96e1Sbostic const char *name;
1433*dded96e1Sbostic const char *lookfor;
1434*dded96e1Sbostic int options;
1435*dded96e1Sbostic {
1436*dded96e1Sbostic char *demangled;
1437*dded96e1Sbostic
1438*dded96e1Sbostic if ((demangled = cplus_demangle (name, options)) != NULL)
1439*dded96e1Sbostic {
1440*dded96e1Sbostic if (strcmp_iw (demangled, lookfor) != 0)
1441*dded96e1Sbostic {
1442*dded96e1Sbostic free (demangled);
1443*dded96e1Sbostic demangled = NULL;
1444*dded96e1Sbostic }
1445*dded96e1Sbostic }
1446*dded96e1Sbostic return (demangled);
1447*dded96e1Sbostic }
1448*dded96e1Sbostic
1449*dded96e1Sbostic #ifdef TIOCGWINSZ
1450*dded96e1Sbostic #ifdef SIGWINCH
1451*dded96e1Sbostic static void
sigwinch()1452*dded96e1Sbostic sigwinch()
1453*dded96e1Sbostic {
1454*dded96e1Sbostic struct winsize win;
1455*dded96e1Sbostic
1456*dded96e1Sbostic if (ioctl(0, TIOCGWINSZ, (char *)&win) < 0) {
1457*dded96e1Sbostic perror("TIOCGWINSZ");
1458*dded96e1Sbostic return;
1459*dded96e1Sbostic }
1460*dded96e1Sbostic lines_per_page = win.ws_row;
1461*dded96e1Sbostic chars_per_line = win.ws_col;
1462*dded96e1Sbostic }
1463*dded96e1Sbostic
1464*dded96e1Sbostic #ifndef SIGWINCH_HANDLER
1465*dded96e1Sbostic #define SIGWINCH_HANDLER sigwinch
1466*dded96e1Sbostic #endif
1467*dded96e1Sbostic
1468*dded96e1Sbostic #endif
1469*dded96e1Sbostic
termdim()1470*dded96e1Sbostic termdim()
1471*dded96e1Sbostic {
1472*dded96e1Sbostic SIGWINCH_HANDLER();
1473*dded96e1Sbostic #ifdef SIGWINCH
1474*dded96e1Sbostic signal(SIGWINCH, SIGWINCH_HANDLER);
1475*dded96e1Sbostic #endif
1476*dded96e1Sbostic }
1477*dded96e1Sbostic
1478*dded96e1Sbostic #else
1479*dded96e1Sbostic /* Initialize the screen height and width from termcap. */
termdim()1480*dded96e1Sbostic termdim()
1481*dded96e1Sbostic {
1482*dded96e1Sbostic register int v;
1483*dded96e1Sbostic register char *cp;
1484*dded96e1Sbostic /* 2048 is large enough for all known terminals, according to the
1485*dded96e1Sbostic GNU termcap manual. */
1486*dded96e1Sbostic char term_buffer[2048];
1487*dded96e1Sbostic
1488*dded96e1Sbostic if ((termtype = getenv ("TERM")) == 0 || tgetent(term_buffer, cp) <= 0)
1489*dded96e1Sbostic return;
1490*dded96e1Sbostic
1491*dded96e1Sbostic v = tgetnum("li");
1492*dded96e1Sbostic if (v >= 0)
1493*dded96e1Sbostic lines_per_page = v;
1494*dded96e1Sbostic else
1495*dded96e1Sbostic /* The number of lines per page is not mentioned
1496*dded96e1Sbostic in the terminal description. This probably means
1497*dded96e1Sbostic that paging is not useful (e.g. emacs shell window),
1498*dded96e1Sbostic so disable paging. */
1499*dded96e1Sbostic lines_per_page = UINT_MAX;
1500*dded96e1Sbostic
1501*dded96e1Sbostic v = tgetnum("co");
1502*dded96e1Sbostic if (v >= 0)
1503*dded96e1Sbostic chars_per_line = v;
1504*dded96e1Sbostic }
1505*dded96e1Sbostic #endif
1506*dded96e1Sbostic
1507*dded96e1Sbostic void
_initialize_utils()1508*dded96e1Sbostic _initialize_utils ()
1509*dded96e1Sbostic {
1510*dded96e1Sbostic struct cmd_list_element *c;
1511*dded96e1Sbostic
1512*dded96e1Sbostic c = add_set_cmd ("width", class_support, var_uinteger,
1513*dded96e1Sbostic (char *)&chars_per_line,
1514*dded96e1Sbostic "Set number of characters gdb thinks are in a line.",
1515*dded96e1Sbostic &setlist);
1516*dded96e1Sbostic add_show_from_set (c, &showlist);
1517*dded96e1Sbostic c->function.sfunc = set_width_command;
1518*dded96e1Sbostic
1519*dded96e1Sbostic add_show_from_set
1520*dded96e1Sbostic (add_set_cmd ("height", class_support,
1521*dded96e1Sbostic var_uinteger, (char *)&lines_per_page,
1522*dded96e1Sbostic "Set number of lines gdb thinks are in a page.", &setlist),
1523*dded96e1Sbostic &showlist);
1524*dded96e1Sbostic
1525*dded96e1Sbostic /* These defaults will be used if we are unable to get the correct
1526*dded96e1Sbostic values from termcap. */
1527*dded96e1Sbostic #if defined(__GO32__)
1528*dded96e1Sbostic lines_per_page = ScreenRows();
1529*dded96e1Sbostic chars_per_line = ScreenCols();
1530*dded96e1Sbostic #else
1531*dded96e1Sbostic lines_per_page = 24;
1532*dded96e1Sbostic chars_per_line = 80;
1533*dded96e1Sbostic termdim();
1534*dded96e1Sbostic #endif
1535*dded96e1Sbostic /* If the output is not a terminal, don't paginate it. */
1536*dded96e1Sbostic if (!ISATTY (stdout))
1537*dded96e1Sbostic lines_per_page = UINT_MAX;
1538*dded96e1Sbostic
1539*dded96e1Sbostic set_width_command ((char *)NULL, 0, c);
1540*dded96e1Sbostic
1541*dded96e1Sbostic add_show_from_set
1542*dded96e1Sbostic (add_set_cmd ("demangle", class_support, var_boolean,
1543*dded96e1Sbostic (char *)&demangle,
1544*dded96e1Sbostic "Set demangling of encoded C++ names when displaying symbols.",
1545*dded96e1Sbostic &setprintlist),
1546*dded96e1Sbostic &showprintlist);
1547*dded96e1Sbostic
1548*dded96e1Sbostic add_show_from_set
1549*dded96e1Sbostic (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1550*dded96e1Sbostic (char *)&sevenbit_strings,
1551*dded96e1Sbostic "Set printing of 8-bit characters in strings as \\nnn.",
1552*dded96e1Sbostic &setprintlist),
1553*dded96e1Sbostic &showprintlist);
1554*dded96e1Sbostic
1555*dded96e1Sbostic add_show_from_set
1556*dded96e1Sbostic (add_set_cmd ("asm-demangle", class_support, var_boolean,
1557*dded96e1Sbostic (char *)&asm_demangle,
1558*dded96e1Sbostic "Set demangling of C++ names in disassembly listings.",
1559*dded96e1Sbostic &setprintlist),
1560*dded96e1Sbostic &showprintlist);
1561*dded96e1Sbostic }
1562*dded96e1Sbostic
1563*dded96e1Sbostic /* Machine specific function to handle SIGWINCH signal. */
1564*dded96e1Sbostic
1565*dded96e1Sbostic #ifdef SIGWINCH_HANDLER_BODY
1566*dded96e1Sbostic SIGWINCH_HANDLER_BODY
1567*dded96e1Sbostic #endif
1568