changeset 1105:27bf094f0ae3

add "bash" Io scheme
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 26 Jun 2017 23:52:59 -0600
parents 772d16c89056
children 151dc95f5e73
files src/luan/modules/IoLuan.java
diffstat 1 files changed, 25 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/IoLuan.java	Mon Jun 19 16:06:12 2017 -0600
+++ b/src/luan/modules/IoLuan.java	Mon Jun 26 23:52:59 2017 -0600
@@ -673,6 +673,7 @@
 			add( schemes, "luan", LuanState.class, String.class );
 			add( schemes, "stdin", LuanState.class );
 			add( schemes, "os", LuanState.class, String.class, LuanTable.class );
+			add( schemes, "bash", LuanState.class, String.class, LuanTable.class );
 		} catch(NoSuchMethodException e) {
 			throw new RuntimeException(e);
 		}
@@ -764,11 +765,12 @@
 	}
 
 
-	public static final class LuanOs extends LuanIO {
+	public static class BaseOs extends LuanIO {
 		private final String cmd;
-		private final Process proc;
+		final File dir;
+		Process proc;
 
-		private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
+		private BaseOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
 			this.cmd = cmd;
 			File dir = null;
 			if( options != null ) {
@@ -780,7 +782,7 @@
 				if( !map.isEmpty() )
 					throw new LuanException( "unrecognized options: "+map );
 			}
-			this.proc = Runtime.getRuntime().exec(cmd,null,dir);
+			this.dir = dir;
 		}
 
 		@Override public InputStream inputStream() throws IOException {
@@ -830,7 +832,7 @@
 			LuanTable tbl = super.table();
 			try {
 				tbl.rawPut( "wait_for", new LuanJavaFunction(
-					LuanOs.class.getMethod( "wait_for" ), this
+					BaseOs.class.getMethod( "wait_for" ), this
 				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
@@ -839,10 +841,28 @@
 		}
 	}
 
+	public static final class LuanOs extends BaseOs {
+		private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
+			super(luan,cmd,options);
+			this.proc = Runtime.getRuntime().exec(cmd,null,dir);
+		}
+	}
+
 	public static LuanTable os(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
 		return new LuanOs(luan,cmd,options).table();
 	}
 
+	public static final class LuanBash extends BaseOs {
+		private LuanBash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
+			super(luan,cmd,options);
+			this.proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir);
+		}
+	}
+
+	public static LuanTable bash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
+		return new LuanBash(luan,cmd,options).table();
+	}
+
 
 	public static String ip(String domain) {
 		try {