From 6bbb44c165af24644e677caa80107b41a2b77926 Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Sat, 7 Aug 2021 14:25:55 -0700 Subject: [PATCH] Add GetHostOs to redbean (#228) --- tool/net/help.txt | 3 +++ tool/net/redbean.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/tool/net/help.txt b/tool/net/help.txt index c51893cc1..bc0516282 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -639,6 +639,9 @@ FUNCTIONS Returns host associated with request. This will be the Host header, if it's supplied. Otherwise it's the bind address. + GetHostOs() → str + Returns string that describes the host OS. + GetMonospaceWidth(str|char) → int Returns monospace display width of string. This is useful for fixed-width formatting. For example, CJK characters typically take diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 62a5ef794..44b8f59da 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -5035,6 +5035,32 @@ static int LuaGetComment(lua_State *L) { return 1; } + +static int LuaGetHostOs(lua_State *L) { + const char *s = NULL; + if (IsLinux()) { + s = "LINUX"; + } else if (IsMetal()) { + s = "METAL"; + } else if (IsWindows()) { + s = "WINDOWS"; + } else if (IsXnu()) { + s = "XNU"; + } else if (IsOpenbsd()) { + s = "OPENBSD"; + } else if (IsFreebsd()) { + s = "FREEBSD"; + } else if (IsNetbsd()) { + s = "NETBSD"; + } + if (s) { + lua_pushstring(L, s); + } else { + lua_pushnil(L); + } + return 1; +} + static void LuaSetIntField(lua_State *L, const char *k, lua_Integer v) { lua_pushinteger(L, v); lua_setfield(L, -2, k); @@ -5194,6 +5220,7 @@ static bool LuaRun(const char *path, bool mandatory) { } else { WARNF("%s %s", path, lua_tostring(L, -1)); } + lua_pop(L, 1); } free(code); } @@ -5236,6 +5263,7 @@ static const luaL_Reg kLuaFuncs[] = { {"GetHeader", LuaGetHeader}, // {"GetHeaders", LuaGetHeaders}, // {"GetHost", LuaGetHost}, // + {"GetHostOs", LuaGetHostOs}, // {"GetHttpReason", LuaGetHttpReason}, // {"GetHttpVersion", LuaGetHttpVersion}, // {"GetLastModifiedTime", LuaGetLastModifiedTime}, //