|
|
|
|
@ -149,7 +149,7 @@ static lua_Number qword_to_number(DWORD hi, DWORD lo)
|
|
|
|
|
/* lua_Number must be floating-point or as large or larger than
|
|
|
|
|
* two DWORDs in order to be considered adequate for representing
|
|
|
|
|
* large file sizes */
|
|
|
|
|
assert( hi == 0
|
|
|
|
|
assert(hi == 0
|
|
|
|
|
|| (lua_Number)0.5 > 0
|
|
|
|
|
|| sizeof(lua_Number) > 2 * sizeof(DWORD)
|
|
|
|
|
|| !"lua_Number cannot adequately represent large file sizes" );
|
|
|
|
|
@ -158,7 +158,8 @@ static lua_Number qword_to_number(DWORD hi, DWORD lo)
|
|
|
|
|
|
|
|
|
|
static lua_Number get_file_size(const char *name)
|
|
|
|
|
{
|
|
|
|
|
HANDLE h = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
|
|
|
|
|
HANDLE h = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, 0,
|
|
|
|
|
OPEN_EXISTING, 0, 0);
|
|
|
|
|
DWORD lo, hi;
|
|
|
|
|
lua_Number size;
|
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
|
@ -289,13 +290,13 @@ static int ex_dir(lua_State *L)
|
|
|
|
|
case LUA_TUSERDATA:
|
|
|
|
|
pd = luaL_checkudata(L, 1, DIR_HANDLE);
|
|
|
|
|
do d = readdir(*pd);
|
|
|
|
|
while (d && isdotfile(d->cFileName));
|
|
|
|
|
while (d && isdotfile(d->cFileName)) continue;
|
|
|
|
|
if (!d) return push_error(L);
|
|
|
|
|
new_dirent(L); /* diriter ... entry */
|
|
|
|
|
diriter_getpathname(L, 1); /* diriter ... entry dirpath */
|
|
|
|
|
lua_pushstring(L, d->cFileName); /* diriter ... entry dirpath name */
|
|
|
|
|
lua_pushvalue(L, -1); /* diriter ... entry dirpath name name */
|
|
|
|
|
lua_setfield(L, -4, "name"); /* diriter ... entry dirpath name */
|
|
|
|
|
diriter_getpathname(L, 1); /* diriter ... entry dir */
|
|
|
|
|
lua_pushstring(L, d->cFileName); /* diriter ... entry dir name */
|
|
|
|
|
lua_pushvalue(L, -1); /* diriter ... entry dir name name */
|
|
|
|
|
lua_setfield(L, -4, "name"); /* diriter ... entry dir name */
|
|
|
|
|
lua_concat(L, 2); /* diriter ... entry fullpath */
|
|
|
|
|
lua_replace(L, 1); /* fullpath ... entry */
|
|
|
|
|
lua_replace(L, 2); /* fullpath entry ... */
|
|
|
|
|
@ -305,7 +306,8 @@ static int ex_dir(lua_State *L)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int file_lock(lua_State *L, FILE *f, const char *mode, long offset, long length)
|
|
|
|
|
static int file_lock(lua_State *L,
|
|
|
|
|
FILE *f, const char *mode, long offset, long length)
|
|
|
|
|
{
|
|
|
|
|
HANDLE h = file_handle(f);
|
|
|
|
|
DWORD flags;
|
|
|
|
|
@ -373,7 +375,8 @@ static int ex_sleep(lua_State *L)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
if (!lua_isnil(L, -1))
|
|
|
|
|
@ -387,7 +390,6 @@ static int ex_spawn(lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
struct spawn_params *params;
|
|
|
|
|
int have_options;
|
|
|
|
|
|
|
|
|
|
switch (lua_type(L, 1)) {
|
|
|
|
|
default: return luaL_typerror(L, 1, "string or table");
|
|
|
|
|
case LUA_TSTRING:
|
|
|
|
|
@ -420,17 +422,15 @@ static int ex_spawn(lua_State *L)
|
|
|
|
|
luaL_typename(L, 1));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params = spawn_param_init(L);
|
|
|
|
|
|
|
|
|
|
/* get filename to execute */
|
|
|
|
|
spawn_param_filename(params, lua_tostring(L, 1));
|
|
|
|
|
|
|
|
|
|
/* get arguments, environment, and redirections */
|
|
|
|
|
if (have_options) {
|
|
|
|
|
lua_getfield(L, 2, "args"); /* cmd opts ... argtab */
|
|
|
|
|
switch (lua_type(L, -1)) {
|
|
|
|
|
default: return luaL_error(L, "bad args option (table expected, got %s)",
|
|
|
|
|
default:
|
|
|
|
|
return luaL_error(L, "bad args option (table expected, got %s)",
|
|
|
|
|
luaL_typename(L, -1));
|
|
|
|
|
case LUA_TNIL:
|
|
|
|
|
lua_pop(L, 1); /* cmd opts ... */
|
|
|
|
|
@ -438,13 +438,15 @@ static int ex_spawn(lua_State *L)
|
|
|
|
|
if (0) /*FALLTHRU*/
|
|
|
|
|
case LUA_TTABLE:
|
|
|
|
|
if (lua_objlen(L, 2) > 0)
|
|
|
|
|
return luaL_error(L, "cannot specify both the args option and array values");
|
|
|
|
|
return
|
|
|
|
|
luaL_error(L, "cannot specify both the args option and array values");
|
|
|
|
|
spawn_param_args(params); /* cmd opts ... */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
lua_getfield(L, 2, "env"); /* cmd opts ... envtab */
|
|
|
|
|
switch (lua_type(L, -1)) {
|
|
|
|
|
default: return luaL_error(L, "bad env option (table expected, got %s)",
|
|
|
|
|
default:
|
|
|
|
|
return luaL_error(L, "bad env option (table expected, got %s)",
|
|
|
|
|
luaL_typename(L, -1));
|
|
|
|
|
case LUA_TNIL:
|
|
|
|
|
break;
|
|
|
|
|
@ -456,7 +458,6 @@ static int ex_spawn(lua_State *L)
|
|
|
|
|
get_redirect(L, 2, "stdout", params); /* cmd opts ... */
|
|
|
|
|
get_redirect(L, 2, "stderr", params); /* cmd opts ... */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return spawn_param_execute(params); /* proc/nil error */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -477,6 +478,8 @@ static void copyfields(lua_State *L, const luaL_reg *l, int from, int to)
|
|
|
|
|
|
|
|
|
|
int luaopen_ex(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)
|
|
|
|
|
@ -484,18 +487,18 @@ int luaopen_ex(lua_State *L)
|
|
|
|
|
{"unlock", ex_unlock},
|
|
|
|
|
{0,0} };
|
|
|
|
|
const luaL_reg ex_oslib[] = {
|
|
|
|
|
/* environment */
|
|
|
|
|
{"getenv", ex_getenv},
|
|
|
|
|
{"setenv", ex_setenv},
|
|
|
|
|
{"environ", ex_environ},
|
|
|
|
|
|
|
|
|
|
/* file system */
|
|
|
|
|
{"currentdir", ex_currentdir},
|
|
|
|
|
{"chdir", ex_chdir},
|
|
|
|
|
{"mkdir", ex_mkdir},
|
|
|
|
|
{"remove", ex_remove},
|
|
|
|
|
|
|
|
|
|
{"dir", ex_dir},
|
|
|
|
|
{"dirent", ex_dirent},
|
|
|
|
|
|
|
|
|
|
/* process control */
|
|
|
|
|
{"sleep", ex_sleep},
|
|
|
|
|
{"spawn", ex_spawn},
|
|
|
|
|
{0,0} };
|
|
|
|
|
@ -507,31 +510,23 @@ int luaopen_ex(lua_State *L)
|
|
|
|
|
#define ex_process_functions (ex_process_methods + 1)
|
|
|
|
|
{"wait", process_wait},
|
|
|
|
|
{0,0} };
|
|
|
|
|
|
|
|
|
|
int ex;
|
|
|
|
|
const char *name = lua_tostring(L, 1);
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
|
|
/* extend the io table */
|
|
|
|
|
lua_getglobal(L, "io"); /* . io */
|
|
|
|
|
if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded");
|
|
|
|
|
@ -540,11 +535,9 @@ int luaopen_ex(lua_State *L)
|
|
|
|
|
lua_getfield(L, -2, "stderr"); /* . io ex_pipe io_stderr */
|
|
|
|
|
lua_getfenv(L, -1); /* . io ex_pipe io_stderr E */
|
|
|
|
|
lua_setfenv(L, -3); /* . io ex_pipe io_stderr */
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|