home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / programm / 5799 < prev    next >
Encoding:
Text File  |  1992-12-30  |  2.4 KB  |  85 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!agate!linus!linus.mitre.org!elara.mitre.org!bdickens
  3. From: bdickens@elara.mitre.org (Brian Dickens)
  4. Subject: Pipes/Sockets/Pty's/Communications
  5. Message-ID: <1992Dec30.160512.10958@linus.mitre.org>
  6. Keywords: pipe socket pty
  7. Sender: news@linus.mitre.org (NONUSER)
  8. Nntp-Posting-Host: elara.mitre.org
  9. Organization: Research Computer Facility, MITRE Corporation, Bedford, MA
  10. Date: Wed, 30 Dec 1992 16:05:12 GMT
  11. Lines: 72
  12.  
  13. I am attempting to remote-control a program that I did not write, which was
  14. written to get its input from and send its output to a terminal.
  15.  
  16. I am calling this program from a nice fancy Motif application, written in C.
  17.  
  18. I want to be able to send commands to the program and get answers back,
  19. but using standard piping techniques have been unable to help me so far,
  20. since the output of the program seems to be *buffered*!!!
  21.  
  22. What's the best way to set up such a remote-control link?  Should I use
  23. pseudo-terminal devices?  This was one reccomendation I received --
  24. but I haven't a clue what they are or how to use them.  Or are standard
  25. pipes okay, and I'm just doing something wrong?  Let me include the
  26. code I have so far, which tries to start up the program (called ARC/info,
  27. executed by typing "arc") and execute its "help" command... which is
  28. supposed to print a listing to stdout of the commands.
  29.  
  30. ----------------------- CODE PART ----------------------------
  31. #include <stdio.h>
  32.  
  33. int in, out;
  34.  
  35. int talkto( cmd )
  36.    char *cmd;
  37. {
  38.    int pid;
  39.    int to_child[2];
  40.    int to_parent[2];
  41.  
  42.    pipe( to_child );
  43.    pipe( to_parent );
  44.    if( pid = fork(), pid == 0 )
  45.    {
  46.       close( 0 );
  47.       dup( to_parent[0] );
  48.       close( 1 );
  49.       dup( to_child[1] );
  50.       close( to_child[0] );
  51.       close( to_child[1] );
  52.       close( to_parent[0] );
  53.       close( to_parent[1] );
  54.       execlp( cmd, cmd, NULL);
  55.    }
  56.    else
  57.       if( pid > 0 )
  58.       {
  59.          in = dup( to_parent[0] );
  60.          out = dup( to_child[1] );
  61.          close( to_child[0] );
  62.          close( to_child[1] );
  63.          close( to_parent[0] );
  64.          close( to_parent[1] );
  65.       }
  66.       else
  67.       {
  68.          fprintf( stderr, "Couldn't fork process.\n" );
  69.          exit( 1 );
  70.       }
  71.  
  72.    return( 0 );
  73. }
  74.  
  75. int main( )
  76. {
  77.    char x[255];
  78.  
  79.    talkto( "arc" );
  80.    write( out, "help\n", strlen( "help\n" ) );
  81.    read( in, x, 255 );
  82.    printf( "%s", x );
  83.    return( 0 );
  84. }
  85.