(no commit message)
[geda-pcb/pcjc2.git] / src / error.c
blob4e802a4ea2f691de3673abedce21e7a231c91f48
1 /*
2 * COPYRIGHT
4 * PCB, interactive printed circuit board design
5 * Copyright (C) 1994,1995,1996, 2005 Thomas Nau
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Contact addresses for paper mail and Email:
22 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
23 * Thomas.Nau@rz.uni-ulm.de
28 /* error and debug functions
29 * getpid() needs a cast to (int) to get rid of compiler warnings
30 * on several architectures
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <stdarg.h>
41 #include <signal.h>
42 #ifdef HAVE_STRING_H
43 #include <string.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <fcntl.h>
50 #include "global.h"
52 #include "data.h"
53 #include "error.h"
54 #include "file.h"
56 #include "misc.h"
58 #ifdef HAVE_LIBDMALLOC
59 #include <dmalloc.h>
60 #endif
62 #define utf8_dup_string(a,b) *(a) = strdup(b)
64 /* ----------------------------------------------------------------------
65 * some external identifiers
68 #if !defined(HAVE_STRERROR)
69 extern int sys_nerr; /* number of messages available from array */
70 #define USE_SYS_ERRLIST
71 #endif
73 /* the list is already defined for some OS */
74 #if !defined(__NetBSD__) && !defined(__FreeBSD__) && !defined(__linux__) && !defined(__DragonFly__)
75 #ifdef USE_SYS_ERRLIST
76 extern char *sys_errlist[]; /* array of error messages */
77 #endif
78 #endif
81 /* ---------------------------------------------------------------------------
82 * output of message in a dialog window or log window
84 void
85 Message (const char *Format, ...)
87 va_list args;
88 va_start (args, Format);
89 gui->logv (Format, args);
90 va_end (args);
94 /* ---------------------------------------------------------------------------
95 * print standard 'open error'
97 void
98 OpenErrorMessage (char *Filename)
100 char *utf8 = NULL;
102 utf8_dup_string (&utf8, Filename);
103 #ifdef USE_SYS_ERRLIST
104 Message (_("Can't open file\n"
105 " '%s'\nfopen() returned: '%s'\n"),
106 utf8, errno <= sys_nerr ? sys_errlist[errno] : "???");
107 #else
108 Message (_("Can't open file\n"
109 " '%s'\nfopen() returned: '%s'\n"), utf8, strerror (errno));
110 #endif
111 free (utf8);
114 /* ---------------------------------------------------------------------------
115 * print standard 'popen error'
117 void
118 PopenErrorMessage (char *Filename)
120 char *utf8 = NULL;
122 utf8_dup_string (&utf8, Filename);
123 #ifdef USE_SYS_ERRLIST
124 Message (_("Can't execute command\n"
125 " '%s'\npopen() returned: '%s'\n"),
126 utf8, errno <= sys_nerr ? sys_errlist[errno] : "???");
127 #else
128 Message (_("Can't execute command\n"
129 " '%s'\npopen() returned: '%s'\n"), utf8, strerror (errno));
130 #endif
131 free (utf8);
134 /* ---------------------------------------------------------------------------
135 * print standard 'opendir'
137 void
138 OpendirErrorMessage (char *DirName)
140 char *utf8 = NULL;
142 utf8_dup_string (&utf8, DirName);
143 #ifdef USE_SYS_ERRLIST
144 Message (_("Can't scan directory\n"
145 " '%s'\nopendir() returned: '%s'\n"),
146 utf8, errno <= sys_nerr ? sys_errlist[errno] : "???");
147 #else
148 Message (_("Can't scan directory\n"
149 " '%s'\nopendir() returned: '%s'\n"), utf8, strerror (errno));
150 #endif
151 free (utf8);
154 /* ---------------------------------------------------------------------------
155 * print standard 'chdir error'
157 void
158 ChdirErrorMessage (char *DirName)
160 char *utf8 = NULL;
162 utf8_dup_string (&utf8, DirName);
163 #ifdef USE_SYS_ERRLIST
164 Message (_("Can't change working directory to\n"
165 " '%s'\nchdir() returned: '%s'\n"),
166 utf8, errno <= sys_nerr ? sys_errlist[errno] : "???");
167 #else
168 Message (_("Can't change working directory to\n"
169 " '%s'\nchdir() returned: '%s'\n"), utf8, strerror (errno));
170 #endif
171 free (utf8);
174 /* ---------------------------------------------------------------------------
175 * output of fatal error message
177 void
178 MyFatal (char *Format, ...)
180 va_list args;
182 va_start (args, Format);
184 /* try to save the layout and do some cleanup */
185 EmergencySave ();
186 fprintf (stderr, "%s (%i): fatal, ", Progname, (int) getpid ());
187 vfprintf (stderr, Format, args);
188 fflush (stderr);
189 va_end (args);
190 exit (1);
193 /* ---------------------------------------------------------------------------
194 * catches signals which abort the program
196 void
197 CatchSignal (int Signal)
199 char *s;
201 switch (Signal)
203 #ifdef SIGHUP
204 case SIGHUP:
205 s = "SIGHUP";
206 break;
207 #endif
208 case SIGINT:
209 s = "SIGINT";
210 break;
211 #ifdef SIGQUIT
212 case SIGQUIT:
213 s = "SIGQUIT";
214 break;
215 #endif
216 case SIGABRT:
217 s = "SIGABRT";
218 break;
219 case SIGTERM:
220 s = "SIGTERM";
221 break;
222 case SIGSEGV:
223 s = "SIGSEGV";
224 break;
225 default:
226 s = "unknown";
227 break;
229 MyFatal ("aborted by %s signal\n", s);