| 1 | /* Copyright (C) 1995,1996,1997,1998, 2000 Free Software Foundation, Inc. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2, or (at your option) |
| 6 | * any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; see the file COPYING. If not, write to |
| 15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 16 | * Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * As a special exception, the Free Software Foundation gives permission |
| 19 | * for additional uses of the text contained in its release of GUILE. |
| 20 | * |
| 21 | * The exception is that, if you link the GUILE library with other files |
| 22 | * to produce an executable, this does not by itself cause the |
| 23 | * resulting executable to be covered by the GNU General Public License. |
| 24 | * Your use of that executable is in no way restricted on account of |
| 25 | * linking the GUILE library code into it. |
| 26 | * |
| 27 | * This exception does not however invalidate any other reasons why |
| 28 | * the executable file might be covered by the GNU General Public License. |
| 29 | * |
| 30 | * This exception applies only to the code released by the |
| 31 | * Free Software Foundation under the name GUILE. If you copy |
| 32 | * code from other Free Software Foundation releases into a copy of |
| 33 | * GUILE, as the General Public License permits, the exception does |
| 34 | * not apply to the code that you add in this way. To avoid misleading |
| 35 | * anyone as to the status of such modified files, you must delete |
| 36 | * this exception notice from them. |
| 37 | * |
| 38 | * If you write modifications of your own for GUILE, it is your choice |
| 39 | * whether to permit this exception to apply to your modifications. |
| 40 | * If you do not wish that, delete this exception notice. */ |
| 41 | |
| 42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, |
| 43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ |
| 44 | |
| 45 | \f |
| 46 | |
| 47 | #include <stdio.h> |
| 48 | |
| 49 | #include "libguile/_scm.h" |
| 50 | #include "libguile/pairs.h" |
| 51 | #include "libguile/strings.h" |
| 52 | #include "libguile/throw.h" |
| 53 | |
| 54 | #include "libguile/validate.h" |
| 55 | #include "libguile/error.h" |
| 56 | |
| 57 | #ifdef HAVE_STRING_H |
| 58 | #include <string.h> |
| 59 | #endif |
| 60 | #ifdef HAVE_UNISTD_H |
| 61 | #include <unistd.h> |
| 62 | #endif |
| 63 | \f |
| 64 | |
| 65 | \f |
| 66 | /* {Errors and Exceptional Conditions} |
| 67 | */ |
| 68 | |
| 69 | |
| 70 | extern int errno; |
| 71 | |
| 72 | /* All errors should pass through here. */ |
| 73 | void |
| 74 | scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest) |
| 75 | { |
| 76 | SCM arg_list; |
| 77 | if (scm_gc_heap_lock) |
| 78 | { |
| 79 | /* The error occured during GC --- abort */ |
| 80 | fprintf (stderr, "Error in %s during GC: %s\n", |
| 81 | subr ? subr : "unknown function", |
| 82 | message ? message : "<empty message>"); |
| 83 | abort (); |
| 84 | } |
| 85 | arg_list = scm_listify (subr ? scm_makfrom0str (subr) : SCM_BOOL_F, |
| 86 | message ? scm_makfrom0str (message) : SCM_BOOL_F, |
| 87 | args, |
| 88 | rest, |
| 89 | SCM_UNDEFINED); |
| 90 | scm_ithrow (key, arg_list, 1); |
| 91 | |
| 92 | /* No return, but just in case: */ |
| 93 | { |
| 94 | const char msg[] = "guile:scm_error:scm_ithrow returned!\n"; |
| 95 | |
| 96 | write (2, msg, (sizeof msg) - 1); |
| 97 | } |
| 98 | exit (1); |
| 99 | } |
| 100 | |
| 101 | /* Scheme interface to scm_error. */ |
| 102 | SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0, |
| 103 | (SCM key, SCM subr, SCM message, SCM args, SCM rest), |
| 104 | "Raise an error with key @var{key}. @var{subr} can be a string naming\n" |
| 105 | "the procedure associated with the error, or @code{#f}. @var{message}\n" |
| 106 | "is the error message string, possibly containing @code{~S} and @code{~A}\n" |
| 107 | "escapes. When an error is reported, these are replaced by formating the\n" |
| 108 | "corresponding members of @var{args}: @code{~A} (was @code{%s}) formats using @code{display}\n" |
| 109 | "and @code(~S) (was @code{%S}) formats using @code{write}. @var{data} is a\n" |
| 110 | "list or @code{#f} depending on @var{key}: if @var{key} is\n" |
| 111 | "@code{system-error} then it should be a list\n" |
| 112 | "containing the Unix @code{errno} value; If @var{key} is @code{signal} then\n" |
| 113 | "it should be a list containing the Unix signal number; otherwise it\n" |
| 114 | "will usually be @code{#f}.") |
| 115 | #define FUNC_NAME s_scm_error_scm |
| 116 | { |
| 117 | char *szSubr; |
| 118 | char *szMessage; |
| 119 | SCM_VALIDATE_SYMBOL (1,key); |
| 120 | SCM_VALIDATE_NULLORROSTRING_COPY (2,subr,szSubr); |
| 121 | SCM_VALIDATE_NULLORROSTRING_COPY (3,message,szMessage); |
| 122 | SCM_COERCE_SUBSTR (message); |
| 123 | |
| 124 | scm_error (key, szSubr, szMessage, args, rest); |
| 125 | /* not reached. */ |
| 126 | } |
| 127 | #undef FUNC_NAME |
| 128 | |
| 129 | SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0, |
| 130 | (SCM err), |
| 131 | "Returns the Unix error message corresponding to @var{err}, an integer.") |
| 132 | #define FUNC_NAME s_scm_strerror |
| 133 | { |
| 134 | SCM_VALIDATE_INUM (1,err); |
| 135 | return scm_makfrom0str (strerror (SCM_INUM (err))); |
| 136 | } |
| 137 | #undef FUNC_NAME |
| 138 | |
| 139 | SCM_SYMBOL (scm_system_error_key, "system-error"); |
| 140 | void |
| 141 | scm_syserror (const char *subr) |
| 142 | { |
| 143 | int save_errno = errno; |
| 144 | |
| 145 | scm_error (scm_system_error_key, |
| 146 | subr, |
| 147 | "~A", |
| 148 | scm_cons (scm_makfrom0str (strerror (save_errno)), SCM_EOL), |
| 149 | scm_cons (SCM_MAKINUM (save_errno), SCM_EOL)); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | scm_syserror_msg (const char *subr, const char *message, SCM args, int eno) |
| 154 | { |
| 155 | scm_error (scm_system_error_key, |
| 156 | subr, |
| 157 | message, |
| 158 | args, |
| 159 | scm_cons (SCM_MAKINUM (eno), SCM_EOL)); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | #if (SCM_DEBUG_DEPRECATED == 0) |
| 164 | |
| 165 | /* scm_sysmissing is no longer used in libguile. it can probably be |
| 166 | removed after a release or two. there's a comment in NEWS about it |
| 167 | (2000-01-09). */ |
| 168 | void |
| 169 | scm_sysmissing (const char *subr) |
| 170 | { |
| 171 | #ifdef ENOSYS |
| 172 | scm_error (scm_system_error_key, |
| 173 | subr, |
| 174 | "~A", |
| 175 | scm_cons (scm_makfrom0str (strerror (ENOSYS)), SCM_EOL), |
| 176 | scm_cons (SCM_MAKINUM (ENOSYS), SCM_EOL)); |
| 177 | #else |
| 178 | scm_error (scm_system_error_key, |
| 179 | subr, |
| 180 | "Missing function", |
| 181 | SCM_BOOL_F, |
| 182 | scm_cons (SCM_MAKINUM (0), SCM_EOL)); |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | #endif /* SCM_DEBUG_DEPRECATED == 0 */ |
| 187 | |
| 188 | |
| 189 | SCM_SYMBOL (scm_num_overflow_key, "numerical-overflow"); |
| 190 | void |
| 191 | scm_num_overflow (const char *subr) |
| 192 | { |
| 193 | scm_error (scm_num_overflow_key, |
| 194 | subr, |
| 195 | "Numerical overflow", |
| 196 | SCM_BOOL_F, |
| 197 | SCM_BOOL_F); |
| 198 | } |
| 199 | |
| 200 | SCM_SYMBOL (scm_out_of_range_key, "out-of-range"); |
| 201 | void |
| 202 | scm_out_of_range (const char *subr, SCM bad_value) |
| 203 | { |
| 204 | scm_error (scm_out_of_range_key, |
| 205 | subr, |
| 206 | "Argument out of range: ~S", |
| 207 | SCM_LIST1(bad_value), |
| 208 | SCM_BOOL_F); |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos) |
| 213 | { |
| 214 | scm_error (scm_out_of_range_key, |
| 215 | subr, |
| 216 | "Argument ~S out of range: ~S", |
| 217 | SCM_LIST2(pos,bad_value), |
| 218 | SCM_BOOL_F); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | SCM_SYMBOL (scm_args_number_key, "wrong-number-of-args"); |
| 223 | void |
| 224 | scm_wrong_num_args (SCM proc) |
| 225 | { |
| 226 | scm_error (scm_args_number_key, |
| 227 | NULL, |
| 228 | "Wrong number of arguments to ~A", |
| 229 | SCM_LIST1(proc), |
| 230 | SCM_BOOL_F); |
| 231 | } |
| 232 | |
| 233 | SCM_SYMBOL (scm_arg_type_key, "wrong-type-arg"); |
| 234 | void |
| 235 | scm_wrong_type_arg (const char *subr, int pos, SCM bad_value) |
| 236 | { |
| 237 | scm_error (scm_arg_type_key, |
| 238 | subr, |
| 239 | (pos == 0) ? "Wrong type argument: ~S" |
| 240 | : "Wrong type argument in position ~A: ~S", |
| 241 | (pos == 0) ? SCM_LIST1(bad_value) |
| 242 | : SCM_LIST2(SCM_MAKINUM(pos), bad_value), |
| 243 | SCM_BOOL_F); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage) |
| 248 | { |
| 249 | SCM msg = scm_makfrom0str(szMessage); |
| 250 | if (pos == 0) { |
| 251 | scm_error (scm_arg_type_key, |
| 252 | subr, "Wrong type argument (expecting ~A): ~S", |
| 253 | SCM_LIST2(msg,bad_value), |
| 254 | SCM_BOOL_F); |
| 255 | } else { |
| 256 | scm_error (scm_arg_type_key, |
| 257 | subr, |
| 258 | "Wrong type argument in position ~A (expecting ~A): ~S", |
| 259 | SCM_LIST3(SCM_MAKINUM(pos),msg,bad_value), |
| 260 | SCM_BOOL_F); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | |
| 265 | SCM_SYMBOL (scm_memory_alloc_key, "memory-allocation-error"); |
| 266 | void |
| 267 | scm_memory_error (const char *subr) |
| 268 | { |
| 269 | scm_error (scm_memory_alloc_key, |
| 270 | subr, |
| 271 | "Memory allocation error", |
| 272 | SCM_BOOL_F, |
| 273 | SCM_BOOL_F); |
| 274 | } |
| 275 | |
| 276 | SCM_SYMBOL (scm_misc_error_key, "misc-error"); |
| 277 | void |
| 278 | scm_misc_error (const char *subr, const char *message, SCM args) |
| 279 | { |
| 280 | scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F); |
| 281 | } |
| 282 | |
| 283 | /* implements the SCM_ASSERT interface. */ |
| 284 | SCM |
| 285 | scm_wta (SCM arg, const char *pos, const char *s_subr) |
| 286 | { |
| 287 | if (!s_subr || !*s_subr) |
| 288 | s_subr = NULL; |
| 289 | if ((~0x1fL) & (long) pos) |
| 290 | { |
| 291 | /* error string supplied. */ |
| 292 | scm_misc_error (s_subr, pos, SCM_LIST1 (arg)); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | /* numerical error code. */ |
| 297 | int error = (long) pos; |
| 298 | |
| 299 | switch (error) |
| 300 | { |
| 301 | case SCM_ARGn: |
| 302 | scm_wrong_type_arg (s_subr, 0, arg); |
| 303 | case SCM_ARG1: |
| 304 | scm_wrong_type_arg (s_subr, 1, arg); |
| 305 | case SCM_ARG2: |
| 306 | scm_wrong_type_arg (s_subr, 2, arg); |
| 307 | case SCM_ARG3: |
| 308 | scm_wrong_type_arg (s_subr, 3, arg); |
| 309 | case SCM_ARG4: |
| 310 | scm_wrong_type_arg (s_subr, 4, arg); |
| 311 | case SCM_ARG5: |
| 312 | scm_wrong_type_arg (s_subr, 5, arg); |
| 313 | case SCM_ARG6: |
| 314 | scm_wrong_type_arg (s_subr, 6, arg); |
| 315 | case SCM_ARG7: |
| 316 | scm_wrong_type_arg (s_subr, 7, arg); |
| 317 | case SCM_WNA: |
| 318 | scm_wrong_num_args (arg); |
| 319 | case SCM_OUTOFRANGE: |
| 320 | scm_out_of_range (s_subr, arg); |
| 321 | case SCM_NALLOC: |
| 322 | scm_memory_error (s_subr); |
| 323 | default: |
| 324 | /* this shouldn't happen. */ |
| 325 | scm_misc_error (s_subr, "Unknown error", SCM_EOL); |
| 326 | } |
| 327 | } |
| 328 | return SCM_UNSPECIFIED; |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | scm_init_error () |
| 333 | { |
| 334 | #include "libguile/cpp_err_symbols.c" |
| 335 | #include "libguile/error.x" |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /* |
| 340 | Local Variables: |
| 341 | c-file-style: "gnu" |
| 342 | End: |
| 343 | */ |