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
69 # Is a filepath, or a executable found in PATH
72 # The arguments of the command
73 # Starts with the first real arguments---ie. does not include the progname (`argv[0]`, in C)
74 var arguments
: nullable Array[String]
76 # Launch a command with some arguments
77 init(command
: String, arguments
: String...) is old_style_init
do
78 self.command
= command
79 self.arguments
= arguments
83 # Launch a simple command with arguments passed as an array
84 init from_a
(command
: String, arguments
: nullable Array[String])
86 self.command
= command
87 self.arguments
= arguments
91 # flags used internally to know whith pipe to open
92 private fun pipeflags
: Int do return 0
94 # Internal code to handle execution
97 # The pass the arguments as a big C string where elements are separated with '\0'
98 var args
= new FlatBuffer
99 var l
= 1 # Number of elements in args
101 var arguments
= self.arguments
102 if arguments
!= null then
103 for a
in arguments
do
108 l
+= arguments
.length
110 data
= basic_exec_execute
(command
.to_cstring
, args
.to_s
.to_cstring
, l
, pipeflags
)
113 private var data
: NativeProcess
114 private fun basic_exec_execute
(prog
, args
: NativeString, argc
: Int, pipeflag
: Int): NativeProcess `{
115 se_exec_data_t* result = NULL;
121 int res = pipe(in_fd);
127 int res = pipe(out_fd);
133 int res = pipe(err_fd);
142 char **arg = malloc(sizeof(char*) * (argc+1));
147 for(i=0; i<argc; i++)
183 result = (se_exec_data_t*)malloc(sizeof(se_exec_data_t));
188 result->in_fd = in_fd[1];
195 result->out_fd = out_fd[0];
202 result->err_fd = err_fd[0];
212 # `Process` on which the `stdout` is readable like a `Reader`
217 # File Descriptor used for the input.
218 var stream_in
: FileReader is noinit
220 redef fun close
do stream_in
.close
222 redef fun read_char
do return stream_in
.read_char
224 redef fun read_byte
do return stream_in
.read_byte
226 redef fun eof
do return stream_in
.eof
228 redef fun pipeflags
do return 2
233 stream_in
= new FileReader.from_fd
(data
.out_fd
)
237 # `Process` on which `stdin` is writable like a `Writer`
242 # File Descriptor used for the output.
243 var stream_out
: Writer is noinit
245 redef fun close
do stream_out
.close
247 redef fun is_writable
do return stream_out
.is_writable
249 redef fun write
(s
) do stream_out
.write
(s
)
251 redef fun pipeflags
do return 1
256 var out
= new FileWriter.from_fd
(data
.in_fd
)
257 out
.set_buffering_mode
(0, sys
.buffer_mode_none
)
262 # `Process` on which stdout can be read and stdin can be written to like a `Duplex`
274 redef fun pipeflags
do return 3
276 redef fun execute
do super
278 # Write `input` to process and return its output
280 # Writing and reading are processed line by line,
281 # reading only when something is available.
284 # var proc = new ProcessDuplex("tr", "[:lower:]", "[:upper:]")
285 # assert proc.write_and_read("""
293 fun write_and_read
(input
: Text): String
295 var read
= new Buffer #new Array[String]
297 # Main loop, read and write line by line
299 for delimiter
in input
.search_all
('\n') do
300 write input
.substring
(prev
, delimiter
.after-prev
)
301 prev
= delimiter
.after
303 while stream_in
.poll_in
do
304 read
.append stream_in
.read_line
308 # Write the last line
309 write input
.substring_from
(prev
)
312 # Read the rest, may be everything for some programs
313 read
.append stream_in
.read_all
323 # Execute a shell command and return its error code
324 fun system
(command
: String): Int
326 return command
.to_cstring
.system
329 # The pid of the program
330 fun pid
: Int `{ return getpid(); `}
333 redef class NativeString
334 # Execute self as a shell command.
336 # See the posix function system(3).
338 int status
= system
(self);
339 if (WIFSIGNALED(status
) && WTERMSIG(status
) == SIGINT) {
340 // system exited on
SIGINT: in my opinion the user wants the main to be discontinued
341 kill
(getpid
(), SIGINT);
347 private extern class NativeProcess `{ se_exec_data_t* `}
349 fun id
: Int `{ return self->id; `}
350 fun status: Int `{ return self->status; `}
351 fun in_fd
: Int `{ return self->in_fd; `}
352 fun out_fd: Int `{ return self->out_fd; `}
353 fun err_fd
: Int `{ return self->err_fd; `}
355 fun is_finished: Bool `{
359 int id
= waitpid
(self-
>id
, &status
, WNOHANG);
361 /* child
is finished
*/
362 result
= (int
)(id
== self-
>id
);
363 self-
>status
= WEXITSTATUS(status
);
376 waitpid
(self-
>id
, &status
, 0);
377 self-
>status
= WEXITSTATUS(status
);