syntax: 'meth' -> 'fun', 'attr' -> 'var'
[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 # This module handle simple system calls
15 # Standard input and output can be handleb trougth streams.
16 package exec
17
18 import stream
19
20 # Simple sub-processus
21 class Process
22
23 # The pid of the processus
24 fun id: Int do return _data.id
25
26 # Is the processus finished?
27 fun is_finished: Bool do return _data.is_finished
28
29 # wait the terminaison of the process
30 fun wait
31 do
32 _data.wait
33 assert is_finished
34 end
35
36 # The status once finished
37 fun status: Int
38 do
39 assert is_finished
40 return _data.status
41 end
42
43 # send a signal to the process
44 fun kill(signal: Int) do _data.kill(signal)
45
46 # send the TERM (15) signal
47 fun term do kill(15)
48
49 # launch a command with some arguments
50 init(command: String, arguments: String...)
51 do
52 execute(command, arguments, 0)
53 end
54
55 # launch a simple command without arguments
56 init init_(command: String)
57 do
58 execute(command, null, 0)
59 end
60
61 # Internal code to handle execusion
62 protected init execute(command: String, arguments: nullable Array[String], pipeflags: Int)
63 do
64 var args = new Buffer
65 var l = 1 # Number of elements in args
66 args.append(command)
67 if arguments != null then
68 for a in arguments do
69 args.add('\0')
70 args.append(a)
71 end
72 l += arguments.length
73 end
74 _data = basic_exec_execute(command.to_cstring, args.to_s.to_cstring, l, pipeflags)
75 end
76
77 var _data: NativeProcess
78 private fun basic_exec_execute(p: NativeString, av: NativeString, ac: Int, pf: Int): NativeProcess is extern "exec_Process_Process_basic_exec_execute_4"
79 end
80
81 # stdout of the processus is readable
82 class IProcess
83 special Process
84 special IStream
85 var _in: FDIStream
86
87 redef fun close do _in.close
88
89 redef fun read_char do return _in.read_char
90
91 redef fun eof do return _in.eof
92
93 init(command: String, arguments: String...)
94 do
95 execute(command, arguments, 2)
96 _in = new FDIStream(_data.out_fd)
97 end
98
99 init init_(command: String)
100 do
101 execute(command, null, 2)
102 _in = new FDIStream(_data.out_fd)
103 end
104 end
105
106 # stdin of the processus is writable
107 class OProcess
108 special Process
109 special OStream
110 var _out: OStream
111
112 redef fun close do _out.close
113
114 redef fun is_writable do return _out.is_writable
115
116 redef fun write(s) do _out.write(s)
117
118 init(command: String, arguments: String...)
119 do
120 execute(command, arguments, 1)
121 _out = new FDOStream(_data.in_fd)
122 end
123
124 init init_(command: String)
125 do
126 execute(command, null, 1)
127 _out = new FDOStream(_data.in_fd)
128 end
129 end
130
131 # stdin and stdout are both accessible
132 class IOProcess
133 special IProcess
134 special OProcess
135 special IOStream
136
137 redef fun close
138 do
139 _in.close
140 _out.close
141 end
142
143 init(command: String, arguments: String...)
144 do
145 execute(command, arguments, 3)
146 _in = new FDIStream(_data.out_fd)
147 _out = new FDOStream(_data.in_fd)
148 end
149
150 init init_(command: String)
151 do
152 execute(command, null, 3)
153 _in = new FDIStream(_data.out_fd)
154 _out = new FDOStream(_data.in_fd)
155 end
156 end
157
158 redef class Sys
159 # Execute a shell command and return it's error code
160 fun system(command: String): Int
161 do
162 return command.to_cstring.system
163 end
164 end
165
166 redef class NativeString
167 fun system: Int is extern "string_NativeString_NativeString_system_0"
168 end
169
170 private universal NativeProcess
171 special Pointer
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 fun kill(s: Int) is extern "exec_NativeProcess_NativeProcess_kill_1"
177
178 fun in_fd: Int is extern "exec_NativeProcess_NativeProcess_in_fd_0"
179 fun out_fd: Int is extern "exec_NativeProcess_NativeProcess_out_fd_0"
180 fun err_fd: Int is extern "exec_NativeProcess_NativeProcess_err_fd_0"
181 end
182