* transcode.c (rb_cEncodingConverter): new class Encoding::Converter.
[ruby-svn.git] / error.c
blobc88ef813231ebbfaffa5d2ed82f0d275e626e8d3
1 /**********************************************************************
3 error.c -
5 $Author$
6 created at: Mon Aug 9 16:11:34 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
13 #include "ruby/st.h"
14 #include "vm_core.h"
16 #include <stdio.h>
17 #include <stdarg.h>
18 #ifdef HAVE_STDLIB_H
19 #include <stdlib.h>
20 #endif
21 #ifndef EXIT_SUCCESS
22 #define EXIT_SUCCESS 0
23 #endif
25 extern const char ruby_description[];
27 static int
28 err_position_0(char *buf, long len, const char *file, int line)
30 if (!file) {
31 return 0;
33 else if (line == 0) {
34 return snprintf(buf, len, "%s: ", file);
36 else {
37 return snprintf(buf, len, "%s:%d: ", file, line);
41 static int
42 err_position(char *buf, long len)
44 return err_position_0(buf, len, rb_sourcefile(), rb_sourceline());
47 static void
48 err_snprintf(char *buf, long len, const char *fmt, va_list args)
50 long n;
52 n = err_position(buf, len);
53 if (len > n) {
54 vsnprintf((char*)buf+n, len-n, fmt, args);
58 static void
59 compile_snprintf(char *buf, long len, const char *file, int line, const char *fmt, va_list args)
61 long n;
63 n = err_position_0(buf, len, file, line);
64 if (len > n) {
65 vsnprintf((char*)buf+n, len-n, fmt, args);
69 static void err_append(const char*);
71 void
72 rb_compile_error(const char *file, int line, const char *fmt, ...)
74 va_list args;
75 char buf[BUFSIZ];
77 va_start(args, fmt);
78 compile_snprintf(buf, BUFSIZ, file, line, fmt, args);
79 va_end(args);
80 err_append(buf);
83 void
84 rb_compile_error_append(const char *fmt, ...)
86 va_list args;
87 char buf[BUFSIZ];
89 va_start(args, fmt);
90 vsnprintf(buf, BUFSIZ, fmt, args);
91 va_end(args);
92 err_append(buf);
95 static void
96 compile_warn_print(const char *file, int line, const char *fmt, va_list args)
98 char buf[BUFSIZ];
99 int len;
101 compile_snprintf(buf, BUFSIZ, file, line, fmt, args);
102 len = strlen(buf);
103 buf[len++] = '\n';
104 rb_write_error2(buf, len);
107 void
108 rb_compile_warn(const char *file, int line, const char *fmt, ...)
110 char buf[BUFSIZ];
111 va_list args;
113 if (NIL_P(ruby_verbose)) return;
115 snprintf(buf, BUFSIZ, "warning: %s", fmt);
117 va_start(args, fmt);
118 compile_warn_print(file, line, buf, args);
119 va_end(args);
122 /* rb_compile_warning() reports only in verbose mode */
123 void
124 rb_compile_warning(const char *file, int line, const char *fmt, ...)
126 char buf[BUFSIZ];
127 va_list args;
129 if (!RTEST(ruby_verbose)) return;
131 snprintf(buf, BUFSIZ, "warning: %s", fmt);
133 va_start(args, fmt);
134 compile_warn_print(file, line, buf, args);
135 va_end(args);
138 static void
139 warn_print(const char *fmt, va_list args)
141 char buf[BUFSIZ];
142 int len;
144 err_snprintf(buf, BUFSIZ, fmt, args);
145 len = strlen(buf);
146 buf[len++] = '\n';
147 rb_write_error2(buf, len);
150 void
151 rb_warn(const char *fmt, ...)
153 char buf[BUFSIZ];
154 va_list args;
156 if (NIL_P(ruby_verbose)) return;
158 snprintf(buf, BUFSIZ, "warning: %s", fmt);
160 va_start(args, fmt);
161 warn_print(buf, args);
162 va_end(args);
165 /* rb_warning() reports only in verbose mode */
166 void
167 rb_warning(const char *fmt, ...)
169 char buf[BUFSIZ];
170 va_list args;
172 if (!RTEST(ruby_verbose)) return;
174 snprintf(buf, BUFSIZ, "warning: %s", fmt);
176 va_start(args, fmt);
177 warn_print(buf, args);
178 va_end(args);
182 * call-seq:
183 * warn(msg) => nil
185 * Display the given message (followed by a newline) on STDERR unless
186 * warnings are disabled (for example with the <code>-W0</code> flag).
189 static VALUE
190 rb_warn_m(VALUE self, VALUE mesg)
192 if (!NIL_P(ruby_verbose)) {
193 rb_io_write(rb_stderr, mesg);
194 rb_io_write(rb_stderr, rb_default_rs);
196 return Qnil;
199 void rb_vm_bugreport(void);
201 static void
202 report_bug(const char *file, int line, const char *fmt, va_list args)
204 char buf[BUFSIZ];
205 FILE *out = stderr;
206 int len = err_position_0(buf, BUFSIZ, file, line);
208 if (fwrite(buf, 1, len, out) == len ||
209 fwrite(buf, 1, len, (out = stdout)) == len) {
210 fputs("[BUG] ", out);
211 vfprintf(out, fmt, args);
212 fprintf(out, "\n%s\n\n", ruby_description);
213 rb_vm_bugreport();
217 void
218 rb_bug(const char *fmt, ...)
220 va_list args;
222 va_start(args, fmt);
223 report_bug(rb_sourcefile(), rb_sourceline(), fmt, args);
224 va_end(args);
226 abort();
229 void
230 rb_compile_bug(const char *file, int line, const char *fmt, ...)
232 va_list args;
234 va_start(args, fmt);
235 report_bug(file, line, fmt, args);
236 va_end(args);
238 abort();
241 static const struct types {
242 int type;
243 const char *name;
244 } builtin_types[] = {
245 {T_NIL, "nil"},
246 {T_OBJECT, "Object"},
247 {T_CLASS, "Class"},
248 {T_ICLASS, "iClass"}, /* internal use: mixed-in module holder */
249 {T_MODULE, "Module"},
250 {T_FLOAT, "Float"},
251 {T_STRING, "String"},
252 {T_REGEXP, "Regexp"},
253 {T_ARRAY, "Array"},
254 {T_FIXNUM, "Fixnum"},
255 {T_HASH, "Hash"},
256 {T_STRUCT, "Struct"},
257 {T_BIGNUM, "Bignum"},
258 {T_FILE, "File"},
259 {T_RATIONAL,"Rational"},
260 {T_COMPLEX, "Complex"},
261 {T_TRUE, "true"},
262 {T_FALSE, "false"},
263 {T_SYMBOL, "Symbol"}, /* :symbol */
264 {T_DATA, "Data"}, /* internal use: wrapped C pointers */
265 {T_MATCH, "MatchData"}, /* data of $~ */
266 {T_NODE, "Node"}, /* internal use: syntax tree node */
267 {T_UNDEF, "undef"}, /* internal use: #undef; should not happen */
270 void
271 rb_check_type(VALUE x, int t)
273 const struct types *type = builtin_types;
274 const struct types *const typeend = builtin_types +
275 sizeof(builtin_types) / sizeof(builtin_types[0]);
277 if (x == Qundef) {
278 rb_bug("undef leaked to the Ruby space");
281 if (TYPE(x) != t) {
282 while (type < typeend) {
283 if (type->type == t) {
284 const char *etype;
286 if (NIL_P(x)) {
287 etype = "nil";
289 else if (FIXNUM_P(x)) {
290 etype = "Fixnum";
292 else if (SYMBOL_P(x)) {
293 etype = "Symbol";
295 else if (rb_special_const_p(x)) {
296 etype = RSTRING_PTR(rb_obj_as_string(x));
298 else {
299 etype = rb_obj_classname(x);
301 rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
302 etype, type->name);
304 type++;
306 rb_bug("unknown type 0x%x (0x%x given)", t, TYPE(x));
310 /* exception classes */
311 #include <errno.h>
313 VALUE rb_eException;
314 VALUE rb_eSystemExit;
315 VALUE rb_eInterrupt;
316 VALUE rb_eSignal;
317 VALUE rb_eFatal;
318 VALUE rb_eStandardError;
319 VALUE rb_eRuntimeError;
320 VALUE rb_eTypeError;
321 VALUE rb_eArgError;
322 VALUE rb_eIndexError;
323 VALUE rb_eKeyError;
324 VALUE rb_eRangeError;
325 VALUE rb_eNameError;
326 VALUE rb_eNoMethodError;
327 VALUE rb_eSecurityError;
328 VALUE rb_eNotImpError;
329 VALUE rb_eNoMemError;
330 VALUE rb_cNameErrorMesg;
332 VALUE rb_eScriptError;
333 VALUE rb_eSyntaxError;
334 VALUE rb_eLoadError;
336 VALUE rb_eSystemCallError;
337 VALUE rb_mErrno;
338 static VALUE rb_eNOERROR;
340 VALUE
341 rb_exc_new(VALUE etype, const char *ptr, long len)
343 return rb_funcall(etype, rb_intern("new"), 1, rb_str_new(ptr, len));
346 VALUE
347 rb_exc_new2(VALUE etype, const char *s)
349 return rb_exc_new(etype, s, strlen(s));
352 VALUE
353 rb_exc_new3(VALUE etype, VALUE str)
355 StringValue(str);
356 return rb_funcall(etype, rb_intern("new"), 1, str);
360 * call-seq:
361 * Exception.new(msg = nil) => exception
363 * Construct a new Exception object, optionally passing in
364 * a message.
367 static VALUE
368 exc_initialize(int argc, VALUE *argv, VALUE exc)
370 VALUE arg;
372 rb_scan_args(argc, argv, "01", &arg);
373 rb_iv_set(exc, "mesg", arg);
374 rb_iv_set(exc, "bt", Qnil);
376 return exc;
380 * Document-method: exception
382 * call-seq:
383 * exc.exception(string) -> an_exception or exc
385 * With no argument, or if the argument is the same as the receiver,
386 * return the receiver. Otherwise, create a new
387 * exception object of the same class as the receiver, but with a
388 * message equal to <code>string.to_str</code>.
392 static VALUE
393 exc_exception(int argc, VALUE *argv, VALUE self)
395 VALUE exc;
397 if (argc == 0) return self;
398 if (argc == 1 && self == argv[0]) return self;
399 exc = rb_obj_clone(self);
400 exc_initialize(argc, argv, exc);
402 return exc;
406 * call-seq:
407 * exception.to_s => string
409 * Returns exception's message (or the name of the exception if
410 * no message is set).
413 static VALUE
414 exc_to_s(VALUE exc)
416 VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));
418 if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
419 if (OBJ_TAINTED(exc)) OBJ_TAINT(mesg);
420 return mesg;
424 * call-seq:
425 * exception.message => string
427 * Returns the result of invoking <code>exception.to_s</code>.
428 * Normally this returns the exception's message or name. By
429 * supplying a to_str method, exceptions are agreeing to
430 * be used where Strings are expected.
433 static VALUE
434 exc_message(VALUE exc)
436 return rb_funcall(exc, rb_intern("to_s"), 0, 0);
440 * call-seq:
441 * exception.inspect => string
443 * Return this exception's class name an message
446 static VALUE
447 exc_inspect(VALUE exc)
449 VALUE str, klass;
451 klass = CLASS_OF(exc);
452 exc = rb_obj_as_string(exc);
453 if (RSTRING_LEN(exc) == 0) {
454 return rb_str_dup(rb_class_name(klass));
457 str = rb_str_buf_new2("#<");
458 klass = rb_class_name(klass);
459 rb_str_buf_append(str, klass);
460 rb_str_buf_cat(str, ": ", 2);
461 rb_str_buf_append(str, exc);
462 rb_str_buf_cat(str, ">", 1);
464 return str;
468 * call-seq:
469 * exception.backtrace => array
471 * Returns any backtrace associated with the exception. The backtrace
472 * is an array of strings, each containing either ``filename:lineNo: in
473 * `method''' or ``filename:lineNo.''
475 * def a
476 * raise "boom"
477 * end
479 * def b
480 * a()
481 * end
483 * begin
484 * b()
485 * rescue => detail
486 * print detail.backtrace.join("\n")
487 * end
489 * <em>produces:</em>
491 * prog.rb:2:in `a'
492 * prog.rb:6:in `b'
493 * prog.rb:10
496 static VALUE
497 exc_backtrace(VALUE exc)
499 ID bt;
501 CONST_ID(bt, "bt");
502 return rb_attr_get(exc, bt);
505 VALUE
506 rb_check_backtrace(VALUE bt)
508 long i;
509 static const char err[] = "backtrace must be Array of String";
511 if (!NIL_P(bt)) {
512 int t = TYPE(bt);
514 if (t == T_STRING) return rb_ary_new3(1, bt);
515 if (t != T_ARRAY) {
516 rb_raise(rb_eTypeError, err);
518 for (i=0;i<RARRAY_LEN(bt);i++) {
519 if (TYPE(RARRAY_PTR(bt)[i]) != T_STRING) {
520 rb_raise(rb_eTypeError, err);
524 return bt;
528 * call-seq:
529 * exc.set_backtrace(array) => array
531 * Sets the backtrace information associated with <i>exc</i>. The
532 * argument must be an array of <code>String</code> objects in the
533 * format described in <code>Exception#backtrace</code>.
537 static VALUE
538 exc_set_backtrace(VALUE exc, VALUE bt)
540 return rb_iv_set(exc, "bt", rb_check_backtrace(bt));
544 * call-seq:
545 * exc == obj => true or false
547 * Equality---If <i>obj</i> is not an <code>Exception</code>, returns
548 * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
549 * <i>obj</i> share same class, messages, and backtrace.
552 static VALUE
553 exc_equal(VALUE exc, VALUE obj)
555 ID id_mesg;
557 if (exc == obj) return Qtrue;
558 if (rb_obj_class(exc) != rb_obj_class(obj))
559 return rb_equal(obj, exc);
560 CONST_ID(id_mesg, "mesg");
561 if (!rb_equal(rb_attr_get(exc, id_mesg), rb_attr_get(obj, id_mesg)))
562 return Qfalse;
563 if (!rb_equal(exc_backtrace(exc), exc_backtrace(obj)))
564 return Qfalse;
565 return Qtrue;
569 * call-seq:
570 * SystemExit.new(status=0) => system_exit
572 * Create a new +SystemExit+ exception with the given status.
575 static VALUE
576 exit_initialize(int argc, VALUE *argv, VALUE exc)
578 VALUE status = INT2FIX(EXIT_SUCCESS);
579 if (argc > 0 && FIXNUM_P(argv[0])) {
580 status = *argv++;
581 --argc;
583 rb_call_super(argc, argv);
584 rb_iv_set(exc, "status", status);
585 return exc;
590 * call-seq:
591 * system_exit.status => fixnum
593 * Return the status value associated with this system exit.
596 static VALUE
597 exit_status(VALUE exc)
599 return rb_attr_get(exc, rb_intern("status"));
604 * call-seq:
605 * system_exit.success? => true or false
607 * Returns +true+ if exiting successful, +false+ if not.
610 static VALUE
611 exit_success_p(VALUE exc)
613 VALUE status = rb_attr_get(exc, rb_intern("status"));
614 if (NIL_P(status)) return Qtrue;
615 if (status == INT2FIX(EXIT_SUCCESS)) return Qtrue;
616 return Qfalse;
619 void
620 rb_name_error(ID id, const char *fmt, ...)
622 VALUE exc, argv[2];
623 va_list args;
625 va_start(args, fmt);
626 argv[0] = rb_vsprintf(fmt, args);
627 va_end(args);
629 argv[1] = ID2SYM(id);
630 exc = rb_class_new_instance(2, argv, rb_eNameError);
631 rb_exc_raise(exc);
635 * call-seq:
636 * NameError.new(msg [, name]) => name_error
638 * Construct a new NameError exception. If given the <i>name</i>
639 * parameter may subsequently be examined using the <code>NameError.name</code>
640 * method.
643 static VALUE
644 name_err_initialize(int argc, VALUE *argv, VALUE self)
646 VALUE name;
648 name = (argc > 1) ? argv[--argc] : Qnil;
649 rb_call_super(argc, argv);
650 rb_iv_set(self, "name", name);
651 return self;
655 * call-seq:
656 * name_error.name => string or nil
658 * Return the name associated with this NameError exception.
661 static VALUE
662 name_err_name(VALUE self)
664 return rb_attr_get(self, rb_intern("name"));
668 * call-seq:
669 * name_error.to_s => string
671 * Produce a nicely-formatted string representing the +NameError+.
674 static VALUE
675 name_err_to_s(VALUE exc)
677 VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));
678 VALUE str = mesg;
680 if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
681 StringValue(str);
682 if (str != mesg) {
683 rb_iv_set(exc, "mesg", mesg = str);
685 if (OBJ_TAINTED(exc)) OBJ_TAINT(mesg);
686 return mesg;
690 * call-seq:
691 * NoMethodError.new(msg, name [, args]) => no_method_error
693 * Construct a NoMethodError exception for a method of the given name
694 * called with the given arguments. The name may be accessed using
695 * the <code>#name</code> method on the resulting object, and the
696 * arguments using the <code>#args</code> method.
699 static VALUE
700 nometh_err_initialize(int argc, VALUE *argv, VALUE self)
702 VALUE args = (argc > 2) ? argv[--argc] : Qnil;
703 name_err_initialize(argc, argv, self);
704 rb_iv_set(self, "args", args);
705 return self;
708 /* :nodoc: */
709 static void
710 name_err_mesg_mark(VALUE *ptr)
712 rb_gc_mark_locations(ptr, ptr+3);
715 /* :nodoc: */
716 static VALUE
717 name_err_mesg_new(VALUE obj, VALUE mesg, VALUE recv, VALUE method)
719 VALUE *ptr = ALLOC_N(VALUE, 3);
721 ptr[0] = mesg;
722 ptr[1] = recv;
723 ptr[2] = method;
724 return Data_Wrap_Struct(rb_cNameErrorMesg, name_err_mesg_mark, -1, ptr);
727 /* :nodoc: */
728 static VALUE
729 name_err_mesg_equal(VALUE obj1, VALUE obj2)
731 VALUE *ptr1, *ptr2;
732 int i;
734 if (obj1 == obj2) return Qtrue;
735 if (rb_obj_class(obj2) != rb_cNameErrorMesg)
736 return Qfalse;
738 Data_Get_Struct(obj1, VALUE, ptr1);
739 Data_Get_Struct(obj2, VALUE, ptr2);
740 for (i=0; i<3; i++) {
741 if (!rb_equal(ptr1[i], ptr2[i]))
742 return Qfalse;
744 return Qtrue;
747 /* :nodoc: */
748 static VALUE
749 name_err_mesg_to_str(VALUE obj)
751 VALUE *ptr, mesg;
752 Data_Get_Struct(obj, VALUE, ptr);
754 mesg = ptr[0];
755 if (NIL_P(mesg)) return Qnil;
756 else {
757 const char *desc = 0;
758 VALUE d = 0, args[3];
760 obj = ptr[1];
761 switch (TYPE(obj)) {
762 case T_NIL:
763 desc = "nil";
764 break;
765 case T_TRUE:
766 desc = "true";
767 break;
768 case T_FALSE:
769 desc = "false";
770 break;
771 default:
772 d = rb_protect(rb_inspect, obj, 0);
773 if (NIL_P(d) || RSTRING_LEN(d) > 65) {
774 d = rb_any_to_s(obj);
776 desc = RSTRING_PTR(d);
777 break;
779 if (desc && desc[0] != '#') {
780 d = rb_str_new2(desc);
781 rb_str_cat2(d, ":");
782 rb_str_cat2(d, rb_obj_classname(obj));
784 args[0] = mesg;
785 args[1] = ptr[2];
786 args[2] = d;
787 mesg = rb_f_sprintf(3, args);
789 if (OBJ_TAINTED(obj)) OBJ_TAINT(mesg);
790 return mesg;
793 /* :nodoc: */
794 static VALUE
795 name_err_mesg_load(VALUE klass, VALUE str)
797 return str;
801 * call-seq:
802 * no_method_error.args => obj
804 * Return the arguments passed in as the third parameter to
805 * the constructor.
808 static VALUE
809 nometh_err_args(VALUE self)
811 return rb_attr_get(self, rb_intern("args"));
814 void
815 rb_invalid_str(const char *str, const char *type)
817 VALUE s = rb_str_inspect(rb_str_new2(str));
819 rb_raise(rb_eArgError, "invalid value for %s: %s", type, RSTRING_PTR(s));
823 * Document-module: Errno
825 * Ruby exception objects are subclasses of <code>Exception</code>.
826 * However, operating systems typically report errors using plain
827 * integers. Module <code>Errno</code> is created dynamically to map
828 * these operating system errors to Ruby classes, with each error
829 * number generating its own subclass of <code>SystemCallError</code>.
830 * As the subclass is created in module <code>Errno</code>, its name
831 * will start <code>Errno::</code>.
833 * The names of the <code>Errno::</code> classes depend on
834 * the environment in which Ruby runs. On a typical Unix or Windows
835 * platform, there are <code>Errno</code> classes such as
836 * <code>Errno::EACCES</code>, <code>Errno::EAGAIN</code>,
837 * <code>Errno::EINTR</code>, and so on.
839 * The integer operating system error number corresponding to a
840 * particular error is available as the class constant
841 * <code>Errno::</code><em>error</em><code>::Errno</code>.
843 * Errno::EACCES::Errno #=> 13
844 * Errno::EAGAIN::Errno #=> 11
845 * Errno::EINTR::Errno #=> 4
847 * The full list of operating system errors on your particular platform
848 * are available as the constants of <code>Errno</code>.
850 * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
853 static st_table *syserr_tbl;
855 static VALUE
856 set_syserr(int n, const char *name)
858 VALUE error;
860 if (!st_lookup(syserr_tbl, n, &error)) {
861 error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError);
862 rb_define_const(error, "Errno", INT2NUM(n));
863 st_add_direct(syserr_tbl, n, error);
865 else {
866 rb_define_const(rb_mErrno, name, error);
868 return error;
871 static VALUE
872 get_syserr(int n)
874 VALUE error;
876 if (!st_lookup(syserr_tbl, n, &error)) {
877 char name[8]; /* some Windows' errno have 5 digits. */
879 snprintf(name, sizeof(name), "E%03d", n);
880 error = set_syserr(n, name);
882 return error;
886 * call-seq:
887 * SystemCallError.new(msg, errno) => system_call_error_subclass
889 * If _errno_ corresponds to a known system error code, constructs
890 * the appropriate <code>Errno</code> class for that error, otherwise
891 * constructs a generic <code>SystemCallError</code> object. The
892 * error number is subsequently available via the <code>errno</code>
893 * method.
896 static VALUE
897 syserr_initialize(int argc, VALUE *argv, VALUE self)
899 #if !defined(_WIN32) && !defined(__VMS)
900 char *strerror();
901 #endif
902 const char *err;
903 VALUE mesg, error;
904 VALUE klass = rb_obj_class(self);
906 if (klass == rb_eSystemCallError) {
907 rb_scan_args(argc, argv, "11", &mesg, &error);
908 if (argc == 1 && FIXNUM_P(mesg)) {
909 error = mesg; mesg = Qnil;
911 if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &klass)) {
912 /* change class */
913 if (TYPE(self) != T_OBJECT) { /* insurance to avoid type crash */
914 rb_raise(rb_eTypeError, "invalid instance type");
916 RBASIC(self)->klass = klass;
919 else {
920 rb_scan_args(argc, argv, "01", &mesg);
921 error = rb_const_get(klass, rb_intern("Errno"));
923 if (!NIL_P(error)) err = strerror(NUM2LONG(error));
924 else err = "unknown error";
925 if (!NIL_P(mesg)) {
926 VALUE str = mesg;
928 StringValue(str);
929 mesg = rb_sprintf("%s - %.*s", err,
930 (int)RSTRING_LEN(str), RSTRING_PTR(str));
932 else {
933 mesg = rb_str_new2(err);
935 rb_call_super(1, &mesg);
936 rb_iv_set(self, "errno", error);
937 return self;
941 * call-seq:
942 * system_call_error.errno => fixnum
944 * Return this SystemCallError's error number.
947 static VALUE
948 syserr_errno(VALUE self)
950 return rb_attr_get(self, rb_intern("errno"));
954 * call-seq:
955 * system_call_error === other => true or false
957 * Return +true+ if the receiver is a generic +SystemCallError+, or
958 * if the error numbers _self_ and _other_ are the same.
961 static VALUE
962 syserr_eqq(VALUE self, VALUE exc)
964 VALUE num, e;
965 ID en;
967 CONST_ID(en, "errno");
969 if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) {
970 if (!rb_respond_to(exc, en)) return Qfalse;
972 else if (self == rb_eSystemCallError) return Qtrue;
974 num = rb_attr_get(exc, rb_intern("errno"));
975 if (NIL_P(num)) {
976 num = rb_funcall(exc, en, 0, 0);
978 e = rb_const_get(self, rb_intern("Errno"));
979 if (FIXNUM_P(num) ? num == e : rb_equal(num, e))
980 return Qtrue;
981 return Qfalse;
985 * call-seq:
986 * Errno.const_missing => SystemCallError
988 * Returns default SystemCallError class.
990 static VALUE
991 errno_missing(VALUE self, VALUE id)
993 return rb_eNOERROR;
997 * Descendants of class <code>Exception</code> are used to communicate
998 * between <code>raise</code> methods and <code>rescue</code>
999 * statements in <code>begin/end</code> blocks. <code>Exception</code>
1000 * objects carry information about the exception---its type (the
1001 * exception's class name), an optional descriptive string, and
1002 * optional traceback information. Programs may subclass
1003 * <code>Exception</code> to add additional information.
1006 void
1007 Init_Exception(void)
1009 rb_eException = rb_define_class("Exception", rb_cObject);
1010 rb_define_singleton_method(rb_eException, "exception", rb_class_new_instance, -1);
1011 rb_define_method(rb_eException, "exception", exc_exception, -1);
1012 rb_define_method(rb_eException, "initialize", exc_initialize, -1);
1013 rb_define_method(rb_eException, "==", exc_equal, 1);
1014 rb_define_method(rb_eException, "to_s", exc_to_s, 0);
1015 rb_define_method(rb_eException, "message", exc_message, 0);
1016 rb_define_method(rb_eException, "inspect", exc_inspect, 0);
1017 rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
1018 rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
1020 rb_eSystemExit = rb_define_class("SystemExit", rb_eException);
1021 rb_define_method(rb_eSystemExit, "initialize", exit_initialize, -1);
1022 rb_define_method(rb_eSystemExit, "status", exit_status, 0);
1023 rb_define_method(rb_eSystemExit, "success?", exit_success_p, 0);
1025 rb_eFatal = rb_define_class("fatal", rb_eException);
1026 rb_eSignal = rb_define_class("SignalException", rb_eException);
1027 rb_eInterrupt = rb_define_class("Interrupt", rb_eSignal);
1029 rb_eStandardError = rb_define_class("StandardError", rb_eException);
1030 rb_eTypeError = rb_define_class("TypeError", rb_eStandardError);
1031 rb_eArgError = rb_define_class("ArgumentError", rb_eStandardError);
1032 rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
1033 rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
1034 rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);
1036 rb_eScriptError = rb_define_class("ScriptError", rb_eException);
1037 rb_eSyntaxError = rb_define_class("SyntaxError", rb_eScriptError);
1038 rb_eLoadError = rb_define_class("LoadError", rb_eScriptError);
1039 rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
1041 rb_eNameError = rb_define_class("NameError", rb_eStandardError);
1042 rb_define_method(rb_eNameError, "initialize", name_err_initialize, -1);
1043 rb_define_method(rb_eNameError, "name", name_err_name, 0);
1044 rb_define_method(rb_eNameError, "to_s", name_err_to_s, 0);
1045 rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cData);
1046 rb_define_singleton_method(rb_cNameErrorMesg, "!", name_err_mesg_new, 3);
1047 rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1);
1048 rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0);
1049 rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_to_str, 1);
1050 rb_define_singleton_method(rb_cNameErrorMesg, "_load", name_err_mesg_load, 1);
1051 rb_eNoMethodError = rb_define_class("NoMethodError", rb_eNameError);
1052 rb_define_method(rb_eNoMethodError, "initialize", nometh_err_initialize, -1);
1053 rb_define_method(rb_eNoMethodError, "args", nometh_err_args, 0);
1055 rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError);
1056 rb_eSecurityError = rb_define_class("SecurityError", rb_eException);
1057 rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
1059 syserr_tbl = st_init_numtable();
1060 rb_eSystemCallError = rb_define_class("SystemCallError", rb_eStandardError);
1061 rb_define_method(rb_eSystemCallError, "initialize", syserr_initialize, -1);
1062 rb_define_method(rb_eSystemCallError, "errno", syserr_errno, 0);
1063 rb_define_singleton_method(rb_eSystemCallError, "===", syserr_eqq, 1);
1065 rb_mErrno = rb_define_module("Errno");
1066 rb_define_singleton_method(rb_mErrno, "const_missing", errno_missing, 1);
1068 rb_define_global_function("warn", rb_warn_m, 1);
1071 void
1072 rb_raise(VALUE exc, const char *fmt, ...)
1074 va_list args;
1075 VALUE mesg;
1077 va_start(args, fmt);
1078 mesg = rb_vsprintf(fmt, args);
1079 va_end(args);
1080 rb_exc_raise(rb_exc_new3(exc, mesg));
1083 void
1084 rb_loaderror(const char *fmt, ...)
1086 va_list args;
1087 VALUE mesg;
1089 va_start(args, fmt);
1090 mesg = rb_vsprintf(fmt, args);
1091 va_end(args);
1092 rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
1095 void
1096 rb_notimplement(void)
1098 rb_raise(rb_eNotImpError,
1099 "%s() function is unimplemented on this machine",
1100 rb_id2name(rb_frame_this_func()));
1103 void
1104 rb_fatal(const char *fmt, ...)
1106 va_list args;
1107 VALUE mesg;
1109 va_start(args, fmt);
1110 mesg = rb_vsprintf(fmt, args);
1111 va_end(args);
1113 rb_exc_fatal(rb_exc_new3(rb_eFatal, mesg));
1116 void
1117 rb_sys_fail(const char *mesg)
1119 int n = errno;
1120 VALUE arg;
1122 errno = 0;
1123 if (n == 0) {
1124 rb_bug("rb_sys_fail(%s) - errno == 0", mesg ? mesg : "");
1127 arg = mesg ? rb_str_new2(mesg) : Qnil;
1128 rb_exc_raise(rb_class_new_instance(1, &arg, get_syserr(n)));
1131 void
1132 rb_sys_warning(const char *fmt, ...)
1134 char buf[BUFSIZ];
1135 va_list args;
1136 int errno_save;
1138 errno_save = errno;
1140 if (!RTEST(ruby_verbose)) return;
1142 snprintf(buf, BUFSIZ, "warning: %s", fmt);
1143 snprintf(buf+strlen(buf), BUFSIZ-strlen(buf), ": %s", strerror(errno_save));
1145 va_start(args, fmt);
1146 warn_print(buf, args);
1147 va_end(args);
1148 errno = errno_save;
1151 void
1152 rb_load_fail(const char *path)
1154 rb_loaderror("%s -- %s", strerror(errno), path);
1157 void
1158 rb_error_frozen(const char *what)
1160 rb_raise(rb_eRuntimeError, "can't modify frozen %s", what);
1163 void
1164 rb_check_frozen(VALUE obj)
1166 if (OBJ_FROZEN(obj)) rb_error_frozen(rb_obj_classname(obj));
1169 void
1170 Init_syserr(void)
1172 #ifdef EPERM
1173 set_syserr(EPERM, "EPERM");
1174 #endif
1175 #ifdef ENOENT
1176 set_syserr(ENOENT, "ENOENT");
1177 #endif
1178 #ifdef ESRCH
1179 set_syserr(ESRCH, "ESRCH");
1180 #endif
1181 #ifdef EINTR
1182 set_syserr(EINTR, "EINTR");
1183 #endif
1184 #ifdef EIO
1185 set_syserr(EIO, "EIO");
1186 #endif
1187 #ifdef ENXIO
1188 set_syserr(ENXIO, "ENXIO");
1189 #endif
1190 #ifdef E2BIG
1191 set_syserr(E2BIG, "E2BIG");
1192 #endif
1193 #ifdef ENOEXEC
1194 set_syserr(ENOEXEC, "ENOEXEC");
1195 #endif
1196 #ifdef EBADF
1197 set_syserr(EBADF, "EBADF");
1198 #endif
1199 #ifdef ECHILD
1200 set_syserr(ECHILD, "ECHILD");
1201 #endif
1202 #ifdef EAGAIN
1203 set_syserr(EAGAIN, "EAGAIN");
1204 #endif
1205 #ifdef ENOMEM
1206 set_syserr(ENOMEM, "ENOMEM");
1207 #endif
1208 #ifdef EACCES
1209 set_syserr(EACCES, "EACCES");
1210 #endif
1211 #ifdef EFAULT
1212 set_syserr(EFAULT, "EFAULT");
1213 #endif
1214 #ifdef ENOTBLK
1215 set_syserr(ENOTBLK, "ENOTBLK");
1216 #endif
1217 #ifdef EBUSY
1218 set_syserr(EBUSY, "EBUSY");
1219 #endif
1220 #ifdef EEXIST
1221 set_syserr(EEXIST, "EEXIST");
1222 #endif
1223 #ifdef EXDEV
1224 set_syserr(EXDEV, "EXDEV");
1225 #endif
1226 #ifdef ENODEV
1227 set_syserr(ENODEV, "ENODEV");
1228 #endif
1229 #ifdef ENOTDIR
1230 set_syserr(ENOTDIR, "ENOTDIR");
1231 #endif
1232 #ifdef EISDIR
1233 set_syserr(EISDIR, "EISDIR");
1234 #endif
1235 #ifdef EINVAL
1236 set_syserr(EINVAL, "EINVAL");
1237 #endif
1238 #ifdef ENFILE
1239 set_syserr(ENFILE, "ENFILE");
1240 #endif
1241 #ifdef EMFILE
1242 set_syserr(EMFILE, "EMFILE");
1243 #endif
1244 #ifdef ENOTTY
1245 set_syserr(ENOTTY, "ENOTTY");
1246 #endif
1247 #ifdef ETXTBSY
1248 set_syserr(ETXTBSY, "ETXTBSY");
1249 #endif
1250 #ifdef EFBIG
1251 set_syserr(EFBIG, "EFBIG");
1252 #endif
1253 #ifdef ENOSPC
1254 set_syserr(ENOSPC, "ENOSPC");
1255 #endif
1256 #ifdef ESPIPE
1257 set_syserr(ESPIPE, "ESPIPE");
1258 #endif
1259 #ifdef EROFS
1260 set_syserr(EROFS, "EROFS");
1261 #endif
1262 #ifdef EMLINK
1263 set_syserr(EMLINK, "EMLINK");
1264 #endif
1265 #ifdef EPIPE
1266 set_syserr(EPIPE, "EPIPE");
1267 #endif
1268 #ifdef EDOM
1269 set_syserr(EDOM, "EDOM");
1270 #endif
1271 #ifdef ERANGE
1272 set_syserr(ERANGE, "ERANGE");
1273 #endif
1274 #ifdef EDEADLK
1275 set_syserr(EDEADLK, "EDEADLK");
1276 #endif
1277 #ifdef ENAMETOOLONG
1278 set_syserr(ENAMETOOLONG, "ENAMETOOLONG");
1279 #endif
1280 #ifdef ENOLCK
1281 set_syserr(ENOLCK, "ENOLCK");
1282 #endif
1283 #ifdef ENOSYS
1284 set_syserr(ENOSYS, "ENOSYS");
1285 #endif
1286 #ifdef ENOTEMPTY
1287 set_syserr(ENOTEMPTY, "ENOTEMPTY");
1288 #endif
1289 #ifdef ELOOP
1290 set_syserr(ELOOP, "ELOOP");
1291 #endif
1292 #ifdef EWOULDBLOCK
1293 set_syserr(EWOULDBLOCK, "EWOULDBLOCK");
1294 #endif
1295 #ifdef ENOMSG
1296 set_syserr(ENOMSG, "ENOMSG");
1297 #endif
1298 #ifdef EIDRM
1299 set_syserr(EIDRM, "EIDRM");
1300 #endif
1301 #ifdef ECHRNG
1302 set_syserr(ECHRNG, "ECHRNG");
1303 #endif
1304 #ifdef EL2NSYNC
1305 set_syserr(EL2NSYNC, "EL2NSYNC");
1306 #endif
1307 #ifdef EL3HLT
1308 set_syserr(EL3HLT, "EL3HLT");
1309 #endif
1310 #ifdef EL3RST
1311 set_syserr(EL3RST, "EL3RST");
1312 #endif
1313 #ifdef ELNRNG
1314 set_syserr(ELNRNG, "ELNRNG");
1315 #endif
1316 #ifdef EUNATCH
1317 set_syserr(EUNATCH, "EUNATCH");
1318 #endif
1319 #ifdef ENOCSI
1320 set_syserr(ENOCSI, "ENOCSI");
1321 #endif
1322 #ifdef EL2HLT
1323 set_syserr(EL2HLT, "EL2HLT");
1324 #endif
1325 #ifdef EBADE
1326 set_syserr(EBADE, "EBADE");
1327 #endif
1328 #ifdef EBADR
1329 set_syserr(EBADR, "EBADR");
1330 #endif
1331 #ifdef EXFULL
1332 set_syserr(EXFULL, "EXFULL");
1333 #endif
1334 #ifdef ENOANO
1335 set_syserr(ENOANO, "ENOANO");
1336 #endif
1337 #ifdef EBADRQC
1338 set_syserr(EBADRQC, "EBADRQC");
1339 #endif
1340 #ifdef EBADSLT
1341 set_syserr(EBADSLT, "EBADSLT");
1342 #endif
1343 #ifdef EDEADLOCK
1344 set_syserr(EDEADLOCK, "EDEADLOCK");
1345 #endif
1346 #ifdef EBFONT
1347 set_syserr(EBFONT, "EBFONT");
1348 #endif
1349 #ifdef ENOSTR
1350 set_syserr(ENOSTR, "ENOSTR");
1351 #endif
1352 #ifdef ENODATA
1353 set_syserr(ENODATA, "ENODATA");
1354 #endif
1355 #ifdef ETIME
1356 set_syserr(ETIME, "ETIME");
1357 #endif
1358 #ifdef ENOSR
1359 set_syserr(ENOSR, "ENOSR");
1360 #endif
1361 #ifdef ENONET
1362 set_syserr(ENONET, "ENONET");
1363 #endif
1364 #ifdef ENOPKG
1365 set_syserr(ENOPKG, "ENOPKG");
1366 #endif
1367 #ifdef EREMOTE
1368 set_syserr(EREMOTE, "EREMOTE");
1369 #endif
1370 #ifdef ENOLINK
1371 set_syserr(ENOLINK, "ENOLINK");
1372 #endif
1373 #ifdef EADV
1374 set_syserr(EADV, "EADV");
1375 #endif
1376 #ifdef ESRMNT
1377 set_syserr(ESRMNT, "ESRMNT");
1378 #endif
1379 #ifdef ECOMM
1380 set_syserr(ECOMM, "ECOMM");
1381 #endif
1382 #ifdef EPROTO
1383 set_syserr(EPROTO, "EPROTO");
1384 #endif
1385 #ifdef EMULTIHOP
1386 set_syserr(EMULTIHOP, "EMULTIHOP");
1387 #endif
1388 #ifdef EDOTDOT
1389 set_syserr(EDOTDOT, "EDOTDOT");
1390 #endif
1391 #ifdef EBADMSG
1392 set_syserr(EBADMSG, "EBADMSG");
1393 #endif
1394 #ifdef EOVERFLOW
1395 set_syserr(EOVERFLOW, "EOVERFLOW");
1396 #endif
1397 #ifdef ENOTUNIQ
1398 set_syserr(ENOTUNIQ, "ENOTUNIQ");
1399 #endif
1400 #ifdef EBADFD
1401 set_syserr(EBADFD, "EBADFD");
1402 #endif
1403 #ifdef EREMCHG
1404 set_syserr(EREMCHG, "EREMCHG");
1405 #endif
1406 #ifdef ELIBACC
1407 set_syserr(ELIBACC, "ELIBACC");
1408 #endif
1409 #ifdef ELIBBAD
1410 set_syserr(ELIBBAD, "ELIBBAD");
1411 #endif
1412 #ifdef ELIBSCN
1413 set_syserr(ELIBSCN, "ELIBSCN");
1414 #endif
1415 #ifdef ELIBMAX
1416 set_syserr(ELIBMAX, "ELIBMAX");
1417 #endif
1418 #ifdef ELIBEXEC
1419 set_syserr(ELIBEXEC, "ELIBEXEC");
1420 #endif
1421 #ifdef EILSEQ
1422 set_syserr(EILSEQ, "EILSEQ");
1423 #endif
1424 #ifdef ERESTART
1425 set_syserr(ERESTART, "ERESTART");
1426 #endif
1427 #ifdef ESTRPIPE
1428 set_syserr(ESTRPIPE, "ESTRPIPE");
1429 #endif
1430 #ifdef EUSERS
1431 set_syserr(EUSERS, "EUSERS");
1432 #endif
1433 #ifdef ENOTSOCK
1434 set_syserr(ENOTSOCK, "ENOTSOCK");
1435 #endif
1436 #ifdef EDESTADDRREQ
1437 set_syserr(EDESTADDRREQ, "EDESTADDRREQ");
1438 #endif
1439 #ifdef EMSGSIZE
1440 set_syserr(EMSGSIZE, "EMSGSIZE");
1441 #endif
1442 #ifdef EPROTOTYPE
1443 set_syserr(EPROTOTYPE, "EPROTOTYPE");
1444 #endif
1445 #ifdef ENOPROTOOPT
1446 set_syserr(ENOPROTOOPT, "ENOPROTOOPT");
1447 #endif
1448 #ifdef EPROTONOSUPPORT
1449 set_syserr(EPROTONOSUPPORT, "EPROTONOSUPPORT");
1450 #endif
1451 #ifdef ESOCKTNOSUPPORT
1452 set_syserr(ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT");
1453 #endif
1454 #ifdef EOPNOTSUPP
1455 set_syserr(EOPNOTSUPP, "EOPNOTSUPP");
1456 #endif
1457 #ifdef EPFNOSUPPORT
1458 set_syserr(EPFNOSUPPORT, "EPFNOSUPPORT");
1459 #endif
1460 #ifdef EAFNOSUPPORT
1461 set_syserr(EAFNOSUPPORT, "EAFNOSUPPORT");
1462 #endif
1463 #ifdef EADDRINUSE
1464 set_syserr(EADDRINUSE, "EADDRINUSE");
1465 #endif
1466 #ifdef EADDRNOTAVAIL
1467 set_syserr(EADDRNOTAVAIL, "EADDRNOTAVAIL");
1468 #endif
1469 #ifdef ENETDOWN
1470 set_syserr(ENETDOWN, "ENETDOWN");
1471 #endif
1472 #ifdef ENETUNREACH
1473 set_syserr(ENETUNREACH, "ENETUNREACH");
1474 #endif
1475 #ifdef ENETRESET
1476 set_syserr(ENETRESET, "ENETRESET");
1477 #endif
1478 #ifdef ECONNABORTED
1479 set_syserr(ECONNABORTED, "ECONNABORTED");
1480 #endif
1481 #ifdef ECONNRESET
1482 set_syserr(ECONNRESET, "ECONNRESET");
1483 #endif
1484 #ifdef ENOBUFS
1485 set_syserr(ENOBUFS, "ENOBUFS");
1486 #endif
1487 #ifdef EISCONN
1488 set_syserr(EISCONN, "EISCONN");
1489 #endif
1490 #ifdef ENOTCONN
1491 set_syserr(ENOTCONN, "ENOTCONN");
1492 #endif
1493 #ifdef ESHUTDOWN
1494 set_syserr(ESHUTDOWN, "ESHUTDOWN");
1495 #endif
1496 #ifdef ETOOMANYREFS
1497 set_syserr(ETOOMANYREFS, "ETOOMANYREFS");
1498 #endif
1499 #ifdef ETIMEDOUT
1500 set_syserr(ETIMEDOUT, "ETIMEDOUT");
1501 #endif
1502 #ifdef ECONNREFUSED
1503 set_syserr(ECONNREFUSED, "ECONNREFUSED");
1504 #endif
1505 #ifdef EHOSTDOWN
1506 set_syserr(EHOSTDOWN, "EHOSTDOWN");
1507 #endif
1508 #ifdef EHOSTUNREACH
1509 set_syserr(EHOSTUNREACH, "EHOSTUNREACH");
1510 #endif
1511 #ifdef EALREADY
1512 set_syserr(EALREADY, "EALREADY");
1513 #endif
1514 #ifdef EINPROGRESS
1515 set_syserr(EINPROGRESS, "EINPROGRESS");
1516 #endif
1517 #ifdef ESTALE
1518 set_syserr(ESTALE, "ESTALE");
1519 #endif
1520 #ifdef EUCLEAN
1521 set_syserr(EUCLEAN, "EUCLEAN");
1522 #endif
1523 #ifdef ENOTNAM
1524 set_syserr(ENOTNAM, "ENOTNAM");
1525 #endif
1526 #ifdef ENAVAIL
1527 set_syserr(ENAVAIL, "ENAVAIL");
1528 #endif
1529 #ifdef EISNAM
1530 set_syserr(EISNAM, "EISNAM");
1531 #endif
1532 #ifdef EREMOTEIO
1533 set_syserr(EREMOTEIO, "EREMOTEIO");
1534 #endif
1535 #ifdef EDQUOT
1536 set_syserr(EDQUOT, "EDQUOT");
1537 #endif
1538 rb_eNOERROR = set_syserr(0, "NOERROR");
1541 static void
1542 err_append(const char *s)
1544 rb_thread_t *th = GET_THREAD();
1545 VALUE err = th->errinfo;
1547 if (th->mild_compile_error) {
1548 if (!RTEST(err)) {
1549 err = rb_exc_new2(rb_eSyntaxError, s);
1550 th->errinfo = err;
1552 else {
1553 VALUE str = rb_obj_as_string(err);
1555 rb_str_cat2(str, "\n");
1556 rb_str_cat2(str, s);
1557 th->errinfo = rb_exc_new3(rb_eSyntaxError, str);
1560 else {
1561 if (!RTEST(err)) {
1562 err = rb_exc_new2(rb_eSyntaxError, "compile error");
1563 th->errinfo = err;
1565 rb_write_error(s);
1566 rb_write_error("\n");