diff options
| author | Stefan Boberg <[email protected]> | 2026-04-23 18:16:57 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2026-04-23 18:16:57 +0200 |
| commit | 0232b991cd7d8e3a2114ea30e4591dd3e7b65c36 (patch) | |
| tree | 94730e7594fd09ae1fa820391ce311f6daf13905 /src/zen/browser_launcher.cpp | |
| parent | Fix forward declaration order for s_GotSigWinch and SigWinchHandler (diff) | |
| parent | trace: declare Region event name fields as AnsiString (#1012) (diff) | |
| download | archived-zen-sb/zen-help.tar.xz archived-zen-sb/zen-help.zip | |
Merge branch 'main' into sb/zen-helpsb/zen-help
- Combine HelpCommand (this branch) with HistoryCommand (main) in zen CLI dispatcher
- Keep filter-aware TuiPickOne rewrite; adopt main's ASCII arrow glyphs in doc comment
Diffstat (limited to 'src/zen/browser_launcher.cpp')
| -rw-r--r-- | src/zen/browser_launcher.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/zen/browser_launcher.cpp b/src/zen/browser_launcher.cpp new file mode 100644 index 000000000..a115bf46a --- /dev/null +++ b/src/zen/browser_launcher.cpp @@ -0,0 +1,71 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include "browser_launcher.h" + +#include <zenbase/zenbase.h> +#include <zencore/except_fmt.h> +#include <zencore/logging.h> + +#include <stdexcept> +#include <string> + +#if ZEN_PLATFORM_WINDOWS +# include <zencore/windows.h> +# include <shellapi.h> +#else +# include <spawn.h> +# include <sys/wait.h> +extern char** environ; +#endif + +namespace zen { + +void +LaunchBrowser(std::string_view Url) +{ + if (Url.empty()) + { + throw zen::runtime_error("Cannot launch browser with empty URL"); + } + + bool Success = false; + +#if ZEN_PLATFORM_WINDOWS + std::string UrlZ(Url); + HINSTANCE Result = ShellExecuteA(nullptr, "open", UrlZ.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + Success = reinterpret_cast<intptr_t>(Result) > 32; +#else +# if ZEN_PLATFORM_MAC + const char* Program = "open"; +# elif ZEN_PLATFORM_LINUX + const char* Program = "xdg-open"; +# else + ZEN_NOT_IMPLEMENTED("Browser launching not implemented on this platform"); + const char* Program = nullptr; +# endif + + // Spawn directly via posix_spawnp to avoid the shell entirely, so URL contents + // cannot be interpreted as shell syntax regardless of what characters they contain. + std::string Url_c(Url); + char* const Argv[] = {const_cast<char*>(Program), Url_c.data(), nullptr}; + pid_t Pid = 0; + const int SpawnRc = posix_spawnp(&Pid, Program, nullptr, nullptr, Argv, environ); + if (SpawnRc == 0) + { + int Status = 0; + if (waitpid(Pid, &Status, 0) == Pid) + { + Success = WIFEXITED(Status) && WEXITSTATUS(Status) == 0; + } + } +#endif + + if (!Success) + { + throw zen::runtime_error("Failed to launch browser for '{}'", Url); + } + + ZEN_CONSOLE("Web browser launched for '{}' successfully", Url); +} + +} // namespace zen |