GCC Code Coverage Report


Directory: src/gate/
File: src/gate/gatemain.c
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 11 11 100.0%
Functions: 3 3 100.0%
Branches: 2 2 100.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, 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
32 /* generic implementation: */
33
34 33 void gate_main_init()
35 {
36 33 gate_platform_init();
37 33 }
38
39 31 void gate_main_uninit()
40 {
41
42 31 }
43
44
45
46 #if defined(GATE_MAIN_WINAPI_IMPL)
47
48 #include "gate/strings.h"
49
50 GATE_CORE_API int GATE_CALL gate_main_wchar_entry(gate_main_func main_func, int argc, wchar_t* argv[], wchar_t* envp[]);
51 GATE_CORE_API int GATE_CALL gate_main_winapi_entry(gate_main_func main_func, void* hInstance, void* hPrevInstance, void* lpCmdLine, int nShowCmd);
52
53 #if defined(GATE_SYS_WIN16)
54 # define GATE_MAX_APP_PATH_LEN 260
55 # define GATE_MAX_APP_ARGS_LEN 260
56 #else
57 # define GATE_MAX_APP_PATH_LEN 2048
58 # define GATE_MAX_APP_ARGS_LEN 8192
59 #endif
60
61 static int gate_main_winapi_entry_impl(gate_main_func main_func, HINSTANCE hInstance, LPCWSTR cmdline)
62 {
63 static TCHAR nativeAppPath[GATE_MAX_APP_PATH_LEN];
64 static DWORD nativeAppPathUsed;
65 static char appPath[GATE_MAX_APP_PATH_LEN];
66 static char cmdLineBuffer[GATE_MAX_APP_ARGS_LEN];
67 static gate_size_t cmdLineBufferLen = sizeof(cmdLineBuffer);
68 gate_uintptr_t apphandle = (gate_uintptr_t)hInstance;
69 char const* cmdArgs[192];
70 gate_size_t cmdArgCount = sizeof(cmdArgs) / sizeof(cmdArgs[0]);
71 gate_size_t cmdArgsUsed = 0;
72 int ret;
73
74 gate_win32_set_hinst(hInstance);
75
76 gate_main_init();
77
78 if (cmdline != NULL)
79 {
80 cmdArgsUsed = gate_win32_parse_command_line(cmdline, &cmdLineBuffer[0], cmdLineBufferLen, cmdArgs, cmdArgCount);
81 }
82 else
83 {
84 cmdArgsUsed = 0;
85 }
86 if (cmdArgsUsed == 0)
87 {
88 cmdArgs[1] = 0;
89 cmdArgsUsed = 1;
90 }
91
92 nativeAppPathUsed = GetModuleFileName((HMODULE)hInstance, nativeAppPath, sizeof(nativeAppPath) / sizeof(nativeAppPath[0]) - 1);
93 if (nativeAppPathUsed == 0)
94 {
95 gate_str_print_text(appPath, sizeof(appPath), "winapp", 6);
96 }
97 else
98 {
99 gate_win32_winstr_2_utf8(nativeAppPath, nativeAppPathUsed, appPath, sizeof(appPath));
100 }
101
102 ret = main_func(appPath, (cmdArgsUsed <= 1) ? NULL : &cmdArgs[1], (cmdArgsUsed <= 1) ? 0 : cmdArgsUsed - 1, apphandle);
103
104 gate_main_uninit();
105 return ret;
106 }
107
108
109 int gate_main_winapi_entry(gate_main_func main_func, void* hInstance, void* hPrevInstance, void* lpCmdLine, int nShowCmd)
110 {
111 #if defined(GATE_SYS_WIN16)
112 LPTSTR tcmd_line = (LPTSTR)lpCmdLine;
113 #else
114 LPTSTR tcmd_line = GetCommandLine();
115 #endif
116
117 #if defined(GATE_WIN32_UNICODE)
118 LPWSTR wcmd_line = tcmd_line;
119 #else
120 #if defined(GATE_SYS_WIN16)
121 LPWSTR wcmd_line = NULL;
122 #else
123 static WCHAR wcmd_line[8192];
124 gate_size_t tcmd_line_length = gate_str_length(tcmd_line);
125 gate_win32_ansi_2_utf16(tcmd_line, tcmd_line_length, (wchar_t*)&wcmd_line[0], sizeof(wcmd_line) / sizeof(wcmd_line[0]));
126 #endif
127 #endif
128 GATE_UNUSED_ARG(hPrevInstance);
129 GATE_UNUSED_ARG(lpCmdLine);
130 GATE_UNUSED_ARG(nShowCmd);
131 return gate_main_winapi_entry_impl(main_func, (HINSTANCE)hInstance, wcmd_line);
132 }
133
134
135 int gate_main_wchar_entry(gate_main_func main_func, int argc, wchar_t* argv[], wchar_t* envp[])
136 {
137 int ret = 0;
138 gate_uintptr_t apphandle = 0;
139 char* c_args[1024] = GATE_INIT_EMPTY;
140 gate_size_t ndx;
141 gate_size_t len;
142
143 GATE_UNUSED_ARG(envp);
144
145 #if !defined(GATE_SYS_WINSTORE)
146 apphandle = (gate_uintptr_t)gate_win32_get_hinst();
147 gate_win32_set_hinst((HINSTANCE)apphandle);
148 #endif
149
150 gate_main_init();
151
152 if (argc >= sizeof(c_args) / sizeof(c_args[0]))
153 {
154 argc = sizeof(c_args) / sizeof(c_args[0]) - 1;
155 }
156
157 for (ndx = 0; ndx != argc; ++ndx)
158 {
159 len = gate_str16_length(argv[ndx]);
160 c_args[ndx] = (char*)gate_mem_alloc(len * 2);
161 gate_str_utf16_2_utf8(argv[ndx], len, c_args[ndx], len * 2);
162 }
163
164 ret = main_func(c_args[0], (argc <= 1) ? NULL : (char const* const*)&c_args[1], (argc < 1) ? 0 : (argc - 1), apphandle);
165
166 for (ndx = 0; ndx != argc; ++ndx)
167 {
168 if (c_args[ndx] != NULL)
169 {
170 gate_mem_dealloc(c_args[ndx]);
171 c_args[ndx] = NULL;
172 }
173 }
174
175 gate_main_uninit();
176
177 return ret;
178 }
179
180
181 #endif /* GATE_MAIN_WINAPI_IMPL */
182
183 #if defined(GATE_MAIN_WASM_IMPL)
184
185 #define GATE_MAIN_NO_STDC_IMPL 1
186
187 #include "gate/results.h"
188 #include "gate/platform/wasm/wasm_gate.h"
189 #include <stdio.h>
190
191 int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
192 {
193 (void)argc;
194 (void)argv;
195 gate_wasm_register_gate_main(main_func);
196 gate_wasm_register_gate_main_program_name(argv[0]);
197 gate_main_init();
198
199 return gate_wasm_start_main();
200 }
201
202 #endif /* GATE_MAIN_WASM_IMPL */
203
204
205
206 #if defined(GATE_MAIN_DOS_IMPL)
207
208 #include "gate/strings.h"
209
210 #define GATE_MAIN_NO_STDC_IMPL 1
211
212 #include "process.h"
213
214 static char near main_command_path[256];
215 static char near main_command_argline[256];
216 static char const* main_command_args[32];
217
218 int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
219 {
220 int ret_code;
221
222 char* start;
223 char* ptr;
224 gate_size_t cmdline_len;
225 gate_size_t args_count = 0;
226 gate_uintptr_t apphandle = 0;
227
228 GATE_UNUSED_ARG(argc);
229 GATE_UNUSED_ARG(argv);
230
231 _cmdname(main_command_path);
232 getcmd(main_command_argline);
233
234 cmdline_len = gate_str_length(main_command_argline);
235 ptr = start = main_command_argline;
236 while (cmdline_len-- > 0)
237 {
238 if (*ptr == ' ')
239 {
240 *ptr = '\0';
241 if (ptr != start)
242 {
243 main_command_args[args_count++] = start;
244 }
245 start = ptr + 1;
246 }
247 ++ptr;
248 }
249 if (ptr != start)
250 {
251 main_command_args[args_count++] = start;
252 }
253
254 gate_dos_register_app_path(main_command_path);
255
256 gate_main_init();
257
258 ret_code = main_func(main_command_path, &main_command_args[0], args_count, apphandle);
259
260 gate_main_uninit();
261
262 return ret_code;
263 }
264
265 #endif /* GATE_MAIN_DOS_IMPL */
266
267
268
269 #if defined(GATE_MAIN_EFI_IMPL)
270 # define GATE_MAIN_NO_STDC_IMPL 1
271 # include <lib.h>
272 # include "gate/platform/efi/efi_gate.h"
273
274 int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
275 {
276 int ret_code;
277 gate_uintptr_t apphandle = 0;
278
279 gate_main_init();
280 gate_platform_efi_init(LibImageHandle, ST);
281
282 ret_code = gate_platform_efi_run_main(main_func);
283
284 gate_main_uninit();
285
286 return ret_code;
287 }
288
289 #endif /* GATE_MAIN_EFI_IMPL */
290
291
292
293 #if defined(GATE_MAIN_ANDROID_IMPL)
294 # define GATE_MAIN_NO_STDC_IMPL 1
295 # include <android/native_activity.h>
296 # include "gate/platform/android/gate_android_app.h"
297
298 void GATE_CORE_API gate_main_android_entry(gate_main_func main_func, void* activity, void* savedState, size_t savedStateSize)
299 {
300 gate_result_t result = gate_platform_init();
301 gate_android_app_create((ANativeActivity*)activity, savedState, savedStateSize, main_func);
302 }
303
304 #endif /* GATE_MAIN_ANDROID_IMPL */
305
306
307 #if defined(GATE_MAIN_ARDUINO_IMPL)
308
309 #include <Arduino.h>
310
311 void gate_main_arduino_setup(void)
312 {
313 gate_main_init();
314 }
315
316 void gate_main_arduino_loop(void)
317 {
318 static char const progname[] PROGMEM = "arduino";
319 gate_main(progname, NULL, 0, 0);
320 }
321
322 #endif
323
324
325 #if !defined(GATE_MAIN_NO_STDC_IMPL)
326
327 GATE_CORE_API int GATE_CALL gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]);
328
329 33 int gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
330 {
331 int ret_code;
332 33 gate_uintptr_t apphandle = 0;
333
334 #if defined(GATE_SYS_WIN)
335 apphandle = (gate_uintptr_t)gate_win32_get_hinst();
336 gate_win32_set_hinst((HINSTANCE)apphandle);
337 #endif
338
339 33 gate_main_init();
340
341
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);
342
343 31 gate_main_uninit();
344
345 31 return ret_code;
346 }
347
348 #endif
349
350