eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'PipeStream':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: PipeStream


Inheritance:

   Object
   |
   +--Stream
      |
      +--PeekableStream
         |
         +--PositionableStream
            |
            +--WriteStream
               |
               +--ReadWriteStream
                  |
                  +--ExternalStream
                     |
                     +--NonPositionableExternalStream
                        |
                        +--PipeStream
                           |
                           +--UnixPTYStream

Package:
stx:libbasic
Category:
Streams-External
Version:
rev: 1.149 date: 2019/03/12 21:05:44
user: cg
file: PipeStream.st directory: libbasic
module: stx stc-classLibrary: libbasic
Author:
Claus Gittinger

Description:


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 ...)


Related information:

    ExternalStream
    FileStream
    Socket
    OperatingSystem

Class protocol:

Signal constants
o  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.

initialization
o  initialize
setup the signal

instance creation
o  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.

o  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

o  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.

o  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.

o  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.

o  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

o  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

o  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

o  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.

o  writingTo: commandString
create and return a new pipeStream which can write to the unix command
given by command.

o  writingTo: commandString environment: aShellEnvironmentOrNil
create and return a new pipeStream which can write to the unix command
given by command.

o  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.

o  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

o  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

o  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.

utilities
o  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).

usage example(s):

     PipeStream outputFromCommand:'ls -l'

o  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

o  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

usage example(s):

     PipeStream outputFromCommand:'ls -l' inDirectory:nil
     PipeStream outputFromCommand:'ls -l' inDirectory:'/'
     PipeStream outputFromCommand:'ls -l' inDirectory:'/etc'

o  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.

usage example(s):

     PipeStream outputFromCommand:'ls -l' inDirectory:nil
     PipeStream outputFromCommand:'ls -l' inDirectory:'/'
     PipeStream outputFromCommand:'ls -l' inDirectory:'/etc'
     PipeStream outputFromCommand:'echo hello >&2' inDirectory:'/etc'


Instance protocol:

accessing
o  commandString
return the command string

o  exitStatus
return the exitStatus

o  pid
return pid

o  setCommandString: aString
for OSProcess only (so we see the command in the streams monitor)

closing
o  abortAndClose
close the Stream and terminate the command

o  close
low level close
This waits for the command to finish.
Use abortAndClose for a fast (nonBlocking) close.

o  shutDown
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.

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  shutDownOutput
signal to the pipestream's command, that no more data will be sent

finalization
o  finalize
redefined to avoid blocking in close.

private
o  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.

o  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

o  terminatePipeCommand

o  waitForPipeCommandWithTimeout: seconds
wait for the pipe command to terminate itself.
Return true, if a timeout occurred.

testing
o  finishedWithSuccess

o  isPipeStream


Examples:


reading:
  |p output|

  p := PipeStream readingFrom:'ls -l'.
  output := p upToEnd.
  p close.
  Transcript showCR:output
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)
  |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.
error output is on my stderr:
  |p|

  p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2'.
  Transcript showCR:p upToEnd.
  p close.
error output is included:
  |p|

  p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2' errorDisposition:#stdout.
  Transcript showCR:p upToEnd.
  p close.
error output is separate:
  |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.


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Fri, 29 Mar 2024 05:26:06 GMT