Exemplo n.º 1
0
mrb_value mrb_discount_header(mrb_state *mrb, mrb_value self)
{
    mrb_md_context *md_ctx = mrb_md_get_context(mrb, self, "mrb_md_context");
    char *header;
    size_t len;

    len = HEADER_SIZE + strlen(md_ctx->title) + strlen(md_ctx->css);
    header = (char *)mrb_malloc(mrb, len);

    snprintf(header, len,
        //"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        //"<!DOCTYPE html PUBLIC \n"
        //"              \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
        //"              \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
        "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
        "<head>\n"
        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
        "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n"
        "<title>%s</title>\n"
        "<link rel=\"stylesheet\" href=\"%s\" type=\"text/css\" />\n"
        "</head>\n"
        "<body>\n"
        , md_ctx->title
        , md_ctx->css
    );

    return mrb_str_new(mrb, header, (mrb_int)strlen(header));
}
Exemplo n.º 2
0
int main()
{
    mrb_state* mrb = mrb_open();

    // mrubyファイルをロードする
    FILE *fd = fopen("person.mrb", "r");
    mrb_load_irep_file(mrb, fd);

    // *.rbを直接ロードする
    // FILE *fd = fopen("person.rb", "r");
    // mrb_load_file(mrb, fd);

    // クラスオブジェクトを取得する
    struct RClass *person = mrb_class_get(mrb, "Person");

    // 引数をmrb_valueに変換する
    mrb_value person_value = mrb_obj_value(person);
    mrb_value name_value = mrb_str_new(mrb, "naoty", 5);
    mrb_value age_value = mrb_fixnum_value(25);

    // Person#newを呼び出す
    mrb_value naoty = mrb_funcall(mrb, person_value, "new", 2, name_value, age_value);

    // Person#greetingを呼び出す
    mrb_value greeting_value = mrb_funcall(mrb, naoty, "greeting", 0);

    // 返り値をchar*に変換して出力する
    char *greeting = mrb_string_value_ptr(mrb, greeting_value);
    printf("%s\n", greeting);

    mrb_close(mrb);
    return 0;
}
Exemplo n.º 3
0
mrb_value mrb_discount_md2html(mrb_state *mrb, mrb_value self)
{
    mrb_md_context *md_ctx = mrb_md_get_context(mrb, self, "mrb_md_context");
    MMIOT *md;
    mrb_value md_obj;
    int size;
    char *html, *title;
    mrb_value header, footer, data;

    mrb_get_args(mrb, "o", &md_obj);

    md = mkd_string(RSTRING_PTR(md_obj), strlen(RSTRING_PTR(md_obj)), 0);
    mkd_compile(md, MKD_TOC|MKD_AUTOLINK);

    title = mkd_doc_title(md);
    if (title) {
        md_ctx->title = title;
    }

    if ((size = mkd_document(md, &html)) == EOF) {
        mrb_raise(mrb, E_RUNTIME_ERROR, "mkd_document() failed");
    }
    header = mrb_discount_header(mrb, self);
    footer = mrb_discount_footer(mrb, self);
    data = mrb_str_plus(mrb, header, mrb_str_new(mrb, html, strlen(html)));
    data = mrb_str_plus(mrb, data, footer);

    mkd_cleanup(md);
        
    return data;
}
mrb_value
regexp_pcre_initialize(mrb_state *mrb, mrb_value self)
{
  int erroff = 0, coptions;
  const char *errstr = NULL;
  struct mrb_regexp_pcre *reg = NULL;
  mrb_value source, opt = mrb_nil_value();

  reg = (struct mrb_regexp_pcre *)DATA_PTR(self);
  if (reg) {
    mrb_regexp_free(mrb, reg);
  }
  DATA_TYPE(self) = &mrb_regexp_type;
  DATA_PTR(self) = NULL;

  mrb_get_args(mrb, "S|o", &source, &opt);

  reg = mrb_malloc(mrb, sizeof(struct mrb_regexp_pcre));
  reg->re = NULL;
  DATA_PTR(self) = reg;

  coptions = mrb_mruby_to_pcre_options(opt);
  source = mrb_str_new(mrb, RSTRING_PTR(source), RSTRING_LEN(source));
  reg->re = pcre_compile(RSTRING_PTR(source), coptions, &errstr, &erroff, NULL);
  if (reg->re == NULL) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid regular expression");
  }
  mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@source"), source);
  mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@options"), mrb_fixnum_value(mrb_pcre_to_mruby_options(coptions)));

  unsigned char *name_table;
  int i, namecount, name_entry_size;

  pcre_fullinfo(reg->re, NULL, PCRE_INFO_NAMECOUNT, &namecount);
  if (namecount > 0) {
    pcre_fullinfo(reg->re, NULL, PCRE_INFO_NAMETABLE, &name_table);
    pcre_fullinfo(reg->re, NULL, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size);
    unsigned char *tabptr = name_table;
    for (i = 0; i < namecount; i++) {
      int n = (tabptr[0] << 8) | tabptr[1];
      mrb_funcall(mrb, self, "name_push", 2, mrb_str_new(mrb, (const char *)(tabptr + 2), strlen((const char *)tabptr + 2)), mrb_fixnum_value(n));
      tabptr += name_entry_size;
    }
  } 

  return self;
}
Exemplo n.º 5
0
static mrb_value
mrb_uefi_pointer_value(mrb_state *mrb, mrb_value self)
{
    mrb_value value = mrb_str_new(mrb, NULL, sizeof(VOID *));
    VOID *ptr = mrb_uefi_pointer_raw_value(mrb, self);
    memcpy(RSTRING_PTR(value), &ptr, sizeof(ptr));
    return value;
}
Exemplo n.º 6
0
mrb_value ap_mrb_get_env_hash(mrb_state *mrb, mrb_value str)
{
    int i;
    mrb_value hash = mrb_hash_new(mrb);
    request_rec *r = ap_mrb_get_request();
    apr_table_t *e = r->subprocess_env;
    const apr_array_header_t *arr = apr_table_elts(e);
    apr_table_entry_t *elts = (apr_table_entry_t *)arr->elts;
    for (i = 0; i < arr->nelts; i++) {
        mrb_hash_set(mrb
            , hash
            , mrb_str_new(mrb, elts[i].key, strlen(elts[i].key))
            , mrb_str_new(mrb, elts[i].val, strlen(elts[i].val))
        );
    }
    return hash;
}
Exemplo n.º 7
0
static mrb_value
sha256_digest(mrb_state *mrb, mrb_value self)
{
  char *digest = (char*)mrb_malloc(mrb, SHA256_DIGEST_LENGTH);
  SHA256_CTX ctx = *(SHA256_CTX*)DATA_PTR(self);
  SHA256_Final((u_int8_t *)digest, &ctx);
  return mrb_str_new(mrb, digest, SHA256_DIGEST_LENGTH);
}
static mrb_value ngx_stream_mrb_proxy_protocol_addr(mrb_state *mrb, mrb_value self)
{
  ngx_stream_mruby_internal_ctx_t *ictx = mrb->ud;
  ngx_stream_session_t *s = ictx->s;

  return mrb_str_new(mrb, (const char *)s->connection->proxy_protocol_addr.data,
                     s->connection->proxy_protocol_addr.len);
}
static mrb_value ngx_stream_mrb_upstream_get_server(mrb_state *mrb, mrb_value self)
{
  ngx_stream_mruby_upstream_context *ctx = DATA_PTR(self);
  if (ctx->target == NULL) {
    return mrb_nil_value();
  }
  return mrb_str_new(mrb, (char *)ctx->target->name.data, ctx->target->name.len);
}
Exemplo n.º 10
0
int main(int argc, char** argv)
{
  const char rite[] = "app.mrb";

  mrb_state* mrb = mrb_open();

  FILE* fp = fopen(rite, "r");
  if (fp == NULL) {
    fprintf(stderr, "%s not found.\n", rite);
    fprintf(stderr, "`mruby/bin/mrbc app.rb` to create app.mrb file.\n");
    exit(EXIT_FAILURE);
  }

  int n = mrb_read_irep_file(mrb, fp);
  fclose(fp);
  if (n < 0) {
    fprintf(stderr, "%s - irep load error.\n", rite);
    exit(EXIT_FAILURE);
  }
  mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));

  if (argc < 2) {
    printf("Usage %s <environment>\n", argv[0]);
    return 0;
  }

  // AppSetting
  struct RClass* clz = mrb_class_get(mrb, "AppSetting");
  mrb_value setting = mrb_obj_value(clz);
  // AppSetting.load(*targets) -> AppConfig
  mrb_value target = mrb_str_new(mrb, argv[1], strlen(argv[1]));
  mrb_value conf = mrb_funcall(mrb, setting, "load", 1, target);
  // AppConfig.#debug -> bool
  mrb_value debug = mrb_funcall(mrb, conf, "debug", 0);
  printf("debug: %d\n", mrb_type(debug) == MRB_TT_TRUE);
  // AppConfig.#timeout -> Integer
  mrb_value timeout = mrb_funcall(mrb, conf, "timeout", 0);
  printf("timeout: %d\n", timeout.value.i);
  // AppConfig.#title -> String
  mrb_value title = mrb_funcall(mrb, conf, "title", 0);
  printf("title: %s\n", mrb_string_value_ptr(mrb, title));
  // AppConfig.#messages -> Array
  mrb_value messages = mrb_funcall(mrb, conf, "messages", 0);
  {
    // Array.#size -> Integer
    mrb_value len = mrb_funcall(mrb, messages, "size", 0);
    int i;
    for (i = 0; i < len.value.i; i++) {
      // Array.#at(nth) -> object
      mrb_value message = mrb_funcall(mrb, messages, "at", 1, mrb_fixnum_value(i));
      printf("%s\n", mrb_string_value_ptr(mrb, message));
    }
  }

  mrb_close(mrb);

  return 0;
}
Exemplo n.º 11
0
Arquivo: hash.c Projeto: Zyxwvu/mruby
static khint_t
mrb_hash_ht_hash_func(mrb_state *mrb, mrb_value key)
{
  char type = mrb_type(key);
  mrb_value s1 = mrb_str_new(mrb, &type, 1);
  mrb_value s2 = mrb_inspect(mrb, key);
  s1 = mrb_str_cat(mrb, s1, RSTRING_PTR(s2), RSTRING_LEN(s2));
  return kh_str_hash_func(mrb, RSTRING_PTR(s1));
}
Exemplo n.º 12
0
Arquivo: hash.c Projeto: gaffo/mruby
static mrb_value
mrb_hash_inspect(mrb_state *mrb, mrb_value hash)
{
  khash_t(ht) *h = RHASH_TBL(hash);

  if (!h || kh_size(h) == 0)
    return mrb_str_new(mrb, "{}", 2);
  return inspect_hash(mrb, hash, 0);
}
Exemplo n.º 13
0
void
mrb_show_copyright(mrb_state *mrb)
{
  static const char copyright_msg[] = "mruby - Copyright (c) 2010-2013 mruby developers\n";
  mrb_value msg;

  msg = mrb_str_new(mrb, copyright_msg, sizeof(copyright_msg) - 1);
  printstr(mrb, msg);
}
Exemplo n.º 14
0
static mrb_value
lib_md_digest_bang(mrb_state *mrb, struct mrb_md *md)
{
  unsigned char mdstr[MAX_DIGEST_LENGTH];

  md_final(md->type, mdstr, md->ctx);
  md_init(md->type, md->ctx);
  return mrb_str_new(mrb, (char *)mdstr, md_digest_length(md->type));
}
Exemplo n.º 15
0
mrb_value
mrb_Git_Buf_to_s(mrb_state * mrb, mrb_value self) {
  git_buf * native_self = mruby_unbox_git_buf(self);
  if (native_self->ptr == NULL) {
    return mrb_nil_value();
  } else {
    return mrb_str_new(mrb, native_self->ptr, native_self->size);
  }
}
Exemplo n.º 16
0
void
mrb_show_version(mrb_state *mrb)
{
  static const char version_msg[] = "mruby - Embeddable Ruby  Copyright (c) 2010-2013 mruby developers\n";
  mrb_value msg;

  msg = mrb_str_new(mrb, version_msg, sizeof(version_msg) - 1);
  printstr(mrb, msg);
}
Exemplo n.º 17
0
static int
parser_settings_on_header_field(http_parser* parser, const char* at, size_t len)
{
    mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data;
    mrb_state* mrb = context->mrb;

    if (context->was_header_value) {
        if (!mrb_nil_p(CTXV_GET(context, "last_header_field"))) {
            mrb_str_concat(mrb, CTXV_GET(context, "last_header_field"), CTXV_GET(context, "last_header_value"));
            CTXV_SET(context, "last_header_value", mrb_nil_value());
        }
        CTXV_SET(context, "last_header_field", mrb_str_new(mrb, at, len));
        context->was_header_value = FALSE;
    } else {
        mrb_str_concat(mrb, CTXV_GET(context, "last_header_field"), mrb_str_new(mrb, at, len));
    }
    return 0;
}
Exemplo n.º 18
0
static mrb_value
lib_md_digest(mrb_state *mrb, const struct mrb_md *md)
{
  union ctx_union ctx;
  unsigned char mdstr[MAX_DIGEST_LENGTH];

  memcpy(&ctx, md->ctx, md_ctx_size(md->type));
  md_final(md->type, mdstr, &ctx);
  return mrb_str_new(mrb, (char *)mdstr, md_digest_length(md->type));
}
Exemplo n.º 19
0
static mrb_value
lib_hmac_digest(mrb_state *mrb, const struct mrb_hmac *hmac)
{
  CCHmacContext ctx;
  unsigned char str[MAX_DIGEST_LENGTH];

  memcpy(&ctx, &hmac->ctx, sizeof(ctx));
  CCHmacFinal(&ctx, str);
  return mrb_str_new(mrb, (const char *)str, md_digest_length(hmac->type));
}
Exemplo n.º 20
0
static int
parser_settings_on_header_value(http_parser* parser, const char* at, size_t len)
{
  mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data;
  mrb_state* mrb = context->mrb;

  int ai = mrb_gc_arena_save(mrb);
  if(!context->was_header_value) {
    OBJECT_SET(mrb, context->instance, "last_header_value", mrb_str_new(mrb, at, len));
    context->was_header_value = TRUE;
    mrb_hash_set(mrb, OBJECT_GET(mrb, context->instance, "headers"),
        OBJECT_GET(mrb, context->instance, "last_header_field"),
        OBJECT_GET(mrb, context->instance, "last_header_value"));
  } else {
    mrb_str_concat(mrb, OBJECT_GET(mrb, context->instance, "last_header_value"), mrb_str_new(mrb, at, len));
  }
  mrb_gc_arena_restore(mrb, ai);
  return 0;
}
Exemplo n.º 21
0
static mrb_value h2o_mrb_req_query(mrb_state *mrb, mrb_value self)
{
    h2o_mruby_internal_context_t *mruby_ctx = (h2o_mruby_internal_context_t *)mrb->ud;
    h2o_iovec_t *path = &mruby_ctx->req->path;
    size_t offset = mruby_ctx->req->query_at;
    if (offset == SIZE_MAX)
        return mrb_nil_value();

    return mrb_str_new(mrb, path->base + offset, path->len - offset);
}
int main(void)
{
  mrb_state *mrb = mrb_open();
  
  struct RClass* cv = mrb_class_get(mrb, "String");
  mrb_value str = mrb_obj_new(mrb, cv, 0, 0);
  
  mrb_value head = mrb_str_new(mrb,"p 'hello",8);
  mrb_value tail = mrb_str_new(mrb," world'",7);

  str = mrb_funcall_argv(mrb, str, mrb_intern2(mrb, "+", 1), 1, &head);
  str = mrb_funcall_argv(mrb, str, mrb_intern2(mrb, "+", 1), 1, &tail);

  printf("str#size is %d \n", mrb_funcall(mrb, str, "size", 0, 0));
  printf("str is '%s' \n", RSTRING_PTR(str));

  mrb_load_string(mrb, RSTRING_PTR(str));
  return 0;
}
Exemplo n.º 23
0
static mrb_value
lib_md_digest_bang(mrb_state *mrb, struct mrb_md *md)
{
  unsigned int mdlen;
  unsigned char mdstr[EVP_MAX_MD_SIZE];

  EVP_DigestFinal_ex(md->ctx, mdstr, &mdlen);
  EVP_DigestInit_ex(md->ctx, EVP_MD_CTX_md(md->ctx), NULL);
  return mrb_str_new(mrb, (char *)mdstr, mdlen);
}
Exemplo n.º 24
0
mrb_value ap_mrb_str_to_value(mrb_state *mrb, apr_pool_t *p, const char *str)
{
  const char *val;

  if (str == NULL) {
    return mrb_nil_value();
  }
  val = apr_pstrdup(p, str);
  return mrb_str_new(mrb, val, strlen(val));
}
Exemplo n.º 25
0
static mrb_value
mrb_uefi_handle_value(mrb_state *mrb, mrb_value self)
{
    struct MRB_UEFI_HANDLE_DATA *hd;
    mrb_value value = mrb_str_new(mrb, NULL, sizeof(EFI_HANDLE));

    hd = (struct MRB_UEFI_HANDLE_DATA *)mrb_get_datatype(mrb, self, &mrb_uefi_handle_type);
    memcpy(RSTRING_PTR(value), &hd->handle, sizeof(hd->handle));
    return value;
}
Exemplo n.º 26
0
Arquivo: symbol.c Projeto: Hozum/mruby
/*
 *  call-seq:
 *     sym.id2name   -> string
 *     sym.to_s      -> string
 *
 *  Returns the name or string corresponding to <i>sym</i>.
 *
 *     :fred.id2name   #=> "fred"
 */
