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!
22 #include <sys/ioctl.h>
28 /* Chain of cleanup actions established with make_cleanup,
29 to be executed if an error happens. */
31 static struct cleanup *cleanup_chain;
33 /* Nonzero means a quit has been requested. */
37 /* Nonzero means quit immediately if Control-C is typed now,
38 rather than waiting until QUIT is executed. */
42 /* Add a new cleanup to the cleanup_chain,
43 and return the previous chain pointer
44 to be passed later to do_cleanups or discard_cleanups.
45 Args are FUNCTION to clean up with, and ARG to pass to it. */
48 make_cleanup (function, arg)
52 register struct cleanup *new
53 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
54 register struct cleanup *old_chain = cleanup_chain;
56 new->next = cleanup_chain;
57 new->function = function;
64 /* Discard cleanups and do the actions they describe
65 until we get back to the point OLD_CHAIN in the cleanup_chain. */
68 do_cleanups (old_chain)
69 register struct cleanup *old_chain;
71 register struct cleanup *ptr;
72 while ((ptr = cleanup_chain) != old_chain)
74 (*ptr->function) (ptr->arg);
75 cleanup_chain = ptr->next;
80 /* Discard cleanups, not doing the actions they describe,
81 until we get back to the point OLD_CHAIN in the cleanup_chain. */
84 discard_cleanups (old_chain)
85 register struct cleanup *old_chain;
87 register struct cleanup *ptr;
88 while ((ptr = cleanup_chain) != old_chain)
90 cleanup_chain = ptr->next;
95 /* This function is useful for cleanups.
99 old_chain = make_cleanup (free_current_contents, &foo);
101 to arrange to free the object thus allocated. */
104 free_current_contents (location)
110 /* Generally useful subroutines used throughout the program. */
112 /* Like malloc but get error if no storage available. */
118 register char *val = (char *) malloc (size);
120 fatal ("virtual memory exhausted.", 0);
124 /* Like realloc but get error if no storage available. */
131 register char *val = (char *) realloc (ptr, size);
133 fatal ("virtual memory exhausted.", 0);
137 /* Print the system error message for errno, and also mention STRING
138 as the file name for which the error was encountered.
139 Then return to command level. */
142 perror_with_name (string)
146 extern char *sys_errlist[];
151 if (errno < sys_nerr)
152 err = sys_errlist[errno];
154 err = "unknown error";
156 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
157 strcpy (combined, string);
158 strcat (combined, ": ");
159 strcat (combined, err);
161 error ("%s.", combined);
164 /* Print the system error message for ERRCODE, and also mention STRING
165 as the file name for which the error was encountered. */
168 print_sys_errmsg (string, errcode)
173 extern char *sys_errlist[];
177 if (errcode < sys_nerr)
178 err = sys_errlist[errcode];
180 err = "unknown error";
182 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
183 strcpy (combined, string);
184 strcat (combined, ": ");
185 strcat (combined, err);
187 printf ("%s.\n", combined);
194 ioctl (fileno (stdout), TIOCFLUSH, 0);
198 /* Control C comes here */
208 /* Print an error message and return to command level.
209 STRING is the error message, used as a fprintf string,
210 and ARG is passed as an argument to it. */
213 error (string, arg1, arg2, arg3)
215 int arg1, arg2, arg3;
218 fprintf (stderr, string, arg1, arg2, arg3);
219 fprintf (stderr, "\n");
220 return_to_top_level ();
223 /* Print an error message and exit reporting failure.
224 This is for a error that we cannot continue from.
225 STRING and ARG are passed to fprintf. */
232 fprintf (stderr, "gdb: ");
233 fprintf (stderr, string, arg);
234 fprintf (stderr, "\n");
238 /* Make a copy of the string at PTR with SIZE characters
239 (and add a null character at the end in the copy).
240 Uses malloc to get the space. Returns the address of the copy. */
243 savestring (ptr, size)
247 register char *p = (char *) xmalloc (size + 1);
248 bcopy (ptr, p, size);
257 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
258 register char *val = (char *) xmalloc (len);
266 print_spaces (n, file)
274 /* Ask user a y-or-n question and return 1 iff answer is yes.
275 Takes three args which are given to printf to print the question.
276 The first, a control string, should end in "? ".
277 It should not say how to answer, because we do that. */
280 query (ctlstr, arg1, arg2)
285 /* Automatically answer "yes" if input is not from a terminal. */
286 if (!input_from_terminal_p ())
291 printf (ctlstr, arg1, arg2);
292 printf ("(y or n) ");
294 answer = fgetc (stdin);
295 clearerr (stdin); /* in case of C-d */
297 while (fgetc (stdin) != '\n') clearerr (stdin);
304 printf ("Please answer y or n.\n");
308 /* Parse a C escape sequence. STRING_PTR points to a variable
309 containing a pointer to the string to parse. That pointer
310 is updated past the characters we use. The value of the
311 escape sequence is returned.
313 A negative value means the sequence \ newline was seen,
314 which is supposed to be equivalent to nothing at all.
316 If \ is followed by a null character, we return a negative
317 value and leave the string pointer pointing at the null character.
319 If \ is followed by 000, we return 0 and leave the string pointer
320 after the zeros. A value of 0 does not mean end of string. */
323 parse_escape (string_ptr)
326 register int c = *(*string_ptr)++;
351 c = *(*string_ptr)++;
353 c = parse_escape (string_ptr);
356 return (c & 0200) | (c & 037);
367 register int i = c - '0';
368 register int count = 0;
371 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
390 printchar (ch, stream)
395 if (c < 040 || c >= 0177)
398 fprintf (stream, "\\n");
400 fprintf (stream, "\\b");
402 fprintf (stream, "\\t");
404 fprintf (stream, "\\f");
406 fprintf (stream, "\\r");
408 fprintf (stream, "\\e");
410 fprintf (stream, "\\a");
412 fprintf (stream, "\\%03o", c);
416 if (c == '\\' || c == '"' || c == '\'')
417 fputc ('\\', stream);