static VALUE syserr_initialize(int argc, VALUE *argv, VALUE self) { #if !defined(_WIN32) char *strerror(); #endif const char *err; VALUE mesg, error; VALUE klass = rb_obj_class(self); if (klass == rb_eSystemCallError) { st_data_t data = (st_data_t)klass; rb_scan_args(argc, argv, "11", &mesg, &error); if (argc == 1 && FIXNUM_P(mesg)) { error = mesg; mesg = Qnil; } if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) { klass = (VALUE)data; /* change class */ if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */ rb_raise(rb_eTypeError, "invalid instance type"); } RBASIC(self)->klass = klass; } } else { rb_scan_args(argc, argv, "01", &mesg); error = rb_const_get(klass, rb_intern("Errno")); } if (!NIL_P(error)) err = strerror(NUM2INT(error)); else err = "unknown error"; if (!NIL_P(mesg)) { rb_encoding *le = rb_locale_encoding(); VALUE str = mesg; StringValue(str); mesg = rb_sprintf("%s - %.*s", err, (int)RSTRING_LEN(str), RSTRING_PTR(str)); if (le == rb_usascii_encoding()) { rb_encoding *me = rb_enc_get(mesg); if (le != me && rb_enc_asciicompat(me)) le = me; }/* else assume err is non ASCII string. */ OBJ_INFECT(mesg, str); rb_enc_associate(mesg, le); } else { mesg = rb_str_new2(err); rb_enc_associate(mesg, rb_locale_encoding()); } rb_call_super(1, &mesg); rb_iv_set(self, "errno", error); return self; }
int eruta_ruby_init() { VALUE options = Qnil, sublet = Qnil; void Init_prelude(void); RUBY_INIT_STACK; ruby_init(); ruby_init_loadpath(); ruby_script("eruta"); #ifdef HAVE_RB_ENC_SET_DEFAULT_INTERNAL { VALUE encoding = Qnil; /* FIXME: Fix for ruby 1.9.2p429 borrowed from ruby? */ (void)rb_filesystem_encoding(); /* Set encoding */ encoding = rb_enc_from_encoding(rb_locale_encoding()); rb_enc_set_default_external(encoding); } #endif /* HAVE_RB_ENC_SET_DEFAULT_INTERNAL */ /* FIXME: Fake ruby_init_gems(Qtrue) */ rb_define_module("Gem"); Init_prelude(); /* Bypassing garbage collection. Why? */ /* shelter = rb_ary_new(); rb_gc_register_address(&shelter); */ }
void rb_loaderror_with_path(VALUE path, const char *fmt, ...) { va_list args; VALUE mesg; va_start(args, fmt); mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args); va_end(args); raise_loaderror(path, mesg); }
void rb_loaderror(const char *fmt, ...) { va_list args; VALUE mesg; va_start(args, fmt); mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args); va_end(args); rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg)); }
rb_encoding * rb_default_external_encoding(void) { if (default_external.enc) return default_external.enc; if (default_external.index >= 0) { default_external.enc = rb_enc_from_index(default_external.index); return default_external.enc; } else { return rb_locale_encoding(); } }
static VALUE syserr_initialize(int argc, VALUE *argv, VALUE self) { #if !defined(_WIN32) char *strerror(); #endif const char *err; VALUE mesg, error, func, errmsg; VALUE klass = rb_obj_class(self); if (klass == rb_eSystemCallError) { st_data_t data = (st_data_t)klass; rb_scan_args(argc, argv, "12", &mesg, &error, &func); if (argc == 1 && FIXNUM_P(mesg)) { error = mesg; mesg = Qnil; } if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) { klass = (VALUE)data; /* change class */ if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */ rb_raise(rb_eTypeError, "invalid instance type"); } RBASIC_SET_CLASS(self, klass); } } else { rb_scan_args(argc, argv, "02", &mesg, &func); error = rb_const_get(klass, rb_intern("Errno")); } if (!NIL_P(error)) err = strerror(NUM2INT(error)); else err = "unknown error"; errmsg = rb_enc_str_new_cstr(err, rb_locale_encoding()); if (!NIL_P(mesg)) { VALUE str = StringValue(mesg); if (!NIL_P(func)) rb_str_catf(errmsg, " @ %"PRIsVALUE, func); rb_str_catf(errmsg, " - %"PRIsVALUE, str); OBJ_INFECT(errmsg, mesg); } mesg = errmsg; rb_call_super(1, &mesg); rb_iv_set(self, "errno", error); return self; }
/* call-seq: * getlogin -> String * * Returns the short user name of the currently logged in user. * Unfortunately, it is often rather easy to fool ::getlogin. * * Avoid ::getlogin for security-related purposes. * * If ::getlogin fails, try ::getpwuid. * * See the unix manpage for <code>getpwuid(3)</code> for more detail. * * e.g. * Etc.getlogin -> 'guest' */ static VALUE etc_getlogin(VALUE obj) { char *login; #ifdef HAVE_GETLOGIN login = getlogin(); if (!login) login = getenv("USER"); #else login = getenv("USER"); #endif if (login) { #ifdef _WIN32 rb_encoding *extenc = rb_utf8_encoding(); #else rb_encoding *extenc = rb_locale_encoding(); #endif return rb_external_str_new_with_enc(login, strlen(login), extenc); } return Qnil; }
/* * TermInfo.wcswidth(str) * * TermInfo.wcswidth returns a the number of columns of str, * according to current locale. */ static VALUE rt_wcswidth(VALUE self, VALUE str) { char *s; size_t l, r; mbstate_t mbs; wchar_t wc; long cols; int width; #ifdef HAVE_RUBY_ENCODING_H /* The encoding of str is assumed to be the locale encoding on Ruby 1.8. */ str = rb_str_encode(str, rb_enc_from_encoding(rb_locale_encoding()), 0, Qnil); #endif memset(&mbs,0,sizeof(mbstate_t)); s = StringValueCStr(str); l = RSTRING_LEN(str); cols = 0; while (0 < l) { r = mbrtowc(&wc, s, l, &mbs); if (r == 0) rb_raise(rb_eArgError, "NUL found"); width = wcwidth(wc); if (width == -1) rb_raise(rb_eArgError, "non-printable charactor found"); cols += width; l -= r; s += r; } return LONG2NUM(cols); }
static VALUE locale_path(VALUE path) { rb_enc_associate(path, rb_locale_encoding()); return path; }
static VALUE encoding_spec_rb_locale_encoding(VALUE self) { return rb_str_new2(rb_locale_encoding()->name); }