reformatted -- use spaces instead of tabs.

master
mark 19 years ago
parent bb1d518bc8
commit ff7e489f7b

@ -3,7 +3,7 @@ include ../conf
CFLAGS= $(WARNINGS) $(DEFINES) $(INCLUDES) CFLAGS= $(WARNINGS) $(DEFINES) $(INCLUDES)
DEFINES= -D_XOPEN_SOURCE=600 $(POSIX_SPAWN) $(ENVIRON) DEFINES= -D_XOPEN_SOURCE=600 $(POSIX_SPAWN) $(ENVIRON)
INCLUDES= $(LUAINC) INCLUDES= $(LUAINC)
WARNINGS= -W -Wall WARNINGS= -W -Wall -std=c89
LIBS= $(LUALIB) LIBS= $(LUALIB)
T= ex.so T= ex.so

@ -102,18 +102,7 @@ static int ex_mkdir(lua_State *L)
return 1; return 1;
} }
/* os.remove provides the correct semantics on POSIX systems */ /* Lua 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
static FILE *check_file(lua_State *L, int idx, const char *argname) 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: case LUA_TUSERDATA:
pd = luaL_checkudata(L, 1, DIR_HANDLE); pd = luaL_checkudata(L, 1, DIR_HANDLE);
do d = readdir(*pd); do d = readdir(*pd);
while (d && isdotfile(d->d_name)); while (d && isdotfile(d->d_name)) continue;
if (!d) return push_error(L); if (!d) return push_error(L);
new_dirent(L); /* diriter ... entry */ new_dirent(L); /* diriter ... entry */
diriter_getpathname(L, 1); /* diriter ... entry dirpath */ diriter_getpathname(L, 1); /* diriter ... entry dir */
lua_pushstring(L, d->d_name); /* diriter ... entry dirpath name */ lua_pushstring(L, d->d_name); /* diriter ... entry dir name */
lua_pushvalue(L, -1); /* diriter ... entry dirpath name name */ lua_pushvalue(L, -1); /* diriter ... entry dir name name */
lua_setfield(L, -4, "name"); /* diriter ... entry dirpath name */ lua_setfield(L, -4, "name"); /* diriter ... entry dir name */
lua_concat(L, 2); /* diriter ... entry fullpath */ lua_concat(L, 2); /* diriter ... entry fullpath */
lua_replace(L, 1); /* fullpath ... entry */ lua_replace(L, 1); /* fullpath ... entry */
lua_replace(L, 2); /* 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; struct flock k;
switch (*mode) { 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); lua_getfield(L, idx, stdname);
if (!lua_isnil(L, -1)) if (!lua_isnil(L, -1))
@ -347,7 +338,6 @@ static int ex_spawn(lua_State *L)
{ {
struct spawn_params *params; struct spawn_params *params;
int have_options; int have_options;
switch (lua_type(L, 1)) { switch (lua_type(L, 1)) {
default: return luaL_typerror(L, 1, "string or table"); default: return luaL_typerror(L, 1, "string or table");
case LUA_TSTRING: case LUA_TSTRING:
@ -380,17 +370,15 @@ static int ex_spawn(lua_State *L)
luaL_typename(L, 1)); luaL_typename(L, 1));
break; break;
} }
params = spawn_param_init(L); params = spawn_param_init(L);
/* get filename to execute */ /* get filename to execute */
spawn_param_filename(params, lua_tostring(L, 1)); spawn_param_filename(params, lua_tostring(L, 1));
/* get arguments, environment, and redirections */ /* get arguments, environment, and redirections */
if (have_options) { if (have_options) {
lua_getfield(L, 2, "args"); /* cmd opts ... argtab */ lua_getfield(L, 2, "args"); /* cmd opts ... argtab */
switch (lua_type(L, -1)) { 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)); luaL_typename(L, -1));
case LUA_TNIL: case LUA_TNIL:
lua_pop(L, 1); /* cmd opts ... */ lua_pop(L, 1); /* cmd opts ... */
@ -398,13 +386,15 @@ static int ex_spawn(lua_State *L)
if (0) /*FALLTHRU*/ if (0) /*FALLTHRU*/
case LUA_TTABLE: case LUA_TTABLE:
if (lua_objlen(L, 2) > 0) 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 ... */ spawn_param_args(params); /* cmd opts ... */
break; break;
} }
lua_getfield(L, 2, "env"); /* cmd opts ... envtab */ lua_getfield(L, 2, "env"); /* cmd opts ... envtab */
switch (lua_type(L, -1)) { 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)); luaL_typename(L, -1));
case LUA_TNIL: case LUA_TNIL:
break; break;
@ -416,7 +406,6 @@ static int ex_spawn(lua_State *L)
get_redirect(L, 2, "stdout", params); /* cmd opts ... */ get_redirect(L, 2, "stdout", params); /* cmd opts ... */
get_redirect(L, 2, "stderr", params); /* cmd opts ... */ get_redirect(L, 2, "stderr", params); /* cmd opts ... */
} }
return spawn_param_execute(params); /* proc/nil error */ 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) int luaopen_ex(lua_State *L)
{ {
const char *name = lua_tostring(L, 1);
int ex;
const luaL_reg ex_iolib[] = { const luaL_reg ex_iolib[] = {
{"pipe", ex_pipe}, {"pipe", ex_pipe},
#define ex_iofile_methods (ex_iolib + 1) #define ex_iofile_methods (ex_iolib + 1)
@ -444,20 +435,17 @@ int luaopen_ex(lua_State *L)
{"unlock", ex_unlock}, {"unlock", ex_unlock},
{0,0} }; {0,0} };
const luaL_reg ex_oslib[] = { const luaL_reg ex_oslib[] = {
/* environment */
{"getenv", ex_getenv}, {"getenv", ex_getenv},
{"setenv", ex_setenv}, {"setenv", ex_setenv},
{"environ", ex_environ}, {"environ", ex_environ},
/* file system */
{"currentdir", ex_currentdir}, {"currentdir", ex_currentdir},
{"chdir", ex_chdir}, {"chdir", ex_chdir},
{"mkdir", ex_mkdir}, {"mkdir", ex_mkdir},
#if 0
{"remove", ex_remove},
#endif
{"dir", ex_dir}, {"dir", ex_dir},
{"dirent", ex_dirent}, {"dirent", ex_dirent},
/* process control */
{"sleep", ex_sleep}, {"sleep", ex_sleep},
{"spawn", ex_spawn}, {"spawn", ex_spawn},
{0,0} }; {0,0} };
@ -469,33 +457,25 @@ int luaopen_ex(lua_State *L)
#define ex_process_functions (ex_process_methods + 1) #define ex_process_functions (ex_process_methods + 1)
{"wait", process_wait}, {"wait", process_wait},
{0,0} }; {0,0} };
int ex;
const char *name = lua_tostring(L, 1);
/* diriter metatable */ /* diriter metatable */
luaL_newmetatable(L, DIR_HANDLE); /* . D */ luaL_newmetatable(L, DIR_HANDLE); /* . D */
luaL_register(L, 0, ex_diriter_methods); /* . D */ luaL_register(L, 0, ex_diriter_methods); /* . D */
/* proc metatable */ /* proc metatable */
luaL_newmetatable(L, PROCESS_HANDLE); /* . P */ luaL_newmetatable(L, PROCESS_HANDLE); /* . P */
luaL_register(L, 0, ex_process_methods); /* . P */ luaL_register(L, 0, ex_process_methods); /* . P */
lua_pushvalue(L, -1); /* . P P */ lua_pushvalue(L, -1); /* . P P */
lua_setfield(L, -2, "__index"); /* . P */ lua_setfield(L, -2, "__index"); /* . P */
/* make all functions available via ex. namespace */ /* make all functions available via ex. namespace */
luaL_register(L, name, ex_oslib); /* . P ex */ luaL_register(L, name, ex_oslib); /* . P ex */
luaL_register(L, 0, ex_iolib); luaL_register(L, 0, ex_iolib);
copyfields(L, ex_process_functions, -2, -1); copyfields(L, ex_process_functions, -2, -1);
ex = lua_gettop(L); ex = lua_gettop(L);
/* extend the os table */ /* extend the os table */
lua_getglobal(L, "os"); /* . 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");
copyfields(L, ex_oslib, ex, -1); copyfields(L, ex_oslib, ex, -1);
lua_getfield(L, -1, "remove"); lua_getfield(L, -1, "remove");
lua_setfield(L, ex, "remove"); lua_setfield(L, ex, "remove");
/* extend the io table */ /* extend the io table */
lua_getglobal(L, "io"); /* . 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");
@ -504,11 +484,9 @@ int luaopen_ex(lua_State *L)
lua_getfield(L, -2, "stderr"); /* . io ex_pipe io_stderr */ lua_getfield(L, -2, "stderr"); /* . io ex_pipe io_stderr */
lua_getfenv(L, -1); /* . io ex_pipe io_stderr E */ lua_getfenv(L, -1); /* . io ex_pipe io_stderr E */
lua_setfenv(L, -3); /* . io ex_pipe io_stderr */ lua_setfenv(L, -3); /* . io ex_pipe io_stderr */
/* extend the io.file metatable */ /* extend the io.file metatable */
luaL_getmetatable(L, LUA_FILEHANDLE); /* . 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");
copyfields(L, ex_iofile_methods, ex, -1); copyfields(L, ex_iofile_methods, ex, -1);
return 1; return 1;
} }

@ -19,13 +19,17 @@ ENVIRON_DECL
#endif #endif
int posix_spawn_file_actions_init(posix_spawn_file_actions_t *act) int posix_spawn_file_actions_init(
posix_spawn_file_actions_t *act)
{ {
act->dups[0] = act->dups[1] = act->dups[2] = -1; act->dups[0] = act->dups[1] = act->dups[2] = -1;
return 0; return 0;
} }
int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *act, int d, int n) int posix_spawn_file_actions_adddup2(
posix_spawn_file_actions_t *act,
int d,
int n)
{ {
/* good faith effort to determine validity of descriptors */ /* good faith effort to determine validity of descriptors */
if (d < 0 || OPEN_MAX < d || n < 0 || OPEN_MAX < n) { if (d < 0 || OPEN_MAX < d || n < 0 || OPEN_MAX < n) {
@ -41,12 +45,14 @@ int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *act, int d, int
return 0; return 0;
} }
int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *act) int posix_spawn_file_actions_destroy(
posix_spawn_file_actions_t *act)
{ {
return 0; return 0;
} }
int posix_spawnp(pid_t *restrict ppid, int posix_spawnp(
pid_t *restrict ppid,
const char *restrict path, const char *restrict path,
const posix_spawn_file_actions_t *act, const posix_spawn_file_actions_t *act,
const posix_spawnattr_t *restrict attrp, const posix_spawnattr_t *restrict attrp,

@ -23,18 +23,42 @@ enum {
}; };
int posix_spawnattr_init(posix_spawnattr_t *attrp); int posix_spawnattr_init(posix_spawnattr_t *attrp);
int posix_spawnattr_getflags(const posix_spawnattr_t *restrict attrp, short *restrict flags); int posix_spawnattr_getflags(
int posix_spawnattr_setflags(posix_spawnattr_t *attrp, short flags); const posix_spawnattr_t *restrict attrp,
int posix_spawnattr_getpgroup(const posix_spawnattr_t *restrict attrp, pid_t *restrict pgroup); short *restrict flags);
int posix_spawnattr_setpgroup(posix_spawnattr_t *attrp, pid_t pgroup); int posix_spawnattr_setflags(
int posix_spawnattr_getschedparam(const posix_spawnattr_t *restrict attrp, struct sched_param *restrict schedparam); posix_spawnattr_t *attrp,
int posix_spawnattr_setschedparam(posix_spawnattr_t *restrict attrp, const struct sched_param *restrict schedparam); short flags);
int posix_spawnattr_getschedpolicy(const posix_spawnattr_t *restrict attrp, int *restrict schedpolicy); int posix_spawnattr_getpgroup(
int posix_spawnattr_setschedpolicy(posix_spawnattr_t *attrp, int schedpolicy); const posix_spawnattr_t *restrict attrp,
int posix_spawnattr_getsigdefault(const posix_spawnattr_t *restrict attrp, sigset_t *restrict sigdefault); pid_t *restrict pgroup);
int posix_spawnattr_setsigdefault(posix_spawnattr_t *restrict attrp, const sigset_t *restrict sigdefault); int posix_spawnattr_setpgroup(
int posix_spawnattr_getsigmask(const posix_spawnattr_t *restrict attrp, sigset_t *restrict sigmask); posix_spawnattr_t *attrp,
int posix_spawnattr_setsigmask(posix_spawnattr_t *restrict attrp, const sigset_t *restrict sigmask); pid_t pgroup);
int posix_spawnattr_getschedparam(
const posix_spawnattr_t *restrict attrp,
struct sched_param *restrict schedparam);
int posix_spawnattr_setschedparam(
posix_spawnattr_t *restrict attrp,
const struct sched_param *restrict schedparam);
int posix_spawnattr_getschedpolicy(
const posix_spawnattr_t *restrict attrp,
int *restrict schedpolicy);
int posix_spawnattr_setschedpolicy(
posix_spawnattr_t *attrp,
int schedpolicy);
int posix_spawnattr_getsigdefault(
const posix_spawnattr_t *restrict attrp,
sigset_t *restrict sigdefault);
int posix_spawnattr_setsigdefault(
posix_spawnattr_t *restrict attrp,
const sigset_t *restrict sigdefault);
int posix_spawnattr_getsigmask(
const posix_spawnattr_t *restrict attrp,
sigset_t *restrict sigmask);
int posix_spawnattr_setsigmask(
posix_spawnattr_t *restrict attrp,
const sigset_t *restrict sigmask);
int posix_spawnattr_destroy(posix_spawnattr_t *attrp); int posix_spawnattr_destroy(posix_spawnattr_t *attrp);
typedef struct posix_spawn_file_actions posix_spawn_file_actions_t; typedef struct posix_spawn_file_actions posix_spawn_file_actions_t;
@ -43,10 +67,32 @@ struct posix_spawn_file_actions {
}; };
int posix_spawn_file_actions_init(posix_spawn_file_actions_t *file_actions); int posix_spawn_file_actions_init(posix_spawn_file_actions_t *file_actions);
int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *file_actions, int filedes, int newfiledes); int posix_spawn_file_actions_adddup2(
int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *file_actions, int filedes); posix_spawn_file_actions_t *file_actions,
int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *restrict file_actions, int filedes, const char *restrict path, int oflag, mode_t mode); int filedes,
int newfiledes);
int posix_spawn_file_actions_addclose(
posix_spawn_file_actions_t *file_actions,
int filedes);
int posix_spawn_file_actions_addopen(
posix_spawn_file_actions_t *restrict file_actions,
int filedes,
const char *restrict path,
int oflag,
mode_t mode);
int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *file_actions); int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *file_actions);
int posix_spawn(pid_t *restrict, const char *restrict, const posix_spawn_file_actions_t *, const posix_spawnattr_t *restrict, char *const argv[restrict], char *const envp[restrict]); int posix_spawn(
int posix_spawnp(pid_t *restrict, const char *restrict, const posix_spawn_file_actions_t *, const posix_spawnattr_t *restrict, char *const argv[restrict], char *const envp[restrict]); pid_t *restrict,
const char *restrict,
const posix_spawn_file_actions_t *,
const posix_spawnattr_t *restrict,
char *const argv[restrict],
char *const envp[restrict]);
int posix_spawnp(
pid_t *restrict,
const char *restrict,
const posix_spawn_file_actions_t *,
const posix_spawnattr_t *restrict,
char *const argv[restrict],
char *const envp[restrict]);

