23182
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
12890
|
3 |
"{ Package: 'stx:libbasic' }"
|
|
4 |
|
19286
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
12890
|
7 |
Object subclass:#OSProcess
|
23035
|
8 |
instanceVariableNames:'pid command environment directory inStream outStream errorStream
|
|
9 |
auxStream showWindow lineWise newPgrp exitStatus finishSema
|
|
10 |
shufflerProcesses streamsToClose terminateActionBlock'
|
|
11 |
classVariableNames:''
|
|
12 |
poolDictionaries:''
|
|
13 |
category:'System-Support'
|
12890
|
14 |
!
|
|
15 |
|
22198
|
16 |
OSProcess subclass:#RemoteOSProcess
|
23035
|
17 |
instanceVariableNames:'host'
|
|
18 |
classVariableNames:'MethodPerHost'
|
|
19 |
poolDictionaries:''
|
|
20 |
privateIn:OSProcess
|
22198
|
21 |
!
|
|
22 |
|
12890
|
23 |
!OSProcess class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
documentation
|
|
26 |
"
|
21552
|
27 |
Instances of OSProcess represent operating system processes that can be executed.
|
12890
|
28 |
(as opposed to Smalltalk processes).
|
22439
|
29 |
Both local and remote processes are suported.
|
|
30 |
|
|
31 |
commandString:
|
|
32 |
If command is a String, the commandString is passed to a shell for execution
|
|
33 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
|
34 |
Regular input/output redirection and pipes etc. are supported as supported by
|
|
35 |
the underlying OS's command interpreter (i.e. some limits apply to MSDOS)
|
|
36 |
|
|
37 |
With an empty command, the shell will read commands from the passed input stream.
|
|
38 |
|
|
39 |
If command is an Array, the first element is the command to be executed,
|
|
40 |
and the other elements are the arguments to the command.
|
|
41 |
No shell is invoked in this case. Any redirection or piping can then be done
|
|
42 |
by Smalltalk code (including piping through smalltalk streams & filters).
|
12890
|
43 |
|
|
44 |
[author:]
|
|
45 |
Stefan Vogel (stefan@zwerg)
|
|
46 |
|
|
47 |
[instance variables:]
|
|
48 |
pid SmallInteger the process id
|
21182
|
49 |
command String the command line of the running command.
|
12890
|
50 |
|
|
51 |
[class variables:]
|
|
52 |
|
|
53 |
[see also:]
|
|
54 |
Process
|
|
55 |
|
|
56 |
"
|
21181
|
57 |
!
|
|
58 |
|
|
59 |
examples
|
21512
|
60 |
"
|
22439
|
61 |
send command's output to the Transcript:
|
|
62 |
[exBegin]
|
|
63 |
OSProcess new
|
22986
|
64 |
terminateActionBlock:[:status | Transcript showCR:status. Transcript showCR:status isError];
|
|
65 |
command:'lsxxxx -l';
|
22439
|
66 |
outStream:Transcript;
|
|
67 |
lineWise:true;
|
22986
|
68 |
startProcess.
|
22439
|
69 |
[exEnd]
|
|
70 |
|
|
71 |
send command's output to my stdout (see console):
|
|
72 |
[exBegin]
|
|
73 |
OSProcess new
|
|
74 |
command:'ls -l';
|
|
75 |
outStream:Stdout;
|
|
76 |
execute.
|
|
77 |
[exEnd]
|
|
78 |
|
|
79 |
send command's output to a Smalltalk stream:
|
22197
|
80 |
[exBegin]
|
22198
|
81 |
|outStream|
|
21181
|
82 |
|
22198
|
83 |
outStream := '' writeStream.
|
21181
|
84 |
|
22198
|
85 |
OSProcess new
|
|
86 |
command:'ls -l';
|
|
87 |
outStream:outStream;
|
|
88 |
execute.
|
21181
|
89 |
|
22439
|
90 |
outStream contents inspect.
|
22197
|
91 |
[exEnd]
|
21184
|
92 |
|
22439
|
93 |
feed it from a smalltalk stream, get command's output into a Smalltalk stream:
|
|
94 |
[exBegin]
|
|
95 |
|inStream outStream|
|
|
96 |
|
|
97 |
inStream := 'hello world' readStream.
|
|
98 |
outStream := '' writeStream.
|
|
99 |
|
|
100 |
OSProcess new
|
|
101 |
command:'tr ''a-z'' ''A-Z''';
|
|
102 |
inStream:inStream;
|
|
103 |
outStream:outStream;
|
|
104 |
execute.
|
|
105 |
|
|
106 |
outStream contents inspect.
|
|
107 |
[exEnd]
|
|
108 |
|
|
109 |
again, send output to a Smalltalk stream;
|
|
110 |
watch the stream getting filled after 10 seconds:
|
22197
|
111 |
[exBegin]
|
22198
|
112 |
|outStream|
|
21184
|
113 |
|
22198
|
114 |
outStream := '' writeStream.
|
21184
|
115 |
|
22198
|
116 |
OSProcess new
|
|
117 |
command:'ls -l; sleep 10; echo =================================; echo hallo after 10s; echo >&2 +++++++++++++++++++++; cat >&2';
|
|
118 |
outStream:outStream;
|
|
119 |
errorStream:outStream;
|
|
120 |
startProcess.
|
21184
|
121 |
|
22198
|
122 |
outStream inspect
|
22197
|
123 |
[exEnd]
|
21184
|
124 |
|
22439
|
125 |
separate stdout and stderr:
|
22197
|
126 |
[exBegin]
|
22439
|
127 |
|outStream errStream|
|
21184
|
128 |
|
22198
|
129 |
outStream := '' writeStream.
|
22439
|
130 |
errStream := '' writeStream.
|
21184
|
131 |
|
22198
|
132 |
OSProcess new
|
22439
|
133 |
command:'ls -l && ls >&2';
|
22198
|
134 |
outStream:outStream;
|
22439
|
135 |
errorStream:errStream;
|
22198
|
136 |
startProcess.
|
21184
|
137 |
|
22439
|
138 |
outStream inspect.
|
|
139 |
errStream inspect.
|
22197
|
140 |
[exEnd]
|
21512
|
141 |
|
22439
|
142 |
Execute commands in shell/cmd.exe and read them from stdin:
|
22197
|
143 |
[exBegin]
|
22198
|
144 |
|outStream|
|
21512
|
145 |
|
22198
|
146 |
outStream := '' writeStream.
|
21512
|
147 |
|
22198
|
148 |
OSProcess new
|
|
149 |
command:'';
|
|
150 |
inStream:'ls -l' readStream;
|
|
151 |
outStream:outStream;
|
|
152 |
lineWise:true;
|
|
153 |
execute.
|
21512
|
154 |
|
22198
|
155 |
outStream contents inspect
|
22197
|
156 |
[exEnd]
|
22439
|
157 |
|
|
158 |
Execute on a remote host (you must have the appropriate ssh setup):
|
|
159 |
[exBegin]
|
|
160 |
|outStream|
|
|
161 |
|
|
162 |
outStream := '' writeStream.
|
|
163 |
|
|
164 |
(OSProcess onHost:'exeptn')
|
|
165 |
command:'ls -l';
|
|
166 |
outStream:outStream;
|
|
167 |
lineWise:true;
|
|
168 |
execute.
|
|
169 |
|
|
170 |
outStream contents inspect
|
|
171 |
[exEnd]
|
21512
|
172 |
"
|
21181
|
173 |
! !
|
|
174 |
|
|
175 |
!OSProcess class methodsFor:'instance creation'!
|
|
176 |
|
22437
|
177 |
command:aCommandString
|
22439
|
178 |
"return an initialized instance to execute aCommandString
|
|
179 |
in the current directory (for a local process)"
|
22437
|
180 |
|
|
181 |
^ self new command:aCommandString.
|
|
182 |
!
|
|
183 |
|
22438
|
184 |
command:aCommandString directory:aStringOrFilename
|
22439
|
185 |
"return an initialized instance to execute aCommandString
|
|
186 |
in a given directory (for a local process)"
|
22437
|
187 |
|
22438
|
188 |
^ self new command:aCommandString directory:aStringOrFilename.
|
22437
|
189 |
!
|
|
190 |
|
22568
|
191 |
commandStringForProgramName:executableFile arguments:arrayOfStrings
|
23184
|
192 |
"generate a command line string from appending args (possibley quoted) to the exe-name (with spaces)"
|
|
193 |
|
22568
|
194 |
^ String streamContents:[:s |
|
|
195 |
s nextPutAll:(self possiblyQuoted:executableFile asFilename pathName).
|
23182
|
196 |
arrayOfStrings notNil ifTrue:[
|
|
197 |
arrayOfStrings do:[:eachArg |
|
23183
|
198 |
s space.
|
23182
|
199 |
s nextPutAll:(self possiblyQuoted:eachArg).
|
|
200 |
].
|
|
201 |
]
|
22568
|
202 |
]
|
23182
|
203 |
|
23184
|
204 |
"Modified (comment): / 02-07-2018 / 20:50:29 / cg"
|
22568
|
205 |
!
|
|
206 |
|
21181
|
207 |
new
|
22198
|
208 |
"return an initialized instance for a local process"
|
21181
|
209 |
|
|
210 |
^ self basicNew initialize.
|
22198
|
211 |
!
|
|
212 |
|
|
213 |
onHost:aHost
|
|
214 |
"return an initialized instance for a remote process running on another host"
|
|
215 |
|
|
216 |
(SocketAddress hostName:aHost) isLocal ifTrue:[
|
|
217 |
^ self basicNew initialize.
|
|
218 |
].
|
|
219 |
^ RemoteOSProcess basicNew host:aHost; initialize.
|
|
220 |
|
|
221 |
"
|
|
222 |
OSProcess onHost:'localhost'
|
|
223 |
OSProcess onHost:'exeptn'
|
22439
|
224 |
|
|
225 |
(OSProcess new) command:'ls'
|
|
226 |
(OSProcess onHost:'exeptn') command:'ls'
|
22198
|
227 |
"
|
22568
|
228 |
!
|
|
229 |
|
|
230 |
possiblyQuoted:aString
|
|
231 |
"should we quote or escape?"
|
|
232 |
|
|
233 |
^ String streamContents:[:s |
|
|
234 |
aString do:[:ch |
|
|
235 |
(ch isSeparator or:[ch = $\]) ifTrue:[
|
|
236 |
s nextPut:$\
|
|
237 |
].
|
|
238 |
s nextPut:ch
|
|
239 |
]
|
|
240 |
]
|
|
241 |
|
|
242 |
"
|
|
243 |
self possiblyQuoted:'foo bar'
|
|
244 |
self possiblyQuoted:'foo\bar'
|
|
245 |
"
|
|
246 |
!
|
|
247 |
|
|
248 |
programName:executableFile arguments:arrayOfStrings initialEnvironment:stringDictionary
|
|
249 |
"similar to command:, but with separate command and arguments"
|
|
250 |
|
|
251 |
^ self new
|
|
252 |
command:(self commandStringForProgramName:executableFile arguments:arrayOfStrings);
|
|
253 |
environment:stringDictionary;
|
|
254 |
yourself
|
23182
|
255 |
|
|
256 |
"
|
|
257 |
(self programName:'notepad.exe' arguments:#() initialEnvironment:nil)
|
|
258 |
showWindow:true;
|
|
259 |
startProcess
|
|
260 |
"
|
|
261 |
|
|
262 |
"Modified (comment): / 02-07-2018 / 20:33:02 / cg"
|
12890
|
263 |
! !
|
|
264 |
|
22439
|
265 |
!OSProcess class methodsFor:'class initialization'!
|
21172
|
266 |
|
|
267 |
initialize
|
22568
|
268 |
"Backward compatibility: Win32Process is an alias for OSProcess"
|
|
269 |
|
21172
|
270 |
Win32Process := self.
|
|
271 |
! !
|
|
272 |
|
22568
|
273 |
!OSProcess class methodsFor:'queries'!
|
|
274 |
|
|
275 |
defaultShellPath
|
|
276 |
OperatingSystem isUNIXlike ifTrue:[
|
|
277 |
^ OperatingSystem getEnvironment:'SHELL'
|
|
278 |
].
|
|
279 |
^ 'cmd'
|
|
280 |
! !
|
|
281 |
|
12890
|
282 |
!OSProcess methodsFor:'accessing'!
|
|
283 |
|
21181
|
284 |
auxStream
|
|
285 |
^ auxStream
|
|
286 |
!
|
|
287 |
|
|
288 |
auxStream:something
|
21552
|
289 |
"set an auxiliary input stream that will be available to the command as
|
21208
|
290 |
file descriptor 3"
|
|
291 |
|
21181
|
292 |
auxStream := something.
|
21552
|
293 |
|
|
294 |
"Modified (comment): / 23-02-2017 / 10:56:57 / Maren"
|
21181
|
295 |
!
|
|
296 |
|
21172
|
297 |
command
|
22438
|
298 |
"the OS (shell-) command"
|
21172
|
299 |
|
|
300 |
^ command
|
|
301 |
|
|
302 |
"Created: / 10.11.1998 / 21:27:07 / cg"
|
|
303 |
!
|
|
304 |
|
22437
|
305 |
command:aStringOrArray
|
21208
|
306 |
"set the command to be executed.
|
22439
|
307 |
|
21208
|
308 |
If aStringOrArray is a String, the commandString is passed to a shell for execution
|
|
309 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
22439
|
310 |
With an empty command, the shell will read commands from the passed input stream.
|
|
311 |
|
21208
|
312 |
If aCommandString is an Array, the first element is the command to be executed,
|
22439
|
313 |
and the other elements are the arguments to the command.
|
|
314 |
No shell is invoked in this case."
|
22437
|
315 |
|
21208
|
316 |
command := aStringOrArray.
|
21172
|
317 |
!
|
|
318 |
|
22436
|
319 |
command:commandStringArg directory:stringOrFilenameArg
|
22438
|
320 |
"set the command to be executed and directory, where to execute.
|
|
321 |
If aStringOrArray is a String, the commandString is passed to a shell for execution
|
|
322 |
- see the description of 'sh -c' in your UNIX manual ('cmd.exe' in your Windows manual).
|
|
323 |
If aCommandString is an Array, the first element is the command to be executed,
|
|
324 |
and the other elements are the arguments to the command. No shell is invoked in this case."
|
|
325 |
|
22436
|
326 |
command := commandStringArg.
|
|
327 |
directory := stringOrFilenameArg.
|
|
328 |
!
|
|
329 |
|
22181
|
330 |
command:commandArg environment:environmentArg directory:directoryArg inStream:inStreamArg outStream:outStreamArg errorStream:errorStreamArg auxStream:auxStreamArg showWindow:showWindowArg lineWise:lineWiseArg
|
22438
|
331 |
"set the command to be executed & directory, where to execute.
|
|
332 |
and input/output streams.
|
|
333 |
See comments in individual setters for more info"
|
|
334 |
|
22181
|
335 |
command := commandArg.
|
|
336 |
environment := environmentArg.
|
|
337 |
directory := directoryArg.
|
|
338 |
inStream := inStreamArg.
|
|
339 |
outStream := outStreamArg.
|
|
340 |
errorStream := errorStreamArg.
|
|
341 |
auxStream := auxStreamArg.
|
|
342 |
showWindow := showWindowArg.
|
|
343 |
lineWise := lineWiseArg.
|
|
344 |
!
|
|
345 |
|
21172
|
346 |
directory
|
22438
|
347 |
"the directory where executed"
|
21172
|
348 |
|
|
349 |
^ directory
|
|
350 |
|
|
351 |
"Created: / 10.11.1998 / 21:21:52 / cg"
|
|
352 |
!
|
|
353 |
|
22436
|
354 |
directory:aStringOrFilename
|
21208
|
355 |
"set the directory that will be set as the current directory of the command to be executed"
|
21172
|
356 |
|
22436
|
357 |
directory := aStringOrFilename.
|
21172
|
358 |
!
|
|
359 |
|
|
360 |
environment
|
22438
|
361 |
"the shell environment"
|
21172
|
362 |
|
|
363 |
^ environment
|
|
364 |
|
|
365 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
366 |
!
|
|
367 |
|
21208
|
368 |
environment:aDictionary
|
|
369 |
"set the environment variables of the command to be executed"
|
21172
|
370 |
|
21208
|
371 |
environment := aDictionary.
|
21172
|
372 |
!
|
|
373 |
|
|
374 |
errorStream
|
|
375 |
^ errorStream
|
|
376 |
|
|
377 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
12890
|
378 |
!
|
|
379 |
|
21208
|
380 |
errorStream:aStream
|
|
381 |
"set the stream where the stderr output of the command is directed to"
|
21172
|
382 |
|
21208
|
383 |
errorStream := aStream.
|
21172
|
384 |
!
|
|
385 |
|
|
386 |
exitStatus
|
21208
|
387 |
"answer the exit status of the command or nil, if the command has not yet been finished"
|
21172
|
388 |
|
|
389 |
^ exitStatus
|
|
390 |
!
|
|
391 |
|
|
392 |
exitStatus:something
|
|
393 |
"set the value of the instance variable 'exitStatus' (automatically generated)"
|
|
394 |
|
|
395 |
exitStatus := something.
|
|
396 |
|
|
397 |
"Created: / 10.11.1998 / 21:24:55 / cg"
|
|
398 |
!
|
|
399 |
|
|
400 |
finishSema
|
21552
|
401 |
"wait on this semaphore if you want to wait until the OS process has finished.
|
21184
|
402 |
There may be multiple waiters, so it is a good idea to do a #waitUncounted"
|
21172
|
403 |
|
|
404 |
^ finishSema
|
21552
|
405 |
|
|
406 |
"Modified (comment): / 23-02-2017 / 10:53:17 / Maren"
|
21172
|
407 |
!
|
|
408 |
|
|
409 |
inStream
|
|
410 |
^ inStream
|
|
411 |
|
|
412 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
413 |
!
|
|
414 |
|
21208
|
415 |
inStream:aStream
|
|
416 |
"set the stream where the stdin input of the command is read from"
|
21172
|
417 |
|
21208
|
418 |
inStream := aStream.
|
21172
|
419 |
!
|
|
420 |
|
21181
|
421 |
lineWise
|
|
422 |
^ lineWise
|
|
423 |
!
|
|
424 |
|
21208
|
425 |
lineWise:aBoolean
|
21209
|
426 |
"When setting to true, read linewise from the command's output and error.
|
|
427 |
This is a bit slower than lineWise = false.
|
21208
|
428 |
|
|
429 |
You may use it also when streaming to e.g. Transcript"
|
|
430 |
|
|
431 |
lineWise := aBoolean.
|
21181
|
432 |
!
|
|
433 |
|
21182
|
434 |
newPgrp
|
|
435 |
^ newPgrp
|
|
436 |
!
|
|
437 |
|
21208
|
438 |
newPgrp:aBoolean
|
|
439 |
"if aBoolean is true, a new process group will be created for the command and its subprocesses"
|
|
440 |
|
|
441 |
newPgrp := aBoolean.
|
21182
|
442 |
!
|
|
443 |
|
21172
|
444 |
outStream
|
22985
|
445 |
"the stream where the stdout output of the command is directed to"
|
|
446 |
|
21172
|
447 |
^ outStream
|
|
448 |
|
|
449 |
"Created: / 10.11.1998 / 21:26:34 / cg"
|
|
450 |
!
|
|
451 |
|
21208
|
452 |
outStream:aStream
|
|
453 |
"set the stream where the stdout output of the command is directed to"
|
21172
|
454 |
|
21208
|
455 |
outStream := aStream.
|
12890
|
456 |
!
|
|
457 |
|
|
458 |
pid
|
21208
|
459 |
"answer the pid of the process the command is running in or nil, if the command has not yet been started."
|
|
460 |
|
12890
|
461 |
^ pid
|
|
462 |
!
|
|
463 |
|
21181
|
464 |
showWindow
|
|
465 |
^ showWindow
|
|
466 |
!
|
|
467 |
|
21208
|
468 |
showWindow:aBooleanOrNil
|
|
469 |
"This parameter is ignored on Unix systems.
|
|
470 |
|
|
471 |
You can control (have to - sigh) if a window should be shown for the command or not.
|
|
472 |
This is the OS's H_SHOWWINDOW argument.
|
|
473 |
If you pass nil as showWindow-argument, the OS's default is used for the particular
|
|
474 |
command, which is correct most of the time: i.e. a notepad will open its window, other (non-UI)
|
|
475 |
executables will not.
|
|
476 |
However, some command-line executables show a window, even if they should not.
|
|
477 |
(and also, there seems to be an inconsistency between windows7 and newer windows: in newer,
|
|
478 |
a shell command opens a cmd-window, whereas in windows7 it did not)
|
|
479 |
In this case, pass an explicit false argument to suppress it."
|
|
480 |
|
|
481 |
showWindow := aBooleanOrNil.
|
21206
|
482 |
!
|
|
483 |
|
|
484 |
terminateActionBlock
|
22985
|
485 |
"the (user provided) callback block,
|
|
486 |
that will be executed when the command has finished or was terminated.
|
|
487 |
If non-nil, it will be called with optional argument:status and:self, the OSProcess."
|
|
488 |
|
21206
|
489 |
^ terminateActionBlock
|
22985
|
490 |
|
|
491 |
"Modified (comment): / 22-05-2018 / 12:21:24 / Claus Gittinger"
|
21206
|
492 |
!
|
|
493 |
|
21208
|
494 |
terminateActionBlock:aBlock
|
22985
|
495 |
"set the callback block,
|
|
496 |
that will be called when the command has finished or was terminated.
|
22986
|
497 |
If non-nil, it will be called with optional argument:status and:self, the OSProcess.
|
|
498 |
WARNING:
|
|
499 |
if the active-process calling #startProcess is already dead (i.e. terminated),
|
|
500 |
this callback will be called by the scheduler;
|
|
501 |
otherwise, the calling process will be interrupted to perform this callback.
|
|
502 |
Therefore: do not open any GUI or other blocking actions in it."
|
21208
|
503 |
|
|
504 |
terminateActionBlock := aBlock.
|
22985
|
505 |
|
22986
|
506 |
"Modified (comment): / 22-05-2018 / 12:42:21 / Claus Gittinger"
|
12890
|
507 |
! !
|
|
508 |
|
21181
|
509 |
!OSProcess methodsFor:'initialization'!
|
|
510 |
|
|
511 |
initialize
|
|
512 |
"Invoked when a new instance is created."
|
|
513 |
|
|
514 |
showWindow := false. "/ for backward compatibility
|
21183
|
515 |
lineWise := false.
|
|
516 |
newPgrp := true.
|
21181
|
517 |
! !
|
|
518 |
|
23002
|
519 |
|
13313
|
520 |
!OSProcess methodsFor:'printing'!
|
|
521 |
|
|
522 |
printOn:aStream
|
|
523 |
aStream
|
|
524 |
nextPutAll:self className;
|
|
525 |
nextPut:$(.
|
|
526 |
pid printOn:aStream.
|
|
527 |
aStream space.
|
21172
|
528 |
command printOn:aStream.
|
13313
|
529 |
aStream nextPut:$)
|
|
530 |
! !
|
|
531 |
|
21181
|
532 |
!OSProcess methodsFor:'private'!
|
|
533 |
|
|
534 |
setupShufflerForInput:aStream
|
21182
|
535 |
"if aStream is an internal Stream, set up a pipe for the command input.
|
21552
|
536 |
Start a process that shuffles the data from the internal stream into the pipe
|
21182
|
537 |
(and into the command's input)."
|
|
538 |
|
21181
|
539 |
|pipe externalStream shuffledStream shufflerProcess|
|
|
540 |
|
|
541 |
(aStream isNil or:[aStream isExternalStream]) ifTrue:[
|
|
542 |
^ aStream.
|
|
543 |
].
|
|
544 |
|
|
545 |
pipe := NonPositionableExternalStream makePipe.
|
|
546 |
externalStream := pipe at:1.
|
|
547 |
shuffledStream := pipe at:2.
|
|
548 |
lineWise ifFalse:[
|
|
549 |
shuffledStream blocking:false.
|
|
550 |
].
|
|
551 |
aStream isBinary ifTrue:[
|
|
552 |
shuffledStream binary.
|
|
553 |
].
|
|
554 |
|
|
555 |
"/ start a reader process, shuffling data from the given
|
|
556 |
"/ inStream to the pipe (which is connected to the commands input)
|
22198
|
557 |
shufflerProcess :=
|
|
558 |
[
|
21181
|
559 |
[
|
21332
|
560 |
lineWise ifTrue:[
|
|
561 |
"shuffle until end-of-input"
|
|
562 |
[aStream atEnd] whileFalse:[
|
|
563 |
|data|
|
21184
|
564 |
|
21332
|
565 |
data := aStream nextLine.
|
|
566 |
data notNil ifTrue:[
|
|
567 |
shuffledStream nextPutLine:data.
|
|
568 |
shuffledStream flush.
|
21184
|
569 |
].
|
|
570 |
].
|
21332
|
571 |
] ifFalse:[
|
|
572 |
aStream copyToEndInto:shuffledStream.
|
21184
|
573 |
].
|
21332
|
574 |
] on:WriteError do:[:ex|
|
|
575 |
"ignore"
|
21181
|
576 |
] ensure:[
|
|
577 |
shuffledStream close.
|
|
578 |
]
|
|
579 |
] newProcess
|
|
580 |
name:'OSProcess input shuffler';
|
|
581 |
"/ beSystemProcess;
|
|
582 |
resume.
|
|
583 |
|
|
584 |
shufflerProcesses add:shufflerProcess.
|
21184
|
585 |
streamsToClose add:externalStream.
|
|
586 |
|
21181
|
587 |
^ externalStream
|
21332
|
588 |
|
|
589 |
"Modified: / 31-01-2017 / 16:50:39 / stefan"
|
21552
|
590 |
"Modified (comment): / 23-02-2017 / 10:51:34 / Maren"
|
21181
|
591 |
!
|
|
592 |
|
|
593 |
setupShufflerForOutput:aStream
|
21182
|
594 |
"if aStream is an internal Stream, set up a pipe for the command output.
|
|
595 |
Start a process that shuffles the data fron the pipe into the internal stream."
|
|
596 |
|
21181
|
597 |
|pipe externalStream shuffledStream shufflerProcess|
|
|
598 |
|
|
599 |
(aStream isNil or:[aStream isExternalStream]) ifTrue:[
|
|
600 |
^ aStream.
|
|
601 |
].
|
|
602 |
|
|
603 |
pipe := NonPositionableExternalStream makePipe.
|
|
604 |
externalStream := pipe at:2.
|
|
605 |
shuffledStream := pipe at:1.
|
|
606 |
aStream isBinary ifTrue:[
|
|
607 |
shuffledStream binary.
|
|
608 |
].
|
|
609 |
|
22198
|
610 |
shufflerProcess :=
|
|
611 |
[
|
21332
|
612 |
[
|
|
613 |
"shuffle until the pipe closes"
|
|
614 |
lineWise ifTrue:[
|
|
615 |
[shuffledStream atEnd] whileFalse:[
|
|
616 |
|data|
|
21184
|
617 |
|
21332
|
618 |
data := shuffledStream nextLine.
|
|
619 |
data notNil ifTrue:[
|
|
620 |
aStream nextPutLine:data
|
21184
|
621 |
].
|
|
622 |
].
|
21332
|
623 |
] ifFalse:[
|
|
624 |
shuffledStream copyToEndInto:aStream.
|
21181
|
625 |
].
|
21332
|
626 |
] on:WriteError do:[:ex |
|
|
627 |
"ignore"
|
|
628 |
] ensure:[
|
|
629 |
shuffledStream close.
|
21181
|
630 |
].
|
|
631 |
] newProcess
|
|
632 |
priority:(Processor userSchedulingPriority "+ 1");
|
|
633 |
name:'OSProcess output shuffler';
|
|
634 |
"/ beSystemProcess;
|
|
635 |
resume.
|
|
636 |
|
21223
|
637 |
shufflerProcesses add:shufflerProcess.
|
21184
|
638 |
streamsToClose add:externalStream.
|
|
639 |
|
21181
|
640 |
^ externalStream
|
21332
|
641 |
|
|
642 |
"Modified: / 31-01-2017 / 16:57:25 / stefan"
|
22198
|
643 |
!
|
|
644 |
|
|
645 |
startCommand
|
|
646 |
"the 'real' command to be executed.
|
22987
|
647 |
Redefined for remote processes (eg. to construct a remote command string).
|
|
648 |
Command may be both an Array or a String"
|
22198
|
649 |
|
|
650 |
^ command
|
22987
|
651 |
|
|
652 |
"Modified (comment): / 22-05-2018 / 12:37:38 / Stefan Vogel"
|
21181
|
653 |
! !
|
|
654 |
|
12890
|
655 |
!OSProcess methodsFor:'queries'!
|
|
656 |
|
21208
|
657 |
finishedWithSuccess
|
|
658 |
^ exitStatus notNil and:[exitStatus success].
|
|
659 |
!
|
|
660 |
|
12890
|
661 |
isAlive
|
21552
|
662 |
"answer true if the process is still alive"
|
12890
|
663 |
|
21205
|
664 |
^ pid notNil and:[exitStatus isNil]
|
21552
|
665 |
|
|
666 |
"Modified (comment): / 23-02-2017 / 10:52:37 / Maren"
|
12890
|
667 |
!
|
|
668 |
|
|
669 |
isDead
|
21552
|
670 |
"answer true if the process is no longer alive"
|
12890
|
671 |
|
|
672 |
^ self isAlive not
|
21552
|
673 |
|
|
674 |
"Modified (comment): / 23-02-2017 / 10:52:31 / Maren"
|
12890
|
675 |
! !
|
|
676 |
|
21172
|
677 |
!OSProcess methodsFor:'starting'!
|
|
678 |
|
21181
|
679 |
execute
|
21184
|
680 |
"execute the command.
|
21552
|
681 |
Wait until it is finished.
|
21184
|
682 |
Abort the execution if I am interrupted.
|
21552
|
683 |
Answer true if it terminated successfully,
|
21184
|
684 |
false if it could not be started or terminated with error."
|
21181
|
685 |
|
21184
|
686 |
|ok|
|
21181
|
687 |
|
|
688 |
[
|
21185
|
689 |
ok := self startProcess.
|
|
690 |
ok ifTrue:[
|
21208
|
691 |
self waitUntilFinished.
|
|
692 |
ok := self finishedWithSuccess.
|
21185
|
693 |
].
|
21184
|
694 |
] ifCurtailed:[
|
|
695 |
"/ we were interrupted -
|
|
696 |
"/ terminate the os-command (and all of its forked commands)
|
21185
|
697 |
pid notNil ifTrue:[
|
|
698 |
self terminateGroup.
|
|
699 |
].
|
21181
|
700 |
].
|
21184
|
701 |
^ ok.
|
21552
|
702 |
|
|
703 |
"Modified (comment): / 23-02-2017 / 10:54:34 / Maren"
|
21181
|
704 |
!
|
|
705 |
|
21172
|
706 |
startProcess
|
22987
|
707 |
"Start the command asynchronously (i.e. don't wait until is has finished).
|
|
708 |
If there are non-external streams, setup transfer (shuffler) processes
|
21184
|
709 |
to transfer data from a pipe to the internal stream.
|
22987
|
710 |
|
|
711 |
Answer true if the command could started succesfully, false if not.
|
|
712 |
|
|
713 |
NOTE: under normal circumstances, even if the command cannot be found,
|
|
714 |
the exit fom the command interpreter/shell is done some time later
|
|
715 |
So you have to check later or set #terminateActionBlock: and check there."
|
21184
|
716 |
|
22986
|
717 |
|externalInStream externalAuxStream externalErrorStream externalOutStream callingProcess|
|
21184
|
718 |
|
21200
|
719 |
shufflerProcesses := OrderedCollection new:4.
|
|
720 |
streamsToClose := OrderedCollection new:2.
|
21184
|
721 |
|
|
722 |
externalInStream := self setupShufflerForInput:inStream.
|
|
723 |
externalAuxStream := self setupShufflerForInput:auxStream.
|
21226
|
724 |
externalOutStream := self setupShufflerForOutput:outStream.
|
22198
|
725 |
|
21184
|
726 |
errorStream == outStream ifTrue:[
|
21226
|
727 |
externalErrorStream := externalOutStream.
|
21184
|
728 |
] ifFalse:[
|
|
729 |
externalErrorStream := self setupShufflerForOutput:errorStream.
|
|
730 |
].
|
|
731 |
|
|
732 |
"start the command"
|
21200
|
733 |
finishSema := EventSemaphore new.
|
21172
|
734 |
|
22983
|
735 |
"/ UserPreferences current logExecutedOSCommands:true
|
|
736 |
UserPreferences current logExecutedOSCommands ifTrue:[
|
23002
|
737 |
Transcript showCR:(('OS command: ', self startCommand printString) withColor:Color brown).
|
22983
|
738 |
].
|
22986
|
739 |
|
|
740 |
callingProcess := Processor activeProcess.
|
22983
|
741 |
|
21172
|
742 |
Processor
|
|
743 |
monitor:[
|
|
744 |
pid := OperatingSystem
|
22198
|
745 |
startProcess:(self startCommand)
|
21200
|
746 |
inputFrom:externalInStream
|
|
747 |
outputTo:externalOutStream
|
|
748 |
errorTo:externalErrorStream
|
|
749 |
auxFrom:externalAuxStream
|
|
750 |
environment:environment
|
|
751 |
inDirectory:directory
|
|
752 |
newPgrp:newPgrp
|
|
753 |
showWindow:showWindow.
|
21172
|
754 |
]
|
|
755 |
action:[:status |
|
|
756 |
status stillAlive ifFalse:[
|
|
757 |
exitStatus := status.
|
23006
|
758 |
self terminate.
|
|
759 |
|
22198
|
760 |
"/ shufflerProcesses do:[:eachProcess|
|
|
761 |
"/ "terminate the shuffler processes.
|
|
762 |
"/ They close the local side of the pipe when being terminated"
|
|
763 |
"/ eachProcess terminate.
|
|
764 |
"/ ].
|
|
765 |
|
21223
|
766 |
finishSema signal.
|
22985
|
767 |
terminateActionBlock notNil ifTrue:[
|
22986
|
768 |
callingProcess isDead ifTrue:[
|
|
769 |
terminateActionBlock valueWithOptionalArgument:status and:self.
|
|
770 |
] ifFalse:[
|
|
771 |
callingProcess
|
|
772 |
interruptWith:[
|
|
773 |
terminateActionBlock valueWithOptionalArgument:status and:self.
|
|
774 |
]
|
|
775 |
].
|
22985
|
776 |
].
|
23126
|
777 |
UserPreferences current logExecutedOSCommands ifTrue:[
|
|
778 |
Transcript
|
|
779 |
showCR:(('OS command finished: %1 status: %2'
|
|
780 |
bindWith: self startCommand
|
|
781 |
with:status code)
|
|
782 |
withColor:(
|
|
783 |
status success
|
|
784 |
ifTrue:[Color green darkened]
|
|
785 |
ifFalse:[Color red])).
|
|
786 |
].
|
21172
|
787 |
].
|
|
788 |
].
|
|
789 |
|
21184
|
790 |
"we can close the remote side of the pipes after the os process has been started (after the fork)"
|
|
791 |
streamsToClose do:[:eachStream | eachStream close].
|
|
792 |
streamsToClose := nil.
|
|
793 |
|
21172
|
794 |
pid isNil ifTrue:[
|
23126
|
795 |
UserPreferences current logExecutedOSCommands ifTrue:[
|
|
796 |
Transcript
|
|
797 |
showCR:(('OS command failed: %1'
|
|
798 |
bindWith: self startCommand)
|
|
799 |
withColor:(Color red)).
|
|
800 |
].
|
|
801 |
|
21184
|
802 |
"process could not be started - terminate shufflers"
|
|
803 |
shufflerProcesses do:[:eachProcess|
|
|
804 |
eachProcess terminate.
|
|
805 |
].
|
|
806 |
shufflerProcesses := nil.
|
21172
|
807 |
exitStatus := OperatingSystem osProcessStatusClass processCreationFailure.
|
21208
|
808 |
finishSema signal.
|
23006
|
809 |
terminateActionBlock notNil ifTrue:[
|
|
810 |
terminateActionBlock valueWithOptionalArgument:exitStatus and:self.
|
|
811 |
].
|
21184
|
812 |
^ false.
|
21172
|
813 |
].
|
|
814 |
|
|
815 |
^ true.
|
22985
|
816 |
|
23006
|
817 |
"Modified: / 24-05-2018 / 17:58:40 / Stefan Vogel"
|
23126
|
818 |
"Modified: / 21-06-2018 / 19:01:38 / Claus Gittinger"
|
21172
|
819 |
! !
|
|
820 |
|
12890
|
821 |
!OSProcess methodsFor:'terminating'!
|
|
822 |
|
|
823 |
kill
|
|
824 |
"kill the process - the process does not get the chance to clean up"
|
|
825 |
|
21208
|
826 |
pid notNil ifTrue:[
|
|
827 |
OperatingSystem killProcess:pid.
|
|
828 |
].
|
12890
|
829 |
!
|
|
830 |
|
|
831 |
killGroup
|
20379
|
832 |
"kill the processGroup - the processes do not get the chance to clean up"
|
|
833 |
|
21208
|
834 |
pid notNil ifTrue:[
|
|
835 |
OperatingSystem
|
|
836 |
killProcessGroup:pid;
|
|
837 |
killProcess:pid.
|
|
838 |
].
|
12890
|
839 |
!
|
|
840 |
|
|
841 |
terminate
|
|
842 |
"terminate the process gracefully"
|
|
843 |
|
21208
|
844 |
pid notNil ifTrue:[
|
|
845 |
OperatingSystem terminateProcess:pid.
|
|
846 |
].
|
12890
|
847 |
!
|
|
848 |
|
|
849 |
terminateGroup
|
|
850 |
"terminate the process group.
|
21552
|
851 |
Under Windows, this is the same as terminateWithAllChildren,
|
12890
|
852 |
under unix, this terminates a subset of all children"
|
|
853 |
|
21208
|
854 |
pid notNil ifTrue:[
|
|
855 |
OperatingSystem
|
21632
|
856 |
terminateProcess:pid;
|
|
857 |
terminateProcessGroup:pid.
|
21208
|
858 |
].
|
21551
|
859 |
|
21552
|
860 |
"Modified (comment): / 23-02-2017 / 10:50:13 / Maren"
|
21632
|
861 |
"Modified: / 09-03-2017 / 15:00:58 / stefan"
|
21184
|
862 |
! !
|
|
863 |
|
|
864 |
!OSProcess methodsFor:'waiting'!
|
|
865 |
|
|
866 |
waitUntilFinished
|
23035
|
867 |
<resource: #skipInDebuggersWalkBack>
|
22148
|
868 |
|
|
869 |
"wait with a veryy long timeout,
|
|
870 |
in order that ProcessorScheduler>>#checkForEndOfDispatch recogizes
|
|
871 |
this waiting process as user process which is still alive.
|
|
872 |
The timout is meant to never occur!!"
|
|
873 |
|
|
874 |
^ self waitUntilFinishedWithTimeout:60*60*24000
|
|
875 |
|
|
876 |
"Modified: / 01-08-2017 / 14:41:10 / stefan"
|
|
877 |
"Modified (comment): / 01-08-2017 / 17:15:55 / stefan"
|
23035
|
878 |
"Modified: / 30-05-2018 / 13:57:42 / Claus Gittinger"
|
21208
|
879 |
!
|
|
880 |
|
|
881 |
waitUntilFinishedWithTimeout:timeout
|
23035
|
882 |
<resource: #skipInDebuggersWalkBack>
|
|
883 |
|
21184
|
884 |
|processList|
|
|
885 |
|
21208
|
886 |
(finishSema waitWithTimeout:timeout) isNil ifTrue:[
|
|
887 |
"timed out"
|
|
888 |
^ nil.
|
|
889 |
].
|
21184
|
890 |
|
|
891 |
"have to wait until the shufflers have finished their work"
|
21208
|
892 |
processList := shufflerProcesses.
|
|
893 |
processList notEmptyOrNil ifTrue:[
|
21184
|
894 |
processList do:[:eachProcess |
|
|
895 |
eachProcess waitUntilTerminated.
|
|
896 |
].
|
|
897 |
shufflerProcesses := nil.
|
|
898 |
].
|
23035
|
899 |
|
|
900 |
"Modified: / 30-05-2018 / 13:57:39 / Claus Gittinger"
|
12890
|
901 |
! !
|
|
902 |
|
22198
|
903 |
!OSProcess::RemoteOSProcess class methodsFor:'documentation'!
|
|
904 |
|
|
905 |
documentation
|
|
906 |
"
|
|
907 |
Instances of OSProcess represent operating system processes that can be executed.
|
|
908 |
(as opposed to Smalltalk processes).
|
|
909 |
|
|
910 |
[author:]
|
|
911 |
cg
|
|
912 |
|
|
913 |
[instance variables:]
|
|
914 |
host String the host on which to execute the command
|
|
915 |
|
|
916 |
[class variables:]
|
|
917 |
|
|
918 |
[see also:]
|
|
919 |
Process
|
|
920 |
"
|
|
921 |
!
|
|
922 |
|
|
923 |
examples
|
|
924 |
"
|
|
925 |
[exBegin]
|
|
926 |
|outStream|
|
|
927 |
|
|
928 |
outStream := '' writeStream.
|
|
929 |
|
|
930 |
(OSProcess onHost:'exeptn')
|
|
931 |
command:'ls -l';
|
|
932 |
inStream:'abc' readStream;
|
|
933 |
outStream:outStream;
|
|
934 |
lineWise:true;
|
|
935 |
execute.
|
|
936 |
|
|
937 |
outStream contents
|
|
938 |
[exEnd]
|
|
939 |
"
|
|
940 |
! !
|
|
941 |
|
|
942 |
!OSProcess::RemoteOSProcess methodsFor:'accessing'!
|
|
943 |
|
|
944 |
host
|
|
945 |
^ host
|
|
946 |
!
|
|
947 |
|
|
948 |
host:aHostName
|
|
949 |
host := aHostName.
|
|
950 |
! !
|
|
951 |
|
|
952 |
!OSProcess::RemoteOSProcess methodsFor:'private'!
|
|
953 |
|
|
954 |
startCommand
|
|
955 |
"the 'real' command"
|
22199
|
956 |
|
|
957 |
"for now, always use ssh;
|
|
958 |
later, this should be configurable,
|
|
959 |
which remote mechanism is to be used per host"
|
|
960 |
|
22198
|
961 |
^ 'ssh %1 "%2"' bindWith:host with:command
|
|
962 |
! !
|
|
963 |
|
12890
|
964 |
!OSProcess class methodsFor:'documentation'!
|
|
965 |
|
19298
|
966 |
version
|
|
967 |
^ '$Header$'
|
|
968 |
!
|
|
969 |
|
12890
|
970 |
version_CVS
|
19286
|
971 |
^ '$Header$'
|
12890
|
972 |
! !
|
19286
|
973 |
|
21172
|
974 |
|
|
975 |
OSProcess initialize!
|