This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / gdbserver / utils.c
1 /* General utility routines for the remote server for GDB.
2    Copyright (C) 1986, 1989, 1993 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
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.
10
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.
15
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., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "server.h"
22 #include <stdio.h>
23 #include <string.h>
24
25 /* Generally useful subroutines used throughout the program.  */
26
27 /* Print the system error message for errno, and also mention STRING
28    as the file name for which the error was encountered.
29    Then return to command level.  */
30
31 void
32 perror_with_name (string)
33      char *string;
34 {
35   extern int sys_nerr;
36   extern char *sys_errlist[];
37   extern int errno;
38   char *err;
39   char *combined;
40
41   if (errno < sys_nerr)
42     err = sys_errlist[errno];
43   else
44     err = "unknown error";
45
46   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
47   strcpy (combined, string);
48   strcat (combined, ": ");
49   strcat (combined, err);
50
51   error ("%s.", combined);
52 }
53
54 /* Print an error message and return to command level.
55    STRING is the error message, used as a fprintf string,
56    and ARG is passed as an argument to it.  */
57
58 #ifdef ANSI_PROTOTYPES
59 NORETURN void
60 error (const char *string,...)
61 #else
62 void
63 error (va_alist)
64      va_dcl
65 #endif
66 {
67   extern jmp_buf toplevel;
68   va_list args;
69 #ifdef ANSI_PROTOTYPES
70   va_start (args, string);
71 #else
72   va_start (args);
73 #endif
74   fflush (stdout);
75 #ifdef ANSI_PROTOTYPES
76   vfprintf (stderr, string, args);
77 #else
78   {
79     char *string1;
80
81     string1 = va_arg (args, char *);
82     vfprintf (stderr, string1, args);
83   }
84 #endif
85   fprintf (stderr, "\n");
86   longjmp (toplevel, 1);
87 }
88
89 /* Print an error message and exit reporting failure.
90    This is for a error that we cannot continue from.
91    STRING and ARG are passed to fprintf.  */
92
93 /* VARARGS */
94 NORETURN void
95 #ifdef ANSI_PROTOTYPES
96 fatal (char *string,...)
97 #else
98 fatal (va_alist)
99      va_dcl
100 #endif
101 {
102   va_list args;
103 #ifdef ANSI_PROTOTYPES
104   va_start (args, string);
105 #else
106   char *string;
107   va_start (args);
108   string = va_arg (args, char *);
109 #endif
110   fprintf (stderr, "gdb: ");
111   vfprintf (stderr, string, args);
112   fprintf (stderr, "\n");
113   va_end (args);
114   exit (1);
115 }