home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13685 < prev    next >
Encoding:
Text File  |  1992-09-15  |  4.5 KB  |  227 lines

  1. Xref: sparky comp.lang.c:13685 comp.unix.programmer:4660
  2. Newsgroups: comp.lang.c,comp.unix.programmer
  3. Path: sparky!uunet!utcsri!torn!watserv2.uwaterloo.ca!watserv1!wu
  4. From: wu@watserv1.uwaterloo.ca (Wenhua Wu - Management Sciences)
  5. Subject: Implement two-way interactive pipes using C ?
  6. Message-ID: <BuMGs0.ABx@watserv1.uwaterloo.ca>
  7. Keywords: Interactive pipes, parent and child communication
  8. Organization: University of Waterloo
  9. Date: Tue, 15 Sep 1992 13:43:11 GMT
  10. Lines: 214
  11.  
  12.  
  13.  
  14.  
  15. Hi, there: 
  16.  
  17.   I have some problems with the interactive (bidirectional) pipes.
  18.  
  19.   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
  20.  
  21.  
  22.  
  23. , then send other message to the child and wait for other result; ...., continue this process until child receives message "quit".
  24.  
  25.  Here child process must be a standalone executable program (no source avaiable) like "ed" in the Unix.
  26.  
  27.  Or is any body knowling author's email address so I can contact author  regarding this program ?  
  28.  
  29.  Your help is greatly apprecited!  
  30.  
  31.   Please send reply to me in the email address: wwu@bnr.ca
  32.    
  33.  
  34.  Thank you very much.
  35.  
  36.  
  37.  
  38. Regards,
  39.  
  40. - Wenhua Wu
  41.  
  42.  email:  wwu@bnr.ca
  43.  
  44.  
  45.  
  46. ---------------------------CUT HERE------------------------------
  47.  
  48. /* This program implement two-way interactive pipes.  The parent is 
  49.   using the editor as a subroutine, sending editor command lines to
  50.   it and getting output back. It is only sending search command 
  51.   "g/string_to_be_searched/p". It funtions as grep, but it illustrates
  52.   two-way interactive communication.
  53. -----------------------------------------------------------*/
  54.  
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <sys/stat.h>
  58. #include <sys/param.h>
  59. #include <sys/types.h>
  60. #include <sys/uio.h>
  61. #include <fcntl.h>
  62.  
  63. typedef unsigned char BOOLEAN;
  64.  
  65. static FILE *sndfp, *rcvfp;
  66.  
  67. int pid;
  68.  
  69. static edinvoke()
  70. {
  71.  
  72.    int pfdout[2], pfdin[2];
  73.    FILE *fdopen();
  74.  
  75.    if (pipe(pfdout)==-1 || pipe(pfdin)==-1)
  76.      perror("pipe");
  77.    
  78.    switch((pid=fork()))
  79.    {
  80.       case -1:  perror("fork");
  81.                 break;
  82.  
  83.       case 0:
  84.                if (close(0)==-1)
  85.                     perror("close");
  86.                if (dup(pfdout[0])!=0)
  87.                     perror("dup");
  88.                
  89.                if (close(1)==-1)
  90.                    perror("close");
  91.                if (dup(pfdin[1])!=1)
  92.                     perror("dup");
  93.                
  94.                if (close(pfdout[0])==-1 || close(pfdout[1])==-1 ||
  95.                    close(pfdin[0])==-1 || close(pfdin[1])==-1)
  96.                     perror("close");
  97.  
  98.                execlp("ed", "ed","-", NULL);
  99.                perror("execlp");
  100.    }
  101.  
  102.    if (close(pfdout[0])==-1 || close(pfdin[1])==-1)
  103.           perror("close");
  104.  
  105.    sndfp = fdopen(pfdout[1], "w");
  106.    rcvfp = fdopen(pfdin[0], "r");
  107. }
  108.  
  109.  
  110.  
  111. static void edsnd(s)
  112. char *s;
  113. {
  114.    printf("enter edsnd....\n");
  115.    if (fputs(s, sndfp)==EOF)
  116.        perror("endsnd");
  117. }
  118.  
  119.  
  120. static BOOLEAN edrcv(s, max)
  121. char *s;
  122. int max;
  123. {
  124.    printf("enter edrcv....\n");
  125.  
  126.    if (fgets(s, max, rcvfp) == NULL)
  127.        perror("edrcv");
  128.    return(strcmp(s, "?end-of-file\n")!=0);
  129.  
  130.  
  131. static void turnaround()
  132. {
  133.  
  134.    edsnd("r end-of-file\n");  /* expect result "?end-of-file" */
  135.    fflush(sndfp);
  136.  
  137. }
  138.  
  139.  
  140. static void rcvall()
  141. {
  142.    char s[200];
  143.    double sum;
  144.    long i;
  145.    
  146.    turnaround();
  147.  
  148.    while(edrcv(s, sizeof(s)))
  149.     printf("%s", s);
  150. }
  151.  
  152.  
  153.  
  154. main()
  155. {
  156.   char s[100], line[100];
  157.   BOOLEAN prompt();
  158.  
  159.   edinvoke();
  160.   if (!prompt("File", s))
  161.       exit(0);
  162.  
  163.   sprintf(line, "e %s\n", s);
  164.   edsnd(line);
  165.   rcvall();
  166.  
  167.   while(prompt("Search pattern", s))
  168.   {
  169.     sprintf(line, "g/%s/p\n",s);
  170.     printf("line=%s\n", line);
  171.     edsnd(line);
  172.     rcvall();
  173.   }
  174.   edsnd("q\n");
  175.   exit(0);
  176. }
  177.  
  178.  
  179. static BOOLEAN prompt(msg, result)
  180. char *msg, *result;
  181. {
  182.  
  183.   printf("\n%s?", msg);
  184.   return(gets(result)!=NULL);
  185. }
  186.    
  187.  
  188.  
  189.  
  190.                  
  191.  
  192. /*--------------------- TEST data  file "data"------------------------*/
  193.  
  194. peach
  195. apple
  196. orangle
  197. strawberry
  198. plum
  199. pear
  200. cherry
  201. banana
  202. apricot
  203. tomato
  204. pineapple
  205. mango
  206.  
  207.  
  208. /************** test session ********/
  209.  
  210.  Test session should look like the following:
  211.  
  212.   $search
  213.    FILE? data
  214.  
  215.    Search Pattern? apple
  216.    apple
  217.    pineapple
  218.    Search pattern? ^a
  219.    apple
  220.    apricot
  221.    Search pattern? EOT
  222.   $
  223.  
  224.  
  225.  
  226.