Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SAGetopt.java
41159 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot;2526import java.util.Arrays;27import java.util.List;2829public class SAGetopt {3031private String[] _argv;3233private int _optind; // index in arguments array34private int _optopt; // index within an argument35private String _optarg; // last option argument36private boolean _optreset; // special handling of first call3738public SAGetopt(String[] args) {39_argv = args.clone();40_optind = 0;41_optopt = 1;42_optarg = null;43_optreset = true;44}4546public String getOptarg() {47return _optarg;48}4950public int getOptind() {51return _optind;52}5354private void extractOptarg(String opt) {55// Argument expected56if (_optind > _argv.length) {57throw new SAGetoptException("Not enough arguments for '" + opt + "'");58}5960if (! _argv[_optind].isEmpty() && _argv[_optind].charAt(0) == '-') {61throw new SAGetoptException("Argument is expected for '" + opt + "'");62}6364_optarg = _argv[_optind];65_optind += 1;66}6768private String processLongOptions(String carg, String[] longOptStr) {69List<String> los = Arrays.asList(longOptStr);70String[] ca = carg.split("=", 2);7172if (los.contains(ca[0])) {73if (ca.length > 1) {74throw new SAGetoptException("Argument is not expected for '" + ca[0] + "'");75}76return carg;77}7879if (los.contains(ca[0] + "=")) {80if (ca.length > 1) {81// GNU style options --file=name82_optarg = ca[1];83}84else {85// Mixed style options --file name86try {87extractOptarg(ca[0]);88} catch (ArrayIndexOutOfBoundsException e) {89throw new SAGetoptException("Argument is expected for '" + ca[0] + "'");90}91}9293return ca[0];94}9596throw new SAGetoptException("Invalid option '" + ca[0] + "'");97}9899public String next(String optStr, String[] longOptStr) {100101if (_optind >= _argv.length || _argv[_optind] == null) {102// All arguments processed103return null;104}105106String carg = _argv[_optind];107_optarg = null;108109if (_optreset) {110// End of option batch like '-abc' reached, expect option to start from '-'111112if (carg.isEmpty() || carg.charAt(0) != '-' || carg.equals("--")) {113// Stop processing on -- or first non-option argument;114return null;115}116117if (carg.startsWith("--")) {118// Handle long options, it can't be combined so it's simple119if (longOptStr == null || longOptStr.length == 0) {120// No long options expected, stop options processing121return null;122}123++ _optind;124125// at this point carg contains at least one character besides --126carg = carg.substring(2);127return processLongOptions(carg, longOptStr);128}129130if (optStr == null || optStr.length() == 0) {131// No short options132return null;133}134135// At this point carg[0] contains '-'136_optreset = false;137_optopt = 1;138}139140char ch = carg.charAt(_optopt);141142// adjust pointer to next character143_optopt += 1;144145// Okay, ready to process options like146// -abc -d bla -ef147148int chIndex = optStr.indexOf(ch);149if (chIndex == -1) {150throw new SAGetoptException("Invalid option '" + ch + "'");151}152153if (_optopt >= carg.length()) {154_optind += 1;155_optreset = true;156}157158if (chIndex < optStr.length()-1 && optStr.charAt(chIndex+1) == ':') {159// Argument expected160extractOptarg(String.valueOf(ch));161}162163return String.valueOf(ch);164}165}166167168