PipeStream.st
author Stefan Vogel <sv@exept.de>
Mon, 24 Jan 2000 17:15:01 +0100
changeset 5205 678e11b1e83e
parent 5203 a1a9869b2d24
child 5243 d3b509746522
permissions -rw-r--r--
Avoid duplicate fclose calling.

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

!PipeStream primitiveDefinitions!
%{

#if defined(WIN32)
# 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!
%{

/*
 * no longer needed - popen is useless ...
 */
#undef NEED_POPEN_WITH_VFORK


/*
 * 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 /* NEED_POPEN_WITH_VFORK */

%}
! !

!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; if you use close in the handler, this would
    try to send any buffered output to the pipe, leading to another brokenPipe exception.

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

    Currently, no filtering pipeStreams (i.e. both reading AND writing) are provided.
    However, if you look at how things are setup, this can be implemented using the
    low level primitives #mapePipe and #executeCommand from the OS class protocol.

    [author:]
	Claus Gittinger

    [see also:]
	ExternalStream FileStream Socket
	OperatingSystem
"
! !

!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. 
     The commands error output is send to my own error output."

    ^ self 
	readingFrom:commandString 
	errorDisposition:#stderr 
	inDirectory:nil

    "unix:
	PipeStream readingFrom:'ls -l'.
    "

    "
	p := PipeStream readingFrom:'ls -l'.
	Transcript showCR:p nextLine.
	p close
    "

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

    "vms:
	PipeStream readingFrom:'dir'.
    "

    "
	|p|
	p := PipeStream readingFrom:'dir'.
	Transcript showCR:p nextLine.
	p close
    "

    "msdos:
	PipeStream readingFrom:'dir'.
    "
    "
	|p|
	p := PipeStream readingFrom:'dir'.
	Transcript showCR:p nextLine.
	p close
    "

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

readingFrom:commandString errorDisposition:errorDisposition inDirectory:aDirectory
    "similar to #readingFrom, but changes the directory while
     executing the command. Use this if a command is to be
     executed in another directory, to avoid any OS dependencies
     in your code.
     errorDisposition may be one of #discard, #inline or #stderr (default).
     #discard causes stderr to be discarded (/dev/null), 
     #inline causes it to be merged into the PipeStream and
     #stderr causes it to be written to smalltalks own stderr.
     Nil is treated like #stderr"

    ^ self basicNew
	openPipeFor:commandString 
	withMode:'r' 
	errorDisposition:errorDisposition 
	inDirectory:aDirectory
!

readingFrom:commandString inDirectory:aDirectory
    "similar to #readingFrom, but changes the directory while
     executing the command. Use this if a command is to be
     executed in another directory, to avoid any OS dependencies
     in your code.
     The commands error output is send to my own error output."

     ^ self 
	readingFrom:commandString
	errorDisposition:#stderr 
	inDirectory:aDirectory

    "Modified: 24.9.1997 / 09:33:48 / stefan"
!

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

    ^ self 
	writingTo:commandString errorDisposition:#stderr inDirectory:nil

    "unix:
	 PipeStream writingTo:'sort'
    "
!

writingTo:commandString errorDisposition:errorDisposition inDirectory:aDirectory
    "similar to #writingTo, but changes the directory while
     executing the command. Use this if a command is to be
     executed in another directory, to avoid any OS dependencies
     in your code.
     errorDisposition may be one of #discard, #inline or #stderr (default).
     #discard causes stderr to be discarded (/dev/null), 
     #inline causes it to be written to smalltalks own stdout and
     #stderr causes it to be written to smalltalks own stderr.
     Nil is treated like #stderr"

    ^ self basicNew
	openPipeFor:commandString 
	withMode:'w' 
	errorDisposition:errorDisposition 
	inDirectory:aDirectory
!

writingTo:commandString inDirectory:aDirectory
    "create and return a new pipeStream which can write to the unix command
     given by command. The command is executed in the given directory."

    ^ self 
	writingTo:commandString errorDisposition:#stderr inDirectory:aDirectory

    "unix:
	 PipeStream writingTo:'sort'
    "
! !

!PipeStream class methodsFor:'Signal constants'!

brokenPipeSignal
    "return the signal used to handle SIGPIPE unix-signals.
     Since SIGPIPE is asynchronous, we can't decide which smalltalk process
     should handle BrokenPipeSignal. So the system doesn't raise 
     BrokenPipeSignal for SIGPIPE any longer."

    ^ BrokenPipeSignal

    "Modified: 24.9.1997 / 09:43:23 / stefan"
! !

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

    |tpid|

    filePointer notNil ifTrue:[
	(tpid := pid) notNil ifTrue:[
	    OperatingSystem isMSDOSlike ifTrue:[
		OperatingSystem terminateProcess:tpid.
		OperatingSystem terminateProcessGroup:tpid.
		pid := nil.
	    ]
	].
	super closeFile.

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

    "Modified: / 12.9.1998 / 16:51:04 / cg"
!

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

    |action|

%{  
#if !defined(transputer)
    OBJ fp;
    FILE *f;
    extern close();

    if ((fp = __INST(filePointer)) != nil) {
	__INST(filePointer) = nil;
	f = __FILEVal(fp);
	if (@global(ExternalStream:FileOpenTrace) == true) {
	    fprintf(stderr, "close [PipeStream] %x fd=%d\n", f, fileno(f));
	}
#ifdef WIN32
	__STX_C_CALL1((void*)close, (void*)fileno(f));
#else
	__BEGIN_INTERRUPTABLE__
	close(fileno(f));
	__END_INTERRUPTABLE__
#endif
    }
#endif /* not transputer  */
%}.
    exitAction notNil ifTrue:[
	action := exitAction.
	exitAction := nil.
	action value.
    ]
!

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.

	"/ terminate first under windows.
	(tpid := pid) notNil ifTrue:[      "copy pid to avoid race"
	    OperatingSystem isMSDOSlike ifTrue:[
		OperatingSystem terminateProcess:tpid.
		OperatingSystem terminateProcessGroup:tpid.
		pid := nil.
	    ].
	].

	self closeFileDescriptor.

	"/ terminate last under unix.
	(tpid := pid) notNil ifTrue:[      "copy pid to avoid race"
	    "/
	    "/ 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"
    "Modified: / 12.9.1998 / 16:50:18 / cg"
! !

!PipeStream methodsFor:'private'!

exitAction:aBlock
    "define a block to be evaluated when the pipe is closed.
     This is only used with VMS, to remove any temporary COM file.
     (see readingFrom:inDirectory:)"

    exitAction := aBlock
!

openPipeFor:aCommandString withMode:rwMode errorDisposition:err inDirectory:aDirectory
    "open a pipe to the OS command in commandString; 
     rwMode may be 'r' or 'w'"

    |blocked pipeFdArray execFdArray execFd myFd shellAndArgs
     shellPath shellArgs closeFdArray mbx mbxName 
     realCmd execDirectory tmpComFile nullOutput|

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

    rwMode = 'r' ifTrue:[
	mode := #readonly. didWrite := false.
    ] ifFalse:[
	mode := #writeonly. didWrite := true.
    ].

    lastErrorNumber := nil.
    exitStatus := nil.
    exitSema := Semaphore new name:'pipe exitSema'.

    realCmd := aCommandString.
    execDirectory := aDirectory.

    OperatingSystem isVMSlike ifTrue:[
	"/
	"/ the generated COM-file includes a 'set default'
	"/
	tmpComFile := OperatingSystem createCOMFileForVMSCommand:aCommandString in:aDirectory.
	realCmd := '@' , tmpComFile osName.
	execDirectory := nil.

	mbx := OperatingSystem createMailBox.
	mbx isNil ifTrue:[
	    lastErrorNumber := OperatingSystem currentErrorNumber.
	    tmpComFile delete.
	    ^ self openError
	].
	mbxName := OperatingSystem mailBoxNameOf:mbx.

	"/ 'mailBox is ' print. mbx print. ' name is ' print. mbxName printCR.
	shellPath := ''.
	shellArgs := realCmd.

	rwMode = 'r' ifTrue:[
	    execFdArray := Array with:0 with:mbx with:2.
	    (err == #inline or:[err == #stdout]) ifTrue:[
		execFdArray at:3 put:mbx
	    ]
	] ifFalse:[
	    execFdArray := Array with:mbx with:1 with:2.
	    (err == #inline or:[err == #stdout]) ifTrue:[
		execFdArray at:3 put:1
	    ]
	].
	closeFdArray := nil.
    ] ifFalse:[
	OperatingSystem isUNIXlike ifTrue:[
	    aDirectory notNil ifTrue:[
		"/ unix - prepend a 'cd' to the command
		realCmd := 'cd ' , aDirectory asFilename name, '; ' , aCommandString.
	    ] ifFalse:[
		realCmd := aCommandString
	    ].
	    execDirectory := nil.
	].

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

	shellAndArgs := OperatingSystem commandAndArgsForOSCommand:realCmd.
	shellPath := shellAndArgs at:1.
	shellArgs := shellAndArgs at:2.

	rwMode = 'r' ifTrue:[
	    myFd := pipeFdArray at:1.
	    execFd := pipeFdArray at:2.
	    execFdArray := Array with:0 with:execFd with:2.
	    (err == #inline or:[err == #stdout]) ifTrue:[
		execFdArray at:3 put:execFd
	    ]
	] ifFalse:[
	    myFd := pipeFdArray at:2.
	    execFd := pipeFdArray at:1.
	    execFdArray := Array with:execFd with:1 with:2.
	    (err == #inline or:[err == #stdout]) ifTrue:[
		execFdArray at:3 put:1
	    ]
	].
	closeFdArray := Array with:myFd.
    ].

    err == #discard ifTrue:[
	nullOutput := Filename nullDevice writeStream.
	execFdArray at:3 put:nullOutput fileDescriptor
    ].

    "/ must block here, to avoid races due to early finishing
    "/ subprocesses ...

    blocked := OperatingSystem blockInterrupts.

    pid := Processor 
	       monitor:[
		  OperatingSystem 
		      exec:shellPath
		      withArguments:shellArgs
		      fileDescriptors:execFdArray
		      closeDescriptors:closeFdArray
		      fork:true
		      newPgrp:true
		      inDirectory:execDirectory.
	       ]
	       action:[:status |
		  status stillAlive ifFalse:[
		      exitStatus := status.
		      OperatingSystem closePid:pid.
		      pid := nil.
		      exitSema signal.
		  ].
	       ].

    OperatingSystem isVMSlike ifFalse:[
	OperatingSystem closeFd:execFd.
    ].

    nullOutput notNil ifTrue:[
	nullOutput close
    ].

    pid notNil ifTrue:[
	OperatingSystem isVMSlike ifTrue:[
	    "/
	    "/ reopen the mailbox as a file ...
	    "/
	    mbxName := OperatingSystem mailBoxNameOf:mbx.
	    mbxName notNil ifTrue:[
		super open:mbxName withMode:rwMode.
		exitAction := [tmpComFile delete].
	    ].
	] ifFalse:[
	    self setFileDescriptor:myFd mode:rwMode.
	]
    ] ifFalse:[
	lastErrorNumber := OperatingSystem currentErrorNumber.
	OperatingSystem isVMSlike ifTrue:[
	    OperatingSystem destroyMailBox:mbx.
	    tmpComFile delete.
	] ifFalse:[
	    OperatingSystem closeFd:myFd.
	].
    ].

    blocked ifFalse:[
	OperatingSystem unblockInterrupts
    ].

    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)
	"
	exitAction value.
	^ self openError
    ].

    commandString := realCmd.
%{
    /* LINUX stdio is corrupt here ... */
#if defined(BUGGY_STDIO_LIB) || defined(WIN32)
    __INST(buffered) = false;
#else
    __INST(buffered) = true;
#endif
%}.
    position := 1.
    hitEOF := false.
    binary := false.
    Lobby register:self.

    "Modified: / 23.4.1996 / 17:05:59 / stefan"
    "Modified: / 28.1.1998 / 14:47:34 / md"
    "Created: / 19.5.1999 / 12:28:54 / cg"
! !

!PipeStream class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.82 2000-01-24 16:15:00 stefan Exp $'
! !
PipeStream initialize!