From 0b41064babf33de0888b1762c2b36b9d08160fb1 Mon Sep 17 00:00:00 2001 From: Mark Edgar Date: Thu, 2 Jul 2009 18:42:20 +0000 Subject: [PATCH] Merge from http://code.google.com/p/lua-ex-api --- COPYRIGHT | 2 +- JUST | 47 +++++++++++++++++++++++++++++++++++++++++++++ README | 2 +- TODO | 6 ++++++ conf.in | 5 ----- posix/Makefile | 2 +- posix/environ.h | 16 +++++++++++++++ posix/ex.c | 11 ++++++----- posix/posix_spawn.c | 4 ++-- posix/posix_spawn.h | 2 +- posix/spawn.c | 5 +++-- posix/spawn.h | 2 +- w32api/dirent.c | 2 +- w32api/dirent.h | 2 +- w32api/ex.c | 8 ++++---- w32api/pusherror.c | 2 +- w32api/pusherror.h | 2 +- w32api/spawn.c | 2 +- w32api/spawn.h | 2 +- 19 files changed, 95 insertions(+), 29 deletions(-) create mode 100755 JUST create mode 100755 TODO create mode 100755 posix/environ.h diff --git a/COPYRIGHT b/COPYRIGHT index fe4d35e..f910540 100755 --- a/COPYRIGHT +++ b/COPYRIGHT @@ -2,7 +2,7 @@ This software is licensed under the terms of the MIT license reproduced below. =============================================================================== -Copyright 2006-2007 Mark Edgar +Copyright 2006-2007 Mark Edgar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/JUST b/JUST new file mode 100755 index 0000000..e3f4720 --- /dev/null +++ b/JUST @@ -0,0 +1,47 @@ +There are several reasons for using the existing ''io'' and ''os'' namespaces. + +The ''io.pipe'' function depends on the Lua core in order to return +newly-constructed file userdata. The ''io.lock'' and ''io.unlock'' functions +are both available as metamethods of file userdata (e.g. ''f:lock("r")''), +which seems a natural extension. + +overloading file metatable, may as well overload io table too + +Adding a function to change environment variables requires that the +implementation of the ''os.getenv'' function be changed, at least on the +Windows implementation. If it is not, then unexpected behavior occurs: + + {{{require "ex" +assert( os.getenv"NEWVAR" == nil ) +ex.setenv("NEWVAR", "value") +print( os.getenv"NEWVAR" ) -- prints "nil" +print( ex.getenv"NEWVAR" ) -- prints "value" +}}} + +Another is that require "ex" changes the semantics of some standard functions, +notably os.remove. + +The ''os.remove'' has similar requirements since on Windows, ''os.remove'' will +only remove non-directories unless ''require "ex"'' replaces its +implementation. + +Even if none of the above implementation details mattered, it is the purpose of +this API to extend the Lua standard namespaces with additional functions. + +It seems there are only two major concerns about using the existing namespaces. +First, there is concern that a future version of Lua might want to use one of +these names. This should not be a concern for one simple reason: this proposal +intends to be that future version. While it may not be the case that this +library would be distributed as part of the Lua core, it is the goal of this +proposal that this extension be recognized to be as much of a standard as +''package.loadlib'' which cannot be implemented in standard C. + +The other major concern is that extending the existing namespaces would be +confusing to Lua users. The best solution to this issue is to make this API +(and its major implementations) a part of the standard Lua distribution. +Similarly to both ''io.popen'' and ''package.loadlib'', the functions proposed +here would be documented in the reference manual and clearly noted that they +are not supported on all platforms. Likewise, these functions would throw an +error when called on platforms which cannot provide an implementation. + +goal: have standard methods when available diff --git a/README b/README index b6bf631..4690fcd 100755 --- a/README +++ b/README @@ -1,6 +1,6 @@ "ex" API implementation http://lua-users.org/wiki/ExtensionProposal -Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > +Copyright 2007 Mark Edgar < medgar at gmail com > -- Environment os.getenv(name) -- get environment variable diff --git a/TODO b/TODO new file mode 100755 index 0000000..6874ffb --- /dev/null +++ b/TODO @@ -0,0 +1,6 @@ +luaL_Buffer usage in posix/spawn.c and w32api/spawn.c is broken (reported by Rici Lake) +w32api/spawn.c spawn_param_args should properly quote arguments according to CommandLineToArgv(quote) (reported by Rici Lake) +os.sleep(math.huge) returns immediately on Windows (reported by David Manura) +os.dir() with no parameter should use the . working directory (reported by David Manura) +Bug in Windows? require "ex"; assert(io.open("234", "w")):lock("w") (reported by David Manura) +run in a tight loop, os.dir() leaks handles faster than garbage collection will collect them (reported by m.i.) diff --git a/conf.in b/conf.in index 3b6b6b2..4a6832e 100755 --- a/conf.in +++ b/conf.in @@ -10,8 +10,3 @@ LUALIB= -L$(LUA)/src -llua51 ### Comment these if your system actually has posix_spawn() POSIX_SPAWN= -DMISSING_POSIX_SPAWN EXTRA= posix_spawn.o - -### Change this if your system properly declares environ. -#ENVIRON= -DENVIRON_DECL= -ENVIRON= -DENVIRON_DECL="extern char **environ;" - diff --git a/posix/Makefile b/posix/Makefile index b83c328..d12c388 100755 --- a/posix/Makefile +++ b/posix/Makefile @@ -1,7 +1,7 @@ include ../conf CFLAGS= $(WARNINGS) $(DEFINES) $(INCLUDES) -DEFINES= -D_XOPEN_SOURCE=600 $(POSIX_SPAWN) $(ENVIRON) +DEFINES= -D_XOPEN_SOURCE=600 $(POSIX_SPAWN) INCLUDES= $(LUAINC) WARNINGS= -W -Wall LIBS= $(LUALIB) diff --git a/posix/environ.h b/posix/environ.h new file mode 100755 index 0000000..c128168 --- /dev/null +++ b/posix/environ.h @@ -0,0 +1,16 @@ +/* + * "ex" API implementation + * http://lua-users.org/wiki/ExtensionProposal + * Copyright 2009 Mark Edgar < medgar123 at gmail com > + */ + +#ifdef __APPLE__ + +#include +#define environ (*_NSGetEnviron()) + +#else + +extern char **environ; + +#endif diff --git a/posix/ex.c b/posix/ex.c index 61deaa2..4399c69 100755 --- a/posix/ex.c +++ b/posix/ex.c @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar123 at gmail com > */ #include #include @@ -10,12 +10,13 @@ #include #include -ENVIRON_DECL #include #include #include #include +#include "environ.h" + #include "lua.h" #include "lualib.h" #include "lauxlib.h" @@ -482,9 +483,9 @@ int luaopen_ex(lua_State *L) if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded"); 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 */ + lua_getfield(L, -2, "open"); /* . io ex_pipe io_open */ + lua_getfenv(L, -1); /* . io ex_pipe io_open E */ + lua_setfenv(L, -3); /* . io ex_pipe io_open */ /* 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"); diff --git a/posix/posix_spawn.c b/posix/posix_spawn.c index 300e20c..fbd957a 100755 --- a/posix/posix_spawn.c +++ b/posix/posix_spawn.c @@ -1,17 +1,17 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #include #include #include #include -ENVIRON_DECL #include #include +#include "environ.h" #include "posix_spawn.h" #ifndef OPEN_MAX diff --git a/posix/posix_spawn.h b/posix/posix_spawn.h index f43f1e2..1b03a6c 100755 --- a/posix/posix_spawn.h +++ b/posix/posix_spawn.h @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #include #include diff --git a/posix/spawn.c b/posix/spawn.c index 8a8b478..fd225e8 100755 --- a/posix/spawn.c +++ b/posix/spawn.c @@ -1,10 +1,9 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #include -ENVIRON_DECL #include #if MISSING_POSIX_SPAWN #include "posix_spawn.h" @@ -12,6 +11,8 @@ ENVIRON_DECL #include #endif +#include "environ.h" + #include "lua.h" #include "lauxlib.h" diff --git a/posix/spawn.h b/posix/spawn.h index 84fb3db..d8b75f1 100755 --- a/posix/spawn.h +++ b/posix/spawn.h @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #ifndef SPAWN_H #define SPAWN_H diff --git a/w32api/dirent.c b/w32api/dirent.c index c9831df..040ac0c 100755 --- a/w32api/dirent.c +++ b/w32api/dirent.c @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #include #include diff --git a/w32api/dirent.h b/w32api/dirent.h index 44c0c10..fc1523a 100755 --- a/w32api/dirent.h +++ b/w32api/dirent.h @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #include diff --git a/w32api/ex.c b/w32api/ex.c index 56e98e8..780bab6 100755 --- a/w32api/ex.c +++ b/w32api/ex.c @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #include #include @@ -541,9 +541,9 @@ int luaopen_ex(lua_State *L) if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded"); 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 */ + lua_getfield(L, -2, "open"); /* . io ex_pipe io_open */ + lua_getfenv(L, -1); /* . io ex_pipe io_open E */ + lua_setfenv(L, -3); /* . io ex_pipe io_open */ /* 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"); diff --git a/w32api/pusherror.c b/w32api/pusherror.c index d656c6a..f78de27 100755 --- a/w32api/pusherror.c +++ b/w32api/pusherror.c @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #include /* sprintf() */ #include diff --git a/w32api/pusherror.h b/w32api/pusherror.h index 6d656de..5b6d949 100755 --- a/w32api/pusherror.h +++ b/w32api/pusherror.h @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #ifndef pusherror_h #define pusherror_h diff --git a/w32api/spawn.c b/w32api/spawn.c index 6ac70b2..479ad3c 100755 --- a/w32api/spawn.c +++ b/w32api/spawn.c @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #include #include diff --git a/w32api/spawn.h b/w32api/spawn.h index 085474d..522a2e7 100755 --- a/w32api/spawn.h +++ b/w32api/spawn.h @@ -1,7 +1,7 @@ /* * "ex" API implementation * http://lua-users.org/wiki/ExtensionProposal - * Copyright 2007 Mark Edgar < medgar at student gc maricopa edu > + * Copyright 2007 Mark Edgar < medgar at gmail com > */ #ifndef SPAWN_H #define SPAWN_H