error.c


DEFINITIONS

This source file includes following functions.
  1. err_snprintf
  2. err_print
  3. rb_compile_error
  4. rb_compile_error_append
  5. warn_print
  6. rb_warn
  7. rb_warning
  8. rb_bug
  9. rb_check_type
  10. rb_exc_new
  11. rb_exc_new2
  12. rb_exc_new3
  13. exc_initialize
  14. exc_exception
  15. exc_to_s
  16. exc_inspect
  17. exc_backtrace
  18. check_backtrace
  19. exc_set_backtrace
  20. exit_status
  21. rb_name_error
  22. name_err_name
  23. nometh_err_args
  24. rb_invalid_str
  25. SYSERR_LIST_B
  26. set_syserr
  27. syserr_errno
  28. syserr_eqq
  29. get_syserr
  30. Init_Exception
  31. rb_raise
  32. rb_loaderror
  33. rb_notimplement
  34. rb_fatal
  35. rb_sys_fail
  36. rb_sys_warning
  37. rb_load_fail
  38. rb_error_frozen
  39. rb_check_frozen
  40. init_syserr
  41. err_append


   1  /**********************************************************************
   2  
   3    error.c -
   4  
   5    $Author: matz $
   6    $Date: 2002/09/03 05:20:06 $
   7    created at: Mon Aug  9 16:11:34 JST 1993
   8  
   9    Copyright (C) 1993-2002 Yukihiro Matsumoto
  10  
  11  **********************************************************************/
  12  
  13  #include "ruby.h"
  14  #include "env.h"
  15  #include "version.h"
  16  
  17  #include <stdio.h>
  18  #ifdef HAVE_STDARG_PROTOTYPES
  19  #include <stdarg.h>
  20  #define va_init_list(a,b) va_start(a,b)
  21  #else
  22  #include <varargs.h>
  23  #define va_init_list(a,b) va_start(a)
  24  #endif
  25  
  26  #if defined __CYGWIN__
  27  # include <cygwin/version.h>
  28  # if (CYGWIN_VERSION_API_MAJOR > 0) || (CYGWIN_VERSION_API_MINOR >= 8)
  29  #  define sys_nerr _sys_nerr
  30  # endif
  31  #endif
  32  
  33  int ruby_nerrs;
  34  
  35  static void
  36  err_snprintf(buf, len, fmt, args)
  37      char *buf, *fmt;
  38      long len;
  39      va_list args;
  40  {
  41      long n;
  42  
  43      ruby_set_current_source();
  44      if (!ruby_sourcefile) {
  45          vsnprintf(buf, len, fmt, args);
  46          return;
  47      }
  48      else if (ruby_sourceline == 0) {
  49          n = snprintf(buf, len, "%s: ", ruby_sourcefile);
  50      }
  51      else {
  52          n = snprintf(buf, len, "%s:%d: ", ruby_sourcefile, ruby_sourceline);
  53      }
  54      if (len > n) {
  55          vsnprintf((char*)buf+n, len-n, fmt, args);
  56      }
  57  }
  58  
  59  static void err_append _((const char*));
  60  static void
  61  err_print(fmt, args)
  62      const char *fmt;
  63      va_list args;
  64  {
  65      char buf[BUFSIZ];
  66  
  67      err_snprintf(buf, BUFSIZ, fmt, args);
  68      err_append(buf);
  69  }
  70  
  71  void
  72  #ifdef HAVE_STDARG_PROTOTYPES
  73  rb_compile_error(const char *fmt, ...)
  74  #else
  75  rb_compile_error(fmt, va_alist)
  76      const char *fmt;
  77      va_dcl
  78  #endif
  79  {
  80      va_list args;
  81  
  82      va_init_list(args, fmt);
  83      err_print(fmt, args);
  84      va_end(args);
  85      ruby_nerrs++;
  86  }
  87  
  88  void
  89  #ifdef HAVE_STDARG_PROTOTYPES
  90  rb_compile_error_append(const char *fmt, ...)
  91  #else
  92  rb_compile_error_append(fmt, va_alist)
  93      const char *fmt;
  94      va_dcl
  95  #endif
  96  {
  97      va_list args;
  98      char buf[BUFSIZ];
  99  
 100      va_init_list(args, fmt);
 101      vsnprintf(buf, BUFSIZ, fmt, args);
 102      va_end(args);
 103      err_append(buf);
 104  }
 105  
 106  static void
 107  warn_print(fmt, args)
 108      const char *fmt;
 109      va_list args;
 110  {
 111      char buf[BUFSIZ];
 112  
 113      err_snprintf(buf, BUFSIZ, fmt, args);
 114      fputs(buf, stderr);
 115      fputs("\n", stderr);
 116      fflush(stderr);
 117  }
 118  
 119  void
 120  #ifdef HAVE_STDARG_PROTOTYPES
 121  rb_warn(const char *fmt, ...)
 122  #else
 123  rb_warn(fmt, va_alist)
 124      const char *fmt;
 125      va_dcl
 126  #endif
 127  {
 128      char buf[BUFSIZ];
 129      va_list args;
 130  
 131      snprintf(buf, BUFSIZ, "warning: %s", fmt);
 132  
 133      va_init_list(args, fmt);
 134      warn_print(buf, args);
 135      va_end(args);
 136  }
 137  
 138  /* rb_warning() reports only in verbose mode */
 139  void
 140  #ifdef HAVE_STDARG_PROTOTYPES
 141  rb_warning(const char *fmt, ...)
 142  #else
 143  rb_warning(fmt, va_alist)
 144      const char *fmt;
 145      va_dcl
 146  #endif
 147  {
 148      char buf[BUFSIZ];
 149      va_list args;
 150  
 151      if (!RTEST(ruby_verbose)) return;
 152  
 153      snprintf(buf, BUFSIZ, "warning: %s", fmt);
 154  
 155      va_init_list(args, fmt);
 156      warn_print(buf, args);
 157      va_end(args);
 158  }
 159  
 160  void
 161  #ifdef HAVE_STDARG_PROTOTYPES
 162  rb_bug(const char *fmt, ...)
 163  #else
 164  rb_bug(fmt, va_alist)
 165      const char *fmt;
 166      va_dcl
 167  #endif
 168  {
 169      char buf[BUFSIZ];
 170      va_list args;
 171  
 172      snprintf(buf, BUFSIZ, "[BUG] %s", fmt);
 173      ruby_in_eval = 0;
 174  
 175      va_init_list(args, fmt);
 176      warn_print(buf, args);
 177      va_end(args);
 178      fprintf(stderr, "ruby %s (%s) [%s]\n", RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_PLATFORM);
 179      abort();
 180  }
 181  
 182  static struct types {
 183      int type;
 184      const char *name;
 185  } builtin_types[] = {
 186      {T_NIL,     "nil"},
 187      {T_OBJECT,  "Object"},
 188      {T_CLASS,   "Class"},
 189      {T_ICLASS,  "iClass"},      /* internal use: mixed-in module holder */
 190      {T_MODULE,  "Module"},
 191      {T_FLOAT,   "Float"},
 192      {T_STRING,  "String"},
 193      {T_REGEXP,  "Regexp"},
 194      {T_ARRAY,   "Array"},
 195      {T_FIXNUM,  "Fixnum"},
 196      {T_HASH,    "Hash"},
 197      {T_STRUCT,  "Struct"},
 198      {T_BIGNUM,  "Bignum"},
 199      {T_FILE,    "File"},
 200      {T_TRUE,    "true"},
 201      {T_FALSE,   "false"},
 202      {T_SYMBOL,  "Symbol"},      /* :symbol */
 203      {T_DATA,    "Data"},        /* internal use: wrapped C pointers */
 204      {T_MATCH,   "MatchData"},   /* data of $~ */
 205      {T_VARMAP,  "Varmap"},      /* internal use: dynamic variables */
 206      {T_SCOPE,   "Scope"},       /* internal use: variable scope */
 207      {T_NODE,    "Node"},        /* internal use: syntax tree node */
 208      {T_UNDEF,   "undef"},       /* internal use: #undef; should not happen */
 209      {-1,        0}
 210  };
 211  
 212  void
 213  rb_check_type(x, t)
 214      VALUE x;
 215      int t;
 216  {
 217      struct types *type = builtin_types;
 218  
 219      if (x == Qundef) {
 220          rb_bug("undef leaked to the Ruby space");
 221      }
 222  
 223      if (TYPE(x) != t) {
 224          while (type->type >= 0) {
 225              if (type->type == t) {
 226                  char *etype;
 227  
 228                  if (NIL_P(x)) {
 229                      etype = "nil";
 230                  }
 231                  else if (FIXNUM_P(x)) {
 232                      etype = "Fixnum";
 233                  }
 234                  else if (rb_special_const_p(x)) {
 235                      etype = RSTRING(rb_obj_as_string(x))->ptr;
 236                  }
 237                  else {
 238                      etype = rb_class2name(CLASS_OF(x));
 239                  }
 240                  rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
 241                           etype, type->name);
 242              }
 243              type++;
 244          }
 245          rb_bug("unknown type 0x%x", t);
 246      }
 247  }
 248  
 249  /* exception classes */
 250  #include <errno.h>
 251  
 252  VALUE rb_eException;
 253  VALUE rb_eSystemExit;
 254  VALUE rb_eInterrupt;
 255  VALUE rb_eSignal;
 256  VALUE rb_eFatal;
 257  VALUE rb_eStandardError;
 258  VALUE rb_eRuntimeError;
 259  VALUE rb_eTypeError;
 260  VALUE rb_eArgError;
 261  VALUE rb_eIndexError;
 262  VALUE rb_eRangeError;
 263  VALUE rb_eNameError;
 264  VALUE rb_eNoMethodError;
 265  VALUE rb_eSecurityError;
 266  VALUE rb_eNotImpError;
 267  VALUE rb_eNoMemError;
 268  
 269  VALUE rb_eScriptError;
 270  VALUE rb_eSyntaxError;
 271  VALUE rb_eLoadError;
 272  
 273  VALUE rb_eSystemCallError;
 274  VALUE rb_mErrno;
 275  
 276  VALUE
 277  rb_exc_new(etype, ptr, len)
 278      VALUE etype;
 279      const char *ptr;
 280      long len;
 281  {
 282      return rb_funcall(etype, rb_intern("new"), 1, rb_str_new(ptr, len));
 283  }
 284  
 285  VALUE
 286  rb_exc_new2(etype, s)
 287      VALUE etype;
 288      const char *s;
 289  {
 290      return rb_exc_new(etype, s, strlen(s));
 291  }
 292  
 293  VALUE
 294  rb_exc_new3(etype, str)
 295      VALUE etype, str;
 296  {
 297      StringValue(str);
 298      return rb_exc_new(etype, RSTRING(str)->ptr, RSTRING(str)->len);
 299  }
 300  
 301  static VALUE
 302  exc_initialize(argc, argv, exc)
 303      int argc;
 304      VALUE *argv;
 305      VALUE exc;
 306  {
 307      VALUE mesg;
 308  
 309      if (rb_scan_args(argc, argv, "01", &mesg) == 1) {
 310          StringValue(mesg);      /* ensure mesg can be converted to String */
 311      }
 312      rb_iv_set(exc, "mesg", mesg);
 313      rb_iv_set(exc, "bt", Qnil);
 314  
 315      return exc;
 316  }
 317  
 318  static VALUE
 319  exc_exception(argc, argv, self)
 320      int argc;
 321      VALUE *argv;
 322      VALUE self;
 323  {
 324      VALUE exc;
 325  
 326      if (argc == 0) return self;
 327      if (argc == 1 && self == argv[0]) return self;
 328      exc = rb_obj_clone(self);
 329      exc_initialize(argc, argv, exc);
 330  
 331      return exc;
 332  }
 333  
 334  static VALUE
 335  exc_to_s(exc)
 336      VALUE exc;
 337  {
 338      VALUE mesg = rb_iv_get(exc, "mesg");
 339  
 340      if (NIL_P(mesg)) return rb_class_path(CLASS_OF(exc));
 341      if (OBJ_TAINTED(exc)) OBJ_TAINT(mesg);
 342      return mesg;
 343  }
 344  
 345  static VALUE
 346  exc_inspect(exc)
 347      VALUE exc;
 348  {
 349      VALUE str, klass;
 350  
 351      klass = CLASS_OF(exc);
 352      exc = rb_obj_as_string(exc);
 353      if (RSTRING(exc)->len == 0) {
 354          return rb_str_dup(rb_class_path(klass));
 355      }
 356  
 357      str = rb_str_buf_new2("#<");
 358      klass = rb_class_path(klass);
 359      rb_str_buf_append(str, klass);
 360      rb_str_buf_cat(str, ": ", 2);
 361      rb_str_buf_append(str, exc);
 362      rb_str_buf_cat(str, ">", 1);
 363  
 364      return str;
 365  }
 366  
 367  static VALUE
 368  exc_backtrace(exc)
 369      VALUE exc;
 370  {
 371      ID bt = rb_intern("bt");
 372  
 373      if (!rb_ivar_defined(exc, bt)) return Qnil;
 374      return rb_ivar_get(exc, bt);
 375  }
 376  
 377  static VALUE
 378  check_backtrace(bt)
 379      VALUE bt;
 380  {
 381      long i;
 382      static char *err = "backtrace must be Array of String";
 383  
 384      if (!NIL_P(bt)) {
 385          int t = TYPE(bt);
 386  
 387          if (t == T_STRING) return rb_ary_new3(1, bt);
 388          if (t != T_ARRAY) {
 389              rb_raise(rb_eTypeError, err);
 390          }
 391          for (i=0;i<RARRAY(bt)->len;i++) {
 392              if (TYPE(RARRAY(bt)->ptr[i]) != T_STRING) {
 393                  rb_raise(rb_eTypeError, err);
 394              }
 395          }
 396      }
 397      return bt;
 398  }
 399  
 400  static VALUE
 401  exc_set_backtrace(exc, bt)
 402      VALUE exc;
 403      VALUE bt;
 404  {
 405      return rb_iv_set(exc, "bt", check_backtrace(bt));
 406  }
 407  
 408  static VALUE
 409  exit_status(exc)
 410      VALUE exc;
 411  {
 412      return rb_iv_get(exc, "status");
 413  }
 414  
 415  void
 416  #ifdef HAVE_STDARG_PROTOTYPES
 417  rb_name_error(ID id, const char *fmt, ...)
 418  #else
 419  rb_name_error(id, fmt, va_alist)
 420      ID id;
 421      const char *fmt;
 422      va_dcl
 423  #endif
 424  {
 425      VALUE exc;
 426  
 427      va_list args;
 428      char buf[BUFSIZ];
 429  
 430      va_init_list(args, fmt);
 431      vsnprintf(buf, BUFSIZ, fmt, args);
 432      va_end(args);
 433      exc = rb_exc_new2(rb_eNameError, buf);
 434      rb_iv_set(exc, "name", ID2SYM(id));
 435      rb_exc_raise(exc);
 436  }
 437  
 438  static VALUE
 439  name_err_name(self)
 440      VALUE self;
 441  {
 442      return rb_iv_get(self, "name");
 443  }
 444  
 445  static VALUE
 446  nometh_err_args(self)
 447      VALUE self;
 448  {
 449      return rb_iv_get(self, "args");
 450  }
 451  
 452  void
 453  rb_invalid_str(str, type)
 454      const char *str, *type;
 455  {
 456      VALUE s = rb_str_inspect(rb_str_new2(str));
 457  
 458      rb_raise(rb_eArgError, "invalid value for %s: %s", type, RSTRING(s)->ptr);
 459  }
 460  
 461  #ifdef __BEOS__
 462  typedef struct {
 463     VALUE *list;
 464     int n;
 465  } syserr_list_entry;
 466  
 467  typedef struct {
 468     int ix;
 469     int n;
 470  } syserr_index_entry;
 471  
 472  static VALUE syserr_error;
 473  static VALUE syserr_list_b_general[16+1];
 474  static VALUE syserr_list_b_os0[2+1];
 475  static VALUE syserr_list_b_os1[5+1];
 476  static VALUE syserr_list_b_os2[2+1];
 477  static VALUE syserr_list_b_os3[3+1];
 478  static VALUE syserr_list_b_os4[1+1];
 479  static VALUE syserr_list_b_app[15+1];
 480  static VALUE syserr_list_b_interface[0+1];
 481  static VALUE syserr_list_b_media[8+1];
 482  static VALUE syserr_list_b_midi[0+1];
 483  static VALUE syserr_list_b_storage[15+1];
 484  static VALUE syserr_list_b_posix[38+1];
 485  static VALUE syserr_list_b_mail[8+1];
 486  static VALUE syserr_list_b_print[1+1];
 487  static VALUE syserr_list_b_device[14+1];
 488  
 489  # define SYSERR_LIST_B(n) {(n), sizeof(n)/sizeof(VALUE)}
 490  static const syserr_list_entry syserr_list[] = {
 491     SYSERR_LIST_B(syserr_list_b_general),
 492     SYSERR_LIST_B(syserr_list_b_os0),
 493     SYSERR_LIST_B(syserr_list_b_os1),
 494     SYSERR_LIST_B(syserr_list_b_os2),
 495     SYSERR_LIST_B(syserr_list_b_os3),
 496     SYSERR_LIST_B(syserr_list_b_os4),
 497     SYSERR_LIST_B(syserr_list_b_app),
 498     SYSERR_LIST_B(syserr_list_b_interface),
 499     SYSERR_LIST_B(syserr_list_b_media),
 500     SYSERR_LIST_B(syserr_list_b_midi),
 501     SYSERR_LIST_B(syserr_list_b_storage),
 502     SYSERR_LIST_B(syserr_list_b_posix),
 503     SYSERR_LIST_B(syserr_list_b_mail),
 504     SYSERR_LIST_B(syserr_list_b_print),
 505     SYSERR_LIST_B(syserr_list_b_device),
 506  };
 507  # undef SYSERR_LIST_B
 508  
 509  static const syserr_index_entry syserr_index[]= {
 510       {0, 1},  {1, 5},  {6, 1},  {7, 1}, {8, 1}, {9, 1}, {10, 1}, {11, 1},
 511       {12, 1}, {13, 1}, {14, 1}, {0, 0},
 512  };
 513  #else
 514  static VALUE *syserr_list;
 515  #endif
 516  
 517  #if !HAVE_DECL_SYS_NERR
 518  extern int sys_nerr;
 519  #endif
 520  
 521  static VALUE
 522  set_syserr(i, name)
 523      int i;
 524      const char *name;
 525  {
 526  #ifdef __BEOS__
 527     VALUE *list;
 528     int ix, offset;
 529  #endif
 530      VALUE error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError);
 531      rb_define_const(error, "Errno", INT2NUM(i));
 532  #ifdef __BEOS__
 533     if (i == B_ERROR) {
 534        syserr_error = error;
 535        rb_global_variable(&syserr_error);
 536        return error;
 537     }
 538     i -= B_GENERAL_ERROR_BASE;
 539     ix = (i >> 12) & 0xf;
 540     offset = (i >> 8) & 0xf;
 541     if (offset < syserr_index[ix].n) {
 542        ix = syserr_index[ix].ix;
 543        if ((i & 0xff) < syserr_list[ix + offset].n) {
 544           list = syserr_list[ix + offset].list;
 545           list[i & 0xff] = error;
 546           rb_global_variable(&list[i & 0xff]);
 547        }
 548     }
 549  #else
 550      if (i <= sys_nerr) {
 551          syserr_list[i] = error;
 552      }
 553  #endif
 554      return error;
 555  }
 556  
 557  static VALUE
 558  syserr_errno(self)
 559      VALUE self;
 560  {
 561      return rb_iv_get(self, "errno");
 562  }
 563  
 564  static VALUE
 565  syserr_eqq(self, exc)
 566      VALUE self, exc;
 567  {
 568      VALUE num;
 569  
 570      if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) return Qfalse;
 571      if (self == rb_eSystemCallError) return Qtrue;
 572  
 573      num = rb_iv_get(exc, "errno");
 574      if (NIL_P(num)) {
 575          VALUE klass = CLASS_OF(exc);
 576  
 577          while (TYPE(klass) == T_ICLASS || FL_TEST(klass, FL_SINGLETON)) {
 578              klass = (VALUE)RCLASS(klass)->super;
 579          }
 580          num = rb_const_get(klass, rb_intern("Errno"));
 581      }
 582      if (rb_const_get(self, rb_intern("Errno")) == num)
 583          return Qtrue;
 584      return Qfalse;
 585  }
 586  
 587  #ifdef __BEOS__
 588  static VALUE
 589  get_syserr(int i)
 590  {
 591     VALUE *list;
 592     int ix, offset;
 593     
 594     if (i == B_ERROR) return syserr_error;
 595     i -= B_GENERAL_ERROR_BASE;
 596     ix = (i >> 12) & 0xf;
 597     offset = (i >> 8) & 0xf;
 598     if (offset < syserr_index[ix].n) {
 599        ix = syserr_index[ix].ix;
 600        if ((i & 0xff) < syserr_list[ix + offset].n) {
 601           list = syserr_list[ix + offset].list;
 602           return list[i & 0xff];
 603        }
 604     }
 605     return 0;
 606  }
 607  #endif /* __BEOS__ */
 608  
 609  static void init_syserr _((void));
 610  
 611  void
 612  Init_Exception()
 613  {
 614      rb_eException   = rb_define_class("Exception", rb_cObject);
 615      rb_define_singleton_method(rb_eException, "exception", rb_class_new_instance, -1);
 616      rb_define_method(rb_eException, "exception", exc_exception, -1);
 617      rb_define_method(rb_eException, "initialize", exc_initialize, -1);
 618      rb_define_method(rb_eException, "to_s", exc_to_s, 0);
 619      rb_define_method(rb_eException, "to_str", exc_to_s, 0);
 620      rb_define_method(rb_eException, "message", exc_to_s, 0);
 621      rb_define_method(rb_eException, "inspect", exc_inspect, 0);
 622      rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
 623      rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
 624  
 625      rb_eSystemExit  = rb_define_class("SystemExit", rb_eException);
 626      rb_define_method(rb_eSystemExit, "status", exit_status, 0);
 627  
 628      rb_eFatal       = rb_define_class("fatal", rb_eException);
 629      rb_eSignal      = rb_define_class("SignalException", rb_eException);
 630      rb_eInterrupt   = rb_define_class("Interrupt", rb_eSignal);
 631  
 632      rb_eStandardError = rb_define_class("StandardError", rb_eException);
 633      rb_eTypeError     = rb_define_class("TypeError", rb_eStandardError);
 634      rb_eArgError      = rb_define_class("ArgumentError", rb_eStandardError);
 635      rb_eIndexError    = rb_define_class("IndexError", rb_eStandardError);
 636      rb_eRangeError    = rb_define_class("RangeError", rb_eStandardError);
 637      rb_eNameError     = rb_define_class("NameError", rb_eStandardError);
 638      rb_define_method(rb_eNameError, "name", name_err_name, 0);
 639      rb_eNoMethodError = rb_define_class("NoMethodError", rb_eNameError);
 640      rb_define_method(rb_eNoMethodError, "args", nometh_err_args, 0);
 641  
 642      rb_eScriptError = rb_define_class("ScriptError", rb_eException);
 643      rb_eSyntaxError = rb_define_class("SyntaxError", rb_eScriptError);
 644      rb_eLoadError   = rb_define_class("LoadError", rb_eScriptError);
 645      rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
 646  
 647      rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError);
 648      rb_eSecurityError = rb_define_class("SecurityError", rb_eStandardError);
 649      rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
 650  
 651      init_syserr();
 652  }
 653  
 654  void
 655  #ifdef HAVE_STDARG_PROTOTYPES
 656  rb_raise(VALUE exc, const char *fmt, ...)
 657  #else
 658  rb_raise(exc, fmt, va_alist)
 659      VALUE exc;
 660      const char *fmt;
 661      va_dcl
 662  #endif
 663  {
 664      va_list args;
 665      char buf[BUFSIZ];
 666  
 667      va_init_list(args,fmt);
 668      vsnprintf(buf, BUFSIZ, fmt, args);
 669      va_end(args);
 670      rb_exc_raise(rb_exc_new2(exc, buf));
 671  }
 672  
 673  void
 674  #ifdef HAVE_STDARG_PROTOTYPES
 675  rb_loaderror(const char *fmt, ...)
 676  #else
 677  rb_loaderror(fmt, va_alist)
 678      const char *fmt;
 679      va_dcl
 680  #endif
 681  {
 682      va_list args;
 683      char buf[BUFSIZ];
 684  
 685      va_init_list(args, fmt);
 686      vsnprintf(buf, BUFSIZ, fmt, args);
 687      va_end(args);
 688      rb_exc_raise(rb_exc_new2(rb_eLoadError, buf));
 689  }
 690  
 691  void
 692  rb_notimplement()
 693  {
 694      rb_raise(rb_eNotImpError,
 695               "The %s() function is unimplemented on this machine",
 696               rb_id2name(ruby_frame->last_func));
 697  }
 698  
 699  void
 700  #ifdef HAVE_STDARG_PROTOTYPES
 701  rb_fatal(const char *fmt, ...)
 702  #else
 703  rb_fatal(fmt, va_alist)
 704      const char *fmt;
 705      va_dcl
 706  #endif
 707  {
 708      va_list args;
 709      char buf[BUFSIZ];
 710  
 711      va_init_list(args, fmt);
 712      vsnprintf(buf, BUFSIZ, fmt, args);
 713      va_end(args);
 714  
 715      ruby_in_eval = 0;
 716      rb_exc_fatal(rb_exc_new2(rb_eFatal, buf));
 717  }
 718  
 719  void
 720  rb_sys_fail(mesg)
 721      const char *mesg;
 722  {
 723  #if !defined(NT) && !defined(__VMS)
 724      char *strerror();
 725  #endif
 726      char *err;
 727      char *buf;
 728      extern int errno;
 729      int n = errno;
 730      VALUE ee;
 731  
 732      if (errno == 0) {
 733          rb_bug("rb_sys_fail() - errno == 0");
 734      }
 735  
 736  #ifdef __BORLANDC__
 737      if (errno == EPIPE) return; // (*moriq*)
 738  #endif
 739      err = strerror(errno);
 740      if (mesg) {
 741          volatile VALUE tmp = rb_str_inspect(rb_str_new2(mesg));
 742  
 743          buf = ALLOCA_N(char, strlen(err)+RSTRING(tmp)->len+4);
 744          sprintf(buf, "%s - %s", err, RSTRING(tmp)->ptr);
 745      }
 746      else {
 747          buf = ALLOCA_N(char, strlen(err)+1);
 748          strcpy(buf, err);
 749      }
 750  
 751      errno = 0;
 752  #ifdef __BEOS__
 753      ee = get_syserr(n);
 754      if (!ee) {
 755          char name[12];
 756        
 757          sprintf(name, "E%03d", n);
 758          ee = set_syserr(n, name);
 759     }
 760  #else
 761      if (n > sys_nerr || !syserr_list[n]) {
 762          char name[12];
 763  
 764          sprintf(name, "E%03d", n);
 765          ee = set_syserr(n, name);
 766      }
 767      else {
 768          ee = syserr_list[n];
 769      }
 770  #endif
 771      ee = rb_exc_new2(ee, buf);
 772      rb_iv_set(ee, "errno", INT2NUM(n));
 773      rb_exc_raise(ee);
 774  }
 775  
 776  void
 777  #ifdef HAVE_STDARG_PROTOTYPES
 778  rb_sys_warning(const char *fmt, ...)
 779  #else
 780  rb_sys_warning(fmt, va_alist)
 781       const char *fmt;
 782       va_dcl
 783  #endif
 784  {
 785       char buf[BUFSIZ];
 786       va_list args;
 787       int errno_save;
 788       
 789       errno_save = errno;
 790  
 791       if (!RTEST(ruby_verbose)) return;
 792  
 793       snprintf(buf, BUFSIZ, "warning: %s", fmt);
 794       snprintf(buf+strlen(buf), BUFSIZ-strlen(buf), ": %s", strerror(errno_save));
 795       
 796       va_init_list(args, fmt);
 797       warn_print(buf, args);
 798       va_end(args);
 799       errno = errno_save;
 800  }
 801  
 802  void
 803  rb_load_fail(path)
 804      char *path;
 805  {
 806      rb_loaderror("%s -- %s", strerror(errno), path);
 807  }
 808  
 809  void
 810  rb_error_frozen(what)
 811      char *what;
 812  {
 813      rb_raise(rb_eTypeError, "can't modify frozen %s", what);
 814  }
 815  
 816  void
 817  rb_check_frozen(obj)
 818      VALUE obj;
 819  {
 820      if (OBJ_FROZEN(obj)) rb_error_frozen(rb_class2name(CLASS_OF(obj)));
 821  }
 822  
 823  static void
 824  init_syserr()
 825  {
 826  #ifdef __BEOS__
 827     int i, ix, offset;
 828  #endif
 829      rb_eSystemCallError = rb_define_class("SystemCallError", rb_eStandardError);
 830      rb_define_method(rb_eSystemCallError, "errno", syserr_errno, 0);
 831      rb_define_singleton_method(rb_eSystemCallError, "===", syserr_eqq, 1);
 832  
 833      rb_mErrno = rb_define_module("Errno");
 834  #ifdef __BEOS__
 835     for (i = 0; syserr_index[i].n != 0; i++) {
 836        ix = syserr_index[i].ix;
 837        for (offset = 0; offset < syserr_index[i].n; offset++) {
 838           MEMZERO(syserr_list[ix + offset].list, VALUE, syserr_list[ix + offset].n);
 839        }
 840     }
 841     set_syserr(B_ERROR, "ERROR");
 842  #else
 843      syserr_list = ALLOC_N(VALUE, sys_nerr+1);
 844      MEMZERO(syserr_list, VALUE, sys_nerr+1);
 845  #endif
 846  
 847  #ifdef EPERM
 848      set_syserr(EPERM, "EPERM");
 849  #endif
 850  #ifdef ENOENT
 851      set_syserr(ENOENT, "ENOENT");
 852  #endif
 853  #ifdef ESRCH
 854      set_syserr(ESRCH, "ESRCH");
 855  #endif
 856  #ifdef EINTR
 857      set_syserr(EINTR, "EINTR");
 858  #endif
 859  #ifdef EIO
 860      set_syserr(EIO, "EIO");
 861  #endif
 862  #ifdef ENXIO
 863      set_syserr(ENXIO, "ENXIO");
 864  #endif
 865  #ifdef E2BIG
 866      set_syserr(E2BIG, "E2BIG");
 867  #endif
 868  #ifdef ENOEXEC
 869      set_syserr(ENOEXEC, "ENOEXEC");
 870  #endif
 871  #ifdef EBADF
 872      set_syserr(EBADF, "EBADF");
 873  #endif
 874  #ifdef ECHILD
 875      set_syserr(ECHILD, "ECHILD");
 876  #endif
 877  #ifdef EAGAIN
 878      set_syserr(EAGAIN, "EAGAIN");
 879  #endif
 880  #ifdef ENOMEM
 881      set_syserr(ENOMEM, "ENOMEM");
 882  #endif
 883  #ifdef EACCES
 884      set_syserr(EACCES, "EACCES");
 885  #endif
 886  #ifdef EFAULT
 887      set_syserr(EFAULT, "EFAULT");
 888  #endif
 889  #ifdef ENOTBLK
 890      set_syserr(ENOTBLK, "ENOTBLK");
 891  #endif
 892  #ifdef EBUSY
 893      set_syserr(EBUSY, "EBUSY");
 894  #endif
 895  #ifdef EEXIST
 896      set_syserr(EEXIST, "EEXIST");
 897  #endif
 898  #ifdef EXDEV
 899      set_syserr(EXDEV, "EXDEV");
 900  #endif
 901  #ifdef ENODEV
 902      set_syserr(ENODEV, "ENODEV");
 903  #endif
 904  #ifdef ENOTDIR
 905      set_syserr(ENOTDIR, "ENOTDIR");
 906  #endif
 907  #ifdef EISDIR
 908      set_syserr(EISDIR, "EISDIR");
 909  #endif
 910  #ifdef EINVAL
 911      set_syserr(EINVAL, "EINVAL");
 912  #endif
 913  #ifdef ENFILE
 914      set_syserr(ENFILE, "ENFILE");
 915  #endif
 916  #ifdef EMFILE
 917      set_syserr(EMFILE, "EMFILE");
 918  #endif
 919  #ifdef ENOTTY
 920      set_syserr(ENOTTY, "ENOTTY");
 921  #endif
 922  #ifdef ETXTBSY
 923      set_syserr(ETXTBSY, "ETXTBSY");
 924  #endif
 925  #ifdef EFBIG
 926      set_syserr(EFBIG, "EFBIG");
 927  #endif
 928  #ifdef ENOSPC
 929      set_syserr(ENOSPC, "ENOSPC");
 930  #endif
 931  #ifdef ESPIPE
 932      set_syserr(ESPIPE, "ESPIPE");
 933  #endif
 934  #ifdef EROFS
 935      set_syserr(EROFS, "EROFS");
 936  #endif
 937  #ifdef EMLINK
 938      set_syserr(EMLINK, "EMLINK");
 939  #endif
 940  #ifdef EPIPE
 941      set_syserr(EPIPE, "EPIPE");
 942  #endif
 943  #ifdef EDOM
 944      set_syserr(EDOM, "EDOM");
 945  #endif
 946  #ifdef ERANGE
 947      set_syserr(ERANGE, "ERANGE");
 948  #endif
 949  #ifdef EDEADLK
 950      set_syserr(EDEADLK, "EDEADLK");
 951  #endif
 952  #ifdef ENAMETOOLONG
 953      set_syserr(ENAMETOOLONG, "ENAMETOOLONG");
 954  #endif
 955  #ifdef ENOLCK
 956      set_syserr(ENOLCK, "ENOLCK");
 957  #endif
 958  #ifdef ENOSYS
 959      set_syserr(ENOSYS, "ENOSYS");
 960  #endif
 961  #ifdef ENOTEMPTY
 962      set_syserr(ENOTEMPTY, "ENOTEMPTY");
 963  #endif
 964  #ifdef ELOOP
 965      set_syserr(ELOOP, "ELOOP");
 966  #endif
 967  #ifdef EWOULDBLOCK
 968      set_syserr(EWOULDBLOCK, "EWOULDBLOCK");
 969  #endif
 970  #ifdef ENOMSG
 971      set_syserr(ENOMSG, "ENOMSG");
 972  #endif
 973  #ifdef EIDRM
 974      set_syserr(EIDRM, "EIDRM");
 975  #endif
 976  #ifdef ECHRNG
 977      set_syserr(ECHRNG, "ECHRNG");
 978  #endif
 979  #ifdef EL2NSYNC
 980      set_syserr(EL2NSYNC, "EL2NSYNC");
 981  #endif
 982  #ifdef EL3HLT
 983      set_syserr(EL3HLT, "EL3HLT");
 984  #endif
 985  #ifdef EL3RST
 986      set_syserr(EL3RST, "EL3RST");
 987  #endif
 988  #ifdef ELNRNG
 989      set_syserr(ELNRNG, "ELNRNG");
 990  #endif
 991  #ifdef EUNATCH
 992      set_syserr(EUNATCH, "EUNATCH");
 993  #endif
 994  #ifdef ENOCSI
 995      set_syserr(ENOCSI, "ENOCSI");
 996  #endif
 997  #ifdef EL2HLT
 998      set_syserr(EL2HLT, "EL2HLT");
 999  #endif
1000  #ifdef EBADE
1001      set_syserr(EBADE, "EBADE");
1002  #endif
1003  #ifdef EBADR
1004      set_syserr(EBADR, "EBADR");
1005  #endif
1006  #ifdef EXFULL
1007      set_syserr(EXFULL, "EXFULL");
1008  #endif
1009  #ifdef ENOANO
1010      set_syserr(ENOANO, "ENOANO");
1011  #endif
1012  #ifdef EBADRQC
1013      set_syserr(EBADRQC, "EBADRQC");
1014  #endif
1015  #ifdef EBADSLT
1016      set_syserr(EBADSLT, "EBADSLT");
1017  #endif
1018  #ifdef EDEADLOCK
1019      set_syserr(EDEADLOCK, "EDEADLOCK");
1020  #endif
1021  #ifdef EBFONT
1022      set_syserr(EBFONT, "EBFONT");
1023  #endif
1024  #ifdef ENOSTR
1025      set_syserr(ENOSTR, "ENOSTR");
1026  #endif
1027  #ifdef ENODATA
1028      set_syserr(ENODATA, "ENODATA");
1029  #endif
1030  #ifdef ETIME
1031      set_syserr(ETIME, "ETIME");
1032  #endif
1033  #ifdef ENOSR
1034      set_syserr(ENOSR, "ENOSR");
1035  #endif
1036  #ifdef ENONET
1037      set_syserr(ENONET, "ENONET");
1038  #endif
1039  #ifdef ENOPKG
1040      set_syserr(ENOPKG, "ENOPKG");
1041  #endif
1042  #ifdef EREMOTE
1043      set_syserr(EREMOTE, "EREMOTE");
1044  #endif
1045  #ifdef ENOLINK
1046      set_syserr(ENOLINK, "ENOLINK");
1047  #endif
1048  #ifdef EADV
1049      set_syserr(EADV, "EADV");
1050  #endif
1051  #ifdef ESRMNT
1052      set_syserr(ESRMNT, "ESRMNT");
1053  #endif
1054  #ifdef ECOMM
1055      set_syserr(ECOMM, "ECOMM");
1056  #endif
1057  #ifdef EPROTO
1058      set_syserr(EPROTO, "EPROTO");
1059  #endif
1060  #ifdef EMULTIHOP
1061      set_syserr(EMULTIHOP, "EMULTIHOP");
1062  #endif
1063  #ifdef EDOTDOT
1064      set_syserr(EDOTDOT, "EDOTDOT");
1065  #endif
1066  #ifdef EBADMSG
1067      set_syserr(EBADMSG, "EBADMSG");
1068  #endif
1069  #ifdef EOVERFLOW
1070      set_syserr(EOVERFLOW, "EOVERFLOW");
1071  #endif
1072  #ifdef ENOTUNIQ
1073      set_syserr(ENOTUNIQ, "ENOTUNIQ");
1074  #endif
1075  #ifdef EBADFD
1076      set_syserr(EBADFD, "EBADFD");
1077  #endif
1078  #ifdef EREMCHG
1079      set_syserr(EREMCHG, "EREMCHG");
1080  #endif
1081  #ifdef ELIBACC
1082      set_syserr(ELIBACC, "ELIBACC");
1083  #endif
1084  #ifdef ELIBBAD
1085      set_syserr(ELIBBAD, "ELIBBAD");
1086  #endif
1087  #ifdef ELIBSCN
1088      set_syserr(ELIBSCN, "ELIBSCN");
1089  #endif
1090  #ifdef ELIBMAX
1091      set_syserr(ELIBMAX, "ELIBMAX");
1092  #endif
1093  #ifdef ELIBEXEC
1094      set_syserr(ELIBEXEC, "ELIBEXEC");
1095  #endif
1096  #ifdef EILSEQ
1097      set_syserr(EILSEQ, "EILSEQ");
1098  #endif
1099  #ifdef ERESTART
1100      set_syserr(ERESTART, "ERESTART");
1101  #endif
1102  #ifdef ESTRPIPE
1103      set_syserr(ESTRPIPE, "ESTRPIPE");
1104  #endif
1105  #ifdef EUSERS
1106      set_syserr(EUSERS, "EUSERS");
1107  #endif
1108  #ifdef ENOTSOCK
1109      set_syserr(ENOTSOCK, "ENOTSOCK");
1110  #endif
1111  #ifdef EDESTADDRREQ
1112      set_syserr(EDESTADDRREQ, "EDESTADDRREQ");
1113  #endif
1114  #ifdef EMSGSIZE
1115      set_syserr(EMSGSIZE, "EMSGSIZE");
1116  #endif
1117  #ifdef EPROTOTYPE
1118      set_syserr(EPROTOTYPE, "EPROTOTYPE");
1119  #endif
1120  #ifdef ENOPROTOOPT
1121      set_syserr(ENOPROTOOPT, "ENOPROTOOPT");
1122  #endif
1123  #ifdef EPROTONOSUPPORT
1124      set_syserr(EPROTONOSUPPORT, "EPROTONOSUPPORT");
1125  #endif
1126  #ifdef ESOCKTNOSUPPORT
1127      set_syserr(ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT");
1128  #endif
1129  #ifdef EOPNOTSUPP
1130      set_syserr(EOPNOTSUPP, "EOPNOTSUPP");
1131  #endif
1132  #ifdef EPFNOSUPPORT
1133      set_syserr(EPFNOSUPPORT, "EPFNOSUPPORT");
1134  #endif
1135  #ifdef EAFNOSUPPORT
1136      set_syserr(EAFNOSUPPORT, "EAFNOSUPPORT");
1137  #endif
1138  #ifdef EADDRINUSE
1139      set_syserr(EADDRINUSE, "EADDRINUSE");
1140  #endif
1141  #ifdef EADDRNOTAVAIL
1142      set_syserr(EADDRNOTAVAIL, "EADDRNOTAVAIL");
1143  #endif
1144  #ifdef ENETDOWN
1145      set_syserr(ENETDOWN, "ENETDOWN");
1146  #endif
1147  #ifdef ENETUNREACH
1148      set_syserr(ENETUNREACH, "ENETUNREACH");
1149  #endif
1150  #ifdef ENETRESET
1151      set_syserr(ENETRESET, "ENETRESET");
1152  #endif
1153  #ifdef ECONNABORTED
1154      set_syserr(ECONNABORTED, "ECONNABORTED");
1155  #endif
1156  #ifdef ECONNRESET
1157      set_syserr(ECONNRESET, "ECONNRESET");
1158  #endif
1159  #ifdef ENOBUFS
1160      set_syserr(ENOBUFS, "ENOBUFS");
1161  #endif
1162  #ifdef EISCONN
1163      set_syserr(EISCONN, "EISCONN");
1164  #endif
1165  #ifdef ENOTCONN
1166      set_syserr(ENOTCONN, "ENOTCONN");
1167  #endif
1168  #ifdef ESHUTDOWN
1169      set_syserr(ESHUTDOWN, "ESHUTDOWN");
1170  #endif
1171  #ifdef ETOOMANYREFS
1172      set_syserr(ETOOMANYREFS, "ETOOMANYREFS");
1173  #endif
1174  #ifdef ETIMEDOUT
1175      set_syserr(ETIMEDOUT, "ETIMEDOUT");
1176  #endif
1177  #ifdef ECONNREFUSED
1178      set_syserr(ECONNREFUSED, "ECONNREFUSED");
1179  #endif
1180  #ifdef EHOSTDOWN
1181      set_syserr(EHOSTDOWN, "EHOSTDOWN");
1182  #endif
1183  #ifdef EHOSTUNREACH
1184      set_syserr(EHOSTUNREACH, "EHOSTUNREACH");
1185  #endif
1186  #ifdef EALREADY
1187      set_syserr(EALREADY, "EALREADY");
1188  #endif
1189  #ifdef EINPROGRESS
1190      set_syserr(EINPROGRESS, "EINPROGRESS");
1191  #endif
1192  #ifdef ESTALE
1193      set_syserr(ESTALE, "ESTALE");
1194  #endif
1195  #ifdef EUCLEAN
1196      set_syserr(EUCLEAN, "EUCLEAN");
1197  #endif
1198  #ifdef ENOTNAM
1199      set_syserr(ENOTNAM, "ENOTNAM");
1200  #endif
1201  #ifdef ENAVAIL
1202      set_syserr(ENAVAIL, "ENAVAIL");
1203  #endif
1204  #ifdef EISNAM
1205      set_syserr(EISNAM, "EISNAM");
1206  #endif
1207  #ifdef EREMOTEIO
1208      set_syserr(EREMOTEIO, "EREMOTEIO");
1209  #endif
1210  #ifdef EDQUOT
1211      set_syserr(EDQUOT, "EDQUOT");
1212  #endif
1213  }
1214  
1215  static void
1216  err_append(s)
1217      const char *s;
1218  {
1219      extern VALUE ruby_errinfo;
1220  
1221      if (ruby_in_eval) {
1222          if (NIL_P(ruby_errinfo)) {
1223              ruby_errinfo = rb_exc_new2(rb_eSyntaxError, s);
1224          }
1225          else {
1226              VALUE str = rb_str_to_str(ruby_errinfo);
1227  
1228              rb_str_cat2(str, "\n");
1229              rb_str_cat2(str, s);
1230              ruby_errinfo = rb_exc_new3(rb_eSyntaxError, str);
1231          }
1232      }
1233      else {
1234          fputs(s, stderr);
1235          fputs("\n", stderr);
1236          fflush(stderr);
1237      }
1238  }