/***************************************************************
From the book "Motif Programming, The Essentials...and More"
by Marshall Brain.
Published by Digital Press, ISBN 1-55558-089-0.
To order the book, call 1-800-DIGITAL and ask for EY-J816E-DP.

Copyright 1992, by Digital Equipment Corp.

This is the header file for the link library.
***************************************************************/

/* link.h */

/* Link module, v1.1, 2/6/92 by Marshall Brain */

/* This module allows a program to form links to other separately
   executing programs and communicate with them. Links can be
   opened and closed, and the program using this library can 
   write to and read from the other program over the link. */

/* Warning -
   This module will not link with all programs. If the program
   does anything weird with stdout, or if it fails to flush
   stdout correctly, then this module will fail. If you are creating
   a stand-alone program that you wish to link to another program
   with this library, then you MUST make sure that stdout is
   flushed correctly. Either call "fflush(stdout)" after every
   printf, or call "setbuf(stdout,NULL)" at the beginning of the
   program to eliminate buffering.                   */
#include <stropts.h>
#include <stdio.h>
#include <strings.h>
#include <signal.h>
#include <sys/ioctl.h>

struct link_handle  /* holds all info relevant to one link. */
{
  int pipefd1[2],pipefd2[2];
  int pid;
  FILE *fpin,*fpout;
};

extern link_open(struct link_handle *l, char name[],char param1[]);
/* open a link to another program named name, passing a param
   to the program if desired. This routine will execute name
   in parallel and you can start communicating with it with
   link_read and link_write.*/

extern link_close(struct link_handle *l);
/* Close the link to a program that has terminated. Use link_kill
   if the program needs to be terminated as well.*/

extern int link_read(struct link_handle *l,char s[]);
/* read from the program started with link_open. Returns a 0 if
   there was stuff to read, or a 1 if the linked program terminated.*/

extern int link_input_waiting(struct link_handle *l);
/* Returns the number of bytes waiting in the input buffer. If
   0, then link_read will block if it is called. */

extern link_write_char(struct link_handle *l,char c);
/* write a char, without a newline, to the program.*/

extern link_write(struct link_handle *l,char s[]);
/* write a string to the program, with a newline.*/

extern link_kill(struct link_handle *l);
/*kill the program and close the link. If the program has terminated
   on its own use link_close instead.*/
