From a334f9cc33d1dd324dd426fb116164f0dfce2b0d Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Fri, 5 Jan 2024 02:17:55 -0800 Subject: [PATCH] Update redbean ProgramDirectory to return a list of previously set directories (#1021) --- tool/net/help.txt | 7 ++++++- tool/net/redbean.c | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tool/net/help.txt b/tool/net/help.txt index 226d6ee56..595475a53 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -1603,11 +1603,16 @@ FUNCTIONS ProgramGid(int) Same as the -G flag if called from .init.lua for setgid() - ProgramDirectory(str) + ProgramDirectory([directory:str]) → {directory, ...} Same as the -D flag if called from .init.lua for overlaying local file system directories. This may be called multiple times. The first directory programmed is preferred. These currently do not show up in the index page listing. + This call also modifies `package.path` value to either prepend + the added directory in front of the default path (if found) or + to append it (in all other cases). + If no directory is provided, then a table with previously set + directories is returned. ProgramLogMessages(bool) Same as the -m flag if called from .init.lua for logging message diff --git a/tool/net/redbean.c b/tool/net/redbean.c index af8af4016..3f22d95e8 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -4580,6 +4580,18 @@ static int LuaProgramBrand(lua_State *L) { } static int LuaProgramDirectory(lua_State *L) { + size_t i; + // if no parameter is provided, then return current directories + if (lua_isnoneornil(L, 1)) { + lua_newtable(L); + if (stagedirs.n) { + for (i = 0; i < stagedirs.n; ++i) { + lua_pushlstring(L, stagedirs.p[i].s, stagedirs.p[i].n); + lua_seti(L, -2, i + 1); + } + } + return 1; + } struct stat st; const char *path = luaL_checkstring(L, 1); // check to raise a Lua error, to allow it to be handled