error.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004, 2006, 2010 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 License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/dynwind.h"
  26. #include "libguile/pairs.h"
  27. #include "libguile/strings.h"
  28. #include "libguile/throw.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/error.h"
  31. #ifdef HAVE_STRING_H
  32. #include <string.h>
  33. #endif
  34. #ifdef HAVE_UNISTD_H
  35. #include <unistd.h>
  36. #endif
  37. /* For Windows... */
  38. #ifdef HAVE_IO_H
  39. #include <io.h>
  40. #endif
  41. /* {Errors and Exceptional Conditions}
  42. */
  43. /* Scheme interface to scm_error_scm. */
  44. void
  45. scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
  46. {
  47. scm_error_scm
  48. (key,
  49. (subr == NULL) ? SCM_BOOL_F : scm_from_locale_string (subr),
  50. (message == NULL) ? SCM_BOOL_F : scm_from_locale_string (message),
  51. args, rest);
  52. }
  53. /* All errors should pass through here. */
  54. SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
  55. (SCM key, SCM subr, SCM message, SCM args, SCM data),
  56. "Raise an error with key @var{key}. @var{subr} can be a string\n"
  57. "naming the procedure associated with the error, or @code{#f}.\n"
  58. "@var{message} is the error message string, possibly containing\n"
  59. "@code{~S} and @code{~A} escapes. When an error is reported,\n"
  60. "these are replaced by formatting the corresponding members of\n"
  61. "@var{args}: @code{~A} (was @code{%s} in older versions of\n"
  62. "Guile) formats using @code{display} and @code{~S} (was\n"
  63. "@code{%S}) formats using @code{write}. @var{data} is a list or\n"
  64. "@code{#f} depending on @var{key}: if @var{key} is\n"
  65. "@code{system-error} then it should be a list containing the\n"
  66. "Unix @code{errno} value; If @var{key} is @code{signal} then it\n"
  67. "should be a list containing the Unix signal number; If\n"
  68. "@var{key} is @code{out-of-range} or @code{wrong-type-arg},\n"
  69. "it is a list containing the bad value; otherwise\n"
  70. "it will usually be @code{#f}.")
  71. #define FUNC_NAME s_scm_error_scm
  72. {
  73. if (scm_gc_running_p)
  74. {
  75. /* The error occured during GC --- abort */
  76. fprintf (stderr, "Guile: error during GC.\n"),
  77. abort ();
  78. }
  79. scm_ithrow (key, scm_list_4 (subr, message, args, data), 1);
  80. /* No return, but just in case: */
  81. fprintf (stderr, "Guile scm_ithrow returned!\n");
  82. exit (EXIT_FAILURE);
  83. }
  84. #undef FUNC_NAME
  85. #ifdef __MINGW32__
  86. # include "win32-socket.h"
  87. # define SCM_I_STRERROR(err) \
  88. ((err >= WSABASEERR) ? scm_i_socket_strerror (err) : strerror (err))
  89. # define SCM_I_ERRNO() \
  90. (errno ? errno : scm_i_socket_errno ())
  91. #else
  92. # define SCM_I_STRERROR(err) strerror (err)
  93. # define SCM_I_ERRNO() errno
  94. #endif /* __MINGW32__ */
  95. /* strerror may not be thread safe, for instance in glibc (version 2.3.2) an
  96. error number not among the known values results in a string like "Unknown
  97. error 9999" formed in a static buffer, which will be overwritten by a
  98. similar call in another thread. A test program running two threads with
  99. different unknown error numbers can trip this fairly quickly.
  100. Some systems don't do what glibc does, instead just giving a single
  101. "Unknown error" for unrecognised numbers. It doesn't seem worth trying
  102. to tell if that's the case, a mutex is reasonably fast, and strerror
  103. isn't needed very often.
  104. strerror_r (when available) could be used, it might be a touch faster
  105. than a frame and a mutex, though there's probably not much
  106. difference. */
  107. SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
  108. (SCM err),
  109. "Return the Unix error message corresponding to @var{err}, which\n"
  110. "must be an integer value.")
  111. #define FUNC_NAME s_scm_strerror
  112. {
  113. SCM ret;
  114. scm_dynwind_begin (0);
  115. scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
  116. ret = scm_from_locale_string (SCM_I_STRERROR (scm_to_int (err)));
  117. scm_dynwind_end ();
  118. return ret;
  119. }
  120. #undef FUNC_NAME
  121. SCM_GLOBAL_SYMBOL (scm_system_error_key, "system-error");
  122. void
  123. scm_syserror (const char *subr)
  124. {
  125. SCM err = scm_from_int (SCM_I_ERRNO ());
  126. /* It could be that we're getting here because the syscall was
  127. interrupted by a signal. In that case a signal handler might have
  128. been queued to run. The signal handler probably throws an
  129. exception.
  130. If we don't try to run the signal handler now, it will run later,
  131. which would result in two exceptions being thrown: this syserror,
  132. and then at some later time the exception thrown by the async
  133. signal handler.
  134. The problem is that we don't know if handling the signal caused an
  135. async to be queued. By this time scmsigs.c:take_signal will have
  136. written a byte on the fd, but we don't know if the signal-handling
  137. thread has read it off and queued an async.
  138. Ideally we need some API like scm_i_ensure_signals_delivered() to
  139. catch up signal delivery. Barring that, we just cross our digits
  140. and pray; it could be that we handle the signal in time, and just
  141. throw once, or it could be that we miss the deadline and throw
  142. twice.
  143. */
  144. #ifdef EINTR
  145. if (scm_to_int (err) == EINTR)
  146. SCM_ASYNC_TICK;
  147. #endif
  148. scm_error (scm_system_error_key,
  149. subr,
  150. "~A",
  151. scm_cons (scm_strerror (err), SCM_EOL),
  152. scm_cons (err, SCM_EOL));
  153. }
  154. void
  155. scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
  156. {
  157. /* See above note about the EINTR signal handling race. */
  158. #ifdef EINTR
  159. if (eno == EINTR)
  160. SCM_ASYNC_TICK;
  161. #endif
  162. scm_error (scm_system_error_key,
  163. subr,
  164. message,
  165. args,
  166. scm_cons (scm_from_int (eno), SCM_EOL));
  167. }
  168. SCM_GLOBAL_SYMBOL (scm_num_overflow_key, "numerical-overflow");
  169. void
  170. scm_num_overflow (const char *subr)
  171. {
  172. scm_error (scm_num_overflow_key,
  173. subr,
  174. "Numerical overflow",
  175. SCM_BOOL_F,
  176. SCM_BOOL_F);
  177. }
  178. SCM_GLOBAL_SYMBOL (scm_out_of_range_key, "out-of-range");
  179. void
  180. scm_out_of_range (const char *subr, SCM bad_value)
  181. {
  182. scm_error (scm_out_of_range_key,
  183. subr,
  184. "Value out of range: ~S",
  185. scm_list_1 (bad_value),
  186. scm_list_1 (bad_value));
  187. }
  188. void
  189. scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
  190. {
  191. scm_error (scm_out_of_range_key,
  192. subr,
  193. "Argument ~A out of range: ~S",
  194. scm_list_2 (pos, bad_value),
  195. scm_list_1 (bad_value));
  196. }
  197. SCM_GLOBAL_SYMBOL (scm_args_number_key, "wrong-number-of-args");
  198. void
  199. scm_wrong_num_args (SCM proc)
  200. {
  201. scm_error (scm_args_number_key,
  202. NULL,
  203. "Wrong number of arguments to ~A",
  204. scm_list_1 (proc),
  205. SCM_BOOL_F);
  206. }
  207. void
  208. scm_error_num_args_subr (const char *subr)
  209. {
  210. scm_error (scm_args_number_key,
  211. NULL,
  212. "Wrong number of arguments to ~A",
  213. scm_list_1 (scm_from_locale_string (subr)),
  214. SCM_BOOL_F);
  215. }
  216. SCM_GLOBAL_SYMBOL (scm_arg_type_key, "wrong-type-arg");
  217. void
  218. scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
  219. {
  220. scm_error (scm_arg_type_key,
  221. subr,
  222. (pos == 0) ? "Wrong type: ~S"
  223. : "Wrong type argument in position ~A: ~S",
  224. (pos == 0) ? scm_list_1 (bad_value)
  225. : scm_list_2 (scm_from_int (pos), bad_value),
  226. scm_list_1 (bad_value));
  227. }
  228. void
  229. scm_i_wrong_type_arg_symbol (SCM symbol, int pos, SCM bad_value)
  230. {
  231. scm_error_scm (scm_arg_type_key,
  232. scm_symbol_to_string (symbol),
  233. (pos == 0) ? scm_from_locale_string ("Wrong type: ~S")
  234. : scm_from_locale_string ("Wrong type argument in position ~A: ~S"),
  235. (pos == 0) ? scm_list_1 (bad_value)
  236. : scm_list_2 (scm_from_int (pos), bad_value),
  237. scm_list_1 (bad_value));
  238. scm_remember_upto_here_2 (symbol, bad_value);
  239. }
  240. void
  241. scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
  242. {
  243. SCM msg = scm_from_locale_string (szMessage);
  244. if (pos == 0)
  245. {
  246. scm_error (scm_arg_type_key,
  247. subr, "Wrong type (expecting ~A): ~S",
  248. scm_list_2 (msg, bad_value),
  249. scm_list_1 (bad_value));
  250. }
  251. else
  252. {
  253. scm_error (scm_arg_type_key,
  254. subr,
  255. "Wrong type argument in position ~A (expecting ~A): ~S",
  256. scm_list_3 (scm_from_int (pos), msg, bad_value),
  257. scm_list_1 (bad_value));
  258. }
  259. }
  260. SCM_GLOBAL_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
  261. void
  262. scm_memory_error (const char *subr)
  263. {
  264. fprintf (stderr, "FATAL: memory error in %s\n", subr);
  265. abort ();
  266. }
  267. SCM_GLOBAL_SYMBOL (scm_misc_error_key, "misc-error");
  268. void
  269. scm_misc_error (const char *subr, const char *message, SCM args)
  270. {
  271. scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
  272. }
  273. void
  274. scm_init_error ()
  275. {
  276. #include "libguile/cpp-E.c"
  277. #include "libguile/error.x"
  278. }
  279. /*
  280. Local Variables:
  281. c-file-style: "gnu"
  282. End:
  283. */