stdlib/strings: Moved Buffer to FlatBuffer, Buffer is now abstract.
[nit.git] / lib / standard / exec.nit
index 72dd195..166cef4 100644 (file)
@@ -5,63 +5,61 @@
 #
 # This file is free software, which comes along with NIT.  This software is
 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
 # PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
 # is kept unaltered, and a notification of the changes is added.
 # You  are  allowed  to  redistribute it and sell it, alone or is a part of
 # another product.
 
-# This module handle simple system calls
-# Standard input and output can be handleb trougth streams.
-package exec 
+# Invocation and management of operating system sub-processes.
+# Standard input and output can be handled through streams.
+module exec
 
 import stream
 
-# Simple sub-processus
+# Simple sub-process
 class Process
+       # The pid of the process
+       fun id: Int do return data.id
 
-       # The pid of the processus
-       meth id: Int do return _data.id
+       # Is the process finished?
+       fun is_finished: Bool do return data.is_finished
 
-       # Is the processus finished?
-       meth is_finished: Bool do return _data.is_finished
-
-       # wait the terminaison of the process
-       meth wait
+       # Wait the termination of the process
+       fun wait
        do
-               _data.wait
+               data.wait
                assert is_finished
        end
-       
+
        # The status once finished
-       meth status: Int
+       fun status: Int
        do
                assert is_finished
-               return _data.status
+               return data.status
        end
 
-       # send a signal to the process
-       meth kill(signal: Int) do _data.kill(signal)
-
-       # send the TERM (15) signal
-       meth term do kill(15)
-
-       # launch a command with some arguments
+       # Launch a command with some arguments
        init(command: String, arguments: String...)
        do
                execute(command, arguments, 0)
        end
 
-       # launch a simple command without arguments
+       # Launch a simple command without arguments
        init init_(command: String)
        do
                execute(command, null, 0)
        end
 
-       # Internal code to handle execusion
-       protected init execute(command: String, arguments: Array[String], pipeflags: Int)
+       init from_a(command: String, arguments: Array[String])
+       do
+               execute(command, arguments, 0)
+       end
+
+       # Internal code to handle execution
+       protected init execute(command: String, arguments: nullable Array[String], pipeflags: Int)
        do
-               var args = new Buffer
+               var args = new FlatBuffer
                var l = 1 # Number of elements in args
                args.append(command)
                if arguments != null then
@@ -71,112 +69,129 @@ class Process
                        end
                        l += arguments.length
                end
-               _data = basic_exec_execute(command.to_cstring, args.to_s.to_cstring, l, pipeflags)
+               data = basic_exec_execute(command.to_cstring, args.to_s.to_cstring, l, pipeflags)
        end
-       
-       attr _data: NativeProcess
-       private meth basic_exec_execute(p: NativeString, av: NativeString, ac: Int, pf: Int): NativeProcess is extern "exec_Process_Process_basic_exec_execute_4"
+
+       private var data: NativeProcess
+       private fun basic_exec_execute(p: NativeString, av: NativeString, ac: Int, pf: Int): NativeProcess is extern "exec_Process_Process_basic_exec_execute_4"
 end
 
-# stdout of the processus is readable
+# stdout of the process is readable
 class IProcess
-special Process
-special IStream
-       attr _in: FDIStream
-       
-       redef meth close do _in.close
-       
-       redef meth read_char do return _in.read_char
+       super Process
+       super IStream
+       var stream_in: FDIStream
+
+       redef fun close do stream_in.close
+
+       redef fun read_char do return stream_in.read_char
 
-       redef meth eof do return _in.eof
+       redef fun eof do return stream_in.eof
 
        init(command: String, arguments: String...)
        do
                execute(command, arguments, 2)
-               _in = new FDIStream(_data.out_fd)
+               stream_in = new FDIStream(data.out_fd)
        end
-       
+
        init init_(command: String)
        do
                execute(command, null, 2)
-               _in = new FDIStream(_data.out_fd)
+               stream_in = new FDIStream(data.out_fd)
+       end
+
+       init from_a(command: String, arguments: Array[String])
+       do
+               execute(command, arguments, 2)
+               stream_in = new FDIStream(data.out_fd)
        end
 end
 
