
/* TH.C             Robert A. Hearn */

#include <types.h>
#include <memory.h>
#include "th.h"
#include "tlex.h"
#include "sc.h"
#include "scparam.h"
#include "string.h"
/*#include "ctype.h"*/
#include "spdef.h"
#include "proxio.h"

/* TH.C: This file contains the TH routines, which interface to the Proximity
   routines.  The assembly language interface to these routines is in thint.src.
 */

unsigned _THID;
extern unsigned _THActive[];
extern unsigned _SPActive[];
unsigned _THXVal;


#define THErr(e, x, r)     { _THErrNum[0] = (e); _THXVal = (x); return(r); }
#define THErr2(e, x)       { _THErrNum[0] = (e); _THXVal = (x); return; }

/* Startup the thesaurus. */

pascal void _THStartUp(mmid, thdir)
unsigned mmid;
char *thdir;
{
unsigned  len;
unsigned  x;

    if (_THActive[0])
        THErr2(THACTIVE, 0);
    _THErrNum[0] = 0;

    _SPTHID[0] = _THID = mmid + SPTHID;
    toolErr[0] = &_toolErr;                 /* &%^$%$!@@!@!! */

    if (!meminit())
        THErr2(THNOMEM, 0);

	len = *(unsigned *) thdir;
	thdir += 2;
	thdir[len] = '\0';
    strcat(thdir, "Dictionary");

    if (!Tlexalloc(thdir))
        THErr2(THPRODOS, *toolErr[0]);

    if (!thesopen())
    {
        Tlexfree();
        THErr2(THPRODOS, *toolErr[0]);
    }

    _THActive[0] = 1;
}

/* Shut down TH */

pascal void _THShutDown()
{
    if (!_THActive[0])
        THErr2(THINACTIVE, 0);

    thesclose();

    Tlexfree();

    _THErrNum[0] = _THActive[0] = 0;
}

pascal unsigned _THWord(theword)
char *theword;
{
unsigned num;

    if (!_THActive[0])
        THErr(THINACTIVE, 0, 0);

    p2cstr(theword);
    num = thesword(theword);
    c2pstr(theword);
    return (num);
}

pascal char *_THInfo(num, lists)
unsigned num;
char **lists[];
{
extern char *thesinfo();

char *thedef;
unsigned x;
char **p;

    if (!_THActive[0])
        THErr(THINACTIVE, 0, 0);

    thedef = thesinfo(num, lists);
    c2pstr(thedef);
    for (x = 0; x < 5; ++x)
    {
        p = lists[x];
        while (*p)
            c2pstr(*p++);
    }

    return (thedef);
}

