From 32162cad792085f70dc94f4dd687a51c5a8ff26a Mon Sep 17 00:00:00 2001 From: mark Date: Sat, 28 Jan 2006 02:35:37 +0000 Subject: [PATCH] Got os.spawn to work. --- w32api/Makefile | 2 +- w32api/ex.c | 230 ++++++++++++++++++++++++++++++------------------ 2 files changed, 143 insertions(+), 89 deletions(-) diff --git a/w32api/Makefile b/w32api/Makefile index 51221b3..03dc2e1 100755 --- a/w32api/Makefile +++ b/w32api/Makefile @@ -2,7 +2,7 @@ TARGET_ARCH = -mno-cygwin CFLAGS = $(WARNINGS) $(DEFINES) $(INCLUDES) DEFINES = -DWIN32_LEAN_AND_MEAN INCLUDES = -I${LUA}/include -WARNINGS = -W -Wall +WARNINGS = -W -Wall -Wno-missing-braces LUA = /home/mark/src/lang/lua/lua-5.1-rc2 LUALIBS = -L$(LUA)/lib -llua51 diff --git a/w32api/ex.c b/w32api/ex.c index f87c6f2..aa3919a 100755 --- a/w32api/ex.c +++ b/w32api/ex.c @@ -14,8 +14,7 @@ /* Generally useful function -- what luaL_checkudata() should do */ -static void * -luaL_checkuserdata(lua_State *L, int idx, const char *tname) +static void *luaL_checkuserdata(lua_State *L, int idx, const char *tname) { void *ud; luaL_argcheck(L, ud = luaL_checkudata(L, idx, tname), idx, tname); @@ -45,24 +44,6 @@ static int windows_error(lua_State *L) } -/* -- environment-table */ -static int ex_environ(lua_State *L) -{ - const char *envs, *nam, *val, *end; - if (!(envs = GetEnvironmentStrings())) - return windows_error(L); - lua_newtable(L); - for (nam = envs; *nam; nam = end + 1) { - val = strchr(nam, '='); - end = strchr(val, '\0'); - lua_pushlstring(L, nam, val - nam); - val++; - lua_pushlstring(L, val, end - val); - lua_settable(L, -3); - } - return 1; -} - /* name -- value/nil */ static int ex_getenv(lua_State *L) { @@ -100,6 +81,24 @@ static int ex_unsetenv(lua_State *L) return 1; } +/* -- environment-table */ +static int ex_environ(lua_State *L) +{ + const char *envs, *nam, *val, *end; + if (!(envs = GetEnvironmentStrings())) + return windows_error(L); + lua_newtable(L); + for (nam = envs; *nam; nam = end + 1) { + val = strchr(nam, '='); + end = strchr(val, '\0'); + lua_pushlstring(L, nam, val - nam); + val++; + lua_pushlstring(L, val, end - val); + lua_settable(L, -3); + } + return 1; +} + /* seconds -- */ static int ex_sleep(lua_State *L) @@ -157,25 +156,21 @@ static int ex_dirent(lua_State *L) { return luaL_error(L, "not yet implemented") static int file_lock(lua_State *L, FILE *fh, const char *mode, long offset, long length) { HANDLE h = get_handle(fh); - DWORD flags = LOCKFILE_FAIL_IMMEDIATELY; + DWORD flags; LARGE_INTEGER len = {0}; OVERLAPPED ov = {0}; BOOL ret; - if (length) - len.LowPart = length; - else - len.LowPart = GetFileSize(h, &len.HighPart); + if (length) len.LowPart = length; + else len.LowPart = GetFileSize(h, &len.HighPart); ov.Offset = offset; switch (*mode) { - case 'r': break; - case 'w': flags |= LOCKFILE_EXCLUSIVE_LOCK; break; + case 'w': flags = LOCKFILE_EXCLUSIVE_LOCK; /*FALLTHRU*/ + case 'r': flags |= LOCKFILE_FAIL_IMMEDIATELY; break; case 'u': flags = 0; break; default: return luaL_error(L, "invalid mode"); } - if (flags) - ret = LockFileEx(h, flags, 0, len.LowPart, len.HighPart, &ov); - else - ret = UnlockFileEx(h, 0, len.LowPart, len.HighPart, &ov); + ret = flags ? LockFileEx(h, flags, 0, len.LowPart, len.HighPart, &ov) + : UnlockFileEx(h, 0, len.LowPart, len.HighPart, &ov); if (!ret) return windows_error(L); lua_pushboolean(L, 1); @@ -245,63 +240,69 @@ static int ex_pipe(lua_State *L) return 2; } - -int abs_index(lua_State *L, int idx) +static int needs_quoting(const char *s) { - return idx >= 0 ? idx : lua_gettop(L) + idx + 1; + return s[0] != '"' && strchr(s, ' '); } -static const char *concat_args(lua_State *L, int index) +/* {arg1,arg 2} => " arg1 \"arg2\"" */ +static const char *concat_args(lua_State *L) { luaL_Buffer args; luaL_buffinit(L, &args); - size_t i, n = lua_objlen(L, index); - index = abs_index(L, index); + size_t i, n = lua_objlen(L, -1); for (i = 1; i <= n; i++) { - int space; - lua_rawgeti(L, index, i); - space = 0 == strchr(luaL_checkstring(L, -1), ' '); + int quote; + lua_rawgeti(L, -1, i); /* ... argtab arg */ + /* XXX checkstring is wrong here */ + quote = needs_quoting(luaL_checkstring(L, -1)); luaL_putchar(&args, ' '); - if (space) luaL_putchar(&args, '"'); + if (quote) luaL_putchar(&args, '"'); luaL_addvalue(&args); - if (space) luaL_putchar(&args, '"'); + if (quote) luaL_putchar(&args, '"'); + lua_pop(L, 1); /* ... argtab */ } - lua_pop(L, n); - luaL_pushresult(&args); + lua_pop(L, 1); /* ... */ + luaL_pushresult(&args); /* ... argstr */ return lua_tostring(L, -1); } -static const char *concat_env(lua_State *L, int index) +/* {nam1=val1,nam2=val2} => "nam1=val1\0nam2=val2\0" */ +static const char *concat_env(lua_State *L) { luaL_Buffer env; - index = abs_index(L, index); luaL_buffinit(L, &env); - lua_pushnil(L); /* nil */ - while (lua_next(L, -2)) { /* k v */ - /* Is the luaL_checktype() warning message useful here? */ + lua_pushnil(L); /* ... envtab nil */ + while (lua_next(L, -2)) { /* ... envtab k v */ + /* XXX luaL_checktype is wrong here */ luaL_checktype(L, -2, LUA_TSTRING); luaL_checktype(L, -1, LUA_TSTRING); - lua_pushvalue(L, -2); /* k v k */ + lua_pushvalue(L, -2); /* ... envtab k v k */ luaL_addvalue(&env); luaL_putchar(&env, '='); - lua_pop(L, 1); /* k v */ + lua_pop(L, 1); /* ... envtab k v */ luaL_addvalue(&env); luaL_putchar(&env, '\0'); - lua_pop(L, 1); /* k */ + lua_pop(L, 1); /* ... envtab k */ } luaL_putchar(&env, '\0'); - luaL_pushresult(&env); /* env */ + lua_pop(L, 1); /* ... */ + luaL_pushresult(&env); /* ... envstr */ return lua_tostring(L, -1); } -static void get_redirect(lua_State *L, const char *stream, HANDLE *handle) +/* XXX document me */ +static int get_redirect(lua_State *L, const char *stdname, HANDLE *ph) { - lua_getfield(L, 2, stream); - if (!lua_isnil(L, -1)) { + int ret; + lua_getfield(L, 2, stdname); + if ((ret = !lua_isnil(L, -1))) { + /* XXX checkuserdata is wrong here */ FILE **pf = luaL_checkuserdata(L, -1, LUA_FILEHANDLE); - *handle = get_handle(*pf); + *ph = get_handle(*pf); } lua_pop(L, 1); + return ret; } #define PROCESS_HANDLE "process" @@ -310,56 +311,110 @@ struct process { int status; }; -/* filename [options] -- true/nil,error */ -/* args-options -- true/nil,error */ +/* filename [args-opts] -- true/nil,error */ +/* args-opts -- true/nil,error */ static int ex_spawn(lua_State *L) { const char *cmdline; const char *environment; struct process *p; - STARTUPINFO si; + STARTUPINFO si = {sizeof si}; PROCESS_INFORMATION pi; BOOL ret; - /* XXX check for filename,options or just options */ - luaL_checktype(L, 1, LUA_TSTRING); - cmdline = lua_tostring(L, 1); + + if (lua_type(L, 1) == LUA_TTABLE) { + lua_getfield(L, 1, "command"); /* opts cmd */ + if (!lua_isnil(L, -1)) { + /* convert {command=command,arg1,...} to command {arg1,...} */ + lua_insert(L, 1); /* cmd opts */ + } + else { + /* convert {arg0,arg1,...} to arg0 {arg1,...} */ + int i, n = lua_objlen(L, 1); + lua_rawgeti(L, 1, 1); /* opts nil cmd */ + if (lua_isnil(L, -1)) + luaL_error(L, "no command specified"); + lua_insert(L, 1); /* cmd opts nil */ + for (i = 2; i <= n; i++) { + lua_rawgeti(L, 2, i); /* cmd opts nil argi */ + lua_rawseti(L, 2, i - 1); /* cmd opts nil */ + } + lua_rawseti(L, 2, n); /* cmd opts */ + } + } + + /* get command */ + cmdline = luaL_checkstring(L, 1); + if (needs_quoting(cmdline)) { + lua_pushliteral(L, "\""); /* cmd ... q */ + lua_pushvalue(L, 1); /* cmd ... q cmd */ + lua_pushvalue(L, -2); /* cmd ... q cmd q */ + lua_concat(L, 3); /* cmd ... "cmd" */ + lua_replace(L, 1); /* "cmd" ... */ + } + + /* set defaults */ environment = 0; - si.cb = sizeof si; - si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); si.hStdError = GetStdHandle(STD_ERROR_HANDLE); - if (lua_type(L, 2) == LUA_TTABLE) { - lua_getfield(L, 2, "args"); - if (!lua_isnil(L, -1)) { - luaL_checktype(L, -1, LUA_TTABLE); - lua_pushvalue(L, 1); /* push filename */ - concat_args(L, -2); - lua_concat(L, 2); /* filename .. (" args")* */ + + /* get arguments, environment, and redirections */ + switch (lua_type(L, 2)) { + default: luaL_argerror(L, 2, "expected options table"); break; + case LUA_TNONE: break; + case LUA_TTABLE: + lua_getfield(L, 2, "args"); /* cmd opts ... argtab */ + switch (lua_type(L, -1)) { + default: luaL_error(L, "args option must be an array"); break; + case LUA_TNIL: + lua_pop(L, 1); /* cmd opts */ + if (lua_objlen(L, 2) == 0) break; + lua_pushvalue(L, 2); /* cmd opts ... opts */ + if (0)/*FALLTHRU*/ + case LUA_TTABLE: + if (lua_objlen(L, -1) > 0) + luaL_error(L, "cannot specify both the args option and array values"); + concat_args(L); /* cmd opts ... argstr */ + lua_pushvalue(L, 1); /* cmd opts ... argstr cmd */ + lua_replace(L, -2); /* cmd opts ... cmd argstr */ + lua_concat(L, 2); /* cmd opts ... cmdline */ cmdline = lua_tostring(L, -1); + break; } - lua_getfield(L, 2, "env"); - if (!lua_isnil(L, -1)) { - luaL_checktype(L, -1, LUA_TTABLE); - environment = concat_env(L, -1); + lua_getfield(L, 2, "env"); /* cmd opts ... envtab */ + switch (lua_type(L, -1)) { + default: luaL_error(L, "env option must be a table"); break; + case LUA_TNIL: + lua_pop(L, 1); /* cmd opts */ + break; + case LUA_TTABLE: + environment = concat_env(L); /* cmd opts ... envstr */ + break; } - get_redirect(L, "stdin", &si.hStdInput); - get_redirect(L, "stdout", &si.hStdOutput); - get_redirect(L, "stderr", &si.hStdError); + if (get_redirect(L, "stdin", &si.hStdInput) /* cmd opts ... in? */ + | get_redirect(L, "stdout", &si.hStdOutput) /* cmd opts ... out? */ + | get_redirect(L, "stderr", &si.hStdError)) /* cmd opts ... err? */ + si.dwFlags = STARTF_USESTDHANDLES; + break; } - p = lua_newuserdata(L, sizeof *p); - luaL_getmetatable(L, PROCESS_HANDLE); - lua_setmetatable(L, -2); + p = lua_newuserdata(L, sizeof *p); /* cmd opts ... proc */ + luaL_getmetatable(L, PROCESS_HANDLE); /* cmd opts ... proc M */ + lua_setmetatable(L, -2); /* cmd opts ... proc */ p->status = -1; + + /* XXX does CreateProcess modify its environment argument? */ cmdline = strdup(cmdline); - environment = strdup(environment); + if (environment) environment = strdup(environment); + fprintf(stderr, "CreateProcess(%s)\n", cmdline); ret = CreateProcess(0, (char *)cmdline, 0, 0, 0, 0, (char *)environment, 0, &si, &pi); - free((char *)cmdline); - free((char *)environment); +// if (environment) free((char *)environment); +// free((char *)cmdline); + if (!ret) return windows_error(L); p->hProcess = pi.hProcess; - return 1; + return 1; /* ... proc */ } /* proc -- exitcode/nil error */ @@ -373,7 +428,7 @@ static int process_wait(lua_State *L) p->status = exitcode; } lua_pushnumber(L, p->status); - return 1; + return 1; /* exitcode */ } @@ -411,7 +466,6 @@ static const luaL_reg ex_process_methods[] = { int luaopen_ex(lua_State *L) { - /* extend the io table */ lua_getglobal(L, "io"); if (lua_isnil(L, -1)) luaL_error(L, "io not loaded");