GCC Code Coverage Report


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