diff --git a/orch.lua b/orch.lua index a1da216..7275764 100644 --- a/orch.lua +++ b/orch.lua @@ -398,8 +398,8 @@ execute = function(func, freshctx) end end -local function include_file(file) - local f = assert(impl.open(file)) +local function include_file(file, alter_path) + local f = assert(impl.open(file, alter_path)) local chunk = f:read("*l") -- * for compatibility with Lua 5.2... if not chunk then @@ -713,10 +713,15 @@ function orch_env.write(str) return true end +-- Valid config options: +-- * alter_path: boolean, add script's directory to $PATH (default: false) +-- * command: argv table to pass to spawn function orch.run_script(scriptfile, config) local done - include_file(scriptfile) + -- Note that the orch(1) driver will setup alter_path == true; scripts + -- importing orch.lua are expected to be more explicit. + include_file(scriptfile, config and config.alter_path) --match_ctx_stack:dump() if config and config.command then diff --git a/orch_interp.c b/orch_interp.c index 3a0f1a1..77c810c 100644 --- a/orch_interp.c +++ b/orch_interp.c @@ -102,19 +102,21 @@ orch_interp(const char *scriptf, const char *orch_invoke_path, if (luaL_dofile(L, orch_interp_script(orch_invoke_path)) != LUA_OK) { status = orch_interp_error(L); } else { - int nargs = 1; - /* * orch table is now at the top of stack, fetch run_script() * and call it. run_script(scriptf[, config]) */ lua_getfield(L, -1, "run_script"); lua_pushstring(L, scriptf); - if (argc > 0) { - /* config */ - lua_createtable(L, 0, 1); - nargs++; + /* config */ + lua_createtable(L, 0, 1); + + /* config.alter_path */ + lua_pushboolean(L, 1); + lua_setfield(L, -2, "alter_path"); + + if (argc > 0) { /* config.command */ lua_createtable(L, argc, 0); for (int i = 0; i < argc; i++) { @@ -125,7 +127,7 @@ orch_interp(const char *scriptf, const char *orch_invoke_path, lua_setfield(L, -2, "command"); } - if (lua_pcall(L, nargs, 1, 0) == LUA_OK) + if (lua_pcall(L, 2, 1, 0) == LUA_OK) status = lua_toboolean(L, -1) ? 0 : 1; else status = orch_interp_error(L); diff --git a/orch_lua.c b/orch_lua.c index 657af1d..2ff30be 100644 --- a/orch_lua.c +++ b/orch_lua.c @@ -76,7 +76,7 @@ orchlua_close(lua_State *L) } static int -orchlua_open_init(const char *filename, const char **script) +orchlua_open_init(const char *filename, const char **script, bool alter_path) { assert(!orchlua_cfg.initialized); @@ -106,8 +106,8 @@ orchlua_open_init(const char *filename, const char **script) if (*script == NULL) return (ENOMEM); - /* XXX Should be configurable */ - orchlua_add_execpath(scriptroot); + if (alter_path) + orchlua_add_execpath(scriptroot); orchlua_cfg.dirfd = open(scriptroot, O_DIRECTORY | O_PATH | O_CLOEXEC); @@ -125,16 +125,19 @@ orchlua_open(lua_State *L) luaL_Stream *p; const char *filename, *script; int fd, rvals; + bool alter_path; rvals = 1; filename = luaL_checkstring(L, 1); + alter_path = lua_toboolean(L, 2); script = NULL; /* First open() sets up the sandbox state. */ if (!orchlua_cfg.initialized) { int error; - if ((error = orchlua_open_init(filename, &script)) != 0) { + error = orchlua_open_init(filename, &script, alter_path); + if (error != 0) { luaL_pushfail(L); lua_pushstring(L, strerror(error)); return (true);