Skip to content

Commit

Permalink
[Truffle] Restore some command line functionality I fudged.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Dec 9, 2016
1 parent 5a948d5 commit 3113f09
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 50 deletions.
14 changes: 13 additions & 1 deletion truffle/src/main/java/org/jruby/truffle/Main.java
Expand Up @@ -49,6 +49,7 @@
import org.jruby.truffle.options.OptionsCatalog;
import org.jruby.truffle.options.OutputStrings;
import org.jruby.truffle.options.RubyInstanceConfig;
import org.jruby.truffle.util.MainExitException;

import java.io.InputStream;
import java.lang.management.ManagementFactory;
Expand All @@ -62,7 +63,18 @@ public static void main(String[] args) {
printTruffleTimeMetric("before-main");

final RubyInstanceConfig config = new RubyInstanceConfig(false);
config.processArguments(args);

try {
config.processArguments(args);
} catch (MainExitException mee) {
if (!mee.isAborted()) {
config.getError().println(mee.getMessage());
if (mee.isUsageError()) {
doPrintUsage(config, true);
}
}
System.exit(mee.getStatus());
}

doShowVersion(config);
doShowCopyright(config);
Expand Down
101 changes: 52 additions & 49 deletions truffle/src/main/java/org/jruby/truffle/options/ArgumentProcessor.java
Expand Up @@ -29,6 +29,8 @@
package org.jruby.truffle.options;

import org.jruby.truffle.core.string.StringSupport;
import org.jruby.truffle.util.KCode;
import org.jruby.truffle.util.MainExitException;
import org.jruby.truffle.util.SafePropertyAccessor;

import java.io.File;
Expand Down Expand Up @@ -197,10 +199,9 @@ private void processArgument() {
//config.setRecordSeparator(String.valueOf((char) val));
throw new UnsupportedOperationException();
} catch (Exception e) {
//MainExitException mee = new MainExitException(1, getArgumentError(" -0 must be followed by either 0, 777, or a valid octal value"));
//mee.setUsageError(true);
//throw mee;
throw new UnsupportedOperationException();
MainExitException mee = new MainExitException(1, getArgumentError(" -0 must be followed by either 0, 777, or a valid octal value"));
mee.setUsageError(true);
throw mee;
}
}
//break FOR;
Expand All @@ -227,12 +228,10 @@ private void processArgument() {
config.setCurrentDirectory(new File(base, newDir.getPath()).getCanonicalPath());
}
if (!(new File(config.getCurrentDirectory()).isDirectory()) && !config.getCurrentDirectory().startsWith("uri:classloader:")) {
//throw new MainExitException(1, "jruby: Can't chdir to " + saved + " (fatal)");
throw new UnsupportedOperationException();
throw new MainExitException(1, "jruby: Can't chdir to " + saved + " (fatal)");
}
} catch (IOException e) {
//throw new MainExitException(1, getArgumentError(" -C must be followed by a valid directory"));
throw new UnsupportedOperationException();
throw new MainExitException(1, getArgumentError(" -C must be followed by a valid directory"));
}
break FOR;
case 'd':
Expand Down Expand Up @@ -282,7 +281,19 @@ private void processArgument() {
}
break FOR;
case 'K': // @Deprecated TODO no longer relevant in Ruby 2.x
throw new UnsupportedOperationException();
String eArg = grabValue(getArgumentError("provide a value for -K"));

config.setKCode(KCode.create(eArg));

// source encoding
config.setSourceEncoding(config.getKCode().getEncoding().toString());

// set external encoding if not already specified
if (config.getExternalEncoding() == null) {
config.setExternalEncoding(config.getKCode().getEncoding().toString());
}

