Added Lua API interface and -b flag to Launch browser at cmdline or runtime

This commit is contained in:
Rowan 2021-04-16 20:27:40 +01:00
parent bbcca4ba87
commit df9e0bf18f

View file

@ -319,6 +319,7 @@ static bool terminated;
static bool uniprocess; static bool uniprocess;
static bool invalidated; static bool invalidated;
static bool logmessages; static bool logmessages;
static bool launchbrowser;
static bool checkedmethod; static bool checkedmethod;
static bool connectionclose; static bool connectionclose;
static bool keyboardinterrupt; static bool keyboardinterrupt;
@ -618,7 +619,8 @@ static void GetOpts(int argc, char *argv[]) {
logmessages = true; logmessages = true;
break; break;
case 'b': case 'b':
logbodies = true; launchbrowser = true;
// logbodies = true;
break; break;
case 'z': case 'z':
printport = true; printport = true;
@ -1461,6 +1463,22 @@ static bool IsHiddenPath(const char *s) {
return false; return false;
} }
static void LaunchBrowser() {
char openbrowsercommand[255];
char *prog;
if (IsWindows()) {
prog = "explorer";
} else if (IsXnu()) {
prog = "open";
} else {
prog = "xdg-open";
}
snprintf(openbrowsercommand, sizeof(openbrowsercommand),
"%s http://127.0.0.1:%d", prog, ntohs(serveraddr.sin_port));
DEBUGF("Opening browser with command %s\n", openbrowsercommand);
system(openbrowsercommand);
}
static int LuaServeAsset(lua_State *L) { static int LuaServeAsset(lua_State *L) {
size_t pathlen; size_t pathlen;
struct Asset *a; struct Asset *a;
@ -1915,6 +1933,11 @@ static int LuaGetZipPaths(lua_State *L) {
return 1; return 1;
} }
static int LuaLaunchBrowser(lua_State *L) {
LaunchBrowser();
return 1;
}
static void LuaRun(const char *path) { static void LuaRun(const char *path) {
struct Asset *a; struct Asset *a;
const char *code; const char *code;
@ -1954,6 +1977,7 @@ static const luaL_Reg kLuaFuncs[] = {
{"GetUri", LuaGetUri}, // {"GetUri", LuaGetUri}, //
{"GetVersion", LuaGetVersion}, // {"GetVersion", LuaGetVersion}, //
{"GetZipPaths", LuaGetZipPaths}, // {"GetZipPaths", LuaGetZipPaths}, //
{"LaunchBrowser", LuaLaunchBrowser}, //
{"HasParam", LuaHasParam}, // {"HasParam", LuaHasParam}, //
{"HidePath", LuaHidePath}, // {"HidePath", LuaHidePath}, //
{"LoadAsset", LuaLoadAsset}, // {"LoadAsset", LuaLoadAsset}, //
@ -2579,22 +2603,6 @@ static void TuneServerSocket(void) {
LOGIFNEG1(setsockopt(server, IPPROTO_TCP, TCP_QUICKACK, &yes, sizeof(yes))); LOGIFNEG1(setsockopt(server, IPPROTO_TCP, TCP_QUICKACK, &yes, sizeof(yes)));
} }
static void OpenBrowser(const char *serveraddrname) {
char openbrowsercommand[255];
char *prog;
if (IsWindows()) {
prog = "explorer";
} else if (IsXnu()) {
prog = "open";
} else {
prog = "xdg-open";
}
snprintf(openbrowsercommand, sizeof(openbrowsercommand), "%s http://%s", prog,
serveraddrname);
DEBUGF("Opening browser with command %s\n", openbrowsercommand);
system(openbrowsercommand);
}
void RedBean(int argc, char *argv[]) { void RedBean(int argc, char *argv[]) {
uint32_t addrsize; uint32_t addrsize;
gmtoff = GetGmtOffset(); gmtoff = GetGmtOffset();
@ -2647,8 +2655,7 @@ void RedBean(int argc, char *argv[]) {
inbuf.p = xvalloc(inbuf.n); inbuf.p = xvalloc(inbuf.n);
hdrbuf.n = 4 * 1024; hdrbuf.n = 4 * 1024;
hdrbuf.p = xvalloc(hdrbuf.n); hdrbuf.p = xvalloc(hdrbuf.n);
// TODO: Maybe make this an optional argv? if (launchbrowser) LaunchBrowser();
OpenBrowser(serveraddrstr);
while (!terminated) { while (!terminated) {
if (zombied) { if (zombied) {
ReapZombies(); ReapZombies();