Example #1
0
void knh_Connection_open(Ctx *ctx, knh_Connection_t *c, knh_String_t *urn)
{
	knh_bytes_t u = S_tobytes(urn);
	KNH_SETv(ctx, (c)->urn, urn);
	(c)->dspi = knh_getQueryDSPI(ctx, S_tobytes(urn));
	(c)->conn = (c)->dspi->qopen(ctx, u);
}
Example #2
0
void knh_write_bool(Ctx *ctx, knh_OutputStream_t *w, int b)
{
	if(b) {
		knh_write(ctx, w, S_tobytes(TS_true));
	}
	else {
		knh_write(ctx, w, S_tobytes(TS_false));
	}
}
Example #3
0
/* added by Wakamori */
static kbool_t method_isPermissionAllowed(CTX ctx, kMethod *mtd)
{
	kDictMap *dmap = ctx->share->securityDictMap;
	CWB_t cwbbuf, *cwb = CWB_open0(ctx, &cwbbuf);
	CWB_write(ctx, cwb, S_tobytes(ClassTBL(mtd->cid)->sname));
	CWB_putc(ctx, cwb, '.');
	CWB_write(ctx, cwb, B(MN__(mtd->mn)));
	DBG_P("[methodname] : %s", CWB_tobytes(cwb).text);
	const char *idx, *p = role;
	while (p[0] != '\0') {
		idx = strchr((const char *)p, ',');
		kArray *a = NULL;
		if (idx != NULL) {
			a = (kArray *)knh_DictMap_getNULL(ctx, dmap, new_bytes2(p, idx - p));
			p = idx + 1;
		} else {
			a = (kArray *)knh_DictMap_getNULL(ctx, dmap, B(p));
			p += knh_strlen(p);
		}
		if (a != NULL) {
			int i;
			for (i = 0; i < knh_Array_size(a); i++) {
				const char *s = S_totext((kString*)knh_Array_n(a, i));
				if (strstr(s, CWB_tobytes(cwb).text) != NULL
					|| strstr(s, "all") != NULL) {
					// this method is allowed to be executed by this role
					CWB_close0(ctx, cwb);
					return 1;
				}
			}
		}
	}
	CWB_close0(ctx, cwb);
	return 0;
}
Example #4
0
KNHAPI(void) knh_write_EOL(Ctx *ctx, knh_OutputStream_t *w)
{
	knh_OutputStream_write(ctx, w, S_tobytes(DP(w)->NEWLINE));
	if(knh_OutputStream_isAutoFlush(w)) {
		knh_OutputStream_flush(ctx, w);
	}
	knh_OutputStream_setBOL(w, 1);
}
Example #5
0
int knh_ResultSet_findColumn(Ctx *ctx, knh_ResultSet_t *o, knh_bytes_t name)
{
	size_t i = 0;
	for(i = 0; i < DP(o)->column_size; i++) {
		if(knh_bytes_strcasecmp(S_tobytes(DP(o)->column[i].name), name) == 0) return i;
	}
	return -1;
}
Example #6
0
static kbytes_t knh_KonohaSpace_getDpiPath(CTX , kKonohaSpace *ns, kbytes_t path)
{
	while(DP(ns)->name2dpiNameDictMapNULL != NULL) {
		kString *s = (kString*)knh_DictMap_getNULL(_ctx, DP(ns)->name2dpiNameDictMapNULL, path);
		if(s != NULL) return S_tobytes(s);
		if(ns->parentNULL == NULL) break;
		ns = ns->parentNULL;
	}
	return path;
}
Example #7
0
KNHAPI(void) knh_write_BOL(Ctx *ctx, knh_OutputStream_t *w)
{
	knh_intptr_t i, n = DP(w)->indent;
	if(!knh_OutputStream_isBOL(w)) {
		knh_write_EOL(ctx, w);
	}
	for(i = 0; i < n; i++) {
		knh_OutputStream_write(ctx, w, S_tobytes(DP(w)->TAB));
	}
	knh_OutputStream_setBOL(w, 0);
}
Example #8
0
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;
}
Example #9
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);
	}
}
Example #10
0
static void knh_setArgs(CTX ctx, Args_t *args, kMap *m)
{
    kDictMap *dmap = knh_toDictMap(ctx, m, 0);
    ksfp_t *lsfp = ctx->esp;
    knitr_t mitrbuf = K_NITR_INIT, *mitr = &mitrbuf;
    klr_setesp(ctx, lsfp+1);
    char buf[256] = {0};
    while(m->spi->next(ctx, m->mapptr, mitr, lsfp)) {
        const char *key = S_totext(lsfp[0].s);
        Object *o = knh_DictMap_getNULL(ctx, dmap, S_tobytes(lsfp[0].s));
        switch (O_cid(o)) {
        case CLASS_Int:
            knh_snprintf(buf, sizeof(buf), "%s=" KINT_FMT, key, N_toint(o));
            break;
        case CLASS_Float:
            knh_snprintf(buf, sizeof(buf), "%s=" KFLOAT_FMT, key, N_tofloat(o));
            break;
        case CLASS_Boolean:
            knh_snprintf(buf, sizeof(buf), "%s=%s",
                         key, N_tobool(o) ? "true" : "false");
            break;
        case CLASS_String:
            knh_snprintf(buf, sizeof(buf), "%s=%s", key, S_totext((kString *)o));
            break;
        default:
            TODO();
            break;
        }
        DBG_P("m[%s] = {class:%s, struct:%s, o:%s}", S_totext(lsfp[0].s),
              CLASS__(O_cid(o)), STRUCT__(O_bcid(o)), O__(o));
        if (knh_strlen(buf) > 0) {
            DBG_P("param: \"%s\"", buf);
            oauth_add_param_to_array(&args->argc, &args->argv, buf);
        }
        buf[0] = '\0';
        klr_setesp(ctx, lsfp+1);
    }
}
Example #11
0
static void load_codegenerator(CTX ctx)
{
    KONOHA_BEGIN(ctx);
    CompilerAPI_disable(ctx);
    if (codegenerator_file[compile_mode]) {
        kbytes_t t = new_bytes((char*)codegenerator_file[compile_mode]);
        knh_loadPackage(ctx, t);
        CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf);
        kString *s = (kString *) knh_DictMap_getNULL(ctx, ctx->share->props, STEXT("konoha.package.path"));
        CWB_clear(cwb, 0);
        knh_buff_addpath(ctx, cwb->ba, cwb->pos, 0, S_tobytes(s));
        knh_buff_addpath(ctx, cwb->ba, cwb->pos, 1, STEXT("konoha.compiler"));
        knh_buff_addpath(ctx, cwb->ba, cwb->pos, 1, STEXT("compiler"));
        knh_buff_addpath(ctx, cwb->ba, cwb->pos, 0, STEXT(K_OSDLLEXT));
        void *p = knh_dlopen(ctx, CWB_totext(ctx, cwb));
        typedef void (*knh_Fpkgcomplete)(CTX);
        knh_Fpkgcomplete pkgcomplete = (knh_Fpkgcomplete) knh_dlsym(ctx, p, "reset_compiler_api", NULL, 1);
        if (pkgcomplete) pkgcomplete(ctx);
        CWB_close(ctx, cwb);
    }
    CompilerAPI_enable(ctx);
    KONOHA_END(ctx);
}
Example #12
0
int knh_addClassConst(CTX, kcid_t cid, kString* name, Object *value)
{
	int ret;
	kDictMap *cmap = ClassTBL(cid)->constDictCaseMapNULL;
	DBG_ASSERT_cid(cid);
	if(cmap == NULL) {
		kclass_t *t = varClassTBL(cid);
		cmap = new_DictMap0(_ctx, 0, 1/*isCaseMap*/, "ClassTBL.constDictMap");
		KINITv(t->constDictCaseMapNULL, cmap);
	}
	DBG_ASSERT(IS_Map(cmap));
	OLD_LOCK(_ctx, LOCK_SYSTBL, NULL);
	int idx = knh_DictMap_index(cmap, S_tobytes(name));
	if(idx != -1) {
		ret =  0;
		goto L_UNLOCK;
	}
	knh_DictMap_set(_ctx, cmap, name, value);
	ret = 1;

	L_UNLOCK:
	OLD_UNLOCK(_ctx, LOCK_SYSTBL, NULL);
	return ret;
}
Example #13
0
int knh_addClassConst(Ctx *ctx, knh_class_t cid, knh_String_t* name, Object *value)
{
	int ret;
	knh_DictCaseMap_t *cmap = ClassTBL(cid).constDictCaseMapNULL;
	DBG_ASSERT_cid(cid);
	if(cmap == NULL) {
		knh_ClassTBL_t *t = pClassTBL(ctx, cid);
		cmap = new_DictCaseMap0(ctx, 0);
		KNH_INITv(t->constDictCaseMapNULL, cmap);
	}
	DBG_ASSERT(IS_Map(cmap));
	OLD_LOCK(ctx, LOCK_SYSTBL, NULL);
	int idx = knh_DictCaseMap_index(cmap, S_tobytes(name));
	if(idx != -1) {
		ret =  0;
		goto L_UNLOCK;
	}
	knh_DictCaseMap_set(ctx, cmap, name, value);
	ret = 1;

	L_UNLOCK:
	OLD_UNLOCK(ctx, LOCK_SYSTBL, NULL);
	return ret;
}
Example #14
0
void knh_System_initPath(CTX ctx, knh_System_t *o)
{
	knh_DictMap_t *sysprops = DP(o)->props;
	knh_path_t phbuf, *ph = knh_path_open(ctx, NULL, ".", &phbuf);
	knh_bytes_t home = {{NULL}, 0}, user = {{NULL}, 0};

	// current working directory
	knh_ospath(ctx, ph);
	KNH_SETv(ctx, ctx->share->rootns->rpath, knh_path_newString(ctx, ph, 0));

	home.text = (const char*)knh_getenv("KONOHAHOME");
#if defined(K_KONOHAHOME)
	if(home.text == NULL) {
		home.text = K_KONOHAHOME;
	}
#endif
	if(home.text != NULL) {
		home.len = knh_strlen(home.text);
		SETPROP("konoha.home.path", new_T(home.text));
	}
#if defined(K_USING_WINDOWS)
	{
		char buf[FILEPATH_BUFSIZ];
		int bufsiz = FILEPATH_BUFSIZ;
		HMODULE h = LoadLibrary(NULL);
		GetModuleFileNameA(h, buf, bufsiz);
		ph = knh_path_open_(ctx, NULL, B(buf), &phbuf);
		SETPROP("konoha.bin.path", knh_path_newString(ctx, ph, 0));
		if(homepath == NULL) {
			knh_String_t *s;
			GetModuleFileNameA(h, buf, bufsiz);
			knh_path_reduce(ctx, ph, '/');
			s = knh_path_newString(ctx, ph);
			SETPROP("konoha.home.path", UPCAST(s));
			home = S_tobytes(shome);
		}
	}
#elif defined(K_USING_LINUX_)
	// @url(http://shinh.skr.jp/binary/b2con.html)
	// http://doc.trolltech.com/3.3/qapplication.html#applicationDirPath
	{
		char buf[FILEPATH_BUFSIZ];
		int bufsiz = FILEPATH_BUFSIZ;
		size_t size = readlink("/proc/self/exe", buf, bufsiz);
		ph = knh_path_open_(ctx, NULL, new_bytes2(buf, size), &phbuf);
		SETPROP("konoha.bin.path", knh_path_newString(ctx, ph, 0));
		if(home.text == NULL) {
			knh_String_t *s;
			knh_path_reduce(ctx, ph, '/');
			knh_path_reduce(ctx, ph, '/');
			knh_path_append(ctx, ph, 1/*isSep*/, "konoha");
			s = knh_path_newString(ctx, ph, 0/*hasScheme*/);
			SETPROP("konoha.home.path", UPCAST(s));
			home = S_tobytes(s);
		}
	}
#elif defined(K_USING_MACOSX_)
	ph = knh_path_open(ctx, NULL, _dyld_get_image_name(0), &phbuf);
	knh_ospath(ctx, ph);
	SETPROP("konoha.bin.path", knh_path_newString(ctx, ph, 0/*hasScheme*/));
	if(home.text == NULL) {
		knh_String_t *s;
		knh_path_reduce(ctx, ph, '/');
		knh_path_reduce(ctx, ph, '/');
		knh_path_append(ctx, ph, 1/*isSep*/, "konoha");
		s = knh_path_newString(ctx, ph, 0/*hasScheme*/);
		SETPROP("konoha.home.path", UPCAST(s));
		home = S_tobytes(s);
	}
#else
	home = STEXT("/opt/konoha");
	SETPROP("konoha.home.path", new_T("/opt/konoha"));
#endif
	DBG_ASSERT(home.utext != NULL);

	/* $konoha.package.path {$konoha.home.path}/package */
	knh_path_reset(ctx, ph, NULL, home);
	knh_path_append(ctx, ph, 1/*sep*/, "package");
	knh_path_append(ctx, ph, 1/*sep*/, LIBK_VERSION);
	SETPROP("konoha.package.path", knh_path_newString(ctx, ph, 0/*hasScheme*/));

	/* $konoha.script.path {$konoha.home.path}/script */
	knh_path_reset(ctx, ph, NULL, home);
	knh_path_append(ctx, ph, 1/*sep*/, "script");
	knh_path_append(ctx, ph, 1/*sep*/, LIBK_VERSION);
	SETPROP("konoha.script.path", knh_path_newString(ctx, ph, 0/*hasScheme*/));

#if defined(K_USING_WINDOWS)
	user.text = knh_getenv("USERPROFILE");
#else
	user.text = knh_getenv("HOME");
#endif
	if(user.text != NULL) {
		/* $user.path */
		user.len = knh_strlen(user.text);
		knh_path_reset(ctx, ph, NULL, user);
		knh_path_append(ctx, ph, 1/*sep*/, K_KONOHAFOLDER);
		SETPROP("user.path", knh_path_newString(ctx, ph, 0));
		knh_ospath(ctx, ph);
		knh_path_mkdir(ctx, ph);

		knh_path_reset(ctx, ph, NULL, user); /* user.package.path */
		knh_path_append(ctx, ph, 1/*sep*/, K_KONOHAFOLDER);
		knh_path_append(ctx, ph, 1/*sep*/, "package");
		knh_path_append(ctx, ph, 1/*sep*/, LIBK_VERSION);
		SETPROP("user.package.path", knh_path_newString(ctx, ph, 0));
	}
	knh_path_close(ctx, ph);
}
Example #15
0
    knh_Connection_open(ctx, o, sfp[1].s);
    RETURN_(sfp[0].o);
}

