1 /* General utility routines for GDB, the GNU debugger.
2 Copyright (C) 1986 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
23 #include <sys/ioctl.h>
33 /* Chain of cleanup actions established with make_cleanup,
34 to be executed if an error happens. */
36 static struct cleanup
*cleanup_chain
;
38 /* Nonzero means a quit has been requested. */
42 /* Nonzero means quit immediately if Control-C is typed now,
43 rather than waiting until QUIT is executed. */
47 /* Add a new cleanup to the cleanup_chain,
48 and return the previous chain pointer
49 to be passed later to do_cleanups or discard_cleanups.
50 Args are FUNCTION to clean up with, and ARG to pass to it. */
53 make_cleanup (function
, arg
)
57 register struct cleanup
*new
58 = (struct cleanup
*) xmalloc (sizeof (struct cleanup
));
59 register struct cleanup
*old_chain
= cleanup_chain
;
61 new->next
= cleanup_chain
;
62 new->function
= function
;
69 /* Discard cleanups and do the actions they describe
70 until we get back to the point OLD_CHAIN in the cleanup_chain. */
73 do_cleanups (old_chain
)
74 register struct cleanup
*old_chain
;
76 register struct cleanup
*ptr
;
77 while ((ptr
= cleanup_chain
) != old_chain
)
79 (*ptr
->function
) (ptr
->arg
);
80 cleanup_chain
= ptr
->next
;
85 /* Discard cleanups, not doing the actions they describe,
86 until we get back to the point OLD_CHAIN in the cleanup_chain. */
89 discard_cleanups (old_chain
)
90 register struct cleanup
*old_chain
;
92 register struct cleanup
*ptr
;
93 while ((ptr
= cleanup_chain
) != old_chain
)
95 cleanup_chain
= ptr
->next
;
100 /* This function is useful for cleanups.
104 old_chain = make_cleanup (free_current_contents, &foo);
106 to arrange to free the object thus allocated. */
109 free_current_contents (location
)
115 /* Generally useful subroutines used throughout the program. */
117 /* Like malloc but get error if no storage available. */
123 register char *val
= (char *) malloc (size
);
125 fatal ("virtual memory exhausted.", 0);
129 /* Like realloc but get error if no storage available. */
136 register char *val
= (char *) realloc (ptr
, size
);
138 fatal ("virtual memory exhausted.", 0);
142 /* Print the system error message for errno, and also mention STRING
143 as the file name for which the error was encountered.
144 Then return to command level. */
147 perror_with_name (string
)
151 extern char *sys_errlist
[];
156 if (errno
< sys_nerr
)
157 err
= sys_errlist
[errno
];
159 err
= "unknown error";
161 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
162 strcpy (combined
, string
);
163 strcat (combined
, ": ");
164 strcat (combined
, err
);
166 error ("%s.", combined
);
169 /* Print the system error message for ERRCODE, and also mention STRING
170 as the file name for which the error was encountered. */
173 print_sys_errmsg (string
, errcode
)
178 extern char *sys_errlist
[];
182 if (errcode
< sys_nerr
)
183 err
= sys_errlist
[errcode
];
185 err
= "unknown error";
187 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
188 strcpy (combined
, string
);
189 strcat (combined
, ": ");
190 strcat (combined
, err
);
192 printf ("%s.\n", combined
);
200 ioctl (fileno (stdout
), TCFLSH
, 1);
201 #else /* not HAVE_TERMIO */
202 ioctl (fileno (stdout
), TIOCFLUSH
, 0);
203 #endif /* not HAVE_TERMIO */
207 error ("Quit (expect signal %d when inferior is resumed)", SIGINT
);
208 #endif /* TIOCGPGRP */
211 /* Control C comes here */
221 /* Print an error message and return to command level.
222 STRING is the error message, used as a fprintf string,
223 and ARG is passed as an argument to it. */
226 error (string
, arg1
, arg2
, arg3
)
228 int arg1
, arg2
, arg3
;
231 fprintf (stderr
, string
, arg1
, arg2
, arg3
);
232 fprintf (stderr
, "\n");
233 return_to_top_level ();
236 /* Print an error message and exit reporting failure.
237 This is for a error that we cannot continue from.
238 STRING and ARG are passed to fprintf. */
245 fprintf (stderr
, "gdb: ");
246 fprintf (stderr
, string
, arg
);
247 fprintf (stderr
, "\n");
251 /* Make a copy of the string at PTR with SIZE characters
252 (and add a null character at the end in the copy).
253 Uses malloc to get the space. Returns the address of the copy. */
256 savestring (ptr
, size
)
260 register char *p
= (char *) xmalloc (size
+ 1);
261 bcopy (ptr
, p
, size
);
270 register int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
271 register char *val
= (char *) xmalloc (len
);
279 print_spaces (n
, file
)
287 /* Ask user a y-or-n question and return 1 iff answer is yes.
288 Takes three args which are given to printf to print the question.
289 The first, a control string, should end in "? ".
290 It should not say how to answer, because we do that. */
293 query (ctlstr
, arg1
, arg2
)
298 /* Automatically answer "yes" if input is not from a terminal. */
299 if (!input_from_terminal_p ())
304 printf (ctlstr
, arg1
, arg2
);
305 printf ("(y or n) ");
307 answer
= fgetc (stdin
);
308 clearerr (stdin
); /* in case of C-d */
310 while (fgetc (stdin
) != '\n') clearerr (stdin
);
317 printf ("Please answer y or n.\n");
321 /* Parse a C escape sequence. STRING_PTR points to a variable
322 containing a pointer to the string to parse. That pointer
323 is updated past the characters we use. The value of the
324 escape sequence is returned.
326 A negative value means the sequence \ newline was seen,
327 which is supposed to be equivalent to nothing at all.
329 If \ is followed by a null character, we return a negative
330 value and leave the string pointer pointing at the null character.
332 If \ is followed by 000, we return 0 and leave the string pointer
333 after the zeros. A value of 0 does not mean end of string. */
336 parse_escape (string_ptr
)
339 register int c
= *(*string_ptr
)++;
364 c
= *(*string_ptr
)++;
366 c
= parse_escape (string_ptr
);
369 return (c
& 0200) | (c
& 037);
380 register int i
= c
- '0';
381 register int count
= 0;
384 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
402 /* Print the character CH on STREAM as part of the contents
403 of a literal string whose delimiter is QUOTER. */
406 printchar (ch
, stream
, quoter
)
412 if (c
< 040 || c
>= 0177)
415 fprintf (stream
, "\\n");
417 fprintf (stream
, "\\b");
419 fprintf (stream
, "\\t");
421 fprintf (stream
, "\\f");
423 fprintf (stream
, "\\r");
425 fprintf (stream
, "\\e");
427 fprintf (stream
, "\\a");
429 fprintf (stream
, "\\%03o", c
);
433 if (c
== '\\' || c
== quoter
)
434 fputc ('\\', stream
);