Mercurial Hosting > luan
changeset 734:e44e98fe9de8
add scheme "os:..."
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 14 Jun 2016 17:54:12 -0600 |
parents | ffbbe25dab09 |
children | 2486fa6490c6 |
files | core/src/luan/modules/IoLuan.java core/src/luan/modules/Utils.java |
diffstat | 2 files changed, 71 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
diff -r ffbbe25dab09 -r e44e98fe9de8 core/src/luan/modules/IoLuan.java --- a/core/src/luan/modules/IoLuan.java Tue Jun 14 00:04:08 2016 -0600 +++ b/core/src/luan/modules/IoLuan.java Tue Jun 14 17:54:12 2016 -0600 @@ -582,6 +582,7 @@ add( schemes, "https", LuanState.class, String.class, LuanTable.class ); add( schemes, "luan", LuanState.class, String.class ); add( schemes, "stdin", LuanState.class ); + add( schemes, "os", String.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -669,6 +670,74 @@ } + public static final class LuanOs extends LuanIO { + private final Process proc; + + private LuanOs(String cmd) throws IOException { + this.proc = Runtime.getRuntime().exec(cmd); + } + + @Override public InputStream inputStream() throws IOException { + return proc.getInputStream(); + } + + @Override OutputStream outputStream() throws IOException { + return proc.getOutputStream(); + } + + @Override public String to_string() { + return proc.toString(); + } + + @Override public String to_uri_string() { + throw new UnsupportedOperationException(); + } + + @Override public boolean exists() { + return true; + } + + public void wait_for() + throws IOException, LuanException + { + try { + proc.waitFor(); + } catch(InterruptedException e) { + throw new RuntimeException(e); + } + int exitVal = proc.exitValue(); + if( exitVal != 0 ) { + Reader err = new InputStreamReader(proc.getErrorStream()); + String error = Utils.readAll(err); + err.close(); + throw new LuanException(error); + } + } + + @Override public String read_text() throws IOException, LuanException { + String s = super.read_text(); + wait_for(); + return s; + } + + @Override public LuanTable table() { + LuanTable tbl = super.table(); + try { + tbl.rawPut( "wait_for", new LuanJavaFunction( + LuanOs.class.getMethod( "wait_for" ), this + ) ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + return tbl; + } + } + + public static LuanTable os(String cmd) throws IOException { + return new LuanOs(cmd).table(); + } + + public static String ip(String domain) { try { return InetAddress.getByName(domain).getHostAddress();
diff -r ffbbe25dab09 -r e44e98fe9de8 core/src/luan/modules/Utils.java --- a/core/src/luan/modules/Utils.java Tue Jun 14 00:04:08 2016 -0600 +++ b/core/src/luan/modules/Utils.java Tue Jun 14 17:54:12 2016 -0600 @@ -152,6 +152,7 @@ +/* replace by uri"os:..." // process @@ -195,5 +196,5 @@ checkProcess(proc); return s; } - +*/ }