static METHOD Method_getName(Ctx *ctx, knh_sfp_t *sfp, long rix)
{
	
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_write_mn(ctx, cwb->w, DP(sfp[0].mtdOBJ)->mn);
	RETURN_(knh_cwb_newString(ctx, cwb));
}
Exemple #2
0
static METHOD knh__String_replace(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_bytes_t base = knh_String_tobytes(sfp[0].s);
	knh_bytes_t target = knh_String_tobytes(sfp[1].s);
	knh_bytes_t alt = knh_String_tobytes(sfp[2].s);
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	int search_flag= 0, ch = target.buf[0], i;

	if (base.len == 0 || target.len == 0) KNH_RETURN(ctx, sfp, sfp[0].o);
	for(i = 0; i < base.len - target.len+1; i++) {
		if(base.buf[i] == ch && knh_bytes_equals_(base, i, target)) {
		    knh_Bytes_write(ctx, cwb->ba, alt);
			i += target.len - 1;
			search_flag = 1;
		}else {
			knh_Bytes_putc(ctx, cwb->ba, base.buf[i]);
		}
	}
	if(search_flag == 0) {
		KNH_RETURN(ctx, sfp, sfp[0].o);
	}
	else {
		knh_bytes_t leftover = {base.buf + i, base.len - i};
		knh_Bytes_write(ctx, cwb->ba, leftover);
		KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
	}
}
KNHAPI(void) knh_OutputStream_writeLine(Ctx *ctx, knh_OutputStream_t *w, knh_bytes_t t, knh_bool_t isNEWLINE)
{
	if(knh_OutputStream_isBOL(w)) {
		knh_write_BOL(ctx, w);
	}
	if(t.len > 0) {
		if(DP(w)->encNULL != NULL && !knh_bytes_isASCII(t)) {
			if(knh_bytes_in(ctx->bufa->bu, t.ubuf)) {
				KNH_TODO("write cwb->buf with encoding");
			}
			else {
				knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
				knh_StringEncoder_t *c = DP(w)->encNULL;
				c->dspi->enc(ctx, c->conv, t, cwb->ba);
				knh_write(ctx, w, knh_cwb_tobytes(cwb));
				knh_cwb_close(cwb);
			}
		}
		else {
			knh_OutputStream_write(ctx, w, t);
		}
	}
	if(isNEWLINE) {
		knh_write_EOL(ctx, w);
	}
}
void knh_makeEvidence(Ctx *ctx, const char *ns, const char *event, int p, const char *fmt, ...)
{
	if(p > LOG_WARNING && !knh_isSystemVerbose()) return;
	va_list ap;
	va_start(ap , fmt);
	if(fmt[0] == '*') {
		char newfmt[512];
		knh_snprintf(newfmt, sizeof(newfmt), K_EVENT_FORMAT "%s", ns, event, fmt+1);
		ctx->share->ebiSPI->vsyslog(p, newfmt, ap);
	}
	else {
		DBG_ASSERT(ctx->bufa != NULL);
		knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
		knh_printf(ctx, cwb->w, K_EVENT_FORMAT, ns, event);
		if(ctx->gma != NULL && SP(ctx->gma)->line != 0) {
			knh_write_uline(ctx, cwb->w, SP(ctx->gma)->uri, SP(ctx->gma)->line);
		}
		knh_vprintf(ctx, cwb->w, fmt, ap);
		ctx->share->ebiSPI->syslog(p, knh_cwb_tochar(ctx, cwb));
		knh_cwb_clear(cwb, 0);
	}
	va_end(ap);
	if(p == LOG_EMERG || p == LOG_CRIT) {
#if defined(K_USING_DEBUG)
		DBG_ABORT();
#endif
		knh_exit(ctx, 0);
	}
}
Exemple #5
0
static METHOD knh__String_opSub(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_bytes_t base = knh_String_tobytes(sfp[0].s);
	knh_bytes_t t = knh_String_tobytes(sfp[1].s);
	knh_uchar_t c = t.buf[0];
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	size_t i;
	for(i = 0; i < base.len; i++) {
		if(base.buf[i] == c) {
			size_t j;
			for(j = 1; j < t.len; j++) {
				if(base.buf[i+j] != t.buf[j]) break;
			}
			if(j == t.len) {
				i += t.len - 1;
				continue;
			}
		}
		knh_Bytes_putc(ctx, cwb->ba, base.buf[i]);
	}
	if(base.len == knh_cwb_size(cwb)) {
		knh_cwb_close(cwb);
		KNH_RETURN(ctx, sfp, sfp[0].o);
	}
	else {
		KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
	}
}
knh_String_t* knh_InputStream_readLine(Ctx *ctx, knh_InputStream_t *in)
{
	int ch;
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	if(DP(in)->decNULL == NULL && in->dspi->getCharset != NULL) {
		char *charset = (char*)in->dspi->getCharset(ctx, DP(in)->fd);
		if(charset != NULL) {
			knh_InputStream_setCharset(ctx, in, new_StringDecoderNULL(ctx, B(charset), NULL));
		}
	}
	while((ch = knh_InputStream_getc(ctx, in)) != EOF) {
		if(ch == '\r') {
			DP(in)->prev = ch;
			goto L_TOSTRING;
		}
		else if(ch == '\n') {
			if(DP(in)->prev == '\r') continue;
			DP(in)->prev = ch;
			goto L_TOSTRING;
		}
		else {
			knh_Bytes_putc(ctx, cwb->ba, ch);
		}
	}
	L_TOSTRING:;
	if(knh_cwb_size(cwb) != 0) {
		if(DP(in)->decNULL == NULL) {
			return knh_cwb_newString(ctx, cwb);
		}
		else {
			return knh_cwb_newStringDECODE(ctx, cwb, DP(in)->decNULL);
		}
	}
	return KNH_TNULL(String);
}
knh_String_t* knh_getPropertyNULL(Ctx *ctx, knh_bytes_t key)
{
	if(knh_bytes_startsWith(key, STEXT("env."))) {
		knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
		knh_cwb_nzenvkey(ctx, cwb, knh_bytes_last(key, 4));
		char *v = knh_getenv(knh_cwb_tochar(ctx, cwb));
		knh_cwb_close(cwb);
		if(v == NULL) return NULL;
		return new_S(ctx, B(v));
	}
	return (knh_String_t*)knh_DictMap_getNULL(ctx,  DP(ctx->sys)->props, key);
}
Exemple #8
0
knh_cwb_t* knh_cwb_copy(CTX ctx, knh_cwb_t *cwbbuf, knh_path_t *ph, int hasScheme)
{
	knh_cwb_t *cwb = knh_cwb_open(ctx, cwbbuf);
	knh_Bytes_ensureSize(ctx, cwb->ba, ph->plen + 1);
	knh_bytes_t t = {{P_text(ph)}, ph->plen};
	if(!hasScheme) {
		t.text = t.text + ph->pbody;
		t.len = t.len - ph->pbody;
	}
	knh_Bytes_write(ctx, cwb->ba, t);
	return cwb;
}
//## method @Virtual void Chardev.read(File! file, OutputStream ous);
static ssize_t script_read (struct file* filp, char __user *user_buf,
		size_t count, loff_t *offset)
{
	printk("%s at %d\n",__func__,__LINE__);
	knh_device_t *dev = filp->private_data;
	knh_Object_t *self = dev->self;

	if(*offset > 0) return 0;
	Ctx *ctx = knh_getCurrentContext();
	knh_sfp_t *sfp = KNH_LOCAL(ctx);
	knh_Method_t *mtd = dev->read;
	if (!IS_Method(mtd) || knh_Method_isAbstract(mtd)) {
		knh_bytes_t fname = B("read");
		knh_methodn_t mn = knh_getmn(ctx, fname, METHODN_NONAME);
		mtd = knh_Class_getMethod(ctx, knh_Object_cid(self), mn);
		KNH_SETv(ctx, dev->read, mtd);
	}
	//knh_File_t *f = (knh_File_t*)new_Object_bcid(ctx, CLASS_File, 0);
	//f->file = filp;

	if (IS_Method(mtd)) {
		KNH_MOV(ctx, sfp[0].o, new_ExceptionHandler(ctx));
		KNH_TRY(ctx, L_CATCH, sfp, 0);
		{
			knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
			// THIS IS DANGEROUS. DON'T USED by KIMIO
//			knh_OutputStream_t *o = new_BytesOutputStream(ctx, cwb->ba);
//			KNH_MOV(ctx, sfp[1].o, self);
//			//KNH_MOV(ctx, sfp[2].o, f);
//			KNH_MOV(ctx, sfp[2].o, KNH_NULL);
//			KNH_MOV(ctx, sfp[3].o, o);
//			KNH_SCALL(ctx, sfp, 0, mtd, 2);
//			{
//				size_t len  = knh_cwb_size(cwb);
//				char   *buf = knh_cwb_tochar(ctx, cwb);
//				if(copy_to_user(user_buf,buf,len)){
//					printk(KERN_ALERT "%s: copy_to_user failed\n",__func__);
//					return -EFAULT;
//				}
//				knh_cwb_close(cwb);
//				knh_Context_clearstack(ctx);
//				*offset += len;
//				return len;
//			}
		}
		/* catch */
		L_CATCH:;
		KNH_PRINT_STACKTRACE(ctx, sfp, 0);
	}
	return 0;
}
Exemple #10
0
static METHOD knh__String_format(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_bytes_t fmt = knh_String_tobytes(sfp[0].s);
	knh_sfp_t *param = sfp + 1;
	int ac = knh_stack_argc(ctx, param);
	knh_bytes_t mt, expr, next;

	if(!knh_bytes_findMT(ctx, fmt, &mt, &expr, &next)) {
		KNH_RETURN(ctx, sfp, sfp[0].s);
	}

	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	int count;
	for(count = 0; ; count++) {
		if(mt.buf > fmt.buf + 1) {
			fmt.len = (mt.buf - fmt.buf) - 1;
			knh_Bytes_write(ctx, cwb->ba, fmt);
		}
		int index = count;
		if(expr.len > 0) {
			knh_int_t num;
			if(knh_bytes_parseint(expr, &num)) {
				index = (int)num;
			}
		}
		if(0 <= index && index < ac) {
			knh_sfp_t *esp = KNH_LOCAL(ctx);
			KNH_SETv(ctx, esp[1].o, param[index].o); esp[1].data = param[index].data;
			Object *m = KNH_NULL;
			if(knh_bytes_isOptionalMT(mt)) m = UP(new_String(ctx, mt, NULL));
			mt.buf = mt.buf - 1; mt.len++;   /* 's' == > '%s' */
			knh_methodn_t mn = knh_getmn(ctx, mt, METHODN__empty);
			knh_esp1_format(ctx, mn, cwb->w, m);
		}
		else {
			if(knh_Context_isDebug(ctx)) {
				KNH_THROW_OUTOFINDEX(ctx, index, ac);
			}
		}
		fmt.buf = next.buf; fmt.len = next.len;
		if(!knh_bytes_findMT(ctx, fmt, &mt, &expr, &next)) {
			break;
		}
	}
	if(fmt.len > 0) {
		knh_Bytes_write(ctx, cwb->ba, fmt);
	}
	KNH_RETURN(ctx, sfp, new_StringX__cwb(ctx, knh_Object_cid(sfp[0].o), cwb));
}
Exemple #11
0
static METHOD knh__String_concat(Ctx *ctx, knh_sfp_t *sfp)
{
	int i, ac = knh_stack_argc(ctx, sfp);
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_sfp_t *esp = KNH_LOCAL(ctx);
	for(i = 0; i < ac; i++) {
		if(IS_bString(sfp[i].o)) {
			knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[i].s));
		}
		else {
			KNH_ASSERT(esp == ctx->esp);
			KNH_SETv(ctx, esp[1].o, sfp[i].o); esp[1].data = sfp[i].data;
			knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL);
		}
	}
	KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
}
Exemple #12
0
METHOD knh__Script_eval(Ctx *ctx, knh_sfp_t *sfp)
{
	if(IS_NOTNULL(sfp[1].s)) {
		knh_sfp_t *lsfp = KNH_LOCAL(ctx);
		knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
		knh_cwb_write(ctx, cwb, knh_String_tobytes(sfp[1].s));
		knh_cwb_putc(ctx, cwb, '\n');
		InputStream *in = new_BytesInputStream(ctx, cwb->ba, cwb->pos, knh_Bytes_size(cwb->ba));
		KNH_LPUSH(ctx, in);
		DP(in)->uri = URI_EVAL;
		DP(in)->line = 0;
		knh_NameSpace_load(ctx, ctx->share->mainns, in, 1/*isEval*/,0/*isThrowable*/);
		knh_cwb_close(cwb);
		KNH_LOCALBACK(ctx, lsfp);
	}
	KNH_RETURN_void(ctx, sfp);
}
Exemple #13
0
static METHOD knh__String_opMul(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_intptr_t n = p_int(sfp[1]);
	if(n <= 0) {
		KNH_RETURN(ctx, sfp, TS_EMPTY);
	}else if(n == 1) {
		KNH_RETURN(ctx, sfp, sfp[0].o);
	}
	else {
		knh_bytes_t base = knh_String_tobytes(sfp[0].s);
		knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
		knh_intptr_t i;
		for(i = 0; i < n; i++) {
			knh_Bytes_write(ctx, cwb->ba, base);
		}
		KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
	}
}
static void knh_vsyslog(Ctx *ctx, int p, const char *fmt, va_list ap)
{
	if(p > LOG_WARNING && !knh_isSystemVerbose()) return;
#ifdef KONOHA_ON_LKM
	//TODO_IDE();
	vprintk(fmt, ap);
#else
	if(ctx == NULL) {
		fprintf(stderr, "konoha[%s] ", LOG__(p));
		vfprintf(stderr, fmt, ap);
		fprintf(stderr, "\n");
	}
	else {
		knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
		knh_printf(ctx, cwb->w, "konoha[%s] ", LOG__(p));
		knh_vprintf(ctx, cwb->w, fmt, ap);
		fprintf(stderr, "%s\n", knh_cwb_tochar(ctx, cwb));
		knh_cwb_clear(cwb, 0);
	}
#endif
}
Exemple #15
0
static METHOD knh__String_opAdd(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_sfp_t *esp = KNH_LOCAL(ctx);
	if(IS_bString(sfp[0].o)) {
		knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[0].s));
	}
	else {
		KNH_SETv(ctx, esp[1].o, sfp[0].o); esp[1].data = sfp[0].data;
		knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL);
	}
	if(IS_bString(sfp[1].o)) {
		knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[1].s));
	}
	else {
		KNH_ASSERT(esp == ctx->esp);
		KNH_SETv(ctx, esp[1].o, sfp[1].o); esp[1].data = sfp[1].data;
		knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL);
	}
	KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
}
Exemple #16
0
static knh_String_t *Gamma_vperror(CTX ctx, int pe, const char *fmt, va_list ap)
{
	knh_String_t *msg = TS_EMPTY;
	int isPRINT = (pe <= KC_DWARN) ? 1 : 0;
	if(pe != KC_DEBUG && (CTX_isInteractive(ctx) || knh_isCompileOnly(ctx))) {
		isPRINT = 1;
	}
	if(Gamma_isQuiet(ctx->gma) || ctx->gma->uline == 0) {
		isPRINT = 0;
	}
	//DBG_P("/*isPRINT=%d*/ uline=%d", isPRINT, ctx->gma->uline);
	if(isPRINT == 1) {
		knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
		knh_write_uline(ctx, cwb->w, ctx->gma->uline);
		knh_write_ascii(ctx, cwb->w, KC__(pe));
		knh_vprintf(ctx, cwb->w, fmt, ap);
		msg = knh_cwb_newString(ctx, cwb);
		knh_Array_add(ctx, DP(ctx->gma)->errmsgs, msg);
		fprintf(stderr, "%s - %s%s\n", TERM_BNOTE(ctx, pe), S_tochar(msg), TERM_ENOTE(ctx, pe));
	}
	return msg;
}
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 knh_String_t *new_String__float(Ctx *ctx, knh_float_t n)
{
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_write_ffmt(ctx, cwb->w, K_FLOAT_FMT, n);
	return knh_cwb_newString(ctx, cwb);
}
static knh_String_t *new_String__int(Ctx *ctx, knh_int_t n)
{
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_write_ifmt(ctx, cwb->w, K_INT_FMT, n);
	return knh_cwb_newString(ctx, cwb);
}