PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/error.c

https://github.com/diabolo/ruby
C | 1613 lines | 903 code | 213 blank | 497 comment | 140 complexity | cf57c2994e728bf13a2efb88d2b03e0e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /**********************************************************************
  2. error.c -
  3. $Author$
  4. created at: Mon Aug 9 16:11:34 JST 1993
  5. Copyright (C) 1993-2007 Yukihiro Matsumoto
  6. **********************************************************************/
  7. #include "ruby/ruby.h"
  8. #include "ruby/st.h"
  9. #include "ruby/encoding.h"
  10. #include "vm_core.h"
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #ifdef HAVE_STDLIB_H
  14. #include <stdlib.h>
  15. #endif
  16. #include <errno.h>
  17. #ifndef EXIT_SUCCESS
  18. #define EXIT_SUCCESS 0
  19. #endif
  20. extern const char ruby_description[];
  21. static const char *
  22. rb_strerrno(int err)
  23. {
  24. #define defined_error(name, num) if (err == num) return name;
  25. #define undefined_error(name)
  26. #include "known_errors.inc"
  27. #undef defined_error
  28. #undef undefined_error
  29. return NULL;
  30. }
  31. static int
  32. err_position_0(char *buf, long len, const char *file, int line)
  33. {
  34. if (!file) {
  35. return 0;
  36. }
  37. else if (line == 0) {
  38. return snprintf(buf, len, "%s: ", file);
  39. }
  40. else {
  41. return snprintf(buf, len, "%s:%d: ", file, line);
  42. }
  43. }
  44. static int
  45. err_position(char *buf, long len)
  46. {
  47. return err_position_0(buf, len, rb_sourcefile(), rb_sourceline());
  48. }
  49. static void
  50. err_snprintf(char *buf, long len, const char *fmt, va_list args)
  51. {
  52. long n;
  53. n = err_position(buf, len);
  54. if (len > n) {
  55. vsnprintf((char*)buf+n, len-n, fmt, args);
  56. }
  57. }
  58. static void
  59. compile_snprintf(char *buf, long len, const char *file, int line, const char *fmt, va_list args)
  60. {
  61. long n;
  62. n = err_position_0(buf, len, file, line);
  63. if (len > n) {
  64. vsnprintf((char*)buf+n, len-n, fmt, args);
  65. }
  66. }
  67. static void err_append(const char*);
  68. void
  69. rb_compile_error(const char *file, int line, const char *fmt, ...)
  70. {
  71. va_list args;
  72. char buf[BUFSIZ];
  73. va_start(args, fmt);
  74. compile_snprintf(buf, BUFSIZ, file, line, fmt, args);
  75. va_end(args);
  76. err_append(buf);
  77. }
  78. void
  79. rb_compile_error_append(const char *fmt, ...)
  80. {
  81. va_list args;
  82. char buf[BUFSIZ];
  83. va_start(args, fmt);
  84. vsnprintf(buf, BUFSIZ, fmt, args);
  85. va_end(args);
  86. err_append(buf);
  87. }
  88. static void
  89. compile_warn_print(const char *file, int line, const char *fmt, va_list args)
  90. {
  91. char buf[BUFSIZ];
  92. int len;
  93. compile_snprintf(buf, BUFSIZ, file, line, fmt, args);
  94. len = (int)strlen(buf);
  95. buf[len++] = '\n';
  96. rb_write_error2(buf, len);
  97. }
  98. void
  99. rb_compile_warn(const char *file, int line, const char *fmt, ...)
  100. {
  101. char buf[BUFSIZ];
  102. va_list args;
  103. if (NIL_P(ruby_verbose)) return;
  104. snprintf(buf, BUFSIZ, "warning: %s", fmt);
  105. va_start(args, fmt);
  106. compile_warn_print(file, line, buf, args);
  107. va_end(args);
  108. }
  109. /* rb_compile_warning() reports only in verbose mode */
  110. void
  111. rb_compile_warning(const char *file, int line, const char *fmt, ...)
  112. {
  113. char buf[BUFSIZ];
  114. va_list args;
  115. if (!RTEST(ruby_verbose)) return;
  116. snprintf(buf, BUFSIZ, "warning: %s", fmt);
  117. va_start(args, fmt);
  118. compile_warn_print(file, line, buf, args);
  119. va_end(args);
  120. }
  121. static void
  122. warn_print(const char *fmt, va_list args)
  123. {
  124. char buf[BUFSIZ];
  125. int len;
  126. err_snprintf(buf, BUFSIZ, fmt, args);
  127. len = (int)strlen(buf);
  128. buf[len++] = '\n';
  129. rb_write_error2(buf, len);
  130. }
  131. void
  132. rb_warn(const char *fmt, ...)
  133. {
  134. char buf[BUFSIZ];
  135. va_list args;
  136. if (NIL_P(ruby_verbose)) return;
  137. snprintf(buf, BUFSIZ, "warning: %s", fmt);
  138. va_start(args, fmt);
  139. warn_print(buf, args);
  140. va_end(args);
  141. }
  142. /* rb_warning() reports only in verbose mode */
  143. void
  144. rb_warning(const char *fmt, ...)
  145. {
  146. char buf[BUFSIZ];
  147. va_list args;
  148. if (!RTEST(ruby_verbose)) return;
  149. snprintf(buf, BUFSIZ, "warning: %s", fmt);
  150. va_start(args, fmt);
  151. warn_print(buf, args);
  152. va_end(args);
  153. }
  154. /*
  155. * call-seq:
  156. * warn(msg) -> nil
  157. *
  158. * Display the given message (followed by a newline) on STDERR unless
  159. * warnings are disabled (for example with the <code>-W0</code> flag).
  160. */
  161. static VALUE
  162. rb_warn_m(VALUE self, VALUE mesg)
  163. {
  164. if (!NIL_P(ruby_verbose)) {
  165. rb_io_write(rb_stderr, mesg);
  166. rb_io_write(rb_stderr, rb_default_rs);
  167. }
  168. return Qnil;
  169. }
  170. void rb_vm_bugreport(void);
  171. static void
  172. report_bug(const char *file, int line, const char *fmt, va_list args)
  173. {
  174. char buf[BUFSIZ];
  175. FILE *out = stderr;
  176. int len = err_position_0(buf, BUFSIZ, file, line);
  177. if ((ssize_t)fwrite(buf, 1, len, out) == (ssize_t)len ||
  178. (ssize_t)fwrite(buf, 1, len, (out = stdout)) == (ssize_t)len) {
  179. fputs("[BUG] ", out);
  180. vfprintf(out, fmt, args);
  181. fprintf(out, "\n%s\n\n", ruby_description);
  182. rb_vm_bugreport();
  183. fprintf(out,
  184. "[NOTE]\n"
  185. "You may have encountered a bug in the Ruby interpreter"
  186. " or extension libraries.\n"
  187. "Bug reports are welcome.\n"
  188. "For details: http://www.ruby-lang.org/bugreport.html\n\n");
  189. }
  190. }
  191. void
  192. rb_bug(const char *fmt, ...)
  193. {
  194. va_list args;
  195. va_start(args, fmt);
  196. report_bug(rb_sourcefile(), rb_sourceline(), fmt, args);
  197. va_end(args);
  198. #if defined(_WIN32) && defined(RT_VER) && RT_VER >= 80
  199. _set_abort_behavior( 0, _CALL_REPORTFAULT);
  200. #endif
  201. abort();
  202. }
  203. void
  204. rb_bug_errno(const char *mesg, int errno_arg)
  205. {
  206. if (errno_arg == 0)
  207. rb_bug("%s: errno == 0 (NOERROR)", mesg);
  208. else {
  209. const char *errno_str = rb_strerrno(errno_arg);
  210. if (errno_str)
  211. rb_bug("%s: %s (%s)", mesg, strerror(errno_arg), errno_str);
  212. else
  213. rb_bug("%s: %s (%d)", mesg, strerror(errno_arg), errno_arg);
  214. }
  215. }
  216. void
  217. rb_compile_bug(const char *file, int line, const char *fmt, ...)
  218. {
  219. va_list args;
  220. va_start(args, fmt);
  221. report_bug(file, line, fmt, args);
  222. va_end(args);
  223. abort();
  224. }
  225. static const struct types {
  226. int type;
  227. const char *name;
  228. } builtin_types[] = {
  229. {T_NIL, "nil"},
  230. {T_OBJECT, "Object"},
  231. {T_CLASS, "Class"},
  232. {T_ICLASS, "iClass"}, /* internal use: mixed-in module holder */
  233. {T_MODULE, "Module"},
  234. {T_FLOAT, "Float"},
  235. {T_STRING, "String"},
  236. {T_REGEXP, "Regexp"},
  237. {T_ARRAY, "Array"},
  238. {T_FIXNUM, "Fixnum"},
  239. {T_HASH, "Hash"},
  240. {T_STRUCT, "Struct"},
  241. {T_BIGNUM, "Bignum"},
  242. {T_FILE, "File"},
  243. {T_RATIONAL,"Rational"},
  244. {T_COMPLEX, "Complex"},
  245. {T_TRUE, "true"},
  246. {T_FALSE, "false"},
  247. {T_SYMBOL, "Symbol"}, /* :symbol */
  248. {T_DATA, "Data"}, /* internal use: wrapped C pointers */
  249. {T_MATCH, "MatchData"}, /* data of $~ */
  250. {T_NODE, "Node"}, /* internal use: syntax tree node */
  251. {T_UNDEF, "undef"}, /* internal use: #undef; should not happen */
  252. };
  253. void
  254. rb_check_type(VALUE x, int t)
  255. {
  256. const struct types *type = builtin_types;
  257. const struct types *const typeend = builtin_types +
  258. sizeof(builtin_types) / sizeof(builtin_types[0]);
  259. int xt;
  260. if (x == Qundef) {
  261. rb_bug("undef leaked to the Ruby space");
  262. }
  263. xt = TYPE(x);
  264. if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) {
  265. while (type < typeend) {
  266. if (type->type == t) {
  267. const char *etype;
  268. if (NIL_P(x)) {
  269. etype = "nil";
  270. }
  271. else if (FIXNUM_P(x)) {
  272. etype = "Fixnum";
  273. }
  274. else if (SYMBOL_P(x)) {
  275. etype = "Symbol";
  276. }
  277. else if (rb_special_const_p(x)) {
  278. etype = RSTRING_PTR(rb_obj_as_string(x));
  279. }
  280. else {
  281. etype = rb_obj_classname(x);
  282. }
  283. rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
  284. etype, type->name);
  285. }
  286. type++;
  287. }
  288. rb_bug("unknown type 0x%x (0x%x given)", t, TYPE(x));
  289. }
  290. }
  291. int
  292. rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
  293. {
  294. if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA ||
  295. !RTYPEDDATA_P(obj) || RTYPEDDATA_TYPE(obj) != data_type) {
  296. return 0;
  297. }
  298. return 1;
  299. }
  300. void *
  301. rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
  302. {
  303. const char *etype;
  304. static const char mesg[] = "wrong argument type %s (expected %s)";
  305. if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA) {
  306. Check_Type(obj, T_DATA);
  307. }
  308. if (!RTYPEDDATA_P(obj)) {
  309. etype = rb_obj_classname(obj);
  310. rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name);
  311. }
  312. else if (RTYPEDDATA_TYPE(obj) != data_type) {
  313. etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
  314. rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name);
  315. }
  316. return DATA_PTR(obj);
  317. }
  318. /* exception classes */
  319. VALUE rb_eException;
  320. VALUE rb_eSystemExit;
  321. VALUE rb_eInterrupt;
  322. VALUE rb_eSignal;
  323. VALUE rb_eFatal;
  324. VALUE rb_eStandardError;
  325. VALUE rb_eRuntimeError;
  326. VALUE rb_eTypeError;
  327. VALUE rb_eArgError;
  328. VALUE rb_eIndexError;
  329. VALUE rb_eKeyError;
  330. VALUE rb_eRangeError;
  331. VALUE rb_eNameError;
  332. VALUE rb_eEncodingError;
  333. VALUE rb_eEncCompatError;
  334. VALUE rb_eNoMethodError;
  335. VALUE rb_eSecurityError;
  336. VALUE rb_eNotImpError;
  337. VALUE rb_eNoMemError;
  338. VALUE rb_cNameErrorMesg;
  339. VALUE rb_eScriptError;
  340. VALUE rb_eSyntaxError;
  341. VALUE rb_eLoadError;
  342. VALUE rb_eSystemCallError;
  343. VALUE rb_mErrno;
  344. static VALUE rb_eNOERROR;
  345. #undef rb_exc_new2
  346. VALUE
  347. rb_exc_new(VALUE etype, const char *ptr, long len)
  348. {
  349. return rb_funcall(etype, rb_intern("new"), 1, rb_str_new(ptr, len));
  350. }
  351. VALUE
  352. rb_exc_new2(VALUE etype, const char *s)
  353. {
  354. return rb_exc_new(etype, s, strlen(s));
  355. }
  356. VALUE
  357. rb_exc_new3(VALUE etype, VALUE str)
  358. {
  359. StringValue(str);
  360. return rb_funcall(etype, rb_intern("new"), 1, str);
  361. }
  362. /*
  363. * call-seq:
  364. * Exception.new(msg = nil) -> exception
  365. *
  366. * Construct a new Exception object, optionally passing in
  367. * a message.
  368. */
  369. static VALUE
  370. exc_initialize(int argc, VALUE *argv, VALUE exc)
  371. {
  372. VALUE arg;
  373. rb_scan_args(argc, argv, "01", &arg);
  374. rb_iv_set(exc, "mesg", arg);
  375. rb_iv_set(exc, "bt", Qnil);
  376. return exc;
  377. }
  378. /*
  379. * Document-method: exception
  380. *
  381. * call-seq:
  382. * exc.exception(string) -> an_exception or exc
  383. *
  384. * With no argument, or if the argument is the same as the receiver,
  385. * return the receiver. Otherwise, create a new
  386. * exception object of the same class as the receiver, but with a
  387. * message equal to <code>string.to_str</code>.
  388. *
  389. */
  390. static VALUE
  391. exc_exception(int argc, VALUE *argv, VALUE self)
  392. {
  393. VALUE exc;
  394. if (argc == 0) return self;
  395. if (argc == 1 && self == argv[0]) return self;
  396. exc = rb_obj_clone(self);
  397. exc_initialize(argc, argv, exc);
  398. return exc;
  399. }
  400. /*
  401. * call-seq:
  402. * exception.to_s -> string
  403. *
  404. * Returns exception's message (or the name of the exception if
  405. * no message is set).
  406. */
  407. static VALUE
  408. exc_to_s(VALUE exc)
  409. {
  410. VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));
  411. if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
  412. if (OBJ_TAINTED(exc)) OBJ_TAINT(mesg);
  413. return mesg;
  414. }
  415. /*
  416. * call-seq:
  417. * exception.message -> string
  418. *
  419. * Returns the result of invoking <code>exception.to_s</code>.
  420. * Normally this returns the exception's message or name. By
  421. * supplying a to_str method, exceptions are agreeing to
  422. * be used where Strings are expected.
  423. */
  424. static VALUE
  425. exc_message(VALUE exc)
  426. {
  427. return rb_funcall(exc, rb_intern("to_s"), 0, 0);
  428. }
  429. /*
  430. * call-seq:
  431. * exception.inspect -> string
  432. *
  433. * Return this exception's class name an message
  434. */
  435. static VALUE
  436. exc_inspect(VALUE exc)
  437. {
  438. VALUE str, klass;
  439. klass = CLASS_OF(exc);
  440. exc = rb_obj_as_string(exc);
  441. if (RSTRING_LEN(exc) == 0) {
  442. return rb_str_dup(rb_class_name(klass));
  443. }
  444. str = rb_str_buf_new2("#<");
  445. klass = rb_class_name(klass);
  446. rb_str_buf_append(str, klass);
  447. rb_str_buf_cat(str, ": ", 2);
  448. rb_str_buf_append(str, exc);
  449. rb_str_buf_cat(str, ">", 1);
  450. return str;
  451. }
  452. /*
  453. * call-seq:
  454. * exception.backtrace -> array
  455. *
  456. * Returns any backtrace associated with the exception. The backtrace
  457. * is an array of strings, each containing either ``filename:lineNo: in
  458. * `method''' or ``filename:lineNo.''
  459. *
  460. * def a
  461. * raise "boom"
  462. * end
  463. *
  464. * def b
  465. * a()
  466. * end
  467. *
  468. * begin
  469. * b()
  470. * rescue => detail
  471. * print detail.backtrace.join("\n")
  472. * end
  473. *
  474. * <em>produces:</em>
  475. *
  476. * prog.rb:2:in `a'
  477. * prog.rb:6:in `b'
  478. * prog.rb:10
  479. */
  480. static VALUE
  481. exc_backtrace(VALUE exc)
  482. {
  483. ID bt;
  484. CONST_ID(bt, "bt");
  485. return rb_attr_get(exc, bt);
  486. }
  487. VALUE
  488. rb_check_backtrace(VALUE bt)
  489. {
  490. long i;
  491. static const char err[] = "backtrace must be Array of String";
  492. if (!NIL_P(bt)) {
  493. int t = TYPE(bt);
  494. if (t == T_STRING) return rb_ary_new3(1, bt);
  495. if (t != T_ARRAY) {
  496. rb_raise(rb_eTypeError, err);
  497. }
  498. for (i=0;i<RARRAY_LEN(bt);i++) {
  499. if (TYPE(RARRAY_PTR(bt)[i]) != T_STRING) {
  500. rb_raise(rb_eTypeError, err);
  501. }
  502. }
  503. }
  504. return bt;
  505. }
  506. /*
  507. * call-seq:
  508. * exc.set_backtrace(array) -> array
  509. *
  510. * Sets the backtrace information associated with <i>exc</i>. The
  511. * argument must be an array of <code>String</code> objects in the
  512. * format described in <code>Exception#backtrace</code>.
  513. *
  514. */
  515. static VALUE
  516. exc_set_backtrace(VALUE exc, VALUE bt)
  517. {
  518. return rb_iv_set(exc, "bt", rb_check_backtrace(bt));
  519. }
  520. /*
  521. * call-seq:
  522. * exc == obj -> true or false
  523. *
  524. * Equality---If <i>obj</i> is not an <code>Exception</code>, returns
  525. * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
  526. * <i>obj</i> share same class, messages, and backtrace.
  527. */
  528. static VALUE
  529. exc_equal(VALUE exc, VALUE obj)
  530. {
  531. VALUE mesg, backtrace;
  532. ID id_mesg;
  533. if (exc == obj) return Qtrue;
  534. CONST_ID(id_mesg, "mesg");
  535. if (rb_obj_class(exc) != rb_obj_class(obj)) {
  536. ID id_message, id_backtrace;
  537. CONST_ID(id_message, "message");
  538. CONST_ID(id_backtrace, "backtrace");
  539. mesg = rb_check_funcall(obj, id_message, 0, 0);
  540. if (mesg == Qundef) return Qfalse;
  541. backtrace = rb_check_funcall(obj, id_backtrace, 0, 0);
  542. if (backtrace == Qundef) return Qfalse;
  543. }
  544. else {
  545. mesg = rb_attr_get(obj, id_mesg);
  546. backtrace = exc_backtrace(obj);
  547. }
  548. if (!rb_equal(rb_attr_get(exc, id_mesg), mesg))
  549. return Qfalse;
  550. if (!rb_equal(exc_backtrace(exc), backtrace))
  551. return Qfalse;
  552. return Qtrue;
  553. }
  554. /*
  555. * call-seq:
  556. * SystemExit.new(status=0) -> system_exit
  557. *
  558. * Create a new +SystemExit+ exception with the given status.
  559. */
  560. static VALUE
  561. exit_initialize(int argc, VALUE *argv, VALUE exc)
  562. {
  563. VALUE status = INT2FIX(EXIT_SUCCESS);
  564. if (argc > 0 && FIXNUM_P(argv[0])) {
  565. status = *argv++;
  566. --argc;
  567. }
  568. rb_call_super(argc, argv);
  569. rb_iv_set(exc, "status", status);
  570. return exc;
  571. }
  572. /*
  573. * call-seq:
  574. * system_exit.status -> fixnum
  575. *
  576. * Return the status value associated with this system exit.
  577. */
  578. static VALUE
  579. exit_status(VALUE exc)
  580. {
  581. return rb_attr_get(exc, rb_intern("status"));
  582. }
  583. /*
  584. * call-seq:
  585. * system_exit.success? -> true or false
  586. *
  587. * Returns +true+ if exiting successful, +false+ if not.
  588. */
  589. static VALUE
  590. exit_success_p(VALUE exc)
  591. {
  592. VALUE status = rb_attr_get(exc, rb_intern("status"));
  593. if (NIL_P(status)) return Qtrue;
  594. if (status == INT2FIX(EXIT_SUCCESS)) return Qtrue;
  595. return Qfalse;
  596. }
  597. void
  598. rb_name_error(ID id, const char *fmt, ...)
  599. {
  600. VALUE exc, argv[2];
  601. va_list args;
  602. va_start(args, fmt);
  603. argv[0] = rb_vsprintf(fmt, args);
  604. va_end(args);
  605. argv[1] = ID2SYM(id);
  606. exc = rb_class_new_instance(2, argv, rb_eNameError);
  607. rb_exc_raise(exc);
  608. }
  609. /*
  610. * call-seq:
  611. * NameError.new(msg [, name]) -> name_error
  612. *
  613. * Construct a new NameError exception. If given the <i>name</i>
  614. * parameter may subsequently be examined using the <code>NameError.name</code>
  615. * method.
  616. */
  617. static VALUE
  618. name_err_initialize(int argc, VALUE *argv, VALUE self)
  619. {
  620. VALUE name;
  621. name = (argc > 1) ? argv[--argc] : Qnil;
  622. rb_call_super(argc, argv);
  623. rb_iv_set(self, "name", name);
  624. return self;
  625. }
  626. /*
  627. * call-seq:
  628. * name_error.name -> string or nil
  629. *
  630. * Return the name associated with this NameError exception.
  631. */
  632. static VALUE
  633. name_err_name(VALUE self)
  634. {
  635. return rb_attr_get(self, rb_intern("name"));
  636. }
  637. /*
  638. * call-seq:
  639. * name_error.to_s -> string
  640. *
  641. * Produce a nicely-formatted string representing the +NameError+.
  642. */
  643. static VALUE
  644. name_err_to_s(VALUE exc)
  645. {
  646. VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));
  647. VALUE str = mesg;
  648. if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
  649. StringValue(str);
  650. if (str != mesg) {
  651. rb_iv_set(exc, "mesg", mesg = str);
  652. }
  653. if (OBJ_TAINTED(exc)) OBJ_TAINT(mesg);
  654. return mesg;
  655. }
  656. /*
  657. * call-seq:
  658. * NoMethodError.new(msg, name [, args]) -> no_method_error
  659. *
  660. * Construct a NoMethodError exception for a method of the given name
  661. * called with the given arguments. The name may be accessed using
  662. * the <code>#name</code> method on the resulting object, and the
  663. * arguments using the <code>#args</code> method.
  664. */
  665. static VALUE
  666. nometh_err_initialize(int argc, VALUE *argv, VALUE self)
  667. {
  668. VALUE args = (argc > 2) ? argv[--argc] : Qnil;
  669. name_err_initialize(argc, argv, self);
  670. rb_iv_set(self, "args", args);
  671. return self;
  672. }
  673. /* :nodoc: */
  674. #define NAME_ERR_MESG_COUNT 3
  675. static void
  676. name_err_mesg_mark(void *p)
  677. {
  678. VALUE *ptr = p;
  679. rb_gc_mark_locations(ptr, ptr+NAME_ERR_MESG_COUNT);
  680. }
  681. #define name_err_mesg_free RUBY_TYPED_DEFAULT_FREE
  682. static size_t
  683. name_err_mesg_memsize(const void *p)
  684. {
  685. return p ? (NAME_ERR_MESG_COUNT * sizeof(VALUE)) : 0;
  686. }
  687. static const rb_data_type_t name_err_mesg_data_type = {
  688. "name_err_mesg",
  689. name_err_mesg_mark,
  690. name_err_mesg_free,
  691. name_err_mesg_memsize,
  692. };
  693. /* :nodoc: */
  694. VALUE
  695. rb_name_err_mesg_new(VALUE obj, VALUE mesg, VALUE recv, VALUE method)
  696. {
  697. VALUE *ptr = ALLOC_N(VALUE, NAME_ERR_MESG_COUNT);
  698. VALUE result;
  699. ptr[0] = mesg;
  700. ptr[1] = recv;
  701. ptr[2] = method;
  702. result = TypedData_Wrap_Struct(rb_cNameErrorMesg, &name_err_mesg_data_type, ptr);
  703. RB_GC_GUARD(mesg);
  704. RB_GC_GUARD(recv);
  705. RB_GC_GUARD(method);
  706. return result;
  707. }
  708. /* :nodoc: */
  709. static VALUE
  710. name_err_mesg_equal(VALUE obj1, VALUE obj2)
  711. {
  712. VALUE *ptr1, *ptr2;
  713. int i;
  714. if (obj1 == obj2) return Qtrue;
  715. if (rb_obj_class(obj2) != rb_cNameErrorMesg)
  716. return Qfalse;
  717. TypedData_Get_Struct(obj1, VALUE, &name_err_mesg_data_type, ptr1);
  718. TypedData_Get_Struct(obj2, VALUE, &name_err_mesg_data_type, ptr2);
  719. for (i=0; i<NAME_ERR_MESG_COUNT; i++) {
  720. if (!rb_equal(ptr1[i], ptr2[i]))
  721. return Qfalse;
  722. }
  723. return Qtrue;
  724. }
  725. /* :nodoc: */
  726. static VALUE
  727. name_err_mesg_to_str(VALUE obj)
  728. {
  729. VALUE *ptr, mesg;
  730. TypedData_Get_Struct(obj, VALUE, &name_err_mesg_data_type, ptr);
  731. mesg = ptr[0];
  732. if (NIL_P(mesg)) return Qnil;
  733. else {
  734. const char *desc = 0;
  735. VALUE d = 0, args[NAME_ERR_MESG_COUNT];
  736. obj = ptr[1];
  737. switch (TYPE(obj)) {
  738. case T_NIL:
  739. desc = "nil";
  740. break;
  741. case T_TRUE:
  742. desc = "true";
  743. break;
  744. case T_FALSE:
  745. desc = "false";
  746. break;
  747. default:
  748. d = rb_protect(rb_inspect, obj, 0);
  749. if (NIL_P(d) || RSTRING_LEN(d) > 65) {
  750. d = rb_any_to_s(obj);
  751. }
  752. desc = RSTRING_PTR(d);
  753. break;
  754. }
  755. if (desc && desc[0] != '#') {
  756. d = d ? rb_str_dup(d) : rb_str_new2(desc);
  757. rb_str_cat2(d, ":");
  758. rb_str_cat2(d, rb_obj_classname(obj));
  759. }
  760. args[0] = mesg;
  761. args[1] = ptr[2];
  762. args[2] = d;
  763. mesg = rb_f_sprintf(NAME_ERR_MESG_COUNT, args);
  764. }
  765. if (OBJ_TAINTED(obj)) OBJ_TAINT(mesg);
  766. return mesg;
  767. }
  768. /* :nodoc: */
  769. static VALUE
  770. name_err_mesg_load(VALUE klass, VALUE str)
  771. {
  772. return str;
  773. }
  774. /*
  775. * call-seq:
  776. * no_method_error.args -> obj
  777. *
  778. * Return the arguments passed in as the third parameter to
  779. * the constructor.
  780. */
  781. static VALUE
  782. nometh_err_args(VALUE self)
  783. {
  784. return rb_attr_get(self, rb_intern("args"));
  785. }
  786. void
  787. rb_invalid_str(const char *str, const char *type)
  788. {
  789. VALUE s = rb_str_inspect(rb_str_new2(str));
  790. rb_raise(rb_eArgError, "invalid value for %s: %s", type, RSTRING_PTR(s));
  791. }
  792. /*
  793. * Document-module: Errno
  794. *
  795. * Ruby exception objects are subclasses of <code>Exception</code>.
  796. * However, operating systems typically report errors using plain
  797. * integers. Module <code>Errno</code> is created dynamically to map
  798. * these operating system errors to Ruby classes, with each error
  799. * number generating its own subclass of <code>SystemCallError</code>.
  800. * As the subclass is created in module <code>Errno</code>, its name
  801. * will start <code>Errno::</code>.
  802. *
  803. * The names of the <code>Errno::</code> classes depend on
  804. * the environment in which Ruby runs. On a typical Unix or Windows
  805. * platform, there are <code>Errno</code> classes such as
  806. * <code>Errno::EACCES</code>, <code>Errno::EAGAIN</code>,
  807. * <code>Errno::EINTR</code>, and so on.
  808. *
  809. * The integer operating system error number corresponding to a
  810. * particular error is available as the class constant
  811. * <code>Errno::</code><em>error</em><code>::Errno</code>.
  812. *
  813. * Errno::EACCES::Errno #=> 13
  814. * Errno::EAGAIN::Errno #=> 11
  815. * Errno::EINTR::Errno #=> 4
  816. *
  817. * The full list of operating system errors on your particular platform
  818. * are available as the constants of <code>Errno</code>.
  819. *
  820. * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
  821. */
  822. static st_table *syserr_tbl;
  823. static VALUE
  824. set_syserr(int n, const char *name)
  825. {
  826. VALUE error;
  827. if (!st_lookup(syserr_tbl, n, &error)) {
  828. error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError);
  829. rb_define_const(error, "Errno", INT2NUM(n));
  830. st_add_direct(syserr_tbl, n, error);
  831. }
  832. else {
  833. rb_define_const(rb_mErrno, name, error);
  834. }
  835. return error;
  836. }
  837. static VALUE
  838. get_syserr(int n)
  839. {
  840. VALUE error;
  841. if (!st_lookup(syserr_tbl, n, &error)) {
  842. char name[8]; /* some Windows' errno have 5 digits. */
  843. snprintf(name, sizeof(name), "E%03d", n);
  844. error = set_syserr(n, name);
  845. }
  846. return error;
  847. }
  848. /*
  849. * call-seq:
  850. * SystemCallError.new(msg, errno) -> system_call_error_subclass
  851. *
  852. * If _errno_ corresponds to a known system error code, constructs
  853. * the appropriate <code>Errno</code> class for that error, otherwise
  854. * constructs a generic <code>SystemCallError</code> object. The
  855. * error number is subsequently available via the <code>errno</code>
  856. * method.
  857. */
  858. static VALUE
  859. syserr_initialize(int argc, VALUE *argv, VALUE self)
  860. {
  861. #if !defined(_WIN32)
  862. char *strerror();
  863. #endif
  864. const char *err;
  865. VALUE mesg, error;
  866. VALUE klass = rb_obj_class(self);
  867. if (klass == rb_eSystemCallError) {
  868. rb_scan_args(argc, argv, "11", &mesg, &error);
  869. if (argc == 1 && FIXNUM_P(mesg)) {
  870. error = mesg; mesg = Qnil;
  871. }
  872. if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &klass)) {
  873. /* change class */
  874. if (TYPE(self) != T_OBJECT) { /* insurance to avoid type crash */
  875. rb_raise(rb_eTypeError, "invalid instance type");
  876. }
  877. RBASIC(self)->klass = klass;
  878. }
  879. }
  880. else {
  881. rb_scan_args(argc, argv, "01", &mesg);
  882. error = rb_const_get(klass, rb_intern("Errno"));
  883. }
  884. if (!NIL_P(error)) err = strerror(NUM2INT(error));
  885. else err = "unknown error";
  886. if (!NIL_P(mesg)) {
  887. VALUE str = mesg;
  888. StringValue(str);
  889. mesg = rb_sprintf("%s - %.*s", err,
  890. (int)RSTRING_LEN(str), RSTRING_PTR(str));
  891. }
  892. else {
  893. mesg = rb_str_new2(err);
  894. }
  895. rb_call_super(1, &mesg);
  896. rb_iv_set(self, "errno", error);
  897. return self;
  898. }
  899. /*
  900. * call-seq:
  901. * system_call_error.errno -> fixnum
  902. *
  903. * Return this SystemCallError's error number.
  904. */
  905. static VALUE
  906. syserr_errno(VALUE self)
  907. {
  908. return rb_attr_get(self, rb_intern("errno"));
  909. }
  910. /*
  911. * call-seq:
  912. * system_call_error === other -> true or false
  913. *
  914. * Return +true+ if the receiver is a generic +SystemCallError+, or
  915. * if the error numbers +self+ and _other_ are the same.
  916. */
  917. static VALUE
  918. syserr_eqq(VALUE self, VALUE exc)
  919. {
  920. VALUE num, e;
  921. ID en;
  922. CONST_ID(en, "errno");
  923. if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) {
  924. if (!rb_respond_to(exc, en)) return Qfalse;
  925. }
  926. else if (self == rb_eSystemCallError) return Qtrue;
  927. num = rb_attr_get(exc, rb_intern("errno"));
  928. if (NIL_P(num)) {
  929. num = rb_funcall(exc, en, 0, 0);
  930. }
  931. e = rb_const_get(self, rb_intern("Errno"));
  932. if (FIXNUM_P(num) ? num == e : rb_equal(num, e))
  933. return Qtrue;
  934. return Qfalse;
  935. }
  936. /*
  937. * Document-class: StandardError
  938. *
  939. * The most standard error types are subclasses of StandardError. A
  940. * rescue clause without an explicit Exception class will rescue all
  941. * StandardErrors (and only those).
  942. *
  943. * def foo
  944. * raise "Oups"
  945. * end
  946. * foo rescue "Hello" #=> "Hello"
  947. *
  948. * On the other hand:
  949. *
  950. * require 'does/not/exist' rescue "Hi"
  951. *
  952. * <em>raises the exception:</em>
  953. *
  954. * LoadError: no such file to load -- does/not/exist
  955. *
  956. */
  957. /*
  958. * Document-class: SystemExit
  959. *
  960. * Raised by +exit+ to initiate the termination of the script.
  961. */
  962. /*
  963. * Document-class: SignalException
  964. *
  965. * Raised when a signal is received.
  966. *
  967. * begin
  968. * Process.kill('HUP',Process.pid)
  969. * rescue SignalException => e
  970. * puts "received Exception #{e}"
  971. * end
  972. *
  973. * <em>produces:</em>
  974. *
  975. * received Exception SIGHUP
  976. */
  977. /*
  978. * Document-class: Interrupt
  979. *
  980. * Raised with the interrupt signal is received, typically because the
  981. * user pressed on Control-C (on most posix platforms). As such, it is a
  982. * subclass of +SignalException+.
  983. *
  984. * begin
  985. * puts "Press ctrl-C when you get bored"
  986. * loop {}
  987. * rescue Interrupt => e
  988. * puts "Note: You will typically use Signal.trap instead."
  989. * end
  990. *
  991. * <em>produces:</em>
  992. *
  993. * Press ctrl-C when you get bored
  994. *
  995. * <em>then waits until it is interrupted with Control-C and then prints:</em>
  996. *
  997. * Note: You will typically use Signal.trap instead.
  998. */
  999. /*
  1000. * Document-class: TypeError
  1001. *
  1002. * Raised when encountering an object that is not of the expected type.
  1003. *
  1004. * [1, 2, 3].first("two")
  1005. *
  1006. * <em>raises the exception:</em>
  1007. *
  1008. * TypeError: can't convert String into Integer
  1009. *
  1010. */
  1011. /*
  1012. * Document-class: ArgumentError
  1013. *
  1014. * Raised when the arguments are wrong and there isn't a more specific
  1015. * Exception class.
  1016. *
  1017. * Ex: passing the wrong number of arguments
  1018. *
  1019. * [1, 2, 3].first(4, 5)
  1020. *
  1021. * <em>raises the exception:</em>
  1022. *
  1023. * ArgumentError: wrong number of arguments (2 for 1)
  1024. *
  1025. * Ex: passing an argument that is not acceptable:
  1026. *
  1027. * [1, 2, 3].first(-4)
  1028. *
  1029. * <em>raises the exception:</em>
  1030. *
  1031. * ArgumentError: negative array size
  1032. */
  1033. /*
  1034. * Document-class: IndexError
  1035. *
  1036. * Raised when the given index is invalid.
  1037. *
  1038. * a = [:foo, :bar]
  1039. * a.fetch(0) #=> :foo
  1040. * a[4] #=> nil
  1041. * a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
  1042. *
  1043. */
  1044. /*
  1045. * Document-class: KeyError
  1046. *
  1047. * Raised when the specified key is not found. It is a subclass of
  1048. * IndexError.
  1049. *
  1050. * h = {"foo" => :bar}
  1051. * h.fetch("foo") #=> :bar
  1052. * h.fetch("baz") #=> KeyError: key not found: "baz"
  1053. *
  1054. */
  1055. /*
  1056. * Document-class: RangeError
  1057. *
  1058. * Raised when a given numerical value is out of range.
  1059. *
  1060. * [1, 2, 3].drop(1 << 100)
  1061. *
  1062. * <em>raises the exception:</em>
  1063. *
  1064. * RangeError: bignum too big to convert into `long'
  1065. */
  1066. /*
  1067. * Document-class: ScriptError
  1068. *
  1069. * ScriptError is the superclass for errors raised when a script
  1070. * can not be executed because of a +LoadError+,
  1071. * +NotImplementedError+ or a +SyntaxError+. Note these type of
  1072. * +ScriptErrors+ are not +StandardError+ and will not be
  1073. * rescued unless it is specified explicitly (or its ancestor
  1074. * +Exception+).
  1075. */
  1076. /*
  1077. * Document-class: SyntaxError
  1078. *
  1079. * Raised when encountering Ruby code with an invalid syntax.
  1080. *
  1081. * eval("1+1=2")
  1082. *
  1083. * <em>raises the exception:</em>
  1084. *
  1085. * SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
  1086. */
  1087. /*
  1088. * Document-class: LoadError
  1089. *
  1090. * Raised when a file required (a Ruby script, extension library, ...)
  1091. * fails to load.
  1092. *
  1093. * require 'this/file/does/not/exist'
  1094. *
  1095. * <em>raises the exception:</em>
  1096. *
  1097. * LoadError: no such file to load -- this/file/does/not/exist
  1098. */
  1099. /*
  1100. * Document-class: NotImplementedError
  1101. *
  1102. * Raised when a feature is not implemented on the current platform. For
  1103. * example, methods depending on the +fsync+ or +fork+ system calls may
  1104. * raise this exception if the underlying operating system or Ruby
  1105. * runtime does not support them.
  1106. *
  1107. * Note that if +fork+ raises a +NotImplementedError+, then
  1108. * <code>respond_to?(:fork)</code> returns +false+.
  1109. */
  1110. /*
  1111. * Document-class: NameError
  1112. *
  1113. * Raised when a given name is invalid or undefined.
  1114. *
  1115. * puts foo
  1116. *
  1117. * <em>raises the exception:</em>
  1118. *
  1119. * NameError: undefined local variable or method `foo' for main:Object
  1120. *
  1121. * Since constant names must start with a capital:
  1122. *
  1123. * Fixnum.const_set :answer, 42
  1124. *
  1125. * <em>raises the exception:</em>
  1126. *
  1127. * NameError: wrong constant name answer
  1128. */
  1129. /*
  1130. * Document-class: NoMethodError
  1131. *
  1132. * Raised when a method is called on a receiver which doesn't have it
  1133. * defined and also fails to respond with +method_missing+.
  1134. *
  1135. * "hello".to_ary
  1136. *
  1137. * <em>raises the exception:</em>
  1138. *
  1139. * NoMethodError: undefined method `to_ary' for "hello":String
  1140. */
  1141. /*
  1142. * Document-class: RuntimeError
  1143. *
  1144. * A generic error class raised when an invalid operation is attempted.
  1145. *
  1146. * [1, 2, 3].freeze << 4
  1147. *
  1148. * <em>raises the exception:</em>
  1149. *
  1150. * RuntimeError: can't modify frozen array
  1151. *
  1152. * Kernel.raise will raise a RuntimeError if no Exception class is
  1153. * specified.
  1154. *
  1155. * raise "ouch"
  1156. *
  1157. * <em>raises the exception:</em>
  1158. *
  1159. * RuntimeError: ouch
  1160. */
  1161. /*
  1162. * Document-class: SecurityError
  1163. *
  1164. * Raised when attempting a potential unsafe operation, typically when
  1165. * the $SAFE level is raised above 0.
  1166. *
  1167. * foo = "bar"
  1168. * proc = Proc.new do
  1169. * $SAFE = 4
  1170. * foo.gsub! "a", "*"
  1171. * end
  1172. * proc.call
  1173. *
  1174. * <em>raises the exception:</em>
  1175. *
  1176. * SecurityError: Insecure: can't modify string
  1177. */
  1178. /*
  1179. * Document-class: NoMemoryError
  1180. *
  1181. * Raised when memory allocation fails.
  1182. */
  1183. /*
  1184. * Document-class: SystemCallError
  1185. *
  1186. * SystemCallError is the base class for all low-level
  1187. * platform-dependent errors.
  1188. *
  1189. * The errors available on the current platform are subclasses of
  1190. * SystemCallError and are defined in the Errno module.
  1191. *
  1192. * File.open("does/not/exist")
  1193. *
  1194. * <em>raises the exception:</em>
  1195. *
  1196. * Errno::ENOENT: No such file or directory - does/not/exist
  1197. */
  1198. /*
  1199. * Document-class: Encoding::CompatibilityError
  1200. *
  1201. * Raised by Encoding and String methods when the source encoding is
  1202. * incompatible with the target encoding.
  1203. */
  1204. /*
  1205. * Descendants of class <code>Exception</code> are used to communicate
  1206. * between <code>raise</code> methods and <code>rescue</code>
  1207. * statements in <code>begin/end</code> blocks. <code>Exception</code>
  1208. * objects carry information about the exception---its type (the
  1209. * exception's class name), an optional descriptive string, and
  1210. * optional traceback information. Programs may subclass
  1211. * <code>Exception</code>, or more typically <code>StandardError</code>
  1212. * to provide custom classes and add additional information.
  1213. */
  1214. void
  1215. Init_Exception(void)
  1216. {
  1217. rb_eException = rb_define_class("Exception", rb_cObject);
  1218. rb_define_singleton_method(rb_eException, "exception", rb_class_new_instance, -1);
  1219. rb_define_method(rb_eException, "exception", exc_exception, -1);
  1220. rb_define_method(rb_eException, "initialize", exc_initialize, -1);
  1221. rb_define_method(rb_eException, "==", exc_equal, 1);
  1222. rb_define_method(rb_eException, "to_s", exc_to_s, 0);
  1223. rb_define_method(rb_eException, "message", exc_message, 0);
  1224. rb_define_method(rb_eException, "inspect", exc_inspect, 0);
  1225. rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
  1226. rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
  1227. rb_eSystemExit = rb_define_class("SystemExit", rb_eException);
  1228. rb_define_method(rb_eSystemExit, "initialize", exit_initialize, -1);
  1229. rb_define_method(rb_eSystemExit, "status", exit_status, 0);
  1230. rb_define_method(rb_eSystemExit, "success?", exit_success_p, 0);
  1231. rb_eFatal = rb_define_class("fatal", rb_eException);
  1232. rb_eSignal = rb_define_class("SignalException", rb_eException);
  1233. rb_eInterrupt = rb_define_class("Interrupt", rb_eSignal);
  1234. rb_eStandardError = rb_define_class("StandardError", rb_eException);
  1235. rb_eTypeError = rb_define_class("TypeError", rb_eStandardError);
  1236. rb_eArgError = rb_define_class("ArgumentError", rb_eStandardError);
  1237. rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
  1238. rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
  1239. rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);
  1240. rb_eScriptError = rb_define_class("ScriptError", rb_eException);
  1241. rb_eSyntaxError = rb_define_class("SyntaxError", rb_eScriptError);
  1242. rb_eLoadError = rb_define_class("LoadError", rb_eScriptError);
  1243. rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
  1244. rb_eNameError = rb_define_class("NameError", rb_eStandardError);
  1245. rb_define_method(rb_eNameError, "initialize", name_err_initialize, -1);
  1246. rb_define_method(rb_eNameError, "name", name_err_name, 0);
  1247. rb_define_method(rb_eNameError, "to_s", name_err_to_s, 0);
  1248. rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cData);
  1249. rb_define_singleton_method(rb_cNameErrorMesg, "!", rb_name_err_mesg_new, NAME_ERR_MESG_COUNT);
  1250. rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1);
  1251. rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0);
  1252. rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_to_str, 1);
  1253. rb_define_singleton_method(rb_cNameErrorMesg, "_load", name_err_mesg_load, 1);
  1254. rb_eNoMethodError = rb_define_class("NoMethodError", rb_eNameError);
  1255. rb_define_method(rb_eNoMethodError, "initialize", nometh_err_initialize, -1);
  1256. rb_define_method(rb_eNoMethodError, "args", nometh_err_args, 0);
  1257. rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError);
  1258. rb_eSecurityError = rb_define_class("SecurityError", rb_eException);
  1259. rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
  1260. rb_eEncodingError = rb_define_class("EncodingError", rb_eStandardError);
  1261. rb_eEncCompatError = rb_define_class_under(rb_cEncoding, "CompatibilityError", rb_eEncodingError);
  1262. syserr_tbl = st_init_numtable();
  1263. rb_eSystemCallError = rb_define_class("SystemCallError", rb_eStandardError);
  1264. rb_define_method(rb_eSystemCallError, "initialize", syserr_initialize, -1);
  1265. rb_define_method(rb_eSystemCallError, "errno", syserr_errno, 0);
  1266. rb_define_singleton_method(rb_eSystemCallError, "===", syserr_eqq, 1);
  1267. rb_mErrno = rb_define_module("Errno");
  1268. rb_define_global_function("warn", rb_warn_m, 1);
  1269. }
  1270. void
  1271. rb_raise(VALUE exc, const char *fmt, ...)
  1272. {
  1273. va_list args;
  1274. VALUE mesg;
  1275. va_start(args, fmt);
  1276. mesg = rb_vsprintf(fmt, args);
  1277. va_end(args);
  1278. rb_exc_raise(rb_exc_new3(exc, mesg));
  1279. }
  1280. void
  1281. rb_loaderror(const char *fmt, ...)
  1282. {
  1283. va_list args;
  1284. VALUE mesg;
  1285. va_start(args, fmt);
  1286. mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
  1287. va_end(args);
  1288. rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
  1289. }
  1290. void
  1291. rb_notimplement(void)
  1292. {
  1293. rb_raise(rb_eNotImpError,
  1294. "%s() function is unimplemented on this machine",
  1295. rb_id2name(rb_frame_this_func()));
  1296. }
  1297. void
  1298. rb_fatal(const char *fmt, ...)
  1299. {
  1300. va_list args;
  1301. VALUE mesg;
  1302. va_start(args, fmt);
  1303. mesg = rb_vsprintf(fmt, args);
  1304. va_end(args);
  1305. rb_exc_fatal(rb_exc_new3(rb_eFatal, mesg));
  1306. }
  1307. static VALUE
  1308. make_errno_exc(const char *mesg)
  1309. {
  1310. int n = errno;
  1311. VALUE arg;
  1312. errno = 0;
  1313. if (n == 0) {
  1314. rb_bug("rb_sys_fail(%s) - errno == 0", mesg ? mesg : "");
  1315. }
  1316. arg = mesg ? rb_str_new2(mesg) : Qnil;
  1317. return rb_class_new_instance(1, &arg, get_syserr(n));
  1318. }
  1319. void
  1320. rb_sys_fail(const char *mesg)
  1321. {
  1322. rb_exc_raise(make_errno_exc(mesg));
  1323. }
  1324. void
  1325. rb_mod_sys_fail(VALUE mod, const char *mesg)
  1326. {
  1327. VALUE exc = make_errno_exc(mesg);
  1328. rb_extend_object(exc, mod);
  1329. rb_exc_raise(exc);
  1330. }
  1331. void
  1332. rb_sys_warning(const char *fmt, ...)
  1333. {
  1334. char buf[BUFSIZ];
  1335. va_list args;
  1336. int errno_save;
  1337. errno_save = errno;
  1338. if (!RTEST(ruby_verbose)) return;
  1339. snprintf(buf, BUFSIZ, "warning: %s", fmt);
  1340. snprintf(buf+strlen(buf), BUFSIZ-strlen(buf), ": %s", strerror(errno_save));
  1341. va_start(args, fmt);
  1342. warn_print(buf, args);
  1343. va_end(args);
  1344. errno = errno_save;
  1345. }
  1346. void
  1347. rb_load_fail(const char *path)
  1348. {
  1349. rb_loaderror("%s -- %s", strerror(errno), path);
  1350. }
  1351. void
  1352. rb_error_frozen(const char *what)
  1353. {
  1354. rb_raise(rb_eRuntimeError, "can't modify frozen %s", what);
  1355. }
  1356. void
  1357. rb_check_frozen(VALUE obj)
  1358. {
  1359. if (OBJ_FROZEN(obj)) rb_error_frozen(rb_obj_classname(obj));
  1360. }
  1361. void
  1362. Init_syserr(void)
  1363. {
  1364. rb_eNOERROR = set_syserr(0, "NOERROR");
  1365. #define defined_error(name, num) set_syserr(num, name);
  1366. #define undefined_error(name) set_syserr(0, name);
  1367. #include "known_errors.inc"
  1368. #undef defined_error
  1369. #undef undefined_error
  1370. }
  1371. static void
  1372. err_append(const char *s)
  1373. {
  1374. rb_thread_t *th = GET_THREAD();
  1375. VALUE err = th->errinfo;
  1376. if (th->mild_compile_error) {
  1377. if (!RTEST(err)) {
  1378. err = rb_exc_new2(rb_eSyntaxError, s);
  1379. th->errinfo = err;
  1380. }
  1381. else {
  1382. VALUE str = rb_obj_as_string(err);
  1383. rb_str_cat2(str, "\n");
  1384. rb_str_cat2(str, s);
  1385. th->errinfo = rb_exc_new3(rb_eSyntaxError, str);
  1386. }
  1387. }
  1388. else {
  1389. if (!RTEST(err)) {
  1390. err = rb_exc_new2(rb_eSyntaxError, "compile error");
  1391. th->errinfo = err;
  1392. }
  1393. rb_write_error(s);
  1394. rb_write_error("\n");
  1395. }
  1396. }