@ -127,7 +127,8 @@ int spawn_param_execute(struct spawn_params *p)
luaL_getmetatable(L, PROCESS_HANDLE); luaL_getmetatable(L, PROCESS_HANDLE);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
proc->status = -1; proc->status = -1;
ret = posix_spawnp(&proc->pid, p->command, &p->redirect, 0, (char *const *)p->argv, (char *const *)p->envp); ret = posix_spawnp(&proc->pid, p->command, &p->redirect, 0,
(char *const *)p->argv, (char *const *)p->envp);
posix_spawn_file_actions_destroy(&p->redirect); posix_spawn_file_actions_destroy(&p->redirect);
return ret != 0 ? push_error(L) : 1; return ret != 0 ? push_error(L) : 1;
} }

@ -19,6 +19,7 @@ void spawn_param_args(struct spawn_params *p);
void spawn_param_env(struct spawn_params *p); void spawn_param_env(struct spawn_params *p);
void spawn_param_redirect(struct spawn_params *p, const char *stdname, int fd); void spawn_param_redirect(struct spawn_params *p, const char *stdname, int fd);
int spawn_param_execute(struct spawn_params *p); int spawn_param_execute(struct spawn_params *p);
int process_wait(lua_State *L); int process_wait(lua_State *L);
int process_tostring(lua_State *L); int process_tostring(lua_State *L);

