LUALIB_API int luaopen_io (lua_State *L) { createmeta(L); #if LUA_OPTIMIZE_MEMORY != 2 /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ newfenv(L, io_fclose); lua_replace(L, LUA_ENVIRONINDEX); /* open library */ luaL_register(L, LUA_IOLIBNAME, iolib); newfenv(L, io_noclose); /* close function for default files */ #else luaL_register_light(L, LUA_IOLIBNAME, iolib); lua_pushvalue(L, -1); lua_setmetatable(L, -2); #endif /* create (and set) default files */ createstdfile(L, stdin, IO_INPUT, "stdin"); createstdfile(L, stdout, IO_OUTPUT, "stdout"); createstdfile(L, stderr, IO_STDERR, "stderr"); #if LUA_OPTIMIZE_MEMORY != 2 lua_pop(L, 1); /* pop environment for default files */ lua_getfield(L, -1, "popen"); newfenv(L, io_pclose); /* create environment for 'popen' */ lua_setfenv(L, -2); /* set fenv for 'popen' */ lua_pop(L, 1); /* pop 'popen' */ #endif return 1; }
LUALIB_API int luaopen_io (lua_State *L) { createmeta(L); /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ newfenv(L, io_fclose); lua_replace(L, LUA_ENVIRONINDEX); /* open library */ luaL_register(L, LUA_IOLIBNAME, iolib); /* create (and set) default files */ newfenv(L, io_noclose); /* close function for default files */ createstdfile(L, stdin, IO_INPUT, "stdin"); createstdfile(L, stdout, IO_OUTPUT, "stdout"); createstdfile(L, stderr, 0, "stderr"); lua_pop(L, 1); /* pop environment for default files */ lua_getfield(L, -1, "popen"); newfenv(L, io_pclose); /* create environment for 'popen' */ lua_setfenv(L, -2); /* set fenv for 'popen' */ lua_pop(L, 1); /* pop 'popen' */ return 1; }
int luaopen_osprocess_core(lua_State *L) { const char *name = lua_tostring(L, 1); int ex; const luaL_Reg osprocess_lib[] = { {"pipe", ex_pipe}, /* environment */ {"getenv", ex_getenv}, {"setenv", ex_setenv}, {"environ", ex_environ}, /* process control */ {"sleep", ex_sleep}, {"spawn", ex_spawn}, {0,0} }; const luaL_Reg ex_process_methods[] = { {"__tostring", process_tostring}, #define ex_process_functions (ex_process_methods + 1) {"wait", process_wait}, {0,0} }; /* proc metatable */ luaL_newmetatable(L, PROCESS_HANDLE); /* . P */ luaL_register(L, 0, ex_process_methods); /* . P */ lua_pushvalue(L, -1); /* . P P */ lua_setfield(L, -2, "__index"); /* . P */ /* make all functions available via ex.process namespace */ lua_newtable(L); luaL_register(L, 0, osprocess_lib); /* . P ex */ ex = lua_gettop(L); #if LUA_VERSION_NUM <= 501 lua_getfield(L, ex, "pipe"); /* . io ex_pipe */ newfenv(L, pipe_close); /* create environment for 'popen' */ lua_setfenv(L, -2); /* set fenv for 'popen' */ lua_pop(L, 1); /* pop 'popen' */ #endif return 1; }
LUAMODULE_API int luaopen_ex_core(lua_State *L) { const char *name = lua_tostring(L, 1); int ex; const luaL_reg ex_iolib[] = { {"pipe", ex_pipe}, #define ex_iofile_methods (ex_iolib + 1) {"lock", ex_lock}, {"unlock", ex_lock}, {0,0} }; const luaL_reg ex_oslib[] = { /* environment */ {"getenv", ex_getenv}, {"setenv", ex_setenv}, {"environ", ex_environ}, /* file system */ {"access", ex_access}, {"getcwd", ex_getcwd}, {"chdir", ex_chdir}, {"chmod", ex_chmod}, {"mkdir", ex_mkdir}, {"remove", ex_remove}, {"dir", ex_dir}, {"dirent", ex_dirent}, {"copyfile", ex_copyfile}, {"movefile", ex_movefile}, {"touch", ex_touch}, {"stdin_binary", ex_stdin_binary}, {"stdout_binary", ex_stdout_binary}, /* process control */ {"sleep", ex_sleep}, {"spawn", ex_spawn}, {0,0} }; const luaL_reg ex_diriter_methods[] = { {"__gc", diriter_close}, {0,0} }; const luaL_reg ex_process_methods[] = { {"__tostring", process_tostring}, #define ex_process_functions (ex_process_methods + 1) {"wait", process_wait}, {"__gc", process_close}, {0,0} }; /* diriter metatable */ luaL_newmetatable(L, DIR_HANDLE); /* . D */ luaL_register(L, 0, ex_diriter_methods); /* . D */ /* proc metatable */ luaL_newmetatable(L, PROCESS_HANDLE); /* . P */ luaL_register(L, 0, ex_process_methods); /* . P */ lua_pushvalue(L, -1); /* . P P */ lua_setfield(L, -2, "__index"); /* . P */ /* make all functions available via ex. namespace */ luaL_register(L, name, ex_oslib); /* . P ex */ luaL_register(L, 0, ex_iolib); copyfields(L, ex_process_functions, -2, -1); ex = lua_gettop(L); /* extend the os table */ lua_getglobal(L, "os"); /* . os */ if (lua_isnil(L, -1)) return luaL_error(L, "os not loaded"); copyfields(L, ex_oslib, ex, -1); luaopen_os_path(L); /* extend the io table */ lua_getglobal(L, "io"); /* . io */ if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded"); copyfields(L, ex_iolib, ex, -1); // copyfields(L, ex_iolib, ex, -1); lua_getfield(L, ex, "pipe"); /* . io ex_pipe */ newfenv(L, pipe_close); /* create environment for 'popen' */ lua_setfenv(L, -2); /* set fenv for 'popen' */ lua_pop(L, 1); /* pop 'popen' */ /* extend the io.file metatable */ luaL_getmetatable(L, LUA_FILEHANDLE); /* . F */ if (lua_isnil(L, -1)) return luaL_error(L, "can't find FILE* metatable"); copyfields(L, ex_iofile_methods, ex, -1); luaopen_windows_hkey(L); return 1; }