mrb_value
mrb_sym_to_s(mrb_state *mrb, mrb_value sym)
{
  mrb_sym id = mrb_symbol(sym);
  const char *p;
  size_t len;

  p = mrb_sym2name_len(mrb, id, &len);
  return mrb_str_new(mrb, p, len);
}
Exemplo n.º 27
0
static mrb_value
mrb_ary_inspect(mrb_state *mrb, mrb_value ary)
{
  if (RARRAY_LEN(ary) == 0) return mrb_str_new(mrb, "[]", 2);
  #if 0 /* THREAD */
    return mrb_exec_recursive(inspect_ary_r, ary, 0);
  #else
    return inspect_ary(mrb, ary, mrb_ary_new(mrb));
  #endif
}
Exemplo n.º 28
0
static mrb_value ap_mrb_get_request_headers_out(mrb_state *mrb, mrb_value str)
{
  mrb_value key;
  const char *val;
  request_rec *r = ap_mrb_get_request();

  mrb_get_args(mrb, "o", &key);
  val = apr_table_get(r->headers_out, mrb_str_to_cstr(mrb, key));
  return mrb_str_new(mrb, val, strlen(val));
}
Exemplo n.º 29
0
static mrb_value
mrb_mod_to_s(mrb_state *mrb, mrb_value klass)
{
  mrb_value str;

  if (mrb_type(klass) == MRB_TT_SCLASS) {
    mrb_value v = mrb_iv_get(mrb, klass, mrb_intern2(mrb, "__attached__", 12));

    str = mrb_str_new(mrb, "#<Class:", 8);

    switch (mrb_type(v)) {
      case MRB_TT_CLASS:
      case MRB_TT_MODULE:
      case MRB_TT_SCLASS:
        mrb_str_append(mrb, str, mrb_inspect(mrb, v));
        break;
      default:
        mrb_str_append(mrb, str, mrb_any_to_s(mrb, v));
        break;
    }
    mrb_str_cat(mrb, str, ">", 1);
  }
  else {
    struct RClass *c;
    mrb_value path;

    str = mrb_str_buf_new(mrb, 32);
    c = mrb_class_ptr(klass);
    path = mrb_class_path(mrb, c);

    if (mrb_nil_p(path)) {
      switch (mrb_type(klass)) {
        case MRB_TT_CLASS:
          mrb_str_cat(mrb, str, "#<Class:", 8);
          break;

        case MRB_TT_MODULE:
          mrb_str_cat(mrb, str, "#<Module:", 9);
          break;

        default:
          /* Shouldn't be happened? */
          mrb_str_cat(mrb, str, "#<??????:", 9);
          break;
      }
      mrb_str_concat(mrb, str, mrb_ptr_to_str(mrb, c));
      mrb_str_cat(mrb, str, ">", 1);
    }
    else {
      str = path;
    }
  }

  return str;
}
Exemplo n.º 30
0
/*
 *  call-seq:
 *     obj.inspect   -> string
 *
 *  Returns a string containing a human-readable representation of
 *  <i>obj</i>. If not overridden and no instance variables, uses the
 *  <code>to_s</code> method to generate the string.
 *  <i>obj</i>.  If not overridden, uses the <code>to_s</code> method to
 *  generate the string.
 *
 *     [ 1, 2, 3..4, 'five' ].inspect   #=> "[1, 2, 3..4, \"five\"]"
 *     Time.new.inspect                 #=> "2008-03-08 19:43:39 +0900"
 */
mrb_value
mrb_obj_inspect(mrb_state *mrb, mrb_value obj)
{
  if ((mrb_type(obj) == MRB_TT_OBJECT) && mrb_obj_basic_to_s_p(mrb, obj)) {
    return mrb_obj_iv_inspect(mrb, mrb_obj_ptr(obj));
  }
  else if (mrb_nil_p(obj)) {
    return mrb_str_new(mrb, "nil", 3);
  }
  return mrb_funcall(mrb, obj, "to_s", 0, 0);
}