static kOutputStream *new_OutputStreamStdOut(CTX, kString *enc) { kio_t *io2 = new_io2_stdio(_ctx, 1, 4096); kOutputStream *w = new_OutputStream(_ctx, io2, new_Path(_ctx, TS_DEVSTDOUT)); OutputStream_setAutoFlush(w, 1); return w; }
IPath* Path_GetCurrentDirectory() { IPath* path; char* current_dir = (char*) CRuntime_calloc( 1024, sizeof( char ) ); current_dir = getcwd( current_dir, 1024 ); path = new_Path( current_dir ); CRuntime_free( current_dir ); return path; }
char* retrieveTargetDirectory( const Arguments* args, const char* default_dir ) { char* target = NULL; const char* tmp; if ( NULL != (tmp = Arguments_getTargetDir( args ) ) ) { IPath* target_path = new_Path( tmp ); target = CharString_copy( Path_getCommon( target_path ) ); free_Path( target_path ); } else { target = CharString_copy( default_dir ); } return target; }
char* retrievePackageDirectory( const Arguments* args ) { const char* tmp_dir; char* pkg_dir; if ( NULL != ( tmp_dir = Arguments_getPackageDir( args ) ) ) { IPath* p = new_Path( tmp_dir ); pkg_dir = CharString_copy( Path_getCommon( p ) ); free_Path( p ); } else { IPath* current_path = Path_GetCurrentDirectory(); pkg_dir = CharString_copy( Path_getCommon( current_path ) ); free_Path( current_path ); } return pkg_dir; }
static kOutputStream *new_OutputStreamStdErr(CTX, kString *enc) { kio_t *io2 = new_io2_stdio(_ctx, 2, 0); return new_OutputStream(_ctx, io2, new_Path(_ctx, TS_DEVSTDERR)); }
static kInputStream *new_InputStreamStdIn(CTX, kString *enc) { kio_t *io2 = new_io2_stdio(_ctx, 0, 0); return new_InputStream(_ctx, io2, new_Path(_ctx, TS_DEVSTDIN)); }
Path* Path_copy( const Path* self ) { return (Path*) new_Path( self->original ); }
static int private_compile( const void* buildParameters, const void* providerContext, const void* targetIDirectory ) { bool status = 1; const BuildParameters* parameters = (BuildParameters*) buildParameters; const ProviderContext* context = (ProviderContext*) providerContext; const IDirectory* target = (IDirectory*) targetIDirectory; //IList* cppflags = translateArguments( context->FULL_CPPFLAGS ); unsigned int i; unsigned int count = List_count( context->sourceFiles ); // fprintf( stdout, "ISLabs::linux-gnu::gnu::compile( %s )\n", Path_getCondensed( Directory_getRealPath( target ) ) ); for ( i = 0; (i < count) && status; i++ ) { // cc $CPPFLAGS $LANGFLAGS $CFLAGS $SRC_FILE -o $OUTPUT IPath* p = new_Path( context->sourceFiles->items[i] ); char* target_file = CharString_cat2( Path_getBasename( p ), ".obj" ); char* target_path = CharString_cat3( Path_getCondensed( Directory_getRealPath( target ) ), "/obj/", target_file ); Command* command; IList* arguments = new_List(); IList* native_arguments; // Compiler List_copyItem( arguments, context->compiler ); // C-preprocessor flags addFlags( arguments, "-I", context->includeDirs ); List_addList( arguments, BuildParameters_getGlobal( parameters )->CPPFLAGS ); // C flags List_copyItem( arguments, "-Wall" ); List_copyItem( arguments, "-fPIC" ); List_copyItem( arguments, "-c" ); if ( BuildParameters_isRelease( parameters ) ) { //List_copyItem( arguments, "-O2" ); } else { List_copyItem( arguments, "-g" ); } List_addList( arguments, BuildParameters_getGlobal( parameters )->CFLAGS ); // Source files List_copyItem( arguments, context->sourceFiles->items[i] ); // Output files List_copyItem( arguments, "-o" ); List_copyItem( arguments, target_path ); native_arguments = Path_ConvertListToNative( arguments ); command = new_Command( context->compiler, (const char**) native_arguments->items ); Command_print( command, stderr ); // if ( !BuildParameters_isNo( parameters ) && Command_run( command ) && Command_waitFor( command ) ) // { // status &= Command_getResult( command ); // } else { // fprintf( stderr, "failed: " ); // Command_print( command, stderr ); // } free_Command( command ); free_CharString( target_file ); free_Path( p ); } return status; }
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); }
/* 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); } }