| 143 | } |
| 144 | |
| 145 | void CommandHelp::executeHelp() { |
| 146 | // On windows open the html pages with the browser |
| 147 | // On linux/macos open the man pages with man |
| 148 | |
| 149 | #if defined(_WIN32) |
| 150 | DWORD size = 256; |
| 151 | std::wstring executablePath; |
| 152 | do { |
| 153 | size *= 2; |
| 154 | executablePath.resize(size); |
| 155 | } while (GetModuleFileNameW(nullptr, executablePath.data(), size) == size); |
| 156 | |
| 157 | PathCchRemoveFileSpec(executablePath.data(), executablePath.size()); |
| 158 | executablePath.resize(wcslen(executablePath.c_str())); |
| 159 | |
| 160 | const auto commandStr = options.command.value_or(""); |
| 161 | const auto systemCommand = fmt::format(L"{}\\..\\share\\doc\\KTX-Software\\html\\ktxtools\\ktx{}{}.html", |
| 162 | executablePath, |
| 163 | options.command ? L"_" : L"", |
| 164 | std::wstring(commandStr.begin(), commandStr.end())); |
| 165 | |
| 166 | auto result = ShellExecuteW(nullptr, nullptr, systemCommand.c_str(), nullptr, nullptr, SW_SHOWNORMAL); |
| 167 | auto r = reinterpret_cast<INT_PTR>(result); |
| 168 | if (r <= 32) // WinAPI is weird |
| 169 | fatal(rc::RUNTIME_ERROR, "Failed to open the html documentation: ERROR {}", r); |
| 170 | |
| 171 | #else |
| 172 | # if defined(__APPLE__) |
| 173 | char buf[PATH_MAX]; |
| 174 | uint32_t bufsize = PATH_MAX; |
| 175 | if (const auto ec = _NSGetExecutablePath(buf, &bufsize)) |
| 176 | fatal(rc::RUNTIME_ERROR, "Failed to determine executable path: ERROR {}", ec); |
| 177 | const auto executablePath = std::filesystem::canonical(buf); |
| 178 | # else // Linux |
| 179 | const auto executablePath = std::filesystem::canonical("/proc/self/exe"); |
| 180 | # endif |
| 181 | |
| 182 | const auto executableDir = std::filesystem::path(executablePath).remove_filename(); |
| 183 | const auto manFile = fmt::format("{}/../share/man/man1/ktx{}{}.1", |
| 184 | executableDir.string(), |
| 185 | options.command ? "_" : "", |
| 186 | options.command.value_or("")); |
| 187 | if (std::filesystem::exists(manFile)) { |
| 188 | // We have relative access to the man file, prioritize opening it |
| 189 | // that way to support custom install locations |
| 190 | const auto systemCommand = fmt::format("man \"{}\"", manFile); |
| 191 | const auto result = std::system(systemCommand.c_str()); |
| 192 | (void) result; |
| 193 | } else { |
| 194 | const auto systemCommand = fmt::format("man ktx{}{}", |
| 195 | options.command ? "_" : "", |
| 196 | options.command.value_or("")); |
| 197 | const auto result = std::system(systemCommand.c_str()); |
| 198 | (void) result; |
| 199 | } |
| 200 | #endif |
| 201 | } |
| 202 | |