/* ------------------------------------------------------------------------ */
//## method ResultSet! Connection.query(String query);

#define K_DEFAULT_DSPI          STEXT("")

KMETHOD Connection_query(CTX ctx, ksfp_t *sfp _RIX)
{
    kConnection *c = (kConnection*)sfp[0].o;
    kString *query = (kString*)sfp[1].o;
    kResultSet *rs = (kResultSet*)new_O(ResultSet, knh_getcid(ctx, STEXT("ResultSet")));
    KNH_RCSETv(ctx, sfp[2].o, rs);
    kqcur_t *qcur = (c)->dspi->qexec(ctx, (c)->conn, S_tobytes(query), rs);
    if(qcur != NULL) {
        DP(rs)->qcur = qcur;
        DP(rs)->qcurfree = (c)->dspi->qcurfree;
    }
    else {
        DP(rs)->qcur = NULL;
        DP(rs)->qcurfree = NULL;
        //DP(rs)->qcurfree = knh_getQueryDSPI(ctx, K_DEFAULT_DSPI)->qcurfree;
    }
    KNH_SETv(ctx, DP(rs)->conn, c);
    RETURN_(rs);
}

/* ------------------------------------------------------------------------ */
//## method void Connection.exec(String query);
Example #16
0
KNHAPI(void) knh_write_TAB(Ctx *ctx, knh_OutputStream_t *w)
{
	knh_OutputStream_write(ctx, w, S_tobytes(DP(w)->TAB));
}
Example #17
0
void knh_System_initPath(CTX)
{
	CWB_t cwbbuf, *cwb = CWB_open(_ctx, &cwbbuf);
	kDictMap *sysprops = ctx->share->props;
	kbytes_t home = {{NULL}, 0}, user = {{NULL}, 0};

	// current working directory
	knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 0, STEXT("."));
	KSETv(ctx->share->rootks->path, new_Path(_ctx, knh_buff_newRealPathString(_ctx, cwb->ba, cwb->pos)));

	home.text = (const char*)knh_getenv("KONOHAHOME");
