-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
288 lines (251 loc) · 7.37 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
/**
* MacOS Launcher for ELS, https://github.com/Corionis/ELS
*
*/
int main(int argc, char *argv[]) {
char cwd[PATH_MAX];
bool isLogging = false;
std::ofstream log;
// is launcher logging enabled?
for (int i = 1; i < argc; ++i)
{
std::string search(argv[i]);
int p = search.find("--launcher-log");
if (p >= 0)
{
isLogging = true;
}
}
// get user home directory
char *homeChar = getenv("HOME");
std::string homeStr = homeChar;
if (isLogging)
{
std::string homeLog = homeStr + "/.els/output";
homeLog += "/ELS-MacOS-Launcher.log";
log.open(homeLog);
log << "log: " << homeLog << std::endl;
}
// get the current working directory
getcwd(cwd, sizeof(cwd));
if (isLogging)
log << "cwd! " << cwd << std::endl;
// get the path to this executable
// from the root of argv[0] by removing /ELS-Navigator.app/Contents/MacOS/ELS-Navigator
if (argc > 0)
{
if (isLogging)
log << "arg0: " << argv[0] <<std::endl;
int count = 0;
int pos = 0;
for (size_t i = strlen(argv[0]); i > 0; --i)
{
if (argv[0][i] == '/')
{
++count;
if (count == 4)
{
pos = i;
break;
}
}
}
// clear cwd
for (int i = 0; i < sizeof(cwd); ++i)
{
cwd[i] = '\0';
}
std::strncpy(cwd, &argv[0][0], pos);
}
if (isLogging)
log << "cwd! " << cwd << std::endl;
// detect -C [configuration directory] argument
bool dashC = true;
for (int i = 1; i < argc; ++i)
{
std::string search(argv[i]);
int p = search.find("-C");
if (p >= 0)
{
dashC = false;
// clear cwd
for (int i = 0; i < sizeof(cwd); ++i)
{
cwd[i] = '\0';
}
// copy working directory argument
if (i + 1 < argc)
strcpy(cwd, argv[i + 1]);
else
{
if (isLogging)
log << "Exception: -C option requires a configuration directory argument" << std::endl;
return 1;
}
// make sure the directory exists
std::filesystem::path cfgPath(cwd);
if (std::filesystem::exists(cfgPath))
{
if (!std::filesystem::is_directory(cfgPath))
{
if (isLogging)
log << "Exception: -C \"" << cwd << "\" exists but is not a directory" << std::endl;
return 1;
}
}
else
{
mkdir(cwd, 0755);
chdir(cwd);
}
break;
}
}
chdir(cwd);
// double check working directory
char wd[PATH_MAX];
getcwd(wd, sizeof(cwd));
if (isLogging)
log << "wd: " << wd << std::endl;
// the java command to execute
std::string command(cwd);;
command += "/rt/Contents/Home/bin/java";
// setup arguments
int sz = dashC ? argc + 5 : argc + 3;
if (isLogging) // --launcher-log is for the launcher only and skipped for the Navigator
sz = sz - 1;
char *arguments[sz];
if (isLogging)
log << "siz: " << sz << std::endl;
int index = 0;
// argv[0]
arguments[index++] = command.data();
// override the java icon
std::string arg1 = "-Xdock:icon=bin/els-logo-98px.icns";
arguments[index++] = arg1.data();
// the Jar
std::string arg2 = "-jar";
arguments[index++] = arg2.data();
std::string arg3(cwd);
arg3 += "/bin/ELS.jar";
arguments[index++] = arg3.data();
// add -C [working-directory] if needed
if (dashC)
{
// see if libraries directory exists in the software location
char check[PATH_MAX];
std::string sd = cwd;
sd += "/libraries";
strncpy(check, &sd[0], sd.length());
// make sure the directory exists
std::filesystem::path programPath(check);
if (std::filesystem::exists(programPath)) // check if libraries is in program directory
{
if (!std::filesystem::is_directory(programPath))
{
std::cout << "Exception: \"" << sd << "\" exists but is not a directory" << std::endl;
return 1;
}
}
else // not in program directory, use default of "USER_HOME/.els"
{
char defaultDirectory[PATH_MAX];
std::string dd = homeChar;
std::cout << "hom: " << homeStr << std::endl;
dd += "/.els";
strncpy(defaultDirectory, &dd[0], dd.length());
// make sure the directory exists
std::filesystem::path defaultPath(defaultDirectory);
if (std::filesystem::exists(defaultPath))
{
if (!std::filesystem::is_directory(defaultPath))
{
std::cout << "Exception: \"" << dd << "\" exists but is not a directory" << std::endl;
return 1;
}
}
else
mkdir(defaultDirectory, 0775); // create default directory
for (int i = 0; i < sizeof(cwd); ++i)
cwd[i] = '\0';
strcpy(cwd, defaultDirectory);
}
std::string arg = "-C";
arguments[index++] = arg.data();
arguments[index++] = cwd;
}
// copy remaining arguments
for (int i = 1; i < argc; ++i)
{
std::string search(argv[i]);
int p = search.find("--launcher-log"); // exclude launcher option
if (p >= 0)
continue;
arguments[index++] = argv[i];
}
// report the entire command line
std::string complete;
for (int i = 0; i < sz; ++i)
{
if (isLogging)
log << "Arg " << i << ": " << arguments[i] << std::endl;
complete += arguments[i];
complete += " ";
}
if (isLogging)
{
log << "complete: " << command << " " << complete << std::endl;
}
// run it
pid_t pid = fork();
if (pid == 0)
{
// Child process
execv(command.c_str(), arguments);
if (isLogging)
log << "execl failed" << std::endl;
return 1;
}
else if (pid > 0)
{
// Parent process
int status;
pid_t child_pid = waitpid(pid, &status, 0);
if (child_pid == -1)
{
if (isLogging)
log << "waitpid error" << std::endl;
return 1;
}
if (isLogging)
{
if (WIFEXITED(status))
{
log << "Child process exited with status: " << WEXITSTATUS(status) << std::endl;
}
else if (WIFSIGNALED(status))
{
log << "Child process terminated by signal: " << WTERMSIG(status) << std::endl;
}
}
}
else
{
// Fork error
if (isLogging)
{
log << "fork error" << std::endl;
log.close();
}
return 1;
}
if (isLogging)
log.close();
return 0;
}