break;
case 'l':
disallowedInRubyOpts(argument);
config.setProcessLineEnds(true);
Expand Down Expand Up @@ -339,10 +350,9 @@ private void processArgument() {
} else if (temp.equals("2")) {
config.setVerbosity(Verbosity.TRUE);
} else {
//MainExitException mee = new MainExitException(1, getArgumentError(" -W must be followed by either 0, 1, 2 or nothing"));
//mee.setUsageError(true);
//throw mee;
throw new UnsupportedOperationException();
MainExitException mee = new MainExitException(1, getArgumentError(" -W must be followed by either 0, 1, 2 or nothing"));
mee.setUsageError(true);
throw mee;
}
}
break FOR;
Expand All @@ -362,33 +372,24 @@ private void processArgument() {
config.setCurrentDirectory(new File(base, newDir.getPath()).getCanonicalPath());
}
if (!(new File(config.getCurrentDirectory()).isDirectory()) && !config.getCurrentDirectory().startsWith("uri:classloader:")) {
//throw new MainExitException(1, "jruby: Can't chdir to " + saved + " (fatal)");
throw new UnsupportedOperationException();
throw new MainExitException(1, "jruby: Can't chdir to " + saved + " (fatal)");
}
}
config.setXFlag(true);
} catch (IOException e) {
//throw new MainExitException(1, getArgumentError(" -x must be followed by a valid directory"));
throw new UnsupportedOperationException();
throw new MainExitException(1, getArgumentError(" -x must be followed by a valid directory"));
}
break FOR;
case 'X':
disallowedInRubyOpts(argument);
String extendedOption = grabOptionalValue();
if (extendedOption == null) {
if (SafePropertyAccessor.getBoolean("jruby.launcher.nopreamble", false)) {
//throw new MainExitException(0, OutputStrings.getExtendedHelp());
throw new UnsupportedOperationException();
} else {
//throw new MainExitException(0, "jruby: missing argument\n" + OutputStrings.getExtendedHelp());
throw new UnsupportedOperationException();
}
throw new MainExitException(0, "no extended options in Truffle");
} else if (extendedOption.equals("-C") || extendedOption.equals("-CIR")) {
throw new UnsupportedOperationException();
} else if (extendedOption.equals("+C") || extendedOption.equals("+CIR")) {
throw new UnsupportedOperationException();
} else if (extendedOption.equals("classic")) {
//throw new MainExitException(0, "jruby: the -Xclassic option should have been handled in the launcher");
throw new UnsupportedOperationException();
} else if (extendedOption.equals("+T")) {
// Nothing
Expand All @@ -397,14 +398,17 @@ private void processArgument() {
} else if (extendedOption.endsWith("?")) {
throw new UnsupportedOperationException();
} else {
//MainExitException mee = new MainExitException(1, "jruby: invalid extended option " + extendedOption + " (-X will list valid options)\n");
//mee.setUsageError(true);
//throw mee;
throw new UnsupportedOperationException();
MainExitException mee = new MainExitException(1, "jruby: invalid extended option " + extendedOption + " (-X will list valid options)\n");
mee.setUsageError(true);
throw mee;
}
break FOR;
case 'y':
throw new UnsupportedOperationException();
disallowedInRubyOpts(argument);
if (!rubyOpts) {
throw new UnsupportedOperationException();
}
break FOR;
case '-':
if (argument.equals("--command") || argument.equals("--bin")) {
characterIndex = argument.length();
Expand Down Expand Up @@ -499,10 +503,9 @@ private void processArgument() {
} else if (dumpArg.equals("insns")) {
config.setShowBytecode(true);
} else {
//MainExitException mee = new MainExitException(1, error);
//mee.setUsageError(true);
//throw mee;
throw new UnsupportedOperationException();
MainExitException mee = new MainExitException(1, error);
mee.setUsageError(true);
throw mee;
}
break;
} else if (argument.equals("--dev")) {
Expand All @@ -516,6 +519,11 @@ private void processArgument() {
} else if (argument.equals("--verbose")) {
config.setVerbosity(Verbosity.TRUE);
break FOR;
} else if (argument.equals("--yydebug")) {
disallowedInRubyOpts(argument);
if (!rubyOpts) {
throw new UnsupportedOperationException();
}
} else {
if (argument.equals("--")) {
// ruby interpreter compatibilty
Expand All @@ -525,8 +533,7 @@ private void processArgument() {
}
}
default:
//throw new MainExitException(1, "jruby: unknown option " + argument);
throw new UnsupportedOperationException();
throw new MainExitException(1, "jruby: unknown option " + argument);
}
}
}
Expand All @@ -552,25 +559,22 @@ private static String[] valueListFor(String argument, String key) {

private void disallowedInRubyOpts(CharSequence option) {
if (rubyOpts) {
//throw new MainExitException(1, "jruby: invalid switch in RUBYOPT: " + option + " (RuntimeError)");
throw new UnsupportedOperationException();
throw new MainExitException(1, "jruby: invalid switch in RUBYOPT: " + option + " (RuntimeError)");
}
}

private static void errorMissingEquals(String label) {
//MainExitException mee;
//mee = new MainExitException(1, "missing argument for --" + label + "\n");
//mee.setUsageError(true);
//throw mee;
throw new UnsupportedOperationException();
MainExitException mee;
mee = new MainExitException(1, "missing argument for --" + label + "\n");
mee.setUsageError(true);
throw mee;
}

private void processEncodingOption(String value) {
List<String> encodings = StringSupport.split(value, ':', 3);
switch (encodings.size()) {
case 3:
//throw new MainExitException(1, "extra argument for -E: " + encodings.get(2));
throw new UnsupportedOperationException();
throw new MainExitException(1, "extra argument for -E: " + encodings.get(2));
case 2:
config.setInternalEncoding(encodings.get(1));
case 1:
Expand Down Expand Up @@ -667,10 +671,9 @@ private String grabValue(String errorMessage, boolean usageError) {
if (argumentIndex < arguments.size()) {
return arguments.get(argumentIndex).originalValue;
}
//MainExitException mee = new MainExitException(1, errorMessage);
//if (usageError) mee.setUsageError(true);
//throw mee;
throw new UnsupportedOperationException();
MainExitException mee = new MainExitException(1, errorMessage);
if (usageError) mee.setUsageError(true);
throw mee;
}

private String grabOptionalValue() {
Expand Down
Expand Up @@ -30,8 +30,10 @@

import com.oracle.truffle.api.TruffleOptions;
import jnr.posix.util.Platform;
import org.jcodings.Encoding;
import org.jruby.truffle.core.string.StringSupport;
import org.jruby.truffle.language.control.JavaException;
import org.jruby.truffle.util.KCode;
import org.jruby.truffle.util.SafePropertyAccessor;

import java.io.BufferedInputStream;
Expand Down Expand Up @@ -633,6 +635,8 @@ public static ClassLoader defaultClassLoader() {
private boolean hasScriptArgv = false;
private boolean frozenStringLiteral = false;
private String jrubyHome;
private KCode kcode;
private String sourceEncoding;

private boolean forceStdin = false;

Expand Down Expand Up @@ -764,4 +768,16 @@ public boolean isCextEnabled() {
}

@Deprecated public static final String JIT_CODE_CACHE = "";

public void setKCode(KCode kcode) {
this.kcode = kcode;
}

public KCode getKCode() {
return kcode;
}

public void setSourceEncoding(String sourceEncoding) {
this.sourceEncoding = sourceEncoding;
}
}
@@ -0,0 +1,79 @@
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
* Copyright (C) 2006 Charles O Nutter <headius@headius.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.truffle.util;

public class MainExitException extends RuntimeException {
private static final long serialVersionUID = -8585821821150293755L;
boolean usageError;
int status;
private boolean aborted;

public MainExitException(int status) {
this(status, true);
}

public MainExitException(int status, String message) {
super(message);

this.status = status;
}

public MainExitException(int status, boolean aborted) {
super("aborted");

this.status = status;
this.aborted = aborted;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public void setAborted(boolean aborted) {
this.aborted = aborted;
}

public boolean isAborted() {
return this.aborted;
}

public boolean isUsageError() {
return usageError;
}

public void setUsageError(boolean usageError) {
this.usageError = usageError;
}

public Throwable fillInStackTrace() {
return this;
}
}

0 comments on commit 3113f09

Please sign in to comment.