@ -23,8 +23,7 @@ static char *mkpattern(const char *name)
return pattern; return pattern;
} }
DIR * DIR *opendir(const char *name)
opendir(const char *name)
{ {
DIR *pi = malloc(sizeof *pi); DIR *pi = malloc(sizeof *pi);
char *pattern = mkpattern(name); char *pattern = mkpattern(name);
@ -46,8 +45,7 @@ static int isdotfile(const char *name)
|| (name[1] == '.' && name[2] == 0)); || (name[1] == '.' && name[2] == 0));
} }
const WIN32_FIND_DATA * const WIN32_FIND_DATA *readdir(DIR *pi)
readdir(DIR *pi)
{ {
switch (pi->first) do { switch (pi->first) do {
default: default:
@ -61,11 +59,9 @@ readdir(DIR *pi)
return &pi->fd; return &pi->fd;
} }
void void closedir(DIR *pi)
closedir(DIR *pi)
{
if (pi->hf != INVALID_HANDLE_VALUE)
{ {
if (pi->hf != INVALID_HANDLE_VALUE) {
FindClose(pi->hf); FindClose(pi->hf);
pi->hf = INVALID_HANDLE_VALUE; pi->hf = INVALID_HANDLE_VALUE;
} }

@ -158,7 +158,8 @@ static lua_Number qword_to_number(DWORD hi, DWORD lo)
static lua_Number get_file_size(const char *name) 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; DWORD lo, hi;
lua_Number size; lua_Number size;
if (h == INVALID_HANDLE_VALUE) if (h == INVALID_HANDLE_VALUE)
@ -289,13 +290,13 @@ static int ex_dir(lua_State *L)
case LUA_TUSERDATA: case LUA_TUSERDATA:
pd = luaL_checkudata(L, 1, DIR_HANDLE); pd = luaL_checkudata(L, 1, DIR_HANDLE);
do d = readdir(*pd); do d = readdir(*pd);
while (d && isdotfile(d->cFileName)); while (d && isdotfile(d->cFileName)) continue;
if (!d) return push_error(L); if (!d) return push_error(L);
new_dirent(L); /* diriter ... entry */ new_dirent(L); /* diriter ... entry */
diriter_getpathname(L, 1); /* diriter ... entry dirpath */ diriter_getpathname(L, 1); /* diriter ... entry dir */
lua_pushstring(L, d->cFileName); /* diriter ... entry dirpath name */ lua_pushstring(L, d->cFileName); /* diriter ... entry dir name */
lua_pushvalue(L, -1); /* diriter ... entry dirpath name name */ lua_pushvalue(L, -1); /* diriter ... entry dir name name */
lua_setfield(L, -4, "name"); /* diriter ... entry dirpath name */ lua_setfield(L, -4, "name"); /* diriter ... entry dir name */
lua_concat(L, 2); /* diriter ... entry fullpath */ lua_concat(L, 2); /* diriter ... entry fullpath */
lua_replace(L, 1); /* fullpath ... entry */ lua_replace(L, 1); /* fullpath ... entry */
lua_replace(L, 2); /* 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); HANDLE h = file_handle(f);
DWORD flags; 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); lua_getfield(L, idx, stdname);
if (!lua_isnil(L, -1)) if (!lua_isnil(L, -1))
@ -387,7 +390,6 @@ static int ex_spawn(lua_State *L)
{ {
struct spawn_params *params; struct spawn_params *params;
int have_options; int have_options;
switch (lua_type(L, 1)) { switch (lua_type(L, 1)) {
default: return luaL_typerror(L, 1, "string or table"); default: return luaL_typerror(L, 1, "string or table");
case LUA_TSTRING: case LUA_TSTRING:
@ -420,17 +422,15 @@ static int ex_spawn(lua_State *L)
luaL_typename(L, 1)); luaL_typename(L, 1));
break; break;
} }
params = spawn_param_init(L); params = spawn_param_init(L);
/* get filename to execute */ /* get filename to execute */
spawn_param_filename(params, lua_tostring(L, 1)); spawn_param_filename(params, lua_tostring(L, 1));
/* get arguments, environment, and redirections */ /* get arguments, environment, and redirections */
if (have_options) { if (have_options) {
lua_getfield(L, 2, "args"); /* cmd opts ... argtab */ lua_getfield(L, 2, "args"); /* cmd opts ... argtab */
switch (lua_type(L, -1)) { 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)); luaL_typename(L, -1));
case LUA_TNIL: case LUA_TNIL:
lua_pop(L, 1); /* cmd opts ... */ lua_pop(L, 1); /* cmd opts ... */
@ -438,13 +438,15 @@ static int ex_spawn(lua_State *L)
if (0) /*FALLTHRU*/ if (0) /*FALLTHRU*/
case LUA_TTABLE: case LUA_TTABLE:
if (lua_objlen(L, 2) > 0) 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 ... */ spawn_param_args(params); /* cmd opts ... */
break; break;
} }
lua_getfield(L, 2, "env"); /* cmd opts ... envtab */ lua_getfield(L, 2, "env"); /* cmd opts ... envtab */
switch (lua_type(L, -1)) { 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)); luaL_typename(L, -1));
case LUA_TNIL: case LUA_TNIL:
break; break;
@ -456,7 +458,6 @@ static int ex_spawn(lua_State *L)
get_redirect(L, 2, "stdout", params); /* cmd opts ... */ get_redirect(L, 2, "stdout", params); /* cmd opts ... */
get_redirect(L, 2, "stderr", params); /* cmd opts ... */ get_redirect(L, 2, "stderr", params); /* cmd opts ... */
} }
return spawn_param_execute(params); /* proc/nil error */ 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) int luaopen_ex(lua_State *L)
{ {
const char *name = lua_tostring(L, 1);
int ex;
const luaL_reg ex_iolib[] = { const luaL_reg ex_iolib[] = {
{"pipe", ex_pipe}, {"pipe", ex_pipe},
#define ex_iofile_methods (ex_iolib + 1) #define ex_iofile_methods (ex_iolib + 1)
@ -484,18 +487,18 @@ int luaopen_ex(lua_State *L)
{"unlock", ex_unlock}, {"unlock", ex_unlock},
{0,0} }; {0,0} };
const luaL_reg ex_oslib[] = { const luaL_reg ex_oslib[] = {
/* environment */
{"getenv", ex_getenv}, {"getenv", ex_getenv},
{"setenv", ex_setenv}, {"setenv", ex_setenv},
{"environ", ex_environ}, {"environ", ex_environ},
/* file system */
{"currentdir", ex_currentdir}, {"currentdir", ex_currentdir},
{"chdir", ex_chdir}, {"chdir", ex_chdir},
{"mkdir", ex_mkdir}, {"mkdir", ex_mkdir},
{"remove", ex_remove}, {"remove", ex_remove},
{"dir", ex_dir}, {"dir", ex_dir},
{"dirent", ex_dirent}, {"dirent", ex_dirent},
/* process control */
{"sleep", ex_sleep}, {"sleep", ex_sleep},
{"spawn", ex_spawn}, {"spawn", ex_spawn},
{0,0} }; {0,0} };
@ -507,31 +510,23 @@ int luaopen_ex(lua_State *L)
#define ex_process_functions (ex_process_methods + 1) #define ex_process_functions (ex_process_methods + 1)
{"wait", process_wait}, {"wait", process_wait},
{0,0} }; {0,0} };
int ex;
const char *name = lua_tostring(L, 1);
/* diriter metatable */ /* diriter metatable */
luaL_newmetatable(L, DIR_HANDLE); /* . D */ luaL_newmetatable(L, DIR_HANDLE); /* . D */
luaL_register(L, 0, ex_diriter_methods); /* . D */ luaL_register(L, 0, ex_diriter_methods); /* . D */
/* proc metatable */ /* proc metatable */
luaL_newmetatable(L, PROCESS_HANDLE); /* . P */ luaL_newmetatable(L, PROCESS_HANDLE); /* . P */
luaL_register(L, 0, ex_process_methods); /* . P */ luaL_register(L, 0, ex_process_methods); /* . P */
lua_pushvalue(L, -1); /* . P P */ lua_pushvalue(L, -1); /* . P P */
lua_setfield(L, -2, "__index"); /* . P */ lua_setfield(L, -2, "__index"); /* . P */
/* make all functions available via ex. namespace */ /* make all functions available via ex. namespace */
luaL_register(L, name, ex_oslib); /* . P ex */ luaL_register(L, name, ex_oslib); /* . P ex */
luaL_register(L, 0, ex_iolib); luaL_register(L, 0, ex_iolib);
copyfields(L, ex_process_functions, -2, -1); copyfields(L, ex_process_functions, -2, -1);
ex = lua_gettop(L); ex = lua_gettop(L);
/* extend the os table */ /* extend the os table */
lua_getglobal(L, "os"); /* . 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");
copyfields(L, ex_oslib, ex, -1); copyfields(L, ex_oslib, ex, -1);
/* extend the io table */ /* extend the io table */
lua_getglobal(L, "io"); /* . 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");
@ -540,11 +535,9 @@ int luaopen_ex(lua_State *L)
lua_getfield(L, -2, "stderr"); /* . io ex_pipe io_stderr */ lua_getfield(L, -2, "stderr"); /* . io ex_pipe io_stderr */
lua_getfenv(L, -1); /* . io ex_pipe io_stderr E */ lua_getfenv(L, -1); /* . io ex_pipe io_stderr E */
lua_setfenv(L, -3); /* . io ex_pipe io_stderr */ lua_setfenv(L, -3); /* . io ex_pipe io_stderr */
/* extend the io.file metatable */ /* extend the io.file metatable */
luaL_getmetatable(L, LUA_FILEHANDLE); /* . 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");
copyfields(L, ex_iofile_methods, ex, -1); copyfields(L, ex_iofile_methods, ex, -1);
return 1; return 1;
} }

@ -18,16 +18,12 @@
* nresults is -1 and error is NO_ERROR, then push true and return 1. * nresults is -1 and error is NO_ERROR, then push true and return 1.
* Otherwise, if error is NO_ERROR, return nresults. * Otherwise, if error is NO_ERROR, return nresults.
*/ */
int int windows_pusherror(lua_State *L, DWORD error, int nresults)
windows_pusherror(lua_State *L, DWORD error, int nresults)
{ {
if (error != NO_ERROR || nresults == -2) { if (error != NO_ERROR || nresults == -2) {
char buffer[1024]; char buffer[1024];
size_t len, res; size_t len = sprintf(buffer, "%lu (0x%lX): ", error, error);
size_t res = FormatMessage(
len = sprintf(buffer, "%lu (0x%lX): ", error, error);
res = FormatMessage(
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
0, error, 0, buffer + len, sizeof buffer - len, 0); 0, error, 0, buffer + len, sizeof buffer - len, 0);
if (res) { if (res) {
@ -35,9 +31,9 @@ windows_pusherror(lua_State *L, DWORD error, int nresults)
while (len > 0 && isspace(buffer[len - 1])) while (len > 0 && isspace(buffer[len - 1]))
len--; len--;
} }
else else {
len += sprintf(buffer + len, "<error string not available>"); len += sprintf(buffer + len, "<error string not available>");
}
lua_pushnil(L); lua_pushnil(L);
lua_pushlstring(L, buffer, len); lua_pushlstring(L, buffer, len);
nresults = 2; nresults = 2;

@ -132,14 +132,12 @@ struct process {
int spawn_param_execute(struct spawn_params *p) int spawn_param_execute(struct spawn_params *p)
{ {
lua_State *L = p->L; lua_State *L = p->L;
struct process *proc;
char *c, *e; char *c, *e;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
BOOL ret; BOOL ret;
struct process *proc = lua_newuserdata(L, sizeof *proc);
proc = lua_newuserdata(L, sizeof *proc); /* cmd opts ... proc */ luaL_getmetatable(L, PROCESS_HANDLE);
luaL_getmetatable(L, PROCESS_HANDLE); /* cmd opts ... proc M */ lua_setmetatable(L, -2);
lua_setmetatable(L, -2); /* cmd opts ... proc */
proc->status = -1; proc->status = -1;
c = strdup(p->cmdline); c = strdup(p->cmdline);
e = (char *)p->environment; /* strdup(p->environment); */ e = (char *)p->environment; /* strdup(p->environment); */

@ -17,7 +17,10 @@ struct spawn_params *spawn_param_init(lua_State *L);
void spawn_param_filename(struct spawn_params *p, const char *filename); void spawn_param_filename(struct spawn_params *p, const char *filename);
void spawn_param_args(struct spawn_params *p); void spawn_param_args(struct spawn_params *p);
void spawn_param_env(struct spawn_params *p); void spawn_param_env(struct spawn_params *p);
void spawn_param_redirect(struct spawn_params *p, const char *stdname, HANDLE h); void spawn_param_redirect(
struct spawn_params *p,
const char *stdname,
HANDLE h);
int spawn_param_execute(struct spawn_params *p); int spawn_param_execute(struct spawn_params *p);
int process_wait(lua_State *L); int process_wait(lua_State *L);

Loading…
Cancel
Save