lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've tried to come up with ways to execute commands without leaving
scripts open to shell-special syntax hacks.

I've created my own value-added "PosixExec" pseudo-module (actually,
just a single Lua executable script that can go with, and probably
should be wrapped up as, a LuaRocks item).  It builds on luaposix,
especially subprocess-spawn facilities, such as dup2(), pipe(),
fork(), setenv(), cwd(), glob(), and ultimately execp().

The parent then uses poll(), read(), write() and, finally, wait()
to work with child stdin/stdout/stderr, receiving "hup" signal from
the I/O streams and to get final exit status.

The PosixExec script is a value-added version of the fork() example
in luaposix.


You can find it used extensively in my "lglicua" SourceForge project,
where I use it as a shim for many things, e.g. "apt-get".

The general invocation is:

	local PE = require("PosixExec")
	local Result

	Result = PE.exec_quietly("ls", {"-al"})

The first parameter, a string, is found (or not) using the shell's
PATH; the remaining arguments are untouched (although an extra
Options item brings file globbing back into play, for one argument

Both "PE.exec" and "PE.exec_quietly" return a Result table,
which especially errno is inspected by ValidateRun:

        return {
                stdout = table.concat(ThisExec.FDs[fd1].Output),
                stderr = table.concat(ThisExec.FDs[fd2].Output),
                errno  = DemiseErrno,
                errstr = DemiseErrstr,
                Command = Command,

An extra Options table on the exec* calls lets you do thing like:

	- Set the working directory (cd) for the command;

	- Merge child's stderr (fd[2]) into stdout(fd1) at the
	  child's level, so the streams are synchronous, not
	  asynchronous (similar to the shell's "2>&1" file

	- Set a list of environment to (optional) values, e.g.:
		{"LC_ALL", "C"},

	- Nominate various parameter(s) for Shell-style file
	  globbing, as this is one of the things deliberately
	  disabled, but which is still very useful for some


You can get PosixExec from the following lglicua tarball (earlier
releases than -alpha6 might have fewer features):


I have only tried PosixExec under GNU/Linux because:
     (a) That was what lglicua was all about; and
     (b) I don't have a workable Windows installation to use.


Hope this helps,

sur-behoffski (Brenton Hoff)
programmer, Grouse Software