Example #1
0
//## String DIR.readFileName()
static KMETHOD DIR_readFileName(KonohaContext *kctx, KonohaStack *sfp)
{
	kDir *dir = (kDir *)sfp[0].asObject;
	if(dir->dirp != NULL) {
		KMakeTrace(trace, sfp);
		struct dirent entry, *result;
		int ret = readdir_r(dir->dirp, &entry, &result);
		if(result != NULL) {
			char *d_name = result->d_name;
			if(dir->readerIconv == ICONV_NULL) {
				KReturn(KLIB new_kString(kctx, OnStack, d_name, strlen(d_name), StringPolicy_SystemInfo));
			}
			else {
				KBuffer wb;
				KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
				KLIB KBuffer_iconv(kctx, &wb, dir->readerIconv, d_name, strlen(d_name), trace);
				KReturn(KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer));
			}
		}
		if(ret == -1) {
			KTraceErrorPoint(trace, SystemFault, "readdir", LogErrno);
		}
		kDir_close(kctx, dir);
	}
	KReturn(KNULL(String));
}
Example #2
0
//## String Curl.receiveString();
static KMETHOD Curl_receiveString(KonohaContext *kctx, KonohaStack *sfp)
{
	kCurl* kcurl = (kCurl *)sfp[0].asObject;

	/* presets */
	struct ReceiveBuffer rbuf = {0};
	rbuf.kctx = kctx;
	KLIB KBuffer_Init(&(kctx->stack->cwb), &rbuf.wb);
	curl_easy_setopt(kcurl->curl, CURLOPT_WRITEFUNCTION, writeToBuffer);
	curl_easy_setopt(kcurl->curl, CURLOPT_WRITEDATA, &rbuf);

	/* perform */
	KMakeTrace(trace, sfp);
	CURLcode res;
	if(kcurl->headers != NULL) {
		curl_easy_setopt(kcurl->curl, CURLOPT_HTTPHEADER, kcurl->headers);
	}
	KTraceResponseCheckPoint(trace, 0, "curl_easy_perform",
		res = curl_easy_perform(kcurl->curl)
	);
	if(res != CURLE_OK) {
		int fault = diagnosisCurlFaultType(kctx, res, (kcurl->URLInfoNULL == NULL) ? 0 : kString_GuessUserFault(kcurl->URLInfoNULL));
		KTraceErrorPoint(trace, fault, "curl_easy_perform", LogURL(kcurl), LogCurlStrError(res));
	}
	KReturn(KLIB KBuffer_Stringfy(rbuf.kctx, &rbuf.wb, OnStack, StringPolicy_FreeKBuffer));
}
Example #3
0
//## String DIR.readPath()
static KMETHOD DIR_readPath(KonohaContext *kctx, KonohaStack *sfp)
{
	kDir *dir = (kDir *)sfp[0].asObject;
	if(dir->dirp != NULL) {
		KMakeTrace(trace, sfp);
		struct dirent entry, *result;
		int ret = readdir_r(dir->dirp, &entry, &result);
		if(result != NULL) {
			char *d_name = result->d_name, delim[2] = {'/', 0};
			KBuffer wb;
			KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
			KLIB KBuffer_Write(kctx, &wb, kString_text(dir->PathInfoNULL), kString_size(dir->PathInfoNULL));
			KLIB KBuffer_Write(kctx, &wb, delim, 1);
			if(dir->readerIconv != ICONV_NULL) {
				KLIB KBuffer_Write(kctx, &wb, d_name, strlen(d_name));
			}
			else {
				KLIB KBuffer_iconv(kctx, &wb, dir->readerIconv, d_name, strlen(d_name), trace);
			}
			KReturn(KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer));
		}
		if(ret == -1) {
			KTraceErrorPoint(trace, SystemFault, "readdir", LogErrno);
		}
		kDir_close(kctx, dir);
	}
	KReturn(KNULL(String));
}
Example #4
0
// boolean NameSpace.import(String pkgname, String symbol);
static KMETHOD NameSpace_ImportPackageSymbol(KonohaContext *kctx, KonohaStack *sfp)
{
	kString *key = sfp[2].asString;
	ksymbol_t keyword = ksymbolA(S_text(key), S_size(key), _NEWID);
	KMakeTrace(trace, sfp);
	kNameSpace_ImportPackageSymbol(kctx, sfp[0].asNameSpace, S_text(sfp[1].asString), keyword, trace);
}
Example #5
0
// boolean NameSpace.load(String path);
static KMETHOD NameSpace_loadScript(KonohaContext *kctx, KonohaStack *sfp)
{
	char pathbuf[512];
	const char *path = PLATAPI formatTransparentPath(pathbuf, sizeof(pathbuf), FileId_t(sfp[K_RTNIDX].calledFileLine), S_text(sfp[1].asString));
	KMakeTrace(trace, sfp);
	kNameSpace_LoadScript(kctx, sfp[0].asNameSpace, path, trace);
}
Example #6
0
// boolean NameSpace.hate(String symbol);
static KMETHOD NameSpace_hate(KonohaContext *kctx, KonohaStack *sfp)
{
	kString *key = sfp[2].asString;
	ksymbol_t keyword = ksymbolA(S_text(key), S_size(key), _NEWID);
	KMakeTrace(trace, sfp);
	kNameSpace_RemoveSyntax(kctx, sfp[0].asNameSpace, keyword, trace);
}
Example #7
0
//## @Native void File.putc(int ch);
static KMETHOD File_putc(KonohaContext *kctx, KonohaStack *sfp)
{
	kFile *file = (kFile *)sfp[0].asObject;
	KMakeTrace(trace, sfp);
	TRACE_fputc(kctx, file, sfp[1].intValue, trace);
	KReturnVoid();
}
Example #8
0
//## @Native int File.getc();
static KMETHOD File_getc(KonohaContext *kctx, KonohaStack *sfp)
{
	kFile *file = (kFile *)sfp[0].asObject;
	KMakeTrace(trace, sfp);
	int ch = file->fp == NULL ? EOF : TRACE_fgetc(kctx, file, trace);
	KReturnUnboxValue(ch);
}
Example #9
0
//## FILE FILE.new(String path, String mode);
static KMETHOD File_new(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX];
	kString *path = sfp[1].asString;
	const char *systemPath = I18NAPI formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	const char *mode = kString_text(sfp[2].asString);
	FILE *fp = fopen(systemPath, mode);
	kFile *file = (kFile *) sfp[0].asObject;
	if(fp == NULL) {
		int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace);
		KTraceErrorPoint(trace, fault, "fopen",
			LogText("filename", kString_text(path)), LogText("mode", mode), LogErrno);
		KLIB KRuntime_raise(kctx, KException_("IO"), fault, NULL, sfp);
	}
	if(mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') {
		KTraceChangeSystemPoint(trace, "fopen", LogFileName(kString_text(path)), LogText("mode", mode));
	}
	file->fp = fp;
	KFieldInit(file, file->PathInfoNULL, path);
	if(!I18NAPI isSystemCharsetUTF8(kctx)) {
		if(mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') {
			file->writerIconv = I18NAPI iconvUTF8ToSystemCharset(kctx, trace);
		}
		else {
			file->readerIconv = I18NAPI iconvSystemCharsetToUTF8(kctx, trace);
		}
	}
	KReturn(file);
}
Example #10
0
//## @Native int File.write(Bytes buf);
static KMETHOD File_Write(KonohaContext *kctx , KonohaStack *sfp)
{
	kFile *file = sfp[0].asFile;
	kBytes *ba  = sfp[1].asBytes;
	KMakeTrace(trace, sfp);
	size_t writtenbyte = TRACE_fwrite(kctx, file, ba->buf, ba->bytesize, trace);
	KReturnUnboxValue(writtenbyte);
}
Example #11
0
//## @Native void File.close();
static KMETHOD File_close(KonohaContext *kctx, KonohaStack *sfp)
{
	kFile *file = (kFile *)sfp[0].asObject;
	if(file->fp != NULL) {
		KMakeTrace(trace, sfp);
		kFile_close(kctx, file, trace);
	}
	KReturnVoid();
}
Example #12
0
//## int System.fchdir(int fd)
static KMETHOD System_fchdir(KonohaContext *kctx, KonohaStack *sfp)
{
	int fd = fchdir(sfp[1].intValue);
	if(fd == -1) {
		// TODO: throw
		KMakeTrace(trace, sfp);
		int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace);
		KTraceErrorPoint(trace, fault, "fchdir", LogUint("fd", fd));
	}
	KReturnUnboxValue(fd == 0);
}
Example #13
0
//## Stat System.fstat(int fd)
static KMETHOD System_fstat(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	int fd = (int)sfp[1].intValue;
	struct stat buf = {}; /* zero */
	int ret = fstat(fd, &buf);
	if(ret == -1) {
		KTraceErrorPoint(trace, SystemFault|SoftwareFault, "fstat", LogInt("fildes", fd), LogErrno);
	}
	KReturn(KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), (uintptr_t)&buf));
}
Example #14
0
//## boolean chdir(String path)
static KMETHOD System_chdir(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX];
	kString *path = sfp[1].asString;
	const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	int ret = chdir(systemPath);
	if(ret == -1) {
		KTraceErrorPoint(trace, SystemFault, "chdir", LogFileName(kString_text(path)), LogErrno);
	}
	KReturnUnboxValue(ret != -1);
}
Example #15
0
//## String System.ttyname(int fd);
static KMETHOD System_ttyname(KonohaContext *kctx, KonohaStack *sfp)
{
	int fd = sfp[1].intValue;
	char buf[K_PAGESIZE];
	int ret = ttyname_r(fd, buf, sizeof(buf));
	if(ret != 0) {
		KMakeTrace(trace, sfp);
		int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace);
		KTraceErrorPoint(trace, fault, "ttyname", LogUint("fd", fd), LogErrno);
	}
	KReturn(KLIB new_kString(kctx, OnStack, buf, strlen(buf), 0));
}
Example #16
0
//## void NameSpace.useStaticFunc(Object o);
static KMETHOD NameSpace_useStaticFunc(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	KonohaClass *ct = O_ct(sfp[1].asObject);
	kNameSpace *ns = sfp[0].asNameSpace;
	kNameSpace_SetStaticFunction(kctx, ns, ct->methodList_OnGlobalConstList, ct->typeId, trace);
	while(ns != NULL) {
		kNameSpace_SetStaticFunction(kctx, ns, ns->methodList_OnList, ct->typeId, trace);
		ns = ns->parentNULL;
	}
	KReturnVoid();
}
Example #17
0
//## boolean System.isDir(String path)
static KMETHOD System_isDir(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX];
	kString *path = sfp[1].asString;
	const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	struct stat buf;
	if(stat(systemPath, &buf) == 0) {
		KReturnUnboxValue(S_ISDIR(buf.st_mode));
	}
	KReturnUnboxValue(false);
}
Example #18
0
// boolean System.access(String path, int mode);
static KMETHOD System_access(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX];
	kString *path = sfp[1].asString;
	const char *systemPath = PLATAPI formatSystemPath(kctx, buffer, sizeof(buffer), S_text(path), S_size(path), trace);
	mode_t mode = (mode_t)sfp[2].intValue;
	int ret = access(systemPath, mode);
	if(ret == -1) {
		KTraceErrorPoint(trace, SystemFault, "access", LogFileName(S_text(path)), LogMode(mode), LogErrno);
	}
	KReturnUnboxValue(ret != -1);
}
Example #19
0
//## Connection Connection.new(String dburl);
static KMETHOD Connection_new(KonohaContext *kctx, KonohaStack *sfp)
{
	kConnection *con = (kConnection *) sfp[0].asObject;
	KMakeTrace(trace, sfp);
	const char  *dburl  = kString_text(sfp[1].asString);
	QueryDriver *driver = FindQueryDriverByScheme(kctx, dburl);
	DBHandler *db = driver->qopen(kctx, dburl, trace);
	if(db != NULL) {
		con->db     = db;
		con->driver = driver;
	}
	KReturn(con);
}
Example #20
0
//## ResultSet Connection.query(String query);
static KMETHOD Connection_query(KonohaContext *kctx, KonohaStack *sfp)
{
	INIT_GCSTACK();
	KMakeTrace(trace, sfp);
	kConnection *conn = (kConnection *)sfp[0].asObject;
	const char *query = kString_text(sfp[1].asString);
	kResultSet *rs = (kResultSet *)KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), (uintptr_t)conn);
	KCursor *qcur = conn->driver->qexec(kctx, conn->db, query, rs, trace);
	if(qcur != NULL) {
		rs->qcur   = qcur;
		rs->driver = conn->driver;
	}
	KReturnWith(rs, RESET_GCSTACK());
}
Example #21
0
//## String String.new(Bytes ba);
static KMETHOD String_new_fromBytes_withDefaultDecode(KonohaContext *kctx, KonohaStack *sfp)
{
	kBytes *ba = sfp[1].asBytes;
	kString *s = TS_EMPTY;
	if(ba->bytesize != 0) {
		KMakeTrace(trace, sfp);
		KBuffer wb;
		KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
		KBuffer_convertCharset(kctx, &wb, "UTF-8", I18NAPI systemCharset, ba->buf, ba->bytesize, trace);
		KLIB KBuffer_text(kctx, &wb, EnsureZero); /* String must be Null terminated */
		s = KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer);
	}
	KReturn(s);
}
Example #22
0
static KMETHOD System_lchown(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX];
	kString *path = sfp[1].asString;
	const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	uid_t owner = (uid_t)sfp[2].intValue;
	gid_t group = (gid_t)sfp[3].intValue;
	int ret = lchown(systemPath, owner, group);
	if(ret == -1) {
		KTraceErrorPoint(trace, SystemFault, "lchown", LogFileName(kString_text(path)), LogOwner(owner), LogGroup(group), LogErrno);
	}
	KReturnUnboxValue(ret != -1);
}
Example #23
0
//## String File.readLine();
static KMETHOD File_readLine(KonohaContext *kctx, KonohaStack *sfp)
{
	kFile *file = (kFile *)sfp[0].asObject;
	if(file->fp != NULL) {
		KBuffer wb;
		KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
		int ch, pos = 0, hasUTF8 = false, bufferCount = 0, policy = StringPolicy_ASCII;
		char buffer[K_PAGESIZE];
		KMakeTrace(trace, sfp);
		while((ch = TRACE_fgetc(kctx, file, trace)) != EOF) {
			//DBG_P("ch='%c', pos=%d", ch, pos);
			if(ch == '\r') continue;
			if(ch == '\n') {
				if(bufferCount == 0 && (!hasUTF8 || file->readerIconv == ICONV_NULL)) {
					KReturn(KLIB new_kString(kctx, OnStack, buffer, pos, policy));
				}
				break;
			}
			if(ch > 127) {
				hasUTF8 = true;
				policy = StringPolicy_UTF8;
			}
			buffer[pos] = ch; pos++;
			if(!(pos < K_PAGESIZE)) {
				if(hasUTF8 && file->readerIconv != ICONV_NULL) {
					KLIB KBuffer_iconv(kctx, &wb, file->readerIconv, buffer, pos, trace);
				}
				else {
					KLIB KBuffer_Write(kctx, &wb, buffer, pos);
				}
				bufferCount++;
				hasUTF8 = false;
				pos = 0;
			}
		}
		if(pos > 0) {
			if(hasUTF8 && file->readerIconv != ICONV_NULL) {
				KLIB KBuffer_iconv(kctx, &wb, file->readerIconv, buffer, pos, trace);
			}
			else {
				KLIB KBuffer_Write(kctx, &wb, buffer, pos);
			}
		}
		kFile_CheckEOF(kctx, file, trace);
		KReturn(KLIB KBuffer_Stringfy(kctx, &wb, OnStack, policy | StringPolicy_FreeKBuffer));
	}
	else {
		KReturn(KNULL(String));
	}
}
Example #24
0
//## Stat System.stat(String path)
static KMETHOD System_stat(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX];
	kString *path = sfp[1].asString;
	const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	struct stat buf = {}; /* zero */
	int ret = stat(systemPath, &buf);
	if(ret == -1) {
		int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace);
		KTraceErrorPoint(trace, fault, "stat", LogText("path", kString_text(path)), LogErrno);
	}
	KReturn(KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), (uintptr_t)&buf));
}
Example #25
0
//## String getcwd()
static KMETHOD System_getcwd(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char filepath[K_PATHMAX] = {0};
	char *cwd = getcwd(filepath, K_PATHMAX);
	if(cwd != NULL) {
		char buffer[K_PATHMAX] = {0};
		const char *konohaPath = PLATAPI I18NModule.formatKonohaPath(kctx, buffer, sizeof(buffer), cwd, strlen(cwd), trace);
		KReturn(KLIB new_kString(kctx, OnStack, konohaPath, strlen(konohaPath), 0));
	}
	else {
		KTraceErrorPoint(trace, SystemFault, "getcwd", LogErrno);
		KReturn(KNULL(String));
	}
}
Example #26
0
//## boolean System.rmdir(String path)
static KMETHOD System_rmdir(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX];
	kString *path = sfp[1].asString;
	const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	int ret = rmdir(systemPath);
	if(ret == -1) {
		int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace);
		KTraceErrorPoint(trace, fault, "rmdir", LogFileName(kString_text(path)), LogErrno);
	}
	else {
		KTraceChangeSystemPoint(trace, "rmdir", LogFileName(kString_text(path)));
	}
	KReturnUnboxValue(ret != -1);
}
Example #27
0
static KMETHOD ResultSet_next(KonohaContext *kctx, KonohaStack *sfp)
{
	kResultSet *rs = (kResultSet *) sfp[0].asObject;
	kbool_t ret = false;
	DBG_ASSERT(rs->qcur != NULL);
	KMakeTrace(trace, sfp);
	if(rs->driver->qcurnext(kctx, rs->qcur, rs, trace)) {
		rs->rowidx++;
		ret = true;
	}
	else {
		rs->driver->qcurfree(rs->qcur);
		rs->qcur = NULL;
	}
	KReturnUnboxValue(ret);
}
Example #28
0
//## boolean System.fchmod(int fd, int length)
static KMETHOD System_fchmod(KonohaContext *kctx, KonohaStack *sfp)
{
	int fd = sfp[1].intValue;
	int mode = sfp[2].intValue;
	int ret = fchmod(fd, mode);
	if(ret != 0) {
		// TODO: throw
		KMakeTrace(trace, sfp);
		int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace);
		KTraceErrorPoint(trace, fault, "fchmod",
						LogUint("fd", fd),
						LogUint("mode", mode)
			);
	}
	KReturnUnboxValue(ret == 0);
}
Example #29
0
//## String System.realpath(String path)
static KMETHOD System_realpath(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	char buffer[K_PATHMAX], filepath[K_PATHMAX] = {0};
	kString *path = sfp[1].asString;
	const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace);
	char *cwd = realpath(systemPath, filepath);
	if(cwd != NULL) {
		const char *konohaPath = PLATAPI I18NModule.formatKonohaPath(kctx, buffer, sizeof(buffer), cwd, strlen(cwd), trace);
		KReturn(KLIB new_kString(kctx, OnStack, konohaPath, strlen(konohaPath), 0));
	}
	else {
		KTraceErrorPoint(trace, SystemFault, "realpath", LogFileName(kString_text(path)), LogErrno);
		KReturn(KNULL(String));
	}
}
Example #30
0
//## String String.new(Bytes ba, String charset);
static KMETHOD String_new_fromBytes_withSpecifiedDecode(KonohaContext *kctx, KonohaStack *sfp)
{
	kBytes *ba = sfp[1].asBytes;
	kString *charset = sfp[2].asString;
	kString *s = TS_EMPTY;
	if(ba->bytesize != 0) {
		// At this point, we assuem 'ba' is null terminated.
		DBG_ASSERT(ba->buf[ba->bytesize] == '\0');
		KMakeTrace(trace, sfp);
		KBuffer wb;
		KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
		KBuffer_convertCharset(kctx, &wb, kString_text(charset), "UTF-8", ba->buf, ba->bytesize, trace);
		s = KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer);
	}
	KReturn(s);
}