PipeStr.st
author claus
Mon, 08 May 1995 05:31:14 +0200
changeset 339 e8658d38abfb
parent 326 d2902942491d
child 345 cf2301210c47
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/Attic/PipeStr.st,v 1.23 1995-04-11 14:50:41 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/Attic/PipeStr.st,v 1.23 1995-04-11 14:50:41 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>
#include <errno.h>
#ifndef transputer
# include <sys/types.h>
# include <sys/stat.h>
#endif

extern int __immediateInterrupt__;
/*
 * 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 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:'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:[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

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

!PipeStream methodsFor:'private'!

XXatEnd
    "return true, if position is at end"

%{  /* NOCONTEXT */
#ifdef IRIX5
    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);
	}
	RETURN ( false );
    }
#endif
%}
.
    ^ 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)) {
	__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__ = 0;
	if (f == NULL) {
	    _INST(lastErrorNumber) = _MKSMALLINT(errno);
	} else {
	    _INST(filePointer) = MKOBJ(f);
	    retVal = self;
	}
    }
#endif
%}.
    retVal notNil ifTrue:[
	commandString := aCommandString.
	buffered := true.
	hitEOF := 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'
! !