|
25 | 25 | package com.oracle.svm.driver;
|
26 | 26 |
|
27 | 27 | import java.io.File;
|
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.charset.StandardCharsets; |
28 | 30 | import java.nio.file.Files;
|
29 | 31 | import java.nio.file.Path;
|
30 | 32 | import java.nio.file.Paths;
|
| 33 | +import java.util.ArrayList; |
31 | 34 | import java.util.Arrays;
|
32 | 35 | import java.util.List;
|
33 | 36 | import java.util.regex.Pattern;
|
@@ -61,6 +64,7 @@ class DefaultOptionHandler extends NativeImage.OptionHandler<NativeImage> {
|
61 | 64 | }
|
62 | 65 |
|
63 | 66 | boolean useDebugAttach = false;
|
| 67 | + boolean disableAtFiles = false; |
64 | 68 |
|
65 | 69 | private static void singleArgumentCheck(ArgumentQueue args, String arg) {
|
66 | 70 | if (!args.isEmpty()) {
|
@@ -220,6 +224,10 @@ public boolean consume(ArgumentQueue args) {
|
220 | 224 | nativeImage.showNewline();
|
221 | 225 | System.exit(0);
|
222 | 226 | return true;
|
| 227 | + case "--disable-@files": |
| 228 | + args.poll(); |
| 229 | + disableAtFiles = true; |
| 230 | + return true; |
223 | 231 | }
|
224 | 232 |
|
225 | 233 | String debugAttach = "--debug-attach";
|
@@ -306,9 +314,221 @@ public boolean consume(ArgumentQueue args) {
|
306 | 314 | }
|
307 | 315 | return true;
|
308 | 316 | }
|
| 317 | + if (headArg.startsWith("@") && !disableAtFiles) { |
| 318 | + args.poll(); |
| 319 | + headArg = headArg.substring(1); |
| 320 | + Path argFile = Paths.get(headArg); |
| 321 | + NativeImage.NativeImageArgsProcessor processor = nativeImage.new NativeImageArgsProcessor(argFile.toString()); |
| 322 | + readArgFile(argFile).forEach(processor::accept); |
| 323 | + List<String> leftoverArgs = processor.apply(false); |
| 324 | + if (leftoverArgs.size() > 0) { |
| 325 | + NativeImage.showError("Found unrecognized options while parsing argument file '" + argFile + "':\n" + String.join("\n", leftoverArgs)); |
| 326 | + } |
| 327 | + return true; |
| 328 | + } |
309 | 329 | return false;
|
310 | 330 | }
|
311 | 331 |
|
| 332 | + // Ported from JDK11's java.base/share/native/libjli/args.c |
| 333 | + enum PARSER_STATE { |
| 334 | + FIND_NEXT, |
| 335 | + IN_COMMENT, |
| 336 | + IN_QUOTE, |
| 337 | + IN_ESCAPE, |
| 338 | + SKIP_LEAD_WS, |
| 339 | + IN_TOKEN |
| 340 | + } |
| 341 | + |
| 342 | + class CTX_ARGS { |
| 343 | + PARSER_STATE state; |
| 344 | + int cptr; |
| 345 | + int eob; |
| 346 | + char quoteChar; |
| 347 | + List<String> parts; |
| 348 | + String options; |
| 349 | + } |
| 350 | + |
| 351 | + // Ported from JDK11's java.base/share/native/libjli/args.c |
| 352 | + private List<String> readArgFile(Path file) { |
| 353 | + List<String> arguments = new ArrayList<>(); |
| 354 | + // Use of the at sign (@) to recursively interpret files isn't supported. |
| 355 | + arguments.add("--disable-@files"); |
| 356 | + |
| 357 | + String options = null; |
| 358 | + try { |
| 359 | + options = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); |
| 360 | + } catch (IOException e) { |
| 361 | + NativeImage.showError("Error reading argument file", e); |
| 362 | + } |
| 363 | + |
| 364 | + CTX_ARGS ctx = new CTX_ARGS(); |
| 365 | + ctx.state = PARSER_STATE.FIND_NEXT; |
| 366 | + ctx.parts = new ArrayList<>(4); |
| 367 | + ctx.quoteChar = '"'; |
| 368 | + ctx.cptr = 0; |
| 369 | + ctx.eob = options.length(); |
| 370 | + ctx.options = options; |
| 371 | + |
| 372 | + String token = nextToken(ctx); |
| 373 | + while (token != null) { |
| 374 | + arguments.add(token); |
| 375 | + token = nextToken(ctx); |
| 376 | + } |
| 377 | + |
| 378 | + // remaining partial token |
| 379 | + if (ctx.state == PARSER_STATE.IN_TOKEN || ctx.state == PARSER_STATE.IN_QUOTE) { |
| 380 | + if (ctx.parts.size() != 0) { |
| 381 | + token = String.join("", ctx.parts); |
| 382 | + arguments.add(token); |
| 383 | + } |
| 384 | + } |
| 385 | + return arguments; |
| 386 | + } |
| 387 | + |
| 388 | + // Ported from JDK11's java.base/share/native/libjli/args.c |
| 389 | + @SuppressWarnings("fallthrough") |
| 390 | + private static String nextToken(CTX_ARGS ctx) { |
| 391 | + int nextc = ctx.cptr; |
| 392 | + int eob = ctx.eob; |
| 393 | + int anchor = nextc; |
| 394 | + String token; |
| 395 | + |
| 396 | + for (; nextc < eob; nextc++) { |
| 397 | + char ch = ctx.options.charAt(nextc); |
| 398 | + |
| 399 | + // Skip white space characters |
| 400 | + if (ctx.state == PARSER_STATE.FIND_NEXT || ctx.state == PARSER_STATE.SKIP_LEAD_WS) { |
| 401 | + while (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f') { |
| 402 | + nextc++; |
| 403 | + if (nextc >= eob) { |
| 404 | + return null; |
| 405 | + } |
| 406 | + ch = ctx.options.charAt(nextc); |
| 407 | + } |
| 408 | + ctx.state = (ctx.state == PARSER_STATE.FIND_NEXT) ? PARSER_STATE.IN_TOKEN : PARSER_STATE.IN_QUOTE; |
| 409 | + anchor = nextc; |
| 410 | + // Deal with escape sequences |
| 411 | + } else if (ctx.state == PARSER_STATE.IN_ESCAPE) { |
| 412 | + // concatenation directive |
| 413 | + if (ch == '\n' || ch == '\r') { |
| 414 | + ctx.state = PARSER_STATE.SKIP_LEAD_WS; |
| 415 | + } else { |
| 416 | + // escaped character |
| 417 | + char[] escaped = new char[2]; |
| 418 | + escaped[1] = '\0'; |
| 419 | + switch (ch) { |
| 420 | + case 'n': |
| 421 | + escaped[0] = '\n'; |
| 422 | + break; |
| 423 | + case 'r': |
| 424 | + escaped[0] = '\r'; |
| 425 | + break; |
| 426 | + case 't': |
| 427 | + escaped[0] = '\t'; |
| 428 | + break; |
| 429 | + case 'f': |
| 430 | + escaped[0] = '\f'; |
| 431 | + break; |
| 432 | + default: |
| 433 | + escaped[0] = ch; |
| 434 | + break; |
| 435 | + } |
| 436 | + ctx.parts.add(String.valueOf(escaped)); |
| 437 | + ctx.state = PARSER_STATE.IN_QUOTE; |
| 438 | + } |
| 439 | + // anchor to next character |
| 440 | + anchor = nextc + 1; |
| 441 | + continue; |
| 442 | + // ignore comment to EOL |
| 443 | + } else if (ctx.state == PARSER_STATE.IN_COMMENT) { |
| 444 | + while (ch != '\n' && ch != '\r') { |
| 445 | + nextc++; |
| 446 | + if (nextc >= eob) { |
| 447 | + return null; |
| 448 | + } |
| 449 | + ch = ctx.options.charAt(nextc); |
| 450 | + } |
| 451 | + anchor = nextc + 1; |
| 452 | + ctx.state = PARSER_STATE.FIND_NEXT; |
| 453 | + continue; |
| 454 | + } |
| 455 | + |
| 456 | + assert (ctx.state != PARSER_STATE.IN_ESCAPE); |
| 457 | + assert (ctx.state != PARSER_STATE.FIND_NEXT); |
| 458 | + assert (ctx.state != PARSER_STATE.SKIP_LEAD_WS); |
| 459 | + assert (ctx.state != PARSER_STATE.IN_COMMENT); |
| 460 | + |
| 461 | + switch (ch) { |
| 462 | + case ' ': |
| 463 | + case '\t': |
| 464 | + case '\f': |
| 465 | + if (ctx.state == PARSER_STATE.IN_QUOTE) { |
| 466 | + continue; |
| 467 | + } |
| 468 | + // fall through |
| 469 | + case '\n': |
| 470 | + case '\r': |
| 471 | + if (ctx.parts.size() == 0) { |
| 472 | + token = ctx.options.substring(anchor, nextc); |
| 473 | + } else { |
| 474 | + ctx.parts.add(ctx.options.substring(anchor, nextc)); |
| 475 | + token = String.join("", ctx.parts); |
| 476 | + ctx.parts = new ArrayList<>(); |
| 477 | + } |
| 478 | + ctx.cptr = nextc + 1; |
| 479 | + ctx.state = PARSER_STATE.FIND_NEXT; |
| 480 | + return token; |
| 481 | + case '#': |
| 482 | + if (ctx.state == PARSER_STATE.IN_QUOTE) { |
| 483 | + continue; |
| 484 | + } |
| 485 | + ctx.state = PARSER_STATE.IN_COMMENT; |
| 486 | + anchor = nextc + 1; |
| 487 | + break; |
| 488 | + case '\\': |
| 489 | + if (ctx.state != PARSER_STATE.IN_QUOTE) { |
| 490 | + continue; |
| 491 | + } |
| 492 | + ctx.parts.add(ctx.options.substring(anchor, nextc)); |
| 493 | + ctx.state = PARSER_STATE.IN_ESCAPE; |
| 494 | + // anchor after backslash character |
| 495 | + anchor = nextc + 1; |
| 496 | + break; |
| 497 | + case '\'': |
| 498 | + case '"': |
| 499 | + if (ctx.state == PARSER_STATE.IN_QUOTE && ctx.quoteChar != ch) { |
| 500 | + // not matching quote |
| 501 | + continue; |
| 502 | + } |
| 503 | + // partial before quote |
| 504 | + if (anchor != nextc) { |
| 505 | + ctx.parts.add(ctx.options.substring(anchor, nextc)); |
| 506 | + } |
| 507 | + // anchor after quote character |
| 508 | + anchor = nextc + 1; |
| 509 | + if (ctx.state == PARSER_STATE.IN_TOKEN) { |
| 510 | + ctx.quoteChar = ch; |
| 511 | + ctx.state = PARSER_STATE.IN_QUOTE; |
| 512 | + } else { |
| 513 | + ctx.state = PARSER_STATE.IN_TOKEN; |
| 514 | + } |
| 515 | + break; |
| 516 | + default: |
| 517 | + break; |
| 518 | + } |
| 519 | + } |
| 520 | + |
| 521 | + assert (nextc == eob); |
| 522 | + // Only need partial token, not comment or whitespaces |
| 523 | + if (ctx.state == PARSER_STATE.IN_TOKEN || ctx.state == PARSER_STATE.IN_QUOTE) { |
| 524 | + if (anchor < nextc) { |
| 525 | + // not yet return until end of stream, we have part of a token. |
| 526 | + ctx.parts.add(ctx.options.substring(anchor, nextc)); |
| 527 | + } |
| 528 | + } |
| 529 | + return null; |
| 530 | + } |
| 531 | + |
312 | 532 | private void processClasspathArgs(String cpArgs) {
|
313 | 533 | for (String cp : cpArgs.split(File.pathSeparator, Integer.MAX_VALUE)) {
|
314 | 534 | /* Conform to `java` command empty cp entry handling. */
|
|
0 commit comments