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 if arguments
!= null then
102 for a
in arguments
do
107 l
+= arguments
.length
109 data
= basic_exec_execute
(command
.to_cstring
, args
.to_s
.to_cstring
, l
, pipeflags
)
112 private var data
: NativeProcess
113 private fun basic_exec_execute
(prog
, args
: NativeString, argc
: Int, pipeflag
: Int): NativeProcess `{
114 se_exec_data_t* result = NULL;
120 int res = pipe(in_fd);
126 int res = pipe(out_fd);
132 int res = pipe(err_fd);
141 char **arg = malloc(sizeof(char*) * (argc+1));
146 for(i=0; i<argc; i++)
182 result = (se_exec_data_t*)malloc(sizeof(se_exec_data_t));
187 result->in_fd = in_fd[1];
194 result->out_fd = out_fd[0];
201 result->err_fd = err_fd[0];
211 # `Process` on which the `stdout` is readable like a `Reader`
216 # File Descriptor used for the input.
217 var stream_in
: FileReader is noinit
219 redef fun close
do stream_in
.close
221 redef fun read_char
do return stream_in
.read_char
223 redef fun read_byte
do return stream_in
.read_byte
225 redef fun eof
do return stream_in
.eof
227 redef fun pipeflags
do return 2
232 stream_in
= new FileReader.from_fd
(data
.out_fd
)
236 # `Process` on which `stdin` is writable like a `Writer`
241 # File Descriptor used for the output.
242 var stream_out
: Writer is noinit
244 redef fun close
do stream_out
.close
246 redef fun is_writable
do return stream_out
.is_writable
248 redef fun write
(s
) do stream_out
.write
(s
)
250 redef fun pipeflags
do return 1
255 var out
= new FileWriter.from_fd
(data
.in_fd
)
256 out
.set_buffering_mode
(0, sys
.buffer_mode_none
)
261 # `Process` on which stdout can be read and stdin can be written to like a `Duplex`
273 redef fun pipeflags
do return 3
275 redef fun execute
do super
277 # Write `input` to process and return its output
279 # Writing and reading are processed line by line,
280 # reading only when something is available.
283 # var proc = new ProcessDuplex("tr", "[:lower:]", "[:upper:]")
284 # assert proc.write_and_read("""
292 fun write_and_read
(input
: Text): String
294 var read
= new Buffer #new Array[String]
296 # Main loop, read and write line by line
298 for delimiter
in input
.search_all
('\n') do
299 write input
.substring
(prev
, delimiter
.after-prev
)
300 prev
= delimiter
.after
302 while stream_in
.poll_in
do
303 read
.append stream_in
.read_line
307 # Write the last line
308 write input
.substring_from
(prev
)
311 # Read the rest, may be everything for some programs
312 read
.append stream_in
.read_all
322 # Execute a shell command and return its error code
323 fun system
(command
: String): Int
325 return command
.to_cstring
.system
328 # The pid of the program
329 fun pid
: Int `{ return getpid(); `}
332 redef class NativeString
333 # Execute self as a shell command.
335 # See the posix function system(3).
337 int status
= system
(self);
338 if (WIFSIGNALED(status
) && WTERMSIG(status
) == SIGINT) {
339 // system exited on
SIGINT: in my opinion the user wants the main to be discontinued
340 kill
(getpid
(), SIGINT);
346 private extern class NativeProcess `{ se_exec_data_t* `}
348 fun id
: Int `{ return self->id; `}
349 fun status: Int `{ return self->status; `}
350 fun in_fd
: Int `{ return self->in_fd; `}
351 fun out_fd: Int `{ return self->out_fd; `}
352 fun err_fd
: Int `{ return self->err_fd; `}
354 fun is_finished: Bool `{
358 int id
= waitpid
(self-
>id
, &status
, WNOHANG);
360 /* child
is finished
*/
361 result
= (int
)(id
== self-
>id
);
362 self-
>status
= WEXITSTATUS(status
);
375 waitpid
(self-
>id
, &status
, 0);
376 self-
>status
= WEXITSTATUS(status
);