metamodel: rename 'universal' to 'enum'
[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 # launch a command with some arguments
44 init(command: String, arguments: String...)
45 do
46 execute(command, arguments, 0)
47 end
48
49 # launch a simple command without arguments
50 init init_(command: String)
51 do
52 execute(command, null, 0)
53 end
54
55 # Internal code to handle execusion
56 protected init execute(command: String, arguments: nullable Array[String], pipeflags: Int)
57 do
58 var args = new Buffer
59 var l = 1 # Number of elements in args
60 args.append(command)
61 if arguments != null then
62 for a in arguments do
63 args.add('\0')
64 args.append(a)
65 end
66 l += arguments.length
67 end
68 _data = basic_exec_execute(command.to_cstring, args.to_s.to_cstring, l, pipeflags)
69 end
70
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"
73 end
74
75 # stdout of the processus is readable
76 class IProcess
77 super Process
78 super IStream
79 var _in: FDIStream
80
81 redef fun close do _in.close
82
83 redef fun read_char do return _in.read_char
84
85 redef fun eof do return _in.eof
86
87 init(command: String, arguments: String...)
88 do
89 execute(command, arguments, 2)
90 _in = new FDIStream(_data.out_fd)
91 end
92
93 init init_(command: String)
94 do
95 execute(command, null, 2)
96 _in = new FDIStream(_data.out_fd)
97 end
98 end
99
100 # stdin of the processus is writable
101 class OProcess
102 super Process
103 super OStream
104 var _out: OStream
105
106 redef fun close do _out.close
107
108 redef fun is_writable do return _out.is_writable
109
110 redef fun write(s) do _out.write(s)
111
112 init(command: String, arguments: String...)
113 do
114 execute(command, arguments, 1)
115 _out = new FDOStream(_data.in_fd)
116 end
117
118 init init_(command: String)
119 do
120 execute(command, null, 1)
121 _out = new FDOStream(_data.in_fd)
122 end
123 end
124
125 # stdin and stdout are both accessible
126 class IOProcess
127 super IProcess
128 super OProcess
129 super IOStream
130
131 redef fun close
132 do
133 _in.close
134 _out.close
135 end
136
137 init(command: String, arguments: String...)
138 do
139 execute(command, arguments, 3)
140 _in = new FDIStream(_data.out_fd)
141 _out = new FDOStream(_data.in_fd)
142 end
143
144 init init_(command: String)
145 do
146 execute(command, null, 3)
147 _in = new FDIStream(_data.out_fd)
148 _out = new FDOStream(_data.in_fd)
149 end
150 end
151
152 redef class Sys
153 # Execute a shell command and return it's error code
154 fun system(command: String): Int
155 do
156 return command.to_cstring.system
157 end
158 end
159
160 redef class NativeString
161 fun system: Int is extern "string_NativeString_NativeString_system_0"
162 end
163
164 private universal NativeProcess
165 super Pointer
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"
170
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"
174 end
175