#if defined(K_KONOHAHOME)
	if(home.text == NULL) {
		home.text = K_KONOHAHOME;
	}
#endif
	if(home.text != NULL) {
		home.len = knh_strlen(home.text);
		SETPROP("konoha.home.path", new_T(home.text));
	}
#if defined(K_USING_WINDOWS_)
	{
		char buf[K_PATHMAX];
		int bufsiz = K_PATHMAX;
		HMODULE h = LoadLibrary(NULL);
		GetModuleshortnameA(h, buf, bufsiz);
		CWB_clear(cwb, 0);
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 0, B(buf));
		SETPROP("konoha.bin.path", knh_buff_newRealPathString(_ctx, cwb->ba, cwb->pos));
		if(home.text == NULL) {
			kString *s;
			CWB_clear(cwb, 0);
			knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 0, B(buf));
			knh_buff_trim(_ctx, cwb->ba, cwb->pos, '\\');
			knh_buff_trim(_ctx, cwb->ba, cwb->pos, '\\');
			knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 1/*isSep*/, STEXT("konoha"));
			s = CWB_newString(_ctx, cwb, 0);
			SETPROP("konoha.home.path", s);
			home = S_tobytes(s);
		}
	}
#elif defined(K_USING_LINUX_)
	// @url(http://shinh.skr.jp/binary/b2con.html)
	// http://doc.trolltech.com/3.3/qapplication.html#applicationDirPath
	{
		char buf[K_PATHMAX];
		int bufsiz = K_PATHMAX;
		ssize_t size = readlink("/proc/self/exe", buf, bufsiz);
		CWB_clear(cwb, 0);
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 0, new_bytes2(buf, size));
		SETPROP("konoha.bin.path", knh_buff_newRealPathString(_ctx, cwb->ba, cwb->pos));
		if(home.text == NULL) {
			kString *s;
			CWB_clear(cwb, 0);
			knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 0, new_bytes2(buf, size));
			knh_buff_trim(_ctx, cwb->ba, cwb->pos, '/');
			knh_buff_trim(_ctx, cwb->ba, cwb->pos, '/');
			knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 1/*isSep*/, STEXT("konoha"));
			s = CWB_newString(_ctx, cwb, 0);
			SETPROP("konoha.home.path", s);
			home = S_tobytes(s);
		}
	}
