rename `NativeString` to `CString`
[nit.git] / lib / core / exec.nit
index 6c03c7f..97ee3fa 100644 (file)
@@ -23,8 +23,10 @@ in "C" `{
        #include <errno.h>
        #include <stdio.h>
        #include <unistd.h>
-       #include <sys/wait.h>
        #include <signal.h>
+#ifndef _WIN32
+       #include <sys/wait.h>
+#endif
 `}
 
 in "C Header" `{
@@ -59,42 +61,44 @@ class Process
        end
 
        # The status once finished
+       #
+       # Require: `is_finished`
        fun status: Int
        do
                assert is_finished
                return data.status
        end
 
-       # The executable run
-       # Is a filepath, or a executable found in PATH
-       var command: String
+       # The target executable
+       # Either a file path or the name of an executable available in PATH.
+       var command: Text
 
        # 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]
+       var arguments: nullable Array[Text]
 
        # Launch a command with some arguments
-       init(command: String, arguments: String...) is old_style_init do
+       init(command: Text, arguments: Text...) is old_style_init do
                self.command = command
                self.arguments = arguments
                execute
        end
 
        # Launch a simple command with arguments passed as an array
-       init from_a(command: String, arguments: nullable Array[String])
+       init from_a(command: Text, arguments: nullable Array[Text])
        do
                self.command = command
                self.arguments = arguments
                execute
        end
 
-       # flags used internally to know whith pipe to open
+       # Flags used internally to know which pipe to open
        private fun pipeflags: Int do return 0
 
        # Internal code to handle execution
        protected fun execute
        do
-               # The pass the arguments as a big C string where elements are separated with '\0'
+               # 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)
@@ -102,7 +106,6 @@ class Process
                if arguments != null then
                        for a in arguments do
                                args.add('\0')
-                               #a.output_class_name
                                args.append(a)
                        end
                        l += arguments.length
@@ -111,7 +114,11 @@ class Process
        end
 
        private var data: NativeProcess
-       private fun basic_exec_execute(prog, args: NativeString, argc: Int, pipeflag: Int): NativeProcess `{
+       private fun basic_exec_execute(prog, args: CString, argc: Int, pipeflag: Int): NativeProcess `{
+#ifdef _WIN32
+               // FIXME use a higher level abstraction to support WIN32
+               return -1;
+#else
                se_exec_data_t* result = NULL;
                int id;
                int in_fd[2];
@@ -206,6 +213,7 @@ class Process
                }
 
                return result;
+#endif
        `}
 end
 
@@ -292,7 +300,7 @@ class ProcessDuplex
        # ~~~
        fun write_and_read(input: Text): String
        do
-               var read = new Buffer #new Array[String]
+               var read = new Buffer
 
                # Main loop, read and write line by line
                var prev = 0
@@ -321,7 +329,7 @@ end
 
 redef class Sys
        # Execute a shell command and return its error code
-       fun system(command: String): Int
+       fun system(command: Text): Int
        do
                return command.to_cstring.system
        end
@@ -330,16 +338,18 @@ redef class Sys
        fun pid: Int `{ return getpid(); `}
 end
 
-redef class NativeString
+redef class CString
        # Execute self as a shell command.
        #
        # See the posix function system(3).
        fun system: Int `{
                int status = system(self);
+#ifndef _WIN32
                if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) {
                        // system exited on SIGINT: in my opinion the user wants the main to be discontinued
                        kill(getpid(), SIGINT);
                }
+#endif
                return status;
        `}
 end
@@ -353,6 +363,10 @@ private extern class NativeProcess `{ se_exec_data_t* `}
        fun err_fd: Int `{ return self->err_fd; `}
 
        fun is_finished: Bool `{
+#ifdef _WIN32
+               // FIXME use a higher level abstraction to support WIN32
+               return 0;
+#else
                int result = (int)0;
                int status;
                if (self->running) {
@@ -368,14 +382,18 @@ private extern class NativeProcess `{ se_exec_data_t* `}
                        result = (int)1;
                }
                return result;
+#endif
        `}
 
        fun wait `{
+#ifndef _WIN32
+               // FIXME use a higher level abstraction to support WIN32
                int status;
                if (self->running) {
                        waitpid(self->id, &status, 0);
                        self->status = WEXITSTATUS(status);
                        self->running = 0;
                }
+#endif
        `}
 end