2002-12-20 David Carlton <carlton@math.stanford.edu>
[deliverable/binutils-gdb.git] / gdb / utils.c
CommitLineData
c906108c 1/* General utility routines for GDB, the GNU debugger.
a752853e
AC
2 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002
d9fcf2fb 4 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c 22
4e8f7a8b
DJ
23#include "defs.h"
24#include "gdb_assert.h"
25#include <ctype.h>
26#include "gdb_string.h"
27#include "event-top.h"
28
9d271fd8
AC
29#ifdef __GO32__
30#include <pc.h>
31#endif
32
c906108c
SS
33/* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */
34#ifdef reg
35#undef reg
36#endif
37
042be3a9 38#include <signal.h>
c906108c
SS
39#include "gdbcmd.h"
40#include "serial.h"
41#include "bfd.h"
42#include "target.h"
43#include "demangle.h"
44#include "expression.h"
45#include "language.h"
234b45d4 46#include "charset.h"
c906108c 47#include "annotate.h"
303c8ebd 48#include "filenames.h"
c906108c 49
ac2e2ef7
AC
50#include "inferior.h" /* for signed_pointer_to_address */
51
2d1b2124
AC
52#include <sys/param.h> /* For MAXPATHLEN */
53
020cc13c
AC
54#ifdef HAVE_CURSES_H
55#include <curses.h>
56#endif
57#ifdef HAVE_TERM_H
58#include <term.h>
59#endif
60
c906108c
SS
61#include <readline/readline.h>
62
ed1801df
AC
63#ifdef USE_MMALLOC
64#include "mmalloc.h"
65#endif
66
3c37485b
AC
67#ifdef NEED_DECLARATION_MALLOC
68extern PTR malloc ();
69#endif
0e52036f
AC
70#ifdef NEED_DECLARATION_REALLOC
71extern PTR realloc ();
72#endif
81b8eb80
AC
73#ifdef NEED_DECLARATION_FREE
74extern void free ();
75#endif
a4db0f07
RH
76/* Actually, we'll never have the decl, since we don't define _GNU_SOURCE. */
77#if defined(HAVE_CANONICALIZE_FILE_NAME) \
78 && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME)
79extern char *canonicalize_file_name (const char *);
80#endif
81b8eb80 81
c906108c
SS
82/* readline defines this. */
83#undef savestring
84
507f3c78 85void (*error_begin_hook) (void);
c906108c 86
2acceee2
JM
87/* Holds the last error message issued by gdb */
88
d9fcf2fb 89static struct ui_file *gdb_lasterr;
2acceee2 90
c906108c
SS
91/* Prototypes for local functions */
92
d9fcf2fb
JM
93static void vfprintf_maybe_filtered (struct ui_file *, const char *,
94 va_list, int);
c906108c 95
d9fcf2fb 96static void fputs_maybe_filtered (const char *, struct ui_file *, int);
c906108c
SS
97
98#if defined (USE_MMALLOC) && !defined (NO_MMCHECK)
a14ed312 99static void malloc_botch (void);
c906108c
SS
100#endif
101
a14ed312 102static void prompt_for_continue (void);
c906108c 103
a14ed312 104static void set_width_command (char *, int, struct cmd_list_element *);
c906108c 105
a14ed312 106static void set_width (void);
c906108c 107
c906108c
SS
108/* Chain of cleanup actions established with make_cleanup,
109 to be executed if an error happens. */
110
c5aa993b
JM
111static struct cleanup *cleanup_chain; /* cleaned up after a failed command */
112static struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */
113static struct cleanup *run_cleanup_chain; /* cleaned up on each 'run' */
114static struct cleanup *exec_cleanup_chain; /* cleaned up on each execution command */
6426a772
JM
115/* cleaned up on each error from within an execution command */
116static struct cleanup *exec_error_cleanup_chain;
43ff13b4
JM
117
118/* Pointer to what is left to do for an execution command after the
119 target stops. Used only in asynchronous mode, by targets that
120 support async execution. The finish and until commands use it. So
121 does the target extended-remote command. */
122struct continuation *cmd_continuation;
c2d11a7d 123struct continuation *intermediate_continuation;
c906108c
SS
124
125/* Nonzero if we have job control. */
126
127int job_control;
128
129/* Nonzero means a quit has been requested. */
130
131int quit_flag;
132
133/* Nonzero means quit immediately if Control-C is typed now, rather
134 than waiting until QUIT is executed. Be careful in setting this;
135 code which executes with immediate_quit set has to be very careful
136 about being able to deal with being interrupted at any time. It is
137 almost always better to use QUIT; the only exception I can think of
138 is being able to quit out of a system call (using EINTR loses if
139 the SIGINT happens between the previous QUIT and the system call).
140 To immediately quit in the case in which a SIGINT happens between
141 the previous QUIT and setting immediate_quit (desirable anytime we
142 expect to block), call QUIT after setting immediate_quit. */
143
144int immediate_quit;
145
4a351cef
AF
146/* Nonzero means that encoded C++/ObjC names should be printed out in their
147 C++/ObjC form rather than raw. */
c906108c
SS
148
149int demangle = 1;
150
4a351cef
AF
151/* Nonzero means that encoded C++/ObjC names should be printed out in their
152 C++/ObjC form even in assembler language displays. If this is set, but
c906108c
SS
153 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
154
155int asm_demangle = 0;
156
157/* Nonzero means that strings with character values >0x7F should be printed
158 as octal escapes. Zero means just print the value (e.g. it's an
159 international character, and the terminal or window can cope.) */
160
161int sevenbit_strings = 0;
162
163/* String to be printed before error messages, if any. */
164
165char *error_pre_print;
166
167/* String to be printed before quit messages, if any. */
168
169char *quit_pre_print;
170
171/* String to be printed before warning messages, if any. */
172
173char *warning_pre_print = "\nwarning: ";
174
175int pagination_enabled = 1;
c906108c 176\f
c5aa993b 177
c906108c
SS
178/* Add a new cleanup to the cleanup_chain,
179 and return the previous chain pointer
180 to be passed later to do_cleanups or discard_cleanups.
181 Args are FUNCTION to clean up with, and ARG to pass to it. */
182
183struct cleanup *
e4005526 184make_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 185{
c5aa993b 186 return make_my_cleanup (&cleanup_chain, function, arg);
c906108c
SS
187}
188
189struct cleanup *
e4005526 190make_final_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 191{
c5aa993b 192 return make_my_cleanup (&final_cleanup_chain, function, arg);
c906108c 193}
7a292a7a 194
c906108c 195struct cleanup *
e4005526 196make_run_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 197{
c5aa993b 198 return make_my_cleanup (&run_cleanup_chain, function, arg);
c906108c 199}
7a292a7a 200
43ff13b4 201struct cleanup *
e4005526 202make_exec_cleanup (make_cleanup_ftype *function, void *arg)
43ff13b4 203{
c5aa993b 204 return make_my_cleanup (&exec_cleanup_chain, function, arg);
43ff13b4
JM
205}
206
6426a772 207struct cleanup *
e4005526 208make_exec_error_cleanup (make_cleanup_ftype *function, void *arg)
6426a772
JM
209{
210 return make_my_cleanup (&exec_error_cleanup_chain, function, arg);
211}
212
7a292a7a 213static void
fba45db2 214do_freeargv (void *arg)
7a292a7a 215{
c5aa993b 216 freeargv ((char **) arg);
7a292a7a
SS
217}
218
219struct cleanup *
fba45db2 220make_cleanup_freeargv (char **arg)
7a292a7a
SS
221{
222 return make_my_cleanup (&cleanup_chain, do_freeargv, arg);
223}
224
5c65bbb6
AC
225static void
226do_bfd_close_cleanup (void *arg)
227{
228 bfd_close (arg);
229}
230
231struct cleanup *
232make_cleanup_bfd_close (bfd *abfd)
233{
234 return make_cleanup (do_bfd_close_cleanup, abfd);
235}
236
f5ff8c83
AC
237static void
238do_close_cleanup (void *arg)
239{
f042532c
AC
240 int *fd = arg;
241 close (*fd);
242 xfree (fd);
f5ff8c83
AC
243}
244
245struct cleanup *
246make_cleanup_close (int fd)
247{
f042532c
AC
248 int *saved_fd = xmalloc (sizeof (fd));
249 *saved_fd = fd;
250 return make_cleanup (do_close_cleanup, saved_fd);
f5ff8c83
AC
251}
252
11cf8741 253static void
d9fcf2fb 254do_ui_file_delete (void *arg)
11cf8741 255{
d9fcf2fb 256 ui_file_delete (arg);
11cf8741
JM
257}
258
259struct cleanup *
d9fcf2fb 260make_cleanup_ui_file_delete (struct ui_file *arg)
11cf8741 261{
d9fcf2fb 262 return make_my_cleanup (&cleanup_chain, do_ui_file_delete, arg);
11cf8741
JM
263}
264
c906108c 265struct cleanup *
e4005526
AC
266make_my_cleanup (struct cleanup **pmy_chain, make_cleanup_ftype *function,
267 void *arg)
c906108c
SS
268{
269 register struct cleanup *new
389e51db 270 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
c906108c
SS
271 register struct cleanup *old_chain = *pmy_chain;
272
273 new->next = *pmy_chain;
274 new->function = function;
275 new->arg = arg;
276 *pmy_chain = new;
277
278 return old_chain;
279}
280
281/* Discard cleanups and do the actions they describe
282 until we get back to the point OLD_CHAIN in the cleanup_chain. */
283
284void
fba45db2 285do_cleanups (register struct cleanup *old_chain)
c906108c 286{
c5aa993b 287 do_my_cleanups (&cleanup_chain, old_chain);
c906108c
SS
288}
289
290void
fba45db2 291do_final_cleanups (register struct cleanup *old_chain)
c906108c 292{
c5aa993b 293 do_my_cleanups (&final_cleanup_chain, old_chain);
c906108c
SS
294}
295
296void
fba45db2 297do_run_cleanups (register struct cleanup *old_chain)
c906108c 298{
c5aa993b 299 do_my_cleanups (&run_cleanup_chain, old_chain);
c906108c
SS
300}
301
43ff13b4 302void
fba45db2 303do_exec_cleanups (register struct cleanup *old_chain)
43ff13b4 304{
c5aa993b 305 do_my_cleanups (&exec_cleanup_chain, old_chain);
43ff13b4
JM
306}
307
6426a772 308void
fba45db2 309do_exec_error_cleanups (register struct cleanup *old_chain)
6426a772
JM
310{
311 do_my_cleanups (&exec_error_cleanup_chain, old_chain);
312}
313
c906108c 314void
fba45db2
KB
315do_my_cleanups (register struct cleanup **pmy_chain,
316 register struct cleanup *old_chain)
c906108c
SS
317{
318 register struct cleanup *ptr;
319 while ((ptr = *pmy_chain) != old_chain)
320 {
321 *pmy_chain = ptr->next; /* Do this first incase recursion */
322 (*ptr->function) (ptr->arg);
b8c9b27d 323 xfree (ptr);
c906108c
SS
324 }
325}
326
327/* Discard cleanups, not doing the actions they describe,
328 until we get back to the point OLD_CHAIN in the cleanup_chain. */
329
330void
fba45db2 331discard_cleanups (register struct cleanup *old_chain)
c906108c 332{
c5aa993b 333 discard_my_cleanups (&cleanup_chain, old_chain);
c906108c
SS
334}
335
336void
fba45db2 337discard_final_cleanups (register struct cleanup *old_chain)
c906108c 338{
c5aa993b 339 discard_my_cleanups (&final_cleanup_chain, old_chain);
c906108c
SS
340}
341
6426a772 342void
fba45db2 343discard_exec_error_cleanups (register struct cleanup *old_chain)
6426a772
JM
344{
345 discard_my_cleanups (&exec_error_cleanup_chain, old_chain);
346}
347
c906108c 348void
fba45db2
KB
349discard_my_cleanups (register struct cleanup **pmy_chain,
350 register struct cleanup *old_chain)
c906108c
SS
351{
352 register struct cleanup *ptr;
353 while ((ptr = *pmy_chain) != old_chain)
354 {
355 *pmy_chain = ptr->next;
b8c9b27d 356 xfree (ptr);
c906108c
SS
357 }
358}
359
360/* Set the cleanup_chain to 0, and return the old cleanup chain. */
361struct cleanup *
fba45db2 362save_cleanups (void)
c906108c 363{
c5aa993b 364 return save_my_cleanups (&cleanup_chain);
c906108c
SS
365}
366
367struct cleanup *
fba45db2 368save_final_cleanups (void)
c906108c 369{
c5aa993b 370 return save_my_cleanups (&final_cleanup_chain);
c906108c
SS
371}
372
373struct cleanup *
fba45db2 374save_my_cleanups (struct cleanup **pmy_chain)
c906108c
SS
375{
376 struct cleanup *old_chain = *pmy_chain;
377
378 *pmy_chain = 0;
379 return old_chain;
380}
381
382/* Restore the cleanup chain from a previously saved chain. */
383void
fba45db2 384restore_cleanups (struct cleanup *chain)
c906108c 385{
c5aa993b 386 restore_my_cleanups (&cleanup_chain, chain);
c906108c
SS
387}
388
389void
fba45db2 390restore_final_cleanups (struct cleanup *chain)
c906108c 391{
c5aa993b 392 restore_my_cleanups (&final_cleanup_chain, chain);
c906108c
SS
393}
394
395void
fba45db2 396restore_my_cleanups (struct cleanup **pmy_chain, struct cleanup *chain)
c906108c
SS
397{
398 *pmy_chain = chain;
399}
400
401/* This function is useful for cleanups.
402 Do
403
c5aa993b
JM
404 foo = xmalloc (...);
405 old_chain = make_cleanup (free_current_contents, &foo);
c906108c
SS
406
407 to arrange to free the object thus allocated. */
408
409void
2f9429ae 410free_current_contents (void *ptr)
c906108c 411{
2f9429ae 412 void **location = ptr;
e2f9c474 413 if (location == NULL)
8e65ff28
AC
414 internal_error (__FILE__, __LINE__,
415 "free_current_contents: NULL pointer");
2f9429ae 416 if (*location != NULL)
e2f9c474 417 {
b8c9b27d 418 xfree (*location);
e2f9c474
AC
419 *location = NULL;
420 }
c906108c
SS
421}
422
423/* Provide a known function that does nothing, to use as a base for
424 for a possibly long chain of cleanups. This is useful where we
425 use the cleanup chain for handling normal cleanups as well as dealing
426 with cleanups that need to be done as a result of a call to error().
427 In such cases, we may not be certain where the first cleanup is, unless
428 we have a do-nothing one to always use as the base. */
429
430/* ARGSUSED */
431void
e4005526 432null_cleanup (void *arg)
c906108c
SS
433{
434}
435
74f832da 436/* Add a continuation to the continuation list, the global list
c2d11a7d 437 cmd_continuation. The new continuation will be added at the front.*/
43ff13b4 438void
74f832da
KB
439add_continuation (void (*continuation_hook) (struct continuation_arg *),
440 struct continuation_arg *arg_list)
43ff13b4 441{
c5aa993b 442 struct continuation *continuation_ptr;
43ff13b4 443
c5aa993b
JM
444 continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation));
445 continuation_ptr->continuation_hook = continuation_hook;
446 continuation_ptr->arg_list = arg_list;
447 continuation_ptr->next = cmd_continuation;
448 cmd_continuation = continuation_ptr;
43ff13b4
JM
449}
450
451/* Walk down the cmd_continuation list, and execute all the
c2d11a7d
JM
452 continuations. There is a problem though. In some cases new
453 continuations may be added while we are in the middle of this
454 loop. If this happens they will be added in the front, and done
455 before we have a chance of exhausting those that were already
456 there. We need to then save the beginning of the list in a pointer
457 and do the continuations from there on, instead of using the
458 global beginning of list as our iteration pointer.*/
c5aa993b 459void
fba45db2 460do_all_continuations (void)
c2d11a7d
JM
461{
462 struct continuation *continuation_ptr;
463 struct continuation *saved_continuation;
464
465 /* Copy the list header into another pointer, and set the global
466 list header to null, so that the global list can change as a side
467 effect of invoking the continuations and the processing of
468 the preexisting continuations will not be affected. */
469 continuation_ptr = cmd_continuation;
470 cmd_continuation = NULL;
471
472 /* Work now on the list we have set aside. */
473 while (continuation_ptr)
474 {
475 (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
476 saved_continuation = continuation_ptr;
477 continuation_ptr = continuation_ptr->next;
b8c9b27d 478 xfree (saved_continuation);
c2d11a7d
JM
479 }
480}
481
482/* Walk down the cmd_continuation list, and get rid of all the
483 continuations. */
484void
fba45db2 485discard_all_continuations (void)
43ff13b4 486{
c5aa993b 487 struct continuation *continuation_ptr;
43ff13b4 488
c5aa993b
JM
489 while (cmd_continuation)
490 {
c5aa993b
JM
491 continuation_ptr = cmd_continuation;
492 cmd_continuation = continuation_ptr->next;
b8c9b27d 493 xfree (continuation_ptr);
c5aa993b 494 }
43ff13b4 495}
c2c6d25f 496
57e687d9 497/* Add a continuation to the continuation list, the global list
c2d11a7d
JM
498 intermediate_continuation. The new continuation will be added at the front.*/
499void
74f832da
KB
500add_intermediate_continuation (void (*continuation_hook)
501 (struct continuation_arg *),
502 struct continuation_arg *arg_list)
c2d11a7d
JM
503{
504 struct continuation *continuation_ptr;
505
506 continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation));
507 continuation_ptr->continuation_hook = continuation_hook;
508 continuation_ptr->arg_list = arg_list;
509 continuation_ptr->next = intermediate_continuation;
510 intermediate_continuation = continuation_ptr;
511}
512
513/* Walk down the cmd_continuation list, and execute all the
514 continuations. There is a problem though. In some cases new
515 continuations may be added while we are in the middle of this
516 loop. If this happens they will be added in the front, and done
517 before we have a chance of exhausting those that were already
518 there. We need to then save the beginning of the list in a pointer
519 and do the continuations from there on, instead of using the
520 global beginning of list as our iteration pointer.*/
521void
fba45db2 522do_all_intermediate_continuations (void)
c2d11a7d
JM
523{
524 struct continuation *continuation_ptr;
525 struct continuation *saved_continuation;
526
527 /* Copy the list header into another pointer, and set the global
528 list header to null, so that the global list can change as a side
529 effect of invoking the continuations and the processing of
530 the preexisting continuations will not be affected. */
531 continuation_ptr = intermediate_continuation;
532 intermediate_continuation = NULL;
533
534 /* Work now on the list we have set aside. */
535 while (continuation_ptr)
536 {
537 (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
538 saved_continuation = continuation_ptr;
539 continuation_ptr = continuation_ptr->next;
b8c9b27d 540 xfree (saved_continuation);
c2d11a7d
JM
541 }
542}
543
c2c6d25f
JM
544/* Walk down the cmd_continuation list, and get rid of all the
545 continuations. */
546void
fba45db2 547discard_all_intermediate_continuations (void)
c2c6d25f
JM
548{
549 struct continuation *continuation_ptr;
550
c2d11a7d 551 while (intermediate_continuation)
c2c6d25f 552 {
c2d11a7d
JM
553 continuation_ptr = intermediate_continuation;
554 intermediate_continuation = continuation_ptr->next;
b8c9b27d 555 xfree (continuation_ptr);
c2c6d25f
JM
556 }
557}
558
c906108c 559\f
c5aa993b 560
f5a96129
AC
561/* Print a warning message. The first argument STRING is the warning
562 message, used as an fprintf format string, the second is the
563 va_list of arguments for that string. A warning is unfiltered (not
564 paginated) so that the user does not need to page through each
565 screen full of warnings when there are lots of them. */
c906108c
SS
566
567void
f5a96129 568vwarning (const char *string, va_list args)
c906108c 569{
f5a96129
AC
570 if (warning_hook)
571 (*warning_hook) (string, args);
572 else
573 {
574 target_terminal_ours ();
575 wrap_here (""); /* Force out any buffered output */
576 gdb_flush (gdb_stdout);
577 if (warning_pre_print)
578 fprintf_unfiltered (gdb_stderr, warning_pre_print);
579 vfprintf_unfiltered (gdb_stderr, string, args);
580 fprintf_unfiltered (gdb_stderr, "\n");
581 va_end (args);
582 }
c906108c
SS
583}
584
585/* Print a warning message.
586 The first argument STRING is the warning message, used as a fprintf string,
587 and the remaining args are passed as arguments to it.
588 The primary difference between warnings and errors is that a warning
589 does not force the return to command level. */
590
c906108c 591void
c5aa993b 592warning (const char *string,...)
c906108c
SS
593{
594 va_list args;
c906108c 595 va_start (args, string);
f5a96129
AC
596 vwarning (string, args);
597 va_end (args);
c906108c
SS
598}
599
c906108c
SS
600/* Print an error message and return to command level.
601 The first argument STRING is the error message, used as a fprintf string,
602 and the remaining args are passed as arguments to it. */
603
4ce44c66
JM
604NORETURN void
605verror (const char *string, va_list args)
606{
fffee0be
AC
607 struct ui_file *tmp_stream = mem_fileopen ();
608 make_cleanup_ui_file_delete (tmp_stream);
609 vfprintf_unfiltered (tmp_stream, string, args);
610 error_stream (tmp_stream);
4ce44c66
JM
611}
612
c906108c 613NORETURN void
c5aa993b 614error (const char *string,...)
c906108c
SS
615{
616 va_list args;
c906108c 617 va_start (args, string);
4ce44c66
JM
618 verror (string, args);
619 va_end (args);
c906108c
SS
620}
621
fffee0be
AC
622static void
623do_write (void *data, const char *buffer, long length_buffer)
624{
625 ui_file_write (data, buffer, length_buffer);
626}
627
2acceee2 628NORETURN void
d9fcf2fb 629error_stream (struct ui_file *stream)
2acceee2 630{
fffee0be
AC
631 if (error_begin_hook)
632 error_begin_hook ();
633
634 /* Copy the stream into the GDB_LASTERR buffer. */
635 ui_file_rewind (gdb_lasterr);
636 ui_file_put (stream, do_write, gdb_lasterr);
637
638 /* Write the message plus any error_pre_print to gdb_stderr. */
639 target_terminal_ours ();
640 wrap_here (""); /* Force out any buffered output */
641 gdb_flush (gdb_stdout);
642 annotate_error_begin ();
643 if (error_pre_print)
644 fprintf_filtered (gdb_stderr, error_pre_print);
645 ui_file_put (stream, do_write, gdb_stderr);
646 fprintf_filtered (gdb_stderr, "\n");
647
b5a2688f 648 throw_exception (RETURN_ERROR);
2acceee2
JM
649}
650
651/* Get the last error message issued by gdb */
652
653char *
654error_last_message (void)
655{
4ce44c66 656 long len;
d9fcf2fb 657 return ui_file_xstrdup (gdb_lasterr, &len);
2acceee2 658}
4ce44c66 659
2acceee2
JM
660/* This is to be called by main() at the very beginning */
661
662void
663error_init (void)
664{
4ce44c66 665 gdb_lasterr = mem_fileopen ();
2acceee2 666}
c906108c 667
dec43320
AC
668/* Print a message reporting an internal error/warning. Ask the user
669 if they want to continue, dump core, or just exit. Return
670 something to indicate a quit. */
c906108c 671
dec43320 672struct internal_problem
c906108c 673{
dec43320
AC
674 const char *name;
675 /* FIXME: cagney/2002-08-15: There should be ``maint set/show''
676 commands available for controlling these variables. */
677 enum auto_boolean should_quit;
678 enum auto_boolean should_dump_core;
679};
680
681/* Report a problem, internal to GDB, to the user. Once the problem
682 has been reported, and assuming GDB didn't quit, the caller can
683 either allow execution to resume or throw an error. */
684
685static void
686internal_vproblem (struct internal_problem *problem,
687const char *file, int line,
688 const char *fmt, va_list ap)
689{
690 static char msg[] = "Recursive internal problem.\n";
691 static int dejavu;
375fc983 692 int quit_p;
7be570e7 693 int dump_core_p;
c906108c 694
dec43320 695 /* Don't allow infinite error/warning recursion. */
96baa820
JM
696 switch (dejavu)
697 {
698 case 0:
699 dejavu = 1;
700 break;
701 case 1:
702 dejavu = 2;
703 fputs_unfiltered (msg, gdb_stderr);
5c7dd748 704 abort (); /* NOTE: GDB has only three calls to abort(). */
96baa820
JM
705 default:
706 dejavu = 3;
707 write (STDERR_FILENO, msg, sizeof (msg));
708 exit (1);
709 }
c906108c 710
dec43320 711 /* Try to get the message out and at the start of a new line. */
4261bedc 712 target_terminal_ours ();
dec43320
AC
713 begin_line ();
714
715 /* The error/warning message. Format using a style similar to a
716 compiler error message. */
717 fprintf_unfiltered (gdb_stderr, "%s:%d: %s: ", file, line, problem->name);
4ce44c66 718 vfprintf_unfiltered (gdb_stderr, fmt, ap);
96baa820 719 fputs_unfiltered ("\n", gdb_stderr);
c906108c 720
dec43320
AC
721 /* Provide more details so that the user knows that they are living
722 on the edge. */
723 fprintf_unfiltered (gdb_stderr, "\
724A problem internal to GDB has been detected. Further\n\
725debugging may prove unreliable.\n");
7be570e7 726
dec43320
AC
727 switch (problem->should_quit)
728 {
729 case AUTO_BOOLEAN_AUTO:
730 /* Default (yes/batch case) is to quit GDB. When in batch mode
731 this lessens the likelhood of GDB going into an infinate
732 loop. */
733 quit_p = query ("Quit this debugging session? ");
734 break;
735 case AUTO_BOOLEAN_TRUE:
736 quit_p = 1;
737 break;
738 case AUTO_BOOLEAN_FALSE:
739 quit_p = 0;
740 break;
741 default:
742 internal_error (__FILE__, __LINE__, "bad switch");
743 }
744
745 switch (problem->should_dump_core)
746 {
747 case AUTO_BOOLEAN_AUTO:
748 /* Default (yes/batch case) is to dump core. This leaves a GDB
749 `dropping' so that it is easier to see that something went
750 wrong in GDB. */
751 dump_core_p = query ("Create a core file of GDB? ");
752 break;
753 break;
754 case AUTO_BOOLEAN_TRUE:
755 dump_core_p = 1;
756 break;
757 case AUTO_BOOLEAN_FALSE:
758 dump_core_p = 0;
759 break;
760 default:
761 internal_error (__FILE__, __LINE__, "bad switch");
762 }
7be570e7 763
375fc983 764 if (quit_p)
7be570e7
JM
765 {
766 if (dump_core_p)
375fc983
AC
767 abort (); /* NOTE: GDB has only three calls to abort(). */
768 else
769 exit (1);
7be570e7
JM
770 }
771 else
772 {
773 if (dump_core_p)
375fc983
AC
774 {
775 if (fork () == 0)
776 abort (); /* NOTE: GDB has only three calls to abort(). */
777 }
7be570e7 778 }
96baa820
JM
779
780 dejavu = 0;
dec43320
AC
781}
782
783static struct internal_problem internal_error_problem = {
784 "internal-error", AUTO_BOOLEAN_AUTO, AUTO_BOOLEAN_AUTO
785};
786
787NORETURN void
788internal_verror (const char *file, int line,
789 const char *fmt, va_list ap)
790{
791 internal_vproblem (&internal_error_problem, file, line, fmt, ap);
b5a2688f 792 throw_exception (RETURN_ERROR);
c906108c
SS
793}
794
4ce44c66 795NORETURN void
8e65ff28 796internal_error (const char *file, int line, const char *string, ...)
4ce44c66
JM
797{
798 va_list ap;
799 va_start (ap, string);
8e65ff28 800 internal_verror (file, line, string, ap);
4ce44c66
JM
801 va_end (ap);
802}
803
dec43320
AC
804static struct internal_problem internal_warning_problem = {
805 "internal-error", AUTO_BOOLEAN_AUTO, AUTO_BOOLEAN_AUTO
806};
807
808void
809internal_vwarning (const char *file, int line,
810 const char *fmt, va_list ap)
811{
812 internal_vproblem (&internal_warning_problem, file, line, fmt, ap);
813}
814
815void
816internal_warning (const char *file, int line, const char *string, ...)
817{
818 va_list ap;
819 va_start (ap, string);
820 internal_vwarning (file, line, string, ap);
821 va_end (ap);
822}
823
c906108c
SS
824/* The strerror() function can return NULL for errno values that are
825 out of range. Provide a "safe" version that always returns a
826 printable string. */
827
828char *
fba45db2 829safe_strerror (int errnum)
c906108c
SS
830{
831 char *msg;
832 static char buf[32];
833
834 if ((msg = strerror (errnum)) == NULL)
835 {
836 sprintf (buf, "(undocumented errno %d)", errnum);
837 msg = buf;
838 }
839 return (msg);
840}
841
c906108c
SS
842/* Print the system error message for errno, and also mention STRING
843 as the file name for which the error was encountered.
844 Then return to command level. */
845
846NORETURN void
6972bc8b 847perror_with_name (const char *string)
c906108c
SS
848{
849 char *err;
850 char *combined;
851
852 err = safe_strerror (errno);
853 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
854 strcpy (combined, string);
855 strcat (combined, ": ");
856 strcat (combined, err);
857
858 /* I understand setting these is a matter of taste. Still, some people
859 may clear errno but not know about bfd_error. Doing this here is not
860 unreasonable. */
861 bfd_set_error (bfd_error_no_error);
862 errno = 0;
863
c5aa993b 864 error ("%s.", combined);
c906108c
SS
865}
866
867/* Print the system error message for ERRCODE, and also mention STRING
868 as the file name for which the error was encountered. */
869
870void
6972bc8b 871print_sys_errmsg (const char *string, int errcode)
c906108c
SS
872{
873 char *err;
874 char *combined;
875
876 err = safe_strerror (errcode);
877 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
878 strcpy (combined, string);
879 strcat (combined, ": ");
880 strcat (combined, err);
881
882 /* We want anything which was printed on stdout to come out first, before
883 this message. */
884 gdb_flush (gdb_stdout);
885 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
886}
887
888/* Control C eventually causes this to be called, at a convenient time. */
889
890void
fba45db2 891quit (void)
c906108c 892{
819cc324 893 struct serial *gdb_stdout_serial = serial_fdopen (1);
c906108c
SS
894
895 target_terminal_ours ();
896
897 /* We want all output to appear now, before we print "Quit". We
898 have 3 levels of buffering we have to flush (it's possible that
899 some of these should be changed to flush the lower-level ones
900 too): */
901
902 /* 1. The _filtered buffer. */
c5aa993b 903 wrap_here ((char *) 0);
c906108c
SS
904
905 /* 2. The stdio buffer. */
906 gdb_flush (gdb_stdout);
907 gdb_flush (gdb_stderr);
908
909 /* 3. The system-level buffer. */
2cd58942
AC
910 serial_drain_output (gdb_stdout_serial);
911 serial_un_fdopen (gdb_stdout_serial);
c906108c
SS
912
913 annotate_error_begin ();
914
915 /* Don't use *_filtered; we don't want to prompt the user to continue. */
916 if (quit_pre_print)
917 fprintf_unfiltered (gdb_stderr, quit_pre_print);
918
7be570e7
JM
919#ifdef __MSDOS__
920 /* No steenking SIGINT will ever be coming our way when the
921 program is resumed. Don't lie. */
922 fprintf_unfiltered (gdb_stderr, "Quit\n");
923#else
c906108c 924 if (job_control
c5aa993b
JM
925 /* If there is no terminal switching for this target, then we can't
926 possibly get screwed by the lack of job control. */
c906108c
SS
927 || current_target.to_terminal_ours == NULL)
928 fprintf_unfiltered (gdb_stderr, "Quit\n");
929 else
930 fprintf_unfiltered (gdb_stderr,
c5aa993b 931 "Quit (expect signal SIGINT when the program is resumed)\n");
7be570e7 932#endif
b5a2688f 933 throw_exception (RETURN_QUIT);
c906108c
SS
934}
935
c906108c 936/* Control C comes here */
c906108c 937void
fba45db2 938request_quit (int signo)
c906108c
SS
939{
940 quit_flag = 1;
941 /* Restore the signal handler. Harmless with BSD-style signals, needed
942 for System V-style signals. So just always do it, rather than worrying
943 about USG defines and stuff like that. */
944 signal (signo, request_quit);
945
946#ifdef REQUEST_QUIT
947 REQUEST_QUIT;
948#else
c5aa993b 949 if (immediate_quit)
c906108c
SS
950 quit ();
951#endif
952}
c906108c
SS
953\f
954/* Memory management stuff (malloc friends). */
955
c906108c
SS
956#if !defined (USE_MMALLOC)
957
c0e61796
AC
958/* NOTE: These must use PTR so that their definition matches the
959 declaration found in "mmalloc.h". */
ed9a39eb 960
ed1801df
AC
961static void *
962mmalloc (void *md, size_t size)
c906108c 963{
c0e61796 964 return malloc (size); /* NOTE: GDB's only call to malloc() */
c906108c
SS
965}
966
ed1801df
AC
967static void *
968mrealloc (void *md, void *ptr, size_t size)
c906108c 969{
c5aa993b 970 if (ptr == 0) /* Guard against old realloc's */
c0e61796 971 return mmalloc (md, size);
c906108c 972 else
c0e61796
AC
973 return realloc (ptr, size); /* NOTE: GDB's only call to ralloc() */
974}
975
ed1801df
AC
976static void *
977mcalloc (void *md, size_t number, size_t size)
c0e61796
AC
978{
979 return calloc (number, size); /* NOTE: GDB's only call to calloc() */
c906108c
SS
980}
981
ed1801df
AC
982static void
983mfree (void *md, void *ptr)
c906108c 984{
c0e61796 985 free (ptr); /* NOTE: GDB's only call to free() */
c906108c
SS
986}
987
c5aa993b 988#endif /* USE_MMALLOC */
c906108c
SS
989
990#if !defined (USE_MMALLOC) || defined (NO_MMCHECK)
991
992void
082faf24 993init_malloc (void *md)
c906108c
SS
994{
995}
996
997#else /* Have mmalloc and want corruption checking */
998
999static void
fba45db2 1000malloc_botch (void)
c906108c 1001{
96baa820 1002 fprintf_unfiltered (gdb_stderr, "Memory corruption\n");
e1e9e218 1003 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
1004}
1005
1006/* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
1007 by MD, to detect memory corruption. Note that MD may be NULL to specify
1008 the default heap that grows via sbrk.
1009
1010 Note that for freshly created regions, we must call mmcheckf prior to any
1011 mallocs in the region. Otherwise, any region which was allocated prior to
1012 installing the checking hooks, which is later reallocated or freed, will
1013 fail the checks! The mmcheck function only allows initial hooks to be
1014 installed before the first mmalloc. However, anytime after we have called
1015 mmcheck the first time to install the checking hooks, we can call it again
1016 to update the function pointer to the memory corruption handler.
1017
1018 Returns zero on failure, non-zero on success. */
1019
1020#ifndef MMCHECK_FORCE
1021#define MMCHECK_FORCE 0
1022#endif
1023
1024void
082faf24 1025init_malloc (void *md)
c906108c
SS
1026{
1027 if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE))
1028 {
1029 /* Don't use warning(), which relies on current_target being set
c5aa993b
JM
1030 to something other than dummy_target, until after
1031 initialize_all_files(). */
c906108c
SS
1032
1033 fprintf_unfiltered
1034 (gdb_stderr, "warning: failed to install memory consistency checks; ");
1035 fprintf_unfiltered
1036 (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n");
1037 }
1038
1039 mmtrace ();
1040}
1041
1042#endif /* Have mmalloc and want corruption checking */
1043
1044/* Called when a memory allocation fails, with the number of bytes of
1045 memory requested in SIZE. */
1046
1047NORETURN void
fba45db2 1048nomem (long size)
c906108c
SS
1049{
1050 if (size > 0)
1051 {
8e65ff28
AC
1052 internal_error (__FILE__, __LINE__,
1053 "virtual memory exhausted: can't allocate %ld bytes.", size);
c906108c
SS
1054 }
1055 else
1056 {
8e65ff28
AC
1057 internal_error (__FILE__, __LINE__,
1058 "virtual memory exhausted.");
c906108c
SS
1059 }
1060}
1061
c0e61796 1062/* The xmmalloc() family of memory management routines.
c906108c 1063
c0e61796
AC
1064 These are are like the mmalloc() family except that they implement
1065 consistent semantics and guard against typical memory management
1066 problems: if a malloc fails, an internal error is thrown; if
1067 free(NULL) is called, it is ignored; if *alloc(0) is called, NULL
1068 is returned.
1069
1070 All these routines are implemented using the mmalloc() family. */
1071
1072void *
1073xmmalloc (void *md, size_t size)
c906108c 1074{
c0e61796 1075 void *val;
c906108c
SS
1076
1077 if (size == 0)
1078 {
1079 val = NULL;
1080 }
c0e61796 1081 else
c906108c 1082 {
c0e61796
AC
1083 val = mmalloc (md, size);
1084 if (val == NULL)
1085 nomem (size);
c906108c
SS
1086 }
1087 return (val);
1088}
1089
c0e61796
AC
1090void *
1091xmrealloc (void *md, void *ptr, size_t size)
c906108c 1092{
c0e61796 1093 void *val;
c906108c 1094
d7fa9de0 1095 if (size == 0)
c906108c 1096 {
d7fa9de0
KB
1097 if (ptr != NULL)
1098 mfree (md, ptr);
1099 val = NULL;
c906108c
SS
1100 }
1101 else
1102 {
d7fa9de0
KB
1103 if (ptr != NULL)
1104 {
1105 val = mrealloc (md, ptr, size);
1106 }
1107 else
1108 {
1109 val = mmalloc (md, size);
1110 }
1111 if (val == NULL)
1112 {
1113 nomem (size);
1114 }
c906108c
SS
1115 }
1116 return (val);
1117}
1118
c0e61796
AC
1119void *
1120xmcalloc (void *md, size_t number, size_t size)
ed9a39eb 1121{
d7fa9de0 1122 void *mem;
d7fa9de0
KB
1123 if (number == 0 || size == 0)
1124 mem = NULL;
1125 else
1126 {
c0e61796 1127 mem = mcalloc (md, number, size);
d7fa9de0
KB
1128 if (mem == NULL)
1129 nomem (number * size);
1130 }
ed9a39eb
JM
1131 return mem;
1132}
1133
c0e61796
AC
1134void
1135xmfree (void *md, void *ptr)
1136{
1137 if (ptr != NULL)
1138 mfree (md, ptr);
1139}
1140
1141/* The xmalloc() (libiberty.h) family of memory management routines.
1142
1143 These are like the ISO-C malloc() family except that they implement
1144 consistent semantics and guard against typical memory management
1145 problems. See xmmalloc() above for further information.
1146
1147 All these routines are wrappers to the xmmalloc() family. */
1148
1149/* NOTE: These are declared using PTR to ensure consistency with
1150 "libiberty.h". xfree() is GDB local. */
1151
1152PTR
1153xmalloc (size_t size)
1154{
1155 return xmmalloc (NULL, size);
1156}
c906108c
SS
1157
1158PTR
fba45db2 1159xrealloc (PTR ptr, size_t size)
c906108c 1160{
c0e61796 1161 return xmrealloc (NULL, ptr, size);
c906108c 1162}
b8c9b27d 1163
c0e61796
AC
1164PTR
1165xcalloc (size_t number, size_t size)
1166{
1167 return xmcalloc (NULL, number, size);
1168}
b8c9b27d
KB
1169
1170void
1171xfree (void *ptr)
1172{
c0e61796 1173 xmfree (NULL, ptr);
b8c9b27d 1174}
c906108c 1175\f
c5aa993b 1176
76995688
AC
1177/* Like asprintf/vasprintf but get an internal_error if the call
1178 fails. */
1179
1180void
1181xasprintf (char **ret, const char *format, ...)
1182{
1183 va_list args;
1184 va_start (args, format);
1185 xvasprintf (ret, format, args);
1186 va_end (args);
1187}
1188
1189void
1190xvasprintf (char **ret, const char *format, va_list ap)
1191{
1192 int status = vasprintf (ret, format, ap);
1193 /* NULL could be returned due to a memory allocation problem; a
1194 badly format string; or something else. */
1195 if ((*ret) == NULL)
8e65ff28
AC
1196 internal_error (__FILE__, __LINE__,
1197 "vasprintf returned NULL buffer (errno %d)",
1198 errno);
76995688
AC
1199 /* A negative status with a non-NULL buffer shouldn't never
1200 happen. But to be sure. */
1201 if (status < 0)
8e65ff28
AC
1202 internal_error (__FILE__, __LINE__,
1203 "vasprintf call failed (errno %d)",
1204 errno);
76995688
AC
1205}
1206
1207
c906108c
SS
1208/* My replacement for the read system call.
1209 Used like `read' but keeps going if `read' returns too soon. */
1210
1211int
fba45db2 1212myread (int desc, char *addr, int len)
c906108c
SS
1213{
1214 register int val;
1215 int orglen = len;
1216
1217 while (len > 0)
1218 {
1219 val = read (desc, addr, len);
1220 if (val < 0)
1221 return val;
1222 if (val == 0)
1223 return orglen - len;
1224 len -= val;
1225 addr += val;
1226 }
1227 return orglen;
1228}
1229\f
1230/* Make a copy of the string at PTR with SIZE characters
1231 (and add a null character at the end in the copy).
1232 Uses malloc to get the space. Returns the address of the copy. */
1233
1234char *
5565b556 1235savestring (const char *ptr, size_t size)
c906108c
SS
1236{
1237 register char *p = (char *) xmalloc (size + 1);
1238 memcpy (p, ptr, size);
1239 p[size] = 0;
1240 return p;
1241}
1242
1243char *
5565b556 1244msavestring (void *md, const char *ptr, size_t size)
c906108c
SS
1245{
1246 register char *p = (char *) xmmalloc (md, size + 1);
1247 memcpy (p, ptr, size);
1248 p[size] = 0;
1249 return p;
1250}
1251
c906108c 1252char *
082faf24 1253mstrsave (void *md, const char *ptr)
c906108c
SS
1254{
1255 return (msavestring (md, ptr, strlen (ptr)));
1256}
1257
1258void
fba45db2 1259print_spaces (register int n, register struct ui_file *file)
c906108c 1260{
392a587b 1261 fputs_unfiltered (n_spaces (n), file);
c906108c
SS
1262}
1263
1264/* Print a host address. */
1265
1266void
d9fcf2fb 1267gdb_print_host_address (void *addr, struct ui_file *stream)
c906108c
SS
1268{
1269
1270 /* We could use the %p conversion specifier to fprintf if we had any
1271 way of knowing whether this host supports it. But the following
1272 should work on the Alpha and on 32 bit machines. */
1273
c5aa993b 1274 fprintf_filtered (stream, "0x%lx", (unsigned long) addr);
c906108c
SS
1275}
1276
1277/* Ask user a y-or-n question and return 1 iff answer is yes.
1278 Takes three args which are given to printf to print the question.
1279 The first, a control string, should end in "? ".
1280 It should not say how to answer, because we do that. */
1281
1282/* VARARGS */
1283int
6972bc8b 1284query (const char *ctlstr,...)
c906108c
SS
1285{
1286 va_list args;
1287 register int answer;
1288 register int ans2;
1289 int retval;
1290
c906108c 1291 va_start (args, ctlstr);
c906108c
SS
1292
1293 if (query_hook)
1294 {
1295 return query_hook (ctlstr, args);
1296 }
1297
1298 /* Automatically answer "yes" if input is not from a terminal. */
1299 if (!input_from_terminal_p ())
1300 return 1;
c906108c
SS
1301
1302 while (1)
1303 {
1304 wrap_here (""); /* Flush any buffered output */
1305 gdb_flush (gdb_stdout);
1306
1307 if (annotation_level > 1)
1308 printf_filtered ("\n\032\032pre-query\n");
1309
1310 vfprintf_filtered (gdb_stdout, ctlstr, args);
1311 printf_filtered ("(y or n) ");
1312
1313 if (annotation_level > 1)
1314 printf_filtered ("\n\032\032query\n");
1315
c5aa993b 1316 wrap_here ("");
c906108c
SS
1317 gdb_flush (gdb_stdout);
1318
37767e42 1319 answer = fgetc (stdin);
c906108c
SS
1320 clearerr (stdin); /* in case of C-d */
1321 if (answer == EOF) /* C-d */
c5aa993b 1322 {
c906108c
SS
1323 retval = 1;
1324 break;
1325 }
1326 /* Eat rest of input line, to EOF or newline */
37767e42 1327 if (answer != '\n')
c5aa993b 1328 do
c906108c 1329 {
37767e42 1330 ans2 = fgetc (stdin);
c906108c
SS
1331 clearerr (stdin);
1332 }
c5aa993b 1333 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
c906108c
SS
1334
1335 if (answer >= 'a')
1336 answer -= 040;
1337 if (answer == 'Y')
1338 {
1339 retval = 1;
1340 break;
1341 }
1342 if (answer == 'N')
1343 {
1344 retval = 0;
1345 break;
1346 }
1347 printf_filtered ("Please answer y or n.\n");
1348 }
1349
1350 if (annotation_level > 1)
1351 printf_filtered ("\n\032\032post-query\n");
1352 return retval;
1353}
c906108c 1354\f
c5aa993b 1355
234b45d4
KB
1356/* Print an error message saying that we couldn't make sense of a
1357 \^mumble sequence in a string or character constant. START and END
1358 indicate a substring of some larger string that contains the
1359 erroneous backslash sequence, missing the initial backslash. */
1360static NORETURN int
1361no_control_char_error (const char *start, const char *end)
1362{
1363 int len = end - start;
1364 char *copy = alloca (end - start + 1);
1365
1366 memcpy (copy, start, len);
1367 copy[len] = '\0';
1368
1369 error ("There is no control character `\\%s' in the `%s' character set.",
1370 copy, target_charset ());
1371}
1372
c906108c
SS
1373/* Parse a C escape sequence. STRING_PTR points to a variable
1374 containing a pointer to the string to parse. That pointer
1375 should point to the character after the \. That pointer
1376 is updated past the characters we use. The value of the
1377 escape sequence is returned.
1378
1379 A negative value means the sequence \ newline was seen,
1380 which is supposed to be equivalent to nothing at all.
1381
1382 If \ is followed by a null character, we return a negative
1383 value and leave the string pointer pointing at the null character.
1384
1385 If \ is followed by 000, we return 0 and leave the string pointer
1386 after the zeros. A value of 0 does not mean end of string. */
1387
1388int
fba45db2 1389parse_escape (char **string_ptr)
c906108c 1390{
234b45d4 1391 int target_char;
c906108c 1392 register int c = *(*string_ptr)++;
234b45d4
KB
1393 if (c_parse_backslash (c, &target_char))
1394 return target_char;
1395 else switch (c)
c906108c 1396 {
c906108c
SS
1397 case '\n':
1398 return -2;
1399 case 0:
1400 (*string_ptr)--;
1401 return 0;
1402 case '^':
234b45d4
KB
1403 {
1404 /* Remember where this escape sequence started, for reporting
1405 errors. */
1406 char *sequence_start_pos = *string_ptr - 1;
1407
1408 c = *(*string_ptr)++;
1409
1410 if (c == '?')
1411 {
1412 /* XXXCHARSET: What is `delete' in the host character set? */
1413 c = 0177;
1414
1415 if (! host_char_to_target (c, &target_char))
1416 error ("There is no character corresponding to `Delete' "
1417 "in the target character set `%s'.",
1418 host_charset ());
1419
1420 return target_char;
1421 }
1422 else if (c == '\\')
1423 target_char = parse_escape (string_ptr);
1424 else
1425 {
1426 if (! host_char_to_target (c, &target_char))
1427 no_control_char_error (sequence_start_pos, *string_ptr);
1428 }
1429
1430 /* Now target_char is something like `c', and we want to find
1431 its control-character equivalent. */
1432 if (! target_char_to_control_char (target_char, &target_char))
1433 no_control_char_error (sequence_start_pos, *string_ptr);
1434
1435 return target_char;
1436 }
1437
1438 /* XXXCHARSET: we need to use isdigit and value-of-digit
1439 methods of the host character set here. */
c5aa993b 1440
c906108c
SS
1441 case '0':
1442 case '1':
1443 case '2':
1444 case '3':
1445 case '4':
1446 case '5':
1447 case '6':
1448 case '7':
1449 {
1450 register int i = c - '0';
1451 register int count = 0;
1452 while (++count < 3)
1453 {
1454 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
1455 {
1456 i *= 8;
1457 i += c - '0';
1458 }
1459 else
1460 {
1461 (*string_ptr)--;
1462 break;
1463 }
1464 }
1465 return i;
1466 }
1467 default:
234b45d4
KB
1468 if (! host_char_to_target (c, &target_char))
1469 error ("The escape sequence `\%c' is equivalent to plain `%c', which"
1470 " has no equivalent\n"
1471 "in the `%s' character set.",
1472 c, c, target_charset ());
1473 return target_char;
c906108c
SS
1474 }
1475}
1476\f
1477/* Print the character C on STREAM as part of the contents of a literal
1478 string whose delimiter is QUOTER. Note that this routine should only
1479 be call for printing things which are independent of the language
1480 of the program being debugged. */
1481
43e526b9 1482static void
74f832da
KB
1483printchar (int c, void (*do_fputs) (const char *, struct ui_file *),
1484 void (*do_fprintf) (struct ui_file *, const char *, ...),
1485 struct ui_file *stream, int quoter)
c906108c
SS
1486{
1487
1488 c &= 0xFF; /* Avoid sign bit follies */
1489
c5aa993b
JM
1490 if (c < 0x20 || /* Low control chars */
1491 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1492 (sevenbit_strings && c >= 0x80))
1493 { /* high order bit set */
1494 switch (c)
1495 {
1496 case '\n':
43e526b9 1497 do_fputs ("\\n", stream);
c5aa993b
JM
1498 break;
1499 case '\b':
43e526b9 1500 do_fputs ("\\b", stream);
c5aa993b
JM
1501 break;
1502 case '\t':
43e526b9 1503 do_fputs ("\\t", stream);
c5aa993b
JM
1504 break;
1505 case '\f':
43e526b9 1506 do_fputs ("\\f", stream);
c5aa993b
JM
1507 break;
1508 case '\r':
43e526b9 1509 do_fputs ("\\r", stream);
c5aa993b
JM
1510 break;
1511 case '\033':
43e526b9 1512 do_fputs ("\\e", stream);
c5aa993b
JM
1513 break;
1514 case '\007':
43e526b9 1515 do_fputs ("\\a", stream);
c5aa993b
JM
1516 break;
1517 default:
43e526b9 1518 do_fprintf (stream, "\\%.3o", (unsigned int) c);
c5aa993b
JM
1519 break;
1520 }
1521 }
1522 else
1523 {
1524 if (c == '\\' || c == quoter)
43e526b9
JM
1525 do_fputs ("\\", stream);
1526 do_fprintf (stream, "%c", c);
c5aa993b 1527 }
c906108c 1528}
43e526b9
JM
1529
1530/* Print the character C on STREAM as part of the contents of a
1531 literal string whose delimiter is QUOTER. Note that these routines
1532 should only be call for printing things which are independent of
1533 the language of the program being debugged. */
1534
1535void
fba45db2 1536fputstr_filtered (const char *str, int quoter, struct ui_file *stream)
43e526b9
JM
1537{
1538 while (*str)
1539 printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter);
1540}
1541
1542void
fba45db2 1543fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream)
43e526b9
JM
1544{
1545 while (*str)
1546 printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1547}
1548
1549void
fba45db2 1550fputstrn_unfiltered (const char *str, int n, int quoter, struct ui_file *stream)
43e526b9
JM
1551{
1552 int i;
1553 for (i = 0; i < n; i++)
1554 printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1555}
1556
c906108c 1557\f
c5aa993b 1558
c906108c
SS
1559/* Number of lines per page or UINT_MAX if paging is disabled. */
1560static unsigned int lines_per_page;
cbfbd72a 1561/* Number of chars per line or UINT_MAX if line folding is disabled. */
c906108c
SS
1562static unsigned int chars_per_line;
1563/* Current count of lines printed on this page, chars on this line. */
1564static unsigned int lines_printed, chars_printed;
1565
1566/* Buffer and start column of buffered text, for doing smarter word-
1567 wrapping. When someone calls wrap_here(), we start buffering output
1568 that comes through fputs_filtered(). If we see a newline, we just
1569 spit it out and forget about the wrap_here(). If we see another
1570 wrap_here(), we spit it out and remember the newer one. If we see
1571 the end of the line, we spit out a newline, the indent, and then
1572 the buffered output. */
1573
1574/* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1575 are waiting to be output (they have already been counted in chars_printed).
1576 When wrap_buffer[0] is null, the buffer is empty. */
1577static char *wrap_buffer;
1578
1579/* Pointer in wrap_buffer to the next character to fill. */
1580static char *wrap_pointer;
1581
1582/* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1583 is non-zero. */
1584static char *wrap_indent;
1585
1586/* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1587 is not in effect. */
1588static int wrap_column;
c906108c 1589\f
c5aa993b 1590
c906108c
SS
1591/* Inialize the lines and chars per page */
1592void
fba45db2 1593init_page_info (void)
c906108c
SS
1594{
1595#if defined(TUI)
5ecb1806 1596 if (!tui_get_command_dimension (&chars_per_line, &lines_per_page))
c906108c
SS
1597#endif
1598 {
1599 /* These defaults will be used if we are unable to get the correct
1600 values from termcap. */
1601#if defined(__GO32__)
c5aa993b
JM
1602 lines_per_page = ScreenRows ();
1603 chars_per_line = ScreenCols ();
1604#else
c906108c
SS
1605 lines_per_page = 24;
1606 chars_per_line = 80;
1607
d036b4d9 1608#if !defined (_WIN32)
c906108c
SS
1609 /* No termcap under MPW, although might be cool to do something
1610 by looking at worksheet or console window sizes. */
1611 /* Initialize the screen height and width from termcap. */
1612 {
c5aa993b 1613 char *termtype = getenv ("TERM");
c906108c 1614
c5aa993b
JM
1615 /* Positive means success, nonpositive means failure. */
1616 int status;
c906108c 1617
c5aa993b
JM
1618 /* 2048 is large enough for all known terminals, according to the
1619 GNU termcap manual. */
1620 char term_buffer[2048];
c906108c 1621
c5aa993b
JM
1622 if (termtype)
1623 {
c906108c
SS
1624 status = tgetent (term_buffer, termtype);
1625 if (status > 0)
1626 {
c5aa993b 1627 int val;
c906108c 1628 int running_in_emacs = getenv ("EMACS") != NULL;
c5aa993b
JM
1629
1630 val = tgetnum ("li");
1631 if (val >= 0 && !running_in_emacs)
1632 lines_per_page = val;
1633 else
1634 /* The number of lines per page is not mentioned
c906108c
SS
1635 in the terminal description. This probably means
1636 that paging is not useful (e.g. emacs shell window),
1637 so disable paging. */
c5aa993b
JM
1638 lines_per_page = UINT_MAX;
1639
1640 val = tgetnum ("co");
1641 if (val >= 0)
1642 chars_per_line = val;
c906108c 1643 }
c5aa993b 1644 }
c906108c
SS
1645 }
1646#endif /* MPW */
1647
1648#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1649
1650 /* If there is a better way to determine the window size, use it. */
1651 SIGWINCH_HANDLER (SIGWINCH);
1652#endif
1653#endif
1654 /* If the output is not a terminal, don't paginate it. */
d9fcf2fb 1655 if (!ui_file_isatty (gdb_stdout))
c5aa993b
JM
1656 lines_per_page = UINT_MAX;
1657 } /* the command_line_version */
1658 set_width ();
c906108c
SS
1659}
1660
1661static void
fba45db2 1662set_width (void)
c906108c
SS
1663{
1664 if (chars_per_line == 0)
c5aa993b 1665 init_page_info ();
c906108c
SS
1666
1667 if (!wrap_buffer)
1668 {
1669 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1670 wrap_buffer[0] = '\0';
1671 }
1672 else
1673 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
c5aa993b 1674 wrap_pointer = wrap_buffer; /* Start it at the beginning */
c906108c
SS
1675}
1676
1677/* ARGSUSED */
c5aa993b 1678static void
fba45db2 1679set_width_command (char *args, int from_tty, struct cmd_list_element *c)
c906108c
SS
1680{
1681 set_width ();
1682}
1683
1684/* Wait, so the user can read what's on the screen. Prompt the user
1685 to continue by pressing RETURN. */
1686
1687static void
fba45db2 1688prompt_for_continue (void)
c906108c
SS
1689{
1690 char *ignore;
1691 char cont_prompt[120];
1692
1693 if (annotation_level > 1)
1694 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1695
1696 strcpy (cont_prompt,
1697 "---Type <return> to continue, or q <return> to quit---");
1698 if (annotation_level > 1)
1699 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1700
1701 /* We must do this *before* we call gdb_readline, else it will eventually
1702 call us -- thinking that we're trying to print beyond the end of the
1703 screen. */
1704 reinitialize_more_filter ();
1705
1706 immediate_quit++;
1707 /* On a real operating system, the user can quit with SIGINT.
1708 But not on GO32.
1709
1710 'q' is provided on all systems so users don't have to change habits
1711 from system to system, and because telling them what to do in
1712 the prompt is more user-friendly than expecting them to think of
1713 SIGINT. */
1714 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1715 whereas control-C to gdb_readline will cause the user to get dumped
1716 out to DOS. */
b4f5539f 1717 ignore = gdb_readline_wrapper (cont_prompt);
c906108c
SS
1718
1719 if (annotation_level > 1)
1720 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1721
1722 if (ignore)
1723 {
1724 char *p = ignore;
1725 while (*p == ' ' || *p == '\t')
1726 ++p;
1727 if (p[0] == 'q')
0f71a2f6 1728 {
6426a772 1729 if (!event_loop_p)
0f71a2f6
JM
1730 request_quit (SIGINT);
1731 else
c5aa993b 1732 async_request_quit (0);
0f71a2f6 1733 }
b8c9b27d 1734 xfree (ignore);
c906108c
SS
1735 }
1736 immediate_quit--;
1737
1738 /* Now we have to do this again, so that GDB will know that it doesn't
1739 need to save the ---Type <return>--- line at the top of the screen. */
1740 reinitialize_more_filter ();
1741
1742 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1743}
1744
1745/* Reinitialize filter; ie. tell it to reset to original values. */
1746
1747void
fba45db2 1748reinitialize_more_filter (void)
c906108c
SS
1749{
1750 lines_printed = 0;
1751 chars_printed = 0;
1752}
1753
1754/* Indicate that if the next sequence of characters overflows the line,
1755 a newline should be inserted here rather than when it hits the end.
1756 If INDENT is non-null, it is a string to be printed to indent the
1757 wrapped part on the next line. INDENT must remain accessible until
1758 the next call to wrap_here() or until a newline is printed through
1759 fputs_filtered().
1760
1761 If the line is already overfull, we immediately print a newline and
1762 the indentation, and disable further wrapping.
1763
1764 If we don't know the width of lines, but we know the page height,
1765 we must not wrap words, but should still keep track of newlines
1766 that were explicitly printed.
1767
1768 INDENT should not contain tabs, as that will mess up the char count
1769 on the next line. FIXME.
1770
1771 This routine is guaranteed to force out any output which has been
1772 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1773 used to force out output from the wrap_buffer. */
1774
1775void
fba45db2 1776wrap_here (char *indent)
c906108c
SS
1777{
1778 /* This should have been allocated, but be paranoid anyway. */
1779 if (!wrap_buffer)
e1e9e218 1780 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
1781
1782 if (wrap_buffer[0])
1783 {
1784 *wrap_pointer = '\0';
1785 fputs_unfiltered (wrap_buffer, gdb_stdout);
1786 }
1787 wrap_pointer = wrap_buffer;
1788 wrap_buffer[0] = '\0';
c5aa993b 1789 if (chars_per_line == UINT_MAX) /* No line overflow checking */
c906108c
SS
1790 {
1791 wrap_column = 0;
1792 }
1793 else if (chars_printed >= chars_per_line)
1794 {
1795 puts_filtered ("\n");
1796 if (indent != NULL)
1797 puts_filtered (indent);
1798 wrap_column = 0;
1799 }
1800 else
1801 {
1802 wrap_column = chars_printed;
1803 if (indent == NULL)
1804 wrap_indent = "";
1805 else
1806 wrap_indent = indent;
1807 }
1808}
1809
4a351cef
AF
1810/* Print input string to gdb_stdout, filtered, with wrap,
1811 arranging strings in columns of n chars. String can be
1812 right or left justified in the column. Never prints
1813 trailing spaces. String should never be longer than
1814 width. FIXME: this could be useful for the EXAMINE
1815 command, which currently doesn't tabulate very well */
1816
1817void
1818puts_filtered_tabular (char *string, int width, int right)
1819{
1820 int spaces = 0;
1821 int stringlen;
1822 char *spacebuf;
1823
1824 gdb_assert (chars_per_line > 0);
1825 if (chars_per_line == UINT_MAX)
1826 {
1827 fputs_filtered (string, gdb_stdout);
1828 fputs_filtered ("\n", gdb_stdout);
1829 return;
1830 }
1831
1832 if (((chars_printed - 1) / width + 2) * width >= chars_per_line)
1833 fputs_filtered ("\n", gdb_stdout);
1834
1835 if (width >= chars_per_line)
1836 width = chars_per_line - 1;
1837
1838 stringlen = strlen (string);
1839
1840 if (chars_printed > 0)
1841 spaces = width - (chars_printed - 1) % width - 1;
1842 if (right)
1843 spaces += width - stringlen;
1844
1845 spacebuf = alloca (spaces + 1);
1846 spacebuf[spaces] = '\0';
1847 while (spaces--)
1848 spacebuf[spaces] = ' ';
1849
1850 fputs_filtered (spacebuf, gdb_stdout);
1851 fputs_filtered (string, gdb_stdout);
1852}
1853
1854
c906108c
SS
1855/* Ensure that whatever gets printed next, using the filtered output
1856 commands, starts at the beginning of the line. I.E. if there is
1857 any pending output for the current line, flush it and start a new
1858 line. Otherwise do nothing. */
1859
1860void
fba45db2 1861begin_line (void)
c906108c
SS
1862{
1863 if (chars_printed > 0)
1864 {
1865 puts_filtered ("\n");
1866 }
1867}
1868
ac9a91a7 1869
c906108c
SS
1870/* Like fputs but if FILTER is true, pause after every screenful.
1871
1872 Regardless of FILTER can wrap at points other than the final
1873 character of a line.
1874
1875 Unlike fputs, fputs_maybe_filtered does not return a value.
1876 It is OK for LINEBUFFER to be NULL, in which case just don't print
1877 anything.
1878
1879 Note that a longjmp to top level may occur in this routine (only if
1880 FILTER is true) (since prompt_for_continue may do so) so this
1881 routine should not be called when cleanups are not in place. */
1882
1883static void
fba45db2
KB
1884fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
1885 int filter)
c906108c
SS
1886{
1887 const char *lineptr;
1888
1889 if (linebuffer == 0)
1890 return;
1891
1892 /* Don't do any filtering if it is disabled. */
7a292a7a 1893 if ((stream != gdb_stdout) || !pagination_enabled
c5aa993b 1894 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
c906108c
SS
1895 {
1896 fputs_unfiltered (linebuffer, stream);
1897 return;
1898 }
1899
1900 /* Go through and output each character. Show line extension
1901 when this is necessary; prompt user for new page when this is
1902 necessary. */
c5aa993b 1903
c906108c
SS
1904 lineptr = linebuffer;
1905 while (*lineptr)
1906 {
1907 /* Possible new page. */
1908 if (filter &&
1909 (lines_printed >= lines_per_page - 1))
1910 prompt_for_continue ();
1911
1912 while (*lineptr && *lineptr != '\n')
1913 {
1914 /* Print a single line. */
1915 if (*lineptr == '\t')
1916 {
1917 if (wrap_column)
1918 *wrap_pointer++ = '\t';
1919 else
1920 fputc_unfiltered ('\t', stream);
1921 /* Shifting right by 3 produces the number of tab stops
1922 we have already passed, and then adding one and
c5aa993b 1923 shifting left 3 advances to the next tab stop. */
c906108c
SS
1924 chars_printed = ((chars_printed >> 3) + 1) << 3;
1925 lineptr++;
1926 }
1927 else
1928 {
1929 if (wrap_column)
1930 *wrap_pointer++ = *lineptr;
1931 else
c5aa993b 1932 fputc_unfiltered (*lineptr, stream);
c906108c
SS
1933 chars_printed++;
1934 lineptr++;
1935 }
c5aa993b 1936
c906108c
SS
1937 if (chars_printed >= chars_per_line)
1938 {
1939 unsigned int save_chars = chars_printed;
1940
1941 chars_printed = 0;
1942 lines_printed++;
1943 /* If we aren't actually wrapping, don't output newline --
c5aa993b
JM
1944 if chars_per_line is right, we probably just overflowed
1945 anyway; if it's wrong, let us keep going. */
c906108c
SS
1946 if (wrap_column)
1947 fputc_unfiltered ('\n', stream);
1948
1949 /* Possible new page. */
1950 if (lines_printed >= lines_per_page - 1)
1951 prompt_for_continue ();
1952
1953 /* Now output indentation and wrapped string */
1954 if (wrap_column)
1955 {
1956 fputs_unfiltered (wrap_indent, stream);
c5aa993b
JM
1957 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1958 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
c906108c
SS
1959 /* FIXME, this strlen is what prevents wrap_indent from
1960 containing tabs. However, if we recurse to print it
1961 and count its chars, we risk trouble if wrap_indent is
1962 longer than (the user settable) chars_per_line.
1963 Note also that this can set chars_printed > chars_per_line
1964 if we are printing a long string. */
1965 chars_printed = strlen (wrap_indent)
c5aa993b 1966 + (save_chars - wrap_column);
c906108c
SS
1967 wrap_pointer = wrap_buffer; /* Reset buffer */
1968 wrap_buffer[0] = '\0';
c5aa993b
JM
1969 wrap_column = 0; /* And disable fancy wrap */
1970 }
c906108c
SS
1971 }
1972 }
1973
1974 if (*lineptr == '\n')
1975 {
1976 chars_printed = 0;
c5aa993b 1977 wrap_here ((char *) 0); /* Spit out chars, cancel further wraps */
c906108c
SS
1978 lines_printed++;
1979 fputc_unfiltered ('\n', stream);
1980 lineptr++;
1981 }
1982 }
1983}
1984
1985void
fba45db2 1986fputs_filtered (const char *linebuffer, struct ui_file *stream)
c906108c
SS
1987{
1988 fputs_maybe_filtered (linebuffer, stream, 1);
1989}
1990
1991int
fba45db2 1992putchar_unfiltered (int c)
c906108c 1993{
11cf8741 1994 char buf = c;
d9fcf2fb 1995 ui_file_write (gdb_stdout, &buf, 1);
c906108c
SS
1996 return c;
1997}
1998
d1f4cff8
AC
1999/* Write character C to gdb_stdout using GDB's paging mechanism and return C.
2000 May return nonlocally. */
2001
2002int
2003putchar_filtered (int c)
2004{
2005 return fputc_filtered (c, gdb_stdout);
2006}
2007
c906108c 2008int
fba45db2 2009fputc_unfiltered (int c, struct ui_file *stream)
c906108c 2010{
11cf8741 2011 char buf = c;
d9fcf2fb 2012 ui_file_write (stream, &buf, 1);
c906108c
SS
2013 return c;
2014}
2015
2016int
fba45db2 2017fputc_filtered (int c, struct ui_file *stream)
c906108c
SS
2018{
2019 char buf[2];
2020
2021 buf[0] = c;
2022 buf[1] = 0;
2023 fputs_filtered (buf, stream);
2024 return c;
2025}
2026
2027/* puts_debug is like fputs_unfiltered, except it prints special
2028 characters in printable fashion. */
2029
2030void
fba45db2 2031puts_debug (char *prefix, char *string, char *suffix)
c906108c
SS
2032{
2033 int ch;
2034
2035 /* Print prefix and suffix after each line. */
2036 static int new_line = 1;
2037 static int return_p = 0;
2038 static char *prev_prefix = "";
2039 static char *prev_suffix = "";
2040
2041 if (*string == '\n')
2042 return_p = 0;
2043
2044 /* If the prefix is changing, print the previous suffix, a new line,
2045 and the new prefix. */
c5aa993b 2046 if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line)
c906108c 2047 {
9846de1b
JM
2048 fputs_unfiltered (prev_suffix, gdb_stdlog);
2049 fputs_unfiltered ("\n", gdb_stdlog);
2050 fputs_unfiltered (prefix, gdb_stdlog);
c906108c
SS
2051 }
2052
2053 /* Print prefix if we printed a newline during the previous call. */
2054 if (new_line)
2055 {
2056 new_line = 0;
9846de1b 2057 fputs_unfiltered (prefix, gdb_stdlog);
c906108c
SS
2058 }
2059
2060 prev_prefix = prefix;
2061 prev_suffix = suffix;
2062
2063 /* Output characters in a printable format. */
2064 while ((ch = *string++) != '\0')
2065 {
2066 switch (ch)
c5aa993b 2067 {
c906108c
SS
2068 default:
2069 if (isprint (ch))
9846de1b 2070 fputc_unfiltered (ch, gdb_stdlog);
c906108c
SS
2071
2072 else
9846de1b 2073 fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff);
c906108c
SS
2074 break;
2075
c5aa993b
JM
2076 case '\\':
2077 fputs_unfiltered ("\\\\", gdb_stdlog);
2078 break;
2079 case '\b':
2080 fputs_unfiltered ("\\b", gdb_stdlog);
2081 break;
2082 case '\f':
2083 fputs_unfiltered ("\\f", gdb_stdlog);
2084 break;
2085 case '\n':
2086 new_line = 1;
2087 fputs_unfiltered ("\\n", gdb_stdlog);
2088 break;
2089 case '\r':
2090 fputs_unfiltered ("\\r", gdb_stdlog);
2091 break;
2092 case '\t':
2093 fputs_unfiltered ("\\t", gdb_stdlog);
2094 break;
2095 case '\v':
2096 fputs_unfiltered ("\\v", gdb_stdlog);
2097 break;
2098 }
c906108c
SS
2099
2100 return_p = ch == '\r';
2101 }
2102
2103 /* Print suffix if we printed a newline. */
2104 if (new_line)
2105 {
9846de1b
JM
2106 fputs_unfiltered (suffix, gdb_stdlog);
2107 fputs_unfiltered ("\n", gdb_stdlog);
c906108c
SS
2108 }
2109}
2110
2111
2112/* Print a variable number of ARGS using format FORMAT. If this
2113 information is going to put the amount written (since the last call
2114 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
2115 call prompt_for_continue to get the users permision to continue.
2116
2117 Unlike fprintf, this function does not return a value.
2118
2119 We implement three variants, vfprintf (takes a vararg list and stream),
2120 fprintf (takes a stream to write on), and printf (the usual).
2121
2122 Note also that a longjmp to top level may occur in this routine
2123 (since prompt_for_continue may do so) so this routine should not be
2124 called when cleanups are not in place. */
2125
2126static void
fba45db2
KB
2127vfprintf_maybe_filtered (struct ui_file *stream, const char *format,
2128 va_list args, int filter)
c906108c
SS
2129{
2130 char *linebuffer;
2131 struct cleanup *old_cleanups;
2132
76995688 2133 xvasprintf (&linebuffer, format, args);
b8c9b27d 2134 old_cleanups = make_cleanup (xfree, linebuffer);
c906108c
SS
2135 fputs_maybe_filtered (linebuffer, stream, filter);
2136 do_cleanups (old_cleanups);
2137}
2138
2139
2140void
fba45db2 2141vfprintf_filtered (struct ui_file *stream, const char *format, va_list args)
c906108c
SS
2142{
2143 vfprintf_maybe_filtered (stream, format, args, 1);
2144}
2145
2146void
fba45db2 2147vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
c906108c
SS
2148{
2149 char *linebuffer;
2150 struct cleanup *old_cleanups;
2151
76995688 2152 xvasprintf (&linebuffer, format, args);
b8c9b27d 2153 old_cleanups = make_cleanup (xfree, linebuffer);
c906108c
SS
2154 fputs_unfiltered (linebuffer, stream);
2155 do_cleanups (old_cleanups);
2156}
2157
2158void
fba45db2 2159vprintf_filtered (const char *format, va_list args)
c906108c
SS
2160{
2161 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
2162}
2163
2164void
fba45db2 2165vprintf_unfiltered (const char *format, va_list args)
c906108c
SS
2166{
2167 vfprintf_unfiltered (gdb_stdout, format, args);
2168}
2169
c906108c 2170void
d9fcf2fb 2171fprintf_filtered (struct ui_file * stream, const char *format,...)
c906108c
SS
2172{
2173 va_list args;
c906108c 2174 va_start (args, format);
c906108c
SS
2175 vfprintf_filtered (stream, format, args);
2176 va_end (args);
2177}
2178
c906108c 2179void
d9fcf2fb 2180fprintf_unfiltered (struct ui_file * stream, const char *format,...)
c906108c
SS
2181{
2182 va_list args;
c906108c 2183 va_start (args, format);
c906108c
SS
2184 vfprintf_unfiltered (stream, format, args);
2185 va_end (args);
2186}
2187
2188/* Like fprintf_filtered, but prints its result indented.
2189 Called as fprintfi_filtered (spaces, stream, format, ...); */
2190
c906108c 2191void
d9fcf2fb 2192fprintfi_filtered (int spaces, struct ui_file * stream, const char *format,...)
c906108c
SS
2193{
2194 va_list args;
c906108c 2195 va_start (args, format);
c906108c
SS
2196 print_spaces_filtered (spaces, stream);
2197
2198 vfprintf_filtered (stream, format, args);
2199 va_end (args);
2200}
2201
2202
c906108c 2203void
c5aa993b 2204printf_filtered (const char *format,...)
c906108c
SS
2205{
2206 va_list args;
c906108c 2207 va_start (args, format);
c906108c
SS
2208 vfprintf_filtered (gdb_stdout, format, args);
2209 va_end (args);
2210}
2211
2212
c906108c 2213void
c5aa993b 2214printf_unfiltered (const char *format,...)
c906108c
SS
2215{
2216 va_list args;
c906108c 2217 va_start (args, format);
c906108c
SS
2218 vfprintf_unfiltered (gdb_stdout, format, args);
2219 va_end (args);
2220}
2221
2222/* Like printf_filtered, but prints it's result indented.
2223 Called as printfi_filtered (spaces, format, ...); */
2224
c906108c 2225void
c5aa993b 2226printfi_filtered (int spaces, const char *format,...)
c906108c
SS
2227{
2228 va_list args;
c906108c 2229 va_start (args, format);
c906108c
SS
2230 print_spaces_filtered (spaces, gdb_stdout);
2231 vfprintf_filtered (gdb_stdout, format, args);
2232 va_end (args);
2233}
2234
2235/* Easy -- but watch out!
2236
2237 This routine is *not* a replacement for puts()! puts() appends a newline.
2238 This one doesn't, and had better not! */
2239
2240void
fba45db2 2241puts_filtered (const char *string)
c906108c
SS
2242{
2243 fputs_filtered (string, gdb_stdout);
2244}
2245
2246void
fba45db2 2247puts_unfiltered (const char *string)
c906108c
SS
2248{
2249 fputs_unfiltered (string, gdb_stdout);
2250}
2251
2252/* Return a pointer to N spaces and a null. The pointer is good
2253 until the next call to here. */
2254char *
fba45db2 2255n_spaces (int n)
c906108c 2256{
392a587b
JM
2257 char *t;
2258 static char *spaces = 0;
2259 static int max_spaces = -1;
c906108c
SS
2260
2261 if (n > max_spaces)
2262 {
2263 if (spaces)
b8c9b27d 2264 xfree (spaces);
c5aa993b
JM
2265 spaces = (char *) xmalloc (n + 1);
2266 for (t = spaces + n; t != spaces;)
c906108c
SS
2267 *--t = ' ';
2268 spaces[n] = '\0';
2269 max_spaces = n;
2270 }
2271
2272 return spaces + max_spaces - n;
2273}
2274
2275/* Print N spaces. */
2276void
fba45db2 2277print_spaces_filtered (int n, struct ui_file *stream)
c906108c
SS
2278{
2279 fputs_filtered (n_spaces (n), stream);
2280}
2281\f
4a351cef 2282/* C++/ObjC demangler stuff. */
c906108c 2283
389e51db
AC
2284/* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
2285 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
2286 If the name is not mangled, or the language for the name is unknown, or
2287 demangling is off, the name is printed in its "raw" form. */
c906108c
SS
2288
2289void
389e51db 2290fprintf_symbol_filtered (struct ui_file *stream, char *name, enum language lang,
fba45db2 2291 int arg_mode)
c906108c
SS
2292{
2293 char *demangled;
2294
2295 if (name != NULL)
2296 {
2297 /* If user wants to see raw output, no problem. */
2298 if (!demangle)
2299 {
2300 fputs_filtered (name, stream);
2301 }
2302 else
2303 {
2304 switch (lang)
2305 {
2306 case language_cplus:
2307 demangled = cplus_demangle (name, arg_mode);
2308 break;
2309 case language_java:
2310 demangled = cplus_demangle (name, arg_mode | DMGL_JAVA);
2311 break;
4a351cef
AF
2312 case language_objc:
2313 /* Commented out until ObjC handling is enabled. */
2314 /*demangled = objc_demangle (name);*/
2315 /*break;*/
db034ac5
AC
2316#if 0
2317 /* OBSOLETE case language_chill: */
2318 /* OBSOLETE demangled = chill_demangle (name); */
2319 /* OBSOLETE break; */
2320#endif
c906108c
SS
2321 default:
2322 demangled = NULL;
2323 break;
2324 }
2325 fputs_filtered (demangled ? demangled : name, stream);
2326 if (demangled != NULL)
2327 {
b8c9b27d 2328 xfree (demangled);
c906108c
SS
2329 }
2330 }
2331 }
2332}
2333
2334/* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
2335 differences in whitespace. Returns 0 if they match, non-zero if they
2336 don't (slightly different than strcmp()'s range of return values).
c5aa993b 2337
c906108c
SS
2338 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2339 This "feature" is useful when searching for matching C++ function names
2340 (such as if the user types 'break FOO', where FOO is a mangled C++
2341 function). */
2342
2343int
fba45db2 2344strcmp_iw (const char *string1, const char *string2)
c906108c
SS
2345{
2346 while ((*string1 != '\0') && (*string2 != '\0'))
2347 {
2348 while (isspace (*string1))
2349 {
2350 string1++;
2351 }
2352 while (isspace (*string2))
2353 {
2354 string2++;
2355 }
2356 if (*string1 != *string2)
2357 {
2358 break;
2359 }
2360 if (*string1 != '\0')
2361 {
2362 string1++;
2363 string2++;
2364 }
2365 }
2366 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
2367}
c906108c 2368\f
c5aa993b 2369
c906108c 2370/*
c5aa993b
JM
2371 ** subset_compare()
2372 ** Answer whether string_to_compare is a full or partial match to
2373 ** template_string. The partial match must be in sequence starting
2374 ** at index 0.
2375 */
c906108c 2376int
fba45db2 2377subset_compare (char *string_to_compare, char *template_string)
7a292a7a
SS
2378{
2379 int match;
c5aa993b
JM
2380 if (template_string != (char *) NULL && string_to_compare != (char *) NULL &&
2381 strlen (string_to_compare) <= strlen (template_string))
2382 match = (strncmp (template_string,
2383 string_to_compare,
2384 strlen (string_to_compare)) == 0);
7a292a7a
SS
2385 else
2386 match = 0;
2387 return match;
2388}
c906108c
SS
2389
2390
a14ed312 2391static void pagination_on_command (char *arg, int from_tty);
7a292a7a 2392static void
fba45db2 2393pagination_on_command (char *arg, int from_tty)
c906108c
SS
2394{
2395 pagination_enabled = 1;
2396}
2397
a14ed312 2398static void pagination_on_command (char *arg, int from_tty);
7a292a7a 2399static void
fba45db2 2400pagination_off_command (char *arg, int from_tty)
c906108c
SS
2401{
2402 pagination_enabled = 0;
2403}
c906108c 2404\f
c5aa993b 2405
c906108c 2406void
fba45db2 2407initialize_utils (void)
c906108c
SS
2408{
2409 struct cmd_list_element *c;
2410
c5aa993b
JM
2411 c = add_set_cmd ("width", class_support, var_uinteger,
2412 (char *) &chars_per_line,
2413 "Set number of characters gdb thinks are in a line.",
2414 &setlist);
c906108c 2415 add_show_from_set (c, &showlist);
9f60d481 2416 set_cmd_sfunc (c, set_width_command);
c906108c
SS
2417
2418 add_show_from_set
2419 (add_set_cmd ("height", class_support,
c5aa993b 2420 var_uinteger, (char *) &lines_per_page,
c906108c
SS
2421 "Set number of lines gdb thinks are in a page.", &setlist),
2422 &showlist);
c5aa993b 2423
c906108c
SS
2424 init_page_info ();
2425
2426 /* If the output is not a terminal, don't paginate it. */
d9fcf2fb 2427 if (!ui_file_isatty (gdb_stdout))
c906108c
SS
2428 lines_per_page = UINT_MAX;
2429
c5aa993b 2430 set_width_command ((char *) NULL, 0, c);
c906108c
SS
2431
2432 add_show_from_set
c5aa993b
JM
2433 (add_set_cmd ("demangle", class_support, var_boolean,
2434 (char *) &demangle,
4a351cef 2435 "Set demangling of encoded C++/ObjC names when displaying symbols.",
c906108c
SS
2436 &setprintlist),
2437 &showprintlist);
2438
2439 add_show_from_set
2440 (add_set_cmd ("pagination", class_support,
c5aa993b 2441 var_boolean, (char *) &pagination_enabled,
c906108c
SS
2442 "Set state of pagination.", &setlist),
2443 &showlist);
4261bedc 2444
c906108c
SS
2445 if (xdb_commands)
2446 {
c5aa993b
JM
2447 add_com ("am", class_support, pagination_on_command,
2448 "Enable pagination");
2449 add_com ("sm", class_support, pagination_off_command,
2450 "Disable pagination");
c906108c
SS
2451 }
2452
2453 add_show_from_set
c5aa993b
JM
2454 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
2455 (char *) &sevenbit_strings,
2456 "Set printing of 8-bit characters in strings as \\nnn.",
c906108c
SS
2457 &setprintlist),
2458 &showprintlist);
2459
2460 add_show_from_set
c5aa993b
JM
2461 (add_set_cmd ("asm-demangle", class_support, var_boolean,
2462 (char *) &asm_demangle,
4a351cef 2463 "Set demangling of C++/ObjC names in disassembly listings.",
c906108c
SS
2464 &setprintlist),
2465 &showprintlist);
2466}
2467
2468/* Machine specific function to handle SIGWINCH signal. */
2469
2470#ifdef SIGWINCH_HANDLER_BODY
c5aa993b 2471SIGWINCH_HANDLER_BODY
c906108c 2472#endif
39424bef 2473
5683e87a
AC
2474/* print routines to handle variable size regs, etc. */
2475
c906108c
SS
2476/* temporary storage using circular buffer */
2477#define NUMCELLS 16
2478#define CELLSIZE 32
c5aa993b 2479static char *
fba45db2 2480get_cell (void)
c906108c
SS
2481{
2482 static char buf[NUMCELLS][CELLSIZE];
c5aa993b
JM
2483 static int cell = 0;
2484 if (++cell >= NUMCELLS)
2485 cell = 0;
c906108c
SS
2486 return buf[cell];
2487}
2488
d4f3574e
SS
2489int
2490strlen_paddr (void)
2491{
79496e2f 2492 return (TARGET_ADDR_BIT / 8 * 2);
d4f3574e
SS
2493}
2494
c5aa993b 2495char *
104c1213 2496paddr (CORE_ADDR addr)
c906108c 2497{
79496e2f 2498 return phex (addr, TARGET_ADDR_BIT / 8);
c906108c
SS
2499}
2500
c5aa993b 2501char *
104c1213 2502paddr_nz (CORE_ADDR addr)
c906108c 2503{
79496e2f 2504 return phex_nz (addr, TARGET_ADDR_BIT / 8);
c906108c
SS
2505}
2506
104c1213
JM
2507static void
2508decimal2str (char *paddr_str, char *sign, ULONGEST addr)
2509{
2510 /* steal code from valprint.c:print_decimal(). Should this worry
2511 about the real size of addr as the above does? */
2512 unsigned long temp[3];
2513 int i = 0;
2514 do
2515 {
2516 temp[i] = addr % (1000 * 1000 * 1000);
2517 addr /= (1000 * 1000 * 1000);
2518 i++;
2519 }
2520 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
2521 switch (i)
2522 {
2523 case 1:
2524 sprintf (paddr_str, "%s%lu",
2525 sign, temp[0]);
2526 break;
2527 case 2:
2528 sprintf (paddr_str, "%s%lu%09lu",
2529 sign, temp[1], temp[0]);
2530 break;
2531 case 3:
2532 sprintf (paddr_str, "%s%lu%09lu%09lu",
2533 sign, temp[2], temp[1], temp[0]);
2534 break;
2535 default:
e1e9e218 2536 internal_error (__FILE__, __LINE__, "failed internal consistency check");
104c1213
JM
2537 }
2538}
2539
2540char *
2541paddr_u (CORE_ADDR addr)
2542{
2543 char *paddr_str = get_cell ();
2544 decimal2str (paddr_str, "", addr);
2545 return paddr_str;
2546}
2547
2548char *
2549paddr_d (LONGEST addr)
2550{
2551 char *paddr_str = get_cell ();
2552 if (addr < 0)
2553 decimal2str (paddr_str, "-", -addr);
2554 else
2555 decimal2str (paddr_str, "", addr);
2556 return paddr_str;
2557}
2558
5683e87a
AC
2559/* eliminate warning from compiler on 32-bit systems */
2560static int thirty_two = 32;
2561
104c1213 2562char *
5683e87a 2563phex (ULONGEST l, int sizeof_l)
104c1213 2564{
45a1e866 2565 char *str;
5683e87a 2566 switch (sizeof_l)
104c1213
JM
2567 {
2568 case 8:
45a1e866 2569 str = get_cell ();
5683e87a
AC
2570 sprintf (str, "%08lx%08lx",
2571 (unsigned long) (l >> thirty_two),
2572 (unsigned long) (l & 0xffffffff));
104c1213
JM
2573 break;
2574 case 4:
45a1e866 2575 str = get_cell ();
5683e87a 2576 sprintf (str, "%08lx", (unsigned long) l);
104c1213
JM
2577 break;
2578 case 2:
45a1e866 2579 str = get_cell ();
5683e87a 2580 sprintf (str, "%04x", (unsigned short) (l & 0xffff));
104c1213
JM
2581 break;
2582 default:
45a1e866 2583 str = phex (l, sizeof (l));
5683e87a 2584 break;
104c1213 2585 }
5683e87a 2586 return str;
104c1213
JM
2587}
2588
c5aa993b 2589char *
5683e87a 2590phex_nz (ULONGEST l, int sizeof_l)
c906108c 2591{
faf833ca 2592 char *str;
5683e87a 2593 switch (sizeof_l)
c906108c 2594 {
c5aa993b
JM
2595 case 8:
2596 {
5683e87a 2597 unsigned long high = (unsigned long) (l >> thirty_two);
faf833ca 2598 str = get_cell ();
c5aa993b 2599 if (high == 0)
5683e87a 2600 sprintf (str, "%lx", (unsigned long) (l & 0xffffffff));
c5aa993b 2601 else
5683e87a
AC
2602 sprintf (str, "%lx%08lx",
2603 high, (unsigned long) (l & 0xffffffff));
c906108c 2604 break;
c5aa993b
JM
2605 }
2606 case 4:
faf833ca 2607 str = get_cell ();
5683e87a 2608 sprintf (str, "%lx", (unsigned long) l);
c5aa993b
JM
2609 break;
2610 case 2:
faf833ca 2611 str = get_cell ();
5683e87a 2612 sprintf (str, "%x", (unsigned short) (l & 0xffff));
c5aa993b
JM
2613 break;
2614 default:
faf833ca 2615 str = phex_nz (l, sizeof (l));
5683e87a 2616 break;
c906108c 2617 }
5683e87a 2618 return str;
c906108c 2619}
ac2e2ef7
AC
2620
2621
2622/* Convert to / from the hosts pointer to GDB's internal CORE_ADDR
2623 using the target's conversion routines. */
2624CORE_ADDR
2625host_pointer_to_address (void *ptr)
2626{
c6caf090 2627 gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr));
090a2205 2628 return POINTER_TO_ADDRESS (builtin_type_void_data_ptr, &ptr);
ac2e2ef7
AC
2629}
2630
2631void *
2632address_to_host_pointer (CORE_ADDR addr)
2633{
2634 void *ptr;
c6caf090
KB
2635
2636 gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr));
090a2205 2637 ADDRESS_TO_POINTER (builtin_type_void_data_ptr, &ptr, addr);
ac2e2ef7
AC
2638 return ptr;
2639}
03dd37c3
AC
2640
2641/* Convert a CORE_ADDR into a string. */
2642const char *
2643core_addr_to_string (const CORE_ADDR addr)
49b563f9
KS
2644{
2645 char *str = get_cell ();
2646 strcpy (str, "0x");
2647 strcat (str, phex (addr, sizeof (addr)));
2648 return str;
2649}
2650
2651const char *
2652core_addr_to_string_nz (const CORE_ADDR addr)
03dd37c3
AC
2653{
2654 char *str = get_cell ();
2655 strcpy (str, "0x");
2656 strcat (str, phex_nz (addr, sizeof (addr)));
2657 return str;
2658}
2659
2660/* Convert a string back into a CORE_ADDR. */
2661CORE_ADDR
2662string_to_core_addr (const char *my_string)
2663{
2664 CORE_ADDR addr = 0;
2665 if (my_string[0] == '0' && tolower (my_string[1]) == 'x')
2666 {
2667 /* Assume that it is in decimal. */
2668 int i;
2669 for (i = 2; my_string[i] != '\0'; i++)
2670 {
2671 if (isdigit (my_string[i]))
2672 addr = (my_string[i] - '0') + (addr * 16);
2673 else if (isxdigit (my_string[i]))
2674 addr = (tolower (my_string[i]) - 'a' + 0xa) + (addr * 16);
2675 else
2676 internal_error (__FILE__, __LINE__, "invalid hex");
2677 }
2678 }
2679 else
2680 {
2681 /* Assume that it is in decimal. */
2682 int i;
2683 for (i = 0; my_string[i] != '\0'; i++)
2684 {
2685 if (isdigit (my_string[i]))
2686 addr = (my_string[i] - '0') + (addr * 10);
2687 else
2688 internal_error (__FILE__, __LINE__, "invalid decimal");
2689 }
2690 }
2691 return addr;
2692}
58d370e0
TT
2693
2694char *
2695gdb_realpath (const char *filename)
2696{
70d35819
AC
2697 /* Method 1: The system has a compile time upper bound on a filename
2698 path. Use that and realpath() to canonicalize the name. This is
2699 the most common case. Note that, if there isn't a compile time
2700 upper bound, you want to avoid realpath() at all costs. */
a4db0f07 2701#if defined(HAVE_REALPATH)
70d35819 2702 {
a4db0f07 2703# if defined (PATH_MAX)
70d35819 2704 char buf[PATH_MAX];
a4db0f07
RH
2705# define USE_REALPATH
2706# elif defined (MAXPATHLEN)
70d35819 2707 char buf[MAXPATHLEN];
a4db0f07
RH
2708# define USE_REALPATH
2709# endif
70d35819 2710# if defined (USE_REALPATH)
82c0260e 2711 const char *rp = realpath (filename, buf);
70d35819
AC
2712 if (rp == NULL)
2713 rp = filename;
2714 return xstrdup (rp);
2715 }
2716# endif
a4db0f07
RH
2717#endif /* HAVE_REALPATH */
2718
70d35819
AC
2719 /* Method 2: The host system (i.e., GNU) has the function
2720 canonicalize_file_name() which malloc's a chunk of memory and
2721 returns that, use that. */
2722#if defined(HAVE_CANONICALIZE_FILE_NAME)
2723 {
2724 char *rp = canonicalize_file_name (filename);
2725 if (rp == NULL)
2726 return xstrdup (filename);
2727 else
2728 return rp;
2729 }
58d370e0 2730#endif
70d35819 2731
6411e720
AC
2732 /* FIXME: cagney/2002-11-13:
2733
2734 Method 2a: Use realpath() with a NULL buffer. Some systems, due
2735 to the problems described in in method 3, have modified their
2736 realpath() implementation so that it will allocate a buffer when
2737 NULL is passed in. Before this can be used, though, some sort of
2738 configure time test would need to be added. Otherwize the code
2739 will likely core dump. */
2740
70d35819
AC
2741 /* Method 3: Now we're getting desperate! The system doesn't have a
2742 compile time buffer size and no alternative function. Query the
2743 OS, using pathconf(), for the buffer limit. Care is needed
2744 though, some systems do not limit PATH_MAX (return -1 for
2745 pathconf()) making it impossible to pass a correctly sized buffer
2746 to realpath() (it could always overflow). On those systems, we
2747 skip this. */
2748#if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H) && defined(HAVE_ALLOCA)
2749 {
2750 /* Find out the max path size. */
2751 long path_max = pathconf ("/", _PC_PATH_MAX);
2752 if (path_max > 0)
2753 {
2754 /* PATH_MAX is bounded. */
2755 char *buf = alloca (path_max);
2756 char *rp = realpath (filename, buf);
2757 return xstrdup (rp ? rp : filename);
2758 }
2759 }
2760#endif
2761
2762 /* This system is a lost cause, just dup the buffer. */
2763 return xstrdup (filename);
58d370e0 2764}
303c8ebd
JB
2765
2766/* Return a copy of FILENAME, with its directory prefix canonicalized
2767 by gdb_realpath. */
2768
2769char *
2770xfullpath (const char *filename)
2771{
2772 const char *base_name = lbasename (filename);
2773 char *dir_name;
2774 char *real_path;
2775 char *result;
2776
2777 /* Extract the basename of filename, and return immediately
2778 a copy of filename if it does not contain any directory prefix. */
2779 if (base_name == filename)
2780 return xstrdup (filename);
2781
2782 dir_name = alloca ((size_t) (base_name - filename + 2));
2783 /* Allocate enough space to store the dir_name + plus one extra
2784 character sometimes needed under Windows (see below), and
2785 then the closing \000 character */
2786 strncpy (dir_name, filename, base_name - filename);
2787 dir_name[base_name - filename] = '\000';
2788
2789#ifdef HAVE_DOS_BASED_FILE_SYSTEM
2790 /* We need to be careful when filename is of the form 'd:foo', which
2791 is equivalent of d:./foo, which is totally different from d:/foo. */
2792 if (strlen (dir_name) == 2 &&
2793 isalpha (dir_name[0]) && dir_name[1] == ':')
2794 {
2795 dir_name[2] = '.';
2796 dir_name[3] = '\000';
2797 }
2798#endif
2799
2800 /* Canonicalize the directory prefix, and build the resulting
2801 filename. If the dirname realpath already contains an ending
2802 directory separator, avoid doubling it. */
2803 real_path = gdb_realpath (dir_name);
2804 if (IS_DIR_SEPARATOR (real_path[strlen (real_path) - 1]))
2805 result = concat (real_path, base_name, NULL);
2806 else
2807 result = concat (real_path, SLASH_STRING, base_name, NULL);
2808
2809 xfree (real_path);
2810 return result;
2811}
This page took 0.455728 seconds and 4 git commands to generate.