-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
274 lines (218 loc) · 5.93 KB
/
main.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
// ObjectTalk Scripting Language
// Copyright (c) 1993-2025 Johan A. Goossens. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
//
// Include files
//
#include <cstring>
#include <string>
#include <argparse/argparse.hpp>
#include "OtConfig.h"
#include "OtLibuv.h"
#include "OtPath.h"
#include "OtStderrMultiplexer.h"
#include "OtModule.h"
#if defined(OT_INCLUDE_UI)
#include "OtFramework.h"
#include "OtSceneApp.h"
#include "OtWorkspace.h"
#endif
//
// ObjectTalk main function
//
//
// Startup logic
// =============
//
// If compiled with UI
// If one file is specified on the command line
// do the following based on file extension
// .ot - compile and run script
// .ots - run scene file
// .otn - open nodes in IDE
// .* - open file in IDE
//
// Elseif more than one files is specified on the command line
// Open all files in the IDE
//
// Elseif no files are specified on the command line
// if MacOS and openFile(s) message is sent from the Finder
// Restart program with files as command line parameters
//
// Else
// Start IDE
//
// Else
// If one file is specified on the command line
// If file is .ot
// compile and run script
//
// Else
// Exit with error
//
// Else
// Exit with error
//
int main(int argc, char* argv[]) {
// parse all command line parameters
argparse::ArgumentParser program(argv[0], "0.3");
bool childProcessFlag = false;
std::string logFile;
program.add_argument("-c", "--child")
.help("run as an IDE child process")
.store_into(childProcessFlag);
program.add_argument("-l", "--log")
.help("specify a file to send log to")
.metavar("filename")
.store_into(logFile);
program.add_argument("files")
.help("files to process")
.remaining();
try {
program.parse_args(argc, argv);
} catch (const std::runtime_error& err) {
OtLogFatal(err.what());
}
// get all the target filenames
std::vector<std::string> files;
try {
files = program.get<std::vector<std::string>>("files");
} catch (std::logic_error& e) {
}
// set configuration
OtConfig::setSubprocessMode(childProcessFlag);
// log to file (if required)
if (logFile.size()) {
OtLog::setFileLogging(logFile);
}
try {
// initialize libuv
OtLibUv::init(argc, argv);
#if defined(OT_INCLUDE_UI)
//
// UI conguration
//
if (files.size() == 1) {
// handle case of single file on command line
auto file = files[0];
auto extension = OtPath::getExtension(file);
// check file extension
if (extension == ".ot" || extension == "") {
// compile and run script as a module
auto module = OtModule::create();
module->load(file);
module->unsetAll();
} else if (extension == ".ots") {
// handle a scene file
OtFramework framework;
OtSceneApp app{file};
framework.initialize();
framework.run(&app);
framework.terminate();
} else {
// handle other file types
OtFramework framework;
framework.initialize();
// open in IDE
OtWorkspace workspace;
workspace.openFile(file, OtEditor::VisualState::inTab);
framework.run(&workspace);
framework.terminate();
}
} else if (files.size() > 1) {
// handle case of multiple files on the command line
OtFramework framework;
framework.initialize();
// open each file in the IDE
OtWorkspace workspace;
for (auto& file : files) {
workspace.openFile(file, OtEditor::VisualState::inTab);
}
framework.run(&workspace);
framework.terminate();
} else {
// handle case of no files specified on command line
OtFramework framework;
framework.initialize();
// see if we have any files passed by the Finder (this only applies to MacOS)
for (auto& filename : OtFramework::getStartupFiles()) {
files.emplace_back(filename);
}
// do we have any files now?
if (files.size()) {
// yes, relaunch and specify files on command line
char** args = new char* [files.size() + 2];
// create argument array
auto executable = OtPath::getExecutable();
args[0] = (char*) executable.c_str();
int i = 1;
for (auto& file : files) {
args[i++] = (char*) file.c_str();
}
args[i] = nullptr;
// setup process options
uv_process_t subprocess;
uv_stdio_container_t stdio[3];
stdio[0].flags = UV_IGNORE;
stdio[1].flags = UV_IGNORE;
stdio[2].flags = UV_IGNORE;
uv_process_options_t options;
memset(&options, 0, sizeof(options));
options.file = args[0];
options.args = args;
options.flags = UV_PROCESS_DETACHED | UV_PROCESS_WINDOWS_HIDE_CONSOLE;
options.stdio_count = 3;
options.stdio = stdio;
// start process
auto status = uv_spawn(uv_default_loop(), &subprocess, &options);
if (status) {
delete [] args;
UV_CHECK_ERROR2("uv_spawn", status, executable.c_str());
}
uv_unref((uv_handle_t*) &subprocess);
delete [] args;
} else {
OtWorkspace workspace;
framework.run(&workspace);
}
framework.terminate();
}
#else
//
// Non-UI configuration
//
// were any files specified?
if (files.size() == 0) {
OtLogFatal("No files specified");
} else {
// we can only handle one file
if (files.size() != 1) {
OtLogFatal("Error: you can only run one file, you specified {}", files.size());
}
// run the file
auto file = files[0];
auto extension = OtPath::getExtension(file);
if (extension == ".ot") {
// compile and run script as a module
auto module = OtModule::create();
module->load(file);
module->unsetAll();
} else {
OtLogFatal("Error: can't execute file with extension [{}]", extension);
}
}
#endif
// cleanup
OtLibUv::end();
} catch (OtException& e) {
// send exception back to IDE (if required)
if (OtConfig::inSubprocessMode()) {
OtStderrMultiplexer::multiplex(e);
}
// output human readable error message
OtLogFatal("Error: {}", e.what());
}
return 0;
}