-# stdin of the processus is writable
+# stdin of the process is writable
 class OProcess
-special Process
-special OStream
-       attr _out: OStream
+       super Process
+       super OStream
+       var stream_out: OStream
 
-       redef meth close do _out.close
+       redef fun close do stream_out.close
 
-       redef meth is_writable do return _out.is_writable
+       redef fun is_writable do return stream_out.is_writable
+
+       redef fun write(s) do stream_out.write(s)
 
-       redef meth write(s) do _out.write(s)
-       
        init(command: String, arguments: String...)
        do
                execute(command, arguments, 1)
-               _out = new FDOStream(_data.in_fd)
+               stream_out = new FDOStream(data.in_fd)
        end
-       
+
        init init_(command: String)
        do
                execute(command, null, 1)
-               _out = new FDOStream(_data.in_fd)
+               stream_out = new FDOStream(data.in_fd)
+       end
+
+       init from_a(command: String, arguments: Array[String])
+       do
+               execute(command, arguments, 1)
+               stream_out = new FDOStream(data.in_fd)
        end
 end
 
 # stdin and stdout are both accessible
 class IOProcess
-special IProcess
-special OProcess
-special IOStream
+       super IProcess
+       super OProcess
+       super IOStream
 
-       redef meth close
+       redef fun close
        do
-               _in.close
-               _out.close
+               stream_in.close
+               stream_out.close
        end
 
        init(command: String, arguments: String...)
        do
                execute(command, arguments, 3)
-               _in = new FDIStream(_data.out_fd)
-               _out = new FDOStream(_data.in_fd)
+               stream_in = new FDIStream(data.out_fd)
+               stream_out = new FDOStream(data.in_fd)
        end
-       
+
        init init_(command: String)
        do
                execute(command, null, 3)
-               _in = new FDIStream(_data.out_fd)
-               _out = new FDOStream(_data.in_fd)
+               stream_in = new FDIStream(data.out_fd)
+               stream_out = new FDOStream(data.in_fd)
+       end
+
+       init from_a(command: String, arguments: Array[String])
+       do
+               execute(command, arguments, 3)
+               stream_in = new FDIStream(data.out_fd)
+               stream_out = new FDOStream(data.in_fd)
        end
 end
 
 redef class Sys
-       # Execute a shell command and return it's error code
-       meth system(command: String): Int
+       # Execute a shell command and return its error code
+       fun system(command: String): Int
        do
-               return command.to_cstring.system        
+               return command.to_cstring.system
        end
 end
 
 redef class NativeString
-       meth system: Int is extern "string_NativeString_NativeString_system_0"
+       fun system: Int is extern "string_NativeString_NativeString_system_0"
 end
 
-private universal NativeProcess
-special Pointer
-       meth id: Int is extern "exec_NativeProcess_NativeProcess_id_0"
-       meth is_finished: Bool is extern "exec_NativeProcess_NativeProcess_is_finished_0"
-       meth status: Int is extern "exec_NativeProcess_NativeProcess_status_0"
-       meth wait is extern "exec_NativeProcess_NativeProcess_wait_0"
-       meth kill(s: Int) is extern "exec_NativeProcess_NativeProcess_kill_1"
-
-       meth in_fd: Int is extern "exec_NativeProcess_NativeProcess_in_fd_0"
-       meth out_fd: Int is extern "exec_NativeProcess_NativeProcess_out_fd_0"
-       meth err_fd: Int is extern "exec_NativeProcess_NativeProcess_err_fd_0"
+private extern NativeProcess
+       fun id: Int is extern "exec_NativeProcess_NativeProcess_id_0"
+       fun is_finished: Bool is extern "exec_NativeProcess_NativeProcess_is_finished_0"
+       fun status: Int is extern "exec_NativeProcess_NativeProcess_status_0"
+       fun wait is extern "exec_NativeProcess_NativeProcess_wait_0"
+
+       fun in_fd: Int is extern "exec_NativeProcess_NativeProcess_in_fd_0"
+       fun out_fd: Int is extern "exec_NativeProcess_NativeProcess_out_fd_0"
+       fun err_fd: Int is extern "exec_NativeProcess_NativeProcess_err_fd_0"
 end