1 /* 2 * Copyright (c) 1999, 2018, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jpackage.main; 27 28 import java.io.FileNotFoundException; 29 import java.io.IOException; 30 import java.io.File; 31 import java.io.Reader; 32 import java.nio.charset.Charset; 33 import java.nio.file.Files; 34 import java.nio.file.Path; 35 import java.nio.file.Paths; 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 import java.util.List; 39 40 /** 41 * This file was originally a copy of CommandLine.java in 42 * com.sun.tools.javac.main. 43 * It should track changes made to that file. 44 */ 45 46 /** 47 * Various utility methods for processing Java tool command line arguments. 48 * 49 * <p><b>This is NOT part of any supported API. 50 * If you write code that depends on this, you do so at your own risk. 51 * This code and its internal interfaces are subject to change or 52 * deletion without notice.</b> 53 */ 54 class CommandLine { 55 /** 56 * Process Win32-style command files for the specified command line 57 * arguments and return the resulting arguments. A command file argument 58 * is of the form '@file' where 'file' is the name of the file whose 59 * contents are to be parsed for additional arguments. The contents of 60 * the command file are parsed using StreamTokenizer and the original 61 * '@file' argument replaced with the resulting tokens. Recursive command 62 * files are not supported. The '@' character itself can be quoted with 63 * the sequence '@@'. 64 * @param args the arguments that may contain @files 65 * @return the arguments, with @files expanded 66 * @throws IOException if there is a problem reading any of the @files 67 */ 68 public static String[] parse(String[] args) throws IOException { 69 List<String> newArgs = new ArrayList<>(); 70 appendParsedCommandArgs(newArgs, Arrays.asList(args)); 71 return newArgs.toArray(new String[newArgs.size()]); 72 } 73 74 private static void appendParsedCommandArgs(List<String> newArgs, List<String> args) throws IOException { 75 for (String arg : args) { 76 if (arg.length() > 1 && arg.charAt(0) == '@') { 77 arg = arg.substring(1); 78 if (arg.charAt(0) == '@') { 79 newArgs.add(arg); 80 } else { 81 loadCmdFile(arg, newArgs); 82 } 83 } else { 84 newArgs.add(arg); 85 } 86 } 87 } 88 89 private static void loadCmdFile(String name, List<String> args) throws IOException { 90 if (!Files.isReadable(Path.of(name))) { 91 throw new FileNotFoundException(name); 92 } 93 try (Reader r = Files.newBufferedReader(Paths.get(name), Charset.defaultCharset())) { 94 Tokenizer t = new Tokenizer(r); 95 String s; 96 while ((s = t.nextToken()) != null) { 97 args.add(s); 98 } 99 } 100 } 101 102 public static class Tokenizer { 103 private final Reader in; 104 private int ch; 105 106 public Tokenizer(Reader in) throws IOException { 107 this.in = in; 108 ch = in.read(); 109 } 110 111 public String nextToken() throws IOException { 112 skipWhite(); 113 if (ch == -1) { 114 return null; 115 } 116 117 StringBuilder sb = new StringBuilder(); 118 char quoteChar = 0; 119 120 while (ch != -1) { 121 switch (ch) { 122 case ' ': 123 case '\t': 124 case '\f': 125 if (quoteChar == 0) { 126 return sb.toString(); 127 } 128 sb.append((char) ch); 129 break; 130 131 case '\n': 132 case '\r': 133 return sb.toString(); 134 135 case '\'': 136 case '"': 137 if (quoteChar == 0) { 138 quoteChar = (char) ch; 139 } else if (quoteChar == ch) { 140 quoteChar = 0; 141 } else { 142 sb.append((char) ch); 143 } 144 break; 145 146 case '\\': 147 if (quoteChar != 0) { 148 ch = in.read(); 149 switch (ch) { 150 case '\n': 151 case '\r': 152 while (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f') { 153 ch = in.read(); 154 } 155 continue; 156 157 case 'n': 158 ch = '\n'; 159 break; 160 case 'r': 161 ch = '\r'; 162 break; 163 case 't': 164 ch = '\t'; 165 break; 166 case 'f': 167 ch = '\f'; 168 break; 169 } 170 } 171 sb.append((char) ch); 172 break; 173 174 default: 175 sb.append((char) ch); 176 } 177 178 ch = in.read(); 179 } 180 181 return sb.toString(); 182 } 183 184 void skipWhite() throws IOException { 185 while (ch != -1) { 186 switch (ch) { 187 case ' ': 188 case '\t': 189 case '\n': 190 case '\r': 191 case '\f': 192 break; 193 194 case '#': 195 ch = in.read(); 196 while (ch != '\n' && ch != '\r' && ch != -1) { 197 ch = in.read(); 198 } 199 break; 200 201 default: 202 return; 203 } 204 205 ch = in.read(); 206 } 207 } 208 } 209 }