gdb-3.1
[external/binutils.git] / gdb / utils.c
1 /* General utility routines for GDB, the GNU debugger.
2    Copyright (C) 1986 Free Software Foundation, Inc.
3
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.
9
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.
16
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!
19 */
20
21 #include <stdio.h>
22 #include <signal.h>
23 #include <sys/ioctl.h>
24 #include <sys/param.h>
25 #include "defs.h"
26 #include "param.h"
27 #ifdef HAVE_TERMIO
28 #include <termio.h>
29 #endif
30
31 void error ();
32 void fatal ();
33
34 /* Chain of cleanup actions established with make_cleanup,
35    to be executed if an error happens.  */
36
37 static struct cleanup *cleanup_chain;
38
39 /* Nonzero means a quit has been requested.  */
40
41 int quit_flag;
42
43 /* Nonzero means quit immediately if Control-C is typed now,
44    rather than waiting until QUIT is executed.  */
45
46 int immediate_quit;
47 \f
48 /* Add a new cleanup to the cleanup_chain,
49    and return the previous chain pointer
50    to be passed later to do_cleanups or discard_cleanups.
51    Args are FUNCTION to clean up with, and ARG to pass to it.  */
52
53 struct cleanup *
54 make_cleanup (function, arg)
55      void (*function) ();
56      int arg;
57 {
58   register struct cleanup *new
59     = (struct cleanup *) xmalloc (sizeof (struct cleanup));
60   register struct cleanup *old_chain = cleanup_chain;
61
62   new->next = cleanup_chain;
63   new->function = function;
64   new->arg = arg;
65   cleanup_chain = new;
66
67   return old_chain;
68 }
69
70 /* Discard cleanups and do the actions they describe
71    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
72
73 void
74 do_cleanups (old_chain)
75      register struct cleanup *old_chain;
76 {
77   register struct cleanup *ptr;
78   while ((ptr = cleanup_chain) != old_chain)
79     {
80       (*ptr->function) (ptr->arg);
81       cleanup_chain = ptr->next;
82       free (ptr);
83     }
84 }
85
86 /* Discard cleanups, not doing the actions they describe,
87    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
88
89 void
90 discard_cleanups (old_chain)
91      register struct cleanup *old_chain;
92 {
93   register struct cleanup *ptr;
94   while ((ptr = cleanup_chain) != old_chain)
95     {
96       cleanup_chain = ptr->next;
97       free (ptr);
98     }
99 }
100
101 /* Set the cleanup_chain to 0, and return the old cleanup chain.  */
102 struct cleanup *
103 save_cleanups ()
104 {
105   struct cleanup *old_chain = cleanup_chain;
106
107   cleanup_chain = 0;
108   return old_chain;
109 }
110
111 /* Restore the cleanup chain from a previously saved chain.  */
112 void
113 restore_cleanups (chain)
114      struct cleanup *chain;
115 {
116   cleanup_chain = chain;
117 }
118
119 /* This function is useful for cleanups.
120    Do
121
122      foo = xmalloc (...);
123      old_chain = make_cleanup (free_current_contents, &foo);
124
125    to arrange to free the object thus allocated.  */
126
127 void
128 free_current_contents (location)
129      char **location;
130 {
131   free (*location);
132 }
133 \f
134 /* Generally useful subroutines used throughout the program.  */
135
136 /* Like malloc but get error if no storage available.  */
137
138 char *
139 xmalloc (size)
140      long size;
141 {
142   register char *val = (char *) malloc (size);
143   if (!val)
144     fatal ("virtual memory exhausted.", 0);
145   return val;
146 }
147
148 /* Like realloc but get error if no storage available.  */
149
150 char *
151 xrealloc (ptr, size)
152      char *ptr;
153      long size;
154 {
155   register char *val = (char *) realloc (ptr, size);
156   if (!val)
157     fatal ("virtual memory exhausted.", 0);
158   return val;
159 }
160
161 /* Print the system error message for errno, and also mention STRING
162    as the file name for which the error was encountered.
163    Then return to command level.  */
164
165 void
166 perror_with_name (string)
167      char *string;
168 {
169   extern int sys_nerr;
170   extern char *sys_errlist[];
171   extern int errno;
172   char *err;
173   char *combined;
174
175   if (errno < sys_nerr)
176     err = sys_errlist[errno];
177   else
178     err = "unknown error";
179
180   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
181   strcpy (combined, string);
182   strcat (combined, ": ");
183   strcat (combined, err);
184
185   error ("%s.", combined);
186 }
187
188 /* Print the system error message for ERRCODE, and also mention STRING
189    as the file name for which the error was encountered.  */
190
191 void
192 print_sys_errmsg (string, errcode)
193      char *string;
194      int errcode;
195 {
196   extern int sys_nerr;
197   extern char *sys_errlist[];
198   char *err;
199   char *combined;
200
201   if (errcode < sys_nerr)
202     err = sys_errlist[errcode];
203   else
204     err = "unknown error";
205
206   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
207   strcpy (combined, string);
208   strcat (combined, ": ");
209   strcat (combined, err);
210
211   printf ("%s.\n", combined);
212 }
213
214 void
215 quit ()
216 {
217   fflush (stdout);
218 #ifdef HAVE_TERMIO
219   ioctl (fileno (stdout), TCFLSH, 1);
220 #else /* not HAVE_TERMIO */
221   ioctl (fileno (stdout), TIOCFLUSH, 0);
222 #endif /* not HAVE_TERMIO */
223
224 #ifdef TIOCGPGRP
225   error ("Quit");
226 #else
227   error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
228 #endif /* TIOCGPGRP */
229 }
230
231 /* Control C comes here */
232
233 void
234 request_quit ()
235 {
236   quit_flag = 1;
237
238 #ifdef USG
239   /* Restore the signal handler */
240   signal(SIGINT, request_quit);
241 #endif
242
243   if (immediate_quit)
244     quit ();
245 }
246
247 /* Print an error message and return to command level.
248    STRING is the error message, used as a fprintf string,
249    and ARG is passed as an argument to it.  */
250
251 void
252 error (string, arg1, arg2, arg3)
253      char *string;
254      int arg1, arg2, arg3;
255 {
256   fflush (stdout);
257   fprintf (stderr, string, arg1, arg2, arg3);
258   fprintf (stderr, "\n");
259   return_to_top_level ();
260 }
261
262 /* Print an error message and exit reporting failure.
263    This is for a error that we cannot continue from.
264    STRING and ARG are passed to fprintf.  */
265
266 void
267 fatal (string, arg)
268      char *string;
269      int arg;
270 {
271   fprintf (stderr, "gdb: ");
272   fprintf (stderr, string, arg);
273   fprintf (stderr, "\n");
274   exit (1);
275 }
276
277 /* Make a copy of the string at PTR with SIZE characters
278    (and add a null character at the end in the copy).
279    Uses malloc to get the space.  Returns the address of the copy.  */
280
281 char *
282 savestring (ptr, size)
283      char *ptr;
284      int size;
285 {
286   register char *p = (char *) xmalloc (size + 1);
287   bcopy (ptr, p, size);
288   p[size] = 0;
289   return p;
290 }
291
292 char *
293 concat (s1, s2, s3)
294      char *s1, *s2, *s3;
295 {
296   register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
297   register char *val = (char *) xmalloc (len);
298   strcpy (val, s1);
299   strcat (val, s2);
300   strcat (val, s3);
301   return val;
302 }
303
304 void
305 print_spaces (n, file)
306      register int n;
307      register FILE *file;
308 {
309   while (n-- > 0)
310     fputc (' ', file);
311 }
312
313 /* Ask user a y-or-n question and return 1 iff answer is yes.
314    Takes three args which are given to printf to print the question.
315    The first, a control string, should end in "? ".
316    It should not say how to answer, because we do that.  */
317
318 int
319 query (ctlstr, arg1, arg2)
320      char *ctlstr;
321 {
322   register int answer;
323
324   /* Automatically answer "yes" if input is not from a terminal.  */
325   if (!input_from_terminal_p ())
326     return 1;
327
328   while (1)
329     {
330       printf (ctlstr, arg1, arg2);
331       printf ("(y or n) ");
332       fflush (stdout);
333       answer = fgetc (stdin);
334       clearerr (stdin);         /* in case of C-d */
335       if (answer != '\n')
336         while (fgetc (stdin) != '\n') clearerr (stdin);
337       if (answer >= 'a')
338         answer -= 040;
339       if (answer == 'Y')
340         return 1;
341       if (answer == 'N')
342         return 0;
343       printf ("Please answer y or n.\n");
344     }
345 }
346 \f
347 /* Parse a C escape sequence.  STRING_PTR points to a variable
348    containing a pointer to the string to parse.  That pointer
349    is updated past the characters we use.  The value of the
350    escape sequence is returned.
351
352    A negative value means the sequence \ newline was seen,
353    which is supposed to be equivalent to nothing at all.
354
355    If \ is followed by a null character, we return a negative
356    value and leave the string pointer pointing at the null character.
357
358    If \ is followed by 000, we return 0 and leave the string pointer
359    after the zeros.  A value of 0 does not mean end of string.  */
360
361 int
362 parse_escape (string_ptr)
363      char **string_ptr;
364 {
365   register int c = *(*string_ptr)++;
366   switch (c)
367     {
368     case 'a':
369       return '\a';
370     case 'b':
371       return '\b';
372     case 'e':
373       return 033;
374     case 'f':
375       return '\f';
376     case 'n':
377       return '\n';
378     case 'r':
379       return '\r';
380     case 't':
381       return '\t';
382     case 'v':
383       return '\v';
384     case '\n':
385       return -2;
386     case 0:
387       (*string_ptr)--;
388       return 0;
389     case '^':
390       c = *(*string_ptr)++;
391       if (c == '\\')
392         c = parse_escape (string_ptr);
393       if (c == '?')
394         return 0177;
395       return (c & 0200) | (c & 037);
396       
397     case '0':
398     case '1':
399     case '2':
400     case '3':
401     case '4':
402     case '5':
403     case '6':
404     case '7':
405       {
406         register int i = c - '0';
407         register int count = 0;
408         while (++count < 3)
409           {
410             if ((c = *(*string_ptr)++) >= '0' && c <= '7')
411               {
412                 i *= 8;
413                 i += c - '0';
414               }
415             else
416               {
417                 (*string_ptr)--;
418                 break;
419               }
420           }
421         return i;
422       }
423     default:
424       return c;
425     }
426 }
427 \f
428 /* Print the character CH on STREAM as part of the contents
429    of a literal string whose delimiter is QUOTER.  */
430
431 void
432 printchar (ch, stream, quoter)
433      unsigned char ch;
434      FILE *stream;
435      int quoter;
436 {
437   register int c = ch;
438   if (c < 040 || c >= 0177)
439     {
440       if (c == '\n')
441         fprintf (stream, "\\n");
442       else if (c == '\b')
443         fprintf (stream, "\\b");
444       else if (c == '\t')
445         fprintf (stream, "\\t");
446       else if (c == '\f')
447         fprintf (stream, "\\f");
448       else if (c == '\r')
449         fprintf (stream, "\\r");
450       else if (c == 033)
451         fprintf (stream, "\\e");
452       else if (c == '\a')
453         fprintf (stream, "\\a");
454       else
455         fprintf (stream, "\\%03o", c);
456     }
457   else
458     {
459       if (c == '\\' || c == quoter)
460         fputc ('\\', stream);
461       fputc (c, stream);
462     }
463 }
464
465 \f
466 #ifdef USG
467 bcopy (from, to, count)
468 char *from, *to;
469 {
470         memcpy (to, from, count);
471 }
472
473 bcmp (from, to, count)
474 {
475         return (memcmp (to, from, count));
476 }
477
478 bzero (to, count)
479 char *to;
480 {
481         while (count--)
482                 *to++ = 0;
483 }
484
485 getwd (buf)
486 char *buf;
487 {
488   getcwd (buf, MAXPATHLEN);
489 }
490
491 char *
492 index (s, c)
493      char *s;
494 {
495   char *strchr ();
496   return strchr (s, c);
497 }
498
499 char *
500 rindex (s, c)
501      char *s;
502 {
503   char *strrchr ();
504   return strrchr (s, c);
505 }
506
507 /* Queue routines */
508
509 struct queue {
510         struct queue *forw;
511         struct queue *back;
512 };
513
514 insque (item, after)
515 struct queue *item;
516 struct queue *after;
517 {
518         item->forw = after->forw;
519         after->forw->back = item;
520
521         item->back = after;
522         after->forw = item;
523 }
524
525 remque (item)
526 struct queue *item;
527 {
528         item->forw->back = item->back;
529         item->back->forw = item->forw;
530 }
531
532
533 /*
534  * There is too much variation in Sys V signal numbers and names, so
535  * we must initialize them at runtime.  If C provided a way to initialize
536  * an array based on subscript and value, this would not be necessary.
537  */
538 static char undoc[] = "(undocumented)";
539
540 char *sys_siglist[NSIG];
541
542 _initialize_utils()
543 {
544         int i;
545         
546         for (i = 0; i < NSIG; i++)
547                 sys_siglist[i] = undoc;
548
549 #ifdef SIGHUP
550         sys_siglist[SIGHUP      ] = "SIGHUP";
551 #endif
552 #ifdef SIGINT
553         sys_siglist[SIGINT      ] = "SIGINT";
554 #endif
555 #ifdef SIGQUIT
556         sys_siglist[SIGQUIT     ] = "SIGQUIT";
557 #endif
558 #ifdef SIGILL
559         sys_siglist[SIGILL      ] = "SIGILL";
560 #endif
561 #ifdef SIGTRAP
562         sys_siglist[SIGTRAP     ] = "SIGTRAP";
563 #endif
564 #ifdef SIGIOT
565         sys_siglist[SIGIOT      ] = "SIGIOT";
566 #endif
567 #ifdef SIGEMT
568         sys_siglist[SIGEMT      ] = "SIGEMT";
569 #endif
570 #ifdef SIGFPE
571         sys_siglist[SIGFPE      ] = "SIGFPE";
572 #endif
573 #ifdef SIGKILL
574         sys_siglist[SIGKILL     ] = "SIGKILL";
575 #endif
576 #ifdef SIGBUS
577         sys_siglist[SIGBUS      ] = "SIGBUS";
578 #endif
579 #ifdef SIGSEGV
580         sys_siglist[SIGSEGV     ] = "SIGSEGV";
581 #endif
582 #ifdef SIGSYS
583         sys_siglist[SIGSYS      ] = "SIGSYS";
584 #endif
585 #ifdef SIGPIPE
586         sys_siglist[SIGPIPE     ] = "SIGPIPE";
587 #endif
588 #ifdef SIGALRM
589         sys_siglist[SIGALRM     ] = "SIGALRM";
590 #endif
591 #ifdef SIGTERM
592         sys_siglist[SIGTERM     ] = "SIGTERM";
593 #endif
594 #ifdef SIGUSR1
595         sys_siglist[SIGUSR1     ] = "SIGUSR1";
596 #endif
597 #ifdef SIGUSR2
598         sys_siglist[SIGUSR2     ] = "SIGUSR2";
599 #endif
600 #ifdef SIGCLD
601         sys_siglist[SIGCLD      ] = "SIGCLD";
602 #endif
603 #ifdef SIGCHLD
604         sys_siglist[SIGCHLD     ] = "SIGCHLD";
605 #endif
606 #ifdef SIGPWR
607         sys_siglist[SIGPWR      ] = "SIGPWR";
608 #endif
609 #ifdef SIGTSTP
610         sys_siglist[SIGTSTP     ] = "SIGTSTP";
611 #endif
612 #ifdef SIGTTIN
613         sys_siglist[SIGTTIN     ] = "SIGTTIN";
614 #endif
615 #ifdef SIGTTOU
616         sys_siglist[SIGTTOU     ] = "SIGTTOU";
617 #endif
618 #ifdef SIGSTOP
619         sys_siglist[SIGSTOP     ] = "SIGSTOP";
620 #endif
621 #ifdef SIGXCPU
622         sys_siglist[SIGXCPU     ] = "SIGXCPU";
623 #endif
624 #ifdef SIGXFSZ
625         sys_siglist[SIGXFSZ     ] = "SIGXFSZ";
626 #endif
627 #ifdef SIGVTALRM
628         sys_siglist[SIGVTALRM   ] = "SIGVTALRM";
629 #endif
630 #ifdef SIGPROF
631         sys_siglist[SIGPROF     ] = "SIGPROF";
632 #endif
633 #ifdef SIGWINCH
634         sys_siglist[SIGWINCH    ] = "SIGWINCH";
635 #endif
636 #ifdef SIGCONT
637         sys_siglist[SIGCONT     ] = "SIGCONT";
638 #endif
639 #ifdef SIGURG
640         sys_siglist[SIGURG      ] = "SIGURG";
641 #endif
642 #ifdef SIGIO
643         sys_siglist[SIGIO       ] = "SIGIO";
644 #endif
645 #ifdef SIGWIND
646         sys_siglist[SIGWIND     ] = "SIGWIND";
647 #endif
648 #ifdef SIGPHONE
649         sys_siglist[SIGPHONE    ] = "SIGPHONE";
650 #endif
651 #ifdef SIGPOLL
652         sys_siglist[SIGPOLL     ] = "SIGPOLL";
653 #endif
654 }
655 #endif /* USG */
656