Merge: Newstreams
[nit.git] / lib / standard / exec.nit
index 166cef4..79f9bfd 100644 (file)
@@ -15,7 +15,7 @@
 # Standard input and output can be handled through streams.
 module exec
 
-import stream
+import file
 
 # Simple sub-process
 class Process
@@ -39,32 +39,43 @@ class Process
                return data.status
        end
 
+       # The executable run
+       # Is a filepath, or a executable found in PATH
+       var command: String
+
+       # The arguments of the command
+       # Starts with the first real arguments---ie. does not include the progname (`argv[0]`, in C)
+       var arguments: nullable Array[String]
+
        # Launch a command with some arguments
-       init(command: String, arguments: String...)
-       do
-               execute(command, arguments, 0)
+       init(command: String, arguments: String...) is old_style_init do
+               self.command = command
+               self.arguments = arguments
+               execute
        end
 
-       # Launch a simple command without arguments
-       init init_(command: String)
+       # Launch a simple command with arguments passed as an array
+       init from_a(command: String, arguments: nullable Array[String])
        do
-               execute(command, null, 0)
+               self.command = command
+               self.arguments = arguments
+               execute
        end
 
-       init from_a(command: String, arguments: Array[String])
-       do
-               execute(command, arguments, 0)
-       end
+       # flags used internally to know whith pipe to open
+       private fun pipeflags: Int do return 0
 
        # Internal code to handle execution
-       protected init execute(command: String, arguments: nullable Array[String], pipeflags: Int)
+       protected fun execute
        do
+               # The pass the arguments as a big C string where elements are separated with '\0'
                var args = new FlatBuffer
                var l = 1 # Number of elements in args
                args.append(command)
                if arguments != null then
                        for a in arguments do
                                args.add('\0')
+                               #a.output_class_name
                                args.append(a)
                        end
                        l += arguments.length
@@ -80,7 +91,9 @@ end
 class IProcess
        super Process
        super IStream
-       var stream_in: FDIStream
+
+       # File Descriptor used for the input.
+       var stream_in: IFStream is noinit
 
        redef fun close do stream_in.close
 
@@ -88,22 +101,12 @@ class IProcess
 
        redef fun eof do return stream_in.eof
 
-       init(command: String, arguments: String...)
-       do
-               execute(command, arguments, 2)
-               stream_in = new FDIStream(data.out_fd)
-       end
-
-       init init_(command: String)
-       do
-               execute(command, null, 2)
-               stream_in = new FDIStream(data.out_fd)
-       end
+       redef fun pipeflags do return 2
 
-       init from_a(command: String, arguments: Array[String])
+       redef fun execute
        do
-               execute(command, arguments, 2)
-               stream_in = new FDIStream(data.out_fd)
+               super
+               stream_in = new IFStream.from_fd(data.out_fd)
        end
 end
 
@@ -111,7 +114,9 @@ end
 class OProcess
        super Process
        super OStream
-       var stream_out: OStream
+
+       # File Descriptor used for the output.
+       var stream_out: OStream is noinit
 
        redef fun close do stream_out.close
 
@@ -119,22 +124,12 @@ class OProcess
 
        redef fun write(s) do stream_out.write(s)
 
-       init(command: String, arguments: String...)
-       do
-               execute(command, arguments, 1)
-               stream_out = new FDOStream(data.in_fd)
-       end
-
-       init init_(command: String)
-       do
-               execute(command, null, 1)
-               stream_out = new FDOStream(data.in_fd)
-       end
+       redef fun pipeflags do return 1
 
-       init from_a(command: String, arguments: Array[String])
+       redef fun execute
        do
-               execute(command, arguments, 1)
-               stream_out = new FDOStream(data.in_fd)
+               super
+               stream_out = new OFStream.from_fd(data.in_fd)
        end
 end
 
@@ -150,25 +145,11 @@ class IOProcess
                stream_out.close
        end
 
-       init(command: String, arguments: String...)
-       do
-               execute(command, arguments, 3)
-               stream_in = new FDIStream(data.out_fd)
-               stream_out = new FDOStream(data.in_fd)
-       end
-
-       init init_(command: String)
-       do
-               execute(command, null, 3)
-               stream_in = new FDIStream(data.out_fd)
-               stream_out = new FDOStream(data.in_fd)
-       end
+       redef fun pipeflags do return 3
 
-       init from_a(command: String, arguments: Array[String])
+       redef fun execute
        do
-               execute(command, arguments, 3)
-               stream_in = new FDIStream(data.out_fd)
-               stream_out = new FDOStream(data.in_fd)
+               super
        end
 end
 
@@ -181,10 +162,13 @@ redef class Sys
 end
 
 redef class NativeString
+       # Execute self as a shell command.
+       #
+       # See the posix function system(3).
        fun system: Int is extern "string_NativeString_NativeString_system_0"
 end
 
-private extern NativeProcess
+private extern class 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"