#BUGFIX by stefan
class: StandaloneStartup class
changed:
#getCurrentBinaryPathFromRegistry
#getCurrentIDFromRegistry
#getIDOfRunningApplicationFromRegistryEntry
close registry keys
"{ Encoding: utf8 }"
"
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
"{ Package: 'stx:libbasic' }"
"{ NameSpace: Smalltalk }"
NonPositionableExternalStream subclass:#PipeStream
instanceVariableNames:'commandString osProcess'
classVariableNames:'BrokenPipeSignal'
poolDictionaries:''
category:'Streams-External'
!
!PipeStream primitiveDefinitions!
%{
#include "stxOSDefs.h"
#if defined(__win32__)
# undef UNIX_LIKE
# define MSDOS_LIKE
#endif
#ifndef _STDIO_H_INCLUDED_
# include <stdio.h>
# define _STDIO_H_INCLUDED_
#endif
#ifndef _ERRNO_H_INCLUDED_
# include <errno.h>
# define _ERRNO_H_INCLUDED_
#endif
#ifndef transputer
# include <sys/types.h>
# include <sys/stat.h>
#endif
/*
* on some systems errno is a macro ... check for it here
*/
#ifndef errno
extern errno;
#endif
#ifdef LINUX
# define BUGGY_STDIO_LIB
#endif
%}
! !
!PipeStream primitiveFunctions!
%{
/*
* no longer needed - popen is useless ...
*/
#undef NEED_POPEN_WITH_VFORK
/*
* some systems (i.e. ultrix) use fork;
* we're better off with a popen based on vfork ...
*/
#ifdef NEED_POPEN_WITH_VFORK
static int popen_pid = 0;
FILE *
popen(command, type)
/* const */ char *command;
/* const */ char *type;
{
int pipes[2];
int itype = (strcmp(type, "w") == 0 ? 1 : 0);
if (pipe(pipes) == -1)
return NULL;
switch (popen_pid = vfork()) {
case -1:
(void)close(pipes[0]);
(void)close(pipes[1]);
return NULL;
case 0:
if (itype) {
dup2(pipes[0], fileno(stdin));
close(pipes[1]);
} else {
dup2(pipes[1], fileno(stdout));
close(pipes[0]);
}
execl("/bin/sh", "/bin/sh", "-c", command, 0);
console_fprintf(stderr, "PipeStream [warning]: execlp failed in popen\n");
_exit(-1);
/* NOTREACHED */
default:
if (itype) {
close(pipes[0]);
return fdopen(pipes[1], "w");
} else {
close(pipes[1]);
return fdopen(pipes[0], "r");
}
}
}
int
pclose(str)
FILE *str;
{
int pd = 0;
int status;
int err;
err = fclose(str);
do {
if ((pd = wait(&status)) == -1)
{
err = EOF;
break;
}
} while (pd != popen_pid);
if (err == EOF)
return -1;
if (status)
status >>= 8; /* exit status in high byte */
return status;
}
#endif /* NEED_POPEN_WITH_VFORK */
%}
! !
!PipeStream class methodsFor:'documentation'!
copyright
"
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
!
documentation
"
Pipestreams allow reading or writing from/to a unix or dos command.
For example, to get a stream reading the output of an 'ls -l'
command, a PipeStream can be created with:
PipeStream readingFrom:'ls -l'
the characters of the command's output can be read using the
standard stream messages, such as next, nextLine etc.
Example for writing to a command:
PipeStream writingTo:'cat >/tmp/x'
Bidirectional pipestreams (supporting both reading an writing) may be used for filters:
PipeStream bidirectionalFor:'sed -u -e ''s/Hello/Greetings/'''
Buffered pipes do not work with Linux - the stdio library seems to be
buggy (trying to restart the read ...)
[author:]
Claus Gittinger
[see also:]
ExternalStream FileStream Socket
OperatingSystem
"
!
examples
"
reading:
[exBegin]
|p output|
p := PipeStream readingFrom:'ls -l'.
output := p upToEnd.
p close.
Transcript showCR:output
[exEnd]
bidirectional:
(must add a '-u' argument, because otherwise sed will read a big junk
waiting for more input, before generating any output)
Notice: OSX sed does not support a '-u' option (sigh)
[exBegin]
|p|
p := PipeStream bidirectionalFor:'sed -u -e s/Hello/Greetings/'.
p nextPutLine:'bla'.
Transcript showCR:p nextLine.
p nextPutLine:'foo Hello'.
Transcript showCR:p nextLine.
p nextPutLine:'bar'.
Transcript showCR:p nextLine.
p close.
[exEnd]
error output is on my stderr:
[exBegin]
|p|
p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2'.
Transcript showCR:p upToEnd.
p close.
[exEnd]
error output is included:
[exBegin]
|p|
p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2' errorDisposition:#stdout.
Transcript showCR:p upToEnd.
p close.
[exEnd]
error output is separate:
[exBegin]
|p errStream|
errStream := '' writeStream.
p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2' errorDisposition:errStream.
Transcript showCR:'output:'; showCR:p upToEnd; cr.
p close.
Transcript showCR:'error:'; showCR:errStream contents; cr.
[exEnd]
"
! !
!PipeStream class methodsFor:'initialization'!
initialize
"setup the signal"
BrokenPipeSignal isNil ifTrue:[
BrokenPipeSignal := WriteError newSignalMayProceed:true.
BrokenPipeSignal nameClass:self message:#brokenPipeSignal.
BrokenPipeSignal notifierString:'write on a pipe with no one to read'.
]
! !
!PipeStream class methodsFor:'instance creation'!
bidirectionalFor:commandString
"create and return a new bidirectonal pipeStream which can both be written to
and read from the unix command given by commandString.
The commands error output is send to my own error output."
^ self
bidirectionalFor:commandString
errorDisposition:#stderr
inDirectory:nil
"
|p|
p := PipeStream bidirectionalFor:'cat -u'.
p nextPutAll:'Wer ist der Bürgermeister von Wesel'; cr.
Transcript showCR:p nextLine.
p close
"
"
|p|
p := PipeStream bidirectionalFor:'sed -u -e ''s/Hello/Greetings/'''.
p nextPutAll:'Hello world'; cr.
p shutDownOutput.
Transcript showCR:p nextLine.
p close
"
"
|p|
p := PipeStream bidirectionalFor:'wc'.
p nextPutAll:'Hello world'; cr.
p shutDownOutput.
Transcript showCR:p nextLine.
p close
"
"Modified (comment): / 22-05-2018 / 10:46:32 / Stefan Vogel"
!
bidirectionalFor:commandString errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectory
"create and return a new bidirectonal pipeStream which can both be written to
and read from the unix command given by commandString.
The directory will be changed to aDirectory while
executing the command. Use this if a command is to be
executed in another directory, to avoid any OS dependencies
in your code.
errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be written to smalltalk's own stdout
#stderr causes it to be written to smalltalk's own stderr.
a stream causes stderr to be sent to that stream (internal or external)
Nil is treated like #stderr"
^ self basicNew
openPipeFor:commandString
withMode:#'r+'
errorDisposition:errorDispositionSymbolOrStream
inDirectory:aDirectory
!
readingFrom:commandString
"create and return a new pipeStream which can read from the unix command
given by commandString.
The command's error output is send to my own error output."
^ self
readingFrom:commandString
errorDisposition:#stderr
inDirectory:nil
"unix:
PipeStream readingFrom:'ls -l'.
"
"
|p|
p := PipeStream readingFrom:'ls -l'.
Transcript showCR:p nextLine.
p close
"
"
|p|
p := PipeStream readingFrom:'echo error >&2'.
Transcript showCR:p nextLine.
p close
"
"
|s|
s := PipeStream readingFrom:'sh -c sleep\ 600'.
(Delay forSeconds:2) wait.
s abortAndClose
"
"
|p|
p := PipeStream readingFrom:'dir'.
Transcript showCR:p nextLine.
p close
"
"Windows:
PipeStream readingFrom:'dir'.
"
"
|p|
p := PipeStream readingFrom:'dir'.
Transcript showCR:p nextLine.
p close
"
"Modified: 24.4.1996 / 09:09:25 / stefan"
!
readingFrom:commandString environment:aShellEnvironmentOrNil
"create and return a new pipeStream which can read from the unix command
given by commandString.
The command's error output is send to my own error output."
^ self
readingFrom:commandString
errorDisposition:#stderr
environment:aShellEnvironmentOrNil
inDirectory:nil
"unix:
PipeStream readingFrom:'ls -l'.
"
"
|p|
p := PipeStream readingFrom:'ls -l'.
Transcript showCR:p nextLine.
p close
"
"
|p|
p := PipeStream readingFrom:'echo error >&2'.
Transcript showCR:p nextLine.
p close
"
"
|s|
s := PipeStream readingFrom:'sh -c sleep\ 600'.
(Delay forSeconds:2) wait.
s abortAndClose
"
"
|p|
p := PipeStream readingFrom:'dir'.
Transcript showCR:p nextLine.
p close
"
"Windows:
PipeStream readingFrom:'dir'.
"
"
|p|
p := PipeStream readingFrom:'dir'.
Transcript showCR:p nextLine.
p close
"
"Modified: 24.4.1996 / 09:09:25 / stefan"
!
readingFrom:commandString environment:aShellEnvironmentOrNil inDirectory:aDirectory
"similar to #readingFrom, but changes the directory while
executing the command. Use this if a command is to be
executed in another directory, to avoid any OS dependencies
in your code.
The command's error output is send to my own error output."
^ self
readingFrom:commandString
errorDisposition:#stderr
environment:aShellEnvironmentOrNil
inDirectory:aDirectory
" UNIX:
|p|
p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'.
Transcript showCR:p upToEnd.
p close
"
"WINDOOF:
|p|
p := PipeStream readingFrom:'dir'.
Transcript showCR:p upToEnd.
p close
"
!
readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream
"create and return a new pipeStream which can read from the unix command
given by commandString.
errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be merged into the PipeStream and
#stderr causes it to be written to smalltalk's own stderr.
a stream causes stderr to be sent to that stream (internal or external)
Nil is treated like #stderr"
^ self
readingFrom:commandString
errorDisposition:errorDispositionSymbolOrStream
inDirectory:nil
"unix:
PipeStream readingFrom:'ls -l'.
"
"
|p|
p := PipeStream readingFrom:'ls -l'.
Transcript showCR:p nextLine.
p close
"
"
|p|
p := PipeStream readingFrom:'echo error >&2'.
Transcript showCR:p nextLine.
p close
"
"
|s|
s := PipeStream readingFrom:'sh -c sleep\ 600'.
(Delay forSeconds:2) wait.
s abortAndClose
"
"
|p|
p := PipeStream readingFrom:'dir'.
Transcript showCR:p nextLine.
p close
"
"Windows:
PipeStream readingFrom:'dir'.
"
"
|p|
p := PipeStream readingFrom:'dir'.
Transcript showCR:p nextLine.
p close
"
"Modified: 24.4.1996 / 09:09:25 / stefan"
!
readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream environment:aShellEnvironmentOrNil inDirectory:aDirectory
"similar to #readingFrom, but changes the directory while
executing the command. Use this if a command is to be
executed in another directory, to avoid any OS dependencies
in your code.
errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be merged into the PipeStream and
#stderr causes it to be written to smalltalk's own stderr.
a stream causes stderr to be sent to that stream (internal or external)
Nil is treated like #stderr"
^ self basicNew
openPipeFor:commandString
withMode:#r
errorDisposition:errorDispositionSymbolOrStream
environment:aShellEnvironmentOrNil
inDirectory:aDirectory
"
|p|
p := PipeStream readingFrom:'bla' errorDisposition:Transcript inDirectory:nil.
Transcript showCR:p nextLine.
p close
"
"
|p|
p := PipeStream readingFrom:'bla' errorDisposition:#inline inDirectory:nil.
Transcript showCR:p nextLine.
p close
"
!
readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectory
"similar to #readingFrom, but changes the directory while
executing the command. Use this if a command is to be
executed in another directory, to avoid any OS dependencies
in your code.
errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be merged into the PipeStream and
#stderr causes it to be written to smalltalk's own stderr.
a stream causes stderr to be sent to that stream (internal or external)
Nil is treated like #stderr"
^ self basicNew
openPipeFor:commandString
withMode:#r
errorDisposition:errorDispositionSymbolOrStream
environment:nil
inDirectory:aDirectory
"
|p|
p := PipeStream readingFrom:'bla' errorDisposition:Transcript inDirectory:nil.
Transcript showCR:p nextLine.
p close
"
"
|p|
p := PipeStream readingFrom:'bla' errorDisposition:#inline inDirectory:nil.
Transcript showCR:p nextLine.
p close
"
"Modified: / 22-05-2018 / 22:13:08 / Stefan Vogel"
!
readingFrom:commandString inDirectory:aDirectory
"similar to #readingFrom, but changes the directory while
executing the command. Use this if a command is to be
executed in another directory, to avoid any OS dependencies
in your code.
The command's error output is send to my own error output."
^ self
readingFrom:commandString
errorDisposition:#stderr
inDirectory:aDirectory
" UNIX:
|p|
p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'.
Transcript showCR:p upToEnd.
p close
"
"WINDOOF:
|p|
p := PipeStream readingFrom:'dir'.
Transcript showCR:p upToEnd.
p close
"
!
writingTo:commandString
"create and return a new pipeStream which can write to the unix command
given by command."
^ self
writingTo:commandString errorDisposition:#stderr inDirectory:nil
"unix:
PipeStream writingTo:'sort'
"
!
writingTo:commandString environment:aShellEnvironmentOrNil
"create and return a new pipeStream which can write to the unix command
given by command."
^ self
writingTo:commandString
errorDisposition:#stderr
environment:aShellEnvironmentOrNil
inDirectory:nil
"unix:
PipeStream writingTo:'sort'
"
!
writingTo:commandString environment:aShellEnvironmentOrNil inDirectory:aDirectory
"create and return a new pipeStream which can write to the unix command
given by commandString. The command is executed in the given directory."
^ self
writingTo:commandString
errorDisposition:#stderr
environment:aShellEnvironmentOrNil
inDirectory:aDirectory
"unix:
PipeStream writingTo:'sort'
"
!
writingTo:commandString errorDisposition:errorDispositionSymbolOrStream environment:aShellEnvironmentOrNil inDirectory:aDirectory
"similar to #writingTo, but changes the directory while
executing the command. Use this if a command is to be
executed in another directory, to avoid any OS dependencies
in your code.
errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be written to smalltalk's own stdout
#stderr causes it to be written to smalltalk's own stderr.
Nil is treated like #stderr"
^ self basicNew
openPipeFor:commandString
withMode:#w
errorDisposition:errorDispositionSymbolOrStream
environment:aShellEnvironmentOrNil
inDirectory:aDirectory
!
writingTo:commandString errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectory
"similar to #writingTo, but changes the directory while
executing the command. Use this if a command is to be
executed in another directory, to avoid any OS dependencies
in your code.
errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be written to smalltalk's own stdout
#stderr causes it to be written to smalltalk's own stderr.
Nil is treated like #stderr"
^ self basicNew
openPipeFor:commandString
withMode:#w
errorDisposition:errorDispositionSymbolOrStream
inDirectory:aDirectory
!
writingTo:commandString inDirectory:aDirectory
"create and return a new pipeStream which can write to the unix command
given by commandString. The command is executed in the given directory."
^ self
writingTo:commandString
errorDisposition:#stderr
inDirectory:aDirectory
"unix:
PipeStream writingTo:'sort'
"
! !
!PipeStream class methodsFor:'Signal constants'!
brokenPipeSignal
"return the signal used to handle SIGPIPE unix-signals.
Since SIGPIPE is asynchronous, we can't decide which smalltalk process
should handle BrokenPipeSignal. So the system doesn't raise
BrokenPipeSignal for SIGPIPE any longer."
^ BrokenPipeSignal
"Modified: 24.9.1997 / 09:43:23 / stefan"
! !
!PipeStream class methodsFor:'utilities'!
outputFromCommand:aCommand
"open a pipe reading from aCommand and return the complete output as a string.
If the command cannot be executed, return nil.
The command's current directory will be the smalltalk current directory.
Only stdout is returned; stderr is ignored (sent to /dev/null)."
^ self outputFromCommand:aCommand inDirectory:nil
"
PipeStream outputFromCommand:'ls -l'
"
"Modified (comment): / 31-05-2018 / 08:56:06 / Claus Gittinger"
!
outputFromCommand:aCommand errorDisposition:errorDispositionSymbolOrStream
"open a pipe reading from aCommand and return the complete output as a string.
If the command cannot be executed, return nil.
The command will be executed in smalltalk's current directory.
errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be merged into the PipeStream and
#stderr causes it to be written to smalltalk's own stderr.
a stream causes stderr to be sent to that stream (internal or external)
Nil is treated like #stderr"
^ self outputFromCommand:aCommand errorDisposition:errorDispositionSymbolOrStream inDirectory:nil
"Created: / 25-09-2018 / 10:32:49 / Claus Gittinger"
!
outputFromCommand:aCommand errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectoryOrNil
"open a pipe reading from aCommand and return the complete output as a string.
If the command cannot be executed, return nil.
The current directory of the command will be aDirectoryOrNil
or the smalltalk's current directory (if nil).
errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be merged into the PipeStream and
#stderr causes it to be written to smalltalk's own stderr.
a stream causes stderr to be sent to that stream (internal or external)
Nil is treated like #stderr"
|p cmdOutput|
p := self readingFrom:aCommand errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectoryOrNil.
p isNil ifTrue:[^ nil].
[
cmdOutput := p contentsAsString.
] ensure:[
p close.
].
^ cmdOutput
"
PipeStream outputFromCommand:'ls -l' inDirectory:nil
PipeStream outputFromCommand:'ls -l' inDirectory:'/'
PipeStream outputFromCommand:'ls -l' inDirectory:'/etc'
"
!
outputFromCommand:aCommand inDirectory:aDirectoryOrNil
"open a pipe reading from aCommand and return the complete output as a string.
If the command cannot be executed, return nil.
The current directory of the command will be aDirectoryOrNil
or the smalltalk's current directory (if nil).
Any stderr output will be discarded (not included) in the returned cmdOutput."
|p cmdOutput|
p := self readingFrom:aCommand inDirectory:aDirectoryOrNil.
p isNil ifTrue:[^ nil].
[
cmdOutput := p contentsAsString.
] ensure:[
p close.
].
^ cmdOutput
"
PipeStream outputFromCommand:'ls -l' inDirectory:nil
PipeStream outputFromCommand:'ls -l' inDirectory:'/'
PipeStream outputFromCommand:'ls -l' inDirectory:'/etc'
PipeStream outputFromCommand:'echo hello >&2' inDirectory:'/etc'
"
"Modified (comment): / 31-05-2018 / 08:57:04 / Claus Gittinger"
! !
!PipeStream methodsFor:'accessing'!
commandString
"return the command string"
^ commandString
!
exitStatus
"return the exitStatus"
osProcess isNil ifTrue:[
^ nil.
].
^ osProcess exitStatus.
"Created: 28.12.1995 / 14:54:41 / stefan"
!
pid
"return pid"
osProcess isNil ifTrue:[
^ nil.
].
^ osProcess pid.
"Created: 28.12.1995 / 14:54:30 / stefan"
!
setCommandString:aString
"for OSProcess only (so we see the command in the streams monitor)"
commandString := aString
"Created: / 29-10-2018 / 15:14:24 / Claus Gittinger"
! !
!PipeStream methodsFor:'closing'!
abortAndClose
"close the Stream and terminate the command"
self unregisterForFinalization.
"terminate first under windows"
OperatingSystem isMSDOSlike ifTrue:[
self terminatePipeCommand.
self closeFile.
^ self.
].
"terminate last under unix"
self closeFile.
self terminatePipeCommand.
!
close
"low level close
This waits for the command to finish.
Use abortAndClose for a fast (nonBlocking) close."
self isOpen ifTrue:[
self shutDownOutput.
super close.
"/ wait for the pipe-command to terminate.
self waitForPipeCommandWithTimeout:nil.
"/ Maybe later, currently senders have to retrieve exitstatus manually:
"/ osProcess finishedWithSuccess ifFalse:[
"/ exitStatus := osProcess exitStatus.
"/ StreamError raiseRequestWith:exitStatus errorString:'pipe command failed with exit code: ', exitStatus code printString.
"/ ].
].
"Modified: / 12-09-1998 / 16:51:04 / cg"
"Modified: / 23-04-2018 / 18:25:04 / stefan"
"Modified (comment): / 22-05-2018 / 17:48:15 / Stefan Vogel"
!
shutDown
<resource: #obsolete>
"this is a historic leftover kept for backward compatibility.
The name collides with the same name in Socket, which does
not hard terminate the connection."
self abortAndClose.
!
shutDownOutput
"signal to the pipestream's command, that no more data will be sent"
|fd|
(self isOpen and:[mode ~~ #readonly]) ifTrue:[
self flush.
fd := self fileDescriptor.
fd notNil ifTrue:[
OperatingSystem shutdownBidirectionalPipeOutput:fd.
].
].
"Modified: / 22-05-2018 / 18:34:28 / Stefan Vogel"
! !
!PipeStream methodsFor:'finalization'!
finalize
"redefined to avoid blocking in close."
self abortAndClose
! !
!PipeStream methodsFor:'private'!
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition
environment:aShellEnvironmentOrNil inDirectory:aDirectory
"open a pipe to the OS command in commandString;
rwMode may be 'r' or 'w' or 'r+'.
errorDisposition controls where the stdErr output should go,
and may be one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be written to smalltalks own stdout and
#stderr causes it to be written to smalltalks own stderr.
Nil is treated like #stderr."
|pipeArray remotePipeEnd nullOutput errorNumber myPipeEnd result|
handle notNil ifTrue:[
"the pipe was already open ...
this should (can) not happen."
^ self errorAlreadyOpen
].
rwMode = #r ifTrue:[
mode := #readonly. didWrite := false.
position := 0. "only reading - can keep track of position"
] ifFalse:[rwMode = #'r+' ifTrue:[
mode := #readwrite. didWrite := true.
] ifFalse:[
mode := #writeonly. didWrite := true.
position := 0. "only writing - can keep track of position"
]].
lastErrorNumber := nil.
commandString := aCommandString.
"stdio lib does not work with blocking pipes and interrupts
for WIN, Linux, Solaris and probably any other UNIX"
buffered := false.
hitEOF := false.
binary := false.
osProcess := OSProcess new
command:aCommandString directory:aDirectory.
aShellEnvironmentOrNil notNil ifTrue:[
osProcess environment:aShellEnvironmentOrNil
].
mode == #readwrite ifTrue:[
pipeArray := self class makeBidirectionalPipe.
pipeArray isNil ifTrue:[
lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber.
^ self openError:errorNumber.
].
myPipeEnd := pipeArray at:1.
remotePipeEnd := pipeArray at:2.
osProcess inStream:remotePipeEnd.
osProcess outStream:remotePipeEnd.
] ifFalse:[
pipeArray := self class makePipe.
pipeArray isNil ifTrue:[
lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber.
^ self openError:errorNumber.
].
mode == #readonly ifTrue:[
"redirect stdout of subprocess to write to pipe"
myPipeEnd := pipeArray at:1.
remotePipeEnd := pipeArray at:2.
osProcess outStream:remotePipeEnd.
] ifFalse:[
"redirect stdin of subprocess to read from pipe"
myPipeEnd := pipeArray at:2.
remotePipeEnd := pipeArray at:1.
osProcess inStream:remotePipeEnd.
].
].
errorDisposition == #discard ifTrue:[
nullOutput := Filename nullDevice writeStream.
osProcess errorStream:nullOutput.
] ifFalse:[(errorDisposition == #inline or:[errorDisposition == #stdout]) ifTrue:[
osProcess errorStream:osProcess outStream.
] ifFalse:[(errorDisposition == #stderr or:[errorDisposition isNil]) ifTrue:[
osProcess errorStream:Stderr.
] ifFalse:[errorDisposition isStream ifTrue:[
osProcess errorStream:errorDisposition.
]]]].
mode ~~ #readonly ifTrue:[
osProcess terminateActionBlock:[
"writing doesn't make sense - there is no reader any longer"
mode == #readwrite ifTrue:[
"... but allow to read the rest of the command's output"
self shutDownOutput.
] ifFalse:[mode == #writeonly ifTrue:[
self unregisterForFinalization.
self closeFile.
]].
].
].
result := osProcess startProcess.
"subprocess has been created.
close unused filedescriptors"
remotePipeEnd notNil ifTrue:[
remotePipeEnd close.
].
nullOutput notNil ifTrue:[
nullOutput close
].
result ifTrue:[
"successful creation of subprocess"
handle := myPipeEnd handle.
handleType := myPipeEnd handleType.
myPipeEnd unregisterForFinalization. "make sure filedescriptor is not closed by finalizer"
myPipeEnd := nil.
] ifFalse:[
"the pipe open failed for some reason ...
... this may be either due to an invalid command string,
or due to the system running out of memory (when forking
the unix process)"
lastErrorNumber := OperatingSystem lastErrorNumber.
myPipeEnd close.
^ self openError:lastErrorNumber.
].
self registerForFinalization.
"Modified: / 23-04-1996 / 17:05:59 / stefan"
"Modified: / 28-01-1998 / 14:47:34 / md"
"Created: / 19-05-1999 / 12:28:54 / cg"
"Modified (comment): / 25-01-2018 / 19:40:27 / mawalch"
"Modified: / 12-03-2019 / 22:03:02 / Claus Gittinger"
!
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition inDirectory:aDirectory
"open a pipe to the OS command in commandString;
rwMode may be 'r' or 'w' or 'r+'.
errorDisposition controls where the stdErr output should go,
and may be one of #discard, #inline or #stderr (default).
#discard causes stderr to be discarded (/dev/null),
#inline causes it to be written to smalltalks own stdout and
#stderr causes it to be written to smalltalks own stderr.
Nil is treated like #stderr"
^ self
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition
environment:nil inDirectory:aDirectory
!
terminatePipeCommand
osProcess notNil ifTrue:[
osProcess terminateGroup.
].
!
waitForPipeCommandWithTimeout:seconds
"wait for the pipe command to terminate itself.
Return true, if a timeout occurred."
osProcess notNil ifTrue:[
^ osProcess finishSema waitWithTimeout:seconds.
].
^ false
! !
!PipeStream methodsFor:'testing'!
finishedWithSuccess
^ osProcess notNil and:[osProcess finishedWithSuccess].
"Created: / 22-05-2018 / 18:17:11 / Stefan Vogel"
!
isPipeStream
^ true
! !
!PipeStream class methodsFor:'documentation'!
version
^ '$Header$'
!
version_CVS
^ '$Header$'
! !
PipeStream initialize!