From 4bf9e1f5c28f27971a5cf0cbc2097f98f40193fa Mon Sep 17 00:00:00 2001 From: mark Date: Wed, 10 Jan 2007 21:40:12 +0000 Subject: [PATCH] 20070110T1431-0700 --- Changelog | 10 +++++ INSTALL | 7 +++ Makefile | 2 +- README | 5 +-- conf => conf.in | 0 posix/ex.c | 114 ++++++++++++++++++++++++++++-------------------- w32api/ex.c | 111 +++++++++++++++++++++++----------------------- 7 files changed, 144 insertions(+), 105 deletions(-) create mode 100755 INSTALL rename conf => conf.in (100%) diff --git a/Changelog b/Changelog index 25ff61a..711939b 100755 --- a/Changelog +++ b/Changelog @@ -1,3 +1,13 @@ +ex-20070110T1431-0700 + +* new API for os.sleep + +ex-20070109T1403-0700 + +* fixed build scripts + +* include Copyright notice + ex-20070109 API changes diff --git a/INSTALL b/INSTALL new file mode 100755 index 0000000..aa835ac --- /dev/null +++ b/INSTALL @@ -0,0 +1,7 @@ +Copy or rename conf.in to conf and edit it appropriately. + +Then, use one of the following commands to build: + +make mingw +make cygwin +make linux diff --git a/Makefile b/Makefile index f37342d..f31bd36 100755 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -default:; @echo Choose a platform: mingw cygwin linux +default:; @cat INSTALL all: mingw cygwin mingw:; $(MAKE) -C w32api ex.dll diff --git a/README b/README index 1190dfb..b6bf631 100755 --- a/README +++ b/README @@ -7,9 +7,6 @@ os.getenv(name) -- get environment variable os.setenv(name, value) -- set/unset environment variable os.environ() -- returns a copy of the environment --- Miscellaneous -os.sleep(seconds) -- sleep for (floating) seconds - -- File system cwd = os.currentdir() os.chdir(pathname) @@ -32,5 +29,7 @@ file:unlock(start, length) -- start and length are optional in, out = io.pipe() -- Process control +os.sleep(seconds) -- sleep for (floating-point) seconds +os.sleep(interval, unit) -- sleep for interval/unit seconds pid = os.spawn(filename, {args={}, env={}, stdin=file, stdout=file, stderr=file}) exitcode = pid:wait(pid) diff --git a/conf b/conf.in similarity index 100% rename from conf rename to conf.in diff --git a/posix/ex.c b/posix/ex.c index e602813..e8692dc 100755 --- a/posix/ex.c +++ b/posix/ex.c @@ -14,6 +14,7 @@ ENVIRON_DECL #include #include #include +#include #include "lua.h" #include "lualib.h" @@ -23,7 +24,6 @@ ENVIRON_DECL #include "spawn.h" - /* -- nil error */ extern int push_error(lua_State *L) { @@ -72,15 +72,16 @@ static int ex_environ(lua_State *L) } -/* seconds -- */ -static int ex_sleep(lua_State *L) +/* -- pathname/nil error */ +static int ex_currentdir(lua_State *L) { - lua_Number seconds = luaL_checknumber(L, 1); - usleep(1e6 * seconds); - return 0; + char pathname[PATH_MAX + 1]; + if (!getcwd(pathname, sizeof pathname)) + return push_error(L); + lua_pushstring(L, pathname); + return 1; } - /* pathname -- true/nil error */ static int ex_chdir(lua_State *L) { @@ -101,15 +102,18 @@ static int ex_mkdir(lua_State *L) return 1; } -/* -- pathname/nil error */ -static int ex_currentdir(lua_State *L) +/* os.remove provides the correct semantics on POSIX systems */ +#if 0 +/* pathname -- true/nil error */ +static int ex_remove(lua_State *L) { - char pathname[PATH_MAX + 1]; - if (!getcwd(pathname, sizeof pathname)) + const char *pathname = luaL_checkstring(L, 1); + if (-1 == remove(pathname)) return push_error(L); - lua_pushstring(L, pathname); + lua_pushboolean(L, 1); return 1; } +#endif static FILE *check_file(lua_State *L, int idx, const char *argname) @@ -318,6 +322,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); + usleep(1e6 * interval / units); + return 0; +} + + static void get_redirect(lua_State *L, int idx, const char *stdname, struct spawn_params *p) { lua_getfield(L, idx, stdname); @@ -406,8 +421,9 @@ static int ex_spawn(lua_State *L) } -/* copy the fields given in 'l' from one table to another; insert missing fields */ -static void copy_fields(lua_State *L, const luaL_reg *l, int from, int to) +/* register functions from 'lib' in table 'to' by copying existing + * 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++) { lua_getfield(L, from, l->name); @@ -423,8 +439,7 @@ int luaopen_ex(lua_State *L) { const luaL_reg ex_iolib[] = { {"pipe", ex_pipe}, - {0,0} }; - const luaL_reg ex_iofile_methods[] = { +#define ex_iofile_methods (ex_iolib + 1) {"lock", ex_lock}, {"unlock", ex_unlock}, {0,0} }; @@ -433,62 +448,67 @@ int luaopen_ex(lua_State *L) {"setenv", ex_setenv}, {"environ", ex_environ}, - {"sleep", ex_sleep}, - + {"currentdir", ex_currentdir}, {"chdir", ex_chdir}, {"mkdir", ex_mkdir}, - {"currentdir", ex_currentdir}, +#if 0 + {"remove", ex_remove}, +#endif {"dir", ex_dir}, {"dirent", ex_dirent}, + {"sleep", ex_sleep}, {"spawn", ex_spawn}, {0,0} }; const luaL_reg ex_diriter_methods[] = { {"__gc", diriter_close}, - /* {"__tostring", diriter_tostring}, */ {0,0} }; const luaL_reg ex_process_methods[] = { {"__tostring", process_tostring}, +#define ex_process_functions (ex_process_methods + 1) {"wait", process_wait}, {0,0} }; - /* Make all functions available via ex. namespace */ - luaL_register(L, "ex", ex_iolib); /* . ex */ - luaL_register(L, 0, ex_oslib); - luaL_register(L, 0, ex_iofile_methods); - luaL_register(L, 0, ex_process_methods + 1); /* XXX don't insert __tostring */ - lua_replace(L, 1); /* ex . */ + 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"); /* ex . os */ + lua_getglobal(L, "os"); /* . os */ 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); + lua_getfield(L, -1, "remove"); + lua_setfield(L, ex, "remove"); /* 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"); - copy_fields(L, ex_iolib, 1, -1); /* ex . io */ - copy_fields(L, ex_iofile_methods, 1, -1); /* ex . io */ - lua_getfield(L, 1, "pipe"); /* ex . io ex_pipe */ - lua_getfield(L, -2, "stderr"); /* ex . io ex_pipe io_stderr */ - lua_getfenv(L, -1); /* ex . io ex_pipe io_stderr E */ - lua_setfenv(L, -3); /* ex . io ex_pipe io_stderr */ + copyfields(L, ex_iolib, ex, -1); + lua_getfield(L, ex, "pipe"); /* . io ex_pipe */ + 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); /* ex . F */ + luaL_getmetatable(L, LUA_FILEHANDLE); /* . F */ if (lua_isnil(L, -1)) return luaL_error(L, "can't find FILE* metatable"); - copy_fields(L, ex_iofile_methods, 1, -1); /* ex . F */ - - /* 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 */ + copyfields(L, ex_iofile_methods, ex, -1); - lua_settop(L, 1); /* ex */ return 1; } diff --git a/w32api/ex.c b/w32api/ex.c index d9c7295..cc14a29 100755 --- a/w32api/ex.c +++ b/w32api/ex.c @@ -26,7 +26,6 @@ #define push_error(L) windows_pushlasterror(L) - /* name -- value/nil */ static int ex_getenv(lua_State *L) { @@ -73,15 +72,16 @@ static int ex_environ(lua_State *L) } -/* seconds -- */ -static int ex_sleep(lua_State *L) +/* -- pathname/nil error */ +static int ex_currentdir(lua_State *L) { - lua_Number seconds = luaL_checknumber(L, 1); - Sleep(1e3 * seconds); - return 0; + 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 */ static int ex_chdir(lua_State *L) { @@ -102,16 +102,6 @@ static int ex_mkdir(lua_State *L) 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 */ 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) { DIR **pd = lua_touserdata(L, 1); - closedir(*pd); - *pd = 0; + if (*pd) { + closedir(*pd); + *pd = 0; + } lua_pushnil(L); diriter_setpathname(L, 1); 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) { 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 */ -static void copy_fields(lua_State *L, const luaL_reg *l, int from, int to) +/* register functions from 'lib' in table 'to' by copying existing + * 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++) { lua_getfield(L, from, l->name); @@ -468,8 +472,7 @@ int luaopen_ex(lua_State *L) { const luaL_reg ex_iolib[] = { {"pipe", ex_pipe}, - {0,0} }; - const luaL_reg ex_iofile_methods[] = { +#define ex_iofile_methods (ex_iolib + 1) {"lock", ex_lock}, {"unlock", ex_unlock}, {0,0} }; @@ -478,63 +481,63 @@ int luaopen_ex(lua_State *L) {"setenv", ex_setenv}, {"environ", ex_environ}, - {"sleep", ex_sleep}, - + {"currentdir", ex_currentdir}, {"chdir", ex_chdir}, {"mkdir", ex_mkdir}, - {"currentdir", ex_currentdir}, {"remove", ex_remove}, {"dir", ex_dir}, {"dirent", ex_dirent}, + {"sleep", ex_sleep}, {"spawn", ex_spawn}, {0,0} }; const luaL_reg ex_diriter_methods[] = { {"__gc", diriter_close}, - /* {"__tostring", diriter_tostring}, */ {0,0} }; const luaL_reg ex_process_methods[] = { {"__tostring", process_tostring}, +#define ex_process_functions (ex_process_methods + 1) {"wait", process_wait}, {0,0} }; - /* Make all functions available via ex. namespace */ - luaL_register(L, "ex", ex_iolib); /* . ex */ - luaL_register(L, 0, ex_oslib); - luaL_register(L, 0, ex_iofile_methods); - luaL_register(L, 0, ex_process_methods + 1); /* XXX don't insert __tostring */ - lua_replace(L, 1); /* ex . */ + 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"); /* ex . os */ + lua_getglobal(L, "os"); /* . os */ 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 */ - lua_getglobal(L, "io"); /* ex . io */ + lua_getglobal(L, "io"); /* . io */ if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded"); - copy_fields(L, ex_iolib, 1, -1); /* ex . io */ - copy_fields(L, ex_iofile_methods, 1, -1); /* ex . io */ - lua_getfield(L, 1, "pipe"); /* ex . io ex_pipe */ - lua_getfield(L, -2, "stderr"); /* ex . io ex_pipe io_stderr */ - lua_getfenv(L, -1); /* ex . io ex_pipe io_stderr E */ - lua_setfenv(L, -3); /* ex . io ex_pipe io_stderr */ + copyfields(L, ex_iolib, ex, -1); + lua_getfield(L, ex, "pipe"); /* . io ex_pipe */ + 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); /* ex . F */ + luaL_getmetatable(L, LUA_FILEHANDLE); /* . F */ if (lua_isnil(L, -1)) return luaL_error(L, "can't find FILE* metatable"); - copy_fields(L, ex_iofile_methods, 1, -1); /* ex . F */ - - /* 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 */ + copyfields(L, ex_iofile_methods, ex, -1); - lua_settop(L, 1); /* ex */ return 1; }