//static kconn_t *MYSQL_qopen(CTX ctx, kbytes_t url) kconn_t *MYSQL_qopen(CTX ctx, kbytes_t url) { char *puser, user[MYSQL_USER_MAXLEN+1] = {0}; char *ppass, pass[MYSQL_PASS_MAXLEN+1] = {0}; // temporary defined char *phost, host[MYSQL_HOST_MAXLEN+1] = {0}; unsigned int port = 0; char *pdbnm, dbnm[MYSQL_DBNM_MAXLEN+1] = {0}; kbytes_t bt = knh_bytes_last(url, 8); // skip: mysql:// const char *btstr = bt.text; sscanf(btstr, "%16[^ :\r\n\t]:%255[^ @\r\n\t]@%255[^ :\r\n\t]:%5d/%64[^ \r\n\t]", (char*)&user, (char*)&pass, (char*)&host, &port, (char*)&dbnm); // consider to buffer over run puser = (user[0]) ? user : NULL; ppass = (pass[0]) ? pass : NULL; phost = (host[0]) ? host : NULL; pdbnm = (dbnm[0]) ? dbnm : NULL; MYSQL *db = mysql_init(NULL); KNH_NTRACE2(ctx, "mysql_init", (db != NULL) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("init", mysql_error(db)))); db = mysql_real_connect(db, phost, puser, ppass, pdbnm, port, NULL, 0); KNH_NTRACE2(ctx, "mysql_real_connect", (db != NULL) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("host", phost), LOG_s("user", puser), /* LOG_s("passwd", ppass), */ LOG_s("dbname", pdbnm), LOG_i("port", port), LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); //if (!mysql_real_connect(db, phost, puser, ppass, pdbnm, port, NULL, 0)) { // knh_mysql_perror(ctx, db, 0); // mysql_close(db); // db = NULL; //} return (kconn_t*)db; }
static kcontext_t* new_hcontext(CTX ctx0) { kcontext_t *ctx; static volatile size_t ctxid_counter = 0; if(ctx0 == NULL) { ctxid_counter = 0; ctx = (kcontext_t*)malloc(sizeof(kcontext_t)); knh_bzero(ctx, sizeof(kcontext_t)); } else { KNH_ASSERT_CTX0(ctx0); ctx = (kcontext_t*)KNH_MALLOC(ctx0, sizeof(kcontext_t)); knh_bzero(ctx, sizeof(kcontext_t)); } ctx->flag = 0; ctx->ctxid = ctxid_counter; ctxid_counter++; // ctx->freeObjectList = NULL; // ctx->freeMemoryList = NULL; ctx->parent = ctx; ctx->api2 = getapi2(); { kuintptr_t i = 0, ch; kuint_t t = knh_rand(); ch = t % 26; ctx->trace[i] = 'A' + ch; for(i = 1; i < 9; i++) { t = t / 36; if (t == 0) t = knh_rand(); ch = t % 36; ctx->trace[i] = (ch < 10) ? '0' + ch : 'A' + (ch - 10); } } ctx->seq = 0; ctx->ctxobjNC = NULL; if(ctx0 == NULL) { const char *ptrace = knh_getenv(K_DEOS_TRACE); if(ptrace == NULL) { ptrace = "$(setenv " K_DEOS_TRACE ")"; } KNH_NTRACE2(ctx, "konoha:newtrace", K_NOTICE, KNH_LDATA( LOG_s("parent", ptrace) #if defined(K_USING_POSIX_) , LOG_u("ppid", getppid()) #endif /* !defined(K_USING_POSIX_) */ )); } else { KNH_NTRACE2(ctx, "konoha:newtrace", K_NOTICE, KNH_LDATA(LOG_s("parent", ctx0->trace), LOG_u("seq", ctx0->seq))); } return ctx; }
//static kqcur_t *MYSQL_query(CTX ctx, kconn_t *hdr, kbytes_t sql, kResultSet *rs) kqcur_t *MYSQL_query(CTX ctx, kconn_t *hdr, kbytes_t sql, kResultSet *rs) { MYSQL_RES *res = NULL; MYSQL *db = (MYSQL*)hdr; if (db == NULL) { /* return NULL */ } else if (rs == NULL) { /* Connection.exec */ int r = mysql_query(db, sql.text); KNH_NTRACE2(ctx, "mysql_query", (r == 0) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("query", sql.text), LOG_i("errno", mysql_errno(db)))); //if(r > 0) { // knh_mysql_perror(ctx, (MYSQL*)db, r); //} } else { /* Connection.query */ int r = mysql_query((MYSQL*)db, sql.text); KNH_NTRACE2(ctx, "mysql_query", (r == 0) ? K_OK : K_FAILED, KNH_LDATA( LOG_s("query", sql.text), LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)) )); if (r == 0) { res = mysql_store_result((MYSQL*)db); if (res == NULL) { // NULL RESULT if (mysql_errno(db) != 0) { mysql_free_result(res); KNH_NTRACE2(ctx, "mysql_store_result", K_FAILED, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); } else { KNH_NTRACE2(ctx, "mysql_store_result", K_OK, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); } } else { knh_ResultSet_initColumn(ctx, rs, (size_t)mysql_num_fields(res)); KNH_NTRACE2(ctx, "mysql_store_result", K_OK, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); int i = 0; MYSQL_FIELD *field = NULL; while((field = mysql_fetch_field(res))) { DP(rs)->column[i].dbtype = field->type; kString *s = new_String(ctx, field->name); ResultSet_setName(ctx, rs, i, s); i++; } } } } return (kqcur_t *) res; }
void *knh_dlsym(CTX, void* handler, const char* symbol, const char *another, int isTest) { const char *func = __FUNCTION__, *emsg = NULL; void *p = NULL; #if defined(K_USING_WINDOWS_) func = "GetProcAddress"; p = GetProcAddress((HMODULE)handler, (LPCSTR)symbol); if(p == NULL && another != NULL) { symbol = another; p = GetProcAddress((HMODULE)handler, (LPCSTR)symbol); } return p; #elif defined(K_USING_POSIX_) func = "dlsym"; p = dlsym(handler, symbol); if(p == NULL && another != NULL) { symbol = another; p = dlsym(handler, symbol); } if(p == NULL) { emsg = dlerror(); } #else #endif if(!isTest) { int pe = (p == NULL) ? K_PERROR : K_OK; KNH_NTRACE2(_ctx, func, pe, KNH_LDATA(LOG_p("handler", handler), LOG_s("symbol", symbol))); } return p; }
static int open_socket(CTX ctx, ksfp_t *sfp, const char *ip_or_host, int port) { int sd = IO_NULL; struct in_addr addr = {0}; struct sockaddr_in server = {0}; const char *errfunc = NULL; if ((addr.s_addr = inet_addr(ip_or_host)) == -1) { struct hostent *host = gethostbyname(ip_or_host); if (host == NULL) { errfunc = "gethostname"; goto L_PERROR; } memcpy(&addr, (struct in_addr *)*host->h_addr_list, sizeof(struct in_addr)); } server.sin_family = AF_INET; server.sin_addr = addr; server.sin_port = htons(port); if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { errfunc = "socket"; goto L_PERROR; } if (connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1) { errfunc = "connect"; close(sd); goto L_PERROR; } L_PERROR:; if (errfunc != NULL) { KNH_NTHROW2(ctx, sfp, "Socket!!", errfunc, K_PERROR, KNH_LDATA(LOG_s("host", ip_or_host), LOG_i("port", port), LOG_sfp)); sd = IO_NULL; } else { KNH_NTRACE2(ctx, "connect", K_OK, KNH_LDATA(LOG_s("host", ip_or_host), LOG_i("port", port), LOG_sfp)); } return (int) sd; }
void THROW_ParamTypeError(CTX ctx, ksfp_t *sfp, size_t n, kmethodn_t mn, kclass_t reqt, kclass_t cid) { CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf); char msg[256], mname[256]; knh_printf(ctx, cwb->w, "Script!!: Type Error: %T.%M(#%d)", cid, mn, (int)n); knh_snprintf(msg, sizeof(msg), "%s", CWB_totext(ctx, cwb)); CWB_close(ctx, cwb); knh_printf(ctx, cwb->w, "%C.%M", cid, mn); knh_snprintf(mname, sizeof(mname), "%s", CWB_totext(ctx, cwb)); CWB_close(ctx, cwb); KNH_NTHROW2(ctx, sfp, msg, "konoha:type", K_FAILED, KNH_LDATA(LOG_msg(msg), LOG_s("method", mname), LOG_i("argument", n), LOG_t("requested_type", reqt), LOG_t("given_type", cid))); }
void THROW_NoSuchMethod(CTX ctx, ksfp_t *sfp, kclass_t cid, kmethodn_t mn) { CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf); char msg[256], mname[256]; knh_printf(ctx, cwb->w, "Script!!: No Such Method: %T.%M", cid, mn); knh_snprintf(msg, sizeof(msg), "%s", CWB_totext(ctx, cwb)); CWB_close(ctx, cwb); knh_printf(ctx, cwb->w, "%C.%M", cid, mn); knh_snprintf(mname, sizeof(mname), "%s", CWB_totext(ctx, cwb)); CWB_close(ctx, cwb); KNH_NTHROW2(ctx, sfp, msg, "konoha:type", K_FAILED, KNH_LDATA(LOG_msg(msg), LOG_s("method", mname))); }
kfileid_t knh_getURI(CTX, kbytes_t t) { OLD_LOCK(_ctx, LOCK_SYSTBL, NULL); kindex_t idx = knh_DictSet_index(_ctx->share->urnDictSet, t); if(idx == -1) { kString *s = new_kString(t.text, t.len, SPOL_POOL); idx = kArray_size(_ctx->share->urns); knh_DictSet_set(_ctx, ctx->share->urnDictSet, s, idx); kArray_add(ctx->share->urns, s); KNH_NTRACE2(_ctx, "konoha:newfileid", K_OK, KNH_LDATA(LOG_s("urn", S_text(s)), LOG_i("fileid", idx))); } else { idx = knh_DictSet_valueAt(_ctx->share->urnDictSet, idx); } OLD_UNLOCK(_ctx, LOCK_SYSTBL, NULL); return (kfileid_t)idx; }
kuri_t knh_getURI(CTX ctx, kbytes_t t) { OLD_LOCK(ctx, LOCK_SYSTBL, NULL); kindex_t idx = knh_DictSet_index(ctx->share->urnDictSet, t); if(idx == -1) { kString *s = new_String2(ctx, CLASS_String, t.text, t.len, SPOL_POOLALWAYS); idx = knh_Array_size(ctx->share->urns); knh_DictSet_set(ctx, ctx->share->urnDictSet, s, idx); knh_Array_add(ctx, ctx->share->urns, s); KNH_NTRACE2(ctx, "konoha:newuri", K_OK, KNH_LDATA(LOG_s("urn", S_totext(s)), LOG_i("uri", idx))); } else { idx = knh_DictSet_valueAt(ctx->share->urnDictSet, idx); } OLD_UNLOCK(ctx, LOCK_SYSTBL, NULL); return (kuri_t)idx; }
void *knh_dlopen(CTX, const char* path) { const char *func = __FUNCTION__; void *handler = NULL; #if defined(K_USING_WINDOWS_) func = "LoadLibrary"; handler = (void*)LoadLibraryA((LPCTSTR)path); #elif defined(K_USING_POSIX_) func = "dlopen"; handler = dlopen(path, RTLD_LAZY); #else #endif int pe = (handler == NULL) ? K_PERROR : K_OK; { KNH_NTRACE2(_ctx, func, pe, KNH_LDATA(LOG_s("path", path), LOG_p("handler", handler))); } return handler; }
}; DEFAPI(void) constXmlReader(CTX ctx, kclass_t cid, const knh_LoaderAPI_t *kapi) { kapi->loadClassIntConst(ctx, cid, XmlReaderConstint); } //## @Native @Throwable XmlReader XmlReader.new(Path _); KMETHOD XmlReader_new(CTX ctx, ksfp_t *sfp _RIX) { kPath *pth = sfp[1].pth; xmlTextReaderPtr reader = xmlNewTextReaderFilename(pth->ospath); kRawPtr *po = new_ReturnCppObject(ctx, sfp, reader, NULL); if(reader == NULL) { KNH_NTHROW2(ctx, sfp, "IO!!", "xmlNewTextReader", K_FAILED, KNH_LDATA(LOG_s("urn", S_totext(pth->urn)), LOG_s("ospath", pth->ospath))); } RETURN_(po); } //## @Native void XmlReader.close(); KMETHOD XmlReader_close(CTX ctx, ksfp_t *sfp _RIX) { xmlTextReaderPtr reader = (xmlTextReaderPtr)(sfp[0].p)->rawptr; if(reader != NULL) { xmlTextReaderClose(reader); } RETURNvoid_(); } //## @Native String XmlReader.getQuoteChar();