#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" `{
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)
+ var arguments = self.arguments
if arguments != null then
for a in arguments do
args.add('\0')
- #a.output_class_name
args.append(a)
end
l += arguments.length
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];
}
return result;
+#endif
`}
end
# ~~~
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
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
+
+ # The pid of the program
+ 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
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) {
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