error.c 8.9 KB

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