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