benchmarks: Reunited common functions for benches in a new shell file.
[nit.git] / lib / standard / exec.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
5 #
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
12 # another product.
13
14 # Invocation and management of operating system sub-processes.
15 # Standard input and output can be handled through streams.
16 module exec
17
18 import stream
19
20 # Simple sub-process
21 class Process
22 # The pid of the process
23 fun id: Int do return data.id
24
25 # Is the process finished?
26 fun is_finished: Bool do return data.is_finished
27
28 # Wait the termination of the process
29 fun wait
30 do
31 data.wait
32 assert is_finished
33 end
34
35 # The status once finished
36 fun status: Int
37 do
38 assert is_finished
39 return data.status
40 end
41
42 # The executable run
43 # Is a filepath, or a executable found in PATH
44 var command: String
45
46 # The arguments of the command
47 # Starts with the first real arguments---ie. does not include the progname (`argv[0]`, in C)
48 var arguments: nullable Array[String]
49
50 # Launch a command with some arguments
51 init(command: String, arguments: String...)
52 do
53 self.command = command
54 self.arguments = arguments
55 execute
56 end
57
58 # Launch a simple command with arguments passed as an array
59 init from_a(command: String, arguments: nullable Array[String])
60 do
61 self.command = command
62 self.arguments = arguments
63 execute
64 end
65
66 # flags used internally to know whith pipe to open
67 private fun pipeflags: Int do return 0
68
69 # Internal code to handle execution
70 protected fun execute
71 do
72 # The pass the arguments as a big C string where elements are separated with '\0'
73 var args = new FlatBuffer
74 var l = 1 # Number of elements in args
75 args.append(command)
76 if arguments != null then
77 for a in arguments do
78 args.add('\0')
79 #a.output_class_name
80 args.append(a)
81 end
82 l += arguments.length
83 end
84 data = basic_exec_execute(command.to_cstring, args.to_s.to_cstring, l, pipeflags)
85 end
86
87 private var data: NativeProcess
88 private fun basic_exec_execute(p: NativeString, av: NativeString, ac: Int, pf: Int): NativeProcess is extern "exec_Process_Process_basic_exec_execute_4"
89 end
90
91 # stdout of the process is readable
92 class IProcess
93 super Process
94 super IStream
95 var stream_in: FDIStream
96
97 redef fun close do stream_in.close
98
99 redef fun read_char do return stream_in.read_char
100
101 redef fun eof do return stream_in.eof
102
103 redef fun pipeflags do return 2
104
105 redef init(command: String, arguments: String...) do super
106
107 redef fun execute
108 do
109 super
110 stream_in = new FDIStream(data.out_fd)
111 end
112 end
113
114 # stdin of the process is writable
115 class OProcess
116 super Process
117 super OStream
118 var stream_out: OStream
119
120 redef fun close do stream_out.close
121
122 redef fun is_writable do return stream_out.is_writable
123
124 redef fun write(s) do stream_out.write(s)
125
126 redef fun pipeflags do return 1
127
128 redef init(command: String, arguments: String...) do super
129
130 redef fun execute
131 do
132 super
133 stream_out = new FDOStream(data.in_fd)
134 end
135 end
136
137 # stdin and stdout are both accessible
138 class IOProcess
139 super IProcess
140 super OProcess
141 super IOStream
142
143 redef fun close
144 do
145 stream_in.close
146 stream_out.close
147 end
148
149 redef fun pipeflags do return 3
150
151 redef init(command: String, arguments: String...) do super
152
153 redef fun execute
154 do
155 super
156 end
157 end
158
159 redef class Sys
160 # Execute a shell command and return its error code
161 fun system(command: String): Int
162 do
163 return command.to_cstring.system
164 end
165 end
166
167 redef class NativeString
168 fun system: Int is extern "string_NativeString_NativeString_system_0"
169 end
170
171 private extern class NativeProcess
172 fun id: Int is extern "exec_NativeProcess_NativeProcess_id_0"
173 fun is_finished: Bool is extern "exec_NativeProcess_NativeProcess_is_finished_0"
174 fun status: Int is extern "exec_NativeProcess_NativeProcess_status_0"
175 fun wait is extern "exec_NativeProcess_NativeProcess_wait_0"
176
177 fun in_fd: Int is extern "exec_NativeProcess_NativeProcess_in_fd_0"
178 fun out_fd: Int is extern "exec_NativeProcess_NativeProcess_out_fd_0"
179 fun err_fd: Int is extern "exec_NativeProcess_NativeProcess_err_fd_0"
180 end
181