PipeStream.st
author Claus Gittinger <cg@exept.de>
Fri, 27 Oct 1995 16:56:51 +0100
changeset 451 5aab4706f2fd
parent 437 a005e97d261e
child 475 b57530aa1b0a
permissions -rw-r--r--
.

"
 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

$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.34 1995-09-16 17:13:53 claus Exp $
'!

!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.34 1995-09-16 17:13:53 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.
"
! !

!PipeStream primitiveDefinitions!

%{

#include <stdio.h>
#define _STDIO_H_INCLUDED_

#include <errno.h>
#define _ERRNO_H_INCLUDED_

#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

%}
! !

!PipeStream primitiveFunctions!

%{

/*
 * some systems (i.e. ultrix) use fork;
 * were 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);
	fprintf(stderr, "XRN Error: failed the execlp\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

%}
! !

!PipeStream class methodsFor:'initialization'!

initialize
    "setup the signal"

    BrokenPipeSignal isNil ifTrue:[
	BrokenPipeSignal := ErrorSignal newSignalMayProceed:true.
	BrokenPipeSignal nameClass:self message:#brokenPipeSignal.
	BrokenPipeSignal notifierString:'write on a pipe with no one to read'.
    ]
! !

!PipeStream class methodsFor:'Signal constants'!

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:'accessing'!

commandString
    "return the command string"

    ^ commandString
! !

!PipeStream methodsFor:'instance release'!

disposed
    "redefined to avoid blocking in close."

    self shutDown
!

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

    BrokenPipeSignal catch:[
	Lobby unregister:self.
	self closeFileDescriptor
    ]
!

closeFile
    "low level close - redefined since we close a pipe here.
     This waits for the command to finish. 
     Use shutDown for a fast (nonBlocking) close."

%{  /* UNLIMITEDSTACK */
#ifndef transputer
    OBJ fp;

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

closeFileDescriptor
    "alternative very low level close 
     This closes the underlying OS-fileDescriptor 
     - and will NOT write any buffered data to the stream.
     You have been warned."

%{  /* NOCONTEXT */

    OBJ fp;
    FILE *f;

    if ((fp = _INST(filePointer)) != nil) {
	_INST(filePointer) = nil;
	__BEGIN_INTERRUPTABLE__
	f = MKFD(fp);
	close(fileno(f));
	__END_INTERRUPTABLE__
    }
%}
! !

!PipeStream methodsFor:'private'!

atEnd
    "return true, if position is at end"

%{  /* NOCONTEXT */
    FILE *f;
    OBJ t;
    OBJ _true = true;
    int c;

    if (_INST(hitEOF) == _true) {
	RETURN (_true);
    }
    if ((t = _INST(filePointer)) != nil) {
	f = MKFD(t);
	if (feof(f)) {
	    _INST(hitEOF) = true;
	    RETURN (true);
	}
	clearerr(f);
	RETURN ( false );
    }
%}
.
    ^ super atEnd
! !

!PipeStream methodsFor:'private'!

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

    |retVal|

    filePointer notNil ifTrue:[
	"the pipe was already open ...
	 this should (can) not happen."
	^ self errorOpen
    ].

%{  /* STACK: 32000 */
#ifndef transputer
    FILE *f;

    _INST(lastErrorNumber) = nil;

    if (__isString(aCommandString) && __isString(mode)) {
	__BEGIN_INTERRUPTABLE__
	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));
	__END_INTERRUPTABLE__

	if (f == NULL) {
	    _INST(lastErrorNumber) = _MKSMALLINT(errno);
	} else {
	    clearerr(f);
	    _INST(filePointer) = MKOBJ(f);
	    retVal = self;
	}
    }
#endif
%}.
    retVal notNil ifTrue:[
	commandString := aCommandString.
	buffered := true.
	hitEOF := false.
	binary := false.
	Lobby register:self
    ].
    lastErrorNumber notNil ifTrue:[
	"
	 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)
	"
	^ self openError
    ].
    ^ retVal
!

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

    mode := #readonly. didWrite := false.
    ^ self openPipeFor:command withMode:'r'
!

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

    mode := #writeonly. didWrite := true.
    ^ self openPipeFor:command withMode:'w'
! !