| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/gatemaindefs.h" | ||
| 30 | #include "gate/platforms.h" | ||
| 31 | #include "gate/strings.h" | ||
| 32 | |||
| 33 | /* generic implementation: */ | ||
| 34 | |||
| 35 | 33 | void gate_main_init() | |
| 36 | { | ||
| 37 | 33 | gate_platform_init(); | |
| 38 | 33 | } | |
| 39 | |||
| 40 | 31 | void gate_main_uninit() | |
| 41 | { | ||
| 42 | |||
| 43 | 31 | } | |
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | #if defined(GATE_MAIN_WINAPI_IMPL) | ||
| 48 | |||
| 49 | static int gate_main_winapi_entry_impl(gate_main_func main_func, HINSTANCE hInstance, LPCWSTR cmdline) | ||
| 50 | { | ||
| 51 | static TCHAR nativeAppPath[2048]; | ||
| 52 | static DWORD nativeAppPathUsed; | ||
| 53 | static char appPath[2048]; | ||
| 54 | static char cmdLineBuffer[8192]; | ||
| 55 | static gate_size_t cmdLineBufferLen = sizeof(cmdLineBuffer); | ||
| 56 | gate_uintptr_t apphandle = (gate_uintptr_t)hInstance; | ||
| 57 | char const* cmdArgs[192]; | ||
| 58 | gate_size_t cmdArgCount = sizeof(cmdArgs) / sizeof(cmdArgs[0]); | ||
| 59 | gate_size_t cmdArgsUsed = 0; | ||
| 60 | int ret; | ||
| 61 | |||
| 62 | gate_win32_set_hinst(hInstance); | ||
| 63 | |||
| 64 | gate_main_init(); | ||
| 65 | |||
| 66 | if (cmdline != NULL) | ||
| 67 | { | ||
| 68 | cmdArgsUsed = gate_win32_parse_command_line(cmdline, &cmdLineBuffer[0], cmdLineBufferLen, cmdArgs, cmdArgCount); | ||
| 69 | } | ||
| 70 | if (cmdArgsUsed == 0) | ||
| 71 | { | ||
| 72 | cmdArgs[1] = 0; | ||
| 73 | cmdArgsUsed = 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | nativeAppPathUsed = GetModuleFileName((HMODULE)hInstance, nativeAppPath, sizeof(nativeAppPath) / sizeof(nativeAppPath[0]) - 1); | ||
| 77 | gate_win32_winstr_2_utf8(nativeAppPath, nativeAppPathUsed, appPath, sizeof(appPath)); | ||
| 78 | |||
| 79 | ret = main_func(appPath, (cmdArgsUsed <= 1) ? NULL : &cmdArgs[1], cmdArgsUsed - 1, apphandle); | ||
| 80 | |||
| 81 | gate_main_uninit(); | ||
| 82 | return ret; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | int GATE_CORE_API gate_main_winapi_entry(gate_main_func main_func, void* hInstance, void* hPrevInstance, void* lpCmdLine, int nShowCmd) | ||
| 87 | { | ||
| 88 | #if defined(GATE_SYS_WIN16) | ||
| 89 | LPTSTR tcmd_line = (LPTSTR)lpCmdLine; | ||
| 90 | #else | ||
| 91 | LPTSTR tcmd_line = GetCommandLine(); | ||
| 92 | #endif | ||
| 93 | |||
| 94 | #if defined(GATE_WIN32_UNICODE) | ||
| 95 | LPWSTR wcmd_line = tcmd_line; | ||
| 96 | #else | ||
| 97 | static WCHAR wcmd_line[8192]; | ||
| 98 | gate_size_t tcmd_line_length = gate_str_length(tcmd_line); | ||
| 99 | |||
| 100 | gate_win32_ansi_2_utf16(tcmd_line, tcmd_line_length, (wchar_t*)&wcmd_line[0], sizeof(wcmd_line) / sizeof(wcmd_line[0])); | ||
| 101 | #endif | ||
| 102 | GATE_UNUSED_ARG(hPrevInstance); | ||
| 103 | GATE_UNUSED_ARG(lpCmdLine); | ||
| 104 | GATE_UNUSED_ARG(nShowCmd); | ||
| 105 | return gate_main_winapi_entry_impl(main_func, (HINSTANCE)hInstance, wcmd_line); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | int GATE_CORE_API gate_main_wchar_entry(gate_main_func main_func, int argc, wchar_t* argv[], wchar_t* envp[]) | ||
| 110 | { | ||
| 111 | int ret = 0; | ||
| 112 | gate_uintptr_t apphandle = 0; | ||
| 113 | char* c_args[1024] = GATE_INIT_EMPTY; | ||
| 114 | gate_size_t ndx; | ||
| 115 | gate_size_t len; | ||
| 116 | |||
| 117 | GATE_UNUSED_ARG(envp); | ||
| 118 | |||
| 119 | #if !defined(GATE_SYS_WINSTORE) | ||
| 120 | apphandle = (gate_uintptr_t)gate_win32_get_hinst(); | ||
| 121 | gate_win32_set_hinst((HINSTANCE)apphandle); | ||
| 122 | #endif | ||
| 123 | |||
| 124 | gate_main_init(); | ||
| 125 | |||
| 126 | if (argc >= sizeof(c_args) / sizeof(c_args[0])) | ||
| 127 | { | ||
| 128 | argc = sizeof(c_args) / sizeof(c_args[0]) - 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | for (ndx = 0; ndx != argc; ++ndx) | ||
| 132 | { | ||
| 133 | len = gate_str16_length(argv[ndx]); | ||
| 134 | c_args[ndx] = (char*)gate_mem_alloc(len * 2); | ||
| 135 | gate_str_utf16_2_utf8(argv[ndx], len, c_args[ndx], len * 2); | ||
| 136 | } | ||
| 137 | |||
| 138 | ret = main_func(c_args[0], (argc <= 1) ? NULL : (char const* const*)&c_args[1], (argc < 1) ? 0 : (argc - 1), apphandle); | ||
| 139 | |||
| 140 | for (ndx = 0; ndx != argc; ++ndx) | ||
| 141 | { | ||
| 142 | if (c_args[ndx] != NULL) | ||
| 143 | { | ||
| 144 | gate_mem_dealloc(c_args[ndx]); | ||
| 145 | c_args[ndx] = NULL; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | gate_main_uninit(); | ||
| 150 | |||
| 151 | return ret; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | #endif /* GATE_MAIN_WINAPI_IMPL */ | ||
| 156 | |||
| 157 | #if defined(GATE_MAIN_WASM_IMPL) | ||
| 158 | |||
| 159 | #define GATE_MAIN_NO_STDC_IMPL 1 | ||
| 160 | |||
| 161 | #include "gate/results.h" | ||
| 162 | #include "gate/platform/wasm/wasm_gate.h" | ||
| 163 | #include <stdio.h> | ||
| 164 | |||
| 165 | int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]) | ||
| 166 | { | ||
| 167 | (void)argc; | ||
| 168 | (void)argv; | ||
| 169 | gate_wasm_register_gate_main(main_func); | ||
| 170 | gate_wasm_register_gate_main_program_name(argv[0]); | ||
| 171 | gate_main_init(); | ||
| 172 | |||
| 173 | return gate_wasm_start_main(); | ||
| 174 | } | ||
| 175 | |||
| 176 | #endif /* GATE_MAIN_WASM_IMPL */ | ||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | #if defined(GATE_MAIN_DOS_IMPL) | ||
| 181 | |||
| 182 | #define GATE_MAIN_NO_STDC_IMPL 1 | ||
| 183 | |||
| 184 | #include "process.h" | ||
| 185 | |||
| 186 | static char near main_command_path[256]; | ||
| 187 | static char near main_command_argline[256]; | ||
| 188 | static char const* main_command_args[32]; | ||
| 189 | |||
| 190 | int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]) | ||
| 191 | { | ||
| 192 | int ret_code; | ||
| 193 | |||
| 194 | char* start; | ||
| 195 | char* ptr; | ||
| 196 | gate_size_t cmdline_len; | ||
| 197 | gate_size_t args_count = 0; | ||
| 198 | gate_uintptr_t apphandle = 0; | ||
| 199 | |||
| 200 | GATE_UNUSED_ARG(argc); | ||
| 201 | GATE_UNUSED_ARG(argv); | ||
| 202 | |||
| 203 | _cmdname(main_command_path); | ||
| 204 | getcmd(main_command_argline); | ||
| 205 | |||
| 206 | cmdline_len = gate_str_length(main_command_argline); | ||
| 207 | ptr = start = main_command_argline; | ||
| 208 | while (cmdline_len-- > 0) | ||
| 209 | { | ||
| 210 | if (*ptr == ' ') | ||
| 211 | { | ||
| 212 | *ptr = '\0'; | ||
| 213 | if (ptr != start) | ||
| 214 | { | ||
| 215 | main_command_args[args_count++] = start; | ||
| 216 | } | ||
| 217 | start = ptr + 1; | ||
| 218 | } | ||
| 219 | ++ptr; | ||
| 220 | } | ||
| 221 | if (ptr != start) | ||
| 222 | { | ||
| 223 | main_command_args[args_count++] = start; | ||
| 224 | } | ||
| 225 | |||
| 226 | gate_dos_register_app_path(main_command_path); | ||
| 227 | |||
| 228 | gate_main_init(); | ||
| 229 | |||
| 230 | ret_code = main_func(main_command_path, &main_command_args[0], args_count, apphandle); | ||
| 231 | |||
| 232 | gate_main_uninit(); | ||
| 233 | |||
| 234 | return ret_code; | ||
| 235 | } | ||
| 236 | |||
| 237 | #endif /* GATE_MAIN_DOS_IMPL */ | ||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | #if defined(GATE_MAIN_EFI_IMPL) | ||
| 242 | # define GATE_MAIN_NO_STDC_IMPL 1 | ||
| 243 | # include <lib.h> | ||
| 244 | # include "gate/platform/efi/efi_gate.h" | ||
| 245 | |||
| 246 | int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]) | ||
| 247 | { | ||
| 248 | int ret_code; | ||
| 249 | gate_uintptr_t apphandle = 0; | ||
| 250 | |||
| 251 | gate_main_init(); | ||
| 252 | gate_platform_efi_init(LibImageHandle, ST); | ||
| 253 | |||
| 254 | ret_code = gate_platform_efi_run_main(main_func); | ||
| 255 | |||
| 256 | gate_main_uninit(); | ||
| 257 | |||
| 258 | return ret_code; | ||
| 259 | } | ||
| 260 | |||
| 261 | #endif /* GATE_MAIN_EFI_IMPL */ | ||
| 262 | |||
| 263 | |||
| 264 | |||
| 265 | #if defined(GATE_MAIN_ANDROID_IMPL) | ||
| 266 | # define GATE_MAIN_NO_STDC_IMPL 1 | ||
| 267 | # include <android/native_activity.h> | ||
| 268 | # include "gate/platform/android/gate_android_app.h" | ||
| 269 | |||
| 270 | void GATE_CORE_API gate_main_android_entry(gate_main_func main_func, void* activity, void* savedState, size_t savedStateSize) | ||
| 271 | { | ||
| 272 | gate_result_t result = gate_platform_init(); | ||
| 273 | gate_android_app_create((ANativeActivity*)activity, savedState, savedStateSize, main_func); | ||
| 274 | } | ||
| 275 | |||
| 276 | #endif /* GATE_MAIN_ANDROID_IMPL */ | ||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | #if !defined(GATE_MAIN_NO_STDC_IMPL) | ||
| 281 | |||
| 282 | 33 | int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]) | |
| 283 | { | ||
| 284 | int ret_code; | ||
| 285 | 33 | gate_uintptr_t apphandle = 0; | |
| 286 | |||
| 287 | #if defined(GATE_SYS_WIN) | ||
| 288 | apphandle = (gate_uintptr_t)gate_win32_get_hinst(); | ||
| 289 | gate_win32_set_hinst((HINSTANCE)apphandle); | ||
| 290 | #endif | ||
| 291 | |||
| 292 | 33 | gate_main_init(); | |
| 293 | |||
| 294 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 20 times.
|
33 | ret_code = main_func(argv[0], (argc <= 1) ? NULL : (char const* const*)&argv[1], (argc <= 1) ? 0 : (argc - 1), apphandle); |
| 295 | |||
| 296 | 31 | gate_main_uninit(); | |
| 297 | |||
| 298 | 31 | return ret_code; | |
| 299 | } | ||
| 300 | |||
| 301 | #endif | ||
| 302 | |||
| 303 |