31978
|
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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
|
|
25 |
package sun.jvm.hotspot;
|
|
26 |
|
|
27 |
import java.util.Arrays;
|
|
28 |
import java.util.List;
|
|
29 |
|
|
30 |
public class SAGetopt {
|
|
31 |
|
|
32 |
private String[] _argv;
|
|
33 |
|
|
34 |
private int _optind; // index in arguments array
|
|
35 |
private int _optopt; // index within an argument
|
|
36 |
private String _optarg; // last option argument
|
|
37 |
private boolean _optreset; // special handling of first call
|
|
38 |
|
|
39 |
public SAGetopt(String[] args) {
|
|
40 |
_argv = args;
|
|
41 |
_optind = 0;
|
|
42 |
_optopt = 1;
|
|
43 |
_optarg = null;
|
|
44 |
_optreset = true;
|
|
45 |
}
|
|
46 |
|
|
47 |
public String getOptarg() {
|
|
48 |
return _optarg;
|
|
49 |
}
|
|
50 |
|
|
51 |
public int getOptind() {
|
|
52 |
return _optind;
|
|
53 |
}
|
|
54 |
|
|
55 |
private void extractOptarg(String opt) {
|
|
56 |
// Argument expected
|
|
57 |
if (_optind > _argv.length) {
|
|
58 |
throw new RuntimeException("Not enough arguments for '" + opt + "'");
|
|
59 |
}
|
|
60 |
|
|
61 |
if (! _argv[_optind].isEmpty() && _argv[_optind].charAt(0) == '-') {
|
|
62 |
throw new RuntimeException("Argument is expected for '" + opt + "'");
|
|
63 |
}
|
|
64 |
|
|
65 |
_optarg = _argv[_optind];
|
|
66 |
_optind += 1;
|
|
67 |
}
|
|
68 |
|
|
69 |
private String processLongOptions(String carg, String[] longOptStr) {
|
|
70 |
List<String> los = Arrays.asList(longOptStr);
|
|
71 |
String[] ca = carg.split("=", 2);
|
|
72 |
|
|
73 |
if (los.contains(ca[0])) {
|
|
74 |
if (ca.length > 1) {
|
|
75 |
throw new RuntimeException("Argument is not expected for '" + ca[0] + "'");
|
|
76 |
}
|
|
77 |
return carg;
|
|
78 |
}
|
|
79 |
|
|
80 |
if (los.contains(ca[0] + "=")) {
|
|
81 |
if (ca.length > 1) {
|
|
82 |
// GNU style options --file=name
|
|
83 |
_optarg = ca[1];
|
|
84 |
}
|
|
85 |
else {
|
|
86 |
// Mixed style options --file name
|
|
87 |
extractOptarg(ca[0]);
|
|
88 |
}
|
|
89 |
|
|
90 |
return ca[0];
|
|
91 |
}
|
|
92 |
|
|
93 |
throw new RuntimeException("Invalid option '" + ca[0] + "'");
|
|
94 |
}
|
|
95 |
|
|
96 |
public String next(String optStr, String[] longOptStr) {
|
|
97 |
|
|
98 |
if (_optind >= _argv.length || _argv[_optind] == null) {
|
|
99 |
// All arguments processed
|
|
100 |
return null;
|
|
101 |
}
|
|
102 |
|
|
103 |
String carg = _argv[_optind];
|
|
104 |
_optarg = null;
|
|
105 |
|
|
106 |
if (_optreset) {
|
|
107 |
// End of option batch like '-abc' reached, expect option to start from '-'
|
|
108 |
|
|
109 |
if (carg.isEmpty() || carg.charAt(0) != '-' || carg.equals("--")) {
|
|
110 |
// Stop processing on -- or first non-option argument;
|
|
111 |
return null;
|
|
112 |
}
|
|
113 |
|
|
114 |
if (carg.startsWith("--")) {
|
|
115 |
// Handle long options, it can't be combined so it's simple
|
|
116 |
if (longOptStr == null || longOptStr.length == 0) {
|
|
117 |
// No long options expected, stop options processing
|
|
118 |
return null;
|
|
119 |
}
|
|
120 |
++ _optind;
|
|
121 |
|
|
122 |
// at this point carg contains at least one character besides --
|
|
123 |
carg = carg.substring(2);
|
|
124 |
return processLongOptions(carg, longOptStr);
|
|
125 |
}
|
|
126 |
|
|
127 |
if (optStr == null || optStr.length() == 0) {
|
|
128 |
// No short options
|
|
129 |
return null;
|
|
130 |
}
|
|
131 |
|
|
132 |
// At this point carg[0] contains '-'
|
|
133 |
_optreset = false;
|
|
134 |
_optopt = 1;
|
|
135 |
}
|
|
136 |
|
|
137 |
char ch = carg.charAt(_optopt);
|
|
138 |
|
|
139 |
// adjust pointer to next character
|
|
140 |
_optopt += 1;
|
|
141 |
|
|
142 |
// Okay, ready to process options like
|
|
143 |
// -abc -d bla -ef
|
|
144 |
|
|
145 |
int chIndex = optStr.indexOf(ch);
|
|
146 |
if (chIndex == -1) {
|
|
147 |
throw new RuntimeException("Invalid option '" + ch + "'");
|
|
148 |
}
|
|
149 |
|
|
150 |
if (_optopt >= carg.length()) {
|
|
151 |
_optind += 1;
|
|
152 |
_optreset = true;
|
|
153 |
}
|
|
154 |
|
|
155 |
if (chIndex < optStr.length()-1 && optStr.charAt(chIndex+1) == ':') {
|
|
156 |
// Argument expected
|
|
157 |
extractOptarg(String.valueOf(ch));
|
|
158 |
}
|
|
159 |
|
|
160 |
return String.valueOf(ch);
|
|
161 |
}
|
|
162 |
}
|