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