From: wu@watserv1.uwaterloo.ca (Wenhua Wu - Management Sciences)
Subject: Implement two-way interactive pipes using C ?
Keywords: Interactive pipes, parent and child communication
Organization: University of Waterloo
Date: Tue, 15 Sep 1992 13:43:11 GMT




Hi, there: 

  I have some problems with the interactive (bidirectional) pipes.

  The attched program is an interactive (two-way) pipe test program, which is from the Book:  "Advanced UNIX Programming" (pg. 151-153) by Marc J. Rochkind.  I tried it several times. It does not work on HP (It works on SUN).  Is there anybody who can give me some hints on how to make this program work on HP workstation, or tell me other way to implement two-way interactive pipes ?  By two-way interactive pipes I mean you send a message to the child through pipe and receive a result from child through pipe



, then send other message to the child and wait for other result; ...., continue this process until child receives message "quit".

 Here child process must be a standalone executable program (no source avaiable) like "ed" in the Unix.

 Or is any body knowling author's email address so I can contact author  regarding this program ?  

 Your help is greatly apprecited!  
 
  Please send reply to me in the email address: wwu@bnr.ca
   

 Thank you very much.



Regards,

- Wenhua Wu

 email:  wwu@bnr.ca



---------------------------CUT HERE------------------------------

/* This program implement two-way interactive pipes.  The parent is 
  using the editor as a subroutine, sending editor command lines to
  it and getting output back. It is only sending search command 
  "g/string_to_be_searched/p". It funtions as grep, but it illustrates
  two-way interactive communication.
-----------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>

typedef unsigned char BOOLEAN;

static FILE *sndfp, *rcvfp;

int pid;

static edinvoke()
{

   int pfdout[2], pfdin[2];
   FILE *fdopen();

   if (pipe(pfdout)==-1 || pipe(pfdin)==-1)
     perror("pipe");
   
   switch((pid=fork()))
   {
      case -1:  perror("fork");
                break;

      case 0:
               if (close(0)==-1)
                    perror("close");
               if (dup(pfdout[0])!=0)
                    perror("dup");
               
               if (close(1)==-1)
                   perror("close");
               if (dup(pfdin[1])!=1)
                    perror("dup");
               
               if (close(pfdout[0])==-1 || close(pfdout[1])==-1 ||
                   close(pfdin[0])==-1 || close(pfdin[1])==-1)
                    perror("close");

               execlp("ed", "ed","-", NULL);
               perror("execlp");
   }
 
   if (close(pfdout[0])==-1 || close(pfdin[1])==-1)
          perror("close");

   sndfp = fdopen(pfdout[1], "w");
   rcvfp = fdopen(pfdin[0], "r");
}



static void edsnd(s)
char *s;
{
   printf("enter edsnd....\n");
   if (fputs(s, sndfp)==EOF)
       perror("endsnd");
}


static BOOLEAN edrcv(s, max)
char *s;
int max;
{
   printf("enter edrcv....\n");

   if (fgets(s, max, rcvfp) == NULL)
       perror("edrcv");
   return(strcmp(s, "?end-of-file\n")!=0);
} 


static void turnaround()
{

   edsnd("r end-of-file\n");  /* expect result "?end-of-file" */
   fflush(sndfp);

}


static void rcvall()
{
   char s[200];
   double sum;
   long i;
   
   turnaround();

   while(edrcv(s, sizeof(s)))
    printf("%s", s);
}



main()
{
  char s[100], line[100];
  BOOLEAN prompt();

  edinvoke();
  if (!prompt("File", s))
      exit(0);

  sprintf(line, "e %s\n", s);
  edsnd(line);
  rcvall();

  while(prompt("Search pattern", s))
  {
    sprintf(line, "g/%s/p\n",s);
    printf("line=%s\n", line);
    edsnd(line);
    rcvall();
  }
  edsnd("q\n");
  exit(0);
}


static BOOLEAN prompt(msg, result)
char *msg, *result;
{

  printf("\n%s?", msg);
  return(gets(result)!=NULL);
}
   


 
                 

/*--------------------- TEST data  file "data"------------------------*/

peach
apple
orangle
strawberry
plum
pear
cherry
banana
apricot
tomato
pineapple
mango


/************** test session ********/

 Test session should look like the following:

  $search
   FILE? data

   Search Pattern? apple
   apple
   pineapple
   Search pattern? ^a
   apple
   apricot
   Search pattern? EOT
  $



From: Volker.Kaechele@quattro.stgt.sub.org (Volker Kaechele)
Sender: FredMail@quattro.stgt.sub.org