error.c 9.2 KB

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