-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
StartNelson.cpp
458 lines (456 loc) · 15 KB
/
StartNelson.cpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
//=============================================================================
#ifdef _MSC_VER
#include <Windows.h>
#endif
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <clocale>
#include <sstream>
#include "StartNelson.h"
#include "StartNelsonMainScript.hpp"
#include "StartNelsonUserScript.hpp"
#include "StartNelsonUserModules.hpp"
#include "FinishNelsonMainScript.hpp"
#include "FinishNelsonUserScript.hpp"
#include "AddGateway.hpp"
#include "EvaluateCommand.hpp"
#include "EvaluateScriptFile.hpp"
#include "Evaluator.hpp"
#include "GetNelsonPath.hpp"
#include "Localization.hpp"
#include "MainEvaluator.hpp"
#include "MaxOpenedFiles.hpp"
#include "ModulesHelpers.hpp"
#include "NelsonNamedMutex.hpp"
#include "Nelson_VERSION.h"
#include "ProgramOptions.hpp"
#include "RecursionStack.hpp"
#include "SetNelSonEnvironmentVariables.hpp"
#include "TimeoutThread.hpp"
#include "characters_encoding.hpp"
#include "SioClientCommand.hpp"
#include "WarningIds.hpp"
#include "WarningEmitter.h"
#include "ErrorEmitter.h"
#include "NelsonPrint.hpp"
#include "MxCall.h"
#include "NelsonConfiguration.hpp"
#include "FilesAssociation.hpp"
#include "NelsonReadyNamedMutex.hpp"
//=============================================================================
static void
ErrorCommandLineMessage_startup_exclusive(NELSON_ENGINE_MODE _mode)
{
std::stringstream msg;
msg << _("'nostartup' and 'startup' are mutual exclusive.\n");
#ifdef _MSC_VER
if (_mode == GUI) {
MessageBox(
NULL, utf8_to_wstring(msg.str()).c_str(), L"Nelson version:", MB_ICONINFORMATION);
} else {
std::cout << msg.str();
}
#else
std::cout << msg.str();
#endif
}
//=============================================================================
static void
ErrorCommandLineMessage_file_commmand(NELSON_ENGINE_MODE _mode)
{
std::wstring msg = _W("Too many arguments -f and -e are exclusive.");
#ifdef _MSC_BUILD
if (_mode == GUI) {
MessageBox(nullptr, msg.c_str(), _W("Error").c_str(), MB_ICONERROR);
} else {
std::cerr << wstring_to_utf8(msg) << std::endl;
}
#else
std::cerr << wstring_to_utf8(msg) << std::endl;
#endif
}
//=============================================================================
static void
ErrorCommandLineMessage_file_execute(NELSON_ENGINE_MODE _mode)
{
std::wstring msg = _W("Too many arguments -f and -F are exclusive.");
#ifdef _MSC_BUILD
if (_mode == GUI) {
MessageBox(nullptr, msg.c_str(), _W("Error").c_str(), MB_ICONERROR);
} else {
std::cerr << wstring_to_utf8(msg) << std::endl;
}
#else
std::cerr << wstring_to_utf8(msg) << std::endl;
#endif
}
//=============================================================================
static void
ErrorPathDetection(NELSON_ENGINE_MODE _mode)
{
std::wstring msg = _W("Nelson paths not initialized.");
#ifdef _MSC_BUILD
if (_mode == GUI) {
MessageBox(nullptr, msg.c_str(), _W("Error").c_str(), MB_ICONERROR);
} else {
msg = msg + L"\n";
fwprintf(stderr, L"%ls", msg.c_str());
}
#else
msg = msg + L"\n";
fwprintf(stderr, L"%ls", msg.c_str());
#endif
}
//=============================================================================
static void
ErrorInterpreter(NELSON_ENGINE_MODE _mode)
{
std::wstring msg = _W("Nelson interpreter not initialized.");
#ifdef _MSC_BUILD
if (_mode == GUI) {
MessageBox(nullptr, msg.c_str(), _W("Error").c_str(), MB_ICONERROR);
} else {
msg = msg + L"\n";
fwprintf(stderr, L"%ls", msg.c_str());
}
#else
msg = msg + L"\n";
fwprintf(stderr, L"%ls", msg.c_str());
#endif
}
//=============================================================================
static void
displayVersion(NELSON_ENGINE_MODE _mode)
{
std::stringstream msg;
msg << NELSON_PRODUCT_NAME << " version: \"" << NELSON_VERSION_STRING << "\"\n";
#ifdef _MSC_VER
if (_mode == GUI) {
MessageBox(
NULL, utf8_to_wstring(msg.str()).c_str(), L"Nelson version:", MB_ICONINFORMATION);
} else {
std::cout << msg.str();
}
#else
std::cout << msg.str();
#endif
}
//=============================================================================
static void
displayHelp(std::wstring description, NELSON_ENGINE_MODE _mode)
{
std::wstring msg = L"Nelson options:";
#ifdef _MSC_VER
if (_mode == GUI) {
MessageBox(nullptr, description.c_str(), msg.c_str(), MB_ICONINFORMATION);
} else {
std::wcout << msg.c_str() << L"\n";
std::wcout << description.c_str();
}
#else
std::wcout << msg.c_str() << L"\n";
std::wcout << description.c_str();
#endif
}
//=============================================================================
static void
ErrorCommandLine(const std::wstring& str, NELSON_ENGINE_MODE _mode)
{
std::wstring msg = L"Error:";
#ifdef _MSC_VER
if (_mode == GUI) {
MessageBox(nullptr, str.c_str(), msg.c_str(), MB_ICONINFORMATION);
} else {
std::wcout << msg.c_str() << L"\n";
std::wcout << str;
}
#else
std::wcout << msg.c_str() << L"\n";
std::wcout << str.c_str();
#endif
}
//=============================================================================
static int
NelsonMainStates(Evaluator* eval, bool haveNoStartup, bool haveNoUserStartup,
bool haveNoUserModules, const std::wstring& commandToExecute, const std::wstring& fileToExecute,
const wstringVector& filesToOpen, const wstringVector& filesToLoad)
{
eval->resetState();
if (!haveNoStartup) {
StartNelsonMainScript(eval);
eval->clearStacks();
if (eval->getState() == NLS_STATE_QUIT) {
goto FINISH;
}
eval->resetState();
bool loadUserModulesSucceeced = false;
if (!haveNoUserModules) {
loadUserModulesSucceeced = StartNelsonUserModules(eval);
eval->clearStacks();
if (eval->getState() == NLS_STATE_QUIT) {
goto FINISH;
}
eval->resetState();
}
if (!haveNoUserStartup && loadUserModulesSucceeced) {
StartNelsonUserScript(eval);
eval->clearStacks();
if (eval->getState() == NLS_STATE_QUIT) {
goto FINISH;
}
eval->resetState();
}
}
try {
if (!commandToExecute.empty()) {
EvaluateCommand(eval, commandToExecute, false);
}
if (!fileToExecute.empty()) {
EvaluateScriptFile(eval, fileToExecute);
}
} catch (Exception& e) {
Interface* io = eval->getInterface();
io->errorMessage(e.getMessage());
}
eval->isReadyToUse = true;
OpenFilesAssociated(
(NELSON_ENGINE_MODE)NelsonConfiguration::getInstance()->getNelsonEngineMode(), filesToOpen,
false);
LoadFilesAssociated(
(NELSON_ENGINE_MODE)NelsonConfiguration::getInstance()->getNelsonEngineMode(), filesToLoad,
false);
while (eval->getState() != NLS_STATE_QUIT) {
if (eval->getState() == NLS_STATE_ABORT) {
eval->clearStacks();
}
eval->resetState();
eval->evalCLI();
}
closeIsReadyNelsonMutex((int)boost::interprocess::ipcdetail::get_current_process_id());
eval->resetState();
FINISH:
if (!haveNoStartup) {
if (!haveNoUserStartup) {
FinishNelsonUserScript(eval);
eval->resetState();
}
FinishNelsonMainScript(eval);
if (eval->getState() == NLS_STATE_QUIT) {
goto EXIT;
}
}
EXIT:
int exitCode = eval->getExitCode();
::destroyMainEvaluator();
clearWarningIdsList();
return exitCode;
}
//=============================================================================
static int
StartNelsonInternal(wstringVector args, NELSON_ENGINE_MODE _mode)
{
int exitCode = -1;
NelsonConfiguration::getInstance()->setNelsonEngineMode(_mode);
ProgramOptions po(args, _mode);
if (!po.isValid()) {
ErrorCommandLine(po.getErrorMessage(), _mode);
return exitCode;
}
if (po.haveOptionsHelp()) {
displayHelp(po.getOptionsHelp(), _mode);
return 0;
}
if (po.haveVersion()) {
displayVersion(_mode);
return 0;
}
if (_mode == NELSON_ENGINE_MODE::GUI && !po.haveNoIpc()) {
bool wasSent = false;
if (po.haveFileToExecuteIPC()) {
std::wstring fileToExecuteIPC = po.getFileToExecuteIPC();
wstringVector fileToExecuteAsVector;
fileToExecuteAsVector.push_back(fileToExecuteIPC);
wasSent = ExecuteFilesAssociated(_mode, fileToExecuteAsVector, true);
if (wasSent) {
return 0;
}
}
if (po.haveOpenFiles()) {
wasSent = OpenFilesAssociated(_mode, po.getFilesToOpen(), true);
if (wasSent) {
return 0;
}
}
if (po.haveLoadFiles()) {
wasSent = LoadFilesAssociated(_mode, po.getFilesToLoad(), true);
if (wasSent) {
return 0;
}
}
}
if (!SetNelSonEnvironmentVariables()) {
ErrorPathDetection(_mode);
return exitCode;
}
setMaxOpenedFiles();
initializeDefaultWarningIdsList();
#ifdef _MSC_VER
#if _MSC_VER < 1900
_set_output_format(_TWO_DIGIT_EXPONENT);
#endif
#else
setRecursionStacksize(SIZE_MAX_RECURSION_CALL);
#endif
setlocale(LC_NUMERIC, "C");
#ifndef _MSC_VER
if (_mode == NELSON_ENGINE_MODE::BASIC_TERMINAL) {
setlocale(LC_CTYPE, "en_US.utf8");
}
#endif
if (getRecursionStacksize() < SIZE_MAX_RECURSION_CALL) {
std::string msg;
msg = _("Recursion stack not enough.\nPlease set C recursion stack to ");
msg = msg + std::to_string(SIZE_MAX_RECURSION_CALL);
msg.append("\n");
msg = msg + _("Current C stack is: ") + std::to_string(getRecursionStacksize());
#ifdef _MSC_VER
if (_mode == GUI) {
MessageBox(nullptr, utf8_to_wstring(msg).c_str(), L"Nelson error:", MB_ICONERROR);
} else {
std::cout << msg << std::endl;
}
#else
std::cout << msg << std::endl;
#endif
return exitCode;
}
std::wstring fileToExecute;
std::wstring commandToExecute;
wstringVector filesToOpen;
wstringVector filesToLoad;
std::wstring lang;
bool bQuietMode = false;
if (po.haveTimeout()) {
createTimeoutThread(po.getTimeout());
}
lang = po.getLanguage();
if (po.haveCommandToExecute() && po.haveFileToExecute()) {
ErrorCommandLineMessage_file_commmand(_mode);
return exitCode;
}
if (po.haveFileToExecute() && po.haveFileToExecuteIPC()) {
ErrorCommandLineMessage_file_execute(_mode);
return exitCode;
}
if (po.haveNoStartup() && po.haveNoUserStartup()) {
ErrorCommandLineMessage_startup_exclusive(_mode);
return exitCode;
}
commandToExecute = po.getCommandToExecute();
filesToOpen = po.getFilesToOpen();
filesToLoad = po.getFilesToLoad();
if (po.haveFileToExecute()) {
fileToExecute = po.getFileToExecute();
}
if (po.haveFileToExecuteIPC()) {
fileToExecute = po.getFileToExecuteIPC();
}
std::wstring socketIoURI;
if (_mode == BASIC_SIO_CLIENT) {
if (po.haveSocketIoUri()) {
socketIoURI = po.getSocketIoUri();
}
}
bQuietMode = po.haveQuietMode();
if (!fileToExecute.empty()) {
// expand filename required for shebang
boost::filesystem::path p(fileToExecute);
boost::filesystem::path full_p = boost::filesystem::complete(p);
fileToExecute = full_p.generic_wstring();
}
Evaluator* eval = createMainEvaluator(_mode, lang, po.haveOptionsMinimize());
if (eval != nullptr) {
setWarningEvaluator(eval);
setErrorEvaluator(eval);
setPrintInterface(eval->getInterface());
mexSetEvaluator(eval);
eval->setQuietMode(bQuietMode);
eval->setCommandLineArguments(args);
if (lang != Localization::Instance()->getCurrentLanguage() && !lang.empty()) {
Interface* io = eval->getInterface();
Exception e(L"Wrong language.");
eval->setLastErrorException(e);
io->errorMessage(e.getMessage());
}
try {
AddGateway(eval, ConstructDynamicLibraryFullname(Nelson::GetRootPath(), L"core"));
AddGateway(
eval, ConstructDynamicLibraryFullname(Nelson::GetRootPath(), L"modules_manager"));
AddGateway(
eval, ConstructDynamicLibraryFullname(Nelson::GetRootPath(), L"dynamic_link"));
AddGateway(eval, ConstructDynamicLibraryFullname(Nelson::GetRootPath(), L"string"));
} catch (const Exception& e) {
Interface* io = eval->getInterface();
eval->setLastErrorException(e);
io->errorMessage(_W("Nelson cannot load base modules.\n"));
}
if (!socketIoURI.empty()) {
SioClientCommand::getInstance()->create(wstring_to_utf8(socketIoURI));
}
exitCode = NelsonMainStates(eval, po.haveNoStartup(), po.haveNoUserStartup(),
po.haveNoUserModules(), commandToExecute, fileToExecute, filesToOpen, filesToLoad);
::destroyMainEvaluator();
clearWarningIdsList();
destroyTimeoutThread();
} else {
ErrorInterpreter(_mode);
}
return exitCode;
}
//=============================================================================
static int
StartNelsonInternalWithMutex(const wstringVector& args, NELSON_ENGINE_MODE _mode)
{
if (!haveNelsonMutex()) {
openNelsonMutex();
}
int exitCode = StartNelsonInternal(args, _mode);
if (haveNelsonMutex()) {
closeNelsonMutex();
}
return exitCode;
}
//=============================================================================
int
StartNelson(int argc, wchar_t* argv[], NELSON_ENGINE_MODE _mode)
{
wstringVector args;
for (size_t l = 0; l < static_cast<size_t>(argc); l++) {
args.push_back(std::wstring(argv[l]));
}
return StartNelsonInternalWithMutex(args, _mode);
}
//=============================================================================
int
StartNelson(int argc, char* argv[], NELSON_ENGINE_MODE _mode)
{
wstringVector args;
for (size_t l = 0; l < static_cast<size_t>(argc); l++) {
args.push_back(utf8_to_wstring(argv[l]));
}
return StartNelsonInternalWithMutex(args, _mode);
}
//=============================================================================