Skip to content

Commit 18eced6

Browse files
committed
Add "platform" key to version map.
1 parent 01fd421 commit 18eced6

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
#include <ctime>
1616
#include <algorithm>
1717

18+
#if defined(__APPLE__)
19+
#include <sys/sysctl.h>
20+
#elif defined(_WIN32)
21+
#include <windows.h>
22+
#elif defined(__linux__)
23+
#include <sys/utsname.h>
24+
#include <fstream>
25+
#endif
26+
1827
namespace MiniScript {
1928

2029
String hostName = "";
@@ -849,6 +858,53 @@ namespace MiniScript {
849858
d.SetValue("host", hostVersion);
850859
d.SetValue("hostName", hostName);
851860
d.SetValue("hostInfo", hostInfo);
861+
862+
// Platform detection
863+
String platform;
864+
#if defined(__APPLE__)
865+
platform = "macOS";
866+
char osversion[32];
867+
size_t osversion_len = sizeof(osversion);
868+
if (sysctlbyname("kern.osproductversion", osversion, &osversion_len, NULL, 0) == 0) {
869+
platform = String("macOS ") + osversion;
870+
}
871+
#elif defined(_WIN32)
872+
platform = "Windows";
873+
typedef LONG(WINAPI* RtlGetVersionPtr)(OSVERSIONINFOW*);
874+
HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
875+
if (ntdll) {
876+
RtlGetVersionPtr fn = (RtlGetVersionPtr)GetProcAddress(ntdll, "RtlGetVersion");
877+
if (fn) {
878+
OSVERSIONINFOW osvi = {};
879+
osvi.dwOSVersionInfoSize = sizeof(osvi);
880+
if (fn(&osvi) == 0) {
881+
platform = String("Windows ") + String::Format((int)osvi.dwMajorVersion)
882+
+ "." + String::Format((int)osvi.dwMinorVersion);
883+
}
884+
}
885+
}
886+
#elif defined(__linux__)
887+
platform = "Linux";
888+
{
889+
std::ifstream osrelease("/etc/os-release");
890+
std::string line;
891+
while (std::getline(osrelease, line)) {
892+
if (line.compare(0, 12, "PRETTY_NAME=") == 0) {
893+
std::string val = line.substr(12);
894+
// Strip surrounding quotes
895+
if (val.size() >= 2 && val.front() == '"' && val.back() == '"') {
896+
val = val.substr(1, val.size() - 2);
897+
}
898+
platform = platform + " " + String(val.c_str());
899+
break;
900+
}
901+
}
902+
}
903+
#else
904+
platform = "Unknown";
905+
#endif
906+
d.SetValue("platform", platform);
907+
852908
context->vm->versionMap = Value(d);
853909
}
854910
return IntrinsicResult(context->vm->versionMap);

0 commit comments

Comments
 (0)