xref: /openbsd-src/gnu/usr.bin/binutils/gdb/utils.c (revision bb02a7f54797b51801cd7f69948ed635c0f8406b)
1e93f7393Sniklas /* General utility routines for GDB, the GNU debugger.
2b725ae77Skettenis 
3b725ae77Skettenis    Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4b725ae77Skettenis    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
5b725ae77Skettenis    Foundation, Inc.
6e93f7393Sniklas 
7e93f7393Sniklas    This file is part of GDB.
8e93f7393Sniklas 
9e93f7393Sniklas    This program is free software; you can redistribute it and/or modify
10e93f7393Sniklas    it under the terms of the GNU General Public License as published by
11e93f7393Sniklas    the Free Software Foundation; either version 2 of the License, or
12e93f7393Sniklas    (at your option) any later version.
13e93f7393Sniklas 
14e93f7393Sniklas    This program is distributed in the hope that it will be useful,
15e93f7393Sniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
16e93f7393Sniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17e93f7393Sniklas    GNU General Public License for more details.
18e93f7393Sniklas 
19e93f7393Sniklas    You should have received a copy of the GNU General Public License
20e93f7393Sniklas    along with this program; if not, write to the Free Software
21b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
22b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
23e93f7393Sniklas 
24e93f7393Sniklas #include "defs.h"
25b725ae77Skettenis #include "gdb_assert.h"
26e93f7393Sniklas #include <ctype.h>
27e93f7393Sniklas #include "gdb_string.h"
28b725ae77Skettenis #include "event-top.h"
29b725ae77Skettenis 
30b725ae77Skettenis #ifdef TUI
31b725ae77Skettenis #include "tui/tui.h"		/* For tui_get_command_dimension.   */
32e93f7393Sniklas #endif
33e93f7393Sniklas 
34b725ae77Skettenis #ifdef __GO32__
35b725ae77Skettenis #include <pc.h>
36b725ae77Skettenis #endif
37b725ae77Skettenis 
38b725ae77Skettenis /* SunOS's curses.h has a '#define reg register' in it.  Thank you Sun. */
39b725ae77Skettenis #ifdef reg
40b725ae77Skettenis #undef reg
41b725ae77Skettenis #endif
42b725ae77Skettenis 
43b725ae77Skettenis #include <signal.h>
44e93f7393Sniklas #include "gdbcmd.h"
45e93f7393Sniklas #include "serial.h"
46e93f7393Sniklas #include "bfd.h"
47e93f7393Sniklas #include "target.h"
48e93f7393Sniklas #include "demangle.h"
49e93f7393Sniklas #include "expression.h"
50e93f7393Sniklas #include "language.h"
51b725ae77Skettenis #include "charset.h"
52e93f7393Sniklas #include "annotate.h"
53b725ae77Skettenis #include "filenames.h"
5463addd46Skettenis #include "symfile.h"
55e93f7393Sniklas 
56b725ae77Skettenis #include "inferior.h"		/* for signed_pointer_to_address */
57b725ae77Skettenis 
58b725ae77Skettenis #include <sys/param.h>		/* For MAXPATHLEN */
59b725ae77Skettenis 
60b725ae77Skettenis #ifdef HAVE_CURSES_H
61b725ae77Skettenis #include <curses.h>
62b725ae77Skettenis #endif
63b725ae77Skettenis #ifdef HAVE_TERM_H
64b725ae77Skettenis #include <term.h>
65b725ae77Skettenis #endif
66b725ae77Skettenis 
67b725ae77Skettenis #include "readline/readline.h"
68b725ae77Skettenis 
69b725ae77Skettenis #ifdef NEED_DECLARATION_MALLOC
70b725ae77Skettenis extern PTR malloc ();		/* OK: PTR */
71b725ae77Skettenis #endif
72b725ae77Skettenis #ifdef NEED_DECLARATION_REALLOC
73b725ae77Skettenis extern PTR realloc ();		/* OK: PTR */
74b725ae77Skettenis #endif
75b725ae77Skettenis #ifdef NEED_DECLARATION_FREE
76b725ae77Skettenis extern void free ();
77b725ae77Skettenis #endif
78b725ae77Skettenis /* Actually, we'll never have the decl, since we don't define _GNU_SOURCE.  */
79b725ae77Skettenis #if defined(HAVE_CANONICALIZE_FILE_NAME) \
80b725ae77Skettenis     && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
81b725ae77Skettenis extern char *canonicalize_file_name (const char *);
82b725ae77Skettenis #endif
83e93f7393Sniklas 
84e93f7393Sniklas /* readline defines this.  */
85e93f7393Sniklas #undef savestring
86e93f7393Sniklas 
8763addd46Skettenis void (*deprecated_error_begin_hook) (void);
88b725ae77Skettenis 
89b725ae77Skettenis /* Holds the last error message issued by gdb */
90b725ae77Skettenis 
91b725ae77Skettenis static struct ui_file *gdb_lasterr;
92b725ae77Skettenis 
93e93f7393Sniklas /* Prototypes for local functions */
94e93f7393Sniklas 
95b725ae77Skettenis static void vfprintf_maybe_filtered (struct ui_file *, const char *,
96b725ae77Skettenis 				     va_list, int);
97e93f7393Sniklas 
98b725ae77Skettenis static void fputs_maybe_filtered (const char *, struct ui_file *, int);
99e93f7393Sniklas 
100b725ae77Skettenis static void do_my_cleanups (struct cleanup **, struct cleanup *);
101e93f7393Sniklas 
102b725ae77Skettenis static void prompt_for_continue (void);
103e93f7393Sniklas 
104b725ae77Skettenis static void set_screen_size (void);
105b725ae77Skettenis static void set_width (void);
106e93f7393Sniklas 
107e93f7393Sniklas /* Chain of cleanup actions established with make_cleanup,
108e93f7393Sniklas    to be executed if an error happens.  */
109e93f7393Sniklas 
110b725ae77Skettenis static struct cleanup *cleanup_chain;	/* cleaned up after a failed command */
111b725ae77Skettenis static struct cleanup *final_cleanup_chain;	/* cleaned up when gdb exits */
112b725ae77Skettenis static struct cleanup *run_cleanup_chain;	/* cleaned up on each 'run' */
113b725ae77Skettenis static struct cleanup *exec_cleanup_chain;	/* cleaned up on each execution command */
114b725ae77Skettenis /* cleaned up on each error from within an execution command */
115b725ae77Skettenis static struct cleanup *exec_error_cleanup_chain;
116b725ae77Skettenis 
117b725ae77Skettenis /* Pointer to what is left to do for an execution command after the
118b725ae77Skettenis    target stops. Used only in asynchronous mode, by targets that
119b725ae77Skettenis    support async execution.  The finish and until commands use it. So
120b725ae77Skettenis    does the target extended-remote command. */
121b725ae77Skettenis struct continuation *cmd_continuation;
122b725ae77Skettenis struct continuation *intermediate_continuation;
123e93f7393Sniklas 
124e93f7393Sniklas /* Nonzero if we have job control. */
125e93f7393Sniklas 
126e93f7393Sniklas int job_control;
127e93f7393Sniklas 
128e93f7393Sniklas /* Nonzero means a quit has been requested.  */
129e93f7393Sniklas 
130e93f7393Sniklas int quit_flag;
131e93f7393Sniklas 
132e93f7393Sniklas /* Nonzero means quit immediately if Control-C is typed now, rather
133e93f7393Sniklas    than waiting until QUIT is executed.  Be careful in setting this;
134e93f7393Sniklas    code which executes with immediate_quit set has to be very careful
135e93f7393Sniklas    about being able to deal with being interrupted at any time.  It is
136e93f7393Sniklas    almost always better to use QUIT; the only exception I can think of
137e93f7393Sniklas    is being able to quit out of a system call (using EINTR loses if
138e93f7393Sniklas    the SIGINT happens between the previous QUIT and the system call).
139e93f7393Sniklas    To immediately quit in the case in which a SIGINT happens between
140e93f7393Sniklas    the previous QUIT and setting immediate_quit (desirable anytime we
141e93f7393Sniklas    expect to block), call QUIT after setting immediate_quit.  */
142e93f7393Sniklas 
143e93f7393Sniklas int immediate_quit;
144e93f7393Sniklas 
145b725ae77Skettenis /* Nonzero means that encoded C++/ObjC names should be printed out in their
146b725ae77Skettenis    C++/ObjC form rather than raw.  */
147e93f7393Sniklas 
148e93f7393Sniklas int demangle = 1;
149e93f7393Sniklas 
150b725ae77Skettenis /* Nonzero means that encoded C++/ObjC names should be printed out in their
151b725ae77Skettenis    C++/ObjC form even in assembler language displays.  If this is set, but
152e93f7393Sniklas    DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls.  */
153e93f7393Sniklas 
154e93f7393Sniklas int asm_demangle = 0;
155e93f7393Sniklas 
156e93f7393Sniklas /* Nonzero means that strings with character values >0x7F should be printed
157e93f7393Sniklas    as octal escapes.  Zero means just print the value (e.g. it's an
158e93f7393Sniklas    international character, and the terminal or window can cope.)  */
159e93f7393Sniklas 
160e93f7393Sniklas int sevenbit_strings = 0;
161e93f7393Sniklas 
162e93f7393Sniklas /* String to be printed before error messages, if any.  */
163e93f7393Sniklas 
164e93f7393Sniklas char *error_pre_print;
165e93f7393Sniklas 
166e93f7393Sniklas /* String to be printed before quit messages, if any.  */
167e93f7393Sniklas 
168e93f7393Sniklas char *quit_pre_print;
169e93f7393Sniklas 
170e93f7393Sniklas /* String to be printed before warning messages, if any.  */
171e93f7393Sniklas 
172e93f7393Sniklas char *warning_pre_print = "\nwarning: ";
173b725ae77Skettenis 
174b725ae77Skettenis int pagination_enabled = 1;
175e93f7393Sniklas 
176b725ae77Skettenis 
177e93f7393Sniklas /* Add a new cleanup to the cleanup_chain,
178e93f7393Sniklas    and return the previous chain pointer
179e93f7393Sniklas    to be passed later to do_cleanups or discard_cleanups.
180e93f7393Sniklas    Args are FUNCTION to clean up with, and ARG to pass to it.  */
181e93f7393Sniklas 
182e93f7393Sniklas struct cleanup *
make_cleanup(make_cleanup_ftype * function,void * arg)183b725ae77Skettenis make_cleanup (make_cleanup_ftype *function, void *arg)
184e93f7393Sniklas {
185b725ae77Skettenis   return make_my_cleanup (&cleanup_chain, function, arg);
186b725ae77Skettenis }
187e93f7393Sniklas 
188b725ae77Skettenis struct cleanup *
make_final_cleanup(make_cleanup_ftype * function,void * arg)189b725ae77Skettenis make_final_cleanup (make_cleanup_ftype *function, void *arg)
190b725ae77Skettenis {
191b725ae77Skettenis   return make_my_cleanup (&final_cleanup_chain, function, arg);
192b725ae77Skettenis }
193b725ae77Skettenis 
194b725ae77Skettenis struct cleanup *
make_run_cleanup(make_cleanup_ftype * function,void * arg)195b725ae77Skettenis make_run_cleanup (make_cleanup_ftype *function, void *arg)
196b725ae77Skettenis {
197b725ae77Skettenis   return make_my_cleanup (&run_cleanup_chain, function, arg);
198b725ae77Skettenis }
199b725ae77Skettenis 
200b725ae77Skettenis struct cleanup *
make_exec_cleanup(make_cleanup_ftype * function,void * arg)201b725ae77Skettenis make_exec_cleanup (make_cleanup_ftype *function, void *arg)
202b725ae77Skettenis {
203b725ae77Skettenis   return make_my_cleanup (&exec_cleanup_chain, function, arg);
204b725ae77Skettenis }
205b725ae77Skettenis 
206b725ae77Skettenis struct cleanup *
make_exec_error_cleanup(make_cleanup_ftype * function,void * arg)207b725ae77Skettenis make_exec_error_cleanup (make_cleanup_ftype *function, void *arg)
208b725ae77Skettenis {
209b725ae77Skettenis   return make_my_cleanup (&exec_error_cleanup_chain, function, arg);
210b725ae77Skettenis }
211b725ae77Skettenis 
212b725ae77Skettenis static void
do_freeargv(void * arg)213b725ae77Skettenis do_freeargv (void *arg)
214b725ae77Skettenis {
215b725ae77Skettenis   freeargv ((char **) arg);
216b725ae77Skettenis }
217b725ae77Skettenis 
218b725ae77Skettenis struct cleanup *
make_cleanup_freeargv(char ** arg)219b725ae77Skettenis make_cleanup_freeargv (char **arg)
220b725ae77Skettenis {
221b725ae77Skettenis   return make_my_cleanup (&cleanup_chain, do_freeargv, arg);
222b725ae77Skettenis }
223b725ae77Skettenis 
224b725ae77Skettenis static void
do_bfd_close_cleanup(void * arg)225b725ae77Skettenis do_bfd_close_cleanup (void *arg)
226b725ae77Skettenis {
227b725ae77Skettenis   bfd_close (arg);
228b725ae77Skettenis }
229b725ae77Skettenis 
230b725ae77Skettenis struct cleanup *
make_cleanup_bfd_close(bfd * abfd)231b725ae77Skettenis make_cleanup_bfd_close (bfd *abfd)
232b725ae77Skettenis {
233b725ae77Skettenis   return make_cleanup (do_bfd_close_cleanup, abfd);
234b725ae77Skettenis }
235b725ae77Skettenis 
236b725ae77Skettenis static void
do_close_cleanup(void * arg)237b725ae77Skettenis do_close_cleanup (void *arg)
238b725ae77Skettenis {
239b725ae77Skettenis   int *fd = arg;
240b725ae77Skettenis   close (*fd);
241b725ae77Skettenis   xfree (fd);
242b725ae77Skettenis }
243b725ae77Skettenis 
244b725ae77Skettenis struct cleanup *
make_cleanup_close(int fd)245b725ae77Skettenis make_cleanup_close (int fd)
246b725ae77Skettenis {
247b725ae77Skettenis   int *saved_fd = xmalloc (sizeof (fd));
248b725ae77Skettenis   *saved_fd = fd;
249b725ae77Skettenis   return make_cleanup (do_close_cleanup, saved_fd);
250b725ae77Skettenis }
251b725ae77Skettenis 
252b725ae77Skettenis static void
do_ui_file_delete(void * arg)253b725ae77Skettenis do_ui_file_delete (void *arg)
254b725ae77Skettenis {
255b725ae77Skettenis   ui_file_delete (arg);
256b725ae77Skettenis }
257b725ae77Skettenis 
258b725ae77Skettenis struct cleanup *
make_cleanup_ui_file_delete(struct ui_file * arg)259b725ae77Skettenis make_cleanup_ui_file_delete (struct ui_file *arg)
260b725ae77Skettenis {
261b725ae77Skettenis   return make_my_cleanup (&cleanup_chain, do_ui_file_delete, arg);
262b725ae77Skettenis }
263b725ae77Skettenis 
26463addd46Skettenis static void
do_free_section_addr_info(void * arg)26563addd46Skettenis do_free_section_addr_info (void *arg)
26663addd46Skettenis {
26763addd46Skettenis   free_section_addr_info (arg);
26863addd46Skettenis }
26963addd46Skettenis 
27063addd46Skettenis struct cleanup *
make_cleanup_free_section_addr_info(struct section_addr_info * addrs)27163addd46Skettenis make_cleanup_free_section_addr_info (struct section_addr_info *addrs)
27263addd46Skettenis {
27363addd46Skettenis   return make_my_cleanup (&cleanup_chain, do_free_section_addr_info, addrs);
27463addd46Skettenis }
27563addd46Skettenis 
27663addd46Skettenis 
277b725ae77Skettenis struct cleanup *
make_my_cleanup(struct cleanup ** pmy_chain,make_cleanup_ftype * function,void * arg)278b725ae77Skettenis make_my_cleanup (struct cleanup **pmy_chain, make_cleanup_ftype *function,
279b725ae77Skettenis 		 void *arg)
280b725ae77Skettenis {
281b725ae77Skettenis   struct cleanup *new
282b725ae77Skettenis     = (struct cleanup *) xmalloc (sizeof (struct cleanup));
283b725ae77Skettenis   struct cleanup *old_chain = *pmy_chain;
284b725ae77Skettenis 
285b725ae77Skettenis   new->next = *pmy_chain;
286e93f7393Sniklas   new->function = function;
287e93f7393Sniklas   new->arg = arg;
288b725ae77Skettenis   *pmy_chain = new;
289e93f7393Sniklas 
290e93f7393Sniklas   return old_chain;
291e93f7393Sniklas }
292e93f7393Sniklas 
293e93f7393Sniklas /* Discard cleanups and do the actions they describe
294e93f7393Sniklas    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
295e93f7393Sniklas 
296e93f7393Sniklas void
do_cleanups(struct cleanup * old_chain)297b725ae77Skettenis do_cleanups (struct cleanup *old_chain)
298e93f7393Sniklas {
299b725ae77Skettenis   do_my_cleanups (&cleanup_chain, old_chain);
300b725ae77Skettenis }
301b725ae77Skettenis 
302b725ae77Skettenis void
do_final_cleanups(struct cleanup * old_chain)303b725ae77Skettenis do_final_cleanups (struct cleanup *old_chain)
304e93f7393Sniklas {
305b725ae77Skettenis   do_my_cleanups (&final_cleanup_chain, old_chain);
306b725ae77Skettenis }
307b725ae77Skettenis 
308b725ae77Skettenis void
do_run_cleanups(struct cleanup * old_chain)309b725ae77Skettenis do_run_cleanups (struct cleanup *old_chain)
310b725ae77Skettenis {
311b725ae77Skettenis   do_my_cleanups (&run_cleanup_chain, old_chain);
312b725ae77Skettenis }
313b725ae77Skettenis 
314b725ae77Skettenis void
do_exec_cleanups(struct cleanup * old_chain)315b725ae77Skettenis do_exec_cleanups (struct cleanup *old_chain)
316b725ae77Skettenis {
317b725ae77Skettenis   do_my_cleanups (&exec_cleanup_chain, old_chain);
318b725ae77Skettenis }
319b725ae77Skettenis 
320b725ae77Skettenis void
do_exec_error_cleanups(struct cleanup * old_chain)321b725ae77Skettenis do_exec_error_cleanups (struct cleanup *old_chain)
322b725ae77Skettenis {
323b725ae77Skettenis   do_my_cleanups (&exec_error_cleanup_chain, old_chain);
324b725ae77Skettenis }
325b725ae77Skettenis 
326b725ae77Skettenis static void
do_my_cleanups(struct cleanup ** pmy_chain,struct cleanup * old_chain)327b725ae77Skettenis do_my_cleanups (struct cleanup **pmy_chain,
328b725ae77Skettenis 		struct cleanup *old_chain)
329b725ae77Skettenis {
330b725ae77Skettenis   struct cleanup *ptr;
331b725ae77Skettenis   while ((ptr = *pmy_chain) != old_chain)
332b725ae77Skettenis     {
333b725ae77Skettenis       *pmy_chain = ptr->next;	/* Do this first incase recursion */
334e93f7393Sniklas       (*ptr->function) (ptr->arg);
335b725ae77Skettenis       xfree (ptr);
336e93f7393Sniklas     }
337e93f7393Sniklas }
338e93f7393Sniklas 
339e93f7393Sniklas /* Discard cleanups, not doing the actions they describe,
340e93f7393Sniklas    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
341e93f7393Sniklas 
342e93f7393Sniklas void
discard_cleanups(struct cleanup * old_chain)343b725ae77Skettenis discard_cleanups (struct cleanup *old_chain)
344e93f7393Sniklas {
345b725ae77Skettenis   discard_my_cleanups (&cleanup_chain, old_chain);
346b725ae77Skettenis }
347b725ae77Skettenis 
348b725ae77Skettenis void
discard_final_cleanups(struct cleanup * old_chain)349b725ae77Skettenis discard_final_cleanups (struct cleanup *old_chain)
350e93f7393Sniklas {
351b725ae77Skettenis   discard_my_cleanups (&final_cleanup_chain, old_chain);
352b725ae77Skettenis }
353b725ae77Skettenis 
354b725ae77Skettenis void
discard_exec_error_cleanups(struct cleanup * old_chain)355b725ae77Skettenis discard_exec_error_cleanups (struct cleanup *old_chain)
356b725ae77Skettenis {
357b725ae77Skettenis   discard_my_cleanups (&exec_error_cleanup_chain, old_chain);
358b725ae77Skettenis }
359b725ae77Skettenis 
360b725ae77Skettenis void
discard_my_cleanups(struct cleanup ** pmy_chain,struct cleanup * old_chain)361b725ae77Skettenis discard_my_cleanups (struct cleanup **pmy_chain,
362b725ae77Skettenis 		     struct cleanup *old_chain)
363b725ae77Skettenis {
364b725ae77Skettenis   struct cleanup *ptr;
365b725ae77Skettenis   while ((ptr = *pmy_chain) != old_chain)
366b725ae77Skettenis     {
367b725ae77Skettenis       *pmy_chain = ptr->next;
368b725ae77Skettenis       xfree (ptr);
369e93f7393Sniklas     }
370e93f7393Sniklas }
371e93f7393Sniklas 
372e93f7393Sniklas /* Set the cleanup_chain to 0, and return the old cleanup chain.  */
373e93f7393Sniklas struct cleanup *
save_cleanups(void)374b725ae77Skettenis save_cleanups (void)
375e93f7393Sniklas {
376b725ae77Skettenis   return save_my_cleanups (&cleanup_chain);
377b725ae77Skettenis }
378e93f7393Sniklas 
379b725ae77Skettenis struct cleanup *
save_final_cleanups(void)380b725ae77Skettenis save_final_cleanups (void)
381b725ae77Skettenis {
382b725ae77Skettenis   return save_my_cleanups (&final_cleanup_chain);
383b725ae77Skettenis }
384b725ae77Skettenis 
385b725ae77Skettenis struct cleanup *
save_my_cleanups(struct cleanup ** pmy_chain)386b725ae77Skettenis save_my_cleanups (struct cleanup **pmy_chain)
387b725ae77Skettenis {
388b725ae77Skettenis   struct cleanup *old_chain = *pmy_chain;
389b725ae77Skettenis 
390b725ae77Skettenis   *pmy_chain = 0;
391e93f7393Sniklas   return old_chain;
392e93f7393Sniklas }
393e93f7393Sniklas 
394e93f7393Sniklas /* Restore the cleanup chain from a previously saved chain.  */
395e93f7393Sniklas void
restore_cleanups(struct cleanup * chain)396b725ae77Skettenis restore_cleanups (struct cleanup *chain)
397e93f7393Sniklas {
398b725ae77Skettenis   restore_my_cleanups (&cleanup_chain, chain);
399b725ae77Skettenis }
400b725ae77Skettenis 
401b725ae77Skettenis void
restore_final_cleanups(struct cleanup * chain)402b725ae77Skettenis restore_final_cleanups (struct cleanup *chain)
403b725ae77Skettenis {
404b725ae77Skettenis   restore_my_cleanups (&final_cleanup_chain, chain);
405b725ae77Skettenis }
406b725ae77Skettenis 
407b725ae77Skettenis void
restore_my_cleanups(struct cleanup ** pmy_chain,struct cleanup * chain)408b725ae77Skettenis restore_my_cleanups (struct cleanup **pmy_chain, struct cleanup *chain)
409b725ae77Skettenis {
410b725ae77Skettenis   *pmy_chain = chain;
411e93f7393Sniklas }
412e93f7393Sniklas 
413e93f7393Sniklas /* This function is useful for cleanups.
414e93f7393Sniklas    Do
415e93f7393Sniklas 
416e93f7393Sniklas    foo = xmalloc (...);
417e93f7393Sniklas    old_chain = make_cleanup (free_current_contents, &foo);
418e93f7393Sniklas 
419e93f7393Sniklas    to arrange to free the object thus allocated.  */
420e93f7393Sniklas 
421e93f7393Sniklas void
free_current_contents(void * ptr)422b725ae77Skettenis free_current_contents (void *ptr)
423e93f7393Sniklas {
424b725ae77Skettenis   void **location = ptr;
425b725ae77Skettenis   if (location == NULL)
426b725ae77Skettenis     internal_error (__FILE__, __LINE__,
427b725ae77Skettenis 		    "free_current_contents: NULL pointer");
428b725ae77Skettenis   if (*location != NULL)
429b725ae77Skettenis     {
430b725ae77Skettenis       xfree (*location);
431b725ae77Skettenis       *location = NULL;
432b725ae77Skettenis     }
433e93f7393Sniklas }
434e93f7393Sniklas 
435e93f7393Sniklas /* Provide a known function that does nothing, to use as a base for
436e93f7393Sniklas    for a possibly long chain of cleanups.  This is useful where we
437e93f7393Sniklas    use the cleanup chain for handling normal cleanups as well as dealing
438e93f7393Sniklas    with cleanups that need to be done as a result of a call to error().
439e93f7393Sniklas    In such cases, we may not be certain where the first cleanup is, unless
440e93f7393Sniklas    we have a do-nothing one to always use as the base. */
441e93f7393Sniklas 
442e93f7393Sniklas void
null_cleanup(void * arg)443b725ae77Skettenis null_cleanup (void *arg)
444e93f7393Sniklas {
445e93f7393Sniklas }
446e93f7393Sniklas 
447b725ae77Skettenis /* Add a continuation to the continuation list, the global list
448b725ae77Skettenis    cmd_continuation. The new continuation will be added at the front.*/
449b725ae77Skettenis void
add_continuation(void (* continuation_hook)(struct continuation_arg *),struct continuation_arg * arg_list)450b725ae77Skettenis add_continuation (void (*continuation_hook) (struct continuation_arg *),
451b725ae77Skettenis 		  struct continuation_arg *arg_list)
452b725ae77Skettenis {
453b725ae77Skettenis   struct continuation *continuation_ptr;
454e93f7393Sniklas 
455b725ae77Skettenis   continuation_ptr =
456b725ae77Skettenis     (struct continuation *) xmalloc (sizeof (struct continuation));
457b725ae77Skettenis   continuation_ptr->continuation_hook = continuation_hook;
458b725ae77Skettenis   continuation_ptr->arg_list = arg_list;
459b725ae77Skettenis   continuation_ptr->next = cmd_continuation;
460b725ae77Skettenis   cmd_continuation = continuation_ptr;
461b725ae77Skettenis }
462b725ae77Skettenis 
463b725ae77Skettenis /* Walk down the cmd_continuation list, and execute all the
464b725ae77Skettenis    continuations. There is a problem though. In some cases new
465b725ae77Skettenis    continuations may be added while we are in the middle of this
466b725ae77Skettenis    loop. If this happens they will be added in the front, and done
467b725ae77Skettenis    before we have a chance of exhausting those that were already
468b725ae77Skettenis    there. We need to then save the beginning of the list in a pointer
469b725ae77Skettenis    and do the continuations from there on, instead of using the
470b725ae77Skettenis    global beginning of list as our iteration pointer.*/
471b725ae77Skettenis void
do_all_continuations(void)472b725ae77Skettenis do_all_continuations (void)
473b725ae77Skettenis {
474b725ae77Skettenis   struct continuation *continuation_ptr;
475b725ae77Skettenis   struct continuation *saved_continuation;
476b725ae77Skettenis 
477b725ae77Skettenis   /* Copy the list header into another pointer, and set the global
478b725ae77Skettenis      list header to null, so that the global list can change as a side
479b725ae77Skettenis      effect of invoking the continuations and the processing of
480b725ae77Skettenis      the preexisting continuations will not be affected. */
481b725ae77Skettenis   continuation_ptr = cmd_continuation;
482b725ae77Skettenis   cmd_continuation = NULL;
483b725ae77Skettenis 
484b725ae77Skettenis   /* Work now on the list we have set aside. */
485b725ae77Skettenis   while (continuation_ptr)
486b725ae77Skettenis     {
487b725ae77Skettenis       (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
488b725ae77Skettenis       saved_continuation = continuation_ptr;
489b725ae77Skettenis       continuation_ptr = continuation_ptr->next;
490b725ae77Skettenis       xfree (saved_continuation);
491b725ae77Skettenis     }
492b725ae77Skettenis }
493b725ae77Skettenis 
494b725ae77Skettenis /* Walk down the cmd_continuation list, and get rid of all the
495b725ae77Skettenis    continuations. */
496b725ae77Skettenis void
discard_all_continuations(void)497b725ae77Skettenis discard_all_continuations (void)
498b725ae77Skettenis {
499b725ae77Skettenis   struct continuation *continuation_ptr;
500b725ae77Skettenis 
501b725ae77Skettenis   while (cmd_continuation)
502b725ae77Skettenis     {
503b725ae77Skettenis       continuation_ptr = cmd_continuation;
504b725ae77Skettenis       cmd_continuation = continuation_ptr->next;
505b725ae77Skettenis       xfree (continuation_ptr);
506b725ae77Skettenis     }
507b725ae77Skettenis }
508b725ae77Skettenis 
509b725ae77Skettenis /* Add a continuation to the continuation list, the global list
510b725ae77Skettenis    intermediate_continuation. The new continuation will be added at the front.*/
511b725ae77Skettenis void
add_intermediate_continuation(void (* continuation_hook)(struct continuation_arg *),struct continuation_arg * arg_list)512b725ae77Skettenis add_intermediate_continuation (void (*continuation_hook)
513b725ae77Skettenis 			       (struct continuation_arg *),
514b725ae77Skettenis 			       struct continuation_arg *arg_list)
515b725ae77Skettenis {
516b725ae77Skettenis   struct continuation *continuation_ptr;
517b725ae77Skettenis 
518b725ae77Skettenis   continuation_ptr =
519b725ae77Skettenis     (struct continuation *) xmalloc (sizeof (struct continuation));
520b725ae77Skettenis   continuation_ptr->continuation_hook = continuation_hook;
521b725ae77Skettenis   continuation_ptr->arg_list = arg_list;
522b725ae77Skettenis   continuation_ptr->next = intermediate_continuation;
523b725ae77Skettenis   intermediate_continuation = continuation_ptr;
524b725ae77Skettenis }
525b725ae77Skettenis 
526b725ae77Skettenis /* Walk down the cmd_continuation list, and execute all the
527b725ae77Skettenis    continuations. There is a problem though. In some cases new
528b725ae77Skettenis    continuations may be added while we are in the middle of this
529b725ae77Skettenis    loop. If this happens they will be added in the front, and done
530b725ae77Skettenis    before we have a chance of exhausting those that were already
531b725ae77Skettenis    there. We need to then save the beginning of the list in a pointer
532b725ae77Skettenis    and do the continuations from there on, instead of using the
533b725ae77Skettenis    global beginning of list as our iteration pointer.*/
534b725ae77Skettenis void
do_all_intermediate_continuations(void)535b725ae77Skettenis do_all_intermediate_continuations (void)
536b725ae77Skettenis {
537b725ae77Skettenis   struct continuation *continuation_ptr;
538b725ae77Skettenis   struct continuation *saved_continuation;
539b725ae77Skettenis 
540b725ae77Skettenis   /* Copy the list header into another pointer, and set the global
541b725ae77Skettenis      list header to null, so that the global list can change as a side
542b725ae77Skettenis      effect of invoking the continuations and the processing of
543b725ae77Skettenis      the preexisting continuations will not be affected. */
544b725ae77Skettenis   continuation_ptr = intermediate_continuation;
545b725ae77Skettenis   intermediate_continuation = NULL;
546b725ae77Skettenis 
547b725ae77Skettenis   /* Work now on the list we have set aside. */
548b725ae77Skettenis   while (continuation_ptr)
549b725ae77Skettenis     {
550b725ae77Skettenis       (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
551b725ae77Skettenis       saved_continuation = continuation_ptr;
552b725ae77Skettenis       continuation_ptr = continuation_ptr->next;
553b725ae77Skettenis       xfree (saved_continuation);
554b725ae77Skettenis     }
555b725ae77Skettenis }
556b725ae77Skettenis 
557b725ae77Skettenis /* Walk down the cmd_continuation list, and get rid of all the
558b725ae77Skettenis    continuations. */
559b725ae77Skettenis void
discard_all_intermediate_continuations(void)560b725ae77Skettenis discard_all_intermediate_continuations (void)
561b725ae77Skettenis {
562b725ae77Skettenis   struct continuation *continuation_ptr;
563b725ae77Skettenis 
564b725ae77Skettenis   while (intermediate_continuation)
565b725ae77Skettenis     {
566b725ae77Skettenis       continuation_ptr = intermediate_continuation;
567b725ae77Skettenis       intermediate_continuation = continuation_ptr->next;
568b725ae77Skettenis       xfree (continuation_ptr);
569b725ae77Skettenis     }
570b725ae77Skettenis }
571b725ae77Skettenis 
572b725ae77Skettenis 
573b725ae77Skettenis 
574b725ae77Skettenis /* Print a warning message.  The first argument STRING is the warning
575b725ae77Skettenis    message, used as an fprintf format string, the second is the
576b725ae77Skettenis    va_list of arguments for that string.  A warning is unfiltered (not
577b725ae77Skettenis    paginated) so that the user does not need to page through each
578b725ae77Skettenis    screen full of warnings when there are lots of them.  */
579e93f7393Sniklas 
580e93f7393Sniklas void
vwarning(const char * string,va_list args)581b725ae77Skettenis vwarning (const char *string, va_list args)
582b725ae77Skettenis {
58363addd46Skettenis   if (deprecated_warning_hook)
58463addd46Skettenis     (*deprecated_warning_hook) (string, args);
585b725ae77Skettenis   else
586e93f7393Sniklas     {
587e93f7393Sniklas       target_terminal_ours ();
588e93f7393Sniklas       wrap_here ("");		/* Force out any buffered output */
589e93f7393Sniklas       gdb_flush (gdb_stdout);
590e93f7393Sniklas       if (warning_pre_print)
591b725ae77Skettenis 	fputs_unfiltered (warning_pre_print, gdb_stderr);
592b725ae77Skettenis       vfprintf_unfiltered (gdb_stderr, string, args);
593b725ae77Skettenis       fprintf_unfiltered (gdb_stderr, "\n");
594b725ae77Skettenis       va_end (args);
595b725ae77Skettenis     }
596e93f7393Sniklas }
597e93f7393Sniklas 
598e93f7393Sniklas /* Print a warning message.
599e93f7393Sniklas    The first argument STRING is the warning message, used as a fprintf string,
600e93f7393Sniklas    and the remaining args are passed as arguments to it.
601e93f7393Sniklas    The primary difference between warnings and errors is that a warning
602e93f7393Sniklas    does not force the return to command level.  */
603e93f7393Sniklas 
604e93f7393Sniklas void
warning(const char * string,...)605b725ae77Skettenis warning (const char *string, ...)
606e93f7393Sniklas {
607e93f7393Sniklas   va_list args;
608e93f7393Sniklas   va_start (args, string);
609b725ae77Skettenis   vwarning (string, args);
610e93f7393Sniklas   va_end (args);
611e93f7393Sniklas }
612e93f7393Sniklas 
613e93f7393Sniklas /* Print an error message and return to command level.
614e93f7393Sniklas    The first argument STRING is the error message, used as a fprintf string,
615e93f7393Sniklas    and the remaining args are passed as arguments to it.  */
616e93f7393Sniklas 
617e93f7393Sniklas NORETURN void
verror(const char * string,va_list args)618b725ae77Skettenis verror (const char *string, va_list args)
619b725ae77Skettenis {
620b725ae77Skettenis   struct ui_file *tmp_stream = mem_fileopen ();
621b725ae77Skettenis   make_cleanup_ui_file_delete (tmp_stream);
622b725ae77Skettenis   vfprintf_unfiltered (tmp_stream, string, args);
623b725ae77Skettenis   error_stream (tmp_stream);
624b725ae77Skettenis }
625b725ae77Skettenis 
626b725ae77Skettenis NORETURN void
error(const char * string,...)627b725ae77Skettenis error (const char *string, ...)
628e93f7393Sniklas {
629e93f7393Sniklas   va_list args;
630e93f7393Sniklas   va_start (args, string);
631b725ae77Skettenis   verror (string, args);
632b725ae77Skettenis   va_end (args);
633b725ae77Skettenis }
634b725ae77Skettenis 
635b725ae77Skettenis static void
do_write(void * data,const char * buffer,long length_buffer)636b725ae77Skettenis do_write (void *data, const char *buffer, long length_buffer)
637b725ae77Skettenis {
638b725ae77Skettenis   ui_file_write (data, buffer, length_buffer);
639b725ae77Skettenis }
640b725ae77Skettenis 
641b725ae77Skettenis /* Cause a silent error to occur.  Any error message is recorded
642b725ae77Skettenis    though it is not issued.  */
643b725ae77Skettenis NORETURN void
error_silent(const char * string,...)644b725ae77Skettenis error_silent (const char *string, ...)
645b725ae77Skettenis {
646b725ae77Skettenis   va_list args;
647b725ae77Skettenis   struct ui_file *tmp_stream = mem_fileopen ();
648b725ae77Skettenis   va_start (args, string);
649b725ae77Skettenis   make_cleanup_ui_file_delete (tmp_stream);
650b725ae77Skettenis   vfprintf_unfiltered (tmp_stream, string, args);
651b725ae77Skettenis   /* Copy the stream into the GDB_LASTERR buffer.  */
652b725ae77Skettenis   ui_file_rewind (gdb_lasterr);
653b725ae77Skettenis   ui_file_put (tmp_stream, do_write, gdb_lasterr);
654b725ae77Skettenis   va_end (args);
655b725ae77Skettenis 
656b725ae77Skettenis   throw_exception (RETURN_ERROR);
657b725ae77Skettenis }
658b725ae77Skettenis 
659b725ae77Skettenis /* Output an error message including any pre-print text to gdb_stderr.  */
660b725ae77Skettenis void
error_output_message(char * pre_print,char * msg)661b725ae77Skettenis error_output_message (char *pre_print, char *msg)
662b725ae77Skettenis {
663b725ae77Skettenis   target_terminal_ours ();
664b725ae77Skettenis   wrap_here ("");		/* Force out any buffered output */
665b725ae77Skettenis   gdb_flush (gdb_stdout);
666b725ae77Skettenis   annotate_error_begin ();
667b725ae77Skettenis   if (pre_print)
668b725ae77Skettenis     fputs_filtered (pre_print, gdb_stderr);
669b725ae77Skettenis   fputs_filtered (msg, gdb_stderr);
670b725ae77Skettenis   fprintf_filtered (gdb_stderr, "\n");
671b725ae77Skettenis }
672b725ae77Skettenis 
673b725ae77Skettenis NORETURN void
error_stream(struct ui_file * stream)674b725ae77Skettenis error_stream (struct ui_file *stream)
675b725ae77Skettenis {
67663addd46Skettenis   if (deprecated_error_begin_hook)
67763addd46Skettenis     deprecated_error_begin_hook ();
678b725ae77Skettenis 
679b725ae77Skettenis   /* Copy the stream into the GDB_LASTERR buffer.  */
680b725ae77Skettenis   ui_file_rewind (gdb_lasterr);
681b725ae77Skettenis   ui_file_put (stream, do_write, gdb_lasterr);
682b725ae77Skettenis 
683b725ae77Skettenis   /* Write the message plus any error_pre_print to gdb_stderr.  */
684b725ae77Skettenis   target_terminal_ours ();
685b725ae77Skettenis   wrap_here ("");		/* Force out any buffered output */
686b725ae77Skettenis   gdb_flush (gdb_stdout);
687b725ae77Skettenis   annotate_error_begin ();
688b725ae77Skettenis   if (error_pre_print)
689b725ae77Skettenis     fputs_filtered (error_pre_print, gdb_stderr);
690b725ae77Skettenis   ui_file_put (stream, do_write, gdb_stderr);
691b725ae77Skettenis   fprintf_filtered (gdb_stderr, "\n");
692b725ae77Skettenis 
693b725ae77Skettenis   throw_exception (RETURN_ERROR);
694b725ae77Skettenis }
695b725ae77Skettenis 
696b725ae77Skettenis /* Get the last error message issued by gdb */
697b725ae77Skettenis 
698b725ae77Skettenis char *
error_last_message(void)699b725ae77Skettenis error_last_message (void)
700b725ae77Skettenis {
701b725ae77Skettenis   long len;
702b725ae77Skettenis   return ui_file_xstrdup (gdb_lasterr, &len);
703b725ae77Skettenis }
704b725ae77Skettenis 
705b725ae77Skettenis /* This is to be called by main() at the very beginning */
706b725ae77Skettenis 
707b725ae77Skettenis void
error_init(void)708b725ae77Skettenis error_init (void)
709b725ae77Skettenis {
710b725ae77Skettenis   gdb_lasterr = mem_fileopen ();
711b725ae77Skettenis }
712b725ae77Skettenis 
713b725ae77Skettenis /* Print a message reporting an internal error/warning. Ask the user
714b725ae77Skettenis    if they want to continue, dump core, or just exit.  Return
715b725ae77Skettenis    something to indicate a quit.  */
716b725ae77Skettenis 
717b725ae77Skettenis struct internal_problem
718b725ae77Skettenis {
719b725ae77Skettenis   const char *name;
720b725ae77Skettenis   /* FIXME: cagney/2002-08-15: There should be ``maint set/show''
721b725ae77Skettenis      commands available for controlling these variables.  */
722b725ae77Skettenis   enum auto_boolean should_quit;
723b725ae77Skettenis   enum auto_boolean should_dump_core;
724b725ae77Skettenis };
725b725ae77Skettenis 
726b725ae77Skettenis /* Report a problem, internal to GDB, to the user.  Once the problem
727b725ae77Skettenis    has been reported, and assuming GDB didn't quit, the caller can
728b725ae77Skettenis    either allow execution to resume or throw an error.  */
729b725ae77Skettenis 
730b725ae77Skettenis static void
internal_vproblem(struct internal_problem * problem,const char * file,int line,const char * fmt,va_list ap)731b725ae77Skettenis internal_vproblem (struct internal_problem *problem,
732b725ae77Skettenis 		   const char *file, int line, const char *fmt, va_list ap)
733b725ae77Skettenis {
734b725ae77Skettenis   static int dejavu;
735b725ae77Skettenis   int quit_p;
736b725ae77Skettenis   int dump_core_p;
737b725ae77Skettenis   char *reason;
738b725ae77Skettenis 
739b725ae77Skettenis   /* Don't allow infinite error/warning recursion.  */
740b725ae77Skettenis   {
741b725ae77Skettenis     static char msg[] = "Recursive internal problem.\n";
742b725ae77Skettenis     switch (dejavu)
743b725ae77Skettenis       {
744b725ae77Skettenis       case 0:
745b725ae77Skettenis 	dejavu = 1;
746b725ae77Skettenis 	break;
747b725ae77Skettenis       case 1:
748b725ae77Skettenis 	dejavu = 2;
749b725ae77Skettenis 	fputs_unfiltered (msg, gdb_stderr);
750b725ae77Skettenis 	abort ();	/* NOTE: GDB has only three calls to abort().  */
751b725ae77Skettenis       default:
752b725ae77Skettenis 	dejavu = 3;
753b725ae77Skettenis 	write (STDERR_FILENO, msg, sizeof (msg));
754b725ae77Skettenis 	exit (1);
755b725ae77Skettenis       }
756b725ae77Skettenis   }
757b725ae77Skettenis 
758b725ae77Skettenis   /* Try to get the message out and at the start of a new line.  */
759b725ae77Skettenis   target_terminal_ours ();
760b725ae77Skettenis   begin_line ();
761b725ae77Skettenis 
762b725ae77Skettenis   /* Create a string containing the full error/warning message.  Need
763b725ae77Skettenis      to call query with this full string, as otherwize the reason
764b725ae77Skettenis      (error/warning) and question become separated.  Format using a
765b725ae77Skettenis      style similar to a compiler error message.  Include extra detail
766b725ae77Skettenis      so that the user knows that they are living on the edge.  */
767b725ae77Skettenis   {
768b725ae77Skettenis     char *msg;
76963addd46Skettenis     msg = xstrvprintf (fmt, ap);
77063addd46Skettenis     reason = xstrprintf ("\
771b725ae77Skettenis %s:%d: %s: %s\n\
772b725ae77Skettenis A problem internal to GDB has been detected,\n\
773b725ae77Skettenis further debugging may prove unreliable.", file, line, problem->name, msg);
774b725ae77Skettenis     xfree (msg);
775b725ae77Skettenis     make_cleanup (xfree, reason);
776b725ae77Skettenis   }
777b725ae77Skettenis 
778b725ae77Skettenis   switch (problem->should_quit)
779b725ae77Skettenis     {
780b725ae77Skettenis     case AUTO_BOOLEAN_AUTO:
781b725ae77Skettenis       /* Default (yes/batch case) is to quit GDB.  When in batch mode
782b725ae77Skettenis          this lessens the likelhood of GDB going into an infinate
783b725ae77Skettenis          loop.  */
784b725ae77Skettenis       quit_p = query ("%s\nQuit this debugging session? ", reason);
785b725ae77Skettenis       break;
786b725ae77Skettenis     case AUTO_BOOLEAN_TRUE:
787b725ae77Skettenis       quit_p = 1;
788b725ae77Skettenis       break;
789b725ae77Skettenis     case AUTO_BOOLEAN_FALSE:
790b725ae77Skettenis       quit_p = 0;
791b725ae77Skettenis       break;
792b725ae77Skettenis     default:
793b725ae77Skettenis       internal_error (__FILE__, __LINE__, "bad switch");
794b725ae77Skettenis     }
795b725ae77Skettenis 
796b725ae77Skettenis   switch (problem->should_dump_core)
797b725ae77Skettenis     {
798b725ae77Skettenis     case AUTO_BOOLEAN_AUTO:
799b725ae77Skettenis       /* Default (yes/batch case) is to dump core.  This leaves a GDB
800b725ae77Skettenis          `dropping' so that it is easier to see that something went
801b725ae77Skettenis          wrong in GDB.  */
802b725ae77Skettenis       dump_core_p = query ("%s\nCreate a core file of GDB? ", reason);
803b725ae77Skettenis       break;
804b725ae77Skettenis       break;
805b725ae77Skettenis     case AUTO_BOOLEAN_TRUE:
806b725ae77Skettenis       dump_core_p = 1;
807b725ae77Skettenis       break;
808b725ae77Skettenis     case AUTO_BOOLEAN_FALSE:
809b725ae77Skettenis       dump_core_p = 0;
810b725ae77Skettenis       break;
811b725ae77Skettenis     default:
812b725ae77Skettenis       internal_error (__FILE__, __LINE__, "bad switch");
813b725ae77Skettenis     }
814b725ae77Skettenis 
815b725ae77Skettenis   if (quit_p)
816b725ae77Skettenis     {
817b725ae77Skettenis       if (dump_core_p)
818b725ae77Skettenis 	abort ();		/* NOTE: GDB has only three calls to abort().  */
819b725ae77Skettenis       else
820b725ae77Skettenis 	exit (1);
821b725ae77Skettenis     }
822e93f7393Sniklas   else
823e93f7393Sniklas     {
824b725ae77Skettenis       if (dump_core_p)
825e93f7393Sniklas 	{
826b725ae77Skettenis 	  if (fork () == 0)
827b725ae77Skettenis 	    abort ();		/* NOTE: GDB has only three calls to abort().  */
828e93f7393Sniklas 	}
829e93f7393Sniklas     }
830e93f7393Sniklas 
831b725ae77Skettenis   dejavu = 0;
832b725ae77Skettenis }
833e93f7393Sniklas 
834b725ae77Skettenis static struct internal_problem internal_error_problem = {
835b725ae77Skettenis   "internal-error", AUTO_BOOLEAN_AUTO, AUTO_BOOLEAN_AUTO
836b725ae77Skettenis };
837e93f7393Sniklas 
838e93f7393Sniklas NORETURN void
internal_verror(const char * file,int line,const char * fmt,va_list ap)839b725ae77Skettenis internal_verror (const char *file, int line, const char *fmt, va_list ap)
840e93f7393Sniklas {
841b725ae77Skettenis   internal_vproblem (&internal_error_problem, file, line, fmt, ap);
842b725ae77Skettenis   throw_exception (RETURN_ERROR);
843e93f7393Sniklas }
844e93f7393Sniklas 
845b725ae77Skettenis NORETURN void
internal_error(const char * file,int line,const char * string,...)846b725ae77Skettenis internal_error (const char *file, int line, const char *string, ...)
847e93f7393Sniklas {
848b725ae77Skettenis   va_list ap;
849b725ae77Skettenis   va_start (ap, string);
850b725ae77Skettenis   internal_verror (file, line, string, ap);
851b725ae77Skettenis   va_end (ap);
852b725ae77Skettenis }
853e93f7393Sniklas 
854b725ae77Skettenis static struct internal_problem internal_warning_problem = {
85563addd46Skettenis   "internal-warning", AUTO_BOOLEAN_AUTO, AUTO_BOOLEAN_AUTO
856b725ae77Skettenis };
857e93f7393Sniklas 
858b725ae77Skettenis void
internal_vwarning(const char * file,int line,const char * fmt,va_list ap)859b725ae77Skettenis internal_vwarning (const char *file, int line, const char *fmt, va_list ap)
860b725ae77Skettenis {
861b725ae77Skettenis   internal_vproblem (&internal_warning_problem, file, line, fmt, ap);
862b725ae77Skettenis }
863b725ae77Skettenis 
864b725ae77Skettenis void
internal_warning(const char * file,int line,const char * string,...)865b725ae77Skettenis internal_warning (const char *file, int line, const char *string, ...)
866b725ae77Skettenis {
867b725ae77Skettenis   va_list ap;
868b725ae77Skettenis   va_start (ap, string);
869b725ae77Skettenis   internal_vwarning (file, line, string, ap);
870b725ae77Skettenis   va_end (ap);
871e93f7393Sniklas }
872e93f7393Sniklas 
873e93f7393Sniklas /* The strerror() function can return NULL for errno values that are
874e93f7393Sniklas    out of range.  Provide a "safe" version that always returns a
875e93f7393Sniklas    printable string. */
876e93f7393Sniklas 
877e93f7393Sniklas char *
safe_strerror(int errnum)878b725ae77Skettenis safe_strerror (int errnum)
879e93f7393Sniklas {
880e93f7393Sniklas   char *msg;
881e93f7393Sniklas   static char buf[32];
882e93f7393Sniklas 
883b725ae77Skettenis   msg = strerror (errnum);
884b725ae77Skettenis   if (msg == NULL)
885e93f7393Sniklas     {
886e93f7393Sniklas       sprintf (buf, "(undocumented errno %d)", errnum);
887e93f7393Sniklas       msg = buf;
888e93f7393Sniklas     }
889e93f7393Sniklas   return (msg);
890e93f7393Sniklas }
891e93f7393Sniklas 
892e93f7393Sniklas /* Print the system error message for errno, and also mention STRING
893e93f7393Sniklas    as the file name for which the error was encountered.
894e93f7393Sniklas    Then return to command level.  */
895e93f7393Sniklas 
896b725ae77Skettenis NORETURN void
perror_with_name(const char * string)897b725ae77Skettenis perror_with_name (const char *string)
898e93f7393Sniklas {
899e93f7393Sniklas   char *err;
900e93f7393Sniklas   char *combined;
901e93f7393Sniklas 
902e93f7393Sniklas   err = safe_strerror (errno);
903e93f7393Sniklas   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
904e93f7393Sniklas   strcpy (combined, string);
905e93f7393Sniklas   strcat (combined, ": ");
906e93f7393Sniklas   strcat (combined, err);
907e93f7393Sniklas 
908e93f7393Sniklas   /* I understand setting these is a matter of taste.  Still, some people
909e93f7393Sniklas      may clear errno but not know about bfd_error.  Doing this here is not
910e93f7393Sniklas      unreasonable. */
911e93f7393Sniklas   bfd_set_error (bfd_error_no_error);
912e93f7393Sniklas   errno = 0;
913e93f7393Sniklas 
914e93f7393Sniklas   error ("%s.", combined);
915e93f7393Sniklas }
916e93f7393Sniklas 
917e93f7393Sniklas /* Print the system error message for ERRCODE, and also mention STRING
918e93f7393Sniklas    as the file name for which the error was encountered.  */
919e93f7393Sniklas 
920e93f7393Sniklas void
print_sys_errmsg(const char * string,int errcode)921b725ae77Skettenis print_sys_errmsg (const char *string, int errcode)
922e93f7393Sniklas {
923e93f7393Sniklas   char *err;
924e93f7393Sniklas   char *combined;
925e93f7393Sniklas 
926e93f7393Sniklas   err = safe_strerror (errcode);
927e93f7393Sniklas   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
928e93f7393Sniklas   strcpy (combined, string);
929e93f7393Sniklas   strcat (combined, ": ");
930e93f7393Sniklas   strcat (combined, err);
931e93f7393Sniklas 
932e93f7393Sniklas   /* We want anything which was printed on stdout to come out first, before
933e93f7393Sniklas      this message.  */
934e93f7393Sniklas   gdb_flush (gdb_stdout);
935e93f7393Sniklas   fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
936e93f7393Sniklas }
937e93f7393Sniklas 
938e93f7393Sniklas /* Control C eventually causes this to be called, at a convenient time.  */
939e93f7393Sniklas 
940e93f7393Sniklas void
quit(void)941b725ae77Skettenis quit (void)
942e93f7393Sniklas {
943b725ae77Skettenis   struct serial *gdb_stdout_serial = serial_fdopen (1);
944e93f7393Sniklas 
945e93f7393Sniklas   target_terminal_ours ();
946e93f7393Sniklas 
947e93f7393Sniklas   /* We want all output to appear now, before we print "Quit".  We
948e93f7393Sniklas      have 3 levels of buffering we have to flush (it's possible that
949e93f7393Sniklas      some of these should be changed to flush the lower-level ones
950e93f7393Sniklas      too):  */
951e93f7393Sniklas 
952e93f7393Sniklas   /* 1.  The _filtered buffer.  */
953e93f7393Sniklas   wrap_here ((char *) 0);
954e93f7393Sniklas 
955e93f7393Sniklas   /* 2.  The stdio buffer.  */
956e93f7393Sniklas   gdb_flush (gdb_stdout);
957e93f7393Sniklas   gdb_flush (gdb_stderr);
958e93f7393Sniklas 
959e93f7393Sniklas   /* 3.  The system-level buffer.  */
960b725ae77Skettenis   serial_drain_output (gdb_stdout_serial);
961b725ae77Skettenis   serial_un_fdopen (gdb_stdout_serial);
962e93f7393Sniklas 
963e93f7393Sniklas   annotate_error_begin ();
964e93f7393Sniklas 
965e93f7393Sniklas   /* Don't use *_filtered; we don't want to prompt the user to continue.  */
966e93f7393Sniklas   if (quit_pre_print)
967b725ae77Skettenis     fputs_unfiltered (quit_pre_print, gdb_stderr);
968e93f7393Sniklas 
969b725ae77Skettenis #ifdef __MSDOS__
970b725ae77Skettenis   /* No steenking SIGINT will ever be coming our way when the
971b725ae77Skettenis      program is resumed.  Don't lie.  */
972b725ae77Skettenis   fprintf_unfiltered (gdb_stderr, "Quit\n");
973b725ae77Skettenis #else
974e93f7393Sniklas   if (job_control
975e93f7393Sniklas       /* If there is no terminal switching for this target, then we can't
976e93f7393Sniklas          possibly get screwed by the lack of job control.  */
977e93f7393Sniklas       || current_target.to_terminal_ours == NULL)
978e93f7393Sniklas     fprintf_unfiltered (gdb_stderr, "Quit\n");
979e93f7393Sniklas   else
980e93f7393Sniklas     fprintf_unfiltered (gdb_stderr,
981e93f7393Sniklas 			"Quit (expect signal SIGINT when the program is resumed)\n");
982e93f7393Sniklas #endif
983b725ae77Skettenis   throw_exception (RETURN_QUIT);
984e93f7393Sniklas }
985e93f7393Sniklas 
986e93f7393Sniklas /* Control C comes here */
987e93f7393Sniklas void
request_quit(int signo)988b725ae77Skettenis request_quit (int signo)
989e93f7393Sniklas {
990e93f7393Sniklas   quit_flag = 1;
99163addd46Skettenis   /* Restore the signal handler.  Harmless with BSD-style signals,
99263addd46Skettenis      needed for System V-style signals.  */
993e93f7393Sniklas   signal (signo, request_quit);
994e93f7393Sniklas 
995e93f7393Sniklas   if (immediate_quit)
996e93f7393Sniklas     quit ();
997e93f7393Sniklas }
998e93f7393Sniklas 
999e93f7393Sniklas /* Called when a memory allocation fails, with the number of bytes of
1000e93f7393Sniklas    memory requested in SIZE. */
1001e93f7393Sniklas 
1002e93f7393Sniklas NORETURN void
nomem(long size)1003b725ae77Skettenis nomem (long size)
1004e93f7393Sniklas {
1005e93f7393Sniklas   if (size > 0)
1006e93f7393Sniklas     {
1007b725ae77Skettenis       internal_error (__FILE__, __LINE__,
1008b725ae77Skettenis 		      "virtual memory exhausted: can't allocate %ld bytes.",
1009b725ae77Skettenis 		      size);
1010e93f7393Sniklas     }
1011e93f7393Sniklas   else
1012e93f7393Sniklas     {
1013b725ae77Skettenis       internal_error (__FILE__, __LINE__, "virtual memory exhausted.");
1014e93f7393Sniklas     }
1015e93f7393Sniklas }
1016e93f7393Sniklas 
101763addd46Skettenis /* The xmalloc() (libiberty.h) family of memory management routines.
1018e93f7393Sniklas 
101963addd46Skettenis    These are like the ISO-C malloc() family except that they implement
1020b725ae77Skettenis    consistent semantics and guard against typical memory management
102163addd46Skettenis    problems.  */
1022b725ae77Skettenis 
102363addd46Skettenis /* NOTE: These are declared using PTR to ensure consistency with
102463addd46Skettenis    "libiberty.h".  xfree() is GDB local.  */
1025b725ae77Skettenis 
102663addd46Skettenis PTR				/* OK: PTR */
xmalloc(size_t size)102763addd46Skettenis xmalloc (size_t size)
1028e93f7393Sniklas {
1029b725ae77Skettenis   void *val;
1030e93f7393Sniklas 
1031b725ae77Skettenis   /* See libiberty/xmalloc.c.  This function need's to match that's
1032b725ae77Skettenis      semantics.  It never returns NULL.  */
1033e93f7393Sniklas   if (size == 0)
1034b725ae77Skettenis     size = 1;
1035b725ae77Skettenis 
103663addd46Skettenis   val = malloc (size);		/* OK: malloc */
1037b725ae77Skettenis   if (val == NULL)
1038e93f7393Sniklas     nomem (size);
1039b725ae77Skettenis 
1040e93f7393Sniklas   return (val);
1041e93f7393Sniklas }
1042e93f7393Sniklas 
104363addd46Skettenis PTR				/* OK: PTR */
xrealloc(PTR ptr,size_t size)104463addd46Skettenis xrealloc (PTR ptr, size_t size)	/* OK: PTR */
1045e93f7393Sniklas {
1046b725ae77Skettenis   void *val;
1047b725ae77Skettenis 
1048b725ae77Skettenis   /* See libiberty/xmalloc.c.  This function need's to match that's
1049b725ae77Skettenis      semantics.  It never returns NULL.  */
1050b725ae77Skettenis   if (size == 0)
1051b725ae77Skettenis     size = 1;
1052e93f7393Sniklas 
1053e93f7393Sniklas   if (ptr != NULL)
105463addd46Skettenis     val = realloc (ptr, size);	/* OK: realloc */
1055e93f7393Sniklas   else
105663addd46Skettenis     val = malloc (size);		/* OK: malloc */
1057e93f7393Sniklas   if (val == NULL)
1058e93f7393Sniklas     nomem (size);
1059b725ae77Skettenis 
1060e93f7393Sniklas   return (val);
1061e93f7393Sniklas }
1062e93f7393Sniklas 
106363addd46Skettenis PTR				/* OK: PTR */
xcalloc(size_t number,size_t size)106463addd46Skettenis xcalloc (size_t number, size_t size)
1065e93f7393Sniklas {
1066b725ae77Skettenis   void *mem;
1067b725ae77Skettenis 
1068b725ae77Skettenis   /* See libiberty/xmalloc.c.  This function need's to match that's
1069b725ae77Skettenis      semantics.  It never returns NULL.  */
1070b725ae77Skettenis   if (number == 0 || size == 0)
1071b725ae77Skettenis     {
1072b725ae77Skettenis       number = 1;
1073b725ae77Skettenis       size = 1;
1074e93f7393Sniklas     }
1075e93f7393Sniklas 
107663addd46Skettenis   mem = calloc (number, size);		/* OK: xcalloc */
1077b725ae77Skettenis   if (mem == NULL)
1078b725ae77Skettenis     nomem (number * size);
1079c074d1c9Sdrahn 
1080b725ae77Skettenis   return mem;
1081c074d1c9Sdrahn }
1082c074d1c9Sdrahn 
1083b725ae77Skettenis void
xfree(void * ptr)1084b725ae77Skettenis xfree (void *ptr)
1085b725ae77Skettenis {
108663addd46Skettenis   if (ptr != NULL)
108763addd46Skettenis     free (ptr);		/* OK: free */
1088b725ae77Skettenis }
1089e93f7393Sniklas 
1090b725ae77Skettenis 
1091b725ae77Skettenis /* Like asprintf/vasprintf but get an internal_error if the call
1092b725ae77Skettenis    fails. */
1093b725ae77Skettenis 
1094b725ae77Skettenis char *
xstrprintf(const char * format,...)1095b725ae77Skettenis xstrprintf (const char *format, ...)
1096b725ae77Skettenis {
1097b725ae77Skettenis   char *ret;
1098b725ae77Skettenis   va_list args;
1099b725ae77Skettenis   va_start (args, format);
110063addd46Skettenis   ret = xstrvprintf (format, args);
1101b725ae77Skettenis   va_end (args);
1102b725ae77Skettenis   return ret;
1103b725ae77Skettenis }
1104b725ae77Skettenis 
1105b725ae77Skettenis void
xasprintf(char ** ret,const char * format,...)1106b725ae77Skettenis xasprintf (char **ret, const char *format, ...)
1107b725ae77Skettenis {
1108b725ae77Skettenis   va_list args;
1109b725ae77Skettenis   va_start (args, format);
111063addd46Skettenis   (*ret) = xstrvprintf (format, args);
1111b725ae77Skettenis   va_end (args);
1112b725ae77Skettenis }
1113b725ae77Skettenis 
1114b725ae77Skettenis void
xvasprintf(char ** ret,const char * format,va_list ap)1115b725ae77Skettenis xvasprintf (char **ret, const char *format, va_list ap)
1116b725ae77Skettenis {
111763addd46Skettenis   (*ret) = xstrvprintf (format, ap);
111863addd46Skettenis }
111963addd46Skettenis 
112063addd46Skettenis char *
xstrvprintf(const char * format,va_list ap)112163addd46Skettenis xstrvprintf (const char *format, va_list ap)
112263addd46Skettenis {
112363addd46Skettenis   char *ret = NULL;
112463addd46Skettenis   int status = vasprintf (&ret, format, ap);
112563addd46Skettenis   /* NULL is returned when there was a memory allocation problem.  */
112663addd46Skettenis   if (ret == NULL)
112763addd46Skettenis     nomem (0);
112863addd46Skettenis   /* A negative status (the printed length) with a non-NULL buffer
112963addd46Skettenis      should never happen, but just to be sure.  */
1130b725ae77Skettenis   if (status < 0)
1131b725ae77Skettenis     internal_error (__FILE__, __LINE__,
1132b725ae77Skettenis 		    "vasprintf call failed (errno %d)", errno);
113363addd46Skettenis   return ret;
1134b725ae77Skettenis }
1135b725ae77Skettenis 
1136*bb02a7f5Skettenis int
xsnprintf(char * str,size_t size,const char * format,...)1137*bb02a7f5Skettenis xsnprintf (char *str, size_t size, const char *format, ...)
1138*bb02a7f5Skettenis {
1139*bb02a7f5Skettenis   va_list args;
1140*bb02a7f5Skettenis   int ret;
1141*bb02a7f5Skettenis 
1142*bb02a7f5Skettenis   va_start (args, format);
1143*bb02a7f5Skettenis   ret = vsnprintf (str, size, format, args);
1144*bb02a7f5Skettenis   gdb_assert (ret < size);
1145*bb02a7f5Skettenis   va_end (args);
1146*bb02a7f5Skettenis 
1147*bb02a7f5Skettenis   return ret;
1148*bb02a7f5Skettenis }
1149*bb02a7f5Skettenis 
1150e93f7393Sniklas /* My replacement for the read system call.
1151e93f7393Sniklas    Used like `read' but keeps going if `read' returns too soon.  */
1152e93f7393Sniklas 
1153e93f7393Sniklas int
myread(int desc,char * addr,int len)1154b725ae77Skettenis myread (int desc, char *addr, int len)
1155e93f7393Sniklas {
1156b725ae77Skettenis   int val;
1157e93f7393Sniklas   int orglen = len;
1158e93f7393Sniklas 
1159e93f7393Sniklas   while (len > 0)
1160e93f7393Sniklas     {
1161e93f7393Sniklas       val = read (desc, addr, len);
1162e93f7393Sniklas       if (val < 0)
1163e93f7393Sniklas 	return val;
1164e93f7393Sniklas       if (val == 0)
1165e93f7393Sniklas 	return orglen - len;
1166e93f7393Sniklas       len -= val;
1167e93f7393Sniklas       addr += val;
1168e93f7393Sniklas     }
1169e93f7393Sniklas   return orglen;
1170e93f7393Sniklas }
1171e93f7393Sniklas 
1172e93f7393Sniklas /* Make a copy of the string at PTR with SIZE characters
1173e93f7393Sniklas    (and add a null character at the end in the copy).
1174e93f7393Sniklas    Uses malloc to get the space.  Returns the address of the copy.  */
1175e93f7393Sniklas 
1176e93f7393Sniklas char *
savestring(const char * ptr,size_t size)1177b725ae77Skettenis savestring (const char *ptr, size_t size)
1178e93f7393Sniklas {
1179b725ae77Skettenis   char *p = (char *) xmalloc (size + 1);
1180e93f7393Sniklas   memcpy (p, ptr, size);
1181e93f7393Sniklas   p[size] = 0;
1182e93f7393Sniklas   return p;
1183e93f7393Sniklas }
1184e93f7393Sniklas 
1185e93f7393Sniklas void
print_spaces(int n,struct ui_file * file)1186b725ae77Skettenis print_spaces (int n, struct ui_file *file)
1187e93f7393Sniklas {
1188b725ae77Skettenis   fputs_unfiltered (n_spaces (n), file);
1189e93f7393Sniklas }
1190e93f7393Sniklas 
1191e93f7393Sniklas /* Print a host address.  */
1192e93f7393Sniklas 
1193e93f7393Sniklas void
gdb_print_host_address(const void * addr,struct ui_file * stream)1194b725ae77Skettenis gdb_print_host_address (const void *addr, struct ui_file *stream)
1195e93f7393Sniklas {
1196e93f7393Sniklas 
1197e93f7393Sniklas   /* We could use the %p conversion specifier to fprintf if we had any
1198e93f7393Sniklas      way of knowing whether this host supports it.  But the following
1199e93f7393Sniklas      should work on the Alpha and on 32 bit machines.  */
1200e93f7393Sniklas 
1201e93f7393Sniklas   fprintf_filtered (stream, "0x%lx", (unsigned long) addr);
1202e93f7393Sniklas }
1203e93f7393Sniklas 
1204e93f7393Sniklas /* Ask user a y-or-n question and return 1 iff answer is yes.
1205e93f7393Sniklas    Takes three args which are given to printf to print the question.
1206e93f7393Sniklas    The first, a control string, should end in "? ".
1207e93f7393Sniklas    It should not say how to answer, because we do that.  */
1208e93f7393Sniklas 
1209e93f7393Sniklas /* VARARGS */
1210e93f7393Sniklas int
query(const char * ctlstr,...)1211b725ae77Skettenis query (const char *ctlstr, ...)
1212e93f7393Sniklas {
1213e93f7393Sniklas   va_list args;
1214b725ae77Skettenis   int answer;
1215b725ae77Skettenis   int ans2;
1216e93f7393Sniklas   int retval;
1217e93f7393Sniklas 
121863addd46Skettenis   if (deprecated_query_hook)
1219e93f7393Sniklas     {
122063addd46Skettenis       va_start (args, ctlstr);
122163addd46Skettenis       return deprecated_query_hook (ctlstr, args);
1222e93f7393Sniklas     }
1223e93f7393Sniklas 
1224e93f7393Sniklas   /* Automatically answer "yes" if input is not from a terminal.  */
1225e93f7393Sniklas   if (!input_from_terminal_p ())
1226e93f7393Sniklas     return 1;
1227e93f7393Sniklas 
1228e93f7393Sniklas   while (1)
1229e93f7393Sniklas     {
1230e93f7393Sniklas       wrap_here ("");		/* Flush any buffered output */
1231e93f7393Sniklas       gdb_flush (gdb_stdout);
1232e93f7393Sniklas 
1233e93f7393Sniklas       if (annotation_level > 1)
1234e93f7393Sniklas 	printf_filtered ("\n\032\032pre-query\n");
1235e93f7393Sniklas 
123663addd46Skettenis       va_start (args, ctlstr);
1237e93f7393Sniklas       vfprintf_filtered (gdb_stdout, ctlstr, args);
123863addd46Skettenis       va_end (args);
1239e93f7393Sniklas       printf_filtered ("(y or n) ");
1240e93f7393Sniklas 
1241e93f7393Sniklas       if (annotation_level > 1)
1242e93f7393Sniklas 	printf_filtered ("\n\032\032query\n");
1243e93f7393Sniklas 
1244b725ae77Skettenis       wrap_here ("");
1245e93f7393Sniklas       gdb_flush (gdb_stdout);
1246b725ae77Skettenis 
1247e93f7393Sniklas       answer = fgetc (stdin);
1248e93f7393Sniklas       clearerr (stdin);		/* in case of C-d */
1249e93f7393Sniklas       if (answer == EOF)	/* C-d */
1250e93f7393Sniklas 	{
1251e93f7393Sniklas 	  retval = 1;
1252e93f7393Sniklas 	  break;
1253e93f7393Sniklas 	}
1254b725ae77Skettenis       /* Eat rest of input line, to EOF or newline */
1255b725ae77Skettenis       if (answer != '\n')
1256e93f7393Sniklas 	do
1257e93f7393Sniklas 	  {
1258e93f7393Sniklas 	    ans2 = fgetc (stdin);
1259e93f7393Sniklas 	    clearerr (stdin);
1260e93f7393Sniklas 	  }
1261b725ae77Skettenis 	while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
1262b725ae77Skettenis 
1263e93f7393Sniklas       if (answer >= 'a')
1264e93f7393Sniklas 	answer -= 040;
1265e93f7393Sniklas       if (answer == 'Y')
1266e93f7393Sniklas 	{
1267e93f7393Sniklas 	  retval = 1;
1268e93f7393Sniklas 	  break;
1269e93f7393Sniklas 	}
1270e93f7393Sniklas       if (answer == 'N')
1271e93f7393Sniklas 	{
1272e93f7393Sniklas 	  retval = 0;
1273e93f7393Sniklas 	  break;
1274e93f7393Sniklas 	}
1275e93f7393Sniklas       printf_filtered ("Please answer y or n.\n");
1276e93f7393Sniklas     }
1277e93f7393Sniklas 
1278e93f7393Sniklas   if (annotation_level > 1)
1279e93f7393Sniklas     printf_filtered ("\n\032\032post-query\n");
1280e93f7393Sniklas   return retval;
1281e93f7393Sniklas }
1282e93f7393Sniklas 
1283b725ae77Skettenis 
1284b725ae77Skettenis /* This function supports the nquery() and yquery() functions.
1285b725ae77Skettenis    Ask user a y-or-n question and return 0 if answer is no, 1 if
1286b725ae77Skettenis    answer is yes, or default the answer to the specified default.
1287b725ae77Skettenis    DEFCHAR is either 'y' or 'n' and refers to the default answer.
1288b725ae77Skettenis    CTLSTR is the control string and should end in "? ".  It should
1289b725ae77Skettenis    not say how to answer, because we do that.
1290b725ae77Skettenis    ARGS are the arguments passed along with the CTLSTR argument to
1291b725ae77Skettenis    printf.  */
1292b725ae77Skettenis 
1293b725ae77Skettenis static int
defaulted_query(const char * ctlstr,const char defchar,va_list args)1294b725ae77Skettenis defaulted_query (const char *ctlstr, const char defchar, va_list args)
1295b725ae77Skettenis {
1296b725ae77Skettenis   int answer;
1297b725ae77Skettenis   int ans2;
1298b725ae77Skettenis   int retval;
1299b725ae77Skettenis   int def_value;
1300b725ae77Skettenis   char def_answer, not_def_answer;
1301b725ae77Skettenis   char *y_string, *n_string;
1302b725ae77Skettenis 
1303b725ae77Skettenis   /* Set up according to which answer is the default.  */
1304b725ae77Skettenis   if (defchar == 'y')
1305b725ae77Skettenis     {
1306b725ae77Skettenis       def_value = 1;
1307b725ae77Skettenis       def_answer = 'Y';
1308b725ae77Skettenis       not_def_answer = 'N';
1309b725ae77Skettenis       y_string = "[y]";
1310b725ae77Skettenis       n_string = "n";
1311b725ae77Skettenis     }
1312b725ae77Skettenis   else
1313b725ae77Skettenis     {
1314b725ae77Skettenis       def_value = 0;
1315b725ae77Skettenis       def_answer = 'N';
1316b725ae77Skettenis       not_def_answer = 'Y';
1317b725ae77Skettenis       y_string = "y";
1318b725ae77Skettenis       n_string = "[n]";
1319b725ae77Skettenis     }
1320b725ae77Skettenis 
132163addd46Skettenis   if (deprecated_query_hook)
1322b725ae77Skettenis     {
132363addd46Skettenis       return deprecated_query_hook (ctlstr, args);
1324b725ae77Skettenis     }
1325b725ae77Skettenis 
1326b725ae77Skettenis   /* Automatically answer default value if input is not from a terminal.  */
1327b725ae77Skettenis   if (!input_from_terminal_p ())
1328b725ae77Skettenis     return def_value;
1329b725ae77Skettenis 
1330b725ae77Skettenis   while (1)
1331b725ae77Skettenis     {
1332b725ae77Skettenis       wrap_here ("");		/* Flush any buffered output */
1333b725ae77Skettenis       gdb_flush (gdb_stdout);
1334b725ae77Skettenis 
1335b725ae77Skettenis       if (annotation_level > 1)
133663addd46Skettenis 	printf_filtered ("\n\032\032pre-query\n");
1337b725ae77Skettenis 
1338b725ae77Skettenis       vfprintf_filtered (gdb_stdout, ctlstr, args);
1339b725ae77Skettenis       printf_filtered ("(%s or %s) ", y_string, n_string);
1340b725ae77Skettenis 
1341b725ae77Skettenis       if (annotation_level > 1)
134263addd46Skettenis 	printf_filtered ("\n\032\032query\n");
1343b725ae77Skettenis 
1344b725ae77Skettenis       wrap_here ("");
1345b725ae77Skettenis       gdb_flush (gdb_stdout);
1346b725ae77Skettenis 
1347b725ae77Skettenis       answer = fgetc (stdin);
1348b725ae77Skettenis       clearerr (stdin);		/* in case of C-d */
1349b725ae77Skettenis       if (answer == EOF)	/* C-d */
1350b725ae77Skettenis 	{
1351b725ae77Skettenis 	  retval = def_value;
1352b725ae77Skettenis 	  break;
1353b725ae77Skettenis 	}
1354b725ae77Skettenis       /* Eat rest of input line, to EOF or newline */
1355b725ae77Skettenis       if (answer != '\n')
1356b725ae77Skettenis 	do
1357b725ae77Skettenis 	  {
1358b725ae77Skettenis 	    ans2 = fgetc (stdin);
1359b725ae77Skettenis 	    clearerr (stdin);
1360b725ae77Skettenis 	  }
1361b725ae77Skettenis 	while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
1362b725ae77Skettenis 
1363b725ae77Skettenis       if (answer >= 'a')
1364b725ae77Skettenis 	answer -= 040;
1365b725ae77Skettenis       /* Check answer.  For the non-default, the user must specify
1366b725ae77Skettenis          the non-default explicitly.  */
1367b725ae77Skettenis       if (answer == not_def_answer)
1368b725ae77Skettenis 	{
1369b725ae77Skettenis 	  retval = !def_value;
1370b725ae77Skettenis 	  break;
1371b725ae77Skettenis 	}
1372b725ae77Skettenis       /* Otherwise, for the default, the user may either specify
1373b725ae77Skettenis          the required input or have it default by entering nothing.  */
1374b725ae77Skettenis       if (answer == def_answer || answer == '\n' ||
1375b725ae77Skettenis 	  answer == '\r' || answer == EOF)
1376b725ae77Skettenis 	{
1377b725ae77Skettenis 	  retval = def_value;
1378b725ae77Skettenis 	  break;
1379b725ae77Skettenis 	}
1380b725ae77Skettenis       /* Invalid entries are not defaulted and require another selection.  */
1381b725ae77Skettenis       printf_filtered ("Please answer %s or %s.\n",
1382b725ae77Skettenis 		       y_string, n_string);
1383b725ae77Skettenis     }
1384b725ae77Skettenis 
1385b725ae77Skettenis   if (annotation_level > 1)
138663addd46Skettenis     printf_filtered ("\n\032\032post-query\n");
1387b725ae77Skettenis   return retval;
1388b725ae77Skettenis }
1389b725ae77Skettenis 
1390b725ae77Skettenis 
1391b725ae77Skettenis /* Ask user a y-or-n question and return 0 if answer is no, 1 if
1392b725ae77Skettenis    answer is yes, or 0 if answer is defaulted.
1393b725ae77Skettenis    Takes three args which are given to printf to print the question.
1394b725ae77Skettenis    The first, a control string, should end in "? ".
1395b725ae77Skettenis    It should not say how to answer, because we do that.  */
1396b725ae77Skettenis 
1397b725ae77Skettenis int
nquery(const char * ctlstr,...)1398b725ae77Skettenis nquery (const char *ctlstr, ...)
1399b725ae77Skettenis {
1400b725ae77Skettenis   va_list args;
1401b725ae77Skettenis 
1402b725ae77Skettenis   va_start (args, ctlstr);
1403b725ae77Skettenis   return defaulted_query (ctlstr, 'n', args);
1404b725ae77Skettenis   va_end (args);
1405b725ae77Skettenis }
1406b725ae77Skettenis 
1407b725ae77Skettenis /* Ask user a y-or-n question and return 0 if answer is no, 1 if
1408b725ae77Skettenis    answer is yes, or 1 if answer is defaulted.
1409b725ae77Skettenis    Takes three args which are given to printf to print the question.
1410b725ae77Skettenis    The first, a control string, should end in "? ".
1411b725ae77Skettenis    It should not say how to answer, because we do that.  */
1412b725ae77Skettenis 
1413b725ae77Skettenis int
yquery(const char * ctlstr,...)1414b725ae77Skettenis yquery (const char *ctlstr, ...)
1415b725ae77Skettenis {
1416b725ae77Skettenis   va_list args;
1417b725ae77Skettenis 
1418b725ae77Skettenis   va_start (args, ctlstr);
1419b725ae77Skettenis   return defaulted_query (ctlstr, 'y', args);
1420b725ae77Skettenis   va_end (args);
1421b725ae77Skettenis }
1422b725ae77Skettenis 
1423b725ae77Skettenis /* Print an error message saying that we couldn't make sense of a
1424b725ae77Skettenis    \^mumble sequence in a string or character constant.  START and END
1425b725ae77Skettenis    indicate a substring of some larger string that contains the
1426b725ae77Skettenis    erroneous backslash sequence, missing the initial backslash.  */
1427b725ae77Skettenis static NORETURN int
no_control_char_error(const char * start,const char * end)1428b725ae77Skettenis no_control_char_error (const char *start, const char *end)
1429b725ae77Skettenis {
1430b725ae77Skettenis   int len = end - start;
1431b725ae77Skettenis   char *copy = alloca (end - start + 1);
1432b725ae77Skettenis 
1433b725ae77Skettenis   memcpy (copy, start, len);
1434b725ae77Skettenis   copy[len] = '\0';
1435b725ae77Skettenis 
1436b725ae77Skettenis   error ("There is no control character `\\%s' in the `%s' character set.",
1437b725ae77Skettenis 	 copy, target_charset ());
1438b725ae77Skettenis }
1439b725ae77Skettenis 
1440e93f7393Sniklas /* Parse a C escape sequence.  STRING_PTR points to a variable
1441e93f7393Sniklas    containing a pointer to the string to parse.  That pointer
1442e93f7393Sniklas    should point to the character after the \.  That pointer
1443e93f7393Sniklas    is updated past the characters we use.  The value of the
1444e93f7393Sniklas    escape sequence is returned.
1445e93f7393Sniklas 
1446e93f7393Sniklas    A negative value means the sequence \ newline was seen,
1447e93f7393Sniklas    which is supposed to be equivalent to nothing at all.
1448e93f7393Sniklas 
1449e93f7393Sniklas    If \ is followed by a null character, we return a negative
1450e93f7393Sniklas    value and leave the string pointer pointing at the null character.
1451e93f7393Sniklas 
1452e93f7393Sniklas    If \ is followed by 000, we return 0 and leave the string pointer
1453e93f7393Sniklas    after the zeros.  A value of 0 does not mean end of string.  */
1454e93f7393Sniklas 
1455e93f7393Sniklas int
parse_escape(char ** string_ptr)1456b725ae77Skettenis parse_escape (char **string_ptr)
1457e93f7393Sniklas {
1458b725ae77Skettenis   int target_char;
1459b725ae77Skettenis   int c = *(*string_ptr)++;
1460b725ae77Skettenis   if (c_parse_backslash (c, &target_char))
1461b725ae77Skettenis     return target_char;
1462b725ae77Skettenis   else
1463e93f7393Sniklas     switch (c)
1464e93f7393Sniklas       {
1465e93f7393Sniklas       case '\n':
1466e93f7393Sniklas 	return -2;
1467e93f7393Sniklas       case 0:
1468e93f7393Sniklas 	(*string_ptr)--;
1469e93f7393Sniklas 	return 0;
1470e93f7393Sniklas       case '^':
1471b725ae77Skettenis 	{
1472b725ae77Skettenis 	  /* Remember where this escape sequence started, for reporting
1473b725ae77Skettenis 	     errors.  */
1474b725ae77Skettenis 	  char *sequence_start_pos = *string_ptr - 1;
1475b725ae77Skettenis 
1476e93f7393Sniklas 	  c = *(*string_ptr)++;
1477b725ae77Skettenis 
1478e93f7393Sniklas 	  if (c == '?')
1479b725ae77Skettenis 	    {
1480b725ae77Skettenis 	      /* XXXCHARSET: What is `delete' in the host character set?  */
1481b725ae77Skettenis 	      c = 0177;
1482b725ae77Skettenis 
1483b725ae77Skettenis 	      if (!host_char_to_target (c, &target_char))
1484b725ae77Skettenis 		error ("There is no character corresponding to `Delete' "
1485b725ae77Skettenis 		       "in the target character set `%s'.", host_charset ());
1486b725ae77Skettenis 
1487b725ae77Skettenis 	      return target_char;
1488b725ae77Skettenis 	    }
1489b725ae77Skettenis 	  else if (c == '\\')
1490b725ae77Skettenis 	    target_char = parse_escape (string_ptr);
1491b725ae77Skettenis 	  else
1492b725ae77Skettenis 	    {
1493b725ae77Skettenis 	      if (!host_char_to_target (c, &target_char))
1494b725ae77Skettenis 		no_control_char_error (sequence_start_pos, *string_ptr);
1495b725ae77Skettenis 	    }
1496b725ae77Skettenis 
1497b725ae77Skettenis 	  /* Now target_char is something like `c', and we want to find
1498b725ae77Skettenis 	     its control-character equivalent.  */
1499b725ae77Skettenis 	  if (!target_char_to_control_char (target_char, &target_char))
1500b725ae77Skettenis 	    no_control_char_error (sequence_start_pos, *string_ptr);
1501b725ae77Skettenis 
1502b725ae77Skettenis 	  return target_char;
1503b725ae77Skettenis 	}
1504b725ae77Skettenis 
1505b725ae77Skettenis 	/* XXXCHARSET: we need to use isdigit and value-of-digit
1506b725ae77Skettenis 	   methods of the host character set here.  */
1507e93f7393Sniklas 
1508e93f7393Sniklas       case '0':
1509e93f7393Sniklas       case '1':
1510e93f7393Sniklas       case '2':
1511e93f7393Sniklas       case '3':
1512e93f7393Sniklas       case '4':
1513e93f7393Sniklas       case '5':
1514e93f7393Sniklas       case '6':
1515e93f7393Sniklas       case '7':
1516e93f7393Sniklas 	{
1517b725ae77Skettenis 	  int i = c - '0';
1518b725ae77Skettenis 	  int count = 0;
1519e93f7393Sniklas 	  while (++count < 3)
1520e93f7393Sniklas 	    {
1521b725ae77Skettenis 	      c = (**string_ptr);
1522b725ae77Skettenis 	      if (c >= '0' && c <= '7')
1523e93f7393Sniklas 		{
1524b725ae77Skettenis 		  (*string_ptr)++;
1525e93f7393Sniklas 		  i *= 8;
1526e93f7393Sniklas 		  i += c - '0';
1527e93f7393Sniklas 		}
1528e93f7393Sniklas 	      else
1529e93f7393Sniklas 		{
1530e93f7393Sniklas 		  break;
1531e93f7393Sniklas 		}
1532e93f7393Sniklas 	    }
1533e93f7393Sniklas 	  return i;
1534e93f7393Sniklas 	}
1535e93f7393Sniklas       default:
1536b725ae77Skettenis 	if (!host_char_to_target (c, &target_char))
1537b725ae77Skettenis 	  error
1538b725ae77Skettenis 	    ("The escape sequence `\%c' is equivalent to plain `%c', which"
1539b725ae77Skettenis 	     " has no equivalent\n" "in the `%s' character set.", c, c,
1540b725ae77Skettenis 	     target_charset ());
1541b725ae77Skettenis 	return target_char;
1542e93f7393Sniklas       }
1543e93f7393Sniklas }
1544e93f7393Sniklas 
1545e93f7393Sniklas /* Print the character C on STREAM as part of the contents of a literal
1546e93f7393Sniklas    string whose delimiter is QUOTER.  Note that this routine should only
1547e93f7393Sniklas    be call for printing things which are independent of the language
1548e93f7393Sniklas    of the program being debugged. */
1549e93f7393Sniklas 
1550b725ae77Skettenis static void
printchar(int c,void (* do_fputs)(const char *,struct ui_file *),void (* do_fprintf)(struct ui_file *,const char *,...),struct ui_file * stream,int quoter)1551b725ae77Skettenis printchar (int c, void (*do_fputs) (const char *, struct ui_file *),
1552b725ae77Skettenis 	   void (*do_fprintf) (struct ui_file *, const char *, ...),
1553b725ae77Skettenis 	   struct ui_file *stream, int quoter)
1554e93f7393Sniklas {
1555e93f7393Sniklas 
1556e93f7393Sniklas   c &= 0xFF;			/* Avoid sign bit follies */
1557e93f7393Sniklas 
1558e93f7393Sniklas   if (c < 0x20 ||		/* Low control chars */
1559e93f7393Sniklas       (c >= 0x7F && c < 0xA0) ||	/* DEL, High controls */
1560b725ae77Skettenis       (sevenbit_strings && c >= 0x80))
1561b725ae77Skettenis     {				/* high order bit set */
1562e93f7393Sniklas       switch (c)
1563e93f7393Sniklas 	{
1564e93f7393Sniklas 	case '\n':
1565b725ae77Skettenis 	  do_fputs ("\\n", stream);
1566e93f7393Sniklas 	  break;
1567e93f7393Sniklas 	case '\b':
1568b725ae77Skettenis 	  do_fputs ("\\b", stream);
1569e93f7393Sniklas 	  break;
1570e93f7393Sniklas 	case '\t':
1571b725ae77Skettenis 	  do_fputs ("\\t", stream);
1572e93f7393Sniklas 	  break;
1573e93f7393Sniklas 	case '\f':
1574b725ae77Skettenis 	  do_fputs ("\\f", stream);
1575e93f7393Sniklas 	  break;
1576e93f7393Sniklas 	case '\r':
1577b725ae77Skettenis 	  do_fputs ("\\r", stream);
1578e93f7393Sniklas 	  break;
1579e93f7393Sniklas 	case '\033':
1580b725ae77Skettenis 	  do_fputs ("\\e", stream);
1581e93f7393Sniklas 	  break;
1582e93f7393Sniklas 	case '\007':
1583b725ae77Skettenis 	  do_fputs ("\\a", stream);
1584e93f7393Sniklas 	  break;
1585e93f7393Sniklas 	default:
1586b725ae77Skettenis 	  do_fprintf (stream, "\\%.3o", (unsigned int) c);
1587e93f7393Sniklas 	  break;
1588e93f7393Sniklas 	}
1589e93f7393Sniklas     }
1590b725ae77Skettenis   else
1591b725ae77Skettenis     {
1592b725ae77Skettenis       if (c == '\\' || c == quoter)
1593b725ae77Skettenis 	do_fputs ("\\", stream);
1594b725ae77Skettenis       do_fprintf (stream, "%c", c);
1595b725ae77Skettenis     }
1596b725ae77Skettenis }
1597b725ae77Skettenis 
1598b725ae77Skettenis /* Print the character C on STREAM as part of the contents of a
1599b725ae77Skettenis    literal string whose delimiter is QUOTER.  Note that these routines
1600b725ae77Skettenis    should only be call for printing things which are independent of
1601b725ae77Skettenis    the language of the program being debugged. */
1602b725ae77Skettenis 
1603b725ae77Skettenis void
fputstr_filtered(const char * str,int quoter,struct ui_file * stream)1604b725ae77Skettenis fputstr_filtered (const char *str, int quoter, struct ui_file *stream)
1605b725ae77Skettenis {
1606b725ae77Skettenis   while (*str)
1607b725ae77Skettenis     printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter);
1608b725ae77Skettenis }
1609b725ae77Skettenis 
1610b725ae77Skettenis void
fputstr_unfiltered(const char * str,int quoter,struct ui_file * stream)1611b725ae77Skettenis fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream)
1612b725ae77Skettenis {
1613b725ae77Skettenis   while (*str)
1614b725ae77Skettenis     printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1615b725ae77Skettenis }
1616b725ae77Skettenis 
1617b725ae77Skettenis void
fputstrn_unfiltered(const char * str,int n,int quoter,struct ui_file * stream)1618b725ae77Skettenis fputstrn_unfiltered (const char *str, int n, int quoter,
1619b725ae77Skettenis 		     struct ui_file *stream)
1620b725ae77Skettenis {
1621b725ae77Skettenis   int i;
1622b725ae77Skettenis   for (i = 0; i < n; i++)
1623b725ae77Skettenis     printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1624e93f7393Sniklas }
1625e93f7393Sniklas 
1626b725ae77Skettenis 
1627e93f7393Sniklas /* Number of lines per page or UINT_MAX if paging is disabled.  */
1628e93f7393Sniklas static unsigned int lines_per_page;
1629b725ae77Skettenis 
1630b725ae77Skettenis /* Number of chars per line or UINT_MAX if line folding is disabled.  */
1631e93f7393Sniklas static unsigned int chars_per_line;
1632b725ae77Skettenis 
1633e93f7393Sniklas /* Current count of lines printed on this page, chars on this line.  */
1634e93f7393Sniklas static unsigned int lines_printed, chars_printed;
1635e93f7393Sniklas 
1636e93f7393Sniklas /* Buffer and start column of buffered text, for doing smarter word-
1637e93f7393Sniklas    wrapping.  When someone calls wrap_here(), we start buffering output
1638e93f7393Sniklas    that comes through fputs_filtered().  If we see a newline, we just
1639e93f7393Sniklas    spit it out and forget about the wrap_here().  If we see another
1640e93f7393Sniklas    wrap_here(), we spit it out and remember the newer one.  If we see
1641e93f7393Sniklas    the end of the line, we spit out a newline, the indent, and then
1642e93f7393Sniklas    the buffered output.  */
1643e93f7393Sniklas 
1644e93f7393Sniklas /* Malloc'd buffer with chars_per_line+2 bytes.  Contains characters which
1645e93f7393Sniklas    are waiting to be output (they have already been counted in chars_printed).
1646e93f7393Sniklas    When wrap_buffer[0] is null, the buffer is empty.  */
1647e93f7393Sniklas static char *wrap_buffer;
1648e93f7393Sniklas 
1649e93f7393Sniklas /* Pointer in wrap_buffer to the next character to fill.  */
1650e93f7393Sniklas static char *wrap_pointer;
1651e93f7393Sniklas 
1652e93f7393Sniklas /* String to indent by if the wrap occurs.  Must not be NULL if wrap_column
1653e93f7393Sniklas    is non-zero.  */
1654e93f7393Sniklas static char *wrap_indent;
1655e93f7393Sniklas 
1656e93f7393Sniklas /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1657e93f7393Sniklas    is not in effect.  */
1658e93f7393Sniklas static int wrap_column;
1659b725ae77Skettenis 
1660e93f7393Sniklas 
1661b725ae77Skettenis /* Inialize the number of lines per page and chars per line.  */
1662b725ae77Skettenis 
1663b725ae77Skettenis void
init_page_info(void)1664b725ae77Skettenis init_page_info (void)
1665e93f7393Sniklas {
1666b725ae77Skettenis #if defined(TUI)
1667b725ae77Skettenis   if (!tui_get_command_dimension (&chars_per_line, &lines_per_page))
1668b725ae77Skettenis #endif
1669b725ae77Skettenis     {
1670b725ae77Skettenis       int rows, cols;
1671b725ae77Skettenis 
1672b725ae77Skettenis #if defined(__GO32__)
1673b725ae77Skettenis       rows = ScreenRows ();
1674b725ae77Skettenis       cols = ScreenCols ();
1675b725ae77Skettenis       lines_per_page = rows;
1676b725ae77Skettenis       chars_per_line = cols;
1677b725ae77Skettenis #else
1678b725ae77Skettenis       /* Make sure Readline has initialized its terminal settings.  */
1679b725ae77Skettenis       rl_reset_terminal (NULL);
1680b725ae77Skettenis 
1681b725ae77Skettenis       /* Get the screen size from Readline.  */
1682b725ae77Skettenis       rl_get_screen_size (&rows, &cols);
1683b725ae77Skettenis       lines_per_page = rows;
1684b725ae77Skettenis       chars_per_line = cols;
1685b725ae77Skettenis 
1686b725ae77Skettenis       /* Readline should have fetched the termcap entry for us.  */
1687b725ae77Skettenis       if (tgetnum ("li") < 0 || getenv ("EMACS"))
1688b725ae77Skettenis 	{
1689b725ae77Skettenis 	  /* The number of lines per page is not mentioned in the
1690b725ae77Skettenis 	     terminal description.  This probably means that paging is
1691b725ae77Skettenis 	     not useful (e.g. emacs shell window), so disable paging.  */
1692b725ae77Skettenis 	  lines_per_page = UINT_MAX;
1693b725ae77Skettenis 	}
1694b725ae77Skettenis 
1695b725ae77Skettenis       /* FIXME: Get rid of this junk.  */
1696b725ae77Skettenis #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1697b725ae77Skettenis       SIGWINCH_HANDLER (SIGWINCH);
1698b725ae77Skettenis #endif
1699b725ae77Skettenis 
1700b725ae77Skettenis       /* If the output is not a terminal, don't paginate it.  */
1701b725ae77Skettenis       if (!ui_file_isatty (gdb_stdout))
1702b725ae77Skettenis 	lines_per_page = UINT_MAX;
1703b725ae77Skettenis #endif
1704b725ae77Skettenis     }
1705b725ae77Skettenis 
1706b725ae77Skettenis   set_screen_size ();
1707b725ae77Skettenis   set_width ();
1708b725ae77Skettenis }
1709b725ae77Skettenis 
1710b725ae77Skettenis /* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE.  */
1711b725ae77Skettenis 
1712b725ae77Skettenis static void
set_screen_size(void)1713b725ae77Skettenis set_screen_size (void)
1714b725ae77Skettenis {
1715b725ae77Skettenis   int rows = lines_per_page;
1716b725ae77Skettenis   int cols = chars_per_line;
1717b725ae77Skettenis 
1718b725ae77Skettenis   if (rows <= 0)
1719b725ae77Skettenis     rows = INT_MAX;
1720b725ae77Skettenis 
1721b725ae77Skettenis   if (cols <= 0)
1722b725ae77Skettenis     rl_get_screen_size (NULL, &cols);
1723b725ae77Skettenis 
1724b725ae77Skettenis   /* Update Readline's idea of the terminal size.  */
1725b725ae77Skettenis   rl_set_screen_size (rows, cols);
1726b725ae77Skettenis }
1727b725ae77Skettenis 
1728b725ae77Skettenis /* Reinitialize WRAP_BUFFER according to the current value of
1729b725ae77Skettenis    CHARS_PER_LINE.  */
1730b725ae77Skettenis 
1731b725ae77Skettenis static void
set_width(void)1732b725ae77Skettenis set_width (void)
1733b725ae77Skettenis {
1734b725ae77Skettenis   if (chars_per_line == 0)
1735b725ae77Skettenis     init_page_info ();
1736b725ae77Skettenis 
1737e93f7393Sniklas   if (!wrap_buffer)
1738e93f7393Sniklas     {
1739e93f7393Sniklas       wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1740e93f7393Sniklas       wrap_buffer[0] = '\0';
1741e93f7393Sniklas     }
1742e93f7393Sniklas   else
1743e93f7393Sniklas     wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1744b725ae77Skettenis   wrap_pointer = wrap_buffer;	/* Start it at the beginning.  */
1745b725ae77Skettenis }
1746b725ae77Skettenis 
1747b725ae77Skettenis static void
set_width_command(char * args,int from_tty,struct cmd_list_element * c)1748b725ae77Skettenis set_width_command (char *args, int from_tty, struct cmd_list_element *c)
1749b725ae77Skettenis {
1750b725ae77Skettenis   set_screen_size ();
1751b725ae77Skettenis   set_width ();
1752b725ae77Skettenis }
1753b725ae77Skettenis 
1754b725ae77Skettenis static void
set_height_command(char * args,int from_tty,struct cmd_list_element * c)1755b725ae77Skettenis set_height_command (char *args, int from_tty, struct cmd_list_element *c)
1756b725ae77Skettenis {
1757b725ae77Skettenis   set_screen_size ();
1758e93f7393Sniklas }
1759e93f7393Sniklas 
1760e93f7393Sniklas /* Wait, so the user can read what's on the screen.  Prompt the user
1761e93f7393Sniklas    to continue by pressing RETURN.  */
1762e93f7393Sniklas 
1763e93f7393Sniklas static void
prompt_for_continue(void)1764b725ae77Skettenis prompt_for_continue (void)
1765e93f7393Sniklas {
1766e93f7393Sniklas   char *ignore;
1767e93f7393Sniklas   char cont_prompt[120];
1768e93f7393Sniklas 
1769e93f7393Sniklas   if (annotation_level > 1)
1770e93f7393Sniklas     printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1771e93f7393Sniklas 
1772e93f7393Sniklas   strcpy (cont_prompt,
1773e93f7393Sniklas 	  "---Type <return> to continue, or q <return> to quit---");
1774e93f7393Sniklas   if (annotation_level > 1)
1775e93f7393Sniklas     strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1776e93f7393Sniklas 
1777e93f7393Sniklas   /* We must do this *before* we call gdb_readline, else it will eventually
1778e93f7393Sniklas      call us -- thinking that we're trying to print beyond the end of the
1779e93f7393Sniklas      screen.  */
1780e93f7393Sniklas   reinitialize_more_filter ();
1781e93f7393Sniklas 
1782e93f7393Sniklas   immediate_quit++;
1783e93f7393Sniklas   /* On a real operating system, the user can quit with SIGINT.
1784e93f7393Sniklas      But not on GO32.
1785e93f7393Sniklas 
1786e93f7393Sniklas      'q' is provided on all systems so users don't have to change habits
1787e93f7393Sniklas      from system to system, and because telling them what to do in
1788e93f7393Sniklas      the prompt is more user-friendly than expecting them to think of
1789e93f7393Sniklas      SIGINT.  */
1790e93f7393Sniklas   /* Call readline, not gdb_readline, because GO32 readline handles control-C
1791e93f7393Sniklas      whereas control-C to gdb_readline will cause the user to get dumped
1792e93f7393Sniklas      out to DOS.  */
1793b725ae77Skettenis   ignore = gdb_readline_wrapper (cont_prompt);
1794e93f7393Sniklas 
1795e93f7393Sniklas   if (annotation_level > 1)
1796e93f7393Sniklas     printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1797e93f7393Sniklas 
1798e93f7393Sniklas   if (ignore)
1799e93f7393Sniklas     {
1800e93f7393Sniklas       char *p = ignore;
1801e93f7393Sniklas       while (*p == ' ' || *p == '\t')
1802e93f7393Sniklas 	++p;
1803e93f7393Sniklas       if (p[0] == 'q')
1804b725ae77Skettenis 	async_request_quit (0);
1805b725ae77Skettenis       xfree (ignore);
1806e93f7393Sniklas     }
1807e93f7393Sniklas   immediate_quit--;
1808e93f7393Sniklas 
1809e93f7393Sniklas   /* Now we have to do this again, so that GDB will know that it doesn't
1810e93f7393Sniklas      need to save the ---Type <return>--- line at the top of the screen.  */
1811e93f7393Sniklas   reinitialize_more_filter ();
1812e93f7393Sniklas 
1813e93f7393Sniklas   dont_repeat ();		/* Forget prev cmd -- CR won't repeat it. */
1814e93f7393Sniklas }
1815e93f7393Sniklas 
1816e93f7393Sniklas /* Reinitialize filter; ie. tell it to reset to original values.  */
1817e93f7393Sniklas 
1818e93f7393Sniklas void
reinitialize_more_filter(void)1819b725ae77Skettenis reinitialize_more_filter (void)
1820e93f7393Sniklas {
1821e93f7393Sniklas   lines_printed = 0;
1822e93f7393Sniklas   chars_printed = 0;
1823e93f7393Sniklas }
1824e93f7393Sniklas 
1825e93f7393Sniklas /* Indicate that if the next sequence of characters overflows the line,
1826e93f7393Sniklas    a newline should be inserted here rather than when it hits the end.
1827e93f7393Sniklas    If INDENT is non-null, it is a string to be printed to indent the
1828e93f7393Sniklas    wrapped part on the next line.  INDENT must remain accessible until
1829e93f7393Sniklas    the next call to wrap_here() or until a newline is printed through
1830e93f7393Sniklas    fputs_filtered().
1831e93f7393Sniklas 
1832e93f7393Sniklas    If the line is already overfull, we immediately print a newline and
1833e93f7393Sniklas    the indentation, and disable further wrapping.
1834e93f7393Sniklas 
1835e93f7393Sniklas    If we don't know the width of lines, but we know the page height,
1836e93f7393Sniklas    we must not wrap words, but should still keep track of newlines
1837e93f7393Sniklas    that were explicitly printed.
1838e93f7393Sniklas 
1839e93f7393Sniklas    INDENT should not contain tabs, as that will mess up the char count
1840e93f7393Sniklas    on the next line.  FIXME.
1841e93f7393Sniklas 
1842e93f7393Sniklas    This routine is guaranteed to force out any output which has been
1843e93f7393Sniklas    squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1844e93f7393Sniklas    used to force out output from the wrap_buffer.  */
1845e93f7393Sniklas 
1846e93f7393Sniklas void
wrap_here(char * indent)1847b725ae77Skettenis wrap_here (char *indent)
1848e93f7393Sniklas {
1849e93f7393Sniklas   /* This should have been allocated, but be paranoid anyway. */
1850e93f7393Sniklas   if (!wrap_buffer)
1851b725ae77Skettenis     internal_error (__FILE__, __LINE__, "failed internal consistency check");
1852e93f7393Sniklas 
1853e93f7393Sniklas   if (wrap_buffer[0])
1854e93f7393Sniklas     {
1855e93f7393Sniklas       *wrap_pointer = '\0';
1856e93f7393Sniklas       fputs_unfiltered (wrap_buffer, gdb_stdout);
1857e93f7393Sniklas     }
1858e93f7393Sniklas   wrap_pointer = wrap_buffer;
1859e93f7393Sniklas   wrap_buffer[0] = '\0';
1860e93f7393Sniklas   if (chars_per_line == UINT_MAX)	/* No line overflow checking */
1861e93f7393Sniklas     {
1862e93f7393Sniklas       wrap_column = 0;
1863e93f7393Sniklas     }
1864e93f7393Sniklas   else if (chars_printed >= chars_per_line)
1865e93f7393Sniklas     {
1866e93f7393Sniklas       puts_filtered ("\n");
1867e93f7393Sniklas       if (indent != NULL)
1868e93f7393Sniklas 	puts_filtered (indent);
1869e93f7393Sniklas       wrap_column = 0;
1870e93f7393Sniklas     }
1871e93f7393Sniklas   else
1872e93f7393Sniklas     {
1873e93f7393Sniklas       wrap_column = chars_printed;
1874e93f7393Sniklas       if (indent == NULL)
1875e93f7393Sniklas 	wrap_indent = "";
1876e93f7393Sniklas       else
1877e93f7393Sniklas 	wrap_indent = indent;
1878e93f7393Sniklas     }
1879e93f7393Sniklas }
1880e93f7393Sniklas 
1881b725ae77Skettenis /* Print input string to gdb_stdout, filtered, with wrap,
1882b725ae77Skettenis    arranging strings in columns of n chars. String can be
1883b725ae77Skettenis    right or left justified in the column.  Never prints
1884b725ae77Skettenis    trailing spaces.  String should never be longer than
1885b725ae77Skettenis    width.  FIXME: this could be useful for the EXAMINE
1886b725ae77Skettenis    command, which currently doesn't tabulate very well */
1887b725ae77Skettenis 
1888b725ae77Skettenis void
puts_filtered_tabular(char * string,int width,int right)1889b725ae77Skettenis puts_filtered_tabular (char *string, int width, int right)
1890b725ae77Skettenis {
1891b725ae77Skettenis   int spaces = 0;
1892b725ae77Skettenis   int stringlen;
1893b725ae77Skettenis   char *spacebuf;
1894b725ae77Skettenis 
1895b725ae77Skettenis   gdb_assert (chars_per_line > 0);
1896b725ae77Skettenis   if (chars_per_line == UINT_MAX)
1897b725ae77Skettenis     {
1898b725ae77Skettenis       fputs_filtered (string, gdb_stdout);
1899b725ae77Skettenis       fputs_filtered ("\n", gdb_stdout);
1900b725ae77Skettenis       return;
1901b725ae77Skettenis     }
1902b725ae77Skettenis 
1903b725ae77Skettenis   if (((chars_printed - 1) / width + 2) * width >= chars_per_line)
1904b725ae77Skettenis     fputs_filtered ("\n", gdb_stdout);
1905b725ae77Skettenis 
1906b725ae77Skettenis   if (width >= chars_per_line)
1907b725ae77Skettenis     width = chars_per_line - 1;
1908b725ae77Skettenis 
1909b725ae77Skettenis   stringlen = strlen (string);
1910b725ae77Skettenis 
1911b725ae77Skettenis   if (chars_printed > 0)
1912b725ae77Skettenis     spaces = width - (chars_printed - 1) % width - 1;
1913b725ae77Skettenis   if (right)
1914b725ae77Skettenis     spaces += width - stringlen;
1915b725ae77Skettenis 
1916b725ae77Skettenis   spacebuf = alloca (spaces + 1);
1917b725ae77Skettenis   spacebuf[spaces] = '\0';
1918b725ae77Skettenis   while (spaces--)
1919b725ae77Skettenis     spacebuf[spaces] = ' ';
1920b725ae77Skettenis 
1921b725ae77Skettenis   fputs_filtered (spacebuf, gdb_stdout);
1922b725ae77Skettenis   fputs_filtered (string, gdb_stdout);
1923b725ae77Skettenis }
1924b725ae77Skettenis 
1925b725ae77Skettenis 
1926e93f7393Sniklas /* Ensure that whatever gets printed next, using the filtered output
1927e93f7393Sniklas    commands, starts at the beginning of the line.  I.E. if there is
1928e93f7393Sniklas    any pending output for the current line, flush it and start a new
1929e93f7393Sniklas    line.  Otherwise do nothing. */
1930e93f7393Sniklas 
1931e93f7393Sniklas void
begin_line(void)1932b725ae77Skettenis begin_line (void)
1933e93f7393Sniklas {
1934e93f7393Sniklas   if (chars_printed > 0)
1935e93f7393Sniklas     {
1936e93f7393Sniklas       puts_filtered ("\n");
1937e93f7393Sniklas     }
1938e93f7393Sniklas }
1939e93f7393Sniklas 
1940e93f7393Sniklas 
1941e93f7393Sniklas /* Like fputs but if FILTER is true, pause after every screenful.
1942e93f7393Sniklas 
1943e93f7393Sniklas    Regardless of FILTER can wrap at points other than the final
1944e93f7393Sniklas    character of a line.
1945e93f7393Sniklas 
1946e93f7393Sniklas    Unlike fputs, fputs_maybe_filtered does not return a value.
1947e93f7393Sniklas    It is OK for LINEBUFFER to be NULL, in which case just don't print
1948e93f7393Sniklas    anything.
1949e93f7393Sniklas 
1950e93f7393Sniklas    Note that a longjmp to top level may occur in this routine (only if
1951e93f7393Sniklas    FILTER is true) (since prompt_for_continue may do so) so this
1952e93f7393Sniklas    routine should not be called when cleanups are not in place.  */
1953e93f7393Sniklas 
1954e93f7393Sniklas static void
fputs_maybe_filtered(const char * linebuffer,struct ui_file * stream,int filter)1955b725ae77Skettenis fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
1956b725ae77Skettenis 		      int filter)
1957e93f7393Sniklas {
1958e93f7393Sniklas   const char *lineptr;
1959e93f7393Sniklas 
1960e93f7393Sniklas   if (linebuffer == 0)
1961e93f7393Sniklas     return;
1962e93f7393Sniklas 
1963e93f7393Sniklas   /* Don't do any filtering if it is disabled.  */
1964b725ae77Skettenis   if ((stream != gdb_stdout) || !pagination_enabled
1965e93f7393Sniklas       || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1966e93f7393Sniklas     {
1967e93f7393Sniklas       fputs_unfiltered (linebuffer, stream);
1968e93f7393Sniklas       return;
1969e93f7393Sniklas     }
1970e93f7393Sniklas 
1971e93f7393Sniklas   /* Go through and output each character.  Show line extension
1972e93f7393Sniklas      when this is necessary; prompt user for new page when this is
1973e93f7393Sniklas      necessary.  */
1974e93f7393Sniklas 
1975e93f7393Sniklas   lineptr = linebuffer;
1976e93f7393Sniklas   while (*lineptr)
1977e93f7393Sniklas     {
1978e93f7393Sniklas       /* Possible new page.  */
1979b725ae77Skettenis       if (filter && (lines_printed >= lines_per_page - 1))
1980e93f7393Sniklas 	prompt_for_continue ();
1981e93f7393Sniklas 
1982e93f7393Sniklas       while (*lineptr && *lineptr != '\n')
1983e93f7393Sniklas 	{
1984e93f7393Sniklas 	  /* Print a single line.  */
1985e93f7393Sniklas 	  if (*lineptr == '\t')
1986e93f7393Sniklas 	    {
1987e93f7393Sniklas 	      if (wrap_column)
1988e93f7393Sniklas 		*wrap_pointer++ = '\t';
1989e93f7393Sniklas 	      else
1990e93f7393Sniklas 		fputc_unfiltered ('\t', stream);
1991e93f7393Sniklas 	      /* Shifting right by 3 produces the number of tab stops
1992e93f7393Sniklas 	         we have already passed, and then adding one and
1993e93f7393Sniklas 	         shifting left 3 advances to the next tab stop.  */
1994e93f7393Sniklas 	      chars_printed = ((chars_printed >> 3) + 1) << 3;
1995e93f7393Sniklas 	      lineptr++;
1996e93f7393Sniklas 	    }
1997e93f7393Sniklas 	  else
1998e93f7393Sniklas 	    {
1999e93f7393Sniklas 	      if (wrap_column)
2000e93f7393Sniklas 		*wrap_pointer++ = *lineptr;
2001e93f7393Sniklas 	      else
2002e93f7393Sniklas 		fputc_unfiltered (*lineptr, stream);
2003e93f7393Sniklas 	      chars_printed++;
2004e93f7393Sniklas 	      lineptr++;
2005e93f7393Sniklas 	    }
2006e93f7393Sniklas 
2007e93f7393Sniklas 	  if (chars_printed >= chars_per_line)
2008e93f7393Sniklas 	    {
2009e93f7393Sniklas 	      unsigned int save_chars = chars_printed;
2010e93f7393Sniklas 
2011e93f7393Sniklas 	      chars_printed = 0;
2012e93f7393Sniklas 	      lines_printed++;
2013e93f7393Sniklas 	      /* If we aren't actually wrapping, don't output newline --
2014e93f7393Sniklas 	         if chars_per_line is right, we probably just overflowed
2015e93f7393Sniklas 	         anyway; if it's wrong, let us keep going.  */
2016e93f7393Sniklas 	      if (wrap_column)
2017e93f7393Sniklas 		fputc_unfiltered ('\n', stream);
2018e93f7393Sniklas 
2019e93f7393Sniklas 	      /* Possible new page.  */
2020e93f7393Sniklas 	      if (lines_printed >= lines_per_page - 1)
2021e93f7393Sniklas 		prompt_for_continue ();
2022e93f7393Sniklas 
2023e93f7393Sniklas 	      /* Now output indentation and wrapped string */
2024e93f7393Sniklas 	      if (wrap_column)
2025e93f7393Sniklas 		{
2026e93f7393Sniklas 		  fputs_unfiltered (wrap_indent, stream);
2027e93f7393Sniklas 		  *wrap_pointer = '\0';	/* Null-terminate saved stuff */
2028e93f7393Sniklas 		  fputs_unfiltered (wrap_buffer, stream);	/* and eject it */
2029e93f7393Sniklas 		  /* FIXME, this strlen is what prevents wrap_indent from
2030e93f7393Sniklas 		     containing tabs.  However, if we recurse to print it
2031e93f7393Sniklas 		     and count its chars, we risk trouble if wrap_indent is
2032e93f7393Sniklas 		     longer than (the user settable) chars_per_line.
2033e93f7393Sniklas 		     Note also that this can set chars_printed > chars_per_line
2034e93f7393Sniklas 		     if we are printing a long string.  */
2035e93f7393Sniklas 		  chars_printed = strlen (wrap_indent)
2036e93f7393Sniklas 		    + (save_chars - wrap_column);
2037e93f7393Sniklas 		  wrap_pointer = wrap_buffer;	/* Reset buffer */
2038e93f7393Sniklas 		  wrap_buffer[0] = '\0';
2039e93f7393Sniklas 		  wrap_column = 0;	/* And disable fancy wrap */
2040e93f7393Sniklas 		}
2041e93f7393Sniklas 	    }
2042e93f7393Sniklas 	}
2043e93f7393Sniklas 
2044e93f7393Sniklas       if (*lineptr == '\n')
2045e93f7393Sniklas 	{
2046e93f7393Sniklas 	  chars_printed = 0;
2047e93f7393Sniklas 	  wrap_here ((char *) 0);	/* Spit out chars, cancel further wraps */
2048e93f7393Sniklas 	  lines_printed++;
2049e93f7393Sniklas 	  fputc_unfiltered ('\n', stream);
2050e93f7393Sniklas 	  lineptr++;
2051e93f7393Sniklas 	}
2052e93f7393Sniklas     }
2053e93f7393Sniklas }
2054e93f7393Sniklas 
2055e93f7393Sniklas void
fputs_filtered(const char * linebuffer,struct ui_file * stream)2056b725ae77Skettenis fputs_filtered (const char *linebuffer, struct ui_file *stream)
2057e93f7393Sniklas {
2058e93f7393Sniklas   fputs_maybe_filtered (linebuffer, stream, 1);
2059e93f7393Sniklas }
2060e93f7393Sniklas 
2061e93f7393Sniklas int
putchar_unfiltered(int c)2062b725ae77Skettenis putchar_unfiltered (int c)
2063e93f7393Sniklas {
2064b725ae77Skettenis   char buf = c;
2065b725ae77Skettenis   ui_file_write (gdb_stdout, &buf, 1);
2066b725ae77Skettenis   return c;
2067b725ae77Skettenis }
2068e93f7393Sniklas 
2069b725ae77Skettenis /* Write character C to gdb_stdout using GDB's paging mechanism and return C.
2070b725ae77Skettenis    May return nonlocally.  */
2071b725ae77Skettenis 
2072b725ae77Skettenis int
putchar_filtered(int c)2073b725ae77Skettenis putchar_filtered (int c)
2074b725ae77Skettenis {
2075b725ae77Skettenis   return fputc_filtered (c, gdb_stdout);
2076b725ae77Skettenis }
2077b725ae77Skettenis 
2078b725ae77Skettenis int
fputc_unfiltered(int c,struct ui_file * stream)2079b725ae77Skettenis fputc_unfiltered (int c, struct ui_file *stream)
2080b725ae77Skettenis {
2081b725ae77Skettenis   char buf = c;
2082b725ae77Skettenis   ui_file_write (stream, &buf, 1);
2083e93f7393Sniklas   return c;
2084e93f7393Sniklas }
2085e93f7393Sniklas 
2086e93f7393Sniklas int
fputc_filtered(int c,struct ui_file * stream)2087b725ae77Skettenis fputc_filtered (int c, struct ui_file *stream)
2088e93f7393Sniklas {
2089e93f7393Sniklas   char buf[2];
2090e93f7393Sniklas 
2091e93f7393Sniklas   buf[0] = c;
2092e93f7393Sniklas   buf[1] = 0;
2093b725ae77Skettenis   fputs_filtered (buf, stream);
2094e93f7393Sniklas   return c;
2095e93f7393Sniklas }
2096e93f7393Sniklas 
2097b725ae77Skettenis /* puts_debug is like fputs_unfiltered, except it prints special
2098b725ae77Skettenis    characters in printable fashion.  */
2099b725ae77Skettenis 
2100b725ae77Skettenis void
puts_debug(char * prefix,char * string,char * suffix)2101b725ae77Skettenis puts_debug (char *prefix, char *string, char *suffix)
2102b725ae77Skettenis {
2103b725ae77Skettenis   int ch;
2104b725ae77Skettenis 
2105b725ae77Skettenis   /* Print prefix and suffix after each line.  */
2106b725ae77Skettenis   static int new_line = 1;
2107b725ae77Skettenis   static int return_p = 0;
2108b725ae77Skettenis   static char *prev_prefix = "";
2109b725ae77Skettenis   static char *prev_suffix = "";
2110b725ae77Skettenis 
2111b725ae77Skettenis   if (*string == '\n')
2112b725ae77Skettenis     return_p = 0;
2113b725ae77Skettenis 
2114b725ae77Skettenis   /* If the prefix is changing, print the previous suffix, a new line,
2115b725ae77Skettenis      and the new prefix.  */
2116b725ae77Skettenis   if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line)
2117b725ae77Skettenis     {
2118b725ae77Skettenis       fputs_unfiltered (prev_suffix, gdb_stdlog);
2119b725ae77Skettenis       fputs_unfiltered ("\n", gdb_stdlog);
2120b725ae77Skettenis       fputs_unfiltered (prefix, gdb_stdlog);
2121b725ae77Skettenis     }
2122b725ae77Skettenis 
2123b725ae77Skettenis   /* Print prefix if we printed a newline during the previous call.  */
2124b725ae77Skettenis   if (new_line)
2125b725ae77Skettenis     {
2126b725ae77Skettenis       new_line = 0;
2127b725ae77Skettenis       fputs_unfiltered (prefix, gdb_stdlog);
2128b725ae77Skettenis     }
2129b725ae77Skettenis 
2130b725ae77Skettenis   prev_prefix = prefix;
2131b725ae77Skettenis   prev_suffix = suffix;
2132b725ae77Skettenis 
2133b725ae77Skettenis   /* Output characters in a printable format.  */
2134b725ae77Skettenis   while ((ch = *string++) != '\0')
2135b725ae77Skettenis     {
2136b725ae77Skettenis       switch (ch)
2137b725ae77Skettenis 	{
2138b725ae77Skettenis 	default:
2139b725ae77Skettenis 	  if (isprint (ch))
2140b725ae77Skettenis 	    fputc_unfiltered (ch, gdb_stdlog);
2141b725ae77Skettenis 
2142b725ae77Skettenis 	  else
2143b725ae77Skettenis 	    fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff);
2144b725ae77Skettenis 	  break;
2145b725ae77Skettenis 
2146b725ae77Skettenis 	case '\\':
2147b725ae77Skettenis 	  fputs_unfiltered ("\\\\", gdb_stdlog);
2148b725ae77Skettenis 	  break;
2149b725ae77Skettenis 	case '\b':
2150b725ae77Skettenis 	  fputs_unfiltered ("\\b", gdb_stdlog);
2151b725ae77Skettenis 	  break;
2152b725ae77Skettenis 	case '\f':
2153b725ae77Skettenis 	  fputs_unfiltered ("\\f", gdb_stdlog);
2154b725ae77Skettenis 	  break;
2155b725ae77Skettenis 	case '\n':
2156b725ae77Skettenis 	  new_line = 1;
2157b725ae77Skettenis 	  fputs_unfiltered ("\\n", gdb_stdlog);
2158b725ae77Skettenis 	  break;
2159b725ae77Skettenis 	case '\r':
2160b725ae77Skettenis 	  fputs_unfiltered ("\\r", gdb_stdlog);
2161b725ae77Skettenis 	  break;
2162b725ae77Skettenis 	case '\t':
2163b725ae77Skettenis 	  fputs_unfiltered ("\\t", gdb_stdlog);
2164b725ae77Skettenis 	  break;
2165b725ae77Skettenis 	case '\v':
2166b725ae77Skettenis 	  fputs_unfiltered ("\\v", gdb_stdlog);
2167b725ae77Skettenis 	  break;
2168b725ae77Skettenis 	}
2169b725ae77Skettenis 
2170b725ae77Skettenis       return_p = ch == '\r';
2171b725ae77Skettenis     }
2172b725ae77Skettenis 
2173b725ae77Skettenis   /* Print suffix if we printed a newline.  */
2174b725ae77Skettenis   if (new_line)
2175b725ae77Skettenis     {
2176b725ae77Skettenis       fputs_unfiltered (suffix, gdb_stdlog);
2177b725ae77Skettenis       fputs_unfiltered ("\n", gdb_stdlog);
2178b725ae77Skettenis     }
2179b725ae77Skettenis }
2180b725ae77Skettenis 
2181e93f7393Sniklas 
2182e93f7393Sniklas /* Print a variable number of ARGS using format FORMAT.  If this
2183e93f7393Sniklas    information is going to put the amount written (since the last call
2184e93f7393Sniklas    to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
2185e93f7393Sniklas    call prompt_for_continue to get the users permision to continue.
2186e93f7393Sniklas 
2187e93f7393Sniklas    Unlike fprintf, this function does not return a value.
2188e93f7393Sniklas 
2189e93f7393Sniklas    We implement three variants, vfprintf (takes a vararg list and stream),
2190e93f7393Sniklas    fprintf (takes a stream to write on), and printf (the usual).
2191e93f7393Sniklas 
2192e93f7393Sniklas    Note also that a longjmp to top level may occur in this routine
2193e93f7393Sniklas    (since prompt_for_continue may do so) so this routine should not be
2194e93f7393Sniklas    called when cleanups are not in place.  */
2195e93f7393Sniklas 
2196e93f7393Sniklas static void
vfprintf_maybe_filtered(struct ui_file * stream,const char * format,va_list args,int filter)2197b725ae77Skettenis vfprintf_maybe_filtered (struct ui_file *stream, const char *format,
2198b725ae77Skettenis 			 va_list args, int filter)
2199e93f7393Sniklas {
2200e93f7393Sniklas   char *linebuffer;
2201e93f7393Sniklas   struct cleanup *old_cleanups;
2202e93f7393Sniklas 
220363addd46Skettenis   linebuffer = xstrvprintf (format, args);
2204b725ae77Skettenis   old_cleanups = make_cleanup (xfree, linebuffer);
2205e93f7393Sniklas   fputs_maybe_filtered (linebuffer, stream, filter);
2206e93f7393Sniklas   do_cleanups (old_cleanups);
2207e93f7393Sniklas }
2208e93f7393Sniklas 
2209e93f7393Sniklas 
2210e93f7393Sniklas void
vfprintf_filtered(struct ui_file * stream,const char * format,va_list args)2211b725ae77Skettenis vfprintf_filtered (struct ui_file *stream, const char *format, va_list args)
2212e93f7393Sniklas {
2213e93f7393Sniklas   vfprintf_maybe_filtered (stream, format, args, 1);
2214e93f7393Sniklas }
2215e93f7393Sniklas 
2216e93f7393Sniklas void
vfprintf_unfiltered(struct ui_file * stream,const char * format,va_list args)2217b725ae77Skettenis vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
2218e93f7393Sniklas {
2219e93f7393Sniklas   char *linebuffer;
2220e93f7393Sniklas   struct cleanup *old_cleanups;
2221e93f7393Sniklas 
222263addd46Skettenis   linebuffer = xstrvprintf (format, args);
2223b725ae77Skettenis   old_cleanups = make_cleanup (xfree, linebuffer);
2224e93f7393Sniklas   fputs_unfiltered (linebuffer, stream);
2225e93f7393Sniklas   do_cleanups (old_cleanups);
2226e93f7393Sniklas }
2227e93f7393Sniklas 
2228e93f7393Sniklas void
vprintf_filtered(const char * format,va_list args)2229b725ae77Skettenis vprintf_filtered (const char *format, va_list args)
2230e93f7393Sniklas {
2231e93f7393Sniklas   vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
2232e93f7393Sniklas }
2233e93f7393Sniklas 
2234e93f7393Sniklas void
vprintf_unfiltered(const char * format,va_list args)2235b725ae77Skettenis vprintf_unfiltered (const char *format, va_list args)
2236e93f7393Sniklas {
2237e93f7393Sniklas   vfprintf_unfiltered (gdb_stdout, format, args);
2238e93f7393Sniklas }
2239e93f7393Sniklas 
2240e93f7393Sniklas void
fprintf_filtered(struct ui_file * stream,const char * format,...)2241b725ae77Skettenis fprintf_filtered (struct ui_file *stream, const char *format, ...)
2242e93f7393Sniklas {
2243e93f7393Sniklas   va_list args;
2244e93f7393Sniklas   va_start (args, format);
2245e93f7393Sniklas   vfprintf_filtered (stream, format, args);
2246e93f7393Sniklas   va_end (args);
2247e93f7393Sniklas }
2248e93f7393Sniklas 
2249e93f7393Sniklas void
fprintf_unfiltered(struct ui_file * stream,const char * format,...)2250b725ae77Skettenis fprintf_unfiltered (struct ui_file *stream, const char *format, ...)
2251e93f7393Sniklas {
2252e93f7393Sniklas   va_list args;
2253e93f7393Sniklas   va_start (args, format);
2254e93f7393Sniklas   vfprintf_unfiltered (stream, format, args);
2255e93f7393Sniklas   va_end (args);
2256e93f7393Sniklas }
2257e93f7393Sniklas 
2258e93f7393Sniklas /* Like fprintf_filtered, but prints its result indented.
2259e93f7393Sniklas    Called as fprintfi_filtered (spaces, stream, format, ...);  */
2260e93f7393Sniklas 
2261e93f7393Sniklas void
fprintfi_filtered(int spaces,struct ui_file * stream,const char * format,...)2262b725ae77Skettenis fprintfi_filtered (int spaces, struct ui_file *stream, const char *format,
2263b725ae77Skettenis 		   ...)
2264e93f7393Sniklas {
2265e93f7393Sniklas   va_list args;
2266e93f7393Sniklas   va_start (args, format);
2267e93f7393Sniklas   print_spaces_filtered (spaces, stream);
2268e93f7393Sniklas 
2269e93f7393Sniklas   vfprintf_filtered (stream, format, args);
2270e93f7393Sniklas   va_end (args);
2271e93f7393Sniklas }
2272e93f7393Sniklas 
2273e93f7393Sniklas 
2274e93f7393Sniklas void
printf_filtered(const char * format,...)2275e93f7393Sniklas printf_filtered (const char *format, ...)
2276e93f7393Sniklas {
2277e93f7393Sniklas   va_list args;
2278e93f7393Sniklas   va_start (args, format);
2279e93f7393Sniklas   vfprintf_filtered (gdb_stdout, format, args);
2280e93f7393Sniklas   va_end (args);
2281e93f7393Sniklas }
2282e93f7393Sniklas 
2283e93f7393Sniklas 
2284e93f7393Sniklas void
printf_unfiltered(const char * format,...)2285e93f7393Sniklas printf_unfiltered (const char *format, ...)
2286e93f7393Sniklas {
2287e93f7393Sniklas   va_list args;
2288e93f7393Sniklas   va_start (args, format);
2289e93f7393Sniklas   vfprintf_unfiltered (gdb_stdout, format, args);
2290e93f7393Sniklas   va_end (args);
2291e93f7393Sniklas }
2292e93f7393Sniklas 
2293e93f7393Sniklas /* Like printf_filtered, but prints it's result indented.
2294e93f7393Sniklas    Called as printfi_filtered (spaces, format, ...);  */
2295e93f7393Sniklas 
2296e93f7393Sniklas void
printfi_filtered(int spaces,const char * format,...)2297e93f7393Sniklas printfi_filtered (int spaces, const char *format, ...)
2298e93f7393Sniklas {
2299e93f7393Sniklas   va_list args;
2300e93f7393Sniklas   va_start (args, format);
2301e93f7393Sniklas   print_spaces_filtered (spaces, gdb_stdout);
2302e93f7393Sniklas   vfprintf_filtered (gdb_stdout, format, args);
2303e93f7393Sniklas   va_end (args);
2304e93f7393Sniklas }
2305e93f7393Sniklas 
2306e93f7393Sniklas /* Easy -- but watch out!
2307e93f7393Sniklas 
2308e93f7393Sniklas    This routine is *not* a replacement for puts()!  puts() appends a newline.
2309e93f7393Sniklas    This one doesn't, and had better not!  */
2310e93f7393Sniklas 
2311e93f7393Sniklas void
puts_filtered(const char * string)2312b725ae77Skettenis puts_filtered (const char *string)
2313e93f7393Sniklas {
2314e93f7393Sniklas   fputs_filtered (string, gdb_stdout);
2315e93f7393Sniklas }
2316e93f7393Sniklas 
2317e93f7393Sniklas void
puts_unfiltered(const char * string)2318b725ae77Skettenis puts_unfiltered (const char *string)
2319e93f7393Sniklas {
2320e93f7393Sniklas   fputs_unfiltered (string, gdb_stdout);
2321e93f7393Sniklas }
2322e93f7393Sniklas 
2323e93f7393Sniklas /* Return a pointer to N spaces and a null.  The pointer is good
2324e93f7393Sniklas    until the next call to here.  */
2325e93f7393Sniklas char *
n_spaces(int n)2326b725ae77Skettenis n_spaces (int n)
2327e93f7393Sniklas {
2328b725ae77Skettenis   char *t;
2329b725ae77Skettenis   static char *spaces = 0;
2330b725ae77Skettenis   static int max_spaces = -1;
2331e93f7393Sniklas 
2332e93f7393Sniklas   if (n > max_spaces)
2333e93f7393Sniklas     {
2334e93f7393Sniklas       if (spaces)
2335b725ae77Skettenis 	xfree (spaces);
2336e93f7393Sniklas       spaces = (char *) xmalloc (n + 1);
2337e93f7393Sniklas       for (t = spaces + n; t != spaces;)
2338e93f7393Sniklas 	*--t = ' ';
2339e93f7393Sniklas       spaces[n] = '\0';
2340e93f7393Sniklas       max_spaces = n;
2341e93f7393Sniklas     }
2342e93f7393Sniklas 
2343e93f7393Sniklas   return spaces + max_spaces - n;
2344e93f7393Sniklas }
2345e93f7393Sniklas 
2346e93f7393Sniklas /* Print N spaces.  */
2347e93f7393Sniklas void
print_spaces_filtered(int n,struct ui_file * stream)2348b725ae77Skettenis print_spaces_filtered (int n, struct ui_file *stream)
2349e93f7393Sniklas {
2350e93f7393Sniklas   fputs_filtered (n_spaces (n), stream);
2351e93f7393Sniklas }
2352e93f7393Sniklas 
2353b725ae77Skettenis /* C++/ObjC demangler stuff.  */
2354e93f7393Sniklas 
2355e93f7393Sniklas /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
2356e93f7393Sniklas    LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
2357e93f7393Sniklas    If the name is not mangled, or the language for the name is unknown, or
2358e93f7393Sniklas    demangling is off, the name is printed in its "raw" form. */
2359e93f7393Sniklas 
2360e93f7393Sniklas void
fprintf_symbol_filtered(struct ui_file * stream,char * name,enum language lang,int arg_mode)2361b725ae77Skettenis fprintf_symbol_filtered (struct ui_file *stream, char *name,
2362b725ae77Skettenis 			 enum language lang, int arg_mode)
2363e93f7393Sniklas {
2364e93f7393Sniklas   char *demangled;
2365e93f7393Sniklas 
2366e93f7393Sniklas   if (name != NULL)
2367e93f7393Sniklas     {
2368e93f7393Sniklas       /* If user wants to see raw output, no problem.  */
2369e93f7393Sniklas       if (!demangle)
2370e93f7393Sniklas 	{
2371e93f7393Sniklas 	  fputs_filtered (name, stream);
2372e93f7393Sniklas 	}
2373e93f7393Sniklas       else
2374e93f7393Sniklas 	{
2375b725ae77Skettenis 	  demangled = language_demangle (language_def (lang), name, arg_mode);
2376e93f7393Sniklas 	  fputs_filtered (demangled ? demangled : name, stream);
2377e93f7393Sniklas 	  if (demangled != NULL)
2378e93f7393Sniklas 	    {
2379b725ae77Skettenis 	      xfree (demangled);
2380e93f7393Sniklas 	    }
2381e93f7393Sniklas 	}
2382e93f7393Sniklas     }
2383e93f7393Sniklas }
2384e93f7393Sniklas 
2385e93f7393Sniklas /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
2386e93f7393Sniklas    differences in whitespace.  Returns 0 if they match, non-zero if they
2387e93f7393Sniklas    don't (slightly different than strcmp()'s range of return values).
2388e93f7393Sniklas 
2389e93f7393Sniklas    As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2390e93f7393Sniklas    This "feature" is useful when searching for matching C++ function names
2391e93f7393Sniklas    (such as if the user types 'break FOO', where FOO is a mangled C++
2392e93f7393Sniklas    function). */
2393e93f7393Sniklas 
2394e93f7393Sniklas int
strcmp_iw(const char * string1,const char * string2)2395b725ae77Skettenis strcmp_iw (const char *string1, const char *string2)
2396e93f7393Sniklas {
2397e93f7393Sniklas   while ((*string1 != '\0') && (*string2 != '\0'))
2398e93f7393Sniklas     {
2399e93f7393Sniklas       while (isspace (*string1))
2400e93f7393Sniklas 	{
2401e93f7393Sniklas 	  string1++;
2402e93f7393Sniklas 	}
2403e93f7393Sniklas       while (isspace (*string2))
2404e93f7393Sniklas 	{
2405e93f7393Sniklas 	  string2++;
2406e93f7393Sniklas 	}
2407e93f7393Sniklas       if (*string1 != *string2)
2408e93f7393Sniklas 	{
2409e93f7393Sniklas 	  break;
2410e93f7393Sniklas 	}
2411e93f7393Sniklas       if (*string1 != '\0')
2412e93f7393Sniklas 	{
2413e93f7393Sniklas 	  string1++;
2414e93f7393Sniklas 	  string2++;
2415e93f7393Sniklas 	}
2416e93f7393Sniklas     }
2417e93f7393Sniklas   return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
2418e93f7393Sniklas }
2419e93f7393Sniklas 
2420b725ae77Skettenis /* This is like strcmp except that it ignores whitespace and treats
2421b725ae77Skettenis    '(' as the first non-NULL character in terms of ordering.  Like
2422b725ae77Skettenis    strcmp (and unlike strcmp_iw), it returns negative if STRING1 <
2423b725ae77Skettenis    STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2
2424b725ae77Skettenis    according to that ordering.
2425b725ae77Skettenis 
2426b725ae77Skettenis    If a list is sorted according to this function and if you want to
2427b725ae77Skettenis    find names in the list that match some fixed NAME according to
2428b725ae77Skettenis    strcmp_iw(LIST_ELT, NAME), then the place to start looking is right
2429b725ae77Skettenis    where this function would put NAME.
2430b725ae77Skettenis 
2431b725ae77Skettenis    Here are some examples of why using strcmp to sort is a bad idea:
2432b725ae77Skettenis 
2433b725ae77Skettenis    Whitespace example:
2434b725ae77Skettenis 
2435b725ae77Skettenis    Say your partial symtab contains: "foo<char *>", "goo".  Then, if
2436b725ae77Skettenis    we try to do a search for "foo<char*>", strcmp will locate this
2437b725ae77Skettenis    after "foo<char *>" and before "goo".  Then lookup_partial_symbol
2438b725ae77Skettenis    will start looking at strings beginning with "goo", and will never
2439b725ae77Skettenis    see the correct match of "foo<char *>".
2440b725ae77Skettenis 
2441b725ae77Skettenis    Parenthesis example:
2442b725ae77Skettenis 
2443b725ae77Skettenis    In practice, this is less like to be an issue, but I'll give it a
2444b725ae77Skettenis    shot.  Let's assume that '$' is a legitimate character to occur in
2445b725ae77Skettenis    symbols.  (Which may well even be the case on some systems.)  Then
2446b725ae77Skettenis    say that the partial symbol table contains "foo$" and "foo(int)".
2447b725ae77Skettenis    strcmp will put them in this order, since '$' < '('.  Now, if the
2448b725ae77Skettenis    user searches for "foo", then strcmp will sort "foo" before "foo$".
2449b725ae77Skettenis    Then lookup_partial_symbol will notice that strcmp_iw("foo$",
2450b725ae77Skettenis    "foo") is false, so it won't proceed to the actual match of
2451b725ae77Skettenis    "foo(int)" with "foo".  */
2452b725ae77Skettenis 
2453b725ae77Skettenis int
strcmp_iw_ordered(const char * string1,const char * string2)2454b725ae77Skettenis strcmp_iw_ordered (const char *string1, const char *string2)
2455b725ae77Skettenis {
2456b725ae77Skettenis   while ((*string1 != '\0') && (*string2 != '\0'))
2457b725ae77Skettenis     {
2458b725ae77Skettenis       while (isspace (*string1))
2459b725ae77Skettenis 	{
2460b725ae77Skettenis 	  string1++;
2461b725ae77Skettenis 	}
2462b725ae77Skettenis       while (isspace (*string2))
2463b725ae77Skettenis 	{
2464b725ae77Skettenis 	  string2++;
2465b725ae77Skettenis 	}
2466b725ae77Skettenis       if (*string1 != *string2)
2467b725ae77Skettenis 	{
2468b725ae77Skettenis 	  break;
2469b725ae77Skettenis 	}
2470b725ae77Skettenis       if (*string1 != '\0')
2471b725ae77Skettenis 	{
2472b725ae77Skettenis 	  string1++;
2473b725ae77Skettenis 	  string2++;
2474b725ae77Skettenis 	}
2475b725ae77Skettenis     }
2476b725ae77Skettenis 
2477b725ae77Skettenis   switch (*string1)
2478b725ae77Skettenis     {
2479b725ae77Skettenis       /* Characters are non-equal unless they're both '\0'; we want to
2480b725ae77Skettenis 	 make sure we get the comparison right according to our
2481b725ae77Skettenis 	 comparison in the cases where one of them is '\0' or '('.  */
2482b725ae77Skettenis     case '\0':
2483b725ae77Skettenis       if (*string2 == '\0')
2484b725ae77Skettenis 	return 0;
2485b725ae77Skettenis       else
2486b725ae77Skettenis 	return -1;
2487b725ae77Skettenis     case '(':
2488b725ae77Skettenis       if (*string2 == '\0')
2489b725ae77Skettenis 	return 1;
2490b725ae77Skettenis       else
2491b725ae77Skettenis 	return -1;
2492b725ae77Skettenis     default:
2493b725ae77Skettenis       if (*string2 == '(')
2494b725ae77Skettenis 	return 1;
2495b725ae77Skettenis       else
2496b725ae77Skettenis 	return *string1 - *string2;
2497b725ae77Skettenis     }
2498b725ae77Skettenis }
2499b725ae77Skettenis 
2500b725ae77Skettenis /* A simple comparison function with opposite semantics to strcmp.  */
2501b725ae77Skettenis 
2502b725ae77Skettenis int
streq(const char * lhs,const char * rhs)2503b725ae77Skettenis streq (const char *lhs, const char *rhs)
2504b725ae77Skettenis {
2505b725ae77Skettenis   return !strcmp (lhs, rhs);
2506b725ae77Skettenis }
2507e93f7393Sniklas 
2508b725ae77Skettenis 
2509b725ae77Skettenis /*
2510b725ae77Skettenis    ** subset_compare()
2511b725ae77Skettenis    **    Answer whether string_to_compare is a full or partial match to
2512b725ae77Skettenis    **    template_string.  The partial match must be in sequence starting
2513b725ae77Skettenis    **    at index 0.
2514b725ae77Skettenis  */
2515b725ae77Skettenis int
subset_compare(char * string_to_compare,char * template_string)2516b725ae77Skettenis subset_compare (char *string_to_compare, char *template_string)
2517b725ae77Skettenis {
2518b725ae77Skettenis   int match;
2519b725ae77Skettenis   if (template_string != (char *) NULL && string_to_compare != (char *) NULL
2520b725ae77Skettenis       && strlen (string_to_compare) <= strlen (template_string))
2521b725ae77Skettenis     match =
2522b725ae77Skettenis       (strncmp
2523b725ae77Skettenis        (template_string, string_to_compare, strlen (string_to_compare)) == 0);
2524b725ae77Skettenis   else
2525b725ae77Skettenis     match = 0;
2526b725ae77Skettenis   return match;
2527b725ae77Skettenis }
2528b725ae77Skettenis 
2529b725ae77Skettenis 
2530b725ae77Skettenis static void pagination_on_command (char *arg, int from_tty);
2531b725ae77Skettenis static void
pagination_on_command(char * arg,int from_tty)2532b725ae77Skettenis pagination_on_command (char *arg, int from_tty)
2533b725ae77Skettenis {
2534b725ae77Skettenis   pagination_enabled = 1;
2535b725ae77Skettenis }
2536b725ae77Skettenis 
2537b725ae77Skettenis static void pagination_on_command (char *arg, int from_tty);
2538b725ae77Skettenis static void
pagination_off_command(char * arg,int from_tty)2539b725ae77Skettenis pagination_off_command (char *arg, int from_tty)
2540b725ae77Skettenis {
2541b725ae77Skettenis   pagination_enabled = 0;
2542b725ae77Skettenis }
2543b725ae77Skettenis 
2544b725ae77Skettenis 
2545e93f7393Sniklas void
initialize_utils(void)2546b725ae77Skettenis initialize_utils (void)
2547e93f7393Sniklas {
2548e93f7393Sniklas   struct cmd_list_element *c;
2549e93f7393Sniklas 
2550b725ae77Skettenis   c = add_set_cmd ("width", class_support, var_uinteger, &chars_per_line,
2551e93f7393Sniklas 		   "Set number of characters gdb thinks are in a line.",
2552e93f7393Sniklas 		   &setlist);
255363addd46Skettenis   deprecated_add_show_from_set (c, &showlist);
2554b725ae77Skettenis   set_cmd_sfunc (c, set_width_command);
2555e93f7393Sniklas 
2556b725ae77Skettenis   c = add_set_cmd ("height", class_support, var_uinteger, &lines_per_page,
2557b725ae77Skettenis 		   "Set number of lines gdb thinks are in a page.", &setlist);
255863addd46Skettenis   deprecated_add_show_from_set (c, &showlist);
2559b725ae77Skettenis   set_cmd_sfunc (c, set_height_command);
2560e93f7393Sniklas 
2561b725ae77Skettenis   init_page_info ();
2562e93f7393Sniklas 
256363addd46Skettenis   deprecated_add_show_from_set
2564e93f7393Sniklas     (add_set_cmd ("demangle", class_support, var_boolean,
2565e93f7393Sniklas 		  (char *) &demangle,
2566b725ae77Skettenis 		  "Set demangling of encoded C++/ObjC names when displaying symbols.",
2567b725ae77Skettenis 		  &setprintlist), &showprintlist);
2568b725ae77Skettenis 
256963addd46Skettenis   deprecated_add_show_from_set
2570b725ae77Skettenis     (add_set_cmd ("pagination", class_support,
2571b725ae77Skettenis 		  var_boolean, (char *) &pagination_enabled,
2572b725ae77Skettenis 		  "Set state of pagination.", &setlist), &showlist);
2573b725ae77Skettenis 
2574b725ae77Skettenis   if (xdb_commands)
2575b725ae77Skettenis     {
2576b725ae77Skettenis       add_com ("am", class_support, pagination_on_command,
2577b725ae77Skettenis 	       "Enable pagination");
2578b725ae77Skettenis       add_com ("sm", class_support, pagination_off_command,
2579b725ae77Skettenis 	       "Disable pagination");
2580b725ae77Skettenis     }
2581e93f7393Sniklas 
258263addd46Skettenis   deprecated_add_show_from_set
2583e93f7393Sniklas     (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
2584e93f7393Sniklas 		  (char *) &sevenbit_strings,
2585e93f7393Sniklas 		  "Set printing of 8-bit characters in strings as \\nnn.",
2586b725ae77Skettenis 		  &setprintlist), &showprintlist);
2587e93f7393Sniklas 
258863addd46Skettenis   deprecated_add_show_from_set
2589e93f7393Sniklas     (add_set_cmd ("asm-demangle", class_support, var_boolean,
2590e93f7393Sniklas 		  (char *) &asm_demangle,
2591b725ae77Skettenis 		  "Set demangling of C++/ObjC names in disassembly listings.",
2592b725ae77Skettenis 		  &setprintlist), &showprintlist);
2593e93f7393Sniklas }
2594e93f7393Sniklas 
2595e93f7393Sniklas /* Machine specific function to handle SIGWINCH signal. */
2596e93f7393Sniklas 
2597e93f7393Sniklas #ifdef  SIGWINCH_HANDLER_BODY
2598e93f7393Sniklas SIGWINCH_HANDLER_BODY
2599e93f7393Sniklas #endif
2600b725ae77Skettenis /* print routines to handle variable size regs, etc. */
2601e93f7393Sniklas /* temporary storage using circular buffer */
2602e93f7393Sniklas #define NUMCELLS 16
260363addd46Skettenis #define CELLSIZE 50
2604e93f7393Sniklas static char *
get_cell(void)2605b725ae77Skettenis get_cell (void)
2606e93f7393Sniklas {
2607e93f7393Sniklas   static char buf[NUMCELLS][CELLSIZE];
2608e93f7393Sniklas   static int cell = 0;
2609b725ae77Skettenis   if (++cell >= NUMCELLS)
2610b725ae77Skettenis     cell = 0;
2611e93f7393Sniklas   return buf[cell];
2612e93f7393Sniklas }
2613e93f7393Sniklas 
2614b725ae77Skettenis int
strlen_paddr(void)2615b725ae77Skettenis strlen_paddr (void)
2616b725ae77Skettenis {
2617b725ae77Skettenis   return (TARGET_ADDR_BIT / 8 * 2);
2618b725ae77Skettenis }
2619b725ae77Skettenis 
2620e93f7393Sniklas char *
paddr(CORE_ADDR addr)2621b725ae77Skettenis paddr (CORE_ADDR addr)
2622e93f7393Sniklas {
2623b725ae77Skettenis   return phex (addr, TARGET_ADDR_BIT / 8);
2624b725ae77Skettenis }
2625b725ae77Skettenis 
2626b725ae77Skettenis char *
paddr_nz(CORE_ADDR addr)2627b725ae77Skettenis paddr_nz (CORE_ADDR addr)
2628e93f7393Sniklas {
2629b725ae77Skettenis   return phex_nz (addr, TARGET_ADDR_BIT / 8);
2630b725ae77Skettenis }
2631b725ae77Skettenis 
2632b725ae77Skettenis static void
decimal2str(char * paddr_str,char * sign,ULONGEST addr,int width)263363addd46Skettenis decimal2str (char *paddr_str, char *sign, ULONGEST addr, int width)
2634b725ae77Skettenis {
2635b725ae77Skettenis   /* steal code from valprint.c:print_decimal().  Should this worry
2636b725ae77Skettenis      about the real size of addr as the above does? */
2637b725ae77Skettenis   unsigned long temp[3];
2638b725ae77Skettenis   int i = 0;
2639b725ae77Skettenis   do
2640b725ae77Skettenis     {
2641b725ae77Skettenis       temp[i] = addr % (1000 * 1000 * 1000);
2642b725ae77Skettenis       addr /= (1000 * 1000 * 1000);
2643b725ae77Skettenis       i++;
264463addd46Skettenis       width -= 9;
2645b725ae77Skettenis     }
2646b725ae77Skettenis   while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
264763addd46Skettenis   width += 9;
264863addd46Skettenis   if (width < 0)
264963addd46Skettenis     width = 0;
2650b725ae77Skettenis   switch (i)
2651b725ae77Skettenis     {
2652b725ae77Skettenis     case 1:
265363addd46Skettenis       sprintf (paddr_str, "%s%0*lu", sign, width, temp[0]);
2654e93f7393Sniklas       break;
2655e93f7393Sniklas     case 2:
265663addd46Skettenis       sprintf (paddr_str, "%s%0*lu%09lu", sign, width, temp[1], temp[0]);
2657b725ae77Skettenis       break;
2658b725ae77Skettenis     case 3:
265963addd46Skettenis       sprintf (paddr_str, "%s%0*lu%09lu%09lu", sign, width,
266063addd46Skettenis 	       temp[2], temp[1], temp[0]);
266163addd46Skettenis       break;
266263addd46Skettenis     default:
266363addd46Skettenis       internal_error (__FILE__, __LINE__,
266463addd46Skettenis 		      "failed internal consistency check");
266563addd46Skettenis     }
266663addd46Skettenis }
266763addd46Skettenis 
266863addd46Skettenis static void
octal2str(char * paddr_str,ULONGEST addr,int width)266963addd46Skettenis octal2str (char *paddr_str, ULONGEST addr, int width)
267063addd46Skettenis {
267163addd46Skettenis   unsigned long temp[3];
267263addd46Skettenis   int i = 0;
267363addd46Skettenis   do
267463addd46Skettenis     {
267563addd46Skettenis       temp[i] = addr % (0100000 * 0100000);
267663addd46Skettenis       addr /= (0100000 * 0100000);
267763addd46Skettenis       i++;
267863addd46Skettenis       width -= 10;
267963addd46Skettenis     }
268063addd46Skettenis   while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
268163addd46Skettenis   width += 10;
268263addd46Skettenis   if (width < 0)
268363addd46Skettenis     width = 0;
268463addd46Skettenis   switch (i)
268563addd46Skettenis     {
268663addd46Skettenis     case 1:
268763addd46Skettenis       if (temp[0] == 0)
268863addd46Skettenis 	sprintf (paddr_str, "%*o", width, 0);
268963addd46Skettenis       else
269063addd46Skettenis 	sprintf (paddr_str, "0%0*lo", width, temp[0]);
269163addd46Skettenis       break;
269263addd46Skettenis     case 2:
269363addd46Skettenis       sprintf (paddr_str, "0%0*lo%010lo", width, temp[1], temp[0]);
269463addd46Skettenis       break;
269563addd46Skettenis     case 3:
269663addd46Skettenis       sprintf (paddr_str, "0%0*lo%010lo%010lo", width,
269763addd46Skettenis 	       temp[2], temp[1], temp[0]);
2698e93f7393Sniklas       break;
2699e93f7393Sniklas     default:
2700b725ae77Skettenis       internal_error (__FILE__, __LINE__,
2701b725ae77Skettenis 		      "failed internal consistency check");
2702e93f7393Sniklas     }
2703b725ae77Skettenis }
2704b725ae77Skettenis 
2705b725ae77Skettenis char *
paddr_u(CORE_ADDR addr)2706b725ae77Skettenis paddr_u (CORE_ADDR addr)
2707b725ae77Skettenis {
2708b725ae77Skettenis   char *paddr_str = get_cell ();
270963addd46Skettenis   decimal2str (paddr_str, "", addr, 0);
2710e93f7393Sniklas   return paddr_str;
2711e93f7393Sniklas }
2712e93f7393Sniklas 
2713e93f7393Sniklas char *
paddr_d(LONGEST addr)2714b725ae77Skettenis paddr_d (LONGEST addr)
2715e93f7393Sniklas {
2716b725ae77Skettenis   char *paddr_str = get_cell ();
2717b725ae77Skettenis   if (addr < 0)
271863addd46Skettenis     decimal2str (paddr_str, "-", -addr, 0);
2719b725ae77Skettenis   else
272063addd46Skettenis     decimal2str (paddr_str, "", addr, 0);
2721b725ae77Skettenis   return paddr_str;
2722e93f7393Sniklas }
2723e93f7393Sniklas 
2724b725ae77Skettenis /* eliminate warning from compiler on 32-bit systems */
2725b725ae77Skettenis static int thirty_two = 32;
2726b725ae77Skettenis 
2727b725ae77Skettenis char *
phex(ULONGEST l,int sizeof_l)2728b725ae77Skettenis phex (ULONGEST l, int sizeof_l)
2729b725ae77Skettenis {
2730b725ae77Skettenis   char *str;
2731b725ae77Skettenis   switch (sizeof_l)
2732b725ae77Skettenis     {
2733b725ae77Skettenis     case 8:
2734b725ae77Skettenis       str = get_cell ();
2735b725ae77Skettenis       sprintf (str, "%08lx%08lx",
2736b725ae77Skettenis 	       (unsigned long) (l >> thirty_two),
2737b725ae77Skettenis 	       (unsigned long) (l & 0xffffffff));
2738b725ae77Skettenis       break;
2739b725ae77Skettenis     case 4:
2740b725ae77Skettenis       str = get_cell ();
2741b725ae77Skettenis       sprintf (str, "%08lx", (unsigned long) l);
2742b725ae77Skettenis       break;
2743b725ae77Skettenis     case 2:
2744b725ae77Skettenis       str = get_cell ();
2745b725ae77Skettenis       sprintf (str, "%04x", (unsigned short) (l & 0xffff));
2746b725ae77Skettenis       break;
2747b725ae77Skettenis     default:
2748b725ae77Skettenis       str = phex (l, sizeof (l));
2749b725ae77Skettenis       break;
2750b725ae77Skettenis     }
2751b725ae77Skettenis   return str;
2752b725ae77Skettenis }
2753b725ae77Skettenis 
2754b725ae77Skettenis char *
phex_nz(ULONGEST l,int sizeof_l)2755b725ae77Skettenis phex_nz (ULONGEST l, int sizeof_l)
2756b725ae77Skettenis {
2757b725ae77Skettenis   char *str;
2758b725ae77Skettenis   switch (sizeof_l)
2759b725ae77Skettenis     {
2760b725ae77Skettenis     case 8:
2761b725ae77Skettenis       {
2762b725ae77Skettenis 	unsigned long high = (unsigned long) (l >> thirty_two);
2763b725ae77Skettenis 	str = get_cell ();
2764b725ae77Skettenis 	if (high == 0)
2765b725ae77Skettenis 	  sprintf (str, "%lx", (unsigned long) (l & 0xffffffff));
2766b725ae77Skettenis 	else
2767b725ae77Skettenis 	  sprintf (str, "%lx%08lx", high, (unsigned long) (l & 0xffffffff));
2768b725ae77Skettenis 	break;
2769b725ae77Skettenis       }
2770b725ae77Skettenis     case 4:
2771b725ae77Skettenis       str = get_cell ();
2772b725ae77Skettenis       sprintf (str, "%lx", (unsigned long) l);
2773b725ae77Skettenis       break;
2774b725ae77Skettenis     case 2:
2775b725ae77Skettenis       str = get_cell ();
2776b725ae77Skettenis       sprintf (str, "%x", (unsigned short) (l & 0xffff));
2777b725ae77Skettenis       break;
2778b725ae77Skettenis     default:
2779b725ae77Skettenis       str = phex_nz (l, sizeof (l));
2780b725ae77Skettenis       break;
2781b725ae77Skettenis     }
2782b725ae77Skettenis   return str;
2783b725ae77Skettenis }
2784b725ae77Skettenis 
278563addd46Skettenis /* Converts a LONGEST to a C-format hexadecimal literal and stores it
278663addd46Skettenis    in a static string.  Returns a pointer to this string.  */
278763addd46Skettenis char *
hex_string(LONGEST num)278863addd46Skettenis hex_string (LONGEST num)
278963addd46Skettenis {
279063addd46Skettenis   char *result = get_cell ();
279163addd46Skettenis   snprintf (result, CELLSIZE, "0x%s", phex_nz (num, sizeof (num)));
279263addd46Skettenis   return result;
279363addd46Skettenis }
279463addd46Skettenis 
279563addd46Skettenis /* Converts a LONGEST number to a C-format hexadecimal literal and
279663addd46Skettenis    stores it in a static string.  Returns a pointer to this string
279763addd46Skettenis    that is valid until the next call.  The number is padded on the
279863addd46Skettenis    left with 0s to at least WIDTH characters.  */
279963addd46Skettenis char *
hex_string_custom(LONGEST num,int width)280063addd46Skettenis hex_string_custom (LONGEST num, int width)
280163addd46Skettenis {
280263addd46Skettenis   char *result = get_cell ();
280363addd46Skettenis   char *result_end = result + CELLSIZE - 1;
280463addd46Skettenis   const char *hex = phex_nz (num, sizeof (num));
280563addd46Skettenis   int hex_len = strlen (hex);
280663addd46Skettenis 
280763addd46Skettenis   if (hex_len > width)
280863addd46Skettenis     width = hex_len;
280963addd46Skettenis   if (width + 2 >= CELLSIZE)
281063addd46Skettenis     internal_error (__FILE__, __LINE__,
281163addd46Skettenis 		    "hex_string_custom: insufficient space to store result");
281263addd46Skettenis 
281363addd46Skettenis   strcpy (result_end - width - 2, "0x");
281463addd46Skettenis   memset (result_end - width, '0', width);
281563addd46Skettenis   strcpy (result_end - hex_len, hex);
281663addd46Skettenis   return result_end - width - 2;
281763addd46Skettenis }
281863addd46Skettenis 
281963addd46Skettenis /* Convert VAL to a numeral in the given radix.  For
282063addd46Skettenis  * radix 10, IS_SIGNED may be true, indicating a signed quantity;
282163addd46Skettenis  * otherwise VAL is interpreted as unsigned.  If WIDTH is supplied,
282263addd46Skettenis  * it is the minimum width (0-padded if needed).  USE_C_FORMAT means
282363addd46Skettenis  * to use C format in all cases.  If it is false, then 'x'
282463addd46Skettenis  * and 'o' formats do not include a prefix (0x or leading 0). */
282563addd46Skettenis 
282663addd46Skettenis char *
int_string(LONGEST val,int radix,int is_signed,int width,int use_c_format)282763addd46Skettenis int_string (LONGEST val, int radix, int is_signed, int width,
282863addd46Skettenis 	    int use_c_format)
282963addd46Skettenis {
283063addd46Skettenis   switch (radix)
283163addd46Skettenis     {
283263addd46Skettenis     case 16:
283363addd46Skettenis       {
283463addd46Skettenis 	char *result;
283563addd46Skettenis 	if (width == 0)
283663addd46Skettenis 	  result = hex_string (val);
283763addd46Skettenis 	else
283863addd46Skettenis 	  result = hex_string_custom (val, width);
283963addd46Skettenis 	if (! use_c_format)
284063addd46Skettenis 	  result += 2;
284163addd46Skettenis 	return result;
284263addd46Skettenis       }
284363addd46Skettenis     case 10:
284463addd46Skettenis       {
284563addd46Skettenis 	char *result = get_cell ();
284663addd46Skettenis 	if (is_signed && val < 0)
284763addd46Skettenis 	  decimal2str (result, "-", -val, width);
284863addd46Skettenis 	else
284963addd46Skettenis 	  decimal2str (result, "", val, width);
285063addd46Skettenis 	return result;
285163addd46Skettenis       }
285263addd46Skettenis     case 8:
285363addd46Skettenis       {
285463addd46Skettenis 	char *result = get_cell ();
285563addd46Skettenis 	octal2str (result, val, width);
285663addd46Skettenis 	if (use_c_format || val == 0)
285763addd46Skettenis 	  return result;
285863addd46Skettenis 	else
285963addd46Skettenis 	  return result + 1;
286063addd46Skettenis       }
286163addd46Skettenis     default:
286263addd46Skettenis       internal_error (__FILE__, __LINE__,
286363addd46Skettenis 		      "failed internal consistency check");
286463addd46Skettenis     }
286563addd46Skettenis }
2866b725ae77Skettenis 
2867b725ae77Skettenis /* Convert a CORE_ADDR into a string.  */
2868b725ae77Skettenis const char *
core_addr_to_string(const CORE_ADDR addr)2869b725ae77Skettenis core_addr_to_string (const CORE_ADDR addr)
2870b725ae77Skettenis {
2871b725ae77Skettenis   char *str = get_cell ();
2872b725ae77Skettenis   strcpy (str, "0x");
2873b725ae77Skettenis   strcat (str, phex (addr, sizeof (addr)));
2874b725ae77Skettenis   return str;
2875b725ae77Skettenis }
2876b725ae77Skettenis 
2877b725ae77Skettenis const char *
core_addr_to_string_nz(const CORE_ADDR addr)2878b725ae77Skettenis core_addr_to_string_nz (const CORE_ADDR addr)
2879b725ae77Skettenis {
2880b725ae77Skettenis   char *str = get_cell ();
2881b725ae77Skettenis   strcpy (str, "0x");
2882b725ae77Skettenis   strcat (str, phex_nz (addr, sizeof (addr)));
2883b725ae77Skettenis   return str;
2884b725ae77Skettenis }
2885b725ae77Skettenis 
2886b725ae77Skettenis /* Convert a string back into a CORE_ADDR.  */
2887b725ae77Skettenis CORE_ADDR
string_to_core_addr(const char * my_string)2888b725ae77Skettenis string_to_core_addr (const char *my_string)
2889b725ae77Skettenis {
2890b725ae77Skettenis   CORE_ADDR addr = 0;
2891b725ae77Skettenis   if (my_string[0] == '0' && tolower (my_string[1]) == 'x')
2892b725ae77Skettenis     {
2893b725ae77Skettenis       /* Assume that it is in decimal.  */
2894b725ae77Skettenis       int i;
2895b725ae77Skettenis       for (i = 2; my_string[i] != '\0'; i++)
2896b725ae77Skettenis 	{
2897b725ae77Skettenis 	  if (isdigit (my_string[i]))
2898b725ae77Skettenis 	    addr = (my_string[i] - '0') + (addr * 16);
2899b725ae77Skettenis 	  else if (isxdigit (my_string[i]))
2900b725ae77Skettenis 	    addr = (tolower (my_string[i]) - 'a' + 0xa) + (addr * 16);
2901b725ae77Skettenis 	  else
2902b725ae77Skettenis 	    internal_error (__FILE__, __LINE__, "invalid hex");
2903b725ae77Skettenis 	}
2904b725ae77Skettenis     }
2905b725ae77Skettenis   else
2906b725ae77Skettenis     {
2907b725ae77Skettenis       /* Assume that it is in decimal.  */
2908b725ae77Skettenis       int i;
2909b725ae77Skettenis       for (i = 0; my_string[i] != '\0'; i++)
2910b725ae77Skettenis 	{
2911b725ae77Skettenis 	  if (isdigit (my_string[i]))
2912b725ae77Skettenis 	    addr = (my_string[i] - '0') + (addr * 10);
2913b725ae77Skettenis 	  else
2914b725ae77Skettenis 	    internal_error (__FILE__, __LINE__, "invalid decimal");
2915b725ae77Skettenis 	}
2916b725ae77Skettenis     }
2917b725ae77Skettenis   return addr;
2918b725ae77Skettenis }
2919b725ae77Skettenis 
2920b725ae77Skettenis char *
gdb_realpath(const char * filename)2921b725ae77Skettenis gdb_realpath (const char *filename)
2922b725ae77Skettenis {
2923b725ae77Skettenis   /* Method 1: The system has a compile time upper bound on a filename
2924b725ae77Skettenis      path.  Use that and realpath() to canonicalize the name.  This is
2925b725ae77Skettenis      the most common case.  Note that, if there isn't a compile time
2926b725ae77Skettenis      upper bound, you want to avoid realpath() at all costs.  */
2927b725ae77Skettenis #if defined(HAVE_REALPATH)
2928b725ae77Skettenis   {
2929b725ae77Skettenis # if defined (PATH_MAX)
2930b725ae77Skettenis     char buf[PATH_MAX];
2931b725ae77Skettenis #  define USE_REALPATH
2932b725ae77Skettenis # elif defined (MAXPATHLEN)
2933b725ae77Skettenis     char buf[MAXPATHLEN];
2934b725ae77Skettenis #  define USE_REALPATH
2935b725ae77Skettenis # endif
2936b725ae77Skettenis # if defined (USE_REALPATH)
2937b725ae77Skettenis     const char *rp = realpath (filename, buf);
2938b725ae77Skettenis     if (rp == NULL)
2939b725ae77Skettenis       rp = filename;
2940b725ae77Skettenis     return xstrdup (rp);
2941b725ae77Skettenis # endif
2942b725ae77Skettenis   }
2943b725ae77Skettenis #endif /* HAVE_REALPATH */
2944b725ae77Skettenis 
2945b725ae77Skettenis   /* Method 2: The host system (i.e., GNU) has the function
2946b725ae77Skettenis      canonicalize_file_name() which malloc's a chunk of memory and
2947b725ae77Skettenis      returns that, use that.  */
2948b725ae77Skettenis #if defined(HAVE_CANONICALIZE_FILE_NAME)
2949b725ae77Skettenis   {
2950b725ae77Skettenis     char *rp = canonicalize_file_name (filename);
2951b725ae77Skettenis     if (rp == NULL)
2952b725ae77Skettenis       return xstrdup (filename);
2953b725ae77Skettenis     else
2954b725ae77Skettenis       return rp;
2955b725ae77Skettenis   }
2956b725ae77Skettenis #endif
2957b725ae77Skettenis 
2958b725ae77Skettenis   /* FIXME: cagney/2002-11-13:
2959b725ae77Skettenis 
2960b725ae77Skettenis      Method 2a: Use realpath() with a NULL buffer.  Some systems, due
2961b725ae77Skettenis      to the problems described in in method 3, have modified their
2962b725ae77Skettenis      realpath() implementation so that it will allocate a buffer when
2963b725ae77Skettenis      NULL is passed in.  Before this can be used, though, some sort of
2964b725ae77Skettenis      configure time test would need to be added.  Otherwize the code
2965b725ae77Skettenis      will likely core dump.  */
2966b725ae77Skettenis 
2967b725ae77Skettenis   /* Method 3: Now we're getting desperate!  The system doesn't have a
2968b725ae77Skettenis      compile time buffer size and no alternative function.  Query the
2969b725ae77Skettenis      OS, using pathconf(), for the buffer limit.  Care is needed
2970b725ae77Skettenis      though, some systems do not limit PATH_MAX (return -1 for
2971b725ae77Skettenis      pathconf()) making it impossible to pass a correctly sized buffer
2972b725ae77Skettenis      to realpath() (it could always overflow).  On those systems, we
2973b725ae77Skettenis      skip this.  */
2974b725ae77Skettenis #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H) && defined(HAVE_ALLOCA)
2975b725ae77Skettenis   {
2976b725ae77Skettenis     /* Find out the max path size.  */
2977b725ae77Skettenis     long path_max = pathconf ("/", _PC_PATH_MAX);
2978b725ae77Skettenis     if (path_max > 0)
2979b725ae77Skettenis       {
2980b725ae77Skettenis 	/* PATH_MAX is bounded.  */
2981b725ae77Skettenis 	char *buf = alloca (path_max);
2982b725ae77Skettenis 	char *rp = realpath (filename, buf);
2983b725ae77Skettenis 	return xstrdup (rp ? rp : filename);
2984b725ae77Skettenis       }
2985b725ae77Skettenis   }
2986b725ae77Skettenis #endif
2987b725ae77Skettenis 
2988b725ae77Skettenis   /* This system is a lost cause, just dup the buffer.  */
2989b725ae77Skettenis   return xstrdup (filename);
2990b725ae77Skettenis }
2991b725ae77Skettenis 
2992b725ae77Skettenis /* Return a copy of FILENAME, with its directory prefix canonicalized
2993b725ae77Skettenis    by gdb_realpath.  */
2994b725ae77Skettenis 
2995b725ae77Skettenis char *
xfullpath(const char * filename)2996b725ae77Skettenis xfullpath (const char *filename)
2997b725ae77Skettenis {
2998b725ae77Skettenis   const char *base_name = lbasename (filename);
2999b725ae77Skettenis   char *dir_name;
3000b725ae77Skettenis   char *real_path;
3001b725ae77Skettenis   char *result;
3002b725ae77Skettenis 
3003b725ae77Skettenis   /* Extract the basename of filename, and return immediately
3004b725ae77Skettenis      a copy of filename if it does not contain any directory prefix. */
3005b725ae77Skettenis   if (base_name == filename)
3006b725ae77Skettenis     return xstrdup (filename);
3007b725ae77Skettenis 
3008b725ae77Skettenis   dir_name = alloca ((size_t) (base_name - filename + 2));
3009b725ae77Skettenis   /* Allocate enough space to store the dir_name + plus one extra
3010b725ae77Skettenis      character sometimes needed under Windows (see below), and
3011b725ae77Skettenis      then the closing \000 character */
3012b725ae77Skettenis   strncpy (dir_name, filename, base_name - filename);
3013b725ae77Skettenis   dir_name[base_name - filename] = '\000';
3014b725ae77Skettenis 
3015b725ae77Skettenis #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3016b725ae77Skettenis   /* We need to be careful when filename is of the form 'd:foo', which
3017b725ae77Skettenis      is equivalent of d:./foo, which is totally different from d:/foo.  */
3018b725ae77Skettenis   if (strlen (dir_name) == 2 && isalpha (dir_name[0]) && dir_name[1] == ':')
3019b725ae77Skettenis     {
3020b725ae77Skettenis       dir_name[2] = '.';
3021b725ae77Skettenis       dir_name[3] = '\000';
3022b725ae77Skettenis     }
3023b725ae77Skettenis #endif
3024b725ae77Skettenis 
3025b725ae77Skettenis   /* Canonicalize the directory prefix, and build the resulting
3026b725ae77Skettenis      filename. If the dirname realpath already contains an ending
3027b725ae77Skettenis      directory separator, avoid doubling it.  */
3028b725ae77Skettenis   real_path = gdb_realpath (dir_name);
3029b725ae77Skettenis   if (IS_DIR_SEPARATOR (real_path[strlen (real_path) - 1]))
3030b725ae77Skettenis     result = concat (real_path, base_name, NULL);
3031b725ae77Skettenis   else
3032b725ae77Skettenis     result = concat (real_path, SLASH_STRING, base_name, NULL);
3033b725ae77Skettenis 
3034b725ae77Skettenis   xfree (real_path);
3035b725ae77Skettenis   return result;
3036b725ae77Skettenis }
3037b725ae77Skettenis 
3038b725ae77Skettenis 
3039b725ae77Skettenis /* This is the 32-bit CRC function used by the GNU separate debug
3040b725ae77Skettenis    facility.  An executable may contain a section named
3041b725ae77Skettenis    .gnu_debuglink, which holds the name of a separate executable file
3042b725ae77Skettenis    containing its debug info, and a checksum of that file's contents,
3043b725ae77Skettenis    computed using this function.  */
3044b725ae77Skettenis unsigned long
gnu_debuglink_crc32(unsigned long crc,unsigned char * buf,size_t len)3045b725ae77Skettenis gnu_debuglink_crc32 (unsigned long crc, unsigned char *buf, size_t len)
3046b725ae77Skettenis {
3047b725ae77Skettenis   static const unsigned long crc32_table[256] = {
3048b725ae77Skettenis     0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
3049b725ae77Skettenis     0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
3050b725ae77Skettenis     0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
3051b725ae77Skettenis     0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
3052b725ae77Skettenis     0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
3053b725ae77Skettenis     0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
3054b725ae77Skettenis     0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
3055b725ae77Skettenis     0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
3056b725ae77Skettenis     0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
3057b725ae77Skettenis     0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
3058b725ae77Skettenis     0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
3059b725ae77Skettenis     0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
3060b725ae77Skettenis     0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
3061b725ae77Skettenis     0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
3062b725ae77Skettenis     0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
3063b725ae77Skettenis     0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
3064b725ae77Skettenis     0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
3065b725ae77Skettenis     0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
3066b725ae77Skettenis     0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
3067b725ae77Skettenis     0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
3068b725ae77Skettenis     0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
3069b725ae77Skettenis     0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
3070b725ae77Skettenis     0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
3071b725ae77Skettenis     0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
3072b725ae77Skettenis     0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
3073b725ae77Skettenis     0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
3074b725ae77Skettenis     0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
3075b725ae77Skettenis     0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
3076b725ae77Skettenis     0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
3077b725ae77Skettenis     0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
3078b725ae77Skettenis     0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
3079b725ae77Skettenis     0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
3080b725ae77Skettenis     0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
3081b725ae77Skettenis     0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
3082b725ae77Skettenis     0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
3083b725ae77Skettenis     0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
3084b725ae77Skettenis     0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
3085b725ae77Skettenis     0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
3086b725ae77Skettenis     0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
3087b725ae77Skettenis     0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
3088b725ae77Skettenis     0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
3089b725ae77Skettenis     0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
3090b725ae77Skettenis     0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
3091b725ae77Skettenis     0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
3092b725ae77Skettenis     0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
3093b725ae77Skettenis     0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
3094b725ae77Skettenis     0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
3095b725ae77Skettenis     0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
3096b725ae77Skettenis     0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
3097b725ae77Skettenis     0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
3098b725ae77Skettenis     0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
3099b725ae77Skettenis     0x2d02ef8d
3100b725ae77Skettenis   };
3101b725ae77Skettenis   unsigned char *end;
3102b725ae77Skettenis 
3103b725ae77Skettenis   crc = ~crc & 0xffffffff;
3104b725ae77Skettenis   for (end = buf + len; buf < end; ++buf)
3105b725ae77Skettenis     crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
3106b725ae77Skettenis   return ~crc & 0xffffffff;;
3107b725ae77Skettenis }
3108b725ae77Skettenis 
3109b725ae77Skettenis ULONGEST
align_up(ULONGEST v,int n)3110b725ae77Skettenis align_up (ULONGEST v, int n)
3111b725ae77Skettenis {
3112b725ae77Skettenis   /* Check that N is really a power of two.  */
3113b725ae77Skettenis   gdb_assert (n && (n & (n-1)) == 0);
3114b725ae77Skettenis   return (v + n - 1) & -n;
3115b725ae77Skettenis }
3116b725ae77Skettenis 
3117b725ae77Skettenis ULONGEST
align_down(ULONGEST v,int n)3118b725ae77Skettenis align_down (ULONGEST v, int n)
3119b725ae77Skettenis {
3120b725ae77Skettenis   /* Check that N is really a power of two.  */
3121b725ae77Skettenis   gdb_assert (n && (n & (n-1)) == 0);
3122b725ae77Skettenis   return (v & -n);
3123b725ae77Skettenis }
3124