error.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001 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., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 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. #include <stdio.h>
  42. #include <errno.h>
  43. #include "libguile/_scm.h"
  44. #include "libguile/pairs.h"
  45. #include "libguile/strings.h"
  46. #include "libguile/throw.h"
  47. #include "libguile/validate.h"
  48. #include "libguile/error.h"
  49. #ifdef HAVE_STRING_H
  50. #include <string.h>
  51. #endif
  52. #ifdef HAVE_UNISTD_H
  53. #include <unistd.h>
  54. #endif
  55. /* For Windows... */
  56. #ifdef HAVE_IO_H
  57. #include <io.h>
  58. #endif
  59. /* {Errors and Exceptional Conditions}
  60. */
  61. /* All errors should pass through here. */
  62. void
  63. scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
  64. {
  65. SCM arg_list;
  66. if (scm_gc_running_p)
  67. {
  68. /* The error occured during GC --- abort */
  69. fprintf (stderr, "Error in %s during GC: %s\n",
  70. subr ? subr : "unknown function",
  71. message ? message : "<empty message>");
  72. abort ();
  73. }
  74. arg_list = scm_list_4 (subr ? scm_makfrom0str (subr) : SCM_BOOL_F,
  75. message ? scm_makfrom0str (message) : SCM_BOOL_F,
  76. args,
  77. rest);
  78. scm_ithrow (key, arg_list, 1);
  79. /* No return, but just in case: */
  80. {
  81. const char msg[] = "guile:scm_error:scm_ithrow returned!\n";
  82. write (2, msg, (sizeof msg) - 1);
  83. }
  84. exit (1);
  85. }
  86. /* Scheme interface to scm_error. */
  87. SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
  88. (SCM key, SCM subr, SCM message, SCM args, SCM data),
  89. "Raise an error with key @var{key}. @var{subr} can be a string\n"
  90. "naming the procedure associated with the error, or @code{#f}.\n"
  91. "@var{message} is the error message string, possibly containing\n"
  92. "@code{~S} and @code{~A} escapes. When an error is reported,\n"
  93. "these are replaced by formatting the corresponding members of\n"
  94. "@var{args}: @code{~A} (was @code{%s} in older versions of\n"
  95. "Guile) formats using @code{display} and @code{~S} (was\n"
  96. "@code{%S}) formats using @code{write}. @var{data} is a list or\n"
  97. "@code{#f} depending on @var{key}: if @var{key} is\n"
  98. "@code{system-error} then it should be a list containing the\n"
  99. "Unix @code{errno} value; If @var{key} is @code{signal} then it\n"
  100. "should be a list containing the Unix signal number; otherwise\n"
  101. "it will usually be @code{#f}.")
  102. #define FUNC_NAME s_scm_error_scm
  103. {
  104. char *szSubr;
  105. char *szMessage;
  106. SCM_VALIDATE_SYMBOL (1, key);
  107. if (SCM_FALSEP (subr))
  108. {
  109. szSubr = NULL;
  110. }
  111. else if (SCM_SYMBOLP (subr))
  112. {
  113. szSubr = SCM_SYMBOL_CHARS (subr);
  114. }
  115. else
  116. {
  117. SCM_VALIDATE_STRING (2, subr);
  118. SCM_STRING_COERCE_0TERMINATION_X (subr);
  119. szSubr = SCM_STRING_CHARS (subr);
  120. }
  121. if (SCM_FALSEP (message))
  122. {
  123. szMessage = NULL;
  124. }
  125. else
  126. {
  127. SCM_VALIDATE_STRING (2, message);
  128. SCM_STRING_COERCE_0TERMINATION_X (message);
  129. szMessage = SCM_STRING_CHARS (message);
  130. }
  131. scm_error (key, szSubr, szMessage, args, data);
  132. /* not reached. */
  133. }
  134. #undef FUNC_NAME
  135. SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
  136. (SCM err),
  137. "Return the Unix error message corresponding to @var{err}, which\n"
  138. "must be an integer value.")
  139. #define FUNC_NAME s_scm_strerror
  140. {
  141. SCM_VALIDATE_INUM (1,err);
  142. return scm_makfrom0str (strerror (SCM_INUM (err)));
  143. }
  144. #undef FUNC_NAME
  145. SCM_GLOBAL_SYMBOL (scm_system_error_key, "system-error");
  146. void
  147. scm_syserror (const char *subr)
  148. {
  149. int save_errno = errno;
  150. scm_error (scm_system_error_key,
  151. subr,
  152. "~A",
  153. scm_cons (scm_makfrom0str (strerror (save_errno)), SCM_EOL),
  154. scm_cons (SCM_MAKINUM (save_errno), SCM_EOL));
  155. }
  156. void
  157. scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
  158. {
  159. scm_error (scm_system_error_key,
  160. subr,
  161. message,
  162. args,
  163. scm_cons (SCM_MAKINUM (eno), SCM_EOL));
  164. }
  165. SCM_GLOBAL_SYMBOL (scm_num_overflow_key, "numerical-overflow");
  166. void
  167. scm_num_overflow (const char *subr)
  168. {
  169. scm_error (scm_num_overflow_key,
  170. subr,
  171. "Numerical overflow",
  172. SCM_BOOL_F,
  173. SCM_BOOL_F);
  174. }
  175. SCM_GLOBAL_SYMBOL (scm_out_of_range_key, "out-of-range");
  176. void
  177. scm_out_of_range (const char *subr, SCM bad_value)
  178. {
  179. scm_error (scm_out_of_range_key,
  180. subr,
  181. "Argument out of range: ~S",
  182. scm_list_1 (bad_value),
  183. SCM_BOOL_F);
  184. }
  185. void
  186. scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
  187. {
  188. scm_error (scm_out_of_range_key,
  189. subr,
  190. "Argument ~S out of range: ~S",
  191. scm_list_2 (pos,bad_value),
  192. SCM_BOOL_F);
  193. }
  194. SCM_GLOBAL_SYMBOL (scm_args_number_key, "wrong-number-of-args");
  195. void
  196. scm_wrong_num_args (SCM proc)
  197. {
  198. scm_error (scm_args_number_key,
  199. NULL,
  200. "Wrong number of arguments to ~A",
  201. scm_list_1 (proc),
  202. SCM_BOOL_F);
  203. }
  204. void
  205. scm_error_num_args_subr (const char *subr)
  206. {
  207. scm_error (scm_args_number_key,
  208. NULL,
  209. "Wrong number of arguments to ~A",
  210. scm_list_1 (scm_makfrom0str (subr)),
  211. SCM_BOOL_F);
  212. }
  213. SCM_GLOBAL_SYMBOL (scm_arg_type_key, "wrong-type-arg");
  214. void
  215. scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
  216. {
  217. scm_error (scm_arg_type_key,
  218. subr,
  219. (pos == 0) ? "Wrong type argument: ~S"
  220. : "Wrong type argument in position ~A: ~S",
  221. (pos == 0) ? scm_list_1 (bad_value)
  222. : scm_list_2 (SCM_MAKINUM (pos), bad_value),
  223. SCM_BOOL_F);
  224. }
  225. void
  226. scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
  227. {
  228. SCM msg = scm_makfrom0str(szMessage);
  229. if (pos == 0) {
  230. scm_error (scm_arg_type_key,
  231. subr, "Wrong type argument (expecting ~A): ~S",
  232. scm_list_2 (msg, bad_value),
  233. SCM_BOOL_F);
  234. } else {
  235. scm_error (scm_arg_type_key,
  236. subr,
  237. "Wrong type argument in position ~A (expecting ~A): ~S",
  238. scm_list_3 (SCM_MAKINUM (pos), msg, bad_value),
  239. SCM_BOOL_F);
  240. }
  241. }
  242. SCM_GLOBAL_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
  243. void
  244. scm_memory_error (const char *subr)
  245. {
  246. scm_error (scm_memory_alloc_key,
  247. subr,
  248. "Memory allocation error",
  249. SCM_BOOL_F,
  250. SCM_BOOL_F);
  251. }
  252. SCM_GLOBAL_SYMBOL (scm_misc_error_key, "misc-error");
  253. void
  254. scm_misc_error (const char *subr, const char *message, SCM args)
  255. {
  256. scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
  257. }
  258. #if (SCM_DEBUG_DEPRECATED == 0)
  259. SCM
  260. scm_wta (SCM arg, const char *pos, const char *s_subr)
  261. {
  262. if (!s_subr || !*s_subr)
  263. s_subr = NULL;
  264. if ((~0x1fL) & (long) pos)
  265. {
  266. /* error string supplied. */
  267. scm_misc_error (s_subr, pos, scm_list_1 (arg));
  268. }
  269. else
  270. {
  271. /* numerical error code. */
  272. scm_t_bits error = (scm_t_bits) pos;
  273. switch (error)
  274. {
  275. case SCM_ARGn:
  276. scm_wrong_type_arg (s_subr, 0, arg);
  277. case SCM_ARG1:
  278. scm_wrong_type_arg (s_subr, 1, arg);
  279. case SCM_ARG2:
  280. scm_wrong_type_arg (s_subr, 2, arg);
  281. case SCM_ARG3:
  282. scm_wrong_type_arg (s_subr, 3, arg);
  283. case SCM_ARG4:
  284. scm_wrong_type_arg (s_subr, 4, arg);
  285. case SCM_ARG5:
  286. scm_wrong_type_arg (s_subr, 5, arg);
  287. case SCM_ARG6:
  288. scm_wrong_type_arg (s_subr, 6, arg);
  289. case SCM_ARG7:
  290. scm_wrong_type_arg (s_subr, 7, arg);
  291. case SCM_WNA:
  292. scm_wrong_num_args (arg);
  293. case SCM_OUTOFRANGE:
  294. scm_out_of_range (s_subr, arg);
  295. case SCM_NALLOC:
  296. scm_memory_error (s_subr);
  297. default:
  298. /* this shouldn't happen. */
  299. scm_misc_error (s_subr, "Unknown error", SCM_EOL);
  300. }
  301. }
  302. return SCM_UNSPECIFIED;
  303. }
  304. #endif /* SCM_DEBUG_DEPRECATED == 0 */
  305. void
  306. scm_init_error ()
  307. {
  308. #include "libguile/cpp_err_symbols.c"
  309. #include "libguile/error.x"
  310. }
  311. /*
  312. Local Variables:
  313. c-file-style: "gnu"
  314. End:
  315. */