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