/**********************************************************************
 * ti library's routines for sending text of a file
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/dev/project/techinfodev/src/libti/RCS/send.c,v $
 * $Header: /afs/net.mit.edu/dev/project/techinfodev/src/libti/RCS/send.c,v 3.0 93/03/03 16:29:39 brlewis Exp $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>

#ifndef lint
static char rcsid_ti_send_c[] = "$Header: /afs/net.mit.edu/dev/project/techinfodev/src/libti/RCS/send.c,v 3.0 93/03/03 16:29:39 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <string.h>
#include <ti/ti.h>

/**********************************************************************
 * ti_sendfirst(TI *tp, ti_node *np)
 *	np must be a node that has been created with TI_NSYSGEN_FN set
 *
 * - sends command to server
 * - returns error code
 **********************************************************************/

long
ti_sendfirst(tp, np)
     TI *tp;
     ti_node *np;
{
  long code;
  char inbuf[BUFSIZ], outbuf[32];

  /* construct techinfo command */
  sprintf(outbuf, "f:%d\n", ti_id(np));

  /* send command and receive reply */
  if (code = _ti_talk(tp, outbuf, strlen(outbuf),
		      inbuf, BUFSIZ-1)) return(code);

  /* success? */
  if (!atoi(inbuf)) return(0L);

  /* failure */
  strcpy(ti_context, ti_title(np));
  return(_ti_srverr(inbuf));
}

/**********************************************************************
 * ti_sendmiddle(TI *tp, char *buf, int size)
 *	buf contains size bytes of file to send to server
 *
 * - sends bytes to server
 * - returns error code
 **********************************************************************/

long
ti_sendmiddle(tp, buf, size)
     TI *tp;
     char *buf;
     int size;
{
  /* XXX should check for . on a line by itself in middle of text
     and should remember if last char sent was a newline */

  return(_ti_sendmsg(tp, buf, size));
}

/**********************************************************************
 * ti_sendlast(TI *tp)
 *
 * - sends EOF command
 * - returns error code
 **********************************************************************/

long
ti_sendlast(tp)
     TI *tp;
{
  long code;
  char inbuf[BUFSIZ];

  /* send command and receive reply */
  if (code = _ti_talk(tp, TI_EOM, TI_EOM_LEN, inbuf, BUFSIZ-1))
    return(code);

  /* success? */
  if (!atoi(inbuf)) return(0L);

  /* failure */
  *ti_context = '\0';
  return(_ti_srverr(inbuf));
}
