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 | 16 | void gate_main_init() | |
36 | { | ||
37 | 16 | gate_platform_init(); | |
38 | 16 | } | |
39 | |||
40 | 16 | void gate_main_uninit() | |
41 | { | ||
42 | |||
43 | 16 | } | |
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_main_init(); | ||
171 | |||
172 | return gate_wasm_start_main(); | ||
173 | } | ||
174 | |||
175 | #endif /* GATE_MAIN_WASM_IMPL */ | ||
176 | |||
177 | |||
178 | |||
179 | #if defined(GATE_MAIN_DOS_IMPL) | ||
180 | |||
181 | #define GATE_MAIN_NO_STDC_IMPL 1 | ||
182 | |||
183 | #include "process.h" | ||
184 | |||
185 | static char near main_command_path[256]; | ||
186 | static char near main_command_argline[256]; | ||
187 | static char const* main_command_args[32]; | ||
188 | |||
189 | int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]) | ||
190 | { | ||
191 | int ret_code; | ||
192 | |||
193 | char* start; | ||
194 | char* ptr; | ||
195 | gate_size_t cmdline_len; | ||
196 | gate_size_t args_count = 0; | ||
197 | gate_uintptr_t apphandle = 0; | ||
198 | |||
199 | GATE_UNUSED_ARG(argc); | ||
200 | GATE_UNUSED_ARG(argv); | ||
201 | |||
202 | _cmdname(main_command_path); | ||
203 | getcmd(main_command_argline); | ||
204 | |||
205 | cmdline_len = gate_str_length(main_command_argline); | ||
206 | ptr = start = main_command_argline; | ||
207 | while (cmdline_len-- > 0) | ||
208 | { | ||
209 | if (*ptr == ' ') | ||
210 | { | ||
211 | *ptr = '\0'; | ||
212 | if (ptr != start) | ||
213 | { | ||
214 | main_command_args[args_count++] = start; | ||
215 | } | ||
216 | start = ptr + 1; | ||
217 | } | ||
218 | ++ptr; | ||
219 | } | ||
220 | if (ptr != start) | ||
221 | { | ||
222 | main_command_args[args_count++] = start; | ||
223 | } | ||
224 | |||
225 | gate_dos_register_app_path(main_command_path); | ||
226 | |||
227 | gate_main_init(); | ||
228 | |||
229 | ret_code = main_func(main_command_path, &main_command_args[0], args_count, apphandle); | ||
230 | |||
231 | gate_main_uninit(); | ||
232 | |||
233 | return ret_code; | ||
234 | } | ||
235 | |||
236 | #endif /* GATE_MAIN_DOS_IMPL */ | ||
237 | |||
238 | |||
239 | |||
240 | #if defined(GATE_MAIN_EFI_IMPL) | ||
241 | # define GATE_MAIN_NO_STDC_IMPL 1 | ||
242 | # include <lib.h> | ||
243 | # include "gate/platform/efi/efi_gate.h" | ||
244 | |||
245 | int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]) | ||
246 | { | ||
247 | int ret_code; | ||
248 | gate_uintptr_t apphandle = 0; | ||
249 | |||
250 | gate_main_init(); | ||
251 | gate_platform_efi_init(LibImageHandle, ST); | ||
252 | |||
253 | ret_code = gate_platform_efi_run_main(main_func); | ||
254 | |||
255 | gate_main_uninit(); | ||
256 | |||
257 | return ret_code; | ||
258 | } | ||
259 | |||
260 | #endif /* GATE_MAIN_EFI_IMPL */ | ||
261 | |||
262 | |||
263 | |||
264 | #if defined(GATE_MAIN_ANDROID_IMPL) | ||
265 | # define GATE_MAIN_NO_STDC_IMPL 1 | ||
266 | # include <android/native_activity.h> | ||
267 | # include "gate/platform/android/gate_android_app.h" | ||
268 | |||
269 | void GATE_CORE_API gate_main_android_entry(gate_main_func main_func, void* activity, void* savedState, size_t savedStateSize) | ||
270 | { | ||
271 | gate_result_t result = gate_platform_init(); | ||
272 | gate_android_app_create((ANativeActivity*)activity, savedState, savedStateSize, main_func); | ||
273 | } | ||
274 | |||
275 | #endif /* GATE_MAIN_ANDROID_IMPL */ | ||
276 | |||
277 | |||
278 | |||
279 | #if !defined(GATE_MAIN_NO_STDC_IMPL) | ||
280 | |||
281 | 16 | int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]) | |
282 | { | ||
283 | int ret_code; | ||
284 | 16 | gate_uintptr_t apphandle = 0; | |
285 | |||
286 | #if defined(GATE_SYS_WIN) | ||
287 | apphandle = (gate_uintptr_t)gate_win32_get_hinst(); | ||
288 | gate_win32_set_hinst((HINSTANCE)apphandle); | ||
289 | #endif | ||
290 | |||
291 | 16 | gate_main_init(); | |
292 | |||
293 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | ret_code = main_func(argv[0], (argc <= 1) ? NULL : (char const* const*)&argv[1], (argc <= 1) ? 0 : (argc - 1), apphandle); |
294 | |||
295 | 16 | gate_main_uninit(); | |
296 | |||
297 | 16 | return ret_code; | |
298 | } | ||
299 | |||
300 | #endif | ||
301 | |||
302 |