|
|
|
@ -26,7 +26,6 @@
|
|
|
|
#define push_error(L) windows_pushlasterror(L)
|
|
|
|
#define push_error(L) windows_pushlasterror(L)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* name -- value/nil */
|
|
|
|
/* name -- value/nil */
|
|
|
|
static int ex_getenv(lua_State *L)
|
|
|
|
static int ex_getenv(lua_State *L)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -73,15 +72,16 @@ static int ex_environ(lua_State *L)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* seconds -- */
|
|
|
|
/* -- pathname/nil error */
|
|
|
|
static int ex_sleep(lua_State *L)
|
|
|
|
static int ex_currentdir(lua_State *L)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lua_Number seconds = luaL_checknumber(L, 1);
|
|
|
|
char pathname[MAX_PATH + 1];
|
|
|
|
Sleep(1e3 * seconds);
|
|
|
|
size_t len = GetCurrentDirectory(sizeof pathname, pathname);
|
|
|
|
return 0;
|
|
|
|
if (len == 0) return push_error(L);
|
|
|
|
|
|
|
|
lua_pushlstring(L, pathname, len);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* pathname -- true/nil error */
|
|
|
|
/* pathname -- true/nil error */
|
|
|
|
static int ex_chdir(lua_State *L)
|
|
|
|
static int ex_chdir(lua_State *L)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -102,16 +102,6 @@ static int ex_mkdir(lua_State *L)
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -- pathname/nil error */
|
|
|
|
|
|
|
|
static int ex_currentdir(lua_State *L)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
char pathname[MAX_PATH + 1];
|
|
|
|
|
|
|
|
size_t len = GetCurrentDirectory(sizeof pathname, pathname);
|
|
|
|
|
|
|
|
if (len == 0) return push_error(L);
|
|
|
|
|
|
|
|
lua_pushlstring(L, pathname, len);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* pathname -- true/nil error */
|
|
|
|
/* pathname -- true/nil error */
|
|
|
|
static int ex_remove(lua_State *L)
|
|
|
|
static int ex_remove(lua_State *L)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -254,8 +244,10 @@ static int diriter_setpathname(lua_State *L, int index)
|
|
|
|
static int diriter_close(lua_State *L)
|
|
|
|
static int diriter_close(lua_State *L)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
DIR **pd = lua_touserdata(L, 1);
|
|
|
|
DIR **pd = lua_touserdata(L, 1);
|
|
|
|
closedir(*pd);
|
|
|
|
if (*pd) {
|
|
|
|
*pd = 0;
|
|
|
|
closedir(*pd);
|
|
|
|
|
|
|
|
*pd = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushnil(L);
|
|
|
|
diriter_setpathname(L, 1);
|
|
|
|
diriter_setpathname(L, 1);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
@ -363,6 +355,17 @@ static int ex_pipe(lua_State *L)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* seconds --
|
|
|
|
|
|
|
|
* interval units -- */
|
|
|
|
|
|
|
|
static int ex_sleep(lua_State *L)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lua_Number interval = luaL_checknumber(L, 1);
|
|
|
|
|
|
|
|
lua_Number units = luaL_optnumber(L, 2, 1);
|
|
|
|
|
|
|
|
Sleep(1e3 * interval / units);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void get_redirect(lua_State *L, int idx, const char *stdname, struct spawn_params *p)
|
|
|
|
static void get_redirect(lua_State *L, int idx, const char *stdname, struct spawn_params *p)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lua_getfield(L, idx, stdname);
|
|
|
|
lua_getfield(L, idx, stdname);
|
|
|
|
@ -451,8 +454,9 @@ static int ex_spawn(lua_State *L)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* copy the fields given in 'l' from one table to another; insert missing fields */
|
|
|
|
/* register functions from 'lib' in table 'to' by copying existing
|
|
|
|
static void copy_fields(lua_State *L, const luaL_reg *l, int from, int to)
|
|
|
|
* closures from table 'from' or by creating new closures */
|
|
|
|
|
|
|
|
static void copyfields(lua_State *L, const luaL_reg *l, int from, int to)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (to = absindex(L, to); l->name; l++) {
|
|
|
|
for (to = absindex(L, to); l->name; l++) {
|
|
|
|
lua_getfield(L, from, l->name);
|
|
|
|
lua_getfield(L, from, l->name);
|
|
|
|
@ -468,8 +472,7 @@ int luaopen_ex(lua_State *L)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const luaL_reg ex_iolib[] = {
|
|
|
|
const luaL_reg ex_iolib[] = {
|
|
|
|
{"pipe", ex_pipe},
|
|
|
|
{"pipe", ex_pipe},
|
|
|
|
{0,0} };
|
|
|
|
#define ex_iofile_methods (ex_iolib + 1)
|
|
|
|
const luaL_reg ex_iofile_methods[] = {
|
|
|
|
|
|
|
|
{"lock", ex_lock},
|
|
|
|
{"lock", ex_lock},
|
|
|
|
{"unlock", ex_unlock},
|
|
|
|
{"unlock", ex_unlock},
|
|
|
|
{0,0} };
|
|
|
|
{0,0} };
|
|
|
|
@ -478,63 +481,63 @@ int luaopen_ex(lua_State *L)
|
|
|
|
{"setenv", ex_setenv},
|
|
|
|
{"setenv", ex_setenv},
|
|
|
|
{"environ", ex_environ},
|
|
|
|
{"environ", ex_environ},
|
|
|
|
|
|
|
|
|
|
|
|
{"sleep", ex_sleep},
|
|
|
|
{"currentdir", ex_currentdir},
|
|
|
|
|
|
|
|
|
|
|
|
{"chdir", ex_chdir},
|
|
|
|
{"chdir", ex_chdir},
|
|
|
|
{"mkdir", ex_mkdir},
|
|
|
|
{"mkdir", ex_mkdir},
|
|
|
|
{"currentdir", ex_currentdir},
|
|
|
|
|
|
|
|
{"remove", ex_remove},
|
|
|
|
{"remove", ex_remove},
|
|
|
|
|
|
|
|
|
|
|
|
{"dir", ex_dir},
|
|
|
|
{"dir", ex_dir},
|
|
|
|
{"dirent", ex_dirent},
|
|
|
|
{"dirent", ex_dirent},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{"sleep", ex_sleep},
|
|
|
|
{"spawn", ex_spawn},
|
|
|
|
{"spawn", ex_spawn},
|
|
|
|
{0,0} };
|
|
|
|
{0,0} };
|
|
|
|
const luaL_reg ex_diriter_methods[] = {
|
|
|
|
const luaL_reg ex_diriter_methods[] = {
|
|
|
|
{"__gc", diriter_close},
|
|
|
|
{"__gc", diriter_close},
|
|
|
|
/* {"__tostring", diriter_tostring}, */
|
|
|
|
|
|
|
|
{0,0} };
|
|
|
|
{0,0} };
|
|
|
|
const luaL_reg ex_process_methods[] = {
|
|
|
|
const luaL_reg ex_process_methods[] = {
|
|
|
|
{"__tostring", process_tostring},
|
|
|
|
{"__tostring", process_tostring},
|
|
|
|
|
|
|
|
#define ex_process_functions (ex_process_methods + 1)
|
|
|
|
{"wait", process_wait},
|
|
|
|
{"wait", process_wait},
|
|
|
|
{0,0} };
|
|
|
|
{0,0} };
|
|
|
|
|
|
|
|
|
|
|
|
/* Make all functions available via ex. namespace */
|
|
|
|
int ex;
|
|
|
|
luaL_register(L, "ex", ex_iolib); /* . ex */
|
|
|
|
const char *name = lua_tostring(L, 1);
|
|
|
|
luaL_register(L, 0, ex_oslib);
|
|
|
|
|
|
|
|
luaL_register(L, 0, ex_iofile_methods);
|
|
|
|
/* diriter metatable */
|
|
|
|
luaL_register(L, 0, ex_process_methods + 1); /* XXX don't insert __tostring */
|
|
|
|
luaL_newmetatable(L, DIR_HANDLE); /* . D */
|
|
|
|
lua_replace(L, 1); /* ex . */
|
|
|
|
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 */
|
|
|
|
/* extend the os table */
|
|
|
|
lua_getglobal(L, "os"); /* ex . os */
|
|
|
|
lua_getglobal(L, "os"); /* . os */
|
|
|
|
if (lua_isnil(L, -1)) return luaL_error(L, "os not loaded");
|
|
|
|
if (lua_isnil(L, -1)) return luaL_error(L, "os not loaded");
|
|
|
|
copy_fields(L, ex_oslib, 1, -1); /* ex . os */
|
|
|
|
copyfields(L, ex_oslib, ex, -1);
|
|
|
|
|
|
|
|
|
|
|
|
/* extend the io table */
|
|
|
|
/* extend the io table */
|
|
|
|
lua_getglobal(L, "io"); /* ex . io */
|
|
|
|
lua_getglobal(L, "io"); /* . io */
|
|
|
|
if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded");
|
|
|
|
if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded");
|
|
|
|
copy_fields(L, ex_iolib, 1, -1); /* ex . io */
|
|
|
|
copyfields(L, ex_iolib, ex, -1);
|
|
|
|
copy_fields(L, ex_iofile_methods, 1, -1); /* ex . io */
|
|
|
|
lua_getfield(L, ex, "pipe"); /* . io ex_pipe */
|
|
|
|
lua_getfield(L, 1, "pipe"); /* ex . io ex_pipe */
|
|
|
|
lua_getfield(L, -2, "stderr"); /* . io ex_pipe io_stderr */
|
|
|
|
lua_getfield(L, -2, "stderr"); /* ex . io ex_pipe io_stderr */
|
|
|
|
lua_getfenv(L, -1); /* . io ex_pipe io_stderr E */
|
|
|
|
lua_getfenv(L, -1); /* ex . io ex_pipe io_stderr E */
|
|
|
|
lua_setfenv(L, -3); /* . io ex_pipe io_stderr */
|
|
|
|
lua_setfenv(L, -3); /* ex . io ex_pipe io_stderr */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* extend the io.file metatable */
|
|
|
|
/* extend the io.file metatable */
|
|
|
|
luaL_getmetatable(L, LUA_FILEHANDLE); /* ex . F */
|
|
|
|
luaL_getmetatable(L, LUA_FILEHANDLE); /* . F */
|
|
|
|
if (lua_isnil(L, -1)) return luaL_error(L, "can't find FILE* metatable");
|
|
|
|
if (lua_isnil(L, -1)) return luaL_error(L, "can't find FILE* metatable");
|
|
|
|
copy_fields(L, ex_iofile_methods, 1, -1); /* ex . F */
|
|
|
|
copyfields(L, ex_iofile_methods, ex, -1);
|
|
|
|
|
|
|
|
|
|
|
|
/* diriter metatable */
|
|
|
|
|
|
|
|
luaL_newmetatable(L, DIR_HANDLE); /* ex . D */
|
|
|
|
|
|
|
|
luaL_register(L, 0, ex_diriter_methods); /* ex . D */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* proc metatable */
|
|
|
|
|
|
|
|
luaL_newmetatable(L, PROCESS_HANDLE); /* ex . P */
|
|
|
|
|
|
|
|
copy_fields(L, ex_process_methods, 1, -1); /* ex . P */
|
|
|
|
|
|
|
|
lua_setfield(L, -1, "__index"); /* ex . P */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lua_settop(L, 1); /* ex */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|