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