* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / error.c
blob683828c9348ac56a0c795fd7c8836e495eae98ee
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;
331 VALUE rb_eEncCompatError;
333 VALUE rb_eScriptError;
334 VALUE rb_eSyntaxError;
335 VALUE rb_eLoadError;
337 VALUE rb_eSystemCallError;
338 VALUE rb_mErrno;
339 static VALUE rb_eNOERROR;
341 VALUE
342 rb_exc_new(VALUE etype, const char *ptr, long len)
344 return rb_funcall(etype, rb_intern("new"), 1, rb_str_new(ptr, len));
347 VALUE
348 rb_exc_new2(VALUE etype, const char *s)
350 return rb_exc_new(etype, s, strlen(s));
353 VALUE
354 rb_exc_new3(VALUE etype, VALUE str)
356 StringValue(str);
357 return rb_funcall(etype, rb_intern("new"), 1, str);
361 * call-seq:
362 * Exception.new(msg = nil) => exception
364 * Construct a new Exception object, optionally passing in
365 * a message.
368 static VALUE
369 exc_initialize(int argc, VALUE *argv, VALUE exc)
371 VALUE arg;
373 rb_scan_args(argc, argv, "01", &arg);
374 rb_iv_set(exc, "mesg", arg);
375 rb_iv_set(exc, "bt", Qnil);
377 return exc;
381 * Document-method: exception
383 * call-seq:
384 * exc.exception(string) -> an_exception or exc
386 * With no argument, or if the argument is the same as the receiver,
387 * return the receiver. Otherwise, create a new
388 * exception object of the same class as the receiver, but with a
389 * message equal to <code>string.to_str</code>.
393 static VALUE
394 exc_exception(int argc, VALUE *argv, VALUE self)
396 VALUE exc;
398 if (argc == 0) return self;
399 if (argc == 1 && self == argv[0]) return self;
400 exc = rb_obj_clone(self);
401 exc_initialize(argc, argv, exc);
403 return exc;
407 * call-seq:
408 * exception.to_s => string
410 * Returns exception's message (or the name of the exception if
411 * no message is set).
414 static VALUE
415 exc_to_s(VALUE exc)
417 VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));
419 if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
420 if (OBJ_TAINTED(exc)) OBJ_TAINT(mesg);
421 return mesg;
425 * call-seq:
426 * exception.message => string
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.
434 static VALUE
435 exc_message(VALUE exc)
437 return rb_funcall(exc, rb_intern("to_s"), 0, 0);
441 * call-seq:
442 * exception.inspect => string
444 * Return this exception's class name an message
447 static VALUE
448 exc_inspect(VALUE exc)
450 VALUE str, klass;
452 klass = CLASS_OF(exc);
453 exc = rb_obj_as_string(exc);
454 if (RSTRING_LEN(exc) == 0) {
455 return rb_str_dup(rb_class_name(klass));
458 str = rb_str_buf_new2("#<");
459 klass = rb_class_name(klass);
460 rb_str_buf_append(str, klass);
461 rb_str_buf_cat(str, ": ", 2);
462 rb_str_buf_append(str, exc);
463 rb_str_buf_cat(str, ">", 1);
465 return str;
469 * call-seq:
470 * exception.backtrace => array
472 * Returns any backtrace associated with the exception. The backtrace
473 * is an array of strings, each containing either ``filename:lineNo: in
474 * `method''' or ``filename:lineNo.''
476 * def a
477 * raise "boom"
478 * end
480 * def b
481 * a()
482 * end
484 * begin
485 * b()
486 * rescue => detail
487 * print detail.backtrace.join("\n")
488 * end
490 * <em>produces:</em>
492 * prog.rb:2:in `a'
493 * prog.rb:6:in `b'
494 * prog.rb:10
497 static VALUE
498 exc_backtrace(VALUE exc)
500 ID bt;
502 CONST_ID(bt, "bt");
503 return rb_attr_get(exc, bt);
506 VALUE
507 rb_check_backtrace(VALUE bt)
509 long i;
510 static const char err[] = "backtrace must be Array of String";
512 if (!NIL_P(bt)) {
513 int t = TYPE(bt);
515 if (t == T_STRING) return rb_ary_new3(1, bt);
516 if (t != T_ARRAY) {
517 rb_raise(rb_eTypeError, err);
519 for (i=0;i<RARRAY_LEN(bt);i++) {
520 if (TYPE(RARRAY_PTR(bt)[i]) != T_STRING) {
521 rb_raise(rb_eTypeError, err);
525 return bt;
529 * call-seq:
530 * exc.set_backtrace(array) => array
532 * Sets the backtrace information associated with <i>exc</i>. The
533 * argument must be an array of <code>String</code> objects in the
534 * format described in <code>Exception#backtrace</code>.
538 static VALUE
539 exc_set_backtrace(VALUE exc, VALUE bt)
541 return rb_iv_set(exc, "bt", rb_check_backtrace(bt));
545 * call-seq:
546 * exc == obj => true or false
548 * Equality---If <i>obj</i> is not an <code>Exception</code>, returns
549 * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
550 * <i>obj</i> share same class, messages, and backtrace.
553 static VALUE
554 exc_equal(VALUE exc, VALUE obj)
556 ID id_mesg;
558 if (exc == obj) return Qtrue;
559 if (rb_obj_class(exc) != rb_obj_class(obj))
560 return rb_equal(obj, exc);
561 CONST_ID(id_mesg, "mesg");
562 if (!rb_equal(rb_attr_get(exc, id_mesg), rb_attr_get(obj, id_mesg)))
563 return Qfalse;
564 if (!rb_equal(exc_backtrace(exc), exc_backtrace(obj)))
565 return Qfalse;
566 return Qtrue;
570 * call-seq:
571 * SystemExit.new(status=0) => system_exit
573 * Create a new +SystemExit+ exception with the given status.
576 static VALUE
577 exit_initialize(int argc, VALUE *argv, VALUE exc)
579 VALUE status = INT2FIX(EXIT_SUCCESS);
580 if (argc > 0 && FIXNUM_P(argv[0])) {
581 status = *argv++;
582 --argc;
584 rb_call_super(argc, argv);
585 rb_iv_set(exc, "status", status);
586 return exc;
591 * call-seq:
592 * system_exit.status => fixnum
594 * Return the status value associated with this system exit.
597 static VALUE
598 exit_status(VALUE exc)
600 return rb_attr_get(exc, rb_intern("status"));
605 * call-seq:
606 * system_exit.success? => true or false
608 * Returns +true+ if exiting successful, +false+ if not.
611 static VALUE
612 exit_success_p(VALUE exc)
614 VALUE status = rb_attr_get(exc, rb_intern("status"));
615 if (NIL_P(status)) return Qtrue;
616 if (status == INT2FIX(EXIT_SUCCESS)) return Qtrue;
617 return Qfalse;
620 void
621 rb_name_error(ID id, const char *fmt, ...)
623 VALUE exc, argv[2];
624 va_list args;
626 va_start(args, fmt);
627 argv[0] = rb_vsprintf(fmt, args);
628 va_end(args);
630 argv[1] = ID2SYM(id);
631 exc = rb_class_new_instance(2, argv, rb_eNameError);
632 rb_exc_raise(exc);
636 * call-seq:
637 * NameError.new(msg [, name]) => name_error
639 * Construct a new NameError exception. If given the <i>name</i>
640 * parameter may subsequently be examined using the <code>NameError.name</code>
641 * method.
644 static VALUE
645 name_err_initialize(int argc, VALUE *argv, VALUE self)
647 VALUE name;
649 name = (argc > 1) ? argv[--argc] : Qnil;
650 rb_call_super(argc, argv);
651 rb_iv_set(self, "name", name);
652 return self;
656 * call-seq:
657 * name_error.name => string or nil
659 * Return the name associated with this NameError exception.
662 static VALUE
663 name_err_name(VALUE self)
665 return rb_attr_get(self, rb_intern("name"));
669 * call-seq:
670 * name_error.to_s => string
672 * Produce a nicely-formatted string representing the +NameError+.
675 static VALUE
676 name_err_to_s(VALUE exc)
678 VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));
679 VALUE str = mesg;
681 if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
682 StringValue(str);
683 if (str != mesg) {
684 rb_iv_set(exc, "mesg", mesg = str);
686 if (OBJ_TAINTED(exc)) OBJ_TAINT(mesg);
687 return mesg;
691 * call-seq:
692 * NoMethodError.new(msg, name [, args]) => no_method_error
694 * Construct a NoMethodError exception for a method of the given name
695 * called with the given arguments. The name may be accessed using
696 * the <code>#name</code> method on the resulting object, and the
697 * arguments using the <code>#args</code> method.
700 static VALUE
701 nometh_err_initialize(int argc, VALUE *argv, VALUE self)
703 VALUE args = (argc > 2) ? argv[--argc] : Qnil;
704 name_err_initialize(argc, argv, self);
705 rb_iv_set(self, "args", args);
706 return self;
709 /* :nodoc: */
710 static void
711 name_err_mesg_mark(VALUE *ptr)
713 rb_gc_mark_locations(ptr, ptr+3);
716 /* :nodoc: */
717 static VALUE
718 name_err_mesg_new(VALUE obj, VALUE mesg, VALUE recv, VALUE method)
720 VALUE *ptr = ALLOC_N(VALUE, 3);
722 ptr[0] = mesg;
723 ptr[1] = recv;
724 ptr[2] = method;
725 return Data_Wrap_Struct(rb_cNameErrorMesg, name_err_mesg_mark, -1, ptr);
728 /* :nodoc: */
729 static VALUE
730 name_err_mesg_equal(VALUE obj1, VALUE obj2)
732 VALUE *ptr1, *ptr2;
733 int i;
735 if (obj1 == obj2) return Qtrue;
736 if (rb_obj_class(obj2) != rb_cNameErrorMesg)
737 return Qfalse;
739 Data_Get_Struct(obj1, VALUE, ptr1);
740 Data_Get_Struct(obj2, VALUE, ptr2);
741 for (i=0; i<3; i++) {
742 if (!rb_equal(ptr1[i], ptr2[i]))
743 return Qfalse;
745 return Qtrue;
748 /* :nodoc: */
749 static VALUE
750 name_err_mesg_to_str(VALUE obj)
752 VALUE *ptr, mesg;
753 Data_Get_Struct(obj, VALUE, ptr);
755 mesg = ptr[0];
756 if (NIL_P(mesg)) return Qnil;
757 else {
758 const char *desc = 0;
759 VALUE d = 0, args[3];
761 obj = ptr[1];
762 switch (TYPE(obj)) {
763 case T_NIL:
764 desc = "nil";
765 break;
766 case T_TRUE:
767 desc = "true";
768 break;
769 case T_FALSE:
770 desc = "false";
771 break;
772 default:
773 d = rb_protect(rb_inspect, obj, 0);
774 if (NIL_P(d) || RSTRING_LEN(d) > 65) {
775 d = rb_any_to_s(obj);
777 desc = RSTRING_PTR(d);
778 break;
780 if (desc && desc[0] != '#') {
781 d = rb_str_new2(desc);
782 rb_str_cat2(d, ":");
783 rb_str_cat2(d, rb_obj_classname(obj));
785 args[0] = mesg;
786 args[1] = ptr[2];
787 args[2] = d;
788 mesg = rb_f_sprintf(3, args);
790 if (OBJ_TAINTED(obj)) OBJ_TAINT(mesg);
791 return mesg;
794 /* :nodoc: */
795 static VALUE
796 name_err_mesg_load(VALUE klass, VALUE str)
798 return str;
802 * call-seq:
803 * no_method_error.args => obj
805 * Return the arguments passed in as the third parameter to
806 * the constructor.
809 static VALUE
810 nometh_err_args(VALUE self)
812 return rb_attr_get(self, rb_intern("args"));
815 void
816 rb_invalid_str(const char *str, const char *type)
818 VALUE s = rb_str_inspect(rb_str_new2(str));
820 rb_raise(rb_eArgError, "invalid value for %s: %s", type, RSTRING_PTR(s));
824 * Document-module: Errno
826 * Ruby exception objects are subclasses of <code>Exception</code>.
827 * However, operating systems typically report errors using plain
828 * integers. Module <code>Errno</code> is created dynamically to map
829 * these operating system errors to Ruby classes, with each error
830 * number generating its own subclass of <code>SystemCallError</code>.
831 * As the subclass is created in module <code>Errno</code>, its name
832 * will start <code>Errno::</code>.
834 * The names of the <code>Errno::</code> classes depend on
835 * the environment in which Ruby runs. On a typical Unix or Windows
836 * platform, there are <code>Errno</code> classes such as
837 * <code>Errno::EACCES</code>, <code>Errno::EAGAIN</code>,
838 * <code>Errno::EINTR</code>, and so on.
840 * The integer operating system error number corresponding to a
841 * particular error is available as the class constant
842 * <code>Errno::</code><em>error</em><code>::Errno</code>.
844 * Errno::EACCES::Errno #=> 13
845 * Errno::EAGAIN::Errno #=> 11
846 * Errno::EINTR::Errno #=> 4
848 * The full list of operating system errors on your particular platform
849 * are available as the constants of <code>Errno</code>.
851 * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
854 static st_table *syserr_tbl;
856 static VALUE
857 set_syserr(int n, const char *name)
859 VALUE error;
861 if (!st_lookup(syserr_tbl, n, &error)) {
862 error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError);
863 rb_define_const(error, "Errno", INT2NUM(n));
864 st_add_direct(syserr_tbl, n, error);
866 else {
867 rb_define_const(rb_mErrno, name, error);
869 return error;
872 static VALUE
873 get_syserr(int n)
875 VALUE error;
877 if (!st_lookup(syserr_tbl, n, &error)) {
878 char name[8]; /* some Windows' errno have 5 digits. */
880 snprintf(name, sizeof(name), "E%03d", n);
881 error = set_syserr(n, name);
883 return error;
887 * call-seq:
888 * SystemCallError.new(msg, errno) => system_call_error_subclass
890 * If _errno_ corresponds to a known system error code, constructs
891 * the appropriate <code>Errno</code> class for that error, otherwise
892 * constructs a generic <code>SystemCallError</code> object. The
893 * error number is subsequently available via the <code>errno</code>
894 * method.
897 static VALUE
898 syserr_initialize(int argc, VALUE *argv, VALUE self)
900 #if !defined(_WIN32) && !defined(__VMS)
901 char *strerror();
902 #endif
903 const char *err;
904 VALUE mesg, error;
905 VALUE klass = rb_obj_class(self);
907 if (klass == rb_eSystemCallError) {
908 rb_scan_args(argc, argv, "11", &mesg, &error);
909 if (argc == 1 && FIXNUM_P(mesg)) {
910 error = mesg; mesg = Qnil;
912 if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &klass)) {
913 /* change class */
914 if (TYPE(self) != T_OBJECT) { /* insurance to avoid type crash */
915 rb_raise(rb_eTypeError, "invalid instance type");
917 RBASIC(self)->klass = klass;
920 else {
921 rb_scan_args(argc, argv, "01", &mesg);
922 error = rb_const_get(klass, rb_intern("Errno"));
924 if (!NIL_P(error)) err = strerror(NUM2LONG(error));
925 else err = "unknown error";
926 if (!NIL_P(mesg)) {
927 VALUE str = mesg;
929 StringValue(str);
930 mesg = rb_sprintf("%s - %.*s", err,
931 (int)RSTRING_LEN(str), RSTRING_PTR(str));
933 else {
934 mesg = rb_str_new2(err);
936 rb_call_super(1, &mesg);
937 rb_iv_set(self, "errno", error);
938 return self;
942 * call-seq:
943 * system_call_error.errno => fixnum
945 * Return this SystemCallError's error number.
948 static VALUE
949 syserr_errno(VALUE self)
951 return rb_attr_get(self, rb_intern("errno"));
955 * call-seq:
956 * system_call_error === other => true or false
958 * Return +true+ if the receiver is a generic +SystemCallError+, or
959 * if the error numbers _self_ and _other_ are the same.
962 static VALUE
963 syserr_eqq(VALUE self, VALUE exc)
965 VALUE num, e;
966 ID en;
968 CONST_ID(en, "errno");
970 if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) {
971 if (!rb_respond_to(exc, en)) return Qfalse;
973 else if (self == rb_eSystemCallError) return Qtrue;
975 num = rb_attr_get(exc, rb_intern("errno"));
976 if (NIL_P(num)) {
977 num = rb_funcall(exc, en, 0, 0);
979 e = rb_const_get(self, rb_intern("Errno"));
980 if (FIXNUM_P(num) ? num == e : rb_equal(num, e))
981 return Qtrue;
982 return Qfalse;
986 * call-seq:
987 * Errno.const_missing => SystemCallError
989 * Returns default SystemCallError class.
991 static VALUE
992 errno_missing(VALUE self, VALUE id)
994 return rb_eNOERROR;
998 * Descendants of class <code>Exception</code> are used to communicate
999 * between <code>raise</code> methods and <code>rescue</code>
1000 * statements in <code>begin/end</code> blocks. <code>Exception</code>
1001 * objects carry information about the exception---its type (the
1002 * exception's class name), an optional descriptive string, and
1003 * optional traceback information. Programs may subclass
1004 * <code>Exception</code> to add additional information.
1007 void
1008 Init_Exception(void)
1010 rb_eException = rb_define_class("Exception", rb_cObject);
1011 rb_define_singleton_method(rb_eException, "exception", rb_class_new_instance, -1);
1012 rb_define_method(rb_eException, "exception", exc_exception, -1);
1013 rb_define_method(rb_eException, "initialize", exc_initialize, -1);
1014 rb_define_method(rb_eException, "==", exc_equal, 1);
1015 rb_define_method(rb_eException, "to_s", exc_to_s, 0);
1016 rb_define_method(rb_eException, "message", exc_message, 0);
1017 rb_define_method(rb_eException, "inspect", exc_inspect, 0);
1018 rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
1019 rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
1021 rb_eSystemExit = rb_define_class("SystemExit", rb_eException);
1022 rb_define_method(rb_eSystemExit, "initialize", exit_initialize, -1);
1023 rb_define_method(rb_eSystemExit, "status", exit_status, 0);
1024 rb_define_method(rb_eSystemExit, "success?", exit_success_p, 0);
1026 rb_eFatal = rb_define_class("fatal", rb_eException);
1027 rb_eSignal = rb_define_class("SignalException", rb_eException);
1028 rb_eInterrupt = rb_define_class("Interrupt", rb_eSignal);
1030 rb_eStandardError = rb_define_class("StandardError", rb_eException);
1031 rb_eTypeError = rb_define_class("TypeError", rb_eStandardError);
1032 rb_eArgError = rb_define_class("ArgumentError", rb_eStandardError);
1033 rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
1034 rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
1035 rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);
1036 rb_eEncCompatError = rb_define_class("EncodingCompatibilityError", rb_eStandardError);
1038 rb_eScriptError = rb_define_class("ScriptError", rb_eException);
1039 rb_eSyntaxError = rb_define_class("SyntaxError", rb_eScriptError);
1040 rb_eLoadError = rb_define_class("LoadError", rb_eScriptError);
1041 rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
1043 rb_eNameError = rb_define_class("NameError", rb_eStandardError);
1044 rb_define_method(rb_eNameError, "initialize", name_err_initialize, -1);
1045 rb_define_method(rb_eNameError, "name", name_err_name, 0);
1046 rb_define_method(rb_eNameError, "to_s", name_err_to_s, 0);
1047 rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cData);
1048 rb_define_singleton_method(rb_cNameErrorMesg, "!", name_err_mesg_new, 3);
1049 rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1);
1050 rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0);
1051 rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_to_str, 1);
1052 rb_define_singleton_method(rb_cNameErrorMesg, "_load", name_err_mesg_load, 1);
1053 rb_eNoMethodError = rb_define_class("NoMethodError", rb_eNameError);
1054 rb_define_method(rb_eNoMethodError, "initialize", nometh_err_initialize, -1);
1055 rb_define_method(rb_eNoMethodError, "args", nometh_err_args, 0);
1057 rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError);
1058 rb_eSecurityError = rb_define_class("SecurityError", rb_eException);
1059 rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
1061 syserr_tbl = st_init_numtable();
1062 rb_eSystemCallError = rb_define_class("SystemCallError", rb_eStandardError);
1063 rb_define_method(rb_eSystemCallError, "initialize", syserr_initialize, -1);
1064 rb_define_method(rb_eSystemCallError, "errno", syserr_errno, 0);
1065 rb_define_singleton_method(rb_eSystemCallError, "===", syserr_eqq, 1);
1067 rb_mErrno = rb_define_module("Errno");
1068 rb_define_singleton_method(rb_mErrno, "const_missing", errno_missing, 1);
1070 rb_define_global_function("warn", rb_warn_m, 1);
1073 void
1074 rb_raise(VALUE exc, const char *fmt, ...)
1076 va_list args;
1077 VALUE mesg;
1079 va_start(args, fmt);
1080 mesg = rb_vsprintf(fmt, args);
1081 va_end(args);
1082 rb_exc_raise(rb_exc_new3(exc, mesg));
1085 void
1086 rb_loaderror(const char *fmt, ...)
1088 va_list args;
1089 VALUE mesg;
1091 va_start(args, fmt);
1092 mesg = rb_vsprintf(fmt, args);
1093 va_end(args);
1094 rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
1097 void
1098 rb_notimplement(void)
1100 rb_raise(rb_eNotImpError,
1101 "%s() function is unimplemented on this machine",
1102 rb_id2name(rb_frame_this_func()));
1105 void
1106 rb_fatal(const char *fmt, ...)
1108 va_list args;
1109 VALUE mesg;
1111 va_start(args, fmt);
1112 mesg = rb_vsprintf(fmt, args);
1113 va_end(args);
1115 rb_exc_fatal(rb_exc_new3(rb_eFatal, mesg));
1118 void
1119 rb_sys_fail(const char *mesg)
1121 int n = errno;
1122 VALUE arg;
1124 errno = 0;
1125 if (n == 0) {
1126 rb_bug("rb_sys_fail(%s) - errno == 0", mesg ? mesg : "");
1129 arg = mesg ? rb_str_new2(mesg) : Qnil;
1130 rb_exc_raise(rb_class_new_instance(1, &arg, get_syserr(n)));
1133 void
1134 rb_sys_warning(const char *fmt, ...)
1136 char buf[BUFSIZ];
1137 va_list args;
1138 int errno_save;
1140 errno_save = errno;
1142 if (!RTEST(ruby_verbose)) return;
1144 snprintf(buf, BUFSIZ, "warning: %s", fmt);
1145 snprintf(buf+strlen(buf), BUFSIZ-strlen(buf), ": %s", strerror(errno_save));
1147 va_start(args, fmt);
1148 warn_print(buf, args);
1149 va_end(args);
1150 errno = errno_save;
1153 void
1154 rb_load_fail(const char *path)
1156 rb_loaderror("%s -- %s", strerror(errno), path);
1159 void
1160 rb_error_frozen(const char *what)
1162 rb_raise(rb_eRuntimeError, "can't modify frozen %s", what);
1165 void
1166 rb_check_frozen(VALUE obj)
1168 if (OBJ_FROZEN(obj)) rb_error_frozen(rb_obj_classname(obj));
1171 void
1172 Init_syserr(void)
1174 #ifdef EPERM
1175 set_syserr(EPERM, "EPERM");
1176 #endif
1177 #ifdef ENOENT
1178 set_syserr(ENOENT, "ENOENT");
1179 #endif
1180 #ifdef ESRCH
1181 set_syserr(ESRCH, "ESRCH");
1182 #endif
1183 #ifdef EINTR
1184 set_syserr(EINTR, "EINTR");
1185 #endif
1186 #ifdef EIO
1187 set_syserr(EIO, "EIO");
1188 #endif
1189 #ifdef ENXIO
1190 set_syserr(ENXIO, "ENXIO");
1191 #endif
1192 #ifdef E2BIG
1193 set_syserr(E2BIG, "E2BIG");
1194 #endif
1195 #ifdef ENOEXEC
1196 set_syserr(ENOEXEC, "ENOEXEC");
1197 #endif
1198 #ifdef EBADF
1199 set_syserr(EBADF, "EBADF");
1200 #endif
1201 #ifdef ECHILD
1202 set_syserr(ECHILD, "ECHILD");
1203 #endif
1204 #ifdef EAGAIN
1205 set_syserr(EAGAIN, "EAGAIN");
1206 #endif
1207 #ifdef ENOMEM
1208 set_syserr(ENOMEM, "ENOMEM");
1209 #endif
1210 #ifdef EACCES
1211 set_syserr(EACCES, "EACCES");
1212 #endif
1213 #ifdef EFAULT
1214 set_syserr(EFAULT, "EFAULT");
1215 #endif
1216 #ifdef ENOTBLK
1217 set_syserr(ENOTBLK, "ENOTBLK");
1218 #endif
1219 #ifdef EBUSY
1220 set_syserr(EBUSY, "EBUSY");
1221 #endif
1222 #ifdef EEXIST
1223 set_syserr(EEXIST, "EEXIST");
1224 #endif
1225 #ifdef EXDEV
1226 set_syserr(EXDEV, "EXDEV");
1227 #endif
1228 #ifdef ENODEV
1229 set_syserr(ENODEV, "ENODEV");
1230 #endif
1231 #ifdef ENOTDIR
1232 set_syserr(ENOTDIR, "ENOTDIR");
1233 #endif
1234 #ifdef EISDIR
1235 set_syserr(EISDIR, "EISDIR");
1236 #endif
1237 #ifdef EINVAL
1238 set_syserr(EINVAL, "EINVAL");
1239 #endif
1240 #ifdef ENFILE
1241 set_syserr(ENFILE, "ENFILE");
1242 #endif
1243 #ifdef EMFILE
1244 set_syserr(EMFILE, "EMFILE");
1245 #endif
1246 #ifdef ENOTTY
1247 set_syserr(ENOTTY, "ENOTTY");
1248 #endif
1249 #ifdef ETXTBSY
1250 set_syserr(ETXTBSY, "ETXTBSY");
1251 #endif
1252 #ifdef EFBIG
1253 set_syserr(EFBIG, "EFBIG");
1254 #endif
1255 #ifdef ENOSPC
1256 set_syserr(ENOSPC, "ENOSPC");
1257 #endif
1258 #ifdef ESPIPE
1259 set_syserr(ESPIPE, "ESPIPE");
1260 #endif
1261 #ifdef EROFS
1262 set_syserr(EROFS, "EROFS");
1263 #endif
1264 #ifdef EMLINK
1265 set_syserr(EMLINK, "EMLINK");
1266 #endif
1267 #ifdef EPIPE
1268 set_syserr(EPIPE, "EPIPE");
1269 #endif
1270 #ifdef EDOM
1271 set_syserr(EDOM, "EDOM");
1272 #endif
1273 #ifdef ERANGE
1274 set_syserr(ERANGE, "ERANGE");
1275 #endif
1276 #ifdef EDEADLK
1277 set_syserr(EDEADLK, "EDEADLK");
1278 #endif
1279 #ifdef ENAMETOOLONG
1280 set_syserr(ENAMETOOLONG, "ENAMETOOLONG");
1281 #endif
1282 #ifdef ENOLCK
1283 set_syserr(ENOLCK, "ENOLCK");
1284 #endif
1285 #ifdef ENOSYS
1286 set_syserr(ENOSYS, "ENOSYS");
1287 #endif
1288 #ifdef ENOTEMPTY
1289 set_syserr(ENOTEMPTY, "ENOTEMPTY");
1290 #endif
1291 #ifdef ELOOP
1292 set_syserr(ELOOP, "ELOOP");
1293 #endif
1294 #ifdef EWOULDBLOCK
1295 set_syserr(EWOULDBLOCK, "EWOULDBLOCK");
1296 #endif
1297 #ifdef ENOMSG
1298 set_syserr(ENOMSG, "ENOMSG");
1299 #endif
1300 #ifdef EIDRM
1301 set_syserr(EIDRM, "EIDRM");
1302 #endif
1303 #ifdef ECHRNG
1304 set_syserr(ECHRNG, "ECHRNG");
1305 #endif
1306 #ifdef EL2NSYNC
1307 set_syserr(EL2NSYNC, "EL2NSYNC");
1308 #endif
1309 #ifdef EL3HLT
1310 set_syserr(EL3HLT, "EL3HLT");
1311 #endif
1312 #ifdef EL3RST
1313 set_syserr(EL3RST, "EL3RST");
1314 #endif
1315 #ifdef ELNRNG
1316 set_syserr(ELNRNG, "ELNRNG");
1317 #endif
1318 #ifdef EUNATCH
1319 set_syserr(EUNATCH, "EUNATCH");
1320 #endif
1321 #ifdef ENOCSI
1322 set_syserr(ENOCSI, "ENOCSI");
1323 #endif
1324 #ifdef EL2HLT
1325 set_syserr(EL2HLT, "EL2HLT");
1326 #endif
1327 #ifdef EBADE
1328 set_syserr(EBADE, "EBADE");
1329 #endif
1330 #ifdef EBADR
1331 set_syserr(EBADR, "EBADR");
1332 #endif
1333 #ifdef EXFULL
1334 set_syserr(EXFULL, "EXFULL");
1335 #endif
1336 #ifdef ENOANO
1337 set_syserr(ENOANO, "ENOANO");
1338 #endif
1339 #ifdef EBADRQC
1340 set_syserr(EBADRQC, "EBADRQC");
1341 #endif
1342 #ifdef EBADSLT
1343 set_syserr(EBADSLT, "EBADSLT");
1344 #endif
1345 #ifdef EDEADLOCK
1346 set_syserr(EDEADLOCK, "EDEADLOCK");
1347 #endif
1348 #ifdef EBFONT
1349 set_syserr(EBFONT, "EBFONT");
1350 #endif
1351 #ifdef ENOSTR
1352 set_syserr(ENOSTR, "ENOSTR");
1353 #endif
1354 #ifdef ENODATA
1355 set_syserr(ENODATA, "ENODATA");
1356 #endif
1357 #ifdef ETIME
1358 set_syserr(ETIME, "ETIME");
1359 #endif
1360 #ifdef ENOSR
1361 set_syserr(ENOSR, "ENOSR");
1362 #endif
1363 #ifdef ENONET
1364 set_syserr(ENONET, "ENONET");
1365 #endif
1366 #ifdef ENOPKG
1367 set_syserr(ENOPKG, "ENOPKG");
1368 #endif
1369 #ifdef EREMOTE
1370 set_syserr(EREMOTE, "EREMOTE");
1371 #endif
1372 #ifdef ENOLINK
1373 set_syserr(ENOLINK, "ENOLINK");
1374 #endif
1375 #ifdef EADV
1376 set_syserr(EADV, "EADV");
1377 #endif
1378 #ifdef ESRMNT
1379 set_syserr(ESRMNT, "ESRMNT");
1380 #endif
1381 #ifdef ECOMM
1382 set_syserr(ECOMM, "ECOMM");
1383 #endif
1384 #ifdef EPROTO
1385 set_syserr(EPROTO, "EPROTO");
1386 #endif
1387 #ifdef EMULTIHOP
1388 set_syserr(EMULTIHOP, "EMULTIHOP");
1389 #endif
1390 #ifdef EDOTDOT
1391 set_syserr(EDOTDOT, "EDOTDOT");
1392 #endif
1393 #ifdef EBADMSG
1394 set_syserr(EBADMSG, "EBADMSG");
1395 #endif
1396 #ifdef EOVERFLOW
1397 set_syserr(EOVERFLOW, "EOVERFLOW");
1398 #endif
1399 #ifdef ENOTUNIQ
1400 set_syserr(ENOTUNIQ, "ENOTUNIQ");
1401 #endif
1402 #ifdef EBADFD
1403 set_syserr(EBADFD, "EBADFD");
1404 #endif
1405 #ifdef EREMCHG
1406 set_syserr(EREMCHG, "EREMCHG");
1407 #endif
1408 #ifdef ELIBACC
1409 set_syserr(ELIBACC, "ELIBACC");
1410 #endif
1411 #ifdef ELIBBAD
1412 set_syserr(ELIBBAD, "ELIBBAD");
1413 #endif
1414 #ifdef ELIBSCN
1415 set_syserr(ELIBSCN, "ELIBSCN");
1416 #endif
1417 #ifdef ELIBMAX
1418 set_syserr(ELIBMAX, "ELIBMAX");
1419 #endif
1420 #ifdef ELIBEXEC
1421 set_syserr(ELIBEXEC, "ELIBEXEC");
1422 #endif
1423 #ifdef EILSEQ
1424 set_syserr(EILSEQ, "EILSEQ");
1425 #endif
1426 #ifdef ERESTART
1427 set_syserr(ERESTART, "ERESTART");
1428 #endif
1429 #ifdef ESTRPIPE
1430 set_syserr(ESTRPIPE, "ESTRPIPE");
1431 #endif
1432 #ifdef EUSERS
1433 set_syserr(EUSERS, "EUSERS");
1434 #endif
1435 #ifdef ENOTSOCK
1436 set_syserr(ENOTSOCK, "ENOTSOCK");
1437 #endif
1438 #ifdef EDESTADDRREQ
1439 set_syserr(EDESTADDRREQ, "EDESTADDRREQ");
1440 #endif
1441 #ifdef EMSGSIZE
1442 set_syserr(EMSGSIZE, "EMSGSIZE");
1443 #endif
1444 #ifdef EPROTOTYPE
1445 set_syserr(EPROTOTYPE, "EPROTOTYPE");
1446 #endif
1447 #ifdef ENOPROTOOPT
1448 set_syserr(ENOPROTOOPT, "ENOPROTOOPT");
1449 #endif
1450 #ifdef EPROTONOSUPPORT
1451 set_syserr(EPROTONOSUPPORT, "EPROTONOSUPPORT");
1452 #endif
1453 #ifdef ESOCKTNOSUPPORT
1454 set_syserr(ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT");
1455 #endif
1456 #ifdef EOPNOTSUPP
1457 set_syserr(EOPNOTSUPP, "EOPNOTSUPP");
1458 #endif
1459 #ifdef EPFNOSUPPORT
1460 set_syserr(EPFNOSUPPORT, "EPFNOSUPPORT");
1461 #endif
1462 #ifdef EAFNOSUPPORT
1463 set_syserr(EAFNOSUPPORT, "EAFNOSUPPORT");
1464 #endif
1465 #ifdef EADDRINUSE
1466 set_syserr(EADDRINUSE, "EADDRINUSE");
1467 #endif
1468 #ifdef EADDRNOTAVAIL
1469 set_syserr(EADDRNOTAVAIL, "EADDRNOTAVAIL");
1470 #endif
1471 #ifdef ENETDOWN
1472 set_syserr(ENETDOWN, "ENETDOWN");
1473 #endif
1474 #ifdef ENETUNREACH
1475 set_syserr(ENETUNREACH, "ENETUNREACH");
1476 #endif
1477 #ifdef ENETRESET
1478 set_syserr(ENETRESET, "ENETRESET");
1479 #endif
1480 #ifdef ECONNABORTED
1481 set_syserr(ECONNABORTED, "ECONNABORTED");
1482 #endif
1483 #ifdef ECONNRESET
1484 set_syserr(ECONNRESET, "ECONNRESET");
1485 #endif
1486 #ifdef ENOBUFS
1487 set_syserr(ENOBUFS, "ENOBUFS");
1488 #endif
1489 #ifdef EISCONN
1490 set_syserr(EISCONN, "EISCONN");
1491 #endif
1492 #ifdef ENOTCONN
1493 set_syserr(ENOTCONN, "ENOTCONN");
1494 #endif
1495 #ifdef ESHUTDOWN
1496 set_syserr(ESHUTDOWN, "ESHUTDOWN");
1497 #endif
1498 #ifdef ETOOMANYREFS
1499 set_syserr(ETOOMANYREFS, "ETOOMANYREFS");
1500 #endif
1501 #ifdef ETIMEDOUT
1502 set_syserr(ETIMEDOUT, "ETIMEDOUT");
1503 #endif
1504 #ifdef ECONNREFUSED
1505 set_syserr(ECONNREFUSED, "ECONNREFUSED");
1506 #endif
1507 #ifdef EHOSTDOWN
1508 set_syserr(EHOSTDOWN, "EHOSTDOWN");
1509 #endif
1510 #ifdef EHOSTUNREACH
1511 set_syserr(EHOSTUNREACH, "EHOSTUNREACH");
1512 #endif
1513 #ifdef EALREADY
1514 set_syserr(EALREADY, "EALREADY");
1515 #endif
1516 #ifdef EINPROGRESS
1517 set_syserr(EINPROGRESS, "EINPROGRESS");
1518 #endif
1519 #ifdef ESTALE
1520 set_syserr(ESTALE, "ESTALE");
1521 #endif
1522 #ifdef EUCLEAN
1523 set_syserr(EUCLEAN, "EUCLEAN");
1524 #endif
1525 #ifdef ENOTNAM
1526 set_syserr(ENOTNAM, "ENOTNAM");
1527 #endif
1528 #ifdef ENAVAIL
1529 set_syserr(ENAVAIL, "ENAVAIL");
1530 #endif
1531 #ifdef EISNAM
1532 set_syserr(EISNAM, "EISNAM");
1533 #endif
1534 #ifdef EREMOTEIO
1535 set_syserr(EREMOTEIO, "EREMOTEIO");
1536 #endif
1537 #ifdef EDQUOT
1538 set_syserr(EDQUOT, "EDQUOT");
1539 #endif
1540 rb_eNOERROR = set_syserr(0, "NOERROR");
1543 static void
1544 err_append(const char *s)
1546 rb_thread_t *th = GET_THREAD();
1547 VALUE err = th->errinfo;
1549 if (th->mild_compile_error) {
1550 if (!RTEST(err)) {
1551 err = rb_exc_new2(rb_eSyntaxError, s);
1552 th->errinfo = err;
1554 else {
1555 VALUE str = rb_obj_as_string(err);
1557 rb_str_cat2(str, "\n");
1558 rb_str_cat2(str, s);
1559 th->errinfo = rb_exc_new3(rb_eSyntaxError, str);
1562 else {
1563 if (!RTEST(err)) {
1564 err = rb_exc_new2(rb_eSyntaxError, "compile error");
1565 th->errinfo = err;
1567 rb_write_error(s);
1568 rb_write_error("\n");