changeset 1849:9f2680fe532b default tip

better proc handling
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 21 Feb 2025 12:23:56 -0700
parents 6f3f1768fdde
children
files src/luan/modules/IoLuan.java
diffstat 1 files changed, 30 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/IoLuan.java	Thu Feb 20 14:38:07 2025 -0700
+++ b/src/luan/modules/IoLuan.java	Fri Feb 21 12:23:56 2025 -0700
@@ -532,11 +532,11 @@
 
 
 	public static class BaseOs extends LuanIO {
-		private final String cmd;
-		final File dir;
-		Process proc;
+		private final String[] cmd;
+		private final File dir;
+		private Process proc = null;
 
-		private BaseOs(Luan luan,String cmd,LuanTable options) throws IOException, LuanException {
+		private BaseOs(Luan luan,String[] cmd,LuanTable options) throws IOException, LuanException {
 			this.cmd = cmd;
 			File dir = null;
 			if( options != null ) {
@@ -551,16 +551,35 @@
 			this.dir = dir;
 		}
 
+		private void setProc() throws IOException {
+			proc = Runtime.getRuntime().exec(cmd,null,dir);
+		}
+
+		private Process proc() throws IOException {
+			if( proc == null ) {
+				setProc();
+			} else {
+				try {
+					proc.getInputStream().available();
+				} catch(IOException e) {
+					if( !e.getMessage().equals("Stream closed") )
+						throw e;
+					setProc();
+				}
+			}
+			return proc;
+		}
+
 		@Override public InputStream inputStream() throws IOException {
-			return proc.getInputStream();
+			return proc().getInputStream();
 		}
 
 		@Override OutputStream outputStream() throws IOException {
-			return proc.getOutputStream();
+			return proc().getOutputStream();
 		}
 
 		@Override public String to_string() {
-			return proc.toString();
+			return proc==null ? "null-proc" : proc.toString();
 		}
 
 		@Override public String to_uri_string() {
@@ -574,6 +593,8 @@
 		public void wait_for()
 			throws IOException
 		{
+			if( proc == null )
+				setProc();
 			IoUtils.waitFor(proc);
 		}
 
@@ -590,17 +611,15 @@
 
 	public static final class LuanOs extends BaseOs {
 		public LuanOs(Luan luan,String cmd,LuanTable options) throws IOException, LuanException {
-			super(luan,cmd,options);
+			super(luan,new String[]{cmd},options);
 			check(luan,"os:"+cmd);
-			this.proc = Runtime.getRuntime().exec(cmd,null,dir);
 		}
 	}
 
 	public static final class LuanBash extends BaseOs {
 		public LuanBash(Luan luan,String cmd,LuanTable options) throws IOException, LuanException {
-			super(luan,cmd,options);
+			super(luan,new String[]{"bash","-c",cmd},options);
 			check(luan,"bash:"+cmd);
-			this.proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir);
 		}
 	}