This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathdskw32main.cpp
More file actions
executable file
·339 lines (268 loc) · 9.35 KB
/
dskw32main.cpp
File metadata and controls
executable file
·339 lines (268 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include "globdefs.h"
#include "filedefs.h"
#include "osspec.h"
#include "typedefs.h"
#include "parsedef.h"
#include "mode.h"
#include "scriptpt.h"
#include "mcerror.h"
#include "globals.h"
#include "util.h"
#include "libscript/script.h"
#include <msctf.h>
////////////////////////////////////////////////////////////////////////////////
HINSTANCE MChInst;
MCStringRef MCcmdline;
// MW-2008-09-15: [[ Bug 7148 ]] Increase the maximum limit on the number of arguments to
// 256 - should hopefully be enough for most people's applications!
#define MAXARGS 256
////////////////////////////////////////////////////////////////////////////////
void X_main_loop(void)
{
while (!MCquit)
{
X_main_loop_iteration();
if (MCpendingstacklimit != MCstacklimit)
break;
}
}
////////////////////////////////////////////////////////////////////////////////
// To allow runtime configuration of the stack, we use fibers to run the main
// part of the engine execution.
// This is the main thread's principal fiber (running WinMain).
static void *s_main_fiber = nil;
// This is the errno pointer (from w32spec.cpp)
extern int *g_mainthread_errno;
struct InitializeFiberContext
{
int argc;
MCStringRef *argv;
MCStringRef *envp;
bool success;
};
static void DisplayStartupErrorAndExit(void)
{
MCAutoStringRef mcap;
MCAutoStringRef mtext;
MCModeGetStartupErrorMessage(&mcap, &mtext);
if (!MCnoui)
{
MCAutoStringRefAsWString t_cap_wstr, t_text_wstr;
/* UNCHECKED */ t_cap_wstr.Lock(*mcap);
/* UNCHECKED */ t_text_wstr.Lock(*mtext);
MessageBoxW(HWND_DESKTOP, *t_text_wstr, *t_cap_wstr, MB_APPLMODAL | MB_OK);
}
else
{
MCAutoStringRefAsSysString t_cap_sys, t_text_sys;
/* UNCHECKED */ t_cap_sys.Lock(*mcap);
/* UNCHECKED */ t_text_sys.Lock(*mtext);
fprintf(stderr, "ERROR: %s: %s\n", *t_cap_sys, *t_text_sys);
}
exit(-1);
}
// The 'init' fiber is used to execute X_init.
static void CALLBACK InitializeFiberRoutine(void *p_context)
{
InitializeFiberContext *context;
context = (InitializeFiberContext *)p_context;
g_mainthread_errno = _errno();
#ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF|_CRTDBG_DELAY_FREE_MEM_DF|_CRTDBG_CHECK_CRT_DF);
#endif
struct X_init_options t_options;
t_options.argc = context -> argc;
t_options.argv = context -> argv;
t_options.envp = context -> envp;
t_options.app_code_path = nullptr;
context -> success = X_init(t_options);
SwitchToFiber(s_main_fiber);
}
// The 'loop' fiber is used to run X_main_loop.
static void CALLBACK LoopFiberRoutine(void *p_parameter)
{
void *t_bottom;
MCstackbottom = (char *)&t_bottom;
g_mainthread_errno = _errno();
X_main_loop();
SwitchToFiber(s_main_fiber);
}
////////////////////////////////////////////////////////////////////////////////
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// MW-2010-05-09: Elevated process handling - if the cmd line begins with '-elevated-slave'
// then we are running in the elevation bootstrap mode.
extern int MCS_windows_elevation_bootstrap_main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
if (strncmp(lpCmdLine, "-elevated-slave", 15) == 0 && strlen(lpCmdLine) == 23)
return MCS_windows_elevation_bootstrap_main(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
MCModePreMain();
int argc = 0;
int envc = 0;
MCAutoArray<MCStringRef> argv;
MCAutoArray<MCStringRef> envp;
// MW-2004-11-25: Under Win9x it would appear that not all exceptions are masked...
// MW-2004-11-28: Actually, this doesn't solve the problem, it seems the OS is
// meddling with it somewhere so have installed a dummy signal handler
// _controlfp( _controlfp(0, 0) | (EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT | EM_ZERODIVIDE | EM_DENORMAL | EM_INVALID), MCW_EM);
#ifdef _DEBUG
int t_dbg_flags;
t_dbg_flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
t_dbg_flags |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(t_dbg_flags);
#endif
// MW-2010-10-12: Turn off critical error reporting dialog - this stops annoying
// 'cant find disk' messages (we hope!). This is advised at:
// http://msdn.microsoft.com/en-us/library/ms680621(VS.85).aspx
SetErrorMode(SEM_FAILCRITICALERRORS);
MCModeSetupCrashReporting();
// SN-2014-08-06: [[ Bug 13027 ]] This line had been removed, thus MChInst was always NULL
MChInst = hInstance;
// Get the command line and convert it into the expected argc/argv form
LPWSTR lpWCmdLine = wcsdup(GetCommandLineW());
// FG-2014-09-23: [[ Bugfix 12444 ]] Re-arrange command line processing to
// match behaviour in 6.x and below.
WCHAR* wcFileNameBuf = new (nothrow) WCHAR[MAX_PATH+1];
DWORD dwFileNameLen = GetModuleFileNameW(NULL, wcFileNameBuf, MAX_PATH+1);
// Windows uses slashes the opposite way around to the other platforms and requires conversion
for (DWORD i = 0; i < dwFileNameLen; i++)
{
if (wcFileNameBuf[i] == '/')
wcFileNameBuf[i] = '\\';
else if (wcFileNameBuf[i] == '\\')
wcFileNameBuf[i] = '/';
}
LPWSTR csptr = lpWCmdLine;
while (*csptr)
{
if (*csptr == '\\')
*csptr = '/';
csptr++;
}
// SN-2014-11-19: [[ Bug 14058 ]] GetCommandLineW returns the executable name and the arguments,
// but we do not want to include the former in MCcmdline
csptr = lpWCmdLine;
if (*csptr == '"')
{
// The executable is the whole enquoted string
++csptr;
while(*csptr && *csptr != '"')
csptr++;
}
else
{
// The executable goes up to the first space
while(*csptr && !iswspace(*csptr))
csptr++;
}
if (*csptr)
{
// We discard all the spaces after the quote/space we found
csptr++;
while (*csptr && iswspace(*csptr))
++csptr;
}
if (!MCInitialize() ||
!MCSInitialize() ||
!MCScriptInitialize())
exit(-1);
// Ensure the command line variable gets set
// SN-2014-11-19: [[ Bug 14058 ]] We use the updated command line (a nil pointer is fine)
/* UNCHECKED */ MCStringCreateWithWString(csptr, MCcmdline);
// Convert the WStrings (UTF-16) into StringRefs
LPWSTR *lpWargv = CommandLineToArgvW(lpWCmdLine, &argc);
argv.New(argc);
/* UNCHECKED */ MCStringCreateWithWString(wcFileNameBuf, argv[0]);
for (int i = 1; i < argc; i++)
/* UNCHECKED */ MCStringCreateWithWString(lpWargv[i], argv[i]);
LocalFree(lpWargv);
delete wcFileNameBuf;
delete lpWCmdLine;
// Convert the environment strings into StringRefs
envp.New(1);
LPWCH lpEnvStrings = GetEnvironmentStringsW();
LPCWSTR sptr = lpEnvStrings;
size_t t_len;
while ((t_len = wcslen(sptr)) > 0)
{
uindex_t t_index = envc++;
/* UNCHECKED */ envp.Extend(envc + 1);
/* UNCHECKED */ MCStringCreateWithWString(sptr, envp[envc - 1]);
sptr += t_len + 1;
}
// Terminate the envp array
envp[envc] = nil;
FreeEnvironmentStringsW(lpEnvStrings);
// Initialize OLE on the main thread.
OleInitialize(nil);
// Initialize the TSF manager on the main thread
ITfThreadMgr *t_tsf_mgr;
t_tsf_mgr = nil;
CoCreateInstance(CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, IID_ITfThreadMgr, (void**)&t_tsf_mgr);
// Create the main thread's principal fiber.
s_main_fiber = ConvertThreadToFiber(nil);
// Now create the init fiber
InitializeFiberContext t_init;
t_init . argc = argc;
t_init . argv = argv.Ptr();
t_init . envp = envp.Ptr();
t_init . success = False;
void *t_init_fiber;
t_init_fiber = CreateFiberEx(64 * 1024, MCstacklimit, 0, InitializeFiberRoutine, &t_init);
if (t_init_fiber == nil)
exit(-1);
SwitchToFiber(t_init_fiber);
DeleteFiber(t_init_fiber);
if (!t_init . success)
DisplayStartupErrorAndExit();
// Now we loop continually until quit. If 'X_main_loop' returns without quitting
// it means a stack size change request has occurred.
while(!MCquit)
{
// Create ourselves a new fiber with appropriate stack size
void *t_loop_fiber;
if (MCpendingstacklimit < 65536)
MCpendingstacklimit = 65536;
for(;;)
{
t_loop_fiber = CreateFiberEx(64 * 1024, MCpendingstacklimit, 0, LoopFiberRoutine, nil);
if (t_loop_fiber != nil)
{
MCstacklimit = MCpendingstacklimit;
break;
}
if (MCpendingstacklimit < 1024 * 1024)
break;
MCpendingstacklimit -= 1024 * 1024;
}
if (t_loop_fiber == nil)
exit(-1);
MCrecursionlimit = MCU_min(MCrecursionlimit, MCstacklimit - MC_UNCHECKED_STACKSIZE);
SwitchToFiber(t_loop_fiber);
DeleteFiber(t_loop_fiber);
}
// MW-2011-08-19: [[ Bug ]] Make sure we reset the 'bottom' of the stack and
// errno pointer (we're executing script on a different fiber than before).
void *t_bottom;
MCstackbottom = (char *)&t_bottom;
g_mainthread_errno = _errno();
int r = X_close();
MCValueRelease(MCcmdline);
MCScriptFinalize();
MCFinalize();
if (t_tsf_mgr != nil)
t_tsf_mgr -> Release();
return r;
}
////////////////////////////////////////////////////////////////////////////////