PipeStream.st
author claus
Fri, 16 Jul 1993 11:39:45 +0200
changeset 1 a27a279701f8
child 2 6526dde5f3ac
permissions -rw-r--r--
Initial revision

"
 COPYRIGHT (c) 1989-93 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:''
       classVariableNames:''
       poolDictionaries:''
       category:'Streams-External'
!

PipeStream comment:'

COPYRIGHT (c) 1989-93 by Claus Gittinger
              All Rights Reserved

%W% %E%
'!

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

!PipeStream class methodsFor:'instance creation'!

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

    ^ (self basicNew) writingTo:command

    "PipeStream writingTo:'sort'"
!

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

    ^ (self basicNew) readingFrom:command

    "PipeStream readingFrom:'ls'"
! !

!PipeStream methodsFor:'instance release'!

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

%{  /* NOCONTEXT */
#ifndef transputer
    pclose(MKFD(_INST(filePointer)));
#endif
%}
! !

!PipeStream methodsFor:'private'!

openPipeFor:commandString withMode:mode
    "open a pipe to commandString; mode may be 'r' or 'w'"

    |retVal|

    unBuffered := true.
%{
#ifndef transputer
    {
        FILE *f;
        extern OBJ ErrorNumber;
        extern errno;

        if (_isString(commandString) && _isString(mode)) {
            f = (FILE *)popen((char *) _stringVal(commandString),
			      (char *) _stringVal(mode));
            if (f == NULL) {
                ErrorNumber = _MKSMALLINT(errno);
            } else {
                _INST(filePointer) = MKOBJ(f);
                retVal = self;
            }
        }
    }
#endif
%}
.
    retVal notNil ifTrue:[
        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'
! !

!PipeStream methodsFor:'redefined basic'!

size
    "redefined since pipes have no size"

    ^ self shouldNotImplement
!

position:newpos
    "redefined since pipes cannot be positioned"

    ^ self shouldNotImplement
!

position
    "redefined since pipes have no position"

    ^ self shouldNotImplement
! !