comparison src/luan/modules/IoLuan.java @ 1105:27bf094f0ae3

add "bash" Io scheme
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 26 Jun 2017 23:52:59 -0600
parents e7fb974e0c26
children 590437ce0be3
comparison
equal deleted inserted replaced
1104:772d16c89056 1105:27bf094f0ae3
671 add( schemes, "http", LuanState.class, String.class, LuanTable.class ); 671 add( schemes, "http", LuanState.class, String.class, LuanTable.class );
672 add( schemes, "https", LuanState.class, String.class, LuanTable.class ); 672 add( schemes, "https", LuanState.class, String.class, LuanTable.class );
673 add( schemes, "luan", LuanState.class, String.class ); 673 add( schemes, "luan", LuanState.class, String.class );
674 add( schemes, "stdin", LuanState.class ); 674 add( schemes, "stdin", LuanState.class );
675 add( schemes, "os", LuanState.class, String.class, LuanTable.class ); 675 add( schemes, "os", LuanState.class, String.class, LuanTable.class );
676 add( schemes, "bash", LuanState.class, String.class, LuanTable.class );
676 } catch(NoSuchMethodException e) { 677 } catch(NoSuchMethodException e) {
677 throw new RuntimeException(e); 678 throw new RuntimeException(e);
678 } 679 }
679 return schemes; 680 return schemes;
680 } 681 }
762 } 763 }
763 }; 764 };
764 } 765 }
765 766
766 767
767 public static final class LuanOs extends LuanIO { 768 public static class BaseOs extends LuanIO {
768 private final String cmd; 769 private final String cmd;
769 private final Process proc; 770 final File dir;
770 771 Process proc;
771 private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException { 772
773 private BaseOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
772 this.cmd = cmd; 774 this.cmd = cmd;
773 File dir = null; 775 File dir = null;
774 if( options != null ) { 776 if( options != null ) {
775 Map map = options.asMap(luan); 777 Map map = options.asMap(luan);
776 Object obj = map.remove("dir"); 778 Object obj = map.remove("dir");
778 if( dir==null ) 780 if( dir==null )
779 throw new LuanException( "bad option 'dir' (string or file table expected)" ); 781 throw new LuanException( "bad option 'dir' (string or file table expected)" );
780 if( !map.isEmpty() ) 782 if( !map.isEmpty() )
781 throw new LuanException( "unrecognized options: "+map ); 783 throw new LuanException( "unrecognized options: "+map );
782 } 784 }
783 this.proc = Runtime.getRuntime().exec(cmd,null,dir); 785 this.dir = dir;
784 } 786 }
785 787
786 @Override public InputStream inputStream() throws IOException { 788 @Override public InputStream inputStream() throws IOException {
787 return proc.getInputStream(); 789 return proc.getInputStream();
788 } 790 }
828 830
829 @Override public LuanTable table() { 831 @Override public LuanTable table() {
830 LuanTable tbl = super.table(); 832 LuanTable tbl = super.table();
831 try { 833 try {
832 tbl.rawPut( "wait_for", new LuanJavaFunction( 834 tbl.rawPut( "wait_for", new LuanJavaFunction(
833 LuanOs.class.getMethod( "wait_for" ), this 835 BaseOs.class.getMethod( "wait_for" ), this
834 ) ); 836 ) );
835 } catch(NoSuchMethodException e) { 837 } catch(NoSuchMethodException e) {
836 throw new RuntimeException(e); 838 throw new RuntimeException(e);
837 } 839 }
838 return tbl; 840 return tbl;
839 } 841 }
840 } 842 }
841 843
844 public static final class LuanOs extends BaseOs {
845 private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
846 super(luan,cmd,options);
847 this.proc = Runtime.getRuntime().exec(cmd,null,dir);
848 }
849 }
850
842 public static LuanTable os(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException { 851 public static LuanTable os(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
843 return new LuanOs(luan,cmd,options).table(); 852 return new LuanOs(luan,cmd,options).table();
853 }
854
855 public static final class LuanBash extends BaseOs {
856 private LuanBash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
857 super(luan,cmd,options);
858 this.proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir);
859 }
860 }
861
862 public static LuanTable bash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
863 return new LuanBash(luan,cmd,options).table();
844 } 864 }
845 865
846 866
847 public static String ip(String domain) { 867 public static String ip(String domain) {
848 try { 868 try {