/**********************************************************************
 * ti library's routine for adding a new node
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/add.c,v $
 * $Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/add.c,v 0.2 92/04/01 16:19:19 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_add_c[] = "$Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/add.c,v 0.2 92/04/01 16:19:19 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <string.h>
#include <ti/ti.h>

/**********************************************************************
 * ti_add(TI_H hti, ti_node_H hnode)
 *	caller fills in appropriate fields of hnode beforehand
 *
 * - fills in hnode's ti_id
 * - returns error code
 **********************************************************************/

/* jms 7/9/92 -- windows-izing completed, pending approval */

long
ti_add(hti, hnode)
     TI_H hti;
     ti_node_H hnode;
{
  long code;
  static char inbuf[BUFSIZ];

  ti_node_LP lpnode;

  lpnode = (ti_node_LP)TI_DEREFERENCE(hnode);
  lpnode->nd_data = TI_DEREFERENCE(lpnode->nd_hdata);

  /* send command and receive reply */
  if (code = _ti_talkf(inbuf, BUFSIZ-1,
                       "a:0:%d:0:%s:%s:%s:%s:%s\n",
                       ti_flags(lpnode)|TI_NRESOLVED,
	               ti_topic(lpnode),
                       ti_title(lpnode),
                       ti_source(lpnode),
	               ti_locker(lpnode),
                       ti_file(lpnode)))
    CLEANUP_AND_RETURN(code)

  /* success? */
  if (!atoi(inbuf))
    {
      if (!index(inbuf, ':'))
	{
	  strcpy(ti_context, inbuf);
	  CLEANUP_AND_RETURN(TI_ERR_SRVHUH)
	}
      ti_id(lpnode) = atol(index(inbuf, ':')+1);
      CLEANUP_AND_RETURN(0L)
    }

  /* failure */
  strcpy(ti_context, ti_title(lpnode));
  code = (_ti_srverr(inbuf));

CLEANUP:
  TI_REMOVE_DEREFERENCE(lpnode->nd_hdata, lpnode->nd_data);
  TI_REMOVE_DEREFERENCE(hnode, lpnode);
  return (code);
}
