utils.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* General utility routines for GDB, the GNU debugger.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include <stdio.h>
  18. #include <sys/ioctl.h>
  19. #include "defs.h"
  20. void error ();
  21. void fatal ();
  22. /* Chain of cleanup actions established with make_cleanup,
  23. to be executed if an error happens. */
  24. static struct cleanup *cleanup_chain;
  25. /* Nonzero means a quit has been requested. */
  26. int quit_flag;
  27. /* Nonzero means quit immediately if Control-C is typed now,
  28. rather than waiting until QUIT is executed. */
  29. int immediate_quit;
  30. /* Add a new cleanup to the cleanup_chain,
  31. and return the previous chain pointer
  32. to be passed later to do_cleanups or discard_cleanups.
  33. Args are FUNCTION to clean up with, and ARG to pass to it. */
  34. struct cleanup *
  35. make_cleanup (function, arg)
  36. void (*function) ();
  37. int arg;
  38. {
  39. register struct cleanup *new
  40. = (struct cleanup *) xmalloc (sizeof (struct cleanup));
  41. register struct cleanup *old_chain = cleanup_chain;
  42. new->next = cleanup_chain;
  43. new->function = function;
  44. new->arg = arg;
  45. cleanup_chain = new;
  46. return old_chain;
  47. }
  48. /* Discard cleanups and do the actions they describe
  49. until we get back to the point OLD_CHAIN in the cleanup_chain. */
  50. void
  51. do_cleanups (old_chain)
  52. register struct cleanup *old_chain;
  53. {
  54. register struct cleanup *ptr;
  55. while ((ptr = cleanup_chain) != old_chain)
  56. {
  57. (*ptr->function) (ptr->arg);
  58. cleanup_chain = ptr->next;
  59. free (ptr);
  60. }
  61. }
  62. /* Discard cleanups, not doing the actions they describe,
  63. until we get back to the point OLD_CHAIN in the cleanup_chain. */
  64. void
  65. discard_cleanups (old_chain)
  66. register struct cleanup *old_chain;
  67. {
  68. register struct cleanup *ptr;
  69. while ((ptr = cleanup_chain) != old_chain)
  70. {
  71. cleanup_chain = ptr->next;
  72. free (ptr);
  73. }
  74. }
  75. /* This function is useful for cleanups.
  76. Do
  77. foo = xmalloc (...);
  78. old_chain = make_cleanup (free_current_contents, &foo);
  79. to arrange to free the object thus allocated. */
  80. void
  81. free_current_contents (location)
  82. char **location;
  83. {
  84. free (*location);
  85. }
  86. /* Generally useful subroutines used throughout the program. */
  87. /* Like malloc but get error if no storage available. */
  88. xmalloc (size)
  89. int size;
  90. {
  91. register val = malloc (size);
  92. if (!val)
  93. fatal ("virtual memory exhausted.", 0);
  94. return val;
  95. }
  96. /* Like realloc but get error if no storage available. */
  97. xrealloc (ptr, size)
  98. char *ptr;
  99. int size;
  100. {
  101. register val = realloc (ptr, size);
  102. if (!val)
  103. fatal ("virtual memory exhausted.", 0);
  104. return val;
  105. }
  106. /* Print the system error message for errno, and also mention STRING
  107. as the file name for which the error was encountered.
  108. Then return to command level. */
  109. void
  110. perror_with_name (string)
  111. char *string;
  112. {
  113. extern int sys_nerr;
  114. extern char *sys_errlist[];
  115. extern int errno;
  116. char *err;
  117. char *combined;
  118. if (errno < sys_nerr)
  119. err = sys_errlist[errno];
  120. else
  121. err = "unknown error";
  122. combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  123. strcpy (combined, string);
  124. strcat (combined, ": ");
  125. strcat (combined, err);
  126. error ("%s.", combined);
  127. }
  128. void
  129. quit ()
  130. {
  131. fflush (stdout);
  132. ioctl (fileno (stdout), TIOCFLUSH, 0);
  133. error ("Quit");
  134. }
  135. /* Control C comes here */
  136. void
  137. request_quit ()
  138. {
  139. quit_flag = 1;
  140. if (immediate_quit)
  141. quit ();
  142. }
  143. /* Print an error message and return to command level.
  144. STRING is the error message, used as a fprintf string,
  145. and ARG is passed as an argument to it. */
  146. void
  147. error (string, arg1, arg2, arg3)
  148. char *string;
  149. int arg1, arg2, arg3;
  150. {
  151. fflush (stdout);
  152. fprintf (stderr, string, arg1, arg2, arg3);
  153. fprintf (stderr, "\n");
  154. return_to_top_level ();
  155. }
  156. /* Print an error message and exit reporting failure.
  157. This is for a error that we cannot continue from.
  158. STRING and ARG are passed to fprintf. */
  159. void
  160. fatal (string, arg)
  161. char *string;
  162. int arg;
  163. {
  164. fprintf (stderr, "gdb: ");
  165. fprintf (stderr, string, arg);
  166. fprintf (stderr, "\n");
  167. exit (1);
  168. }
  169. /* Make a copy of the string at PTR with SIZE characters
  170. (and add a null character at the end in the copy).
  171. Uses malloc to get the space. Returns the address of the copy. */
  172. char *
  173. savestring (ptr, size)
  174. char *ptr;
  175. int size;
  176. {
  177. register char *p = (char *) xmalloc (size + 1);
  178. bcopy (ptr, p, size);
  179. p[size] = 0;
  180. return p;
  181. }
  182. char *
  183. concat (s1, s2, s3)
  184. char *s1, *s2, *s3;
  185. {
  186. register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
  187. register char *val = (char *) xmalloc (len);
  188. strcpy (val, s1);
  189. strcat (val, s2);
  190. strcat (val, s3);
  191. return val;
  192. }
  193. void
  194. print_spaces (n, file)
  195. register int n;
  196. register FILE *file;
  197. {
  198. while (n-- > 0)
  199. fputc (' ', file);
  200. }
  201. /* Ask user a y-or-n question and return 1 iff answer is yes.
  202. Takes three args which are given to printf to print the question.
  203. The first, a control string, should end in "? ".
  204. It should not say how to answer, because we do that. */
  205. int
  206. query (ctlstr, arg1, arg2)
  207. char *ctlstr;
  208. {
  209. register int answer;
  210. /* Automatically answer "yes" if input is not from a terminal. */
  211. if (!input_from_terminal_p ())
  212. return 1;
  213. while (1)
  214. {
  215. printf (ctlstr, arg1, arg2);
  216. printf ("(y or n) ");
  217. fflush (stdout);
  218. answer = fgetc (stdin);
  219. if (answer != '\n')
  220. while (fgetc (stdin) != '\n');
  221. if (answer >= 'a')
  222. answer -= 040;
  223. if (answer == 'Y')
  224. return 1;
  225. if (answer == 'N')
  226. return 0;
  227. printf ("Please answer y or n.\n");
  228. }
  229. }
  230. /* Parse a C escape sequence. STRING_PTR points to a variable
  231. containing a pointer to the string to parse. That pointer
  232. is updated past the characters we use. The value of the
  233. escape sequence is returned.
  234. A negative value means the sequence \ newline was seen,
  235. which is supposed to be equivalent to nothing at all.
  236. If \ is followed by a null character, we return a negative
  237. value and leave the string pointer pointing at the null character.
  238. If \ is followed by 000, we return 0 and leave the string pointer
  239. after the zeros. A value of 0 does not mean end of string. */
  240. int
  241. parse_escape (string_ptr)
  242. char **string_ptr;
  243. {
  244. register int c = *(*string_ptr)++;
  245. switch (c)
  246. {
  247. case 'a':
  248. return '\a';
  249. case 'b':
  250. return '\b';
  251. case 'e':
  252. return 033;
  253. case 'f':
  254. return '\f';
  255. case 'n':
  256. return '\n';
  257. case 'r':
  258. return '\r';
  259. case 't':
  260. return '\t';
  261. case 'v':
  262. return '\v';
  263. case '\n':
  264. return -2;
  265. case 0:
  266. (*string_ptr)--;
  267. return 0;
  268. case '^':
  269. c = *(*string_ptr)++;
  270. if (c == '\\')
  271. c = parse_escape (string_ptr);
  272. if (c == '?')
  273. return 0177;
  274. return (c & 0200) | (c & 037);
  275. case '0':
  276. case '1':
  277. case '2':
  278. case '3':
  279. case '4':
  280. case '5':
  281. case '6':
  282. case '7':
  283. {
  284. register int i = c - '0';
  285. register int count = 0;
  286. while (++count < 3)
  287. {
  288. if ((c = *(*string_ptr)++) >= '0' && c <= '7')
  289. {
  290. i *= 8;
  291. i += c - '0';
  292. }
  293. else
  294. {
  295. (*string_ptr)--;
  296. break;
  297. }
  298. }
  299. return i;
  300. }
  301. default:
  302. return c;
  303. }
  304. }
  305. void
  306. printchar (ch, stream)
  307. unsigned char ch;
  308. FILE *stream;
  309. {
  310. register int c = ch;
  311. if (c < 040 || c >= 0177)
  312. {
  313. if (c == '\n')
  314. fprintf (stream, "\\n");
  315. else if (c == '\b')
  316. fprintf (stream, "\\b");
  317. else if (c == '\t')
  318. fprintf (stream, "\\t");
  319. else if (c == '\f')
  320. fprintf (stream, "\\f");
  321. else if (c == '\r')
  322. fprintf (stream, "\\r");
  323. else if (c == 033)
  324. fprintf (stream, "\\e");
  325. else if (c == '\a')
  326. fprintf (stream, "\\a");
  327. else
  328. fprintf (stream, "\\%03o", c);
  329. }
  330. else
  331. {
  332. if (c == '\\' || c == '"' || c == '\'')
  333. fputc ('\\', stream);
  334. fputc (c, stream);
  335. }
  336. }