PipeStream.st
author claus
Fri, 05 Aug 1994 02:55:07 +0200
changeset 92 0c73b48551ac
parent 88 81dacba7a63a
child 93 e31220cb391f
permissions -rw-r--r--
*** empty log message ***

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

NonPositionableExternalStream subclass:#PipeStream
       instanceVariableNames:'commandString'
       classVariableNames:'BrokenPipeSignal'
       poolDictionaries:''
       category:'Streams-External'
!

PipeStream comment:'
COPYRIGHT (c) 1989 by Claus Gittinger
              All Rights Reserved
'!

!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.
"
!

version
"
$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.11 1994-06-02 16:21:16 claus Exp $
"
!

documentation
"
Pipestreams allow reading or writing from/to a unix 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 commands output can be read using the
standard stream messages as next, nextLine etc.

If a writing pipeStream is written to, after the command has finished,
UNIX will generate an error-signal (SIGPIPE), which will raise the
BrokenPipeSignal. 
Thus, to handle this condition correctly, the following code is suggested:

        |p|
        p := PipeStream writingTo:'echo hello'.
        PipeStream brokenPipeSignal handle:[:ex |
            'broken pipe' printNewline.
            p shutDown.
            ex return
        ] do:[
            p nextPutLine:'oops'.
           'after write' printNewline.
            p close.
           'after close' printNewline
        ]

Notice, that if the Stream is buffered, the Signal may occur some time after
the write - or even at close time; to avoid a recursive signal in the exception
handler, a shutDown is useful there.
"
! !

%{
#include <stdio.h>
#include <errno.h>
#ifndef transputer
# include <sys/types.h>
# include <sys/stat.h>
#endif
%}

!PipeStream class methodsFor:'initialization'!

initialize
    "setup the signal"

    BrokenPipeSignal isNil ifTrue:[
        BrokenPipeSignal := (Signal new) mayProceed:true.
        BrokenPipeSignal notifierString:'write on a pipe with no one to read'.
    ]
! !

!PipeStream class methodsFor:'signal access'!

brokenPipeSignal
    "return the signal used to handle SIGPIPE unix-signals"

    ^ BrokenPipeSignal
! !

!PipeStream class methodsFor:'instance creation'!

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

    ^ (self basicNew) writingTo:commandString

    "PipeStream writingTo:'sort'"
!

readingFrom:commandString
    "create and return a new pipeStream which can read from the unix command
     given by command."

    ^ (self basicNew) readingFrom:commandString

    "PipeStream readingFrom:'ls -l'"
! !

!PipeStream methodsFor:'instance release'!

shutDown
    "close the Stream, ignoring any broken-pipe errors"

    BrokenPipeSignal catch:[self closeFile]
!

closeFile
    "low level close - redefined since we close a pipe here"

%{  /* UNLIMITEDSTACK */
#ifndef transputer
    extern int _immediateInterrupt;
    int savInt;

    if (_INST(filePointer) != nil) {
        savInt = _immediateInterrupt;
        /*
         * allow interrupt even when blocking here ...
         */
        _immediateInterrupt = 1;
        pclose(MKFD(_INST(filePointer)));
        _immediateInterrupt = savInt;
    }
#endif
%}
! !

!PipeStream methodsFor:'private'!

openPipeFor:aCommandString withMode:mode
    "open a pipe to the unix command in aCcommandString; mode may be 'r' or 'w'"

    |retVal|

%{  /* STACK: 32000 */
#ifndef transputer
    {
        FILE *f;
        extern errno;
        extern int _immediateInterrupt;
        int savInt;

        if (__isString(aCommandString) && __isString(mode)) {
            savInt = _immediateInterrupt;
            _immediateInterrupt = 1;
            do {
#ifdef LINUX
                /* LINUX returns a non-NULL f even when interrupted */
                errno = 0;
                f = (FILE *)popen((char *) _stringVal(aCommandString),
                                  (char *) _stringVal(mode));
                if (errno == EINTR)
                    f = NULL;
#else
                f = (FILE *)popen((char *) _stringVal(aCommandString),
                                  (char *) _stringVal(mode));
#endif
            } while ((f == NULL) && (errno == EINTR));
            _immediateInterrupt = savInt;
            if (f == NULL) {
                ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
            } else {
                _INST(filePointer) = MKOBJ(f);
                retVal = self;
            }
        }
    }
#endif
%}
.
    retVal notNil ifTrue:[
        commandString := aCommandString.
        buffered := true.
        Lobby register:self
    ].
    ^ retVal
!

readingFrom:command
    "setup the receiver to read from command"

    self readonly.
    ^ self openPipeFor:command withMode:'r'
!

writingTo:command
    "setup the receiver to write to command"

    self writeonly.
    ^ self openPipeFor:command withMode:'w'
! !