annotate jdk/src/java.base/share/native/libjli/args.c @ 32267:4e96a9ee01b1

8027634: Support @argfiles for java command-line tool Reviewed-by: ksrini, mchung
author henryjen
date Wed, 08 Jul 2015 23:26:48 -0700
parents
children 9d0388c6b336
rev   line source
henryjen@32267 1 /*
henryjen@32267 2 * Copyright (c) 2015, 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@32267 26 #include <stdio.h>
henryjen@32267 27 #include <assert.h>
henryjen@32267 28 #include <sys/stat.h>
henryjen@32267 29
henryjen@32267 30 #ifdef DEBUG_ARGFILE
henryjen@32267 31 #ifndef NO_JNI
henryjen@32267 32 #define NO_JNI
henryjen@32267 33 #endif
henryjen@32267 34 #define JLI_ReportMessage(p1, p2) printf((p1), (p2))
henryjen@32267 35 #else
henryjen@32267 36 #include "java.h"
henryjen@32267 37 #endif
henryjen@32267 38
henryjen@32267 39 #include "jli_util.h"
henryjen@32267 40 #include "emessages.h"
henryjen@32267 41
henryjen@32267 42 #define MAX_ARGF_SIZE 0x7fffffffL
henryjen@32267 43
henryjen@32267 44 static char* clone_substring(const char *begin, size_t len) {
henryjen@32267 45 char *rv = (char *) JLI_MemAlloc(len + 1);
henryjen@32267 46 memcpy(rv, begin, len);
henryjen@32267 47 rv[len] = '\0';
henryjen@32267 48 return rv;
henryjen@32267 49 }
henryjen@32267 50
henryjen@32267 51 enum STATE {
henryjen@32267 52 FIND_NEXT,
henryjen@32267 53 IN_COMMENT,
henryjen@32267 54 IN_QUOTE,
henryjen@32267 55 IN_ESCAPE,
henryjen@32267 56 SKIP_LEAD_WS,
henryjen@32267 57 IN_TOKEN
henryjen@32267 58 };
henryjen@32267 59
henryjen@32267 60 typedef struct {
henryjen@32267 61 enum STATE state;
henryjen@32267 62 const char* cptr;
henryjen@32267 63 const char* eob;
henryjen@32267 64 char quote_char;
henryjen@32267 65 JLI_List parts;
henryjen@32267 66 } __ctx_args;
henryjen@32267 67
henryjen@32267 68 #define NOT_FOUND -1
henryjen@32267 69 static int firstAppArgIndex = NOT_FOUND;
henryjen@32267 70
henryjen@32267 71 static jboolean expectingNoDashArg = JNI_FALSE;
henryjen@32267 72 static size_t argsCount = 0;
henryjen@32267 73 static jboolean stopExpansion = JNI_FALSE;
henryjen@32267 74
henryjen@32267 75 void JLI_InitArgProcessing(jboolean isJava, jboolean disableArgFile) {
henryjen@32267 76 // No expansion for relaunch
henryjen@32267 77 if (argsCount != 0) {
henryjen@32267 78 stopExpansion = JNI_TRUE;
henryjen@32267 79 argsCount = 0;
henryjen@32267 80 } else {
henryjen@32267 81 stopExpansion = disableArgFile;
henryjen@32267 82 }
henryjen@32267 83
henryjen@32267 84 expectingNoDashArg = JNI_FALSE;
henryjen@32267 85
henryjen@32267 86 // for tools, this value remains 0 all the time.
henryjen@32267 87 firstAppArgIndex = isJava ? NOT_FOUND : 0;
henryjen@32267 88 }
henryjen@32267 89
henryjen@32267 90 int JLI_GetAppArgIndex() {
henryjen@32267 91 // Will be 0 for tools
henryjen@32267 92 return firstAppArgIndex;
henryjen@32267 93 }
henryjen@32267 94
henryjen@32267 95 static void checkArg(const char *arg) {
henryjen@32267 96 size_t idx = 0;
henryjen@32267 97 argsCount++;
henryjen@32267 98 if (argsCount == 1) {
henryjen@32267 99 // ignore first argument, the application name
henryjen@32267 100 return;
henryjen@32267 101 }
henryjen@32267 102
henryjen@32267 103 // All arguments arrive here must be a launcher argument,
henryjen@32267 104 // ie. by now, all argfile expansions must have been performed.
henryjen@32267 105 if (*arg++ == '-') {
henryjen@32267 106 expectingNoDashArg = JNI_FALSE;
henryjen@32267 107 if (JLI_StrCmp(arg, "cp") == 0 ||
henryjen@32267 108 JLI_StrCmp(arg, "classpath") == 0) {
henryjen@32267 109 expectingNoDashArg = JNI_TRUE;
henryjen@32267 110 } else if (JLI_StrCmp(arg, "jar") == 0) {
henryjen@32267 111 // This is tricky, we do expect NoDashArg
henryjen@32267 112 // But that is considered main class to stop expansion
henryjen@32267 113 expectingNoDashArg = JNI_FALSE;
henryjen@32267 114 // We can not just update the idx here because if -jar @file
henryjen@32267 115 // still need expansion of @file to get the argument for -jar
henryjen@32267 116 } else if (JLI_StrCmp(arg, "Xdisable-@files") == 0) {
henryjen@32267 117 stopExpansion = JNI_TRUE;
henryjen@32267 118 }
henryjen@32267 119 } else {
henryjen@32267 120 if (!expectingNoDashArg) {
henryjen@32267 121 // this is main class, argsCount is index to next arg
henryjen@32267 122 idx = argsCount;
henryjen@32267 123 }
henryjen@32267 124 expectingNoDashArg = JNI_FALSE;
henryjen@32267 125 }
henryjen@32267 126 // only update on java mode and not yet found main class
henryjen@32267 127 if (firstAppArgIndex == -1 && idx != 0) {
henryjen@32267 128 firstAppArgIndex = (int) idx;
henryjen@32267 129 }
henryjen@32267 130 }
henryjen@32267 131
henryjen@32267 132 /*
henryjen@32267 133 [\n\r] +------------+ +------------+ [\n\r]
henryjen@32267 134 +---------+ IN_COMMENT +<------+ | IN_ESCAPE +---------+
henryjen@32267 135 | +------------+ | +------------+ |
henryjen@32267 136 | [#] ^ |[#] ^ | |
henryjen@32267 137 | +----------+ | [\\]| |[^\n\r] |
henryjen@32267 138 v | | | v |
henryjen@32267 139 +------------+ [^ \t\n\r\f] +------------+['"]> +------------+ |
henryjen@32267 140 | FIND_NEXT +-------------->+ IN_TOKEN +-----------+ IN_QUOTE + |
henryjen@32267 141 +------------+ +------------+ <[quote]+------------+ |
henryjen@32267 142 | ^ | | ^ ^ |
henryjen@32267 143 | | [ \t\n\r\f]| [\n\r]| | |[^ \t\n\r\f]v
henryjen@32267 144 | +--------------------------+-----------------------+ | +--------------+
henryjen@32267 145 | ['"] | | SKIP_LEAD_WS |
henryjen@32267 146 +---------------------------------------------------------+ +--------------+
henryjen@32267 147 */
henryjen@32267 148 static char* nextToken(__ctx_args *pctx) {
henryjen@32267 149 const char* nextc = pctx->cptr;
henryjen@32267 150 const char* const eob = pctx->eob;
henryjen@32267 151 const char* anchor = nextc;
henryjen@32267 152 char *token;
henryjen@32267 153
henryjen@32267 154 for (; nextc < eob; nextc++) {
henryjen@32267 155 register char ch = *nextc;
henryjen@32267 156
henryjen@32267 157 // Skip white space characters
henryjen@32267 158 if (pctx->state == FIND_NEXT || pctx->state == SKIP_LEAD_WS) {
henryjen@32267 159 while (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f') {
henryjen@32267 160 nextc++;
henryjen@32267 161 if (nextc >= eob) {
henryjen@32267 162 return NULL;
henryjen@32267 163 }
henryjen@32267 164 ch = *nextc;
henryjen@32267 165 }
henryjen@32267 166 pctx->state = (pctx->state == FIND_NEXT) ? IN_TOKEN : IN_QUOTE;
henryjen@32267 167 anchor = nextc;
henryjen@32267 168 // Deal with escape sequences
henryjen@32267 169 } else if (pctx->state == IN_ESCAPE) {
henryjen@32267 170 // concatenation directive
henryjen@32267 171 if (ch == '\n' || ch == '\r') {
henryjen@32267 172 pctx->state = SKIP_LEAD_WS;
henryjen@32267 173 } else {
henryjen@32267 174 // escaped character
henryjen@32267 175 char* escaped = (char*) JLI_MemAlloc(2 * sizeof(char));
henryjen@32267 176 escaped[1] = '\0';
henryjen@32267 177 switch (ch) {
henryjen@32267 178 case 'n':
henryjen@32267 179 escaped[0] = '\n';
henryjen@32267 180 break;
henryjen@32267 181 case 'r':
henryjen@32267 182 escaped[0] = '\r';
henryjen@32267 183 break;
henryjen@32267 184 case 't':
henryjen@32267 185 escaped[0] = '\t';
henryjen@32267 186 break;
henryjen@32267 187 case 'f':
henryjen@32267 188 escaped[0] = '\f';
henryjen@32267 189 break;
henryjen@32267 190 default:
henryjen@32267 191 escaped[0] = ch;
henryjen@32267 192 break;
henryjen@32267 193 }
henryjen@32267 194 JLI_List_add(pctx->parts, escaped);
henryjen@32267 195 pctx->state = IN_QUOTE;
henryjen@32267 196 }
henryjen@32267 197 // anchor to next character
henryjen@32267 198 anchor = nextc + 1;
henryjen@32267 199 continue;
henryjen@32267 200 // ignore comment to EOL
henryjen@32267 201 } else if (pctx->state == IN_COMMENT) {
henryjen@32267 202 while (ch != '\n' && ch != '\r') {
henryjen@32267 203 nextc++;
henryjen@32267 204 if (nextc > eob) {
henryjen@32267 205 return NULL;
henryjen@32267 206 }
henryjen@32267 207 ch = *nextc;
henryjen@32267 208 }
henryjen@32267 209 pctx->state = FIND_NEXT;
henryjen@32267 210 continue;
henryjen@32267 211 }
henryjen@32267 212
henryjen@32267 213 assert(pctx->state != IN_ESCAPE);
henryjen@32267 214 assert(pctx->state != FIND_NEXT);
henryjen@32267 215 assert(pctx->state != SKIP_LEAD_WS);
henryjen@32267 216 assert(pctx->state != IN_COMMENT);
henryjen@32267 217
henryjen@32267 218 switch(ch) {
henryjen@32267 219 case ' ':
henryjen@32267 220 case '\t':
henryjen@32267 221 case '\f':
henryjen@32267 222 if (pctx->state == IN_QUOTE) {
henryjen@32267 223 continue;
henryjen@32267 224 }
henryjen@32267 225 // fall through
henryjen@32267 226 case '\n':
henryjen@32267 227 case '\r':
henryjen@32267 228 if (pctx->parts->size == 0) {
henryjen@32267 229 token = clone_substring(anchor, nextc - anchor);
henryjen@32267 230 } else {
henryjen@32267 231 JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor);
henryjen@32267 232 token = JLI_List_combine(pctx->parts);
henryjen@32267 233 JLI_List_free(pctx->parts);
henryjen@32267 234 pctx->parts = JLI_List_new(4);
henryjen@32267 235 }
henryjen@32267 236 pctx->cptr = nextc + 1;
henryjen@32267 237 pctx->state = FIND_NEXT;
henryjen@32267 238 return token;
henryjen@32267 239 case '#':
henryjen@32267 240 if (pctx->state == IN_QUOTE) {
henryjen@32267 241 continue;
henryjen@32267 242 }
henryjen@32267 243 pctx->state = IN_COMMENT;
henryjen@32267 244 break;
henryjen@32267 245 case '\\':
henryjen@32267 246 if (pctx->state != IN_QUOTE) {
henryjen@32267 247 continue;
henryjen@32267 248 }
henryjen@32267 249 JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor);
henryjen@32267 250 pctx->state = IN_ESCAPE;
henryjen@32267 251 break;
henryjen@32267 252 case '\'':
henryjen@32267 253 case '"':
henryjen@32267 254 if (pctx->state == IN_QUOTE && pctx->quote_char != ch) {
henryjen@32267 255 // not matching quote
henryjen@32267 256 continue;
henryjen@32267 257 }
henryjen@32267 258 // partial before quote
henryjen@32267 259 if (anchor != nextc) {
henryjen@32267 260 JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor);
henryjen@32267 261 }
henryjen@32267 262 // anchor after quote character
henryjen@32267 263 anchor = nextc + 1;
henryjen@32267 264 if (pctx->state == IN_TOKEN) {
henryjen@32267 265 pctx->quote_char = ch;
henryjen@32267 266 pctx->state = IN_QUOTE;
henryjen@32267 267 } else {
henryjen@32267 268 pctx->state = IN_TOKEN;
henryjen@32267 269 }
henryjen@32267 270 break;
henryjen@32267 271 default:
henryjen@32267 272 break;
henryjen@32267 273 }
henryjen@32267 274 }
henryjen@32267 275
henryjen@32267 276 assert(nextc == eob);
henryjen@32267 277 if (anchor != nextc) {
henryjen@32267 278 // not yet return until end of stream, we have part of a token.
henryjen@32267 279 JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor);
henryjen@32267 280 }
henryjen@32267 281 return NULL;
henryjen@32267 282 }
henryjen@32267 283
henryjen@32267 284 static JLI_List readArgFile(FILE *file) {
henryjen@32267 285 char buf[4096];
henryjen@32267 286 JLI_List rv;
henryjen@32267 287 __ctx_args ctx;
henryjen@32267 288 size_t size;
henryjen@32267 289 char *token;
henryjen@32267 290
henryjen@32267 291 ctx.state = FIND_NEXT;
henryjen@32267 292 ctx.parts = JLI_List_new(4);
henryjen@32267 293
henryjen@32267 294 /* arbitrarily pick 8, seems to be a reasonable number of arguments */
henryjen@32267 295 rv = JLI_List_new(8);
henryjen@32267 296
henryjen@32267 297 while (!feof(file)) {
henryjen@32267 298 size = fread(buf, sizeof(char), sizeof(buf), file);
henryjen@32267 299 if (ferror(file)) {
henryjen@32267 300 JLI_List_free(rv);
henryjen@32267 301 return NULL;
henryjen@32267 302 }
henryjen@32267 303
henryjen@32267 304 /* nextc is next character to read from the buffer
henryjen@32267 305 * eob is the end of input
henryjen@32267 306 * token is the copied token value, NULL if no a complete token
henryjen@32267 307 */
henryjen@32267 308 ctx.cptr = buf;
henryjen@32267 309 ctx.eob = buf + size;
henryjen@32267 310 token = nextToken(&ctx);
henryjen@32267 311 while (token != NULL) {
henryjen@32267 312 checkArg(token);
henryjen@32267 313 JLI_List_add(rv, token);
henryjen@32267 314 token = nextToken(&ctx);
henryjen@32267 315 }
henryjen@32267 316 }
henryjen@32267 317
henryjen@32267 318 // remaining partial token
henryjen@32267 319 if (ctx.state == IN_TOKEN || ctx.state == IN_QUOTE) {
henryjen@32267 320 if (ctx.parts->size != 0) {
henryjen@32267 321 JLI_List_add(rv, JLI_List_combine(ctx.parts));
henryjen@32267 322 }
henryjen@32267 323 }
henryjen@32267 324 JLI_List_free(ctx.parts);
henryjen@32267 325
henryjen@32267 326 return rv;
henryjen@32267 327 }
henryjen@32267 328
henryjen@32267 329 /*
henryjen@32267 330 * if the arg represent a file, that is, prefix with a single '@',
henryjen@32267 331 * return a list of arguments from the file.
henryjen@32267 332 * otherwise, return NULL.
henryjen@32267 333 */
henryjen@32267 334 static JLI_List expandArgFile(const char *arg) {
henryjen@32267 335 FILE *fptr;
henryjen@32267 336 struct stat st;
henryjen@32267 337 JLI_List rv;
henryjen@32267 338
henryjen@32267 339 /* failed to access the file */
henryjen@32267 340 if (stat(arg, &st) != 0) {
henryjen@32267 341 JLI_ReportMessage(CFG_ERROR6, arg);
henryjen@32267 342 exit(1);
henryjen@32267 343 }
henryjen@32267 344
henryjen@32267 345 if (st.st_size > MAX_ARGF_SIZE) {
henryjen@32267 346 JLI_ReportMessage(CFG_ERROR10, MAX_ARGF_SIZE);
henryjen@32267 347 exit(1);
henryjen@32267 348 }
henryjen@32267 349
henryjen@32267 350 fptr = fopen(arg, "r");
henryjen@32267 351 /* arg file cannot be openned */
henryjen@32267 352 if (fptr == NULL) {
henryjen@32267 353 JLI_ReportMessage(CFG_ERROR6, arg);
henryjen@32267 354 exit(1);
henryjen@32267 355 }
henryjen@32267 356
henryjen@32267 357 rv = readArgFile(fptr);
henryjen@32267 358 fclose(fptr);
henryjen@32267 359
henryjen@32267 360 /* error occurred reading the file */
henryjen@32267 361 if (rv == NULL) {
henryjen@32267 362 JLI_ReportMessage(DLL_ERROR4, arg);
henryjen@32267 363 exit(1);
henryjen@32267 364 }
henryjen@32267 365
henryjen@32267 366 return rv;
henryjen@32267 367 }
henryjen@32267 368
henryjen@32267 369 JLI_List JLI_PreprocessArg(const char *arg)
henryjen@32267 370 {
henryjen@32267 371 JLI_List rv;
henryjen@32267 372
henryjen@32267 373 if (firstAppArgIndex > 0) {
henryjen@32267 374 // In user application arg, no more work.
henryjen@32267 375 return NULL;
henryjen@32267 376 }
henryjen@32267 377
henryjen@32267 378 if (stopExpansion) {
henryjen@32267 379 // still looking for user application arg
henryjen@32267 380 checkArg(arg);
henryjen@32267 381 return NULL;
henryjen@32267 382 }
henryjen@32267 383
henryjen@32267 384 if (arg[0] != '@') {
henryjen@32267 385 checkArg(arg);
henryjen@32267 386 return NULL;
henryjen@32267 387 }
henryjen@32267 388
henryjen@32267 389 if (arg[1] == '\0') {
henryjen@32267 390 // @ by itself is an argument
henryjen@32267 391 checkArg(arg);
henryjen@32267 392 return NULL;
henryjen@32267 393 }
henryjen@32267 394
henryjen@32267 395 arg++;
henryjen@32267 396 if (arg[0] == '@') {
henryjen@32267 397 // escaped @argument
henryjen@32267 398 rv = JLI_List_new(1);
henryjen@32267 399 checkArg(arg);
henryjen@32267 400 JLI_List_add(rv, JLI_StringDup(arg));
henryjen@32267 401 } else {
henryjen@32267 402 rv = expandArgFile(arg);
henryjen@32267 403 }
henryjen@32267 404 return rv;
henryjen@32267 405 }
henryjen@32267 406
henryjen@32267 407 #ifdef DEBUG_ARGFILE
henryjen@32267 408 /*
henryjen@32267 409 * Stand-alone sanity test, build with following command line
henryjen@32267 410 * $ CC -DDEBUG_ARGFILE -DNO_JNI -g args.c jli_util.c
henryjen@32267 411 */
henryjen@32267 412
henryjen@32267 413 void fail(char *expected, char *actual, size_t idx) {
henryjen@32267 414 printf("FAILED: Token[%lu] expected to be <%s>, got <%s>\n", idx, expected, actual);
henryjen@32267 415 exit(1);
henryjen@32267 416 }
henryjen@32267 417
henryjen@32267 418 void test_case(char *case_data, char **tokens, size_t cnt_tokens) {
henryjen@32267 419 size_t actual_cnt;
henryjen@32267 420 char *token;
henryjen@32267 421 __ctx_args ctx;
henryjen@32267 422
henryjen@32267 423 actual_cnt = 0;
henryjen@32267 424
henryjen@32267 425 ctx.state = FIND_NEXT;
henryjen@32267 426 ctx.parts = JLI_List_new(4);
henryjen@32267 427 ctx.cptr = case_data;
henryjen@32267 428 ctx.eob = case_data + strlen(case_data);
henryjen@32267 429
henryjen@32267 430 printf("Test case: <%s>, expected %lu tokens.\n", case_data, cnt_tokens);
henryjen@32267 431
henryjen@32267 432 for (token = nextToken(&ctx); token != NULL; token = nextToken(&ctx)) {
henryjen@32267 433 // should not have more tokens than expected
henryjen@32267 434 if (actual_cnt >= cnt_tokens) {
henryjen@32267 435 printf("FAILED: Extra token detected: <%s>\n", token);
henryjen@32267 436 exit(2);
henryjen@32267 437 }
henryjen@32267 438 if (JLI_StrCmp(token, tokens[actual_cnt]) != 0) {
henryjen@32267 439 fail(tokens[actual_cnt], token, actual_cnt);
henryjen@32267 440 }
henryjen@32267 441 actual_cnt++;
henryjen@32267 442 }
henryjen@32267 443
henryjen@32267 444 char* last = NULL;
henryjen@32267 445 if (ctx.parts->size != 0) {
henryjen@32267 446 last = JLI_List_combine(ctx.parts);
henryjen@32267 447 }
henryjen@32267 448 JLI_List_free(ctx.parts);
henryjen@32267 449
henryjen@32267 450 if (actual_cnt >= cnt_tokens) {
henryjen@32267 451 // same number of tokens, should have nothing left to parse
henryjen@32267 452 if (last != NULL) {
henryjen@32267 453 if (*last != '#') {
henryjen@32267 454 printf("Leftover detected: %s", last);
henryjen@32267 455 exit(2);
henryjen@32267 456 }
henryjen@32267 457 }
henryjen@32267 458 } else {
henryjen@32267 459 if (JLI_StrCmp(last, tokens[actual_cnt]) != 0) {
henryjen@32267 460 fail(tokens[actual_cnt], last, actual_cnt);
henryjen@32267 461 }
henryjen@32267 462 actual_cnt++;
henryjen@32267 463 }
henryjen@32267 464 if (actual_cnt != cnt_tokens) {
henryjen@32267 465 printf("FAILED: Number of tokens not match, expected %lu, got %lu\n",
henryjen@32267 466 cnt_tokens, actual_cnt);
henryjen@32267 467 exit(3);
henryjen@32267 468 }
henryjen@32267 469
henryjen@32267 470 printf("PASS\n");
henryjen@32267 471 }
henryjen@32267 472
henryjen@32267 473 #define DO_CASE(name) \
henryjen@32267 474 test_case(name[0], name + 1, sizeof(name)/sizeof(char*) - 1)
henryjen@32267 475
henryjen@32267 476 int main(int argc, char** argv) {
henryjen@32267 477 size_t i, j;
henryjen@32267 478
henryjen@32267 479 char* case1[] = { "-version -cp \"c:\\\\java libs\\\\one.jar\" \n",
henryjen@32267 480 "-version", "-cp", "c:\\java libs\\one.jar" };
henryjen@32267 481 DO_CASE(case1);
henryjen@32267 482
henryjen@32267 483 // note the open quote at the end
henryjen@32267 484 char* case2[] = { "com.foo.Panda \"Furious 5\"\fand\t'Shi Fu' \"escape\tprison",
henryjen@32267 485 "com.foo.Panda", "Furious 5", "and", "Shi Fu", "escape\tprison"};
henryjen@32267 486 DO_CASE(case2);
henryjen@32267 487
henryjen@32267 488 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 489 "escaped", "chars", "testing", "abc\f\n\r\tv96238228377477278287"};
henryjen@32267 490 DO_CASE(escaped_chars);
henryjen@32267 491
henryjen@32267 492 char* mixed_quote[] = { "\"mix 'single quote' in double\" 'mix \"double quote\" in single' partial\"quote me\"this",
henryjen@32267 493 "mix 'single quote' in double", "mix \"double quote\" in single", "partialquote methis"};
henryjen@32267 494 DO_CASE(mixed_quote);
henryjen@32267 495
henryjen@32267 496 char* comments[] = { "line one #comment\n'line #2' #rest are comment\r\n#comment on line 3\nline 4 #comment to eof",
henryjen@32267 497 "line", "one", "line #2", "line", "4"};
henryjen@32267 498 DO_CASE(comments);
henryjen@32267 499
henryjen@32267 500 char* open_quote[] = { "This is an \"open quote \n across line\n\t, note for WS.",
henryjen@32267 501 "This", "is", "an", "open quote ", "across", "line", ",", "note", "for", "WS." };
henryjen@32267 502 DO_CASE(open_quote);
henryjen@32267 503
henryjen@32267 504 char* escape_in_open_quote[] = { "Try \"this \\\\\\\\ escape\\n double quote \\\" in open quote",
henryjen@32267 505 "Try", "this \\\\ escape\n double quote \" in open quote" };
henryjen@32267 506 DO_CASE(escape_in_open_quote);
henryjen@32267 507
henryjen@32267 508 char* quote[] = { "'-Dmy.quote.single'='Property in single quote. Here a double quote\" Add some slashes \\\\/'",
henryjen@32267 509 "-Dmy.quote.single=Property in single quote. Here a double quote\" Add some slashes \\/" };
henryjen@32267 510 DO_CASE(quote);
henryjen@32267 511
henryjen@32267 512 char* multi[] = { "\"Open quote to \n new \"line \\\n\r third\\\n\r\\\tand\ffourth\"",
henryjen@32267 513 "Open quote to ", "new", "line third\tand\ffourth" };
henryjen@32267 514 DO_CASE(multi);
henryjen@32267 515
henryjen@32267 516 char* escape_quote[] = { "c:\\\"partial quote\"\\lib",
henryjen@32267 517 "c:\\partial quote\\lib" };
henryjen@32267 518 DO_CASE(escape_quote);
henryjen@32267 519
henryjen@32267 520 if (argc > 1) {
henryjen@32267 521 for (i = 0; i < argc; i++) {
henryjen@32267 522 JLI_List tokens = JLI_PreprocessArg(argv[i]);
henryjen@32267 523 if (NULL != tokens) {
henryjen@32267 524 for (j = 0; j < tokens->size; j++) {
henryjen@32267 525 printf("Token[%lu]: <%s>\n", (unsigned long) j, tokens->elements[j]);
henryjen@32267 526 }
henryjen@32267 527 }
henryjen@32267 528 }
henryjen@32267 529 }
henryjen@32267 530 }
henryjen@32267 531
henryjen@32267 532 #endif // DEBUG_ARGFILE