comparison core/src/luan/modules/IoLuan.java @ 734:e44e98fe9de8

add scheme "os:..."
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Jun 2016 17:54:12 -0600
parents a741a3a33423
children 2486fa6490c6
comparison
equal deleted inserted replaced
733:ffbbe25dab09 734:e44e98fe9de8
580 add( schemes, "socket", String.class ); 580 add( schemes, "socket", String.class );
581 add( schemes, "http", LuanState.class, String.class, LuanTable.class ); 581 add( schemes, "http", LuanState.class, String.class, LuanTable.class );
582 add( schemes, "https", LuanState.class, String.class, LuanTable.class ); 582 add( schemes, "https", LuanState.class, String.class, LuanTable.class );
583 add( schemes, "luan", LuanState.class, String.class ); 583 add( schemes, "luan", LuanState.class, String.class );
584 add( schemes, "stdin", LuanState.class ); 584 add( schemes, "stdin", LuanState.class );
585 add( schemes, "os", String.class );
585 } catch(NoSuchMethodException e) { 586 } catch(NoSuchMethodException e) {
586 throw new RuntimeException(e); 587 throw new RuntimeException(e);
587 } 588 }
588 return schemes; 589 return schemes;
589 } 590 }
664 } catch(IOException e) { 665 } catch(IOException e) {
665 throw new LuanException(e); 666 throw new LuanException(e);
666 } 667 }
667 } 668 }
668 }; 669 };
670 }
671
672
673 public static final class LuanOs extends LuanIO {
674 private final Process proc;
675
676 private LuanOs(String cmd) throws IOException {
677 this.proc = Runtime.getRuntime().exec(cmd);
678 }
679
680 @Override public InputStream inputStream() throws IOException {
681 return proc.getInputStream();
682 }
683
684 @Override OutputStream outputStream() throws IOException {
685 return proc.getOutputStream();
686 }
687
688 @Override public String to_string() {
689 return proc.toString();
690 }
691
692 @Override public String to_uri_string() {
693 throw new UnsupportedOperationException();
694 }
695
696 @Override public boolean exists() {
697 return true;
698 }
699
700 public void wait_for()
701 throws IOException, LuanException
702 {
703 try {
704 proc.waitFor();
705 } catch(InterruptedException e) {
706 throw new RuntimeException(e);
707 }
708 int exitVal = proc.exitValue();
709 if( exitVal != 0 ) {
710 Reader err = new InputStreamReader(proc.getErrorStream());
711 String error = Utils.readAll(err);
712 err.close();
713 throw new LuanException(error);
714 }
715 }
716
717 @Override public String read_text() throws IOException, LuanException {
718 String s = super.read_text();
719 wait_for();
720 return s;
721 }
722
723 @Override public LuanTable table() {
724 LuanTable tbl = super.table();
725 try {
726 tbl.rawPut( "wait_for", new LuanJavaFunction(
727 LuanOs.class.getMethod( "wait_for" ), this
728 ) );
729 } catch(NoSuchMethodException e) {
730 throw new RuntimeException(e);
731 }
732 return tbl;
733 }
734 }
735
736 public static LuanTable os(String cmd) throws IOException {
737 return new LuanOs(cmd).table();
669 } 738 }
670 739
671 740
672 public static String ip(String domain) { 741 public static String ip(String domain) {
673 try { 742 try {