home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 4
/
FreshFish_May-June1994.bin
/
new
/
amigalibdisks
/
d980
/
apipe
/
pgmpipe.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-04
|
25KB
|
860 lines
/*
** Pgmpipe.c --- main module of APipe-Handler
**
** Copyright (C) 1991 by Per Bojsen. All Rights Reserved.
**
** Permission is granted to any individual or institution to use, copy,
** modify, and distribute this software, provided that this complete
** copyright and permission notice is maintained, intact, in all copies
** and supporting documentation.
**
** This software is provided on an "as is" basis without express or
** implied warranty.
**
** NOTE: The code is reentrant. Be careful when modifying it.
**
** $Id: pgmpipe.c,v 1.7 92/09/12 16:49:11 bojsen Exp Locker: bojsen $
**
** $Log: pgmpipe.c,v $
** Revision 1.7 92/09/12 16:49:11 bojsen
** Improved cloning of file handles so that reading/writing will start
** from current position.
**
** Revision 1.6 92/02/20 23:46:11 bojsen
** Fixed bug in handling multiple outstanding read packets add EOF.
**
** Revision 1.5 91/11/24 17:01:25 bojsen
** Made extraction of command line from file name more robust; the code now
** handles the case where the handler name is not prepended.
**
** Revision 1.4 91/11/24 02:22:39 bojsen
** First released version. Changed references to ChildProcess() to
** _ChildProcess().
**
** Revision 1.3 91/10/01 02:35:30 bojsen
** Added code to clone current dir of requester process. Changed code
** to obtain pointer to requester process.
**
** Revision 1.2 91/09/24 23:59:44 bojsen
** Fixed typo in #include line.
**
** Revision 1.1 91/09/24 23:54:06 bojsen
** Initial revision
**
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <utility/tagitem.h>
#include <clib/macros.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/dostags.h>
#include <dos/var.h>
#ifdef __SASC
#include <proto/exec.h>
#include <proto/dos.h>
#endif /* __SASC */
#include <string.h>
#include "APipe-Handler_rev.h"
/*
* Version tagging.
*/
STATIC char VersionTag[] = VERSTAG;
/*
* Debugging.
*/
#ifdef DEBUG
#define DPrintF(args) do { KPrintF args; } while (0)
#else /* !DEBUG */
#define DPrintF(args)
#endif /* !DEBUG */
#define AbsExecBase 4L
#define LIB_VERSION_SUPPORTED 37L
#define PIPEBUFFER_SIZE 4096
/* defines for read/write status */
#define PPIPE_NOTOPEN 0
#define PPIPE_OPEN 4
#define PPIPE_READ 1
#define PPIPE_READOPEN (PGMPIPE_READ | PGMPIPE_OPEN)
#define PPIPE_WRITE 2
#define PPIPE_WRITEOPEN (PGMPIPE_WRITE | PGMPIPE_OPEN)
#define PPIPE_RWMASK 3
/* defines for fh_Arg1 field of filehandles */
#define PPIPE_READER 1
#define PPIPE_WRITER 2
/* defines for closing variable */
#define PPIPE_C_OPEN 0
#define PPIPE_C_RDRCLOSING 1
#define PPIPE_C_WRTCLOSING 2
#define PPIPE_C_CLOSINGMASK 3
#define PPIPE_C_RDRCLOSED 4
#define PPIPE_C_WRTCLOSED 8
#define PPIPE_C_CLOSEDMASK 0xC
/*
* Parameter packet to child process.
*/
struct ChildMsg
{
struct Message ExecMsg;
char *CmdLine; /* the command line to execute */
LONG StackSize; /* use this stacksize for child's child */
BPTR PathList; /* path list for child */
ULONG Flags; /* flags for child */
LONG RC; /* return code from child process */
};
/* Defines for ChildMsg.Flags */
#define CHMF_PIPE 1L /* stdin of child is a pipe (of PIPE: type) */
#define CHMF_PATH 2L /* path is passed in in PathList */
/*
* Command path element.
*/
struct PathEntry
{
BPTR pe_NextPathEntry;
BPTR pe_PathLock;
};
/*
* The library bases must be global by AmigaOS programming standards,
* and the pragma libcall requires them.
*/
struct ExecBase *SysBase;
struct DosLibrary *DOSBase;
/*
* Prototypes for external functions.
*/
extern int _ChildProcess(void);
/*
* Prototypes for local functions.
*/
ULONG CopyFromFIFO(UBYTE *FIFO, ULONG FIFOSize, UBYTE **Start, UBYTE *End,
ULONG *FIFOFill, UBYTE *Dest, ULONG NumBytes);
ULONG CopyToFIFO(UBYTE *FIFO, ULONG FIFOSize, UBYTE **Start, UBYTE **End,
ULONG *FIFOFill, UBYTE *Src, ULONG NumBytes);
STRPTR GetCommandLine(STRPTR FileSpec, BSTR HandlerName);
LONG ExecCommand(char *CmdLine, struct MsgPort **ParentPort,
struct Process *Requester, struct FileHandle *ChildIO,
LONG IoDirection);
BPTR CloneProcessIO(struct Process *Friend, LONG IoDirection);
BPTR ClonePathList(struct CommandLineInterface *Peer);
void FreePathList(BPTR);
void
PgmPipe()
{
struct DosLibrary *l_DOSBase;
SysBase = *((struct ExecBase **) AbsExecBase); /* `open' exec.library */
if (l_DOSBase =
(struct DosLibrary *) OpenLibrary("dos.library", LIB_VERSION_SUPPORTED))
{
struct Process *ourProc, *theirProc;
struct DosList *ourDevNode;
struct DosPacket *packet, *writePkt = NULL, *readPkt = NULL;
struct DosPacket *writeCls = NULL, *readCls = NULL;
struct MsgPort *childPort = NULL;
struct MsgPort *oldConTask = NULL;
struct ChildMsg *childMsg = NULL;
struct FileHandle *fhIncoming, *fhOutgoing;
UBYTE *pipeBuffer;
UBYTE *pbStart, *pbEnd;
ULONG bytesReady, bytesR = 0, bytesW = 0;
UWORD closing = PPIPE_C_RDRCLOSED | PPIPE_C_WRTCLOSED;
UWORD readwrite = PPIPE_NOTOPEN;
ULONG waitSigMask;
struct MinList readQ, writeQ;
#ifdef DEBUG
ULONG packetNum = 0;
#endif /* DEBUG */
NewList((struct List *) &readQ);
NewList((struct List *) &writeQ);
DOSBase = l_DOSBase; /* patch the global library base */
ourProc = (struct Process *) FindTask(NULL);
waitSigMask = 1L << ourProc->pr_MsgPort.mp_SigBit;
packet = WaitPkt(); /* get parameter packet */
if ((pipeBuffer = AllocMem(PIPEBUFFER_SIZE, 0)) == NULL)
ReplyPkt(packet, DOSFALSE, ERROR_NO_FREE_STORE);
else
{
pbStart = pbEnd = pipeBuffer;
bytesReady = 0;
/*
* Currently we don't use the parameters provided in the parameter
* packet. Since we want a new process for each instance of reference
* to our handler we don't patch the DeviceList node, but use the
* node to get the handler name.
*/
ourDevNode = (struct DosList *) BADDR(packet->dp_Arg3);
ReplyPkt(packet, DOSTRUE, packet->dp_Arg2);
do
{
struct Node *node;
char *cmdLine;
LONG ioerr;
if (readPkt && bytesReady == 0 && writePkt == NULL &&
closing & PPIPE_C_WRTCLOSED)
{
ReplyPkt(readPkt, bytesR, 0);
while (readPkt = (struct DosPacket *)
(node = RemHead((struct List *) &readQ),
node ? node->ln_Name : NULL))
ReplyPkt(readPkt, 0, 0);
}
#ifdef DEBUG
if (readPkt || writePkt)
DPrintF(("Event loop: readPkt == 0x%08lx writePkt == 0x%08lx\n",
readPkt, writePkt));
#endif /* DEBUG */
if (packet = WaitPkt())
{
DPrintF(("Packet %ld, port 0x%08lx: ", ++packetNum, packet->dp_Port));
switch (packet->dp_Type)
{
struct TagItem adoTags[2];
case ACTION_FINDINPUT:
DPrintF(("ACTION_FINDINPUT 0x%08lx 0x%08lx %s\n",
packet->dp_Arg1 << 2, packet->dp_Arg2 << 2,
(char *) (packet->dp_Arg3 << 2) + 1));
if (readwrite != PPIPE_NOTOPEN)
{
ReplyPkt(packet, DOSFALSE, ERROR_OBJECT_IN_USE);
break;
}
else
readwrite = PPIPE_READ;
case ACTION_FINDOUTPUT:
#ifdef DEBUG
if (packet->dp_Type == ACTION_FINDOUTPUT)
DPrintF(("ACTION_FINDOUTPUT 0x%08lx 0x%08lx %s\n",
packet->dp_Arg1 << 2, packet->dp_Arg2 << 2,
(char *) (packet->dp_Arg3 << 2) + 1));
#endif /* DEBUG */
if (readwrite & PPIPE_OPEN)
{
ReplyPkt(packet, DOSFALSE, ERROR_OBJECT_IN_USE);
break;
}
else if (readwrite != PPIPE_READ)
readwrite = PPIPE_WRITE;
fhIncoming = (struct FileHandle *) BADDR(packet->dp_Arg1);
/*
* Obtai