error.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004, 2006 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/dynwind.h"
  24. #include "libguile/pairs.h"
  25. #include "libguile/strings.h"
  26. #include "libguile/throw.h"
  27. #include "libguile/validate.h"
  28. #include "libguile/error.h"
  29. #ifdef HAVE_STRING_H
  30. #include <string.h>
  31. #endif
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. /* For Windows... */
  36. #ifdef HAVE_IO_H
  37. #include <io.h>
  38. #endif
  39. /* {Errors and Exceptional Conditions}
  40. */
  41. /* Scheme interface to scm_error_scm. */
  42. void
  43. scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
  44. {
  45. scm_error_scm
  46. (key,
  47. (subr == NULL) ? SCM_BOOL_F : scm_from_locale_string (subr),
  48. (message == NULL) ? SCM_BOOL_F : scm_from_locale_string (message),
  49. args, rest);
  50. }
  51. /* All errors should pass through here. */
  52. SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
  53. (SCM key, SCM subr, SCM message, SCM args, SCM data),
  54. "Raise an error with key @var{key}. @var{subr} can be a string\n"
  55. "naming the procedure associated with the error, or @code{#f}.\n"
  56. "@var{message} is the error message string, possibly containing\n"
  57. "@code{~S} and @code{~A} escapes. When an error is reported,\n"
  58. "these are replaced by formatting the corresponding members of\n"
  59. "@var{args}: @code{~A} (was @code{%s} in older versions of\n"
  60. "Guile) formats using @code{display} and @code{~S} (was\n"
  61. "@code{%S}) formats using @code{write}. @var{data} is a list or\n"
  62. "@code{#f} depending on @var{key}: if @var{key} is\n"
  63. "@code{system-error} then it should be a list containing the\n"
  64. "Unix @code{errno} value; If @var{key} is @code{signal} then it\n"
  65. "should be a list containing the Unix signal number; If\n"
  66. "@var{key} is @code{out-of-range} or @code{wrong-type-arg},\n"
  67. "it is a list containing the bad value; otherwise\n"
  68. "it will usually be @code{#f}.")
  69. #define FUNC_NAME s_scm_error_scm
  70. {
  71. if (scm_gc_running_p)
  72. {
  73. /* The error occured during GC --- abort */
  74. fprintf (stderr, "Guile: error during GC.\n"),
  75. abort ();
  76. }
  77. scm_ithrow (key, scm_list_4 (subr, message, args, data), 1);
  78. /* No return, but just in case: */
  79. fprintf (stderr, "Guile scm_ithrow returned!\n");
  80. exit (1);
  81. }
  82. #undef FUNC_NAME
  83. #ifdef __MINGW32__
  84. # include "win32-socket.h"
  85. # define SCM_I_STRERROR(err) \
  86. ((err >= WSABASEERR) ? scm_i_socket_strerror (err) : strerror (err))
  87. # define SCM_I_ERRNO() \
  88. (errno ? errno : scm_i_socket_errno ())
  89. #else
  90. # define SCM_I_STRERROR(err) strerror (err)
  91. # define SCM_I_ERRNO() errno
  92. #endif /* __MINGW32__ */
  93. /* strerror may not be thread safe, for instance in glibc (version 2.3.2) an
  94. error number not among the known values results in a string like "Unknown
  95. error 9999" formed in a static buffer, which will be overwritten by a
  96. similar call in another thread. A test program running two threads with
  97. different unknown error numbers can trip this fairly quickly.
  98. Some systems don't do what glibc does, instead just giving a single
  99. "Unknown error" for unrecognised numbers. It doesn't seem worth trying
  100. to tell if that's the case, a mutex is reasonably fast, and strerror
  101. isn't needed very often.
  102. strerror_r (when available) could be used, it might be a touch faster
  103. than a frame and a mutex, though there's probably not much
  104. difference. */
  105. SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
  106. (SCM err),
  107. "Return the Unix error message corresponding to @var{err}, which\n"
  108. "must be an integer value.")
  109. #define FUNC_NAME s_scm_strerror
  110. {
  111. SCM ret;
  112. scm_dynwind_begin (0);
  113. scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
  114. ret = scm_from_locale_string (SCM_I_STRERROR (scm_to_int (err)));
  115. scm_dynwind_end ();
  116. return ret;
  117. }
  118. #undef FUNC_NAME
  119. SCM_GLOBAL_SYMBOL (scm_system_error_key, "system-error");
  120. void
  121. scm_syserror (const char *subr)
  122. {
  123. SCM err = scm_from_int (SCM_I_ERRNO ());
  124. scm_error (scm_system_error_key,
  125. subr,
  126. "~A",
  127. scm_cons (scm_strerror (err), SCM_EOL),
  128. scm_cons (err, SCM_EOL));
  129. }
  130. void
  131. scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
  132. {
  133. scm_error (scm_system_error_key,
  134. subr,
  135. message,
  136. args,
  137. scm_cons (scm_from_int (eno), SCM_EOL));
  138. }
  139. SCM_GLOBAL_SYMBOL (scm_num_overflow_key, "numerical-overflow");
  140. void
  141. scm_num_overflow (const char *subr)
  142. {
  143. scm_error (scm_num_overflow_key,
  144. subr,
  145. "Numerical overflow",
  146. SCM_BOOL_F,
  147. SCM_BOOL_F);
  148. }
  149. SCM_GLOBAL_SYMBOL (scm_out_of_range_key, "out-of-range");
  150. void
  151. scm_out_of_range (const char *subr, SCM bad_value)
  152. {
  153. scm_error (scm_out_of_range_key,
  154. subr,
  155. "Value out of range: ~S",
  156. scm_list_1 (bad_value),
  157. scm_list_1 (bad_value));
  158. }
  159. void
  160. scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
  161. {
  162. scm_error (scm_out_of_range_key,
  163. subr,
  164. "Argument ~A out of range: ~S",
  165. scm_list_2 (pos, bad_value),
  166. scm_list_1 (bad_value));
  167. }
  168. SCM_GLOBAL_SYMBOL (scm_args_number_key, "wrong-number-of-args");
  169. void
  170. scm_wrong_num_args (SCM proc)
  171. {
  172. scm_error (scm_args_number_key,
  173. NULL,
  174. "Wrong number of arguments to ~A",
  175. scm_list_1 (proc),
  176. SCM_BOOL_F);
  177. }
  178. void
  179. scm_error_num_args_subr (const char *subr)
  180. {
  181. scm_error (scm_args_number_key,
  182. NULL,
  183. "Wrong number of arguments to ~A",
  184. scm_list_1 (scm_from_locale_string (subr)),
  185. SCM_BOOL_F);
  186. }
  187. SCM_GLOBAL_SYMBOL (scm_arg_type_key, "wrong-type-arg");
  188. void
  189. scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
  190. {
  191. scm_error (scm_arg_type_key,
  192. subr,
  193. (pos == 0) ? "Wrong type: ~S"
  194. : "Wrong type argument in position ~A: ~S",
  195. (pos == 0) ? scm_list_1 (bad_value)
  196. : scm_list_2 (scm_from_int (pos), bad_value),
  197. scm_list_1 (bad_value));
  198. }
  199. void
  200. scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
  201. {
  202. SCM msg = scm_from_locale_string (szMessage);
  203. if (pos == 0)
  204. {
  205. scm_error (scm_arg_type_key,
  206. subr, "Wrong type (expecting ~A): ~S",
  207. scm_list_2 (msg, bad_value),
  208. scm_list_1 (bad_value));
  209. }
  210. else
  211. {
  212. scm_error (scm_arg_type_key,
  213. subr,
  214. "Wrong type argument in position ~A (expecting ~A): ~S",
  215. scm_list_3 (scm_from_int (pos), msg, bad_value),
  216. scm_list_1 (bad_value));
  217. }
  218. }
  219. SCM_GLOBAL_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
  220. void
  221. scm_memory_error (const char *subr)
  222. {
  223. fprintf (stderr, "FATAL: memory error in %s\n", subr);
  224. abort ();
  225. }
  226. SCM_GLOBAL_SYMBOL (scm_misc_error_key, "misc-error");
  227. void
  228. scm_misc_error (const char *subr, const char *message, SCM args)
  229. {
  230. scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
  231. }
  232. void
  233. scm_init_error ()
  234. {
  235. #include "libguile/cpp_err_symbols.c"
  236. #include "libguile/error.x"
  237. }
  238. /*
  239. Local Variables:
  240. c-file-style: "gnu"
  241. End:
  242. */