-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
218 lines (175 loc) · 4.5 KB
/
main.cpp
File metadata and controls
218 lines (175 loc) · 4.5 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
// ObjectTalk Scripting Language
// Copyright (c) 1993-2026 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>
#ifndef _WIN32
#include <signal.h>
#endif
#include <argparse/argparse.hpp>
#include "OtConfig.h"
#include "OtException.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
// .ota - open audio in IDE
// .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
// 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[]) {
#ifndef _WIN32
// disable signal handler for SIGPIPE on UNIX type systems
// we handle EPIPE errors instead
signal(SIGPIPE, SIG_IGN);
#endif
// parse all command line parameters
argparse::ArgumentParser program(argv[0], "0.4");
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 ([[maybe_unused]] 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 configuration
//
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.run(&app);
} else {
// handle other file types
OtFramework framework;
OtWorkspace workspace;
workspace.openFile(file, OtEditor::VisualState::inTab);
framework.run(&workspace);
}
} else if (files.size() > 1) {
// handle case of multiple files on the command line
OtFramework framework;
OtWorkspace workspace;
for (auto& file : files) {
workspace.openFile(file, OtEditor::VisualState::inTab);
}
framework.run(&workspace);
} else {
// handle case of no files specified on command line
OtFramework framework;
OtWorkspace workspace;
framework.run(&workspace);
}
#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;
}