/** rexxmathlib.c
*
*   DESCRIPTION:
*   ===========
*
*	An exec library compiled with Aztec 3.40b, small model.
*
*	Based on "elib", an example library:
*	created by jim mackraz using mylib.asm by neil katin.
*	May be used and distributed providing this comment block
*	is retained in the source code.
*
*   THIS VERSION:
*   ============
*
*	This version implements a math support library for use with AmigaREXX.
*
*	This file only handles the calls to Open, Close and Expunge.
*	The real work is done by the routine Dispatch(), in dispatch.c, and
*	the functions it calls.
*
*   AUTHOR/DATE:  W.G.J. Langeveld, November 1987.
*   ============
*
**/

#include "rexxmathlib.h"

extern PFL   libfunctab[];   /* RXML function table (libface.asm)     */
extern LONG  funkyInit();    /* hacked up version of Aztec crt0.a68   */

LONG RXMLExpunge();

struct InitTable RXMLInitTab =  {
   sizeof (struct RXMLBase),
   libfunctab,
   NULL,                     /* will initialize RXML data in funkymain() */
   funkyInit
};

#define RXMLREVISION 0

char RXMLname[] = "rexxmathlib.library";
char RXMLid[]   = "rexxmathlib 1.0 (Nov 87)\r\n";

extern struct Resident RXMLRomTag;

struct Library *RexxSysBase = NULL;

/**
*
*   This function is Jim's C-language library initRoutine.  It is called
*   by funkyInit() after register saves and small model initialization is
*   done.
*
**/
LONG funkymain(libbase, seglist)
struct RXMLBase *libbase;
ULONG seglist;
{
   register   struct RXMLBase *base;
/*
*   Cookie
*/
   base = libbase;
/*
*   Debug kind of stuff
*/
   base->mb_Cookie = 0xDEAD1234;
   base->mb_SegList = seglist;
/*
*   init. library structure (since I don't do automatic data init.)
*/
   base->mb_Lib.lib_Node.ln_Type = NT_LIBRARY;
   base->mb_Lib.lib_Node.ln_Name = (char *) RXMLname;   
   base->mb_Lib.lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
   base->mb_Lib.lib_Version = RXMLRomTag.rt_Version;
   base->mb_Lib.lib_Revision = RXMLREVISION;
   base->mb_Lib.lib_IdString = (APTR) RXMLid;
/*
*   Open the REXX system library.
*/

   RexxSysBase = (struct Library *) OpenLibrary("rexxsyslib.library", 0L);

   return;
}


/**
*
*   Open library. Baseptr in A6, version in D0.
*
**/
LONG RXMLOpen(base)
struct RXMLBase *base;
{
/*
*   mark us as having another customer
*/
   base->mb_Lib.lib_OpenCnt++;
/*
*   prevent delayed expunges (standard procedure)
*/
   base->mb_Lib.lib_Flags &= ~LIBF_DELEXP;

   return ((LONG) base);
}

/**
*
*   Close library
*
**/
LONG RXMLClose(base)
struct RXMLBase *base;
{
   LONG retval = 0;

   if ((--base->mb_Lib.lib_OpenCnt == 0) &&
         (base->mb_Lib.lib_Flags & LIBF_DELEXP)) {
/*
*   No more people have me open,
*   and I have a delayed expunge pending:
*   return segment list.
*/
      retval = RXMLExpunge();
   }

   return (retval);
}

/**
*
*   Expunge library
*
**/
LONG RXMLExpunge(base)
struct RXMLBase *base;
{
   ULONG seglist = 0;
   LONG libsize;

   if (base->mb_Lib.lib_OpenCnt == 0) {
/*
*   Really expunge: remove libbase and freemem
*/
      seglist   = base->mb_SegList;

      Remove(base);

      libsize = base->mb_Lib.lib_NegSize + base->mb_Lib.lib_PosSize;

      FreeMem((char *) base - base->mb_Lib.lib_NegSize, (LONG) libsize);
/*
*   Close the REXX system library
*/
      if (RexxSysBase) CloseLibrary(RexxSysBase);
   }
   else {
      base->mb_Lib.lib_Flags &= LIBF_DELEXP;
   }
/*
*   Return NULL or real seglist
*/
   return ((LONG) seglist);
}


