mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Fix the behavior of redbean -i -e CODE
When redbean is functioning as a Lua interpreter, the `-e` flag should behave the same way as other open source language interpreters. Namely it should exit after evaluating the code rather than showing the REPL.
This commit is contained in:
parent
614229e3f4
commit
497d4fa6d1
2 changed files with 16 additions and 8 deletions
|
@ -46,8 +46,6 @@ FLAGS
|
||||||
-b log message bodies
|
-b log message bodies
|
||||||
-a log resource usage
|
-a log resource usage
|
||||||
-g log handler latency
|
-g log handler latency
|
||||||
-e eval Lua code in arg
|
|
||||||
-F eval Lua code in file
|
|
||||||
-E show crash reports to public ips
|
-E show crash reports to public ips
|
||||||
-j enable ssl client verify
|
-j enable ssl client verify
|
||||||
-k disable ssl fetch verify
|
-k disable ssl fetch verify
|
||||||
|
@ -62,6 +60,8 @@ FLAGS
|
||||||
-v increase verbosity [repeatable]
|
-v increase verbosity [repeatable]
|
||||||
-V increase ssl verbosity [repeatable]
|
-V increase ssl verbosity [repeatable]
|
||||||
-S increase pledge sandboxing [repeatable]
|
-S increase pledge sandboxing [repeatable]
|
||||||
|
-e CODE eval Lua code in arg [repeatable]
|
||||||
|
-F PATH eval Lua code in file [repeatable]
|
||||||
-H K:V sets http header globally [repeatable]
|
-H K:V sets http header globally [repeatable]
|
||||||
-D DIR overlay assets in local directory [repeatable]
|
-D DIR overlay assets in local directory [repeatable]
|
||||||
-r /X=/Y redirect X to Y [repeatable]
|
-r /X=/Y redirect X to Y [repeatable]
|
||||||
|
|
|
@ -1166,22 +1166,24 @@ static void LogLuaError(char *hook, char *err) {
|
||||||
ERRORF("(lua) failed to run %s: %s", hook, err);
|
ERRORF("(lua) failed to run %s: %s", hook, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool LuaEvalCode(const char *code) {
|
// handles `-e CODE` (frontloads web server code)
|
||||||
|
// handles `-i -e CODE` (interprets expression and exits)
|
||||||
|
static void LuaEvalCode(const char *code) {
|
||||||
lua_State *L = GL;
|
lua_State *L = GL;
|
||||||
int status = luaL_loadstring(L, code);
|
int status = luaL_loadstring(L, code);
|
||||||
if (status != LUA_OK || LuaCallWithTrace(L, 0, 0, NULL) != LUA_OK) {
|
if (status != LUA_OK || LuaCallWithTrace(L, 0, 0, NULL) != LUA_OK) {
|
||||||
LogLuaError("lua code", lua_tostring(L, -1));
|
LogLuaError("lua code", lua_tostring(L, -1));
|
||||||
lua_pop(L, 1); // pop error
|
lua_pop(L, 1); // pop error
|
||||||
return false;
|
exit(1);
|
||||||
}
|
}
|
||||||
AssertLuaStackIsAt(L, 0);
|
AssertLuaStackIsAt(L, 0);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool LuaEvalFile(const char *path) {
|
// handle `-F PATH` arg
|
||||||
|
static void LuaEvalFile(const char *path) {
|
||||||
char *f = _gc(xslurp(path, 0));
|
char *f = _gc(xslurp(path, 0));
|
||||||
if (!f) FATALF("(cfg) error: failed to read file %`'s", path);
|
if (!f) FATALF("(cfg) error: failed to read file %`'s", path);
|
||||||
return LuaEvalCode(f);
|
LuaEvalCode(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool LuaOnClientConnection(void) {
|
static bool LuaOnClientConnection(void) {
|
||||||
|
@ -7256,6 +7258,7 @@ static void TlsDestroy(void) {
|
||||||
|
|
||||||
static void GetOpts(int argc, char *argv[]) {
|
static void GetOpts(int argc, char *argv[]) {
|
||||||
int opt;
|
int opt;
|
||||||
|
bool got_e_arg = false;
|
||||||
bool storeasset = false;
|
bool storeasset = false;
|
||||||
// only generate ecp cert under blinkenlights (rsa is slow)
|
// only generate ecp cert under blinkenlights (rsa is slow)
|
||||||
norsagen = IsGenuineBlink();
|
norsagen = IsGenuineBlink();
|
||||||
|
@ -7304,11 +7307,14 @@ static void GetOpts(int argc, char *argv[]) {
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
#ifndef STATIC
|
#ifndef STATIC
|
||||||
CASE('e', LuaEvalCode(optarg));
|
|
||||||
CASE('F', LuaEvalFile(optarg));
|
CASE('F', LuaEvalFile(optarg));
|
||||||
CASE('*', selfmodifiable = true);
|
CASE('*', selfmodifiable = true);
|
||||||
CASE('i', interpretermode = true);
|
CASE('i', interpretermode = true);
|
||||||
CASE('E', leakcrashreports = true);
|
CASE('E', leakcrashreports = true);
|
||||||
|
case 'e':
|
||||||
|
got_e_arg = true;
|
||||||
|
LuaEvalCode(optarg);
|
||||||
|
break;
|
||||||
case 'A':
|
case 'A':
|
||||||
if (!storeasset) {
|
if (!storeasset) {
|
||||||
storeasset = true;
|
storeasset = true;
|
||||||
|
@ -7332,6 +7338,8 @@ static void GetOpts(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
// if storing asset(s) is requested, don't need to continue
|
// if storing asset(s) is requested, don't need to continue
|
||||||
if (storeasset) exit(0);
|
if (storeasset) exit(0);
|
||||||
|
// we don't want to drop into a repl after using -e in -i mode
|
||||||
|
if (interpretermode && got_e_arg) exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RedBean(int argc, char *argv[]) {
|
void RedBean(int argc, char *argv[]) {
|
||||||
|
|
Loading…
Reference in a new issue