/**********************************************************************
 * ti library's routine for getting a keyword search
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/dapps/project/techinfodev/src/libti/RCS/kwd.c,v $
 * $Header: /afs/net.mit.edu/dapps/project/techinfodev/src/libti/RCS/kwd.c,v 0.1 92/07/09 17:02:50 brlewis Exp Locker: brlewis $
 *
 * Copyright 1992 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_kwd_c[] = "$Header: /afs/net.mit.edu/dapps/project/techinfodev/src/libti/RCS/kwd.c,v 0.1 92/07/09 17:02:50 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <errno.h>
#include <string.h>
#include <ti/memory.h>
#include <ti/ti.h>

/**********************************************************************
 * ti_kwd(TI_H hti, ti_menu **mp, char *keyword, long top, char *title)
 *	caller should have opened hti
 *
 * - searches from node "top" for matches of "keyword"
 * - creates menu with "title" given
 * - returns error code
 **********************************************************************/

/* jms 7/3/92 -- windows-izing begun */

long
ti_kwd(hti, mp, keyword, top, title)
     TI_H hti;
     ti_menu **mp;
     char *keyword;
     long top;
     char *title;
{
  char newquery[64];

  TI_LP lpti;

  lpti = (TI_LP)TI_DEREFERENCE(hti);

  /* initialize new menu */
  TI_MEM(*mp = New(ti_menu));
  bzero(*mp, sizeof(ti_menu));

  /* form query b:keyword:top */
  if (top) { sprintf(newquery, "b:%.48s:%d", keyword, top); }
  else { sprintf(newquery, "b:%.60s", keyword); }
  TI_MEM((*mp)->query = ti_dupstring(newquery)); /* XXX small leak */

  /* insert title */
  TI_MEM((*mp)->title = ti_dupstring(title)); /* XXX */

  /* success */
  lpti->links++;
  (*mp)->tp=lpti; /* !!!!!!!!!!!!! */
  CLEANUP_AND_RETURN(0L)

CLEANUP:
  TI_REMOVE_DEREFERENCE(hti, lpti);
  return(code);
}
