1 /* 2 * Copyright (c) 2015, 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 sun.tools.jar; 27 28 import java.io.File; 29 import java.io.PrintWriter; 30 import java.lang.module.ModuleDescriptor.Version; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 import java.util.regex.Pattern; 34 import java.util.regex.PatternSyntaxException; 35 import jdk.internal.module.ModulePath; 36 import jdk.internal.module.ModuleResolution; 37 38 /** 39 * Parser for GNU Style Options. 40 */ 41 class GNUStyleOptions { 42 43 static class BadArgs extends Exception { 44 static final long serialVersionUID = 0L; 45 46 boolean showUsage; 47 48 BadArgs(String key, String arg) { super(Main.formatMsg(key, arg)); } 49 BadArgs(String key) { super(Main.getMsg(key)); } 50 51 BadArgs showUsage(boolean b) { 52 showUsage = b; 53 return this; 54 } 55 } 56 57 static Option[] recognizedOptions = { 58 // Main operations 59 new Option(false, OptionType.MAIN_OPERATION, "--create", "-c") { 60 void process(Main tool, String opt, String arg) throws BadArgs { 61 if (tool.iflag || tool.tflag || tool.uflag || tool.xflag || tool.dflag) 62 throw new BadArgs("error.multiple.main.operations").showUsage(true); 63 tool.cflag = true; 64 } 65 }, 66 new Option(true, OptionType.MAIN_OPERATION, "--generate-index", "-i") { 67 void process(Main tool, String opt, String arg) throws BadArgs { 68 if (tool.cflag || tool.tflag || tool.uflag || tool.xflag || tool.dflag) 69 throw new BadArgs("error.multiple.main.operations").showUsage(true); 70 tool.iflag = true; 71 tool.rootjar = arg; 72 } 73 }, 74 new Option(false, OptionType.MAIN_OPERATION, "--list", "-t") { 75 void process(Main tool, String opt, String arg) throws BadArgs { 76 if (tool.cflag || tool.iflag || tool.uflag || tool.xflag || tool.dflag) 77 throw new BadArgs("error.multiple.main.operations").showUsage(true); 78 tool.tflag = true; 79 } 80 }, 81 new Option(false, OptionType.MAIN_OPERATION, "--update", "-u") { 82 void process(Main tool, String opt, String arg) throws BadArgs { 83 if (tool.cflag || tool.iflag || tool.tflag || tool.xflag || tool.dflag) 84 throw new BadArgs("error.multiple.main.operations").showUsage(true); 85 tool.uflag = true; 86 } 87 }, 88 new Option(false, OptionType.MAIN_OPERATION, "--extract", "-x") { 89 void process(Main tool, String opt, String arg) throws BadArgs { 90 if (tool.cflag || tool.iflag || tool.tflag || tool.uflag || tool.dflag) 91 throw new BadArgs("error.multiple.main.operations").showUsage(true); 92 tool.xflag = true; 93 } 94 }, 95 new Option(false, OptionType.MAIN_OPERATION, "--describe-module", "-d") { 96 void process(Main tool, String opt, String arg) throws BadArgs { 97 if (tool.cflag || tool.iflag || tool.tflag || tool.uflag || tool.xflag) 98 throw new BadArgs("error.multiple.main.operations").showUsage(true); 99 tool.dflag = true; 100 } 101 }, 102 103 // Additional options 104 new Option(true, OptionType.ANY, "--file", "-f") { 105 void process(Main jartool, String opt, String arg) { 106 jartool.fname = arg; 107 } 108 }, 109 new Option(false, OptionType.ANY, "--verbose", "-v") { 110 void process(Main jartool, String opt, String arg) { 111 jartool.vflag = true; 112 } 113 }, 114 new Option(false, OptionType.CREATE, "--normalize", "-n") { 115 void process(Main jartool, String opt, String arg) { 116 jartool.nflag = true; 117 } 118 boolean isExtra() { return true; } 119 }, 120 new Option(true, OptionType.CREATE_UPDATE, "--main-class", "-e") { 121 void process(Main jartool, String opt, String arg) { 122 jartool.ename = arg; 123 } 124 }, 125 new Option(true, OptionType.CREATE_UPDATE, "--manifest", "-m") { 126 void process(Main jartool, String opt, String arg) { 127 jartool.mname = arg; 128 } 129 }, 130 new Option(false, OptionType.CREATE_UPDATE, "--no-manifest", "-M") { 131 void process(Main jartool, String opt, String arg) { 132 jartool.Mflag = true; 133 } 134 }, 135 new Option(true, OptionType.CREATE_UPDATE, "--module-version") { 136 void process(Main jartool, String opt, String arg) { 137 jartool.moduleVersion = Version.parse(arg); 138 } 139 }, 140 new Option(true, OptionType.CREATE_UPDATE, "--hash-modules") { 141 void process(Main jartool, String opt, String arg) throws BadArgs { 142 try { 143 jartool.modulesToHash = Pattern.compile(arg); 144 } catch (PatternSyntaxException e) { 145 throw new BadArgs("err.badpattern", arg).showUsage(true); 146 } 147 } 148 }, 149 new Option(true, OptionType.CREATE_UPDATE, "--module-path", "-p") { 150 void process(Main jartool, String opt, String arg) { 151 String[] dirs = arg.split(File.pathSeparator); 152 Path[] paths = new Path[dirs.length]; 153 int i = 0; 154 for (String dir : dirs) { 155 paths[i++] = Paths.get(dir); 156 } 157 jartool.moduleFinder = ModulePath.of(Runtime.version(), true, paths); 158 } 159 }, 160 new Option(false, OptionType.CREATE_UPDATE, "--do-not-resolve-by-default") { 161 void process(Main jartool, String opt, String arg) { 162 ModuleResolution mres = jartool.moduleResolution; 163 jartool.moduleResolution = mres.withDoNotResolveByDefault(); 164 } 165 boolean isExtra() { return true; } 166 }, 167 new Option(true, OptionType.CREATE_UPDATE, "--warn-if-resolved") { 168 void process(Main jartool, String opt, String arg) throws BadArgs { 169 ModuleResolution mres = ModuleResolution.empty(); 170 if (jartool.moduleResolution.doNotResolveByDefault()) { 171 mres.withDoNotResolveByDefault(); 172 } 173 if (arg.equals("deprecated")) { 174 jartool.moduleResolution = mres.withDeprecated(); 175 } else if (arg.equals("deprecated-for-removal")) { 176 jartool.moduleResolution = mres.withDeprecatedForRemoval(); 177 } else if (arg.equals("incubating")) { 178 jartool.moduleResolution = mres.withIncubating(); 179 } else { 180 throw new BadArgs("error.bad.reason", arg); 181 } 182 } 183 boolean isExtra() { return true; } 184 }, 185 new Option(false, OptionType.CREATE_UPDATE_INDEX, "--no-compress", "-0") { 186 void process(Main jartool, String opt, String arg) { 187 jartool.flag0 = true; 188 } 189 }, 190 191 // Hidden options 192 new Option(false, OptionType.OTHER, "-P") { 193 void process(Main jartool, String opt, String arg) { 194 jartool.pflag = true; 195 } 196 boolean isHidden() { return true; } 197 }, 198 199 // Other options 200 new Option(true, true, OptionType.OTHER, "--help", "-h", "-?") { 201 void process(Main jartool, String opt, String arg) throws BadArgs { 202 if (jartool.info == null) { 203 if (arg == null) { 204 jartool.info = GNUStyleOptions::printHelp; // Main.Info.HELP; 205 return; 206 } 207 if (!arg.equals("compat")) 208 throw new BadArgs("error.illegal.option", arg).showUsage(true); 209 // jartool.info = Main.Info.COMPAT_HELP; 210 jartool.info = GNUStyleOptions::printCompatHelp; 211 } 212 } 213 }, 214 new Option(false, OptionType.OTHER, "--help-extra") { 215 void process(Main jartool, String opt, String arg) throws BadArgs { 216 jartool.info = GNUStyleOptions::printHelpExtra; 217 } 218 }, 219 new Option(false, OptionType.OTHER, "--version") { 220 void process(Main jartool, String opt, String arg) { 221 if (jartool.info == null) 222 jartool.info = GNUStyleOptions::printVersion; 223 } 224 } 225 }; 226 227 enum OptionType { 228 MAIN_OPERATION("main"), 229 ANY("any"), 230 CREATE("create"), 231 CREATE_UPDATE("create.update"), 232 CREATE_UPDATE_INDEX("create.update.index"), 233 OTHER("other"); 234 235 /** Resource lookup section prefix. */ 236 final String name; 237 238 OptionType(String name) { this.name = name; } 239 } 240 241 static abstract class Option { 242 final boolean hasArg; 243 final boolean argIsOptional; 244 final String[] aliases; 245 final OptionType type; 246 247 Option(boolean hasArg, OptionType type, String... aliases) { 248 this(hasArg, false, type, aliases); 249 } 250 251 Option(boolean hasArg, boolean argIsOptional, OptionType type, String... aliases) { 252 this.hasArg = hasArg; 253 this.argIsOptional = argIsOptional; 254 this.type = type; 255 this.aliases = aliases; 256 } 257 258 boolean isHidden() { return false; } 259 260 boolean isExtra() { return false; } 261 262 boolean matches(String opt) { 263 for (String a : aliases) { 264 if (a.equals(opt)) { 265 return true; 266 } else if (opt.startsWith("--") && hasArg && opt.startsWith(a + "=")) { 267 return true; 268 } else if (opt.startsWith("--help") && opt.startsWith(a + ":")) { 269 return true; 270 } 271 } 272 return false; 273 } 274 275 abstract void process(Main jartool, String opt, String arg) throws BadArgs; 276 } 277 278 static int parseOptions(Main jartool, String[] args) throws BadArgs { 279 int count = 0; 280 if (args.length == 0) { 281 jartool.info = GNUStyleOptions::printUsageTryHelp; // never be here 282 return 0; 283 } 284 285 // process options 286 for (; count < args.length; count++) { 287 if (args[count].charAt(0) != '-' || args[count].equals("-C") || 288 args[count].equals("--release")) 289 break; 290 291 String name = args[count]; 292 if (name.equals("-XDsuppress-tool-removal-message")) { 293 jartool.suppressDeprecateMsg = true; 294 continue; 295 } 296 Option option = getOption(name); 297 String param = null; 298 if (option.hasArg) { 299 if (name.startsWith("--help")) { // "special" optional separator 300 if (name.indexOf(':') > 0) { 301 param = name.substring(name.indexOf(':') + 1, name.length()); 302 } 303 } else if (name.startsWith("--") && name.indexOf('=') > 0) { 304 param = name.substring(name.indexOf('=') + 1, name.length()); 305 } else if (count + 1 < args.length) { 306 param = args[++count]; 307 } 308 if (!option.argIsOptional && 309 (param == null || param.isEmpty() || param.charAt(0) == '-')) { 310 throw new BadArgs("error.missing.arg", name).showUsage(true); 311 } 312 } 313 option.process(jartool, name, param); 314 } 315 316 return count; 317 } 318 319 private static Option getOption(String name) throws BadArgs { 320 for (Option o : recognizedOptions) { 321 if (o.matches(name)) { 322 return o; 323 } 324 } 325 throw new BadArgs("error.unrecognized.option", name).showUsage(true); 326 } 327 328 static void printHelpExtra(PrintWriter out) { 329 printHelp0(out, true); 330 } 331 332 static void printHelp(PrintWriter out) { 333 printHelp0(out, false); 334 } 335 336 private static void printHelp0(PrintWriter out, boolean printExtra) { 337 out.format("%s%n", Main.getMsg("main.help.preopt")); 338 for (OptionType type : OptionType.values()) { 339 boolean typeHeadingWritten = false; 340 341 for (Option o : recognizedOptions) { 342 if (!o.type.equals(type)) 343 continue; 344 String name = o.aliases[0].substring(1); // there must always be at least one name 345 name = name.charAt(0) == '-' ? name.substring(1) : name; 346 if (o.isHidden() || name.equals("h")) { 347 continue; 348 } 349 if (o.isExtra() && !printExtra) { 350 continue; 351 } 352 if (!typeHeadingWritten) { 353 out.format("%n%s%n", Main.getMsg("main.help.opt." + type.name)); 354 typeHeadingWritten = true; 355 } 356 out.format("%s%n", Main.getMsg("main.help.opt." + type.name + "." + name)); 357 } 358 } 359 out.format("%n%s%n%n", Main.getMsg("main.help.postopt")); 360 } 361 362 static void printCompatHelp(PrintWriter out) { 363 out.format("%s%n", Main.getMsg("usage.compat")); 364 } 365 366 static void printUsageTryHelp(PrintWriter out) { 367 out.format("%s%n", Main.getMsg("main.usage.summary.try")); 368 } 369 370 static void printVersion(PrintWriter out) { 371 out.format("%s %s%n", "jar", System.getProperty("java.version")); 372 } 373 }