static void knh_Exception_addStackTrace(CTX ctx, kException *e, ksfp_t *sfp) { CWB_t cwbbuf, *cwb = CWB_open0(ctx, &cwbbuf); kMethod *mtd = sfp[K_MTDIDX].mtdNC; if((mtd)->mn != MN_LAMBDA) { int i = 0, psize = knh_Method_psize(mtd); kline_t uline = knh_stack_uline(ctx, sfp); knh_write_uline(ctx, cwb->w, uline); knh_write_type(ctx, cwb->w, (mtd)->cid); knh_putc(ctx, cwb->w, '.'); knh_write_mn(ctx, cwb->w, (mtd)->mn); knh_putc(ctx, cwb->w, '('); for(i = 0; i < psize; i++) { kparam_t *p = knh_Param_get(DP(mtd)->mp, i); ktype_t type = ktype_tocid(ctx, p->type, O_cid(sfp[0].o)); if(i > 0) { knh_putc(ctx, cwb->w, ','); } knh_write_fn(ctx, cwb->w, p->fn); knh_putc(ctx, cwb->w, '='); knh_write_sfp(ctx, cwb->w, type, &sfp[i+1], FMT_line); } knh_putc(ctx, cwb->w, ')'); if(e->tracesNULL == NULL) { KNH_INITv(e->tracesNULL, new_Array(ctx, CLASS_String, 0)); } knh_Array_add(ctx, e->tracesNULL, CWB_newString0(ctx, cwb)); } }
static ktype_t msgpack_read(CTX ctx, msgpack_object obj, ksfp_t *sfp) { switch (obj.type) { case MSGPACK_OBJECT_NIL: KNH_SETv(ctx, sfp[0].o, KNH_NULL); return CLASS_Tdynamic; case MSGPACK_OBJECT_BOOLEAN: sfp[0].bvalue = obj.via.boolean; return CLASS_Boolean; case MSGPACK_OBJECT_NEGATIVE_INTEGER: sfp[0].ivalue = obj.via.i64; return CLASS_Int; case MSGPACK_OBJECT_POSITIVE_INTEGER: sfp[0].uvalue = obj.via.u64; return CLASS_Int; case MSGPACK_OBJECT_DOUBLE: sfp[0].fvalue = obj.via.dec; return CLASS_Float; case MSGPACK_OBJECT_RAW: KNH_SETv(ctx, sfp[0].o, new_String(ctx, obj.via.raw.ptr)); return CLASS_String; case MSGPACK_OBJECT_ARRAY: { msgpack_object *a = obj.via.array.ptr; size_t asize = obj.via.array.size; const msgpack_object *array_end = obj.via.array.ptr + asize; KNH_SETv(ctx, sfp[0].o, new_Array(ctx, CLASS_Tdynamic, asize)); for (; a < array_end; a++) { ktype_t type = msgpack_read(ctx, *a, sfp+1); knh_boxing(ctx, sfp+1, type); knh_Array_add(ctx, sfp[0].a, sfp[1].o); } return CLASS_Array; } case MSGPACK_OBJECT_MAP: { if (obj.via.map.size == 0) break; msgpack_object_kv *map = obj.via.map.ptr; msgpack_object_kv *map_end = obj.via.map.ptr + obj.via.map.size; KNH_SETv(ctx, sfp[0].o, new_DataMap(ctx/*, obj.via.map.size*/)); for (; map < map_end; map++) { const char *key = map->key.via.raw.ptr; ktype_t type = msgpack_read(ctx, map->val, sfp+1); knh_boxing(ctx, sfp+1, type); klr_setesp(ctx, sfp+2); knh_DataMap_set(ctx, sfp[0].m, new_String(ctx, key), sfp[1].o); } return CLASS_Map; } } return CLASS_Tvoid; }
static Array *knh_String_toCharArray(Ctx *ctx, String *bs, int istrim) { knh_bytes_t base = knh_String_tobytes(bs); if(knh_String_isASCII(bs)) { size_t i, n = base.len; Array *a = new_Array(ctx, CLASS_String, n); for(i = 0; i < n; i++) { if(istrim && isspace(base.buf[i])) continue; knh_bytes_t sub = { base.buf + i, 1}; knh_Array_add(ctx, a, UP(new_String(ctx, sub, bs))); } return a; } else { size_t i, n = knh_bytes_mlen(base); Array *a = new_Array(ctx, CLASS_String, n); for(i = 0; i < n; i++) { if(istrim && isspace(base.buf[i])) continue; knh_bytes_t sub = knh_bytes_mofflen(base, n, 1); knh_Array_add(ctx, a, UP(new_String(ctx, sub, bs))); } return a; } }
static PDTColumn * PDTColumn_add_validation(PDTColumn * self, char * validation, size_t validation_length) { PDTValidation * pdt_validation; if(self != null_PDTColumn) { if(self->validations == null_Array) { self->validations = new_Array(); self->validations->auto_free = _PDTValidation_Array_free; } pdt_validation = new_PDTValidation(validation, validation_length); self->validations->m->append(self->validations, pdt_validation); } return self; }
int main(int argc, const char *argv[]) { int i, argc_ = argc; const char *argv_[argc_]; const char *fname = parse_option(&argc_, argv, argv_); if (fname == NULL) { fprintf(stderr, "%s [--emit-llvm/--emit-js] file\n", argv[0]); return 1; } konoha_t konoha = konoha_open(); CTX ctx = konoha; kString *s = new_T(fname); knh_DictMap_set(ctx, ctx->share->props, new_T("script.name"), s); kArray *a = new_Array(ctx, CLASS_String, argc_); for(i = 2; i < argc_; i++) { knh_Array_add(ctx, a, new_String2(ctx, CLASS_String, argv_[i], knh_strlen(argv_[i]), SPOL_TEXT|SPOL_POOLALWAYS)); } knh_DictMap_set(ctx, ctx->share->props, new_T("script.argv"), a); kbytes_t t = knh_bytes_nsname(S_tobytes(s)); knh_Script_setNSName(ctx, ctx->script, new_S(t.text, t.len)); kbytes_t pkgname = STEXT("konoha.compiler"); knh_loadPackage(ctx, pkgname); load_codegenerator(ctx); if (!compiler_run_main) { knh_setCompileMode(ctx, 1); } knh_startScript(ctx, (const char*)fname); /* CompilerAPI->dump() */ kMethod *mtd = load_method(ctx, O_cid(ctx->share->konoha_compiler), STEXT("dump")); BEGIN_LOCAL(ctx, lsfp, K_CALLDELTA+1); { KNH_SETv(ctx, lsfp[K_CALLDELTA].o, ctx->share->konoha_compiler); KNH_SCALL(ctx, lsfp, 0, mtd, 0); } END_LOCAL(ctx, lsfp); if (compiler_run_main) { knh_stack_clear(ctx, ctx->stack); knh_runMain(ctx, argc_, argv_); } konoha_close(konoha); return 0; }
static void knh_parsearg(CTX ctx, int argc, const char **argv) { int i; kArray *a = new_Array(ctx, CLASS_String, argc); for(i = 1; i < argc; i++) { knh_Array_add(ctx, a, new_String2(ctx, CLASS_String, argv[i], knh_strlen(argv[i]), SPOL_TEXT|SPOL_POOLALWAYS)); } knh_DictMap_set(ctx, ctx->share->props, new_T("script.argv"), a); if(argc > 0) { kString *s = new_T(argv[0]); knh_DictMap_set(ctx, ctx->share->props, new_T("script.name"), s); kbytes_t t = knh_bytes_nsname(S_tobytes(s)); knh_Script_setNSName(ctx, ctx->script, new_String2(ctx, CLASS_String, t.text, t.len, SPOL_TEXT|SPOL_POOLALWAYS)); } else { knh_Script_setNSName(ctx, ctx->script, TS_main); } }
void test_gc_with_pointer_array() { User *u, *t; String *s; Array *a; char *expected, *result_before, *result_after; gc(); GC_SAVE_RP; s = new_String("tim"); u = new_User(102, s); a = new_Array(2, ARRAY_POINTER); add_to(a, 0, s); add_to(a, 1, u); ADD_ROOT(a); expected = "heap1[116,1000]\n" "0068:Array[32+2]->[0, 36]\n" "0000:String[32+4]=\"tim\"\n" "0036:User[32]->[0]\n"; result_before = calloc(1000, sizeof(char)); heap_dump(result_before); ASSERT_STR(expected, result_before); free(result_before); gc(); t = new_User(102, NULL); ADD_ROOT(t); expected = "heap2[148,1000]\n" "0000:Array[32+2]->[48, 84]\n" "0048:String[32+4]=\"tim\"\n" "0084:User[32]->[48]\n" "0116:User[32]->[-1]\n"; result_after = calloc(1000, sizeof(char)); heap_dump(result_after); ASSERT_STR(expected, result_after); // printf("\n%s", result_after); free(result_after); GC_RESTORE_RP; }
static METHOD knh__String_split(Ctx *ctx, knh_sfp_t *sfp) { Array *a = NULL; int istrim = IS_NULL(sfp[2].o) ? 0 : (int)sfp[2].ivalue; if(IS_NULL(sfp[1].o)) { a = knh_String_toCharArray(ctx, sfp[0].s, istrim); } else { knh_bytes_t delim = knh_String_tobytes(sfp[1].s); if(delim.len == 0) { a = knh_String_toCharArray(ctx, sfp[0].s, istrim); } else { knh_bytes_t base = knh_String_tobytes(sfp[0].s); a = new_Array(ctx, CLASS_String, 8); while(1) { knh_index_t loc = knh_bytes_indexOf(base, delim); if(loc == -1) { if(istrim) base = knh_bytes_trim(base); knh_Array_add(ctx, a, UP(new_String(ctx, base, sfp[0].s))); break; } else if(loc == 0) { knh_Array_add(ctx, a, UP(TS_EMPTY)); } else { knh_bytes_t t = knh_bytes_first(base, loc); if(istrim) t = knh_bytes_trim(t); knh_Array_add(ctx, a, UP(new_String(ctx, t, sfp[0].s))); } base.buf = base.buf + loc + delim.len; base.len = base.len - loc - delim.len; } } } KNH_ASSERT(a != NULL); KNH_RETURN(ctx, sfp, a); }
Array *knh_Regex_split(Ctx *ctx, Regex *o, String *s) { char *str = knh_String_tochar(s); knh_regmatch_t pmatch[KNH_REGEX_NMATCH_SIZE]; int res = o->df->regexec(ctx, o->reg, str, KNH_REGEX_NMATCH_SIZE, pmatch, 0); if(res == 0) { Array *a = new_Array(ctx, CLASS_String, KNH_REGEX_NMATCH_SIZE); knh_bytes_t sub = knh_String_tobytes(s); int i; for(i = 1; i < KNH_REGEX_NMATCH_SIZE; i++) { if(pmatch[i].rm_so == -1) break; //DBG_P("[%d], rm_so=%d, rm_eo=%d", i, pmatch[i].rm_so, pmatch[i].rm_eo); sub.buf = (knh_uchar_t*)str + pmatch[i].rm_so; sub.len = pmatch[i].rm_eo - pmatch[i].rm_so; knh_Array_add(ctx, a, UP(new_String(ctx, sub, s))); } return a; } else { return (Array*)knh_getClassDefaultValue(ctx, ACLASS_String); //return (Array*)KNH_NULL; } }
static knh_sfp_t *knh_Exception_addStackTrace(Ctx *ctx, knh_Exception_t *e, knh_sfp_t *sfp) { knh_intptr_t shift = sfp[K_SHIFTIDX].shift; knh_sfp_t *psfp = sfp - shift; if(ctx->stack < psfp && psfp < sfp) { if(psfp[K_MTDIDX].callmtd != NULL && isCalledMethod(ctx, psfp + K_MTDIDX)) { knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); knh_opline_t *pc = psfp[K_SHIFTIDX].pc; knh_Method_t *mtd = psfp[K_MTDIDX].callmtd; int linenum = pc->line; const char *file = knh_Method_file(ctx, mtd); knh_write_cline(ctx, cwb->w, file, linenum); knh_putc(ctx, cwb->w, ':'); knh_stack_writeStackTrace(ctx, sfp, cwb->w); if(DP(e)->tracesNULL == NULL) { KNH_INITv(DP(e)->tracesNULL, new_Array(ctx, CLASS_String, 0)); } knh_Array_add(ctx, DP(e)->tracesNULL, knh_cwb_newString(ctx, cwb)); sfp = psfp; } } return sfp - 1; }
static SV *node_to_sv(pTHX_ Node *node) { SV *ret = NULL; if (!node) return ret; if (TYPE_match(node, BranchNode)) { BranchNode *branch = dynamic_cast<BranchNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, branch->tk); add_key(hash, "left", branch->left); add_key(hash, "right", branch->right); add_key(hash, "next", branch->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Branch"); } else if (TYPE_match(node, FunctionCallNode)) { FunctionCallNode *call = dynamic_cast<FunctionCallNode *>(node); Nodes *args = call->args; size_t argsize = args->size(); AV *array = new_Array(); for (size_t i = 0; i < argsize; i++) { SV *arg = node_to_sv(aTHX_ args->at(i)); if (!arg) continue; av_push(array, set(arg)); } HV *hash = (HV*)new_Hash(); add_key(hash, "next", call->next); add_token(hash, call->tk); (void)hv_stores(hash, "args", set(new_Ref(array))); ret = bless(aTHX_ hash, "Compiler::Parser::Node::FunctionCall"); } else if (TYPE_match(node, ArrayNode)) { ArrayNode *array = dynamic_cast<ArrayNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, array->tk); add_key(hash, "next", array->next); add_key(hash, "idx", array->idx); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Array"); } else if (TYPE_match(node, HashNode)) { HashNode *h = dynamic_cast<HashNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, h->tk); add_key(hash, "next", h->next); add_key(hash, "key", h->key); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Hash"); } else if (TYPE_match(node, DereferenceNode)) { DereferenceNode *dref = dynamic_cast<DereferenceNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, dref->tk); add_key(hash, "next", dref->next); add_key(hash, "expr", dref->expr); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Dereference"); } else if (TYPE_match(node, FunctionNode)) { FunctionNode *f = dynamic_cast<FunctionNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, f->tk); add_key(hash, "next", f->next); add_key(hash, "body", f->body); add_key(hash, "prototype", f->prototype); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Function"); } else if (TYPE_match(node, BlockNode)) { BlockNode *b = dynamic_cast<BlockNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, b->tk); add_key(hash, "next", b->next); add_key(hash, "body", b->body); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Block"); } else if (TYPE_match(node, ReturnNode)) { ReturnNode *r = dynamic_cast<ReturnNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, r->tk); add_key(hash, "next", r->next); add_key(hash, "body", r->body); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Return"); } else if (TYPE_match(node, SingleTermOperatorNode)) { SingleTermOperatorNode *s = dynamic_cast<SingleTermOperatorNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, s->tk); add_key(hash, "next", s->next); add_key(hash, "expr", s->expr); ret = bless(aTHX_ hash, "Compiler::Parser::Node::SingleTermOperator"); } else if (TYPE_match(node, DoubleTermOperatorNode)) { } else if (TYPE_match(node, LeafNode)) { LeafNode *leaf = dynamic_cast<LeafNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, leaf->tk); add_key(hash, "next", leaf->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Leaf"); } else if (TYPE_match(node, ListNode)) { ListNode *list = dynamic_cast<ListNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, list->tk); add_key(hash, "data", list->data); add_key(hash, "next", list->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::List"); } else if (TYPE_match(node, ArrayRefNode)) { ArrayRefNode *ref = dynamic_cast<ArrayRefNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, ref->tk); add_key(hash, "data", ref->data); add_key(hash, "next", ref->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::ArrayRef"); } else if (TYPE_match(node, HashRefNode)) { HashRefNode *ref = dynamic_cast<HashRefNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, ref->tk); add_key(hash, "data", ref->data); add_key(hash, "next", ref->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::HashRef"); } else if (TYPE_match(node, IfStmtNode)) { IfStmtNode *stmt = dynamic_cast<IfStmtNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, stmt->tk); add_key(hash, "next", stmt->next); add_key(hash, "expr", stmt->expr); add_key(hash, "true_stmt", stmt->true_stmt); add_key(hash, "false_stmt", stmt->false_stmt); ret = bless(aTHX_ hash, "Compiler::Parser::Node::IfStmt"); } else if (TYPE_match(node, ElseStmtNode)) { ElseStmtNode *stmt = dynamic_cast<ElseStmtNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, stmt->tk); add_key(hash, "next", stmt->next); add_key(hash, "stmt", stmt->stmt); ret = bless(aTHX_ hash, "Compiler::Parser::Node::ElseStmt"); } else if (TYPE_match(node, DoStmtNode)) { DoStmtNode *stmt = dynamic_cast<DoStmtNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, stmt->tk); add_key(hash, "next", stmt->next); add_key(hash, "stmt", stmt->stmt); ret = bless(aTHX_ hash, "Compiler::Parser::Node::DoStmt"); } else if (TYPE_match(node, ForStmtNode)) { ForStmtNode *stmt = dynamic_cast<ForStmtNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, stmt->tk); add_key(hash, "next", stmt->next); add_key(hash, "init", stmt->init); add_key(hash, "cond", stmt->cond); add_key(hash, "progress", stmt->progress); add_key(hash, "true_stmt", stmt->true_stmt); ret = bless(aTHX_ hash, "Compiler::Parser::Node::ForStmt"); } else if (TYPE_match(node, ForeachStmtNode)) { ForeachStmtNode *stmt = dynamic_cast<ForeachStmtNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, stmt->tk); add_key(hash, "next", stmt->next); add_key(hash, "itr", stmt->itr); add_key(hash, "cond", stmt->cond); add_key(hash, "true_stmt", stmt->true_stmt); ret = bless(aTHX_ hash, "Compiler::Parser::Node::ForeachStmt"); } else if (TYPE_match(node, WhileStmtNode)) { WhileStmtNode *stmt = dynamic_cast<WhileStmtNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, stmt->tk); add_key(hash, "next", stmt->next); add_key(hash, "true_stmt", stmt->true_stmt); add_key(hash, "expr", stmt->expr); ret = bless(aTHX_ hash, "Compiler::Parser::Node::WhileStmt"); } else if (TYPE_match(node, ModuleNode)) { ModuleNode *mod = dynamic_cast<ModuleNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, mod->tk); add_key(hash, "next", mod->next); add_key(hash, "args", mod->args); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Module"); } else if (TYPE_match(node, PackageNode)) { PackageNode *pkg = dynamic_cast<PackageNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, pkg->tk); add_key(hash, "next", pkg->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Package"); } else if (TYPE_match(node, RegPrefixNode)) { RegPrefixNode *reg = dynamic_cast<RegPrefixNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, reg->tk); add_key(hash, "next", reg->next); add_key(hash, "option", reg->option); add_key(hash, "expr", reg->exp); ret = bless(aTHX_ hash, "Compiler::Parser::Node::RegPrefix"); } else if (TYPE_match(node, RegReplaceNode)) { RegReplaceNode *reg = dynamic_cast<RegReplaceNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, reg->tk); add_key(hash, "next", reg->next); add_key(hash, "from", reg->from); add_key(hash, "to", reg->to); add_key(hash, "option", reg->option); ret = bless(aTHX_ hash, "Compiler::Parser::Node::RegReplace"); } else if (TYPE_match(node, RegexpNode)) { RegexpNode *reg = dynamic_cast<RegexpNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, reg->tk); add_key(hash, "next", reg->next); add_key(hash, "option", reg->option); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Regexp"); } else if (TYPE_match(node, LabelNode)) { LabelNode *label = dynamic_cast<LabelNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, label->tk); add_key(hash, "next", label->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Label"); } else if (TYPE_match(node, HandleNode)) { HandleNode *fh = dynamic_cast<HandleNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, fh->tk); add_key(hash, "expr", fh->expr); add_key(hash, "next", fh->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::Handle"); } else if (TYPE_match(node, HandleReadNode)) { HandleReadNode *fh = dynamic_cast<HandleReadNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, fh->tk); add_key(hash, "next", fh->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::HandleRead"); } else if (TYPE_match(node, ThreeTermOperatorNode)) { ThreeTermOperatorNode *term = dynamic_cast<ThreeTermOperatorNode *>(node); HV *hash = (HV*)new_Hash(); add_token(hash, term->tk); add_key(hash, "cond", term->cond); add_key(hash, "true_expr", term->true_expr); add_key(hash, "false_expr", term->false_expr); add_key(hash, "next", term->next); ret = bless(aTHX_ hash, "Compiler::Parser::Node::ThreeTermOperator"); } else { assert(0 && "node type is not found"); } return ret; }
int main(int argc, char * argv[], char * envp[]) { ParseDelimitedText * parser; int input_file_fd; char input_file_buffer[PDT_BLK_SIZE]; size_t bytes_read; /* Parse command line parameters */ set_params(argc, argv); /* Load the format file */ printf("Reading format file %s\n", format_file->string); format = new_PDTFormat(); format->m->read_file(format, format_file->string, format_file->length); format->columns->m->reset_each(format->columns); /* Open the input file */ input_file_fd = open_input_file(); parser = new_ParseDelimitedText(0); /* Set the parser's options */ parser->m->apply_format(parser, format); parser->m->set_block_size(parser, PDT_BLK_SIZE); parser->m->set_field_callback(parser, header_field); parser->m->set_record_callback(parser, header_record); /* Initialize Globals */ header_fields = new_Array(); header_fields->auto_free = null_String->m->Array_free; valid = 1; field_count = 0; /* Validate the file header */ while(parser->stop == 0 && (bytes_read = read(input_file_fd, input_file_buffer, PDT_BLK_SIZE)) != 0) { parser->m->parse(parser, input_file_buffer, bytes_read); } parser->m->finish(parser); valid = validate_header(); /* Validate the file body */ if(valid == 1) { parser->m->set_field_callback(parser, field); parser->m->set_record_callback(parser, record); parser->stop = 0; while(parser->stop == 0 && (bytes_read = read(input_file_fd, input_file_buffer, PDT_BLK_SIZE)) != 0) { parser->m->parse(parser, input_file_buffer, bytes_read); } parser->m->finish(parser); } /* Cleanup */ close(input_file_fd); parser->m->free(parser); header_fields->m->free(header_fields); format->m->free(format); return (valid) ? SHELL_TRUE : SHELL_FALSE; }
/* added by Wakamori */ void loadPolicy(CTX ctx) { if (enforce_security == 0) return; // load $konoha.home.path/policy knh_setProperty(ctx, new_String(ctx, "role"), (dynamic *)new_String(ctx, role)); CWB_t cwbbuf, *cwb = CWB_open0(ctx, &cwbbuf); kString *s = knh_getPropertyNULL(ctx, STEXT("konoha.home.path")); CWB_write(ctx, cwb, S_tobytes(s)); CWB_write(ctx, cwb, STEXT("/policy")); kInputStream *is = new_InputStream(ctx, NULL, new_Path(ctx, CWB_newString0(ctx, cwb))); if (is == NULL) { DBG_P("policy file not found. All @Restricted annotated method is rescricted"); } else { /* if (enforce_security == 0) { enforce_security = 1; knh_memcpy(role, "Default", 7); role[7] = '\0'; } */ // parse policy file written in JSON // it must be refactored in the future kDictMap *dmap = ctx->share->securityDictMap; kString *line = knh_InputStream_readLine(ctx, is); while (IS_NOTNULL(line)) { //fprintf(stderr, "line=%s\n", S_totext(line)); if (S_equals(line, STEXT("[")) || S_equals(line, STEXT("]"))) { /* ignore */ } else { kString *key = NULL; kArray *a = new_Array(ctx, CLASS_String, 0); const char *idx = NULL; char *p = strstr(S_totext(line), "\"name\": \""); if (p != NULL) { p += 9; // == strlen("\"name\": \"") idx = strchr((const char *)p, '"'); if (idx != NULL) { p[idx - p] = '\0'; //fprintf(stderr, "name: %s\n", p); //knh_DictMap_set(ctx, dmap, new_String(ctx, "name"), new_String(ctx, p)); key = new_String(ctx, p); p = (char *)idx + 1; } } p = strstr((const char *)p, "\"permission\": ["); if (p != NULL) { p += 16; // == strlen("\"permission\": \[\"") idx = strchr((const char *)p, '"'); while (idx != NULL) { p[idx - p] = '\0'; if (strstr((const char *)p, ", ") == NULL) { //fprintf(stderr, "permission: %s\n", p); knh_Array_add(ctx, a, new_String(ctx, p)); } p = (char *)idx + 1; idx = strchr((const char *)p, '"'); } } if (key != NULL) { knh_DictMap_set(ctx, dmap, key, a); } } line = knh_InputStream_readLine(ctx, is); } knh_InputStream_close(ctx, is); } }
int main(int argc, char * argv[], char * envp[]) { ParseDelimitedText * parser; int input_file_fd; char input_file_buffer[PDT_BLK_SIZE]; size_t bytes_read; /* Initalize the classes we're using */ class_Array(); class_String(); class_PDTFormat(); class_PDTColumn(); class_PDTFormatFile(); class_ParseDelimitedText(); /* Parse command line parameters */ set_params(argc, argv); /* Load the format file */ format = new_PDTFormat(); format->m->read_file(format, format_file->string, format_file->length); /* Open the files */ input_file_fd = open_input_file(); output_file_fd = open_output_file(); /* Set the parser's options */ parser = new_ParseDelimitedText(0); parser->m->apply_format(parser, format); parser->m->set_block_size(parser, PDT_BLK_SIZE); parser->m->set_field_callback(parser, fieldcb); parser->m->set_record_callback(parser, recordcb); /* Initialize Globals */ record = new_Array(); record->auto_free = null_String->m->Array_free; format->columns->m->reset_each(format->columns); while(format->columns->m->each(format->columns) != null_ArrayElement) { record->m->append(record, new_String("", 0)); } field_count = 0; valid = 1; output_buffer = new_String("", 0); output_buffer->m->extend(output_buffer, 8191); /* Convert the file to standard form */ while(parser->stop == 0 && (bytes_read = read(input_file_fd, input_file_buffer, PDT_BLK_SIZE)) != 0) { parser->m->parse(parser, input_file_buffer, bytes_read); } parser->m->finish(parser); if(output_file->length != 0) { write_output_file(); } /* Cleanup */ close_input_file(input_file_fd); close_output_file(output_file_fd); parser->m->free(parser); record->m->free(record); format->m->free(format); output_buffer->m->free(output_buffer); return (valid) ? SHELL_TRUE : SHELL_FALSE; }