-
Notifications
You must be signed in to change notification settings - Fork 211
/
launcher.factor
330 lines (247 loc) · 7.93 KB
/
launcher.factor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
! Copyright (C) 2008, 2011 Slava Pestov.
! See https://factorcode.org/license.txt for BSD license.
USING: accessors assocs calendar combinators concurrency.flags
debugger destructors environment fry init io io.backend
io.encodings io.encodings.utf8 io.pipes io.pipes.private
io.ports io.streams.duplex io.timeouts kernel math math.order
namespaces prettyprint sequences strings system threads vocabs ;
IN: io.launcher
TUPLE: process < identity-tuple
command
detached
hidden
environment
environment-mode
stdin
stdout
stderr
priority
group
timeout
handle status
killed
pipe ;
SYMBOL: +closed+
SYMBOL: +stdout+
TUPLE: appender path ;
C: <appender> appender
SYMBOL: +prepend-environment+
SYMBOL: +replace-environment+
SYMBOL: +append-environment+
SYMBOL: +lowest-priority+
SYMBOL: +low-priority+
SYMBOL: +normal-priority+
SYMBOL: +high-priority+
SYMBOL: +highest-priority+
SYMBOL: +realtime-priority+
SYMBOL: +same-group+
SYMBOL: +new-group+
SYMBOL: +new-session+
: <process> ( -- process )
process new
H{ } clone >>environment
+append-environment+ >>environment-mode
+same-group+ >>group ;
: process-started? ( process -- ? )
[ handle>> ] [ status>> ] bi or ;
: process-running? ( process -- ? )
handle>> >boolean ;
! Non-blocking process exit notification facility
SYMBOL: processes
HOOK: (wait-for-processes) io-backend ( -- ? )
<PRIVATE
SYMBOL: wait-flag
SYMBOL: wait-delay
: wait-loop ( -- )
processes get assoc-empty? [
5 wait-delay set-global
wait-flag get-global lower-flag
] [
(wait-for-processes) [
wait-delay [
[ milliseconds sleep ] [ 5 + 100 max ] bi
] change-global
] when
] if ;
: start-wait-thread ( -- )
<flag> wait-flag set-global
5 wait-delay set-global
[ wait-loop t ] "Process wait" spawn-server drop ;
STARTUP-HOOK: [
H{ } clone processes set-global
start-wait-thread
]
: process-started ( process handle -- )
>>handle
V{ } clone swap processes get set-at
wait-flag get-global raise-flag ;
: notify-exit ( process status -- )
>>status
[ processes get delete-at* drop [ resume ] each ] keep
f >>handle drop ;
: pass-environment? ( process -- ? )
dup environment>> assoc-empty? not
swap environment-mode>> +replace-environment+ eq? or ;
: get-environment ( process -- env )
[ environment>> ] [ environment-mode>> ] bi {
{ +prepend-environment+ [ os-envs assoc-union ] }
{ +append-environment+ [ os-envs swap assoc-union ] }
{ +replace-environment+ [ ] }
} case ;
PRIVATE>
GENERIC: >process ( obj -- process )
ERROR: process-already-started process ;
M: process-already-started error.
"Process has already been started" print nl
"Launch descriptor:" print nl
process>> . ;
M: process >process
dup process-started? [ process-already-started ] when
clone ;
M: object >process <process> swap >>command ;
HOOK: (current-process) io-backend ( -- handle )
ERROR: process-was-killed process ;
M: process-was-killed error.
"Process was killed as a result of a call to" print
"kill-process, or a timeout" print
nl
"Launch descriptor:" print nl
process>> . ;
: (wait-for-process) ( process -- status )
dup handle>>
[ self over processes get at push "process" suspend drop ] when
dup killed>> [ process-was-killed ] [ status>> ] if ;
: wait-for-process ( process -- status )
[ (wait-for-process) ] with-timeout ;
HOOK: (run-process) io-backend ( process -- handle )
: run-detached ( desc -- process )
>process [ dup (run-process) process-started ] keep ;
: run-process ( desc -- process )
run-detached
dup detached>> [ dup wait-for-process drop ] unless ;
: run-processes ( descs -- processes )
[ run-process ] map ;
ERROR: process-failed process ;
M: process-failed error.
[
"Process exited with error code " write process>> status>> . nl
"Launch descriptor:" print nl
] [ process>> . ] bi ;
: check-success ( process status -- )
0 = [ drop ] [ process-failed ] if ;
GENERIC: wait-for-success ( obj -- )
M: process wait-for-success
dup wait-for-process check-success ;
M: sequence wait-for-success
[ wait-for-success ] each ;
: try-process ( desc -- )
run-process wait-for-success ;
HOOK: (kill-process) io-backend ( process -- )
: kill-process ( process -- )
t >>killed
[ pipe>> [ dispose ] when* ]
[ dup handle>> [ (kill-process) ] [ drop ] if ] bi ;
M: process timeout timeout>> ;
M: process set-timeout timeout<< ;
M: process cancel-operation kill-process ;
M: object run-pipeline-element
[
>process
swap >>stdout
swap >>stdin
run-detached
] [
[
drop [ [ &dispose drop ] when* ] bi@
] with-destructors
] 3bi wait-for-process ;
<PRIVATE
: <process-with-pipe> ( desc -- process pipe )
>process (pipe) |dispose [ >>pipe ] keep ;
: (process-reader) ( desc encoding -- stream process )
[
[
<process-with-pipe> {
[ '[ _ out>> or ] change-stdout ]
[ drop run-detached ]
[ out>> dispose ]
[ in>> <input-port> ]
} cleave
] dip <decoder> swap
] with-destructors ;
PRIVATE>
: <process-reader> ( desc encoding -- stream )
(process-reader) drop ; inline
: with-process-reader* ( desc encoding quot -- process status )
[ (process-reader) ] dip '[
[ _ with-input-stream ] dip dup (wait-for-process)
] with-timeout ; inline
: with-process-reader ( desc encoding quot -- )
with-process-reader* check-success ; inline
: process-lines ( desc -- lines )
utf8 <process-reader> stream-lines ;
: process-contents ( desc -- contents )
utf8 <process-reader> stream-contents ;
<PRIVATE
: (process-writer) ( desc encoding -- stream process )
[
[
<process-with-pipe> {
[ '[ _ in>> or ] change-stdin ]
[ drop run-detached ]
[ in>> dispose ]
[ out>> <output-port> ]
} cleave
] dip <encoder> swap
] with-destructors ;
PRIVATE>
: <process-writer> ( desc encoding -- stream )
(process-writer) drop ; inline
: with-process-writer* ( desc encoding quot -- process status )
[ (process-writer) ] dip '[
[ _ with-output-stream ] dip dup (wait-for-process)
] with-timeout ; inline
: with-process-writer ( desc encoding quot -- )
with-process-writer* check-success ; inline
<PRIVATE
: (process-stream) ( desc encoding -- stream process )
[
[
(pipe) |dispose
(pipe) |dispose {
[
rot >process t >>hidden
[ swap in>> or ] change-stdin
[ swap out>> or ] change-stdout
run-detached
]
[ [ out>> &dispose drop ] [ in>> &dispose drop ] bi* ]
[ [ in>> <input-port> ] [ out>> <output-port> ] bi* ]
} 2cleave
] dip <encoder-duplex> swap
] with-destructors ;
PRIVATE>
: <process-stream> ( desc encoding -- stream )
(process-stream) drop ; inline
: with-process-stream* ( desc encoding quot -- process status )
[ (process-stream) ] dip '[
[ _ with-stream ] dip dup (wait-for-process)
] with-timeout ; inline
: with-process-stream ( desc encoding quot -- )
with-process-stream* check-success ; inline
ERROR: output-process-error { output string } { process process } ;
M: output-process-error error.
[ "Process:" print process>> . nl ]
[ "Output:" print output>> print ]
bi ;
: try-output-process ( command -- )
>process
+stdout+ >>stderr
[ +closed+ or ] change-stdin
utf8 (process-reader)
[ [ stream-contents ] [ dup (wait-for-process) ] bi* ] with-timeout
0 = [ 2drop ] [ output-process-error ] if ;
{
{ [ os unix? ] [ "io.launcher.unix" require ] }
{ [ os windows? ] [ "io.launcher.windows" require ] }
} cond