#elif defined(K_USING_MACOSX_)
	{
		kString *binpath;
		CWB_clear(cwb, 0);
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 0, B(_dyld_get_image_name(0)));
		binpath = knh_buff_newRealPathString(_ctx, cwb->ba, cwb->pos);
		SETPROP("konoha.bin.path", binpath);
		if(home.text == NULL) {
			CWB_clear(cwb, 0);
			knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 0, S_tobytes(binpath));
			knh_buff_trim(_ctx, cwb->ba, cwb->pos, '/');
			knh_buff_trim(_ctx, cwb->ba, cwb->pos, '/');
			knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 1/*isSep*/, STEXT("konoha"));
			kString *s = CWB_newString(_ctx, cwb, 0);
			SETPROP("konoha.home.path", s);
			home = S_tobytes(s);
		}
	}
#else
	home = STEXT("/opt/konoha");
	SETPROP("konoha.home.path", new_T("/opt/konoha"));
#endif
	DBG_ASSERT(home.utext != NULL);

	/* $konoha.package.path {$konoha.home.path}/package */
	CWB_clear(cwb, 0);
	knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 0, home);
	knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 1/*sep*/, STEXT("package"));
	knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 1/*sep*/, STEXT(LIBK_VERSION));
	SETPROP("konoha.package.path", CWB_newString(_ctx, cwb, 0));

	/* $konoha.script.path {$konoha.home.path}/script */
	knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 0, home);
	knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 1/*sep*/, STEXT("script"));
	knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 1/*sep*/, STEXT(LIBK_VERSION));
	SETPROP("konoha.script.path", CWB_newString(_ctx, cwb, 0));

