a8ea9a15f8b7f0facfc6aa270fac64fa039d535e
[external/binutils.git] / gdb / gdbserver / utils.c
1 /* General utility routines for the remote server for GDB.
2    Copyright 1986, 1989, 1993, 1995, 1996, 1997, 1999, 2000, 2002
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "server.h"
23 #include <stdio.h>
24 #include <string.h>
25
26 /* Generally useful subroutines used throughout the program.  */
27
28 /* Print the system error message for errno, and also mention STRING
29    as the file name for which the error was encountered.
30    Then return to command level.  */
31
32 void
33 perror_with_name (char *string)
34 {
35 #ifndef STDC_HEADERS
36   extern int sys_nerr;
37   extern char *sys_errlist[];
38   extern int errno;
39 #endif
40   const char *err;
41   char *combined;
42
43   if (errno < sys_nerr)
44     err = sys_errlist[errno];
45   else
46     err = "unknown error";
47
48   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
49   strcpy (combined, string);
50   strcat (combined, ": ");
51   strcat (combined, err);
52
53   error ("%s.", combined);
54 }
55
56 /* Print an error message and return to command level.
57    STRING is the error message, used as a fprintf string,
58    and ARG is passed as an argument to it.  */
59
60 NORETURN void
61 error (const char *string,...)
62 {
63   extern jmp_buf toplevel;
64   va_list args;
65   va_start (args, string);
66   fflush (stdout);
67   vfprintf (stderr, string, args);
68   fprintf (stderr, "\n");
69   longjmp (toplevel, 1);
70 }
71
72 /* Print an error message and exit reporting failure.
73    This is for a error that we cannot continue from.
74    STRING and ARG are passed to fprintf.  */
75
76 /* VARARGS */
77 NORETURN void
78 fatal (const char *string,...)
79 {
80   va_list args;
81   va_start (args, string);
82   fprintf (stderr, "gdb: ");
83   vfprintf (stderr, string, args);
84   fprintf (stderr, "\n");
85   va_end (args);
86   exit (1);
87 }
88
89 /* VARARGS */
90 void
91 warning (const char *string,...)
92 {
93   va_list args;
94   va_start (args, string);
95   fprintf (stderr, "gdb: ");
96   vfprintf (stderr, string, args);
97   fprintf (stderr, "\n");
98   va_end (args);
99 }