/*
 * popen() -- open a pipe to a subprocess. It can be either a "read"
 *            pipe or a "write" pipe. Since AmigaDog does not come
 *            standard with pipes, but they are available from 3rd
 *            parties, popen() will first try to open the device "PIPE:"
 *            with the subprocess RUNning asynchronously. If it does not
 *            succeed, it will open a temporary file in RAM: and run
 *            the processes sequentially, using Execute.
 *
 *            Author: Scott Henry,  22 Feb 88
 */

#ifndef PIPE_DEVICE
#define PIPE_DEVICE "PIPE:Q"
#endif

#ifdef DEBUG
#define DPRINTF1(s,x) fprintf(stderr,s,x)
#else
#define DPRINTF1(s,x)
#endif

#include <libraries/dos.h>
#include <stdio.h>

FILE *
popen( command, type)
        char *command;
        char *type;
{
   struct FileHandle *FH=NULL, *Open();
   FILE   *fp=NULL, *fopen();
   char   buffer[128];
   char   *malloc();
   int    i;

   setmem( buffer, 128, '\0');
   if (*type == 'w')		/* write pipe */
    {
      if ((fp = fopen(PIPE_DEVICE, "w")) != NULL)
       {
         strcpy( buffer, "c:run ");
         for (i=6; *command && *command != ' ' && *command != '\t'; ++i)
          {
            buffer[i] = *command++;
          }
         strcat( buffer, " <");
         strcat( buffer, PIPE_DEVICE);
         strcat( buffer, " ");
         strcat( buffer, command);
         DPRINTF1( "popen(\"%s\", \"w\")\n", buffer);
         if (Execute( buffer, 0L, 0L) == 0L)
          {
            fclose( fp);
            return NULL;
          }
         return fp;

       }
      else
       {
         return NULL;	/* can't use a temp file on popen() for write */
			/* (think about it) */
       }
    }
   else if (*type == 'r')
    {
      if ((fp = fopen(PIPE_DEVICE, "r")) != NULL)
       {
         strcpy( buffer, "c:run ");
         for (i=6; *command && *command != ' ' && *command != '\t'; ++i)
          {
            buffer[i] = *command++;
          }
         strcat( buffer, " >");
         strcat( buffer, PIPE_DEVICE);
         strcat( buffer, " ");
         strcat( buffer, command);
         DPRINTF1( "popen(\"%s\", \"r\")\n", buffer);
         if (Execute( buffer, 0L, 0L) == 0L)
          {
            fclose( fp);
            return NULL;
          }
         return fp;

       }
      else
       {	/* can't open PIPE:, use a temporary file instead */
         strcpy( buffer, "RAM:");
         tmpnam( &buffer[4]);
         DPRINTF1( "popen: tempfile=\"%s\"\n", buffer);
         if ((FH = Open( buffer, MODE_NEWFILE)) == NULL)
          {
            return NULL;
          }
         if (Execute( command, 0L, FH) != 0)   /* implied wait for command... */
          {
            Close( FH);
            return NULL;
          }
         Close( FH);
         if ((fp = fopen( buffer, "r")) == NULL)
          {
            DeleteFile( buffer);
            return NULL;
          }
         fp->_tmpname = (char *)malloc( strlen(buffer)+1);
         strcpy( fp->_tmpname, buffer);
         fp->_flags |= _TEMP;
         return fp;
       }
    }
   else
    {
      return NULL;	/* invalid mode */
    }
}
