author | henryjen |
Fri, 28 Sep 2018 13:15:01 -0700 | |
changeset 51958 | 53a4760e9fcc |
parent 50469 | 1476689320e0 |
child 59020 | aebd72de84b0 |
permissions | -rw-r--r-- |
32267 | 1 |
/* |
49440 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
32267 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
26 |
#include <stdlib.h> |
32267 | 27 |
#include <stdio.h> |
28 |
#include <assert.h> |
|
29 |
#include <sys/stat.h> |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
30 |
#include <ctype.h> |
32267 | 31 |
|
32 |
#ifdef DEBUG_ARGFILE |
|
33 |
#ifndef NO_JNI |
|
34 |
#define NO_JNI |
|
35 |
#endif |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
36 |
#define JLI_ReportMessage(...) printf(__VA_ARGS__) |
43691
f3c0d65c7b17
8173712: Rename JAVA_OPTIONS environment variable to JDK_JAVA_OPTIONS
mchung
parents:
43340
diff
changeset
|
37 |
#define JDK_JAVA_OPTIONS "JDK_JAVA_OPTIONS" |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
38 |
int IsWhiteSpaceOption(const char* name) { return 1; } |
32267 | 39 |
#else |
40 |
#include "java.h" |
|
49440 | 41 |
#include "jni.h" |
32267 | 42 |
#endif |
43 |
||
44 |
#include "jli_util.h" |
|
45 |
#include "emessages.h" |
|
46 |
||
47 |
#define MAX_ARGF_SIZE 0x7fffffffL |
|
48 |
||
49 |
static char* clone_substring(const char *begin, size_t len) { |
|
50 |
char *rv = (char *) JLI_MemAlloc(len + 1); |
|
51 |
memcpy(rv, begin, len); |
|
52 |
rv[len] = '\0'; |
|
53 |
return rv; |
|
54 |
} |
|
55 |
||
56 |
enum STATE { |
|
57 |
FIND_NEXT, |
|
58 |
IN_COMMENT, |
|
59 |
IN_QUOTE, |
|
60 |
IN_ESCAPE, |
|
61 |
SKIP_LEAD_WS, |
|
62 |
IN_TOKEN |
|
63 |
}; |
|
64 |
||
65 |
typedef struct { |
|
66 |
enum STATE state; |
|
67 |
const char* cptr; |
|
68 |
const char* eob; |
|
69 |
char quote_char; |
|
70 |
JLI_List parts; |
|
71 |
} __ctx_args; |
|
72 |
||
73 |
#define NOT_FOUND -1 |
|
74 |
static int firstAppArgIndex = NOT_FOUND; |
|
75 |
||
76 |
static jboolean expectingNoDashArg = JNI_FALSE; |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
77 |
// Initialize to 1, as the first argument is the app name and not preprocessed |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
78 |
static size_t argsCount = 1; |
32267 | 79 |
static jboolean stopExpansion = JNI_FALSE; |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
80 |
static jboolean relaunch = JNI_FALSE; |
32267 | 81 |
|
50453 | 82 |
/* |
83 |
* Prototypes for internal functions. |
|
84 |
*/ |
|
85 |
static jboolean expand(JLI_List args, const char *str, const char *var_name); |
|
86 |
||
49440 | 87 |
JNIEXPORT void JNICALL |
88 |
JLI_InitArgProcessing(jboolean hasJavaArgs, jboolean disableArgFile) { |
|
32267 | 89 |
// No expansion for relaunch |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
90 |
if (argsCount != 1) { |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
91 |
relaunch = JNI_TRUE; |
32267 | 92 |
stopExpansion = JNI_TRUE; |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
93 |
argsCount = 1; |
32267 | 94 |
} else { |
95 |
stopExpansion = disableArgFile; |
|
96 |
} |
|
97 |
||
98 |
expectingNoDashArg = JNI_FALSE; |
|
99 |
||
100 |
// for tools, this value remains 0 all the time. |
|
45463
d9325138bac6
8180334: Unable to build jaotc launcher on windows
ksrini
parents:
45252
diff
changeset
|
101 |
firstAppArgIndex = hasJavaArgs ? 0: NOT_FOUND; |
32267 | 102 |
} |
103 |
||
49440 | 104 |
JNIEXPORT int JNICALL |
105 |
JLI_GetAppArgIndex() { |
|
32267 | 106 |
// Will be 0 for tools |
107 |
return firstAppArgIndex; |
|
108 |
} |
|
109 |
||
110 |
static void checkArg(const char *arg) { |
|
111 |
size_t idx = 0; |
|
112 |
argsCount++; |
|
113 |
||
114 |
// All arguments arrive here must be a launcher argument, |
|
115 |
// ie. by now, all argfile expansions must have been performed. |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
116 |
if (*arg == '-') { |
32267 | 117 |
expectingNoDashArg = JNI_FALSE; |
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
118 |
if (IsWhiteSpaceOption(arg)) { |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
119 |
// expect an argument |
32267 | 120 |
expectingNoDashArg = JNI_TRUE; |
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
121 |
|
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
122 |
if (JLI_StrCmp(arg, "-jar") == 0 || |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
123 |
JLI_StrCmp(arg, "--module") == 0 || |
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
124 |
JLI_StrCmp(arg, "-m") == 0) { |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
125 |
// This is tricky, we do expect NoDashArg |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
126 |
// But that is considered main class to stop expansion |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
127 |
expectingNoDashArg = JNI_FALSE; |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
128 |
// We can not just update the idx here because if -jar @file |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
129 |
// still need expansion of @file to get the argument for -jar |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39640
diff
changeset
|
130 |
} |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
131 |
} else if (JLI_StrCmp(arg, "--disable-@files") == 0) { |
32267 | 132 |
stopExpansion = JNI_TRUE; |
133 |
} |
|
134 |
} else { |
|
135 |
if (!expectingNoDashArg) { |
|
136 |
// this is main class, argsCount is index to next arg |
|
137 |
idx = argsCount; |
|
138 |
} |
|
139 |
expectingNoDashArg = JNI_FALSE; |
|
140 |
} |
|
141 |
// only update on java mode and not yet found main class |
|
39640
6f1a839ff43d
8132379: -J options can cause crash or "Warning: app args parsing error passing arguments as-is"
henryjen
parents:
36511
diff
changeset
|
142 |
if (firstAppArgIndex == NOT_FOUND && idx != 0) { |
32267 | 143 |
firstAppArgIndex = (int) idx; |
144 |
} |
|
145 |
} |
|
146 |
||
147 |
/* |
|
148 |
[\n\r] +------------+ +------------+ [\n\r] |
|
149 |
+---------+ IN_COMMENT +<------+ | IN_ESCAPE +---------+ |
|
150 |
| +------------+ | +------------+ | |
|
151 |
| [#] ^ |[#] ^ | | |
|
152 |
| +----------+ | [\\]| |[^\n\r] | |
|
153 |
v | | | v | |
|
154 |
+------------+ [^ \t\n\r\f] +------------+['"]> +------------+ | |
|
155 |
| FIND_NEXT +-------------->+ IN_TOKEN +-----------+ IN_QUOTE + | |
|
156 |
+------------+ +------------+ <[quote]+------------+ | |
|
157 |
| ^ | | ^ ^ | |
|
158 |
| | [ \t\n\r\f]| [\n\r]| | |[^ \t\n\r\f]v |
|
159 |
| +--------------------------+-----------------------+ | +--------------+ |
|
160 |
| ['"] | | SKIP_LEAD_WS | |
|
161 |
+---------------------------------------------------------+ +--------------+ |
|
162 |
*/ |
|
163 |
static char* nextToken(__ctx_args *pctx) { |
|
164 |
const char* nextc = pctx->cptr; |
|
165 |
const char* const eob = pctx->eob; |
|
166 |
const char* anchor = nextc; |
|
167 |
char *token; |
|
168 |
||
169 |
for (; nextc < eob; nextc++) { |
|
170 |
register char ch = *nextc; |
|
171 |
||
172 |
// Skip white space characters |
|
173 |
if (pctx->state == FIND_NEXT || pctx->state == SKIP_LEAD_WS) { |
|
174 |
while (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f') { |
|
175 |
nextc++; |
|
176 |
if (nextc >= eob) { |
|
177 |
return NULL; |
|
178 |
} |
|
179 |
ch = *nextc; |
|
180 |
} |
|
181 |
pctx->state = (pctx->state == FIND_NEXT) ? IN_TOKEN : IN_QUOTE; |
|
182 |
anchor = nextc; |
|
183 |
// Deal with escape sequences |
|
184 |
} else if (pctx->state == IN_ESCAPE) { |
|
185 |
// concatenation directive |
|
186 |
if (ch == '\n' || ch == '\r') { |
|
187 |
pctx->state = SKIP_LEAD_WS; |
|
188 |
} else { |
|
189 |
// escaped character |
|
190 |
char* escaped = (char*) JLI_MemAlloc(2 * sizeof(char)); |
|
191 |
escaped[1] = '\0'; |
|
192 |
switch (ch) { |
|
193 |
case 'n': |
|
194 |
escaped[0] = '\n'; |
|
195 |
break; |
|
196 |
case 'r': |
|
197 |
escaped[0] = '\r'; |
|
198 |
break; |
|
199 |
case 't': |
|
200 |
escaped[0] = '\t'; |
|
201 |
break; |
|
202 |
case 'f': |
|
203 |
escaped[0] = '\f'; |
|
204 |
break; |
|
205 |
default: |
|
206 |
escaped[0] = ch; |
|
207 |
break; |
|
208 |
} |
|
209 |
JLI_List_add(pctx->parts, escaped); |
|
210 |
pctx->state = IN_QUOTE; |
|
211 |
} |
|
212 |
// anchor to next character |
|
213 |
anchor = nextc + 1; |
|
214 |
continue; |
|
215 |
// ignore comment to EOL |
|
216 |
} else if (pctx->state == IN_COMMENT) { |
|
217 |
while (ch != '\n' && ch != '\r') { |
|
218 |
nextc++; |
|
219 |
if (nextc > eob) { |
|
220 |
return NULL; |
|
221 |
} |
|
222 |
ch = *nextc; |
|
223 |
} |
|
224 |
pctx->state = FIND_NEXT; |
|
225 |
continue; |
|
226 |
} |
|
227 |
||
228 |
assert(pctx->state != IN_ESCAPE); |
|
229 |
assert(pctx->state != FIND_NEXT); |
|
230 |
assert(pctx->state != SKIP_LEAD_WS); |
|
231 |
assert(pctx->state != IN_COMMENT); |
|
232 |
||
233 |
switch(ch) { |
|
234 |
case ' ': |
|
235 |
case '\t': |
|
236 |
case '\f': |
|
237 |
if (pctx->state == IN_QUOTE) { |
|
238 |
continue; |
|
239 |
} |
|
240 |
// fall through |
|
241 |
case '\n': |
|
242 |
case '\r': |
|
243 |
if (pctx->parts->size == 0) { |
|
244 |
token = clone_substring(anchor, nextc - anchor); |
|
245 |
} else { |
|
246 |
JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor); |
|
247 |
token = JLI_List_combine(pctx->parts); |
|
248 |
JLI_List_free(pctx->parts); |
|
249 |
pctx->parts = JLI_List_new(4); |
|
250 |
} |
|
251 |
pctx->cptr = nextc + 1; |
|
252 |
pctx->state = FIND_NEXT; |
|
253 |
return token; |
|
254 |
case '#': |
|
255 |
if (pctx->state == IN_QUOTE) { |
|
256 |
continue; |
|
257 |
} |
|
258 |
pctx->state = IN_COMMENT; |
|
259 |
break; |
|
260 |
case '\\': |
|
261 |
if (pctx->state != IN_QUOTE) { |
|
262 |
continue; |
|
263 |
} |
|
264 |
JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor); |
|
265 |
pctx->state = IN_ESCAPE; |
|
51958
53a4760e9fcc
8210810: Escaped character at specific position in argument file is not handled properly
henryjen
parents:
50469
diff
changeset
|
266 |
// anchor after backslash character |
53a4760e9fcc
8210810: Escaped character at specific position in argument file is not handled properly
henryjen
parents:
50469
diff
changeset
|
267 |
anchor = nextc + 1; |
32267 | 268 |
break; |
269 |
case '\'': |
|
270 |
case '"': |
|
271 |
if (pctx->state == IN_QUOTE && pctx->quote_char != ch) { |
|
272 |
// not matching quote |
|
273 |
continue; |
|
274 |
} |
|
275 |
// partial before quote |
|
276 |
if (anchor != nextc) { |
|
277 |
JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor); |
|
278 |
} |
|
279 |
// anchor after quote character |
|
280 |
anchor = nextc + 1; |
|
281 |
if (pctx->state == IN_TOKEN) { |
|
282 |
pctx->quote_char = ch; |
|
283 |
pctx->state = IN_QUOTE; |
|
284 |
} else { |
|
285 |
pctx->state = IN_TOKEN; |
|
286 |
} |
|
287 |
break; |
|
288 |
default: |
|
289 |
break; |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
assert(nextc == eob); |
|
294 |
if (anchor != nextc) { |
|
295 |
// not yet return until end of stream, we have part of a token. |
|
296 |
JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor); |
|
297 |
} |
|
298 |
return NULL; |
|
299 |
} |
|
300 |
||
301 |
static JLI_List readArgFile(FILE *file) { |
|
302 |
char buf[4096]; |
|
303 |
JLI_List rv; |
|
304 |
__ctx_args ctx; |
|
305 |
size_t size; |
|
306 |
char *token; |
|
307 |
||
308 |
ctx.state = FIND_NEXT; |
|
309 |
ctx.parts = JLI_List_new(4); |
|
50469 | 310 |
// initialize to avoid -Werror=maybe-uninitialized issues from gcc 7.3 onwards. |
311 |
ctx.quote_char = '"'; |
|
32267 | 312 |
|
313 |
/* arbitrarily pick 8, seems to be a reasonable number of arguments */ |
|
314 |
rv = JLI_List_new(8); |
|
315 |
||
316 |
while (!feof(file)) { |
|
317 |
size = fread(buf, sizeof(char), sizeof(buf), file); |
|
318 |
if (ferror(file)) { |
|
319 |
JLI_List_free(rv); |
|
320 |
return NULL; |
|
321 |
} |
|
322 |
||
323 |
/* nextc is next character to read from the buffer |
|
324 |
* eob is the end of input |
|
325 |
* token is the copied token value, NULL if no a complete token |
|
326 |
*/ |
|
327 |
ctx.cptr = buf; |
|
328 |
ctx.eob = buf + size; |
|
329 |
token = nextToken(&ctx); |
|
330 |
while (token != NULL) { |
|
331 |
checkArg(token); |
|
332 |
JLI_List_add(rv, token); |
|
333 |
token = nextToken(&ctx); |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
// remaining partial token |
|
338 |
if (ctx.state == IN_TOKEN || ctx.state == IN_QUOTE) { |
|
339 |
if (ctx.parts->size != 0) { |
|
340 |
JLI_List_add(rv, JLI_List_combine(ctx.parts)); |
|
341 |
} |
|
342 |
} |
|
343 |
JLI_List_free(ctx.parts); |
|
344 |
||
345 |
return rv; |
|
346 |
} |
|
347 |
||
348 |
/* |
|
349 |
* if the arg represent a file, that is, prefix with a single '@', |
|
350 |
* return a list of arguments from the file. |
|
351 |
* otherwise, return NULL. |
|
352 |
*/ |
|
353 |
static JLI_List expandArgFile(const char *arg) { |
|
354 |
FILE *fptr; |
|
355 |
struct stat st; |
|
356 |
JLI_List rv; |
|
357 |
||
358 |
/* failed to access the file */ |
|
359 |
if (stat(arg, &st) != 0) { |
|
360 |
JLI_ReportMessage(CFG_ERROR6, arg); |
|
361 |
exit(1); |
|
362 |
} |
|
363 |
||
364 |
if (st.st_size > MAX_ARGF_SIZE) { |
|
365 |
JLI_ReportMessage(CFG_ERROR10, MAX_ARGF_SIZE); |
|
366 |
exit(1); |
|
367 |
} |
|
368 |
||
369 |
fptr = fopen(arg, "r"); |
|
370 |
/* arg file cannot be openned */ |
|
371 |
if (fptr == NULL) { |
|
372 |
JLI_ReportMessage(CFG_ERROR6, arg); |
|
373 |
exit(1); |
|
374 |
} |
|
375 |
||
376 |
rv = readArgFile(fptr); |
|
377 |
fclose(fptr); |
|
378 |
||
379 |
/* error occurred reading the file */ |
|
380 |
if (rv == NULL) { |
|
381 |
JLI_ReportMessage(DLL_ERROR4, arg); |
|
382 |
exit(1); |
|
383 |
} |
|
384 |
||
385 |
return rv; |
|
386 |
} |
|
387 |
||
50453 | 388 |
/* |
389 |
* expand a string into a list of words separated by whitespace. |
|
390 |
*/ |
|
391 |
static JLI_List expandArg(const char *arg) { |
|
392 |
JLI_List rv; |
|
393 |
||
394 |
/* arbitrarily pick 8, seems to be a reasonable number of arguments */ |
|
395 |
rv = JLI_List_new(8); |
|
396 |
||
397 |
expand(rv, arg, NULL); |
|
398 |
||
399 |
return rv; |
|
400 |
} |
|
401 |
||
49440 | 402 |
JNIEXPORT JLI_List JNICALL |
50453 | 403 |
JLI_PreprocessArg(const char *arg, jboolean expandSourceOpt) { |
32267 | 404 |
JLI_List rv; |
405 |
||
406 |
if (firstAppArgIndex > 0) { |
|
407 |
// In user application arg, no more work. |
|
408 |
return NULL; |
|
409 |
} |
|
410 |
||
411 |
if (stopExpansion) { |
|
412 |
// still looking for user application arg |
|
413 |
checkArg(arg); |
|
414 |
return NULL; |
|
415 |
} |
|
416 |
||
50453 | 417 |
if (expandSourceOpt |
418 |
&& JLI_StrCCmp(arg, "--source") == 0 |
|
419 |
&& JLI_StrChr(arg, ' ') != NULL) { |
|
420 |
return expandArg(arg); |
|
421 |
} |
|
422 |
||
32267 | 423 |
if (arg[0] != '@') { |
424 |
checkArg(arg); |
|
425 |
return NULL; |
|
426 |
} |
|
427 |
||
428 |
if (arg[1] == '\0') { |
|
429 |
// @ by itself is an argument |
|
430 |
checkArg(arg); |
|
431 |
return NULL; |
|
432 |
} |
|
433 |
||
434 |
arg++; |
|
435 |
if (arg[0] == '@') { |
|
436 |
// escaped @argument |
|
437 |
rv = JLI_List_new(1); |
|
438 |
checkArg(arg); |
|
439 |
JLI_List_add(rv, JLI_StringDup(arg)); |
|
440 |
} else { |
|
441 |
rv = expandArgFile(arg); |
|
442 |
} |
|
443 |
return rv; |
|
444 |
} |
|
445 |
||
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
446 |
int isTerminalOpt(char *arg) { |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
447 |
return JLI_StrCmp(arg, "-jar") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
448 |
JLI_StrCmp(arg, "-m") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
449 |
JLI_StrCmp(arg, "--module") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
450 |
JLI_StrCmp(arg, "--dry-run") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
451 |
JLI_StrCmp(arg, "-h") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
452 |
JLI_StrCmp(arg, "-?") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
453 |
JLI_StrCmp(arg, "-help") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
454 |
JLI_StrCmp(arg, "--help") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
455 |
JLI_StrCmp(arg, "-X") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
456 |
JLI_StrCmp(arg, "--help-extra") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
457 |
JLI_StrCmp(arg, "-version") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
458 |
JLI_StrCmp(arg, "--version") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
459 |
JLI_StrCmp(arg, "-fullversion") == 0 || |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
460 |
JLI_StrCmp(arg, "--full-version") == 0; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
461 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
462 |
|
49440 | 463 |
JNIEXPORT jboolean JNICALL |
464 |
JLI_AddArgsFromEnvVar(JLI_List args, const char *var_name) { |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
465 |
char *env = getenv(var_name); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
466 |
|
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
467 |
if (firstAppArgIndex == 0) { |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
468 |
// Not 'java', return |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
469 |
return JNI_FALSE; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
470 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
471 |
|
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
472 |
if (relaunch) { |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
473 |
return JNI_FALSE; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
474 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
475 |
|
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
476 |
if (NULL == env) { |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
477 |
return JNI_FALSE; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
478 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
479 |
|
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
480 |
JLI_ReportMessage(ARG_INFO_ENVVAR, var_name, env); |
50453 | 481 |
return expand(args, env, var_name); |
482 |
} |
|
483 |
||
484 |
/* |
|
485 |
* Expand a string into a list of args. |
|
486 |
* If the string is the result of looking up an environment variable, |
|
487 |
* var_name should be set to the name of that environment variable, |
|
488 |
* for use if needed in error messages. |
|
489 |
*/ |
|
490 |
||
491 |
static jboolean expand(JLI_List args, const char *str, const char *var_name) { |
|
492 |
jboolean inEnvVar = (var_name != NULL); |
|
493 |
||
494 |
char *p, *arg; |
|
495 |
char quote; |
|
496 |
JLI_List argsInFile; |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
497 |
|
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
498 |
// This is retained until the process terminates as it is saved as the args |
50453 | 499 |
p = JLI_MemAlloc(JLI_StrLen(str) + 1); |
500 |
while (*str != '\0') { |
|
501 |
while (*str != '\0' && isspace(*str)) { |
|
502 |
str++; |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
503 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
504 |
|
45252
654115968257
8180447: Trailing space in JDK_JAVA_OPTIONS causes an application fail to launch
henryjen
parents:
43691
diff
changeset
|
505 |
// Trailing space |
50453 | 506 |
if (*str == '\0') { |
45252
654115968257
8180447: Trailing space in JDK_JAVA_OPTIONS causes an application fail to launch
henryjen
parents:
43691
diff
changeset
|
507 |
break; |
654115968257
8180447: Trailing space in JDK_JAVA_OPTIONS causes an application fail to launch
henryjen
parents:
43691
diff
changeset
|
508 |
} |
654115968257
8180447: Trailing space in JDK_JAVA_OPTIONS causes an application fail to launch
henryjen
parents:
43691
diff
changeset
|
509 |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
510 |
arg = p; |
50453 | 511 |
while (*str != '\0' && !isspace(*str)) { |
512 |
if (inEnvVar && (*str == '"' || *str == '\'')) { |
|
513 |
quote = *str++; |
|
514 |
while (*str != quote && *str != '\0') { |
|
515 |
*p++ = *str++; |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
516 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
517 |
|
50453 | 518 |
if (*str == '\0') { |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
519 |
JLI_ReportMessage(ARG_ERROR8, var_name); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
520 |
exit(1); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
521 |
} |
50453 | 522 |
str++; |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
523 |
} else { |
50453 | 524 |
*p++ = *str++; |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
525 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
526 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
527 |
|
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
528 |
*p++ = '\0'; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
529 |
|
50453 | 530 |
argsInFile = JLI_PreprocessArg(arg, JNI_FALSE); |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
531 |
|
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
532 |
if (NULL == argsInFile) { |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
533 |
if (isTerminalOpt(arg)) { |
50453 | 534 |
if (inEnvVar) { |
535 |
JLI_ReportMessage(ARG_ERROR9, arg, var_name); |
|
536 |
} else { |
|
537 |
JLI_ReportMessage(ARG_ERROR15, arg); |
|
538 |
} |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
539 |
exit(1); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
540 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
541 |
JLI_List_add(args, arg); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
542 |
} else { |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
543 |
size_t cnt, idx; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
544 |
char *argFile = arg; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
545 |
cnt = argsInFile->size; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
546 |
for (idx = 0; idx < cnt; idx++) { |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
547 |
arg = argsInFile->elements[idx]; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
548 |
if (isTerminalOpt(arg)) { |
50453 | 549 |
if (inEnvVar) { |
550 |
JLI_ReportMessage(ARG_ERROR10, arg, argFile, var_name); |
|
551 |
} else { |
|
552 |
JLI_ReportMessage(ARG_ERROR16, arg, argFile); |
|
553 |
} |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
554 |
exit(1); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
555 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
556 |
JLI_List_add(args, arg); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
557 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
558 |
// Shallow free, we reuse the string to avoid copy |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
559 |
JLI_MemFree(argsInFile->elements); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
560 |
JLI_MemFree(argsInFile); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
561 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
562 |
/* |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
563 |
* Check if main-class is specified after argument being checked. It |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
564 |
* must always appear after expansion, as a main-class could be specified |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
565 |
* indirectly into environment variable via an @argfile, and it must be |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
566 |
* caught now. |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
567 |
*/ |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
568 |
if (firstAppArgIndex != NOT_FOUND) { |
50453 | 569 |
if (inEnvVar) { |
570 |
JLI_ReportMessage(ARG_ERROR11, var_name); |
|
571 |
} else { |
|
572 |
JLI_ReportMessage(ARG_ERROR17); |
|
573 |
} |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
574 |
exit(1); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
575 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
576 |
|
50453 | 577 |
assert (*str == '\0' || isspace(*str)); |
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
578 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
579 |
|
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
580 |
return JNI_TRUE; |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
581 |
} |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
40261
diff
changeset
|
582 |
|
32267 | 583 |
#ifdef DEBUG_ARGFILE |
584 |
/* |
|
585 |
* Stand-alone sanity test, build with following command line |
|
586 |
* $ CC -DDEBUG_ARGFILE -DNO_JNI -g args.c jli_util.c |
|
587 |
*/ |
|
588 |
||
589 |
void fail(char *expected, char *actual, size_t idx) { |
|
590 |
printf("FAILED: Token[%lu] expected to be <%s>, got <%s>\n", idx, expected, actual); |
|
591 |
exit(1); |
|
592 |
} |
|
593 |
||
594 |
void test_case(char *case_data, char **tokens, size_t cnt_tokens) { |
|
595 |
size_t actual_cnt; |
|
596 |
char *token; |
|
597 |
__ctx_args ctx; |
|
598 |
||
599 |
actual_cnt = 0; |
|
600 |
||
601 |
ctx.state = FIND_NEXT; |
|
602 |
ctx.parts = JLI_List_new(4); |
|
603 |
ctx.cptr = case_data; |
|
604 |
ctx.eob = case_data + strlen(case_data); |
|
605 |
||
606 |
printf("Test case: <%s>, expected %lu tokens.\n", case_data, cnt_tokens); |
|
607 |
||
608 |
for (token = nextToken(&ctx); token != NULL; token = nextToken(&ctx)) { |
|
609 |
// should not have more tokens than expected |
|
610 |
if (actual_cnt >= cnt_tokens) { |
|
611 |
printf("FAILED: Extra token detected: <%s>\n", token); |
|
612 |
exit(2); |
|
613 |
} |
|
614 |
if (JLI_StrCmp(token, tokens[actual_cnt]) != 0) { |
|
615 |
fail(tokens[actual_cnt], token, actual_cnt); |
|
616 |
} |
|
617 |
actual_cnt++; |
|
618 |
} |
|
619 |
||
620 |
char* last = NULL; |
|
621 |
if (ctx.parts->size != 0) { |
|
622 |
last = JLI_List_combine(ctx.parts); |
|
623 |
} |
|
624 |
JLI_List_free(ctx.parts); |
|
625 |
||
626 |
if (actual_cnt >= cnt_tokens) { |
|
627 |
// same number of tokens, should have nothing left to parse |
|
628 |
if (last != NULL) { |
|
629 |
if (*last != '#') { |
|
630 |
printf("Leftover detected: %s", last); |
|
631 |
exit(2); |
|
632 |
} |
|
633 |
} |
|
634 |
} else { |
|
635 |
if (JLI_StrCmp(last, tokens[actual_cnt]) != 0) { |
|
636 |
fail(tokens[actual_cnt], last, actual_cnt); |
|
637 |
} |
|
638 |
actual_cnt++; |
|
639 |
} |
|
640 |
if (actual_cnt != cnt_tokens) { |
|
641 |
printf("FAILED: Number of tokens not match, expected %lu, got %lu\n", |
|
642 |
cnt_tokens, actual_cnt); |
|
643 |
exit(3); |
|
644 |
} |
|
645 |
||
646 |
printf("PASS\n"); |
|
647 |
} |
|
648 |
||
649 |
#define DO_CASE(name) \ |
|
650 |
test_case(name[0], name + 1, sizeof(name)/sizeof(char*) - 1) |
|
651 |
||
652 |
int main(int argc, char** argv) { |
|
653 |
size_t i, j; |
|
654 |
||
655 |
char* case1[] = { "-version -cp \"c:\\\\java libs\\\\one.jar\" \n", |
|
656 |
"-version", "-cp", "c:\\java libs\\one.jar" }; |
|
657 |
DO_CASE(case1); |
|
658 |
||
659 |
// note the open quote at the end |
|
660 |
char* case2[] = { "com.foo.Panda \"Furious 5\"\fand\t'Shi Fu' \"escape\tprison", |
|
661 |
"com.foo.Panda", "Furious 5", "and", "Shi Fu", "escape\tprison"}; |
|
662 |
DO_CASE(case2); |
|
663 |
||
664 |
char* escaped_chars[] = { "escaped chars testing \"\\a\\b\\c\\f\\n\\r\\t\\v\\9\\6\\23\\82\\28\\377\\477\\278\\287\"", |
|
665 |
"escaped", "chars", "testing", "abc\f\n\r\tv96238228377477278287"}; |
|
666 |
DO_CASE(escaped_chars); |
|
667 |
||
668 |
char* mixed_quote[] = { "\"mix 'single quote' in double\" 'mix \"double quote\" in single' partial\"quote me\"this", |
|
669 |
"mix 'single quote' in double", "mix \"double quote\" in single", "partialquote methis"}; |
|
670 |
DO_CASE(mixed_quote); |
|
671 |
||
672 |
char* comments[] = { "line one #comment\n'line #2' #rest are comment\r\n#comment on line 3\nline 4 #comment to eof", |
|
673 |
"line", "one", "line #2", "line", "4"}; |
|
674 |
DO_CASE(comments); |
|
675 |
||
676 |
char* open_quote[] = { "This is an \"open quote \n across line\n\t, note for WS.", |
|
677 |
"This", "is", "an", "open quote ", "across", "line", ",", "note", "for", "WS." }; |
|
678 |
DO_CASE(open_quote); |
|
679 |
||
680 |
char* escape_in_open_quote[] = { "Try \"this \\\\\\\\ escape\\n double quote \\\" in open quote", |
|
681 |
"Try", "this \\\\ escape\n double quote \" in open quote" }; |
|
682 |
DO_CASE(escape_in_open_quote); |
|
683 |
||
684 |
char* quote[] = { "'-Dmy.quote.single'='Property in single quote. Here a double quote\" Add some slashes \\\\/'", |
|
685 |
"-Dmy.quote.single=Property in single quote. Here a double quote\" Add some slashes \\/" }; |
|
686 |
DO_CASE(quote); |
|
687 |
||
688 |
char* multi[] = { "\"Open quote to \n new \"line \\\n\r third\\\n\r\\\tand\ffourth\"", |
|
689 |
"Open quote to ", "new", "line third\tand\ffourth" }; |
|
690 |
DO_CASE(multi); |
|
691 |
||
692 |
char* escape_quote[] = { "c:\\\"partial quote\"\\lib", |
|
693 |
"c:\\partial quote\\lib" }; |
|
694 |
DO_CASE(escape_quote); |
|
695 |
||
696 |
if (argc > 1) { |
|
697 |
for (i = 0; i < argc; i++) { |
|
50453 | 698 |
JLI_List tokens = JLI_PreprocessArg(argv[i], JNI_FALSE); |
32267 | 699 |
if (NULL != tokens) { |
700 |
for (j = 0; j < tokens->size; j++) { |
|
701 |
printf("Token[%lu]: <%s>\n", (unsigned long) j, tokens->elements[j]); |
|
702 |
} |
|
703 |
} |
|
704 |
} |
|
705 |
} |
|
706 |
} |
|
707 |
||
708 |
#endif // DEBUG_ARGFILE |