|
|
|
|
@ -102,18 +102,7 @@ static int ex_mkdir(lua_State *L)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* os.remove provides the correct semantics on POSIX systems */
|
|
|
|
|
#if 0
|
|
|
|
|
/* pathname -- true/nil error */
|
|
|
|
|
static int ex_remove(lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
const char *pathname = luaL_checkstring(L, 1);
|
|
|
|
|
if (-1 == remove(pathname))
|
|
|
|
|
return push_error(L);
|
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
/* Lua os.remove provides the correct semantics on POSIX systems */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static FILE *check_file(lua_State *L, int idx, const char *argname)
|
|
|
|
|
@ -247,13 +236,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->d_name));
|
|
|
|
|
while (d && isdotfile(d->d_name)) continue;
|
|
|
|
|
if (!d) return push_error(L);
|
|
|
|
|
new_dirent(L); /* diriter ... entry */
|
|
|
|
|
diriter_getpathname(L, 1); /* diriter ... entry dirpath */
|
|
|
|
|
lua_pushstring(L, d->d_name); /* 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->d_name); /* 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 ... */
|
|
|
|
|
@ -263,7 +252,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)
|
|
|
|
|
{
|
|
|
|
|
struct flock k;
|
|
|
|
|
switch (*mode) {
|
|
|
|
|
@ -333,7 +323,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))
|
|
|
|
|
@ -347,7 +338,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:
|
|
|
|
|
@ -380,17 +370,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 ... */
|
|
|
|
|
@ -398,13 +386,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;
|
|
|
|
|
@ -416,7 +406,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 */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -437,6 +426,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)
|
|
|
|
|
@ -444,20 +435,17 @@ 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},
|
|
|
|
|
#if 0
|
|
|
|
|
{"remove", ex_remove},
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
{"dir", ex_dir},
|
|
|
|
|
{"dirent", ex_dirent},
|
|
|
|
|
|
|
|
|
|
/* process control */
|
|
|
|
|
{"sleep", ex_sleep},
|
|
|
|
|
{"spawn", ex_spawn},
|
|
|
|
|
{0,0} };
|
|
|
|
|
@ -469,33 +457,25 @@ 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);
|
|
|
|
|
lua_getfield(L, -1, "remove");
|
|
|
|
|
lua_setfield(L, ex, "remove");
|
|
|
|
|
|
|
|
|
|
/* extend the io table */
|
|
|
|
|
lua_getglobal(L, "io"); /* . io */
|
|
|
|
|
if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded");
|
|
|
|
|
@ -504,11 +484,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;
|
|
|
|
|
}
|
|
|
|
|
|