comparison src/luan/modules/IoLuan.java @ 1280:781ec0a92bb5

add Boot.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 20 Dec 2018 13:38:16 -0700
parents d83f6cc558de
children ca742d51b31f
comparison
equal deleted inserted replaced
1279:323743a7f317 1280:781ec0a92bb5
14 import java.io.FileInputStream; 14 import java.io.FileInputStream;
15 import java.io.FileOutputStream; 15 import java.io.FileOutputStream;
16 import java.io.InputStreamReader; 16 import java.io.InputStreamReader;
17 import java.io.OutputStreamWriter; 17 import java.io.OutputStreamWriter;
18 import java.io.ByteArrayInputStream; 18 import java.io.ByteArrayInputStream;
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.StringWriter; 19 import java.io.StringWriter;
22 import java.io.IOException; 20 import java.io.IOException;
23 import java.io.FileNotFoundException; 21 import java.io.FileNotFoundException;
24 import java.net.URL; 22 import java.net.URL;
25 import java.net.Socket;
26 import java.net.ServerSocket;
27 import java.net.InetAddress; 23 import java.net.InetAddress;
28 import java.net.Inet4Address; 24 import java.net.Inet4Address;
29 import java.net.NetworkInterface; 25 import java.net.NetworkInterface;
30 import java.net.MalformedURLException; 26 import java.net.MalformedURLException;
31 import java.net.UnknownHostException; 27 import java.net.UnknownHostException;
32 import java.util.Enumeration; 28 import java.util.Enumeration;
33 import java.util.List;
34 import java.util.Map; 29 import java.util.Map;
35 import luan.Luan;
36 import luan.LuanState; 30 import luan.LuanState;
37 import luan.LuanTable; 31 import luan.LuanTable;
38 import luan.LuanFunction; 32 import luan.LuanFunction;
39 import luan.LuanJavaFunction;
40 import luan.LuanException; 33 import luan.LuanException;
41 import luan.modules.url.LuanUrl; 34 import luan.modules.url.LuanUrl;
42 35
43 36
44 public final class IoLuan { 37 public final class IoLuan {
45
46 private static void add(LuanTable t,String method,Class... parameterTypes) throws NoSuchMethodException {
47 t.rawPut( method, new LuanJavaFunction(IoLuan.class.getMethod(method,parameterTypes),null) );
48 }
49 38
50 public static String read_console_line(String prompt) throws IOException { 39 public static String read_console_line(String prompt) throws IOException {
51 if( prompt==null ) 40 if( prompt==null )
52 prompt = "> "; 41 prompt = "> ";
53 return System.console().readLine(prompt); 42 return System.console().readLine(prompt);
58 public Object out(); 47 public Object out();
59 public void write(LuanState luan,Object... args) throws LuanException, IOException; 48 public void write(LuanState luan,Object... args) throws LuanException, IOException;
60 public void close() throws IOException; 49 public void close() throws IOException;
61 } 50 }
62 51
63 private static LuanWriter luanWriter(final PrintStream out) { 52 public static LuanWriter luanWriter(final PrintStream out) {
64 return new LuanWriter() { 53 return new LuanWriter() {
65 54
66 public Object out() { 55 public Object out() {
67 return out; 56 return out;
68 } 57 }
77 out.close(); 66 out.close();
78 } 67 }
79 }; 68 };
80 } 69 }
81 70
82 public static LuanTable textWriter(LuanState luan,final PrintStream out) { 71 public static LuanWriter luanWriter(final Writer out) {
83 return writer(luan,luanWriter(out));
84 }
85
86 private static LuanWriter luanWriter(final Writer out) {
87 return new LuanWriter() { 72 return new LuanWriter() {
88 73
89 public Object out() { 74 public Object out() {
90 return out; 75 return out;
91 } 76 }
98 83
99 public void close() throws IOException { 84 public void close() throws IOException {
100 out.close(); 85 out.close();
101 } 86 }
102 }; 87 };
103 }
104
105 public static LuanTable textWriter(LuanState luan,final Writer out) {
106 return writer(luan,luanWriter(out));
107 }
108
109 private static LuanTable writer(LuanState luan,LuanWriter luanWriter) {
110 LuanTable writer = new LuanTable(luan);
111 writer.rawPut( "java", luanWriter.out() );
112 try {
113 writer.rawPut( "write", new LuanJavaFunction(
114 LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter
115 ) );
116 writer.rawPut( "close", new LuanJavaFunction(
117 LuanWriter.class.getMethod( "close" ), luanWriter
118 ) );
119 } catch(NoSuchMethodException e) {
120 throw new RuntimeException(e);
121 }
122 return writer;
123 }
124
125
126 public static LuanTable binaryWriter(LuanState luan,final OutputStream out) {
127 LuanTable writer = new LuanTable(luan);
128 writer.rawPut( "java", out );
129 try {
130 writer.rawPut( "write", new LuanJavaFunction(
131 OutputStream.class.getMethod( "write", new byte[0].getClass() ), out
132 ) );
133 writer.rawPut( "close", new LuanJavaFunction(
134 OutputStream.class.getMethod( "close" ), out
135 ) );
136 } catch(NoSuchMethodException e) {
137 throw new RuntimeException(e);
138 }
139 return writer;
140 } 88 }
141 89
142 static LuanFunction lines(final BufferedReader in) { 90 static LuanFunction lines(final BufferedReader in) {
143 return new LuanFunction() { 91 return new LuanFunction() {
144 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 92 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
263 } 211 }
264 212
265 public void set_charset(String charset) { 213 public void set_charset(String charset) {
266 this.charset = charset; 214 this.charset = charset;
267 } 215 }
268
269 public LuanTable table(LuanState luan) {
270 LuanTable tbl = new LuanTable(luan);
271 try {
272 tbl.rawPut( "java", this );
273 tbl.rawPut( "to_string", new LuanJavaFunction(
274 LuanIn.class.getMethod( "to_string" ), this
275 ) );
276 tbl.rawPut( "to_uri_string", new LuanJavaFunction(
277 LuanIn.class.getMethod( "to_uri_string" ), this
278 ) );
279 tbl.rawPut( "read_text", new LuanJavaFunction(
280 LuanIn.class.getMethod( "read_text", LuanState.class ), this
281 ) );
282 tbl.rawPut( "read_binary", new LuanJavaFunction(
283 LuanIn.class.getMethod( "read_binary", LuanState.class ), this
284 ) );
285 tbl.rawPut( "read_lines", new LuanJavaFunction(
286 LuanIn.class.getMethod( "read_lines", LuanState.class ), this
287 ) );
288 tbl.rawPut( "read_blocks", new LuanJavaFunction(
289 LuanIn.class.getMethod( "read_blocks", LuanState.class, Integer.class ), this
290 ) );
291 tbl.rawPut( "exists", new LuanJavaFunction(
292 LuanIn.class.getMethod( "exists", LuanState.class ), this
293 ) );
294 tbl.rawPut( "checksum", new LuanJavaFunction(
295 LuanIn.class.getMethod( "checksum", LuanState.class ), this
296 ) );
297 tbl.rawPut( "charset", new LuanJavaFunction(
298 LuanIn.class.getMethod( "charset" ), this
299 ) );
300 tbl.rawPut( "set_charset", new LuanJavaFunction(
301 LuanIn.class.getMethod( "set_charset", String.class ), this
302 ) );
303 } catch(NoSuchMethodException e) {
304 throw new RuntimeException(e);
305 }
306 return tbl;
307 }
308 } 216 }
309 217
310 public static final LuanIn defaultStdin = new LuanIn() { 218 public static final LuanIn defaultStdin = new LuanIn() {
311 219
312 @Override public InputStream inputStream(LuanState luan) { 220 @Override public InputStream inputStream(LuanState luan) {
371 } 279 }
372 } 280 }
373 throw new LuanException( "bad argument #1 to 'write' (string or binary or Io.uri expected)" ); 281 throw new LuanException( "bad argument #1 to 'write' (string or binary or Io.uri expected)" );
374 } 282 }
375 283
376 public LuanTable text_writer(LuanState luan) throws IOException { 284 public LuanWriter text_writer() throws IOException {
377 return textWriter(luan,new BufferedWriter(writer())); 285 return luanWriter(new BufferedWriter(writer()));
378 } 286 }
379 287
380 public LuanTable binary_writer(LuanState luan) throws IOException { 288 public OutputStream binary_writer() throws IOException {
381 return binaryWriter(luan,new BufferedOutputStream(outputStream())); 289 return new BufferedOutputStream(outputStream());
382 } 290 }
383 291
384 public void write_text(LuanState luan,Object... args) throws LuanException, IOException { 292 public void write_text(LuanState luan,Object... args) throws LuanException, IOException {
385 LuanWriter luanWriter = luanWriter(new BufferedWriter(writer())); 293 LuanWriter luanWriter = luanWriter(new BufferedWriter(writer()));
386 luanWriter.write(luan,args); 294 luanWriter.write(luan,args);
387 luanWriter.close(); 295 luanWriter.close();
388 } 296 }
389 297 }
390 @Override public LuanTable table(LuanState luan) { 298
391 LuanTable tbl = super.table(luan); 299 public static final LuanIO nullIO = new LuanIO() {
392 try {
393 tbl.rawPut( "write", new LuanJavaFunction(
394 LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this
395 ) );
396 tbl.rawPut( "text_writer", new LuanJavaFunction(
397 LuanIO.class.getMethod( "text_writer", LuanState.class ), this
398 ) );
399 tbl.rawPut( "binary_writer", new LuanJavaFunction(
400 LuanIO.class.getMethod( "binary_writer", LuanState.class ), this
401 ) );
402 tbl.rawPut( "write_text", new LuanJavaFunction(
403 LuanIO.class.getMethod( "write_text", LuanState.class, new Object[0].getClass() ), this
404 ) );
405 } catch(NoSuchMethodException e) {
406 throw new RuntimeException(e);
407 }
408 return tbl;
409 }
410 }
411
412 private static final LuanIO nullIO = new LuanIO() {
413 private final InputStream in = new InputStream() { 300 private final InputStream in = new InputStream() {
414 @Override public int read() { 301 @Override public int read() {
415 return -1; 302 return -1;
416 } 303 }
417 }; 304 };
438 }; 325 };
439 326
440 public static final class LuanString extends LuanIO { 327 public static final class LuanString extends LuanIO {
441 private String s; 328 private String s;
442 329
443 private LuanString(String s) { 330 public LuanString(String s) throws LuanException {
331 Utils.checkNotNull(s);
444 this.s = s; 332 this.s = s;
445 } 333 }
446 334
447 @Override public InputStream inputStream(LuanState luan) { 335 @Override public InputStream inputStream(LuanState luan) {
448 throw new UnsupportedOperationException(); 336 throw new UnsupportedOperationException();
470 358
471 @Override public boolean exists(LuanState luan) { 359 @Override public boolean exists(LuanState luan) {
472 return true; 360 return true;
473 } 361 }
474 362
475 @Override public LuanTable text_writer(LuanState luan) throws IOException { 363 @Override public LuanWriter text_writer() {
476 LuanWriter luanWriter = new LuanWriter() { 364 return new LuanWriter() {
477 private final Writer out = new StringWriter(); 365 private final Writer out = new StringWriter();
478 366
479 public Object out() { 367 public Object out() {
480 return out; 368 return out;
481 } 369 }
488 376
489 public void close() throws IOException { 377 public void close() throws IOException {
490 s = out.toString(); 378 s = out.toString();
491 } 379 }
492 }; 380 };
493 return writer(luan,luanWriter);
494 } 381 }
495 } 382 }
496 383
497 public static final class LuanFile extends LuanIO { 384 public static final class LuanFile extends LuanIO {
498 public final File file; 385 public final File file;
386
387 public LuanFile(LuanState luan,String path) throws LuanException {
388 this(luan,new File(path));
389 }
499 390
500 private LuanFile(LuanState luan,File file) throws LuanException { 391 private LuanFile(LuanState luan,File file) throws LuanException {
501 this(file); 392 this(file);
502 check(luan,"file:"+file.toString()); 393 check(luan,"file:"+file.toString());
503 } 394 }
520 411
521 @Override public String to_uri_string() { 412 @Override public String to_uri_string() {
522 return "file:" + file.toString(); 413 return "file:" + file.toString();
523 } 414 }
524 415
525 public LuanTable child(LuanState luan,String name) throws LuanException { 416 public LuanFile child(LuanState luan,String name) throws LuanException {
526 return new LuanFile(luan,new File(file,name)).table(luan); 417 return new LuanFile(luan,new File(file,name));
527 } 418 }
528 419
529 public LuanTable children(LuanState luan) throws LuanException { 420 public LuanTable children(LuanState luan) throws LuanException {
530 File[] files = file.listFiles(); 421 File[] files = file.listFiles();
531 if( files==null ) 422 if( files==null )
532 return null; 423 return null;
533 LuanTable list = new LuanTable(luan); 424 LuanTable list = new LuanTable(luan);
534 for( File f : files ) { 425 for( File f : files ) {
535 list.rawPut(list.rawLength()+1,new LuanFile(luan,f).table(luan)); 426 list.rawPut(list.rawLength()+1,new LuanFile(luan,f));
536 } 427 }
537 return list; 428 return list;
538 } 429 }
539 430
540 public LuanTable parent(LuanState luan) throws LuanException, IOException { 431 public LuanFile parent(LuanState luan) throws LuanException, IOException {
541 File parent = file.getParentFile(); 432 File parent = file.getParentFile();
542 if( parent==null ) 433 if( parent==null )
543 parent = file.getCanonicalFile().getParentFile(); 434 parent = file.getCanonicalFile().getParentFile();
544 return new LuanFile(luan,parent).table(luan); 435 return new LuanFile(luan,parent);
545 } 436 }
546 437
547 @Override public boolean exists(LuanState luan) { 438 @Override public boolean exists(LuanState luan) {
548 return file.exists(); 439 return file.exists();
549 } 440 }
554 throw new LuanException( "bad argument #1 to 'objToFile' (string or file table expected)" ); 445 throw new LuanException( "bad argument #1 to 'objToFile' (string or file table expected)" );
555 if( !file.renameTo(dest) ) 446 if( !file.renameTo(dest) )
556 throw new LuanException("couldn't rename file "+file+" to "+dest); 447 throw new LuanException("couldn't rename file "+file+" to "+dest);
557 } 448 }
558 449
559 public LuanTable canonical(LuanState luan) throws LuanException, IOException { 450 public LuanFile canonical(LuanState luan) throws LuanException, IOException {
560 return new LuanFile(luan,file.getCanonicalFile()).table(luan); 451 return new LuanFile(luan,file.getCanonicalFile());
561 } 452 }
562 453
563 public LuanTable create_temp_file(LuanState luan,String prefix,String suffix) throws LuanException, IOException { 454 public LuanFile create_temp_file(LuanState luan,String prefix,String suffix) throws LuanException, IOException {
564 File tmp = File.createTempFile(prefix,suffix,file); 455 File tmp = File.createTempFile(prefix,suffix,file);
565 return new LuanFile(luan,tmp).table(luan); 456 return new LuanFile(luan,tmp);
566 } 457 }
567 458
568 public void delete() throws LuanException { 459 public void delete() throws LuanException {
569 if( file.exists() ) 460 if( file.exists() )
570 delete(file); 461 delete(file);
590 481
591 public void set_last_modified(long time) throws LuanException { 482 public void set_last_modified(long time) throws LuanException {
592 if( !file.setLastModified(time) ) 483 if( !file.setLastModified(time) )
593 throw new LuanException("couldn't set_last_modified on "+file); 484 throw new LuanException("couldn't set_last_modified on "+file);
594 } 485 }
595 486 }
596 @Override public LuanTable table(LuanState luan) { 487
597 LuanTable tbl = super.table(luan); 488 public static LuanUrl classpath(LuanState luan,String name) throws LuanException {
598 try {
599 tbl.rawPut( "name", new LuanJavaFunction(
600 File.class.getMethod( "getName" ), file
601 ) );
602 tbl.rawPut( "is_directory", new LuanJavaFunction(
603 File.class.getMethod( "isDirectory" ), file
604 ) );
605 tbl.rawPut( "is_file", new LuanJavaFunction(
606 File.class.getMethod( "isFile" ), file
607 ) );
608 tbl.rawPut( "delete", new LuanJavaFunction(
609 LuanFile.class.getMethod( "delete" ), this
610 ) );
611 tbl.rawPut( "delete_on_exit", new LuanJavaFunction(
612 File.class.getMethod( "deleteOnExit" ), file
613 ) );
614 tbl.rawPut( "mkdir", new LuanJavaFunction(
615 LuanFile.class.getMethod( "mkdir" ), this
616 ) );
617 tbl.rawPut( "last_modified", new LuanJavaFunction(
618 File.class.getMethod( "lastModified" ), file
619 ) );
620 tbl.rawPut( "set_last_modified", new LuanJavaFunction(
621 LuanFile.class.getMethod( "set_last_modified", Long.TYPE ), this
622 ) );
623 tbl.rawPut( "length", new LuanJavaFunction(
624 File.class.getMethod( "length" ), file
625 ) );
626 tbl.rawPut( "child", new LuanJavaFunction(
627 LuanFile.class.getMethod( "child", LuanState.class, String.class ), this
628 ) );
629 tbl.rawPut( "children", new LuanJavaFunction(
630 LuanFile.class.getMethod( "children", LuanState.class ), this
631 ) );
632 tbl.rawPut( "parent", new LuanJavaFunction(
633 LuanFile.class.getMethod( "parent", LuanState.class ), this
634 ) );
635 tbl.rawPut( "rename_to", new LuanJavaFunction(
636 LuanFile.class.getMethod( "rename_to", Object.class ), this
637 ) );
638 tbl.rawPut( "canonical", new LuanJavaFunction(
639 LuanFile.class.getMethod( "canonical", LuanState.class ), this
640 ) );
641 tbl.rawPut( "create_temp_file", new LuanJavaFunction(
642 LuanFile.class.getMethod( "create_temp_file", LuanState.class, String.class, String.class ), this
643 ) );
644 } catch(NoSuchMethodException e) {
645 throw new RuntimeException(e);
646 }
647 return tbl;
648 }
649 }
650
651 public static LuanTable null_(LuanState luan,String ignore) {
652 return nullIO.table(luan);
653 }
654
655 public static LuanTable string(LuanState luan,String s) throws LuanException {
656 Utils.checkNotNull(s);
657 return new LuanString(s).table(luan);
658 }
659
660 public static LuanTable file(LuanState luan,String name) throws LuanException {
661 File file = new File(name);
662 return new LuanFile(luan,file).table(luan);
663 }
664
665 public static LuanTable classpath(LuanState luan,String name) throws LuanException {
666 if( name.contains("//") ) 489 if( name.contains("//") )
667 return null; 490 return null;
668 String path = name; 491 String path = name;
669 check(luan,"classpath:"+path); 492 check(luan,"classpath:"+path);
670 URL url; 493 URL url;
686 } 509 }
687 } 510 }
688 } 511 }
689 } 512 }
690 if( url != null ) 513 if( url != null )
691 return new LuanUrl(url,null).table(luan); 514 return new LuanUrl(url,null);
692 515
693 return null; 516 return null;
694 } 517 }
695 518
696 private static LuanTable url(LuanState luan,String url,LuanTable options) throws IOException, LuanException {
697 return new LuanUrl(new URL(url),options).table(luan);
698 }
699
700 public static LuanTable http(LuanState luan,String path,LuanTable options) throws IOException, LuanException {
701 return url(luan,"http:"+path,options);
702 }
703
704 public static LuanTable https(LuanState luan,String path,LuanTable options) throws IOException, LuanException {
705 return url(luan,"https:"+path,options);
706 }
707
708 public static LuanTable luan(LuanState luan,String path) throws LuanException {
709 return classpath( luan, "luan/modules/" + path );
710 }
711
712 public static LuanTable stdin(LuanState luan) throws LuanException {
713 LuanTable io = (LuanTable)PackageLuan.require(luan,"luan:Io.luan");
714 return (LuanTable)io.get("stdin");
715 }
716
717 public static LuanTable newSchemes(LuanState luan) {
718 LuanTable schemes = new LuanTable(luan);
719 try {
720 schemes.rawPut( "null", new LuanJavaFunction(IoLuan.class.getMethod("null_",LuanState.class,String.class),null) );
721 add( schemes, "string", LuanState.class, String.class );
722 add( schemes, "file", LuanState.class, String.class );
723 add( schemes, "classpath", LuanState.class, String.class );
724 // add( schemes, "socket", String.class );
725 add( schemes, "http", LuanState.class, String.class, LuanTable.class );
726 add( schemes, "https", LuanState.class, String.class, LuanTable.class );
727 add( schemes, "luan", LuanState.class, String.class );
728 add( schemes, "stdin", LuanState.class );
729 add( schemes, "os", LuanState.class, String.class, LuanTable.class );
730 add( schemes, "bash", LuanState.class, String.class, LuanTable.class );
731 } catch(NoSuchMethodException e) {
732 throw new RuntimeException(e);
733 }
734 return schemes;
735 }
736
737 private static LuanTable schemes(LuanState luan) throws LuanException {
738 LuanTable t = (LuanTable)PackageLuan.loaded(luan).rawGet("luan:Io.luan");
739 if( t == null )
740 return newSchemes(luan);
741 t = (LuanTable)t.get("schemes");
742 if( t == null )
743 return newSchemes(luan);
744 return t;
745 }
746
747 public static LuanTable uri(LuanState luan,String name,LuanTable options) throws LuanException {
748 int i = name.indexOf(':');
749 if( i == -1 )
750 throw new LuanException( "invalid Io.uri name '"+name+"', missing scheme" );
751 String scheme = name.substring(0,i);
752 String location = name.substring(i+1);
753 LuanTable schemes = schemes(luan);
754 LuanFunction opener = (LuanFunction)schemes.get(scheme);
755 if( opener == null )
756 throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" );
757 return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
758 }
759 /*
760 public static final class LuanSocket extends LuanIO {
761 public final Socket socket;
762
763 private LuanSocket(String host,int port) throws LuanException {
764 try {
765 this.socket = new Socket(host,port);
766 } catch(IOException e) {
767 throw new LuanException(e.toString());
768 }
769 }
770
771 private LuanSocket(Socket socket) {
772 this.socket = socket;
773 }
774
775 @Override public InputStream inputStream() throws IOException {
776 return socket.getInputStream();
777 }
778
779 @Override OutputStream outputStream() throws IOException {
780 return socket.getOutputStream();
781 }
782
783 @Override public String to_string() {
784 return socket.toString();
785 }
786
787 @Override public String to_uri_string() {
788 throw new UnsupportedOperationException();
789 }
790 }
791
792 public static LuanTable socket(String name) throws LuanException, IOException {
793 int i = name.indexOf(':');
794 if( i == -1 )
795 throw new LuanException( "invalid socket '"+name+"', format is: <host>:<port>" );
796 String host = name.substring(0,i);
797 String portStr = name.substring(i+1);
798 int port = Integer.parseInt(portStr);
799 return new LuanSocket(host,port).table();
800 }
801
802 public static LuanFunction socket_server(int port) throws IOException {
803 final ServerSocket ss = new ServerSocket(port);
804 return new LuanFunction() {
805 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
806 try {
807 if( args.length > 0 ) {
808 if( args.length > 1 || !"close".equals(args[0]) )
809 throw new LuanException( "the only argument allowed is 'close'" );
810 ss.close();
811 return null;
812 }
813 return new LuanSocket(ss.accept()).table();
814 } catch(IOException e) {
815 throw new LuanException(e);
816 }
817 }
818 };
819 }
820 */
821 519
822 public static class BaseOs extends LuanIO { 520 public static class BaseOs extends LuanIO {
823 private final String cmd; 521 private final String cmd;
824 final File dir; 522 final File dir;
825 Process proc; 523 Process proc;
879 @Override public String read_text(LuanState luan) throws IOException, LuanException { 577 @Override public String read_text(LuanState luan) throws IOException, LuanException {
880 String s = super.read_text(luan); 578 String s = super.read_text(luan);
881 wait_for(); 579 wait_for();
882 return s; 580 return s;
883 } 581 }
884
885 @Override public LuanTable table(LuanState luan) {
886 LuanTable tbl = super.table(luan);
887 try {
888 tbl.rawPut( "wait_for", new LuanJavaFunction(
889 BaseOs.class.getMethod( "wait_for" ), this
890 ) );
891 } catch(NoSuchMethodException e) {
892 throw new RuntimeException(e);
893 }
894 return tbl;
895 }
896 } 582 }
897 583
898 public static final class LuanOs extends BaseOs { 584 public static final class LuanOs extends BaseOs {
899 private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException { 585 public LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
900 super(cmd,options); 586 super(cmd,options);
901 check(luan,"os:"+cmd); 587 check(luan,"os:"+cmd);
902 this.proc = Runtime.getRuntime().exec(cmd,null,dir); 588 this.proc = Runtime.getRuntime().exec(cmd,null,dir);
903 } 589 }
904 } 590 }
905 591
906 public static LuanTable os(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
907 return new LuanOs(luan,cmd,options).table(luan);
908 }
909
910 public static final class LuanBash extends BaseOs { 592 public static final class LuanBash extends BaseOs {
911 private LuanBash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException { 593 public LuanBash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
912 super(cmd,options); 594 super(cmd,options);
913 check(luan,"bash:"+cmd); 595 check(luan,"bash:"+cmd);
914 this.proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir); 596 this.proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir);
915 } 597 }
916 } 598 }
917
918 public static LuanTable bash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
919 return new LuanBash(luan,cmd,options).table(luan);
920 }
921
922 599
923 600
924 public static class LuanInput extends LuanIn { 601 public static class LuanInput extends LuanIn {
925 private final InputStream in; 602 private final InputStream in;
926 603
965 } 642 }
966 } 643 }
967 return tbl; 644 return tbl;
968 } 645 }
969 646
970 /*
971 // files maps zip name to uri
972 public static void zip(LuanState luan,String zipUri,LuanTable files) throws LuanException, IOException {
973 Object obj = uri(luan,zipUri,null).rawGet("java");
974 if( !(obj instanceof LuanIO) )
975 throw new LuanException("invalid uri for zip");
976 LuanIO zipIo = (LuanIO)obj;
977 ZipOutputStream out = new ZipOutputStream(zipIo.outputStream());
978 for( Map.Entry<Object,Object> entry : files.iterable(luan) ) {
979 obj = entry.getKey();
980 if( !(obj instanceof String) )
981 throw new LuanException("zip file table keys must be strings");
982 String fileName = (String)obj;
983 obj = entry.getValue();
984 if( !(obj instanceof String) )
985 throw new LuanException("zip file table values must be strings");
986 String uriStr = (String)obj;
987 out.putNextEntry(new ZipEntry(fileName));
988 obj = uri(luan,uriStr,null).rawGet("java");
989 if( !(obj instanceof LuanIn) )
990 throw new LuanException("invalid uri for zip");
991 LuanIn zipIn = (LuanIn)obj;
992 InputStream in = zipIn.inputStream();
993 Utils.copyAll(in,out);
994 in.close();
995 out.closeEntry();
996 }
997 out.close();
998 }
999 */
1000 647
1001 // security 648 // security
1002 649
1003 public static void unrestricted(LuanState luan) throws LuanException { 650 public static void unrestricted(LuanState luan) throws LuanException {
1004 JavaLuan.check(luan); 651 JavaLuan.check(luan);