|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package com.oracle.truffle.espresso.launcher; |
| 24 | + |
| 25 | +import static com.oracle.truffle.espresso.launcher.ArgFileParser.State.FIND_NEXT; |
| 26 | +import static com.oracle.truffle.espresso.launcher.ArgFileParser.State.IN_COMMENT; |
| 27 | +import static com.oracle.truffle.espresso.launcher.ArgFileParser.State.IN_ESCAPE; |
| 28 | +import static com.oracle.truffle.espresso.launcher.ArgFileParser.State.IN_QUOTE; |
| 29 | +import static com.oracle.truffle.espresso.launcher.ArgFileParser.State.IN_TOKEN; |
| 30 | +import static com.oracle.truffle.espresso.launcher.ArgFileParser.State.SKIP_LEAD_WS; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.Reader; |
| 34 | +import java.util.function.Consumer; |
| 35 | + |
| 36 | +/** |
| 37 | + * Parse {@code @arg-files} as handled by libjli. See libjli/args.c. |
| 38 | + */ |
| 39 | +public final class ArgFileParser { |
| 40 | + private final Reader reader; |
| 41 | + |
| 42 | + protected enum State { |
| 43 | + FIND_NEXT, |
| 44 | + IN_COMMENT, |
| 45 | + IN_QUOTE, |
| 46 | + IN_ESCAPE, |
| 47 | + SKIP_LEAD_WS, |
| 48 | + IN_TOKEN |
| 49 | + } |
| 50 | + |
| 51 | + public ArgFileParser(Reader reader) { |
| 52 | + this.reader = reader; |
| 53 | + } |
| 54 | + |
| 55 | + public void parse(Consumer<String> argConsumer) throws IOException { |
| 56 | + String token; |
| 57 | + while ((token = nextToken()) != null) { |
| 58 | + argConsumer.accept(token); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("fallthrough") |
| 63 | + private String nextToken() throws IOException { |
| 64 | + State state = FIND_NEXT; |
| 65 | + int currentQuoteChar = -1; |
| 66 | + int ch; |
| 67 | + StringBuilder sb = new StringBuilder(); |
| 68 | + charloop: while ((ch = reader.read()) >= 0) { |
| 69 | + // Skip white space characters |
| 70 | + if (state == FIND_NEXT || state == SKIP_LEAD_WS) { |
| 71 | + while (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f') { |
| 72 | + ch = reader.read(); |
| 73 | + if (ch < 0) { |
| 74 | + break charloop; |
| 75 | + } |
| 76 | + } |
| 77 | + state = (state == FIND_NEXT) ? IN_TOKEN : IN_QUOTE; |
| 78 | + // Deal with escape sequences |
| 79 | + } else if (state == IN_ESCAPE) { |
| 80 | + // concatenation directive |
| 81 | + if (ch == '\n' || ch == '\r') { |
| 82 | + state = SKIP_LEAD_WS; |
| 83 | + } else { |
| 84 | + // escaped character |
| 85 | + switch (ch) { |
| 86 | + case 'n': |
| 87 | + sb.append('\n'); |
| 88 | + break; |
| 89 | + case 'r': |
| 90 | + sb.append('\r'); |
| 91 | + break; |
| 92 | + case 't': |
| 93 | + sb.append('\t'); |
| 94 | + break; |
| 95 | + case 'f': |
| 96 | + sb.append('\f'); |
| 97 | + break; |
| 98 | + default: |
| 99 | + sb.append((char) ch); |
| 100 | + break; |
| 101 | + } |
| 102 | + state = IN_QUOTE; |
| 103 | + } |
| 104 | + continue; |
| 105 | + // ignore comment to EOL |
| 106 | + } else if (state == IN_COMMENT) { |
| 107 | + while (ch != '\n' && ch != '\r') { |
| 108 | + ch = reader.read(); |
| 109 | + if (ch < 0) { |
| 110 | + break charloop; |
| 111 | + } |
| 112 | + } |
| 113 | + state = FIND_NEXT; |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + assert (state != IN_ESCAPE); |
| 118 | + assert (state != FIND_NEXT); |
| 119 | + assert (state != SKIP_LEAD_WS); |
| 120 | + assert (state != IN_COMMENT); |
| 121 | + |
| 122 | + switch (ch) { |
| 123 | + case ' ': |
| 124 | + case '\t': |
| 125 | + case '\f': |
| 126 | + if (state == IN_QUOTE) { |
| 127 | + sb.append((char) ch); |
| 128 | + continue; |
| 129 | + } |
| 130 | + // fallthrough |
| 131 | + case '\n': |
| 132 | + case '\r': |
| 133 | + return sb.toString(); |
| 134 | + case '#': |
| 135 | + if (state == IN_QUOTE) { |
| 136 | + sb.append((char) ch); |
| 137 | + continue; |
| 138 | + } |
| 139 | + state = IN_COMMENT; |
| 140 | + break; |
| 141 | + case '\\': |
| 142 | + if (state != IN_QUOTE) { |
| 143 | + sb.append((char) ch); |
| 144 | + continue; |
| 145 | + } |
| 146 | + state = IN_ESCAPE; |
| 147 | + break; |
| 148 | + case '\'': |
| 149 | + case '"': |
| 150 | + if (state == IN_QUOTE && currentQuoteChar != ch) { |
| 151 | + // not matching quote |
| 152 | + sb.append((char) ch); |
| 153 | + continue; |
| 154 | + } |
| 155 | + // anchor after quote character |
| 156 | + if (state == IN_TOKEN) { |
| 157 | + currentQuoteChar = ch; |
| 158 | + state = IN_QUOTE; |
| 159 | + } else { |
| 160 | + state = IN_TOKEN; |
| 161 | + } |
| 162 | + break; |
| 163 | + default: |
| 164 | + sb.append((char) ch); |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + if (sb.isEmpty()) { |
| 169 | + return null; |
| 170 | + } |
| 171 | + return sb.toString(); |
| 172 | + } |
| 173 | +} |
0 commit comments