1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
14 # Invocation and management of operating system sub-processes.
15 # Standard input and output can be handled through streams.
31 #include <sys/types.h>
33 // FIXME this should be in the "C" block when bug on module blocks is fixed
34 // or, even better, replace the C structure by a Nit object.
35 typedef struct se_exec_data se_exec_data_t;
48 # The pid of the process
49 fun id
: Int do return data
.id
51 # Is the process finished?
52 fun is_finished
: Bool do return data
.is_finished
54 # Wait the termination of the process
61 # The status once finished
63 # Require: `is_finished`
70 # The target executable
71 # Either a file path or the name of an executable available in PATH.
74 # The arguments of the command
75 # Starts with the first real arguments---ie. does not include the progname (`argv[0]`, in C)
76 var arguments
: nullable Array[Text]
78 # Launch a command with some arguments
79 init(command
: Text, arguments
: Text...) is old_style_init
do
80 self.command
= command
81 self.arguments
= arguments
85 # Launch a simple command with arguments passed as an array
86 init from_a
(command
: Text, arguments
: nullable Array[Text])
88 self.command
= command
89 self.arguments
= arguments
93 # Flags used internally to know which pipe to open
94 private fun pipeflags
: Int do return 0
96 # Internal code to handle execution
99 # Pass the arguments as a big C string where elements are separated with '\0'
100 var args
= new FlatBuffer
101 var l
= 1 # Number of elements in args
103 var arguments
= self.arguments
104 if arguments
!= null then
105 for a
in arguments
do
109 l
+= arguments
.length
111 data
= basic_exec_execute
(command
.to_cstring
, args
.to_s
.to_cstring
, l
, pipeflags
)
114 private var data
: NativeProcess
115 private fun basic_exec_execute
(prog
, args
: NativeString, argc
: Int, pipeflag
: Int): NativeProcess `{
116 se_exec_data_t* result = NULL;
122 int res = pipe(in_fd);
128 int res = pipe(out_fd);
134 int res = pipe(err_fd);
143 char **arg = malloc(sizeof(char*) * (argc+1));
148 for(i=0; i<argc; i++)
184 result = (se_exec_data_t*)malloc(sizeof(se_exec_data_t));
189 result->in_fd = in_fd[1];
196 result->out_fd = out_fd[0];
203 result->err_fd = err_fd[0];
213 # `Process` on which the `stdout` is readable like a `Reader`
218 # File Descriptor used for the input.
219 var stream_in
: FileReader is noinit
221 redef fun close
do stream_in
.close
223 redef fun read_char
do return stream_in
.read_char
225 redef fun read_byte
do return stream_in
.read_byte
227 redef fun eof
do return stream_in
.eof
229 redef fun pipeflags
do return 2
234 stream_in
= new FileReader.from_fd
(data
.out_fd
)
238 # `Process` on which `stdin` is writable like a `Writer`
243 # File Descriptor used for the output.
244 var stream_out
: Writer is noinit
246 redef fun close
do stream_out
.close
248 redef fun is_writable
do return stream_out
.is_writable
250 redef fun write
(s
) do stream_out
.write
(s
)
252 redef fun pipeflags
do return 1
257 var out
= new FileWriter.from_fd
(data
.in_fd
)
258 out
.set_buffering_mode
(0, sys
.buffer_mode_none
)
263 # `Process` on which stdout can be read and stdin can be written to like a `Duplex`
275 redef fun pipeflags
do return 3
277 redef fun execute
do super
279 # Write `input` to process and return its output
281 # Writing and reading are processed line by line,
282 # reading only when something is available.
285 # var proc = new ProcessDuplex("tr", "[:lower:]", "[:upper:]")
286 # assert proc.write_and_read("""
294 fun write_and_read
(input
: Text): String
296 var read
= new Buffer
298 # Main loop, read and write line by line
300 for delimiter
in input
.search_all
('\n') do
301 write input
.substring
(prev
, delimiter
.after-prev
)
302 prev
= delimiter
.after
304 while stream_in
.poll_in
do
305 read
.append stream_in
.read_line
309 # Write the last line
310 write input
.substring_from
(prev
)
313 # Read the rest, may be everything for some programs
314 read
.append stream_in
.read_all
324 # Execute a shell command and return its error code
325 fun system
(command
: Text): Int
327 return command
.to_cstring
.system
330 # The pid of the program
331 fun pid
: Int `{ return getpid(); `}
334 redef class NativeString
335 # Execute self as a shell command.
337 # See the posix function system(3).
339 int status
= system
(self);
341 if (WIFSIGNALED(status
) && WTERMSIG(status
) == SIGINT) {
342 // system exited on
SIGINT: in my opinion the user wants the main to be discontinued
343 kill
(getpid
(), SIGINT);
350 private extern class NativeProcess `{ se_exec_data_t* `}
352 fun id
: Int `{ return self->id; `}
353 fun status: Int `{ return self->status; `}
354 fun in_fd
: Int `{ return self->in_fd; `}
355 fun out_fd: Int `{ return self->out_fd; `}
356 fun err_fd
: Int `{ return self->err_fd; `}
358 fun is_finished: Bool `{
362 int id
= waitpid
(self-
>id
, &status
, WNOHANG);
364 /* child
is finished
*/
365 result
= (int
)(id
== self-
>id
);
366 self-
>status
= WEXITSTATUS(status
);
379 waitpid
(self-
>id
, &status
, 0);
380 self-
>status
= WEXITSTATUS(status
);