PageRenderTime 69ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/error.c

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