-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargparser.cpp
579 lines (509 loc) · 17.9 KB
/
argparser.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
* Copyright 2009-2010 JRuby Team (www.jruby.org).
*/
#include <cstring>
#include <cstdlib>
#include <climits>
#include <memory>
#include "utilsfuncs.h"
#include "argparser.h"
#include "argnames.h"
#include "version.h"
#ifndef WIN32
#include <sys/types.h>
#include <sys/utsname.h>
#include <dirent.h>
#define EXEEXT ""
#else
#define EXEEXT ".exe"
#endif
using namespace std;
const char *ArgParser::HELP_MSG =
"JRuby Launcher usage: jruby" EXEEXT " {options} arguments\n\n\
Options:\n\
-Xhelp show this help\n\
-Xversion print launcher's version\n\
\nJvm Management:\n\
-Xjdkhome <path> set path to JDK\n\
-Xfork-java run java in separate process\n\
-J<jvm_option> pass <jvm_option> to JVM\n\
\nClasspath Management:\n\
-Xcp <classpath> set the classpath\n\
-Xcp:p <classpath> prepend <classpath> to classpath\n\
-Xcp:a <classpath> append <classpath> to classpath\n\
-Xnobootclasspath don't put jruby jars on the bootclasspath\n\
\nMisc:\n\
-Xtrace <path> path for launcher log (for troubleshooting)\n\
-Xcommand just print the equivalent java command and exit\n"
#ifdef WIN32
" -Xconsole <mode> jrubyw console attach mode (new|attach|suppress)\n\n"
#endif
"To see general JRuby options, type 'jruby -h' or 'jruby --help'.\n";
const char *ArgParser::REQ_JAVA_VERSION = "1.5";
const char *ArgParser::OPT_JDK_HOME = "-Djdk.home=";
const char *ArgParser::OPT_JRUBY_HOME = "-Djruby.home=";
const char *ArgParser::OPT_JRUBY_COMMAND_NAME = "-Dsun.java.command=";
const char *ArgParser::OPT_CLASS_PATH = "-Djava.class.path=";
const char *ArgParser::OPT_BOOT_CLASS_PATH = "-Xbootclasspath/a:";
const char *ArgParser::OPT_JFFI_PATH = "-Djffi.boot.library.path=";
const char *ArgParser::OPT_JRUBY_SHELL = "-Djruby.shell=";
const char *ArgParser::OPT_JRUBY_SCRIPT = "-Djruby.script=";
const char *ArgParser::MAIN_CLASS = "org/jruby/Main";
const char *ArgParser::DEFAULT_EXECUTABLE = "jruby";
ArgParser::ArgParser()
: separateProcess(false)
, nailgunClient(false)
, noBootClassPath(false)
, printCommandLine(false)
{
}
ArgParser::ArgParser(const ArgParser& orig) {
}
ArgParser::~ArgParser() {
}
void ArgParser::addEnvVarToOptions(std::list<std::string> & optionsList, const char * envvar) {
const char * value = getenv(envvar);
string opts("");
if (value) {
opts = value;
}
if (opts.size() > 0) {
if (opts[0] == '"' || opts[0] == '\'') {
char quote = opts[0];
if (opts[opts.size() - 1] == quote) {
opts = opts.substr(1, opts.size() - 2);
}
}
size_t start = 0, pos = 0;
while ((pos = opts.find(' ', start)) != string::npos) {
string part(opts.substr(start, pos - start));
if (part.size() > 0) {
logMsg("%s += %s", envvar, part.c_str());
optionsList.push_back(part);
}
start = pos + 1;
}
if (start < opts.size()) {
string part(opts.substr(start));
if (part.size() > 0) {
logMsg("%s += %s", envvar, part.c_str());
optionsList.push_back(part);
}
}
}
}
#ifdef __MACH__
#include <mach-o/dyld.h>
#endif
bool ArgParser::initPlatformDir() {
#ifdef WIN32
char path[MAX_PATH] = "";
getCurrentModulePath(path, MAX_PATH);
#else
char path[PATH_MAX] = "";
bool found = false;
// first try via linux /proc/self/exe
logMsg("initPlatformDir: trying /proc/self/exe");
found = readlink("/proc/self/exe", path, PATH_MAX) != -1;
#ifdef __MACH__
uint32_t sz = PATH_MAX;
if (_NSGetExecutablePath(path, &sz) == 0) { // OSX-specific
logMsg("initPlatformDir: using _NSGetExecutablePath");
string tmpPath(path);
realpath(tmpPath.c_str(), path);
found = true;
}
#endif
#ifdef __SUNOS__
const char* execname = getexecname();
if (execname) {
logMsg("initPlatformDir: using getexecname");
char * dst = path;
if (execname[0] != '/') {
getcwd(path, PATH_MAX - strlen(execname) - 2);
dst = path + strlen(path);
*dst++ = '/';
}
strncpy(dst, execname, strlen(execname));
found = true;
}
#endif
if (!found && platformDir[0] == '/') { // argv[0]: absolute path
logMsg("initPlatformDir: argv[0] appears to be an absolute path");
strncpy(path, platformDir.c_str(), PATH_MAX);
found = true;
}
if (!found && platformDir.find('/') != string::npos) { // argv[0]: relative path
logMsg("initPlatformDir: argv[0] appears to be a relative path");
getcwd(path, PATH_MAX - platformDir.length() - 1);
strncpy(path + strlen(path), platformDir.c_str(), platformDir.length());
found = true;
}
if (!found) { // try via PATH search
logMsg("initPlatformDir: trying to find executable on PATH");
char * location = findOnPath(platformDir.c_str());
if (location != NULL) {
strncpy(path, location, PATH_MAX);
free(location);
found = true;
}
}
if (!found) { // try via JRUBY_HOME
if (getenv("JRUBY_HOME") != NULL) {
logMsg("initPlatformDir: trying JRUBY_HOME environment variable");
strncpy(path, getenv("JRUBY_HOME"), PATH_MAX - 11);
strncpy(path + strlen(path), "/bin/jruby", 10);
found = true;
}
}
if (!fileExists(path)) {
printToConsole("Could not figure out a proper location for JRuby.\n"
"Try `jruby -Xtrace trace.log ...` and view trace.log for details.");
return false;
}
#endif
logMsg("Module: %s", path);
char *bslash = strrchr(path, FILE_SEP);
if (!bslash) {
return false;
}
*bslash = '\0';
bslash = strrchr(path, FILE_SEP);
if (!bslash) {
return false;
}
*bslash = '\0';
platformDir = path;
logMsg("Platform dir: %s", platformDir.c_str());
return true;
}
bool ArgParser::parseArgs(int argc, char *argv[]) {
list<string> args;
#define CHECK_ARG \
it++; \
if (it == args.end() || it->empty() || it->at(0) == '-') { \
logErr(false, true, "Argument is missing for \"%s\" option.", (--it)->c_str()); \
return false; \
} \
it--;
#define INCR ++it, ++i
addEnvVarToOptions(javaOptions, "JAVA_OPTS");
addEnvVarToOptions(args, "JRUBY_OPTS");
#ifdef __MACH__
if (getenv("JAVA_ENCODING") == NULL) {
javaOptions.push_back("-Dfile.encoding=UTF-8");
}
#endif
addToArgList(args, argc, argv);
logMsg("Parsing arguments:");
for (list<string>::iterator it = args.begin(); it != args.end(); ++it) {
logMsg("\t%s", it->c_str());
}
bool doneScanning = false;
int i = 0;
for (list<string>::iterator it = args.begin(); it != args.end(); INCR) {
if (doneScanning) {
progArgs.push_back(*it);
} else if (it->compare("--") == 0 || it->empty() || it->at(0) != '-') {
progArgs.push_back(*it);
doneScanning = true;
} else if (it->compare(ARG_NAME_SEPAR_PROC) == 0) {
separateProcess = true;
logMsg("Run Java in separater process");
} else if (it->compare(ARG_NAME_CMD_ONLY) == 0) {
printCommandLine = true;
} else if (it->compare(ARG_NAME_NO_BOOTCLASSPATH) == 0) {
noBootClassPath = true;
} else if (it->compare(ARG_NAME_LAUNCHER_LOG) == 0) {
// We only check the validity of args here,
// the actual parsing and setting the log file
// is done earlier, in checkLoggingArg()
CHECK_ARG;
INCR;
} else if (it->compare(ARG_NAME_BOOTCLASS) == 0) {
CHECK_ARG;
INCR;
bootclass = *it;
} else if (it->compare(ARG_NAME_JDKHOME) == 0) {
CHECK_ARG;
INCR;
jdkhome = *it;
} else if (it->compare(ARG_NAME_CP_PREPEND) == 0) {
CHECK_ARG;
if (!cpBefore.empty()) {
cpBefore += PATH_SEP;
}
INCR;
cpBefore += *it;
} else if (it->compare(ARG_NAME_CP_APPEND) == 0) {
CHECK_ARG;
if (!cpAfter.empty()) {
cpAfter += PATH_SEP;
}
INCR;
cpAfter += *it;
} else if (it->compare(ARG_NAME_CP) == 0
|| it->compare(ARG_NAME_CLASSPATH) == 0) {
CHECK_ARG;
if (!cpExplicit.empty()) {
cpExplicit += PATH_SEP;
}
INCR;
cpExplicit += *it;
} else if (it->compare(ARG_NAME_SERVER) == 0
|| it->compare(ARG_NAME_CLIENT) == 0) {
javaOptions.push_back(it->substr(1)); // to JVMLauncher, -server instead of --server
} else if (it->compare(ARG_NAME_SAMPLE) == 0) {
javaOptions.push_back("-Xprof");
} else if (it->compare(ARG_NAME_MANAGE) == 0) {
javaOptions.push_back("-Dcom.sun.management.jmxremote");
javaOptions.push_back("-Djruby.management.enabled=true");
} else if (it->compare(ARG_NAME_HEADLESS) == 0) {
javaOptions.push_back("-Djava.awt.headless=true");
} else if (it->compare(ARG_NAME_NG) == 0) {
nailgunClient = true;
} else if (it->compare(ARG_NAME_NG_SERVER) == 0) {
bootclass = "com/martiansoftware/nailgun/NGServer";
javaOptions.push_back("-server");
noBootClassPath = true;
} else if (it->compare(0, 2, "-J", 2) == 0) {
std::string javaOpt = it->substr(2);
if (javaOpt.compare(0, 3, "-ea", 3) == 0
|| javaOpt.compare(0, 17, "-enableassertions", 17) == 0) {
logMsg("Note: -ea option is specified, there will be no bootclasspath in order to enable assertions");
noBootClassPath = true;
}
javaOptions.push_back(javaOpt);
} else if (strcmp(it->c_str(), "-Xhelp") == 0) {
printToConsole(HELP_MSG);
if (!appendHelp.empty()) {
printToConsole(appendHelp.c_str());
}
return false;
} else if (strcmp(it->c_str(), "-Xversion") == 0) {
printToConsole("JRuby Launcher Version " JRUBY_LAUNCHER_VERSION "\n");
return false;
} else {
progArgs.push_back(*it);
}
}
return true;
}
void ArgParser::prepareOptions() {
string option = OPT_JDK_HOME;
option += jdkhome;
javaOptions.push_back(option);
option = OPT_JRUBY_HOME;
option += platformDir;
javaOptions.push_back(option);
option = OPT_JRUBY_SCRIPT;
option += "jruby";
javaOptions.push_back(option);
option = OPT_JRUBY_SHELL;
#ifdef WIN32
option += "cmd.exe";
#else
const char* shell = getenv("SHELL");
if (shell == NULL) {
shell = "/bin/sh";
}
option += shell;
#endif
javaOptions.push_back(option);
option = OPT_JFFI_PATH;
#ifdef WIN32
option += (platformDir + "\\lib\\native\\i386-Windows;"
+ platformDir + "\\lib\\native\\x86_64-Windows");
#else
struct utsname name;
if (uname(&name) == 0) {
string ffiPath, ffiBase(platformDir + "/lib/native");
DIR* dir = opendir(ffiBase.c_str());
struct dirent* ent;
if (dir != NULL) {
while ((ent = readdir(dir)) != NULL) {
string entry(ent->d_name);
if (entry.find(name.sysname) != string::npos) {
if (!ffiPath.empty()) {
ffiPath += PATH_SEP;
}
ffiPath += ffiBase + FILE_SEP + entry;
}
}
closedir(dir);
}
option += ffiPath;
}
#endif
javaOptions.push_back(option);
setupMaxHeapAndStack();
constructBootClassPath();
constructClassPath();
if (bootclass.empty()) {
bootclass = MAIN_CLASS;
}
// replace '/' by '.' to report a better name to jps/jconsole
string cmdName = bootclass;
size_t position = cmdName.find("/");
while (position != string::npos) {
cmdName.replace(position, 1, ".");
position = cmdName.find("/", position + 1);
}
option = OPT_JRUBY_COMMAND_NAME;
option += cmdName;
javaOptions.push_back(option);
option = OPT_CLASS_PATH;
option += classPath;
javaOptions.push_back(option);
if (!bootClassPath.empty()) {
option = OPT_BOOT_CLASS_PATH;
option += bootClassPath;
javaOptions.push_back(option);
}
}
void ArgParser::setupMaxHeapAndStack() {
// Hard-coded 500m, 1024k is for consistency with jruby shell script.
string heapSize("500m"), stackSize("1024k");
bool maxHeap = false, maxStack = false;
for (list<string>::iterator it = javaOptions.begin(); it != javaOptions.end(); it++) {
if (!maxHeap && strncmp("-Xmx", it->c_str(), 4) == 0) {
heapSize = it->substr(4, it->size() - 4);
maxHeap = true;
}
if (!maxStack && strncmp("-Xss", it->c_str(), 4) == 0) {
stackSize = it->substr(4, it->size() - 4);
maxStack = true;
}
}
if (!maxHeap) {
javaOptions.push_back("-Xmx" + heapSize);
}
if (!maxStack) {
javaOptions.push_back("-Xss" + stackSize);
}
javaOptions.push_back("-Djruby.memory.max=" + heapSize);
javaOptions.push_back("-Djruby.stack.max=" + stackSize);
}
void ArgParser::constructBootClassPath() {
logMsg("constructBootClassPath()");
addedToBootCP.clear();
addedToCP.clear();
classPath = cpBefore;
string jruby_complete_jar = platformDir + FILE_SEP + "lib" + FILE_SEP + "jruby-complete.jar";
string jruby_jar = platformDir + FILE_SEP + "lib" + FILE_SEP + "jruby.jar";
if (fileExists(jruby_jar.c_str())) {
addToBootClassPath(jruby_jar.c_str(), true);
if (fileExists(jruby_complete_jar.c_str())) {
printToConsole("WARNING: Both jruby-complete.jar and jruby.jar are present in the 'lib' directory. Will use jruby.jar\n");
}
} else if (fileExists(jruby_complete_jar.c_str())) {
addToBootClassPath(jruby_complete_jar.c_str());
}
logMsg("BootclassPath: %s", bootClassPath.c_str());
}
void ArgParser::constructClassPath() {
logMsg("constructClassPath()");
addJarsToClassPathFrom(platformDir.c_str());
if (cpExplicit.empty()) {
logMsg("No explicit classpath option is used, looking up %%CLASSPATH%% env");
char *envCP = getenv("CLASSPATH");
if (envCP) {
addToClassPath(envCP, false);
}
} else {
logMsg("Explicit classpath option is used, ignoring %%CLASSPATH%% env");
addToClassPath(cpExplicit.c_str(), false);
}
if (!cpAfter.empty()) {
classPath += PATH_SEP;
classPath += cpAfter;
}
// JRUBY-4709: Include this by default to have PWD as part of classpath
if (!classPath.empty()) {
classPath += PATH_SEP;
}
logMsg("ClassPath: %s", classPath.c_str());
}
void ArgParser::addJarsToClassPathFrom(const char *dir) {
logMsg("addJarsToClassPathFrom()\n\tdir: %s", dir);
string path = dir;
path += FILE_SEP;
path += "lib";
#ifdef WIN32
WIN32_FIND_DATA fd = {0};
string patternPath = path + FILE_SEP + "*.jar";
HANDLE hFind = FindFirstFile(patternPath.c_str(), &fd);
if (hFind == INVALID_HANDLE_VALUE) {
logMsg("Nothing found (%s)", patternPath.c_str());
return;
}
do {
string fullName = path + FILE_SEP + fd.cFileName;
addToClassPath(fullName.c_str());
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
#else
DIR *directory = opendir(path.c_str());
if (!directory) {
logMsg("Nothing found (%s)", path.c_str());
return;
}
struct dirent *ent;
while((ent = readdir(directory)) != NULL) {
int len = strlen(ent->d_name);
if (len > 4 && strncmp(".jar", (ent->d_name + (len - 4)), 4) == 0) {
string fullName = path + FILE_SEP + ent->d_name;
addToClassPath(fullName.c_str());
}
}
closedir(directory);
#endif
}
void ArgParser::addToClassPath(const char *path, bool onlyIfExists) {
logMsg("addToClassPath()\n\tpath: %s\n\tonlyIfExists: %s", path, onlyIfExists ? "true" : "false");
if (onlyIfExists && !fileExists(path)) {
return;
}
if (!addedToCP.insert(path).second) {
logMsg("\"%s\" already added, skipping", path);
return;
}
// check that this hasn't been added to boot class path already
if (addedToBootCP.find(path) == addedToBootCP.end()) {
if (!classPath.empty()) {
classPath += PATH_SEP;
}
classPath += path;
} else {
logMsg("No need to add \"%s\" to classpath, it's already in bootclasspath", path);
}
}
void ArgParser::addToBootClassPath(const char *path, bool onlyIfExists) {
logMsg("addToBootClassPath()\n\tpath: %s\n\tonlyIfExists: %s", path, onlyIfExists ? "true" : "false");
if (noBootClassPath) {
logMsg("NOTE: In this mode there is no bootclasspath, adding to the classpath instead...");
return addToClassPath(path, onlyIfExists);
}
if (onlyIfExists && !fileExists(path)) {
return;
}
// only add non-duplicates
if (addedToBootCP.insert(path).second) {
if (!bootClassPath.empty()) {
bootClassPath += PATH_SEP;
}
bootClassPath += path;
} else {
logMsg("\"%s\" already in bootclasspath", path);
}
}
void ArgParser::appendToHelp(const char *msg) {
if (msg) {
appendHelp = msg;
}
}
void ArgParser::addOptionsToCommandLine(list<string> & commandLine) {
commandLine.insert(commandLine.end(), javaOptions.begin(), javaOptions.end());
commandLine.insert(commandLine.end(), bootclass);
commandLine.insert(commandLine.end(), progArgs.begin(), progArgs.end());
}