1 /* General utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #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 */
201 request_quit (ignored
)
209 /* Print an error message and return to command level.
210 STRING is the error message, used as a fprintf string,
211 and ARG is passed as an argument to it. */
214 error (string
, arg1
, arg2
, arg3
)
216 int arg1
, arg2
, arg3
;
218 extern jmp_buf toplevel
;
221 fprintf (stderr
, string
, arg1
, arg2
, arg3
);
222 fprintf (stderr
, "\n");
223 longjmp(toplevel
, 1);
226 /* Print an error message and exit reporting failure.
227 This is for a error that we cannot continue from.
228 STRING and ARG are passed to fprintf. */
235 fprintf (stderr
, "gdb: ");
236 fprintf (stderr
, string
, arg
);
237 fprintf (stderr
, "\n");
241 /* Make a copy of the string at PTR with SIZE characters
242 (and add a null character at the end in the copy).
243 Uses malloc to get the space. Returns the address of the copy. */
246 savestring (ptr
, size
)
250 register char *p
= (char *) xmalloc (size
+ 1);
251 bcopy (ptr
, p
, size
);
257 print_spaces (n
, file
)
265 /* Ask user a y-or-n question and return 1 iff answer is yes.
266 Takes three args which are given to printf to print the question.
267 The first, a control string, should end in "? ".
268 It should not say how to answer, because we do that. */
271 query (ctlstr
, arg1
, arg2
)
276 /* Automatically answer "yes" if input is not from a terminal. */
277 /***********if (!input_from_terminal_p ())
278 return 1; *************************/
282 printf (ctlstr
, arg1
, arg2
);
283 printf ("(y or n) ");
285 answer
= fgetc (stdin
);
286 clearerr (stdin
); /* in case of C-d */
288 while (fgetc (stdin
) != '\n')
296 printf ("Please answer y or n.\n");
300 /* Parse a C escape sequence. STRING_PTR points to a variable
301 containing a pointer to the string to parse. That pointer
302 is updated past the characters we use. The value of the
303 escape sequence is returned.
305 A negative value means the sequence \ newline was seen,
306 which is supposed to be equivalent to nothing at all.
308 If \ is followed by a null character, we return a negative
309 value and leave the string pointer pointing at the null character.
311 If \ is followed by 000, we return 0 and leave the string pointer
312 after the zeros. A value of 0 does not mean end of string. */
315 parse_escape (string_ptr
)
318 register int c
= *(*string_ptr
)++;
343 c
= *(*string_ptr
)++;
345 c
= parse_escape (string_ptr
);
348 return (c
& 0200) | (c
& 037);
359 register int i
= c
- '0';
360 register int count
= 0;
363 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
382 printchar (ch
, stream
)
387 if (c
< 040 || c
>= 0177)
390 fprintf (stream
, "\\n");
392 fprintf (stream
, "\\b");
394 fprintf (stream
, "\\t");
396 fprintf (stream
, "\\f");
398 fprintf (stream
, "\\r");
400 fprintf (stream
, "\\e");
402 fprintf (stream
, "\\a");
404 fprintf (stream
, "\\%03o", c
);
408 if (c
== '\\' || c
== '"' || c
== '\'')
409 fputc ('\\', stream
);