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