PipeStream.st
changeset 22975 6b24e729a7d9
parent 22974 f1df0b1303ab
child 22980 8ea9d4f9983a
equal deleted inserted replaced
22974:f1df0b1303ab 22975:6b24e729a7d9
   192 
   192 
   193     [see also:]
   193     [see also:]
   194 	ExternalStream FileStream Socket
   194 	ExternalStream FileStream Socket
   195 	OperatingSystem
   195 	OperatingSystem
   196 "
   196 "
       
   197 !
       
   198 
       
   199 examples
       
   200 "
       
   201   reading:
       
   202                                                     [exBegin]
       
   203     |p output|
       
   204 
       
   205     p := PipeStream readingFrom:'ls -l'.
       
   206     output := p upToEnd.
       
   207     p close.
       
   208     Transcript showCR:output
       
   209                                                     [exEnd]
       
   210 
       
   211   bidirectional:
       
   212                                                     [exBegin]
       
   213     |p|
       
   214 
       
   215     p := PipeStream bidirectionalFor:'sed -e s/Hello/Greetings/'.
       
   216     p nextPutLine:'bla'; flush.
       
   217     Transcript showCR:p nextLine.
       
   218     p nextPutLine:'foo Hello'.
       
   219     Transcript showCR:p nextLine.
       
   220     p nextPutLine:'bar'.
       
   221     Transcript showCR:p nextLine.
       
   222     p close.
       
   223                                                     [exEnd]
       
   224                                                 
       
   225   error output is on my stderr:
       
   226                                                     [exBegin]
       
   227     |p|
       
   228 
       
   229     p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2'.
       
   230     Transcript showCR:p upToEnd.
       
   231     p close.
       
   232                                                     [exEnd]
       
   233                                                 
       
   234   error output is included:
       
   235                                                     [exBegin]
       
   236     |p|
       
   237 
       
   238     p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2' errorDisposition:#stdout.
       
   239     Transcript showCR:p upToEnd.
       
   240     p close.
       
   241                                                     [exEnd]
       
   242 
       
   243   error output is separate:
       
   244                                                     [exBegin]
       
   245     |p errStream|
       
   246 
       
   247     errStream := '' writeStream.
       
   248     p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2' errorDisposition:errStream.
       
   249     Transcript showCR:'output:'; showCR:p upToEnd; cr.
       
   250     p close.
       
   251     Transcript showCR:'error:'; showCR:errStream contents; cr.
       
   252                                                     [exEnd]
       
   253 "
   197 ! !
   254 ! !
   198 
   255 
   199 !PipeStream class methodsFor:'initialization'!
   256 !PipeStream class methodsFor:'initialization'!
   200 
   257 
   201 initialize
   258 initialize
   414 
   471 
   415         p := PipeStream readingFrom:'dir'.
   472         p := PipeStream readingFrom:'dir'.
   416         Transcript showCR:p upToEnd.
   473         Transcript showCR:p upToEnd.
   417         p close
   474         p close
   418    "
   475    "
       
   476 !
       
   477 
       
   478 readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream
       
   479     "create and return a new pipeStream which can read from the unix command
       
   480      given by commandString.
       
   481      errorDisposition may be a stream or one of #discard, #inline or #stderr (default).
       
   482        #discard causes stderr to be discarded (/dev/null),
       
   483        #inline causes it to be merged into the PipeStream and
       
   484        #stderr causes it to be written to smalltalk's own stderr.
       
   485        a stream causes stderr to be sent to that stream (internal or external)
       
   486        Nil is treated like #stderr"
       
   487 
       
   488     ^ self
       
   489         readingFrom:commandString
       
   490         errorDisposition:errorDispositionSymbolOrStream
       
   491         inDirectory:nil
       
   492 
       
   493     "unix:
       
   494         PipeStream readingFrom:'ls -l'.
       
   495     "
       
   496 
       
   497     "
       
   498         |p|
       
   499 
       
   500         p := PipeStream readingFrom:'ls -l'.
       
   501         Transcript showCR:p nextLine.
       
   502         p close
       
   503     "
       
   504 
       
   505 
       
   506     "
       
   507         |p|
       
   508 
       
   509         p := PipeStream readingFrom:'echo error >&2'.
       
   510         Transcript showCR:p nextLine.
       
   511         p close
       
   512     "
       
   513 
       
   514     "
       
   515         |s|
       
   516         s := PipeStream readingFrom:'sh -c sleep\ 600'.
       
   517         (Delay forSeconds:2) wait.
       
   518         s abortAndClose
       
   519     "
       
   520 
       
   521     "
       
   522         |p|
       
   523         p := PipeStream readingFrom:'dir'.
       
   524         Transcript showCR:p nextLine.
       
   525         p close
       
   526     "
       
   527 
       
   528     "Windows:
       
   529         PipeStream readingFrom:'dir'.
       
   530     "
       
   531     "
       
   532         |p|
       
   533         p := PipeStream readingFrom:'dir'.
       
   534         Transcript showCR:p nextLine.
       
   535         p close
       
   536     "
       
   537 
       
   538     "Modified: 24.4.1996 / 09:09:25 / stefan"
   419 !
   539 !
   420 
   540 
   421 readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream environment:aShellEnvironmentOrNil inDirectory:aDirectory
   541 readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream environment:aShellEnvironmentOrNil inDirectory:aDirectory
   422     "similar to #readingFrom, but changes the directory while
   542     "similar to #readingFrom, but changes the directory while
   423      executing the command. Use this if a command is to be
   543      executing the command. Use this if a command is to be