Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SAGetopt.java
41159 views
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.clone();
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 SAGetoptException("Not enough arguments for '" + opt + "'");
59
}
60
61
if (! _argv[_optind].isEmpty() && _argv[_optind].charAt(0) == '-') {
62
throw new SAGetoptException("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 SAGetoptException("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
try {
88
extractOptarg(ca[0]);
89
} catch (ArrayIndexOutOfBoundsException e) {
90
throw new SAGetoptException("Argument is expected for '" + ca[0] + "'");
91
}
92
}
93
94
return ca[0];
95
}
96
97
throw new SAGetoptException("Invalid option '" + ca[0] + "'");
98
}
99
100
public String next(String optStr, String[] longOptStr) {
101
102
if (_optind >= _argv.length || _argv[_optind] == null) {
103
// All arguments processed
104
return null;
105
}
106
107
String carg = _argv[_optind];
108
_optarg = null;
109
110
if (_optreset) {
111
// End of option batch like '-abc' reached, expect option to start from '-'
112
113
if (carg.isEmpty() || carg.charAt(0) != '-' || carg.equals("--")) {
114
// Stop processing on -- or first non-option argument;
115
return null;
116
}
117
118
if (carg.startsWith("--")) {
119
// Handle long options, it can't be combined so it's simple
120
if (longOptStr == null || longOptStr.length == 0) {
121
// No long options expected, stop options processing
122
return null;
123
}
124
++ _optind;
125
126
// at this point carg contains at least one character besides --
127
carg = carg.substring(2);
128
return processLongOptions(carg, longOptStr);
129
}
130
131
if (optStr == null || optStr.length() == 0) {
132
// No short options
133
return null;
134
}
135
136
// At this point carg[0] contains '-'
137
_optreset = false;
138
_optopt = 1;
139
}
140
141
char ch = carg.charAt(_optopt);
142
143
// adjust pointer to next character
144
_optopt += 1;
145
146
// Okay, ready to process options like
147
// -abc -d bla -ef
148
149
int chIndex = optStr.indexOf(ch);
150
if (chIndex == -1) {
151
throw new SAGetoptException("Invalid option '" + ch + "'");
152
}
153
154
if (_optopt >= carg.length()) {
155
_optind += 1;
156
_optreset = true;
157
}
158
159
if (chIndex < optStr.length()-1 && optStr.charAt(chIndex+1) == ':') {
160
// Argument expected
161
extractOptarg(String.valueOf(ch));
162
}
163
164
return String.valueOf(ch);
165
}
166
}
167
168