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 # This module handle simple system calls
15 # Standard input and output can be handleb trougth streams.
20 # Simple sub-processus
23 # The pid of the processus
24 fun id
: Int do return _data
.id
26 # Is the processus finished?
27 fun is_finished
: Bool do return _data
.is_finished
29 # wait the terminaison of the process
36 # The status once finished
43 # launch a command with some arguments
44 init(command
: String, arguments
: String...)
46 execute
(command
, arguments
, 0)
49 # launch a simple command without arguments
50 init init_
(command
: String)
52 execute
(command
, null, 0)
55 # Internal code to handle execusion
56 protected init execute
(command
: String, arguments
: nullable Array[String], pipeflags
: Int)
59 var l
= 1 # Number of elements in args
61 if arguments
!= null then
68 _data
= basic_exec_execute
(command
.to_cstring
, args
.to_s
.to_cstring
, l
, pipeflags
)
71 var _data
: NativeProcess
72 private fun basic_exec_execute
(p
: NativeString, av
: NativeString, ac
: Int, pf
: Int): NativeProcess is extern "exec_Process_Process_basic_exec_execute_4"
75 # stdout of the processus is readable
81 redef fun close
do _in
.close
83 redef fun read_char
do return _in
.read_char
85 redef fun eof
do return _in
.eof
87 init(command
: String, arguments
: String...)
89 execute
(command
, arguments
, 2)
90 _in
= new FDIStream(_data
.out_fd
)
93 init init_
(command
: String)
95 execute
(command
, null, 2)
96 _in
= new FDIStream(_data
.out_fd
)
100 # stdin of the processus is writable
106 redef fun close
do _out
.close
108 redef fun is_writable
do return _out
.is_writable
110 redef fun write
(s
) do _out
.write
(s
)
112 init(command
: String, arguments
: String...)
114 execute
(command
, arguments
, 1)
115 _out
= new FDOStream(_data
.in_fd
)
118 init init_
(command
: String)
120 execute
(command
, null, 1)
121 _out
= new FDOStream(_data
.in_fd
)
125 # stdin and stdout are both accessible
137 init(command
: String, arguments
: String...)
139 execute
(command
, arguments
, 3)
140 _in
= new FDIStream(_data
.out_fd
)
141 _out
= new FDOStream(_data
.in_fd
)
144 init init_
(command
: String)
146 execute
(command
, null, 3)
147 _in
= new FDIStream(_data
.out_fd
)
148 _out
= new FDOStream(_data
.in_fd
)
153 # Execute a shell command and return it's error code
154 fun system
(command
: String): Int
156 return command
.to_cstring
.system
160 redef class NativeString
161 fun system
: Int is extern "string_NativeString_NativeString_system_0"
164 private universal NativeProcess
166 fun id
: Int is extern "exec_NativeProcess_NativeProcess_id_0"
167 fun is_finished
: Bool is extern "exec_NativeProcess_NativeProcess_is_finished_0"
168 fun status
: Int is extern "exec_NativeProcess_NativeProcess_status_0"
169 fun wait
is extern "exec_NativeProcess_NativeProcess_wait_0"
171 fun in_fd
: Int is extern "exec_NativeProcess_NativeProcess_in_fd_0"
172 fun out_fd
: Int is extern "exec_NativeProcess_NativeProcess_out_fd_0"
173 fun err_fd
: Int is extern "exec_NativeProcess_NativeProcess_err_fd_0"