#if defined(K_USING_WINDOWS_)
	user.text = knh_getenv("USERPROFILE");
#else
	user.text = knh_getenv("HOME");
#endif
	if(user.text != NULL) {
		/* $user.path */
		user.len = knh_strlen(user.text);
		knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 0, user);
		knh_buff_addpath(_ctx, cwb->ba, cwb->pos, 1, STEXT(K_KONOHAFOLDER));
		SETPROP("user.path", CWB_newString(_ctx, cwb, 0));
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 0, user);
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 1, STEXT(K_KONOHAFOLDER));
		knh_buff_mkdir(_ctx, cwb->ba, cwb->pos);
		CWB_clear(cwb, 0);
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 0, user);
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 1/*sep*/, STEXT(K_KONOHAFOLDER));
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 1/*sep*/, STEXT("package"));
		knh_buff_addospath(_ctx, cwb->ba, cwb->pos, 1/*sep*/, STEXT(LIBK_VERSION));
		SETPROP("user.package.path", CWB_newString(_ctx, cwb, 0));
	}
	CWB_close(_ctx, cwb);
}
Example #18
0
/* 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);
	}
}
Example #19
0
//@Native @Public System.ntrace_notice(String title, Map ldata);
KMETHOD System_ntraceNotice (CTX ctx, ksfp_t *sfp _RIX)
{
	char *title = String_to(char *, sfp[1]);
	kMap *mdata = sfp[2].m;
	kmapptr_t *mapptr = mdata->mapptr;
	knh_ldata_t log_buffer[64] = {0};
	int logidx = 0;
	// suppose p1 is "String"
	ksfp_t *lsfp = ctx->esp;
	knitr_t mitrbuf = K_NITR_INIT, *mitr = &mitrbuf;
	klr_setesp(ctx, lsfp+1);
	while(mdata->spi->next(ctx, mapptr, mitr, lsfp)) {
		kString *key = lsfp[0].s;
		kObject *value = knh_DictMap_getNULL(ctx, (kDictMap*)mdata, S_tobytes(key));
		kclass_t cid = O_cid(value);
		if (cid == CLASS_Int) {
			knh_ldata_t item[] = {LOG_i(S_totext(key), N_toint(value))};
			log_buffer[logidx++] = item[0];
			log_buffer[logidx++] = item[1];
			log_buffer[logidx++] = item[2];
		} else if (cid == CLASS_Float) {
			knh_ldata_t item[] = {LOG_f(S_totext(key), N_tofloat(value))};
			log_buffer[logidx++] = item[0];
			log_buffer[logidx++] = item[1];
			log_buffer[logidx++] = item[2];
		} else if (cid == CLASS_String) {
			knh_ldata_t item[] = {LOG_s(S_totext(key), S_totext((kString*)value))};
			log_buffer[logidx++] = item[0];
			log_buffer[logidx++] = item[1];
Example #20
0
//	char buf[128] = {0};
//	int pid = fork();
//	snprintf(buf, 128, "/usr/local/bin/konoha %s/%s", dir_name, script_name);
//	if (pid == 0) {
//		system(buf);
//		exit(0);
//	}
//}

/* ======================================================================== */
// [KMETHODS]

//## @Hidden Actor Actor.opLINK(String path, NameSpace _);
KMETHOD Actor_opLINK(CTX ctx, knh_sfp_t *sfp _RIX)
{
	knh_bytes_t host_port = knh_bytes_next(S_tobytes(sfp[1].s), ':');
	knh_bytes_t host;
	knh_index_t idx = knh_bytes_index(host_port, ':');
	if (idx == -1) {
		LOGDATA = {sDATA("path", host_port.text)};
		LIB_Failed("Actor.opLINK", "Script!!");
		knh_Object_toNULL(ctx, sfp[0].o);
		RETURN_(sfp[0].o);
	}
	host = new_bytes2(host_port.text, idx);
	knh_int_t port;
	if (!knh_bytes_parseint(knh_bytes_next(host_port, ':'), &port)) {
		LOGDATA = {sDATA("path", host_port.text)};
		LIB_Failed("Actor.opLINK", "Script!!");
		knh_Object_toNULL(ctx, sfp[0].o);
		RETURN_(sfp[0].o);