Skip to content

Commit

Permalink
stdio streams may now be set at a single place in ERT#setXxxStream() …
Browse files Browse the repository at this point in the history
…or ...

...ERT#set_stdio()

Manually applied patch by jetztgradnet

9abc3cc
  • Loading branch information
krestenkrab committed Sep 22, 2011
1 parent 644bf65 commit 91f305e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 16 deletions.
6 changes: 3 additions & 3 deletions erjang_cfg.properties
@@ -1,6 +1,6 @@
#erjang.otp.root = /usr/local/lib/erlang
#erjang.erts.version = 5.8
#erjang.otp.version = R14B
erjang.otp.root = /usr/local/lib/erlang
erjang.erts.version = 5.8.3
erjang.otp.version = R14B02

# erjang.debug.port=true
# erjang.debug.inet=true
Expand Down
76 changes: 70 additions & 6 deletions src/main/java/erjang/ERT.java
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -1050,15 +1051,56 @@ public static void shutdown() {
async_scheduler.shutdown();
}

static public InputStream orig_in = System.in;
static public PrintStream orig_out = System.out;
static public PrintStream orig_err = System.err;
static protected volatile InputStream in = System.in;
static protected volatile PrintStream out = System.out;
static protected volatile PrintStream err = System.err;

public static void set_stdio(InputStream in,
PrintStream out, PrintStream err) {
System.setIn(in);
System.setOut(out);
System.setErr(err);
// avoid null
ERT.in = (in != null ? in : new NullInputStream());
ERT.out = (out != null ? out : new PrintStream(new NullOutputStream()));
ERT.err = (err != null ? err : new PrintStream(new NullOutputStream()));
}

public static void set_stdio(InputStream in,
OutputStream out, OutputStream err) {
// avoid null
ERT.in = (in != null ? in : new NullInputStream());
ERT.out = (out != null ? new PrintStream(out) : new PrintStream(new NullOutputStream()));
ERT.err = (err != null ? new PrintStream(err) : new PrintStream(new NullOutputStream()));
}

public static void setInputStream(InputStream in) {
ERT.in = (in != null ? in : new NullInputStream());
}

public static InputStream getInputStream() {
return in;
}

public static void setOutputStream(PrintStream out) {
ERT.out = (out != null ? out : new PrintStream(new NullOutputStream()));
}

public static void setOutputStream(OutputStream out) {
ERT.out = (out != null ? new PrintStream(out) : new PrintStream(new NullOutputStream()));
}

public static PrintStream getOutputStream() {
return out;
}

public static void setErrorStream(PrintStream err) {
ERT.err = (err != null ? err : new PrintStream(new NullOutputStream()));
}

public static void setErrorStream(OutputStream err) {
ERT.err = (err != null ? new PrintStream(err) : new PrintStream(new NullOutputStream()));
}

public static PrintStream getErrorStream() {
return err;
}

public static File newFile(String file_name) {
Expand All @@ -1077,4 +1119,26 @@ public static void debug(boolean condition, String text) {
debug(text);
}
}

static class NullInputStream extends InputStream {
@Override
public int read() throws IOException {
return -1;
}
}
static class NullOutputStream extends OutputStream {
@Override
public void write(int arg0) throws IOException {
// ignore
}
@Override
public void write(byte[] b) throws IOException {
// ignore
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
// ignore
}
}

}
3 changes: 2 additions & 1 deletion src/main/java/erjang/console/TTYTextAreaDriverControl.java
Expand Up @@ -47,6 +47,7 @@
import erjang.EHandle;
import erjang.EObject;
import erjang.EPID;
import erjang.ERT;
import erjang.EString;
import erjang.driver.EDriverInstance;
import erjang.driver.IO;
Expand Down Expand Up @@ -157,7 +158,7 @@ public void replace(DocumentFilter.FilterBypass fb,
}

public InputStream getInputStream() {
return System.in;
return ERT.getInputStream();
}

public OutputStream getOutputStream() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/erjang/driver/FDDriverInstance.java
Expand Up @@ -60,15 +60,15 @@ public FDDriverInstance(int in, int out) {
this.out = out;

if (in == 0) {
ins = System.in;
ins = ERT.getInputStream();
}

if (out == 1) {
outs = System.out;
outs = ERT.getOutputStream();
}

if (out == 2) {
outs = System.err;
outs = ERT.getErrorStream();
}
// that's it.
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/erjang/driver/efile/EFile.java
Expand Up @@ -1965,7 +1965,7 @@ void reply_list_directory(String[] files) throws Pausable {
*
* @see http://www.erlang.org/doc/apps/stdlib/unicode_usage.html#id60205
*/
static boolean unicodeDriverInterface = ("R14B".compareTo(erjang.Main.otp_version) < 0);
static boolean unicodeDriverInterface = ("R14B".compareTo(erjang.Main.otp_version == null ? "" : erjang.Main.otp_version) < 0);

/**
* Determine whether to use the new unicode driver interface
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/erjang/m/erlang/ErlProc.java
Expand Up @@ -120,7 +120,7 @@ public static EObject process_info(EObject pid) {

@BIF
public static EObject display(EProc proc, EObject obj) {
System.out.println(obj);
ERT.getOutputStream().println(obj);
return ERT.TRUE;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/erjang/util/Progress.java
Expand Up @@ -21,6 +21,8 @@
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import erjang.ERT;

public class Progress {
static ProgressListener listener;

Expand All @@ -47,7 +49,7 @@ static public void activity(String string) {
}

if (!Boolean.parseBoolean(suppress)) {
System.err.write(wheel[next % 4]);
ERT.getErrorStream().write(wheel[next % 4]);
}
} catch (IOException e) {
// ignore
Expand Down

0 comments on commit 91f305e

Please sign in to comment.