annotate src/java.base/share/native/libjli/args.c @ 52874:20e49753c388

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