PipeStream.st
author Claus Gittinger <cg@exept.de>
Tue, 28 Jan 1997 01:02:13 +0100
changeset 2292 e1c0e8ada72f
parent 2266 a94af740c68a
child 2498 eb0bad72f05e
permissions -rw-r--r--
can wait forEver, if there is nothing to do

"
 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 pid exitStatus exitSema'
	classVariableNames:'BrokenPipeSignal'
	poolDictionaries:''
	category:'Streams-External'
!

!PipeStream primitiveDefinitions!
%{

#if defined(NT) || defined(WIN32) || defined(MSDOS)
# undef UNIX_LIKE
# define MSDOS_LIKE
#endif

#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

#ifdef LINUX
# define BUGGY_STDIO_LIB
#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:'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.
"
!

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

    Buffered pipes do not work with Linux - the stdio library seems to be
    buggy (trying to restart the read ...)

    [author:]
        Claus Gittinger
"
! !

!PipeStream class methodsFor:'initialization'!

initialize
    "setup the signal"

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

!PipeStream class methodsFor:'instance creation'!

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'

        |s|
        s := PipeStream readingFrom:'sh -c sleep\ 600'.
        (Delay forSeconds:2) wait.
        s shutDown
    "

    "Modified: 24.4.1996 / 09:09:25 / stefan"
!

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

!PipeStream class methodsFor:'Signal constants'!

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

    ^ BrokenPipeSignal
! !

!PipeStream methodsFor:'accessing'!

commandString
    "return the command string"

    ^ commandString
!

exitStatus
    "return exitStatus"

    ^ exitStatus

    "Created: 28.12.1995 / 14:54:41 / stefan"
!

pid
    "return pid"

    ^ pid

    "Created: 28.12.1995 / 14:54:30 / stefan"
! !

!PipeStream methodsFor:'instance release'!

closeFile
    "low level close
     This waits for the command to finish. 
     Use shutDown for a fast (nonBlocking) close."

    filePointer notNil ifTrue:[
        super closeFile.
        filePointer := nil.
        pid notNil ifTrue:[
	    [
                pid notNil ifTrue:[
                    exitSema wait.
		]
	    ] valueUninterruptably
        ].
    ].
!

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 */
#if !defined(transputer) && !defined(MSDOS_LIKE)

    OBJ fp;
    FILE *f;

    if ((fp = __INST(filePointer)) != nil) {
	__INST(filePointer) = nil;
	f = __FILEVal(fp);
	__BEGIN_INTERRUPTABLE__
	close(fileno(f));
	__END_INTERRUPTABLE__
    }
#endif /* not transputer && not MSDOS_LIKE */
%}
!

disposed
    "redefined to avoid blocking in close."

    self shutDown
!

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

    BrokenPipeSignal catch:[
        |tpid|

        Lobby unregister:self.
        self closeFileDescriptor.
        tpid := pid.                    "copy pid to avoid race"
        tpid notNil ifTrue:[
            "/
            "/ Terminate both the process and group, just in case the
            "/ operating system does not support process groups.
            "/
            OperatingSystem terminateProcess:tpid.
            OperatingSystem terminateProcessGroup:tpid.
            pid := nil.
        ].
    ]

    "Modified: 23.5.1996 / 09:15:41 / stefan"
! !

!PipeStream methodsFor:'private'!

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

    |blocked pipeFdArray execFdArray execFd myFd|

    filePointer notNil ifTrue:[
        "the pipe was already open ...
         this should (can) not happen."
        ^ self errorOpen
    ].
    lastErrorNumber := nil.
    exitStatus := nil.
    exitSema := Semaphore new name:'pipe exitSema'.

    pipeFdArray := OperatingSystem makePipe.
    pipeFdArray isNil ifTrue:[
        lastErrorNumber := OperatingSystem currentErrorNumber.
        ^ self openError
    ].

    mode = 'r' ifTrue:[
        execFd := pipeFdArray at:2.
        execFdArray := Array with:0 with:execFd with:2.
        myFd := pipeFdArray at:1.
    ] ifFalse:[
        execFd := pipeFdArray at:1.
        execFdArray := Array with:execFd with:1 with:2.
        myFd := pipeFdArray at:2.
    ].

    blocked := OperatingSystem blockInterrupts.
    pid := OperatingSystem exec:'/bin/sh'
                           withArguments:(Array with:'sh' with:'-c' with:aCommandString)
                           fileDescriptors:execFdArray
                           closeDescriptors:(Array with:myFd)
                           fork:true
                           newPgrp:true.

    OperatingSystem closeFd:execFd.
    pid > 0 ifTrue:[
        self fileDescriptor:myFd withMode:mode.
        Processor monitorPid:pid action:[ :status |
            exitStatus := status.
            pid := nil.
            exitSema signal.
        ].
    ] ifFalse:[
        pid := nil.
        lastErrorNumber := OperatingSystem currentErrorNumber.
        OperatingSystem closeFd:myFd.
    ].
    blocked ifFalse:[
        OperatingSystem unblockInterrupts
    ].

    lastErrorNumber isNil ifTrue:[
        commandString := aCommandString.
%{
        /* LINUX stdio is corrupt here ... */
#ifdef BUGGY_STDIO_LIB
        __INST(buffered) = false;
#else
        __INST(buffered) = true;
#endif
%}.
        hitEOF := false.
        binary := false.
        Lobby register:self
    ] ifFalse:[
        "
         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
    ].
    ^ self

    "Modified: 23.4.1996 / 17:05:59 / stefan"
!

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

!PipeStream class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.50 1997-01-24 23:18:40 cg Exp $'
! !
PipeStream initialize!