static mrb_value mrb_mod_cvar_get(mrb_state *mrb, mrb_value mod) { mrb_sym id; mrb_get_args(mrb, "n", &id); check_cv_name_sym(mrb, id); return mrb_cv_get(mrb, mod, id); }
mrb_value mrb_f_syslog_ident(mrb_state *mrb, mrb_value self) { mrb_value s; s = mrb_cv_get(mrb, self, mrb_intern_lit(mrb, "ident")); if (! mrb_string_p(s)) { if (mrb_nil_p(s)) return s; mrb_raisef(mrb, E_RUNTIME_ERROR, "class variable ident of Syslog is not a string"); } return mrb_str_new_cstr(mrb, RSTRING_PTR(s)); }
static void ev3_set_current_font(mrb_state *mrb, mrb_value self) { static mrb_bool first = TRUE; struct RClass *lcd = mrb_obj_class(mrb, self); mrb_int current = mrb_fixnum(mrb_cv_get(mrb, mrb_obj_value(lcd), mrb_intern_lit(mrb, "@@font"))); mrb_lcd_t *plcd = (mrb_lcd_t*)DATA_PTR(self); mrb_int target = plcd->font; if (first || current != target) { ev3_lcd_set_font((uint16_t)target); mrb_cv_set(mrb, mrb_obj_value(lcd), mrb_intern_lit(mrb, "@@font"), mrb_fixnum_value(target)); first = FALSE; } }
mrb_value mrb_f_syslog_close(mrb_state *mrb, mrb_value self) { mrb_value opened; opened = mrb_cv_get(mrb, self, mrb_intern_lit(mrb, "@opened")); if (! mrb_bool(opened)) { mrb_raise(mrb, E_RUNTIME_ERROR, "syslog not opened"); } closelog(); reset_vars(mrb, self); return mrb_nil_value(); }
mrb_value mrb_f_syslog_open(mrb_state *mrb, mrb_value self) { mrb_value ident, opened; mrb_int facility, options; opened = mrb_cv_get(mrb, self, mrb_intern_lit(mrb, "@opened")); if (mrb_bool(opened)) { mrb_raise(mrb, E_RUNTIME_ERROR, "syslog already open"); } ident = mrb_gv_get(mrb, mrb_intern_lit(mrb, "$0")); options = LOG_PID | LOG_CONS; facility = LOG_USER; mrb_get_args(mrb, "|Sii", &ident, &options, &facility); ident = mrb_check_string_type(mrb, ident); /* * We assume mrb_str_new() returns a *not-shared* string with a NUL * character at the end of the string. */ ident = mrb_str_new(mrb, RSTRING_PTR(ident), RSTRING_LEN(ident)); /* * We must set "ident" to a class variable before calling openlog(3). * Otherwise, syslog(3) may refer to some unallocated (GC'ed out) memory * area when mrb_cv_set() fails. Note that some openlog(3) implementations * store "ident" argument as is (not strdup(3)ed!). * http://man7.org/linux/man-pages/man3/syslog.3.html#NOTES * * And we make class variable "ident" inaccessible from Ruby world * for safety. */ mrb_cv_set(mrb, self, mrb_intern_lit(mrb, "ident"), ident); openlog(RSTRING_PTR(ident), options, facility); mrb_cv_set(mrb, self, mrb_intern_lit(mrb, "@opened"), mrb_true_value()); mrb_cv_set(mrb, self, mrb_intern_lit(mrb, "@options"), mrb_fixnum_value(options)); mrb_cv_set(mrb, self, mrb_intern_lit(mrb, "@facility"), mrb_fixnum_value(facility)); return self; }
static void mrb_lcd_get_font_size(mrb_state *mrb, mrb_value lcd, mrb_int *fw, mrb_int *fh) { mrb_value font = mrb_cv_get(mrb, lcd, mrb_intern_lit(mrb, "@@font")); ev3_font_get_size(mrb_fixnum(font), fw, fh); }
static mrb_value mrb_addrinfo_getaddrinfo(mrb_state *mrb, mrb_value klass) { struct addrinfo hints, *res0, *res; mrb_value ai, ary, family, lastai, nodename, protocol, sa, service, socktype; mrb_int flags; int arena_idx, error; const char *hostname = NULL, *servname = NULL; ary = mrb_ary_new(mrb); arena_idx = mrb_gc_arena_save(mrb); /* ary must be on arena! */ family = socktype = protocol = mrb_nil_value(); flags = 0; mrb_get_args(mrb, "oo|oooi", &nodename, &service, &family, &socktype, &protocol, &flags); if (mrb_string_p(nodename)) { hostname = mrb_str_to_cstr(mrb, nodename); } else if (mrb_nil_p(nodename)) { hostname = NULL; } else { mrb_raise(mrb, E_TYPE_ERROR, "nodename must be String or nil"); } if (mrb_string_p(service)) { servname = mrb_str_to_cstr(mrb, service); } else if (mrb_fixnum_p(service)) { servname = mrb_str_to_cstr(mrb, mrb_funcall(mrb, service, "to_s", 0)); } else if (mrb_nil_p(service)) { servname = NULL; } else { mrb_raise(mrb, E_TYPE_ERROR, "service must be String, Fixnum, or nil"); } memset(&hints, 0, sizeof(hints)); hints.ai_flags = flags; if (mrb_fixnum_p(family)) { hints.ai_family = mrb_fixnum(family); } if (mrb_fixnum_p(socktype)) { hints.ai_socktype = mrb_fixnum(socktype); } lastai = mrb_cv_get(mrb, klass, mrb_intern_lit(mrb, "_lastai")); if (mrb_cptr_p(lastai)) { freeaddrinfo(mrb_cptr(lastai)); mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value()); } error = getaddrinfo(hostname, servname, &hints, &res0); if (error) { mrb_raisef(mrb, E_SOCKET_ERROR, "getaddrinfo: %S", mrb_str_new_cstr(mrb, gai_strerror(error))); } mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_cptr_value(mrb, res0)); for (res = res0; res != NULL; res = res->ai_next) { sa = mrb_str_new(mrb, (void *)res->ai_addr, res->ai_addrlen); ai = mrb_funcall(mrb, klass, "new", 4, sa, mrb_fixnum_value(res->ai_family), mrb_fixnum_value(res->ai_socktype), mrb_fixnum_value(res->ai_protocol)); mrb_ary_push(mrb, ary, ai); mrb_gc_arena_restore(mrb, arena_idx); } freeaddrinfo(res0); mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value()); return ary; }