/* $Revision Header * Header built automatically - do not edit! *************
 *
 *	(C) Copyright 1990 by ???
 *
 *	Name .....: LibMain.c
 *	Created ..: Sunday 07-Jan-90 18:55
 *	Revision .: 6
 *
 *	Date            Author          Comment
 *	=========       ========        ====================
 *	07-Jan-90       Olsen           - Empty log message -
 *	07-Jan-90       Olsen           Moved OpenLibrary call
 *	07-Jan-90       Olsen           - Empty log message -
 *	07-Jan-90       Olsen           Created this file!
 *
 ****************************************************************************
 *
 *	This Amiga shared library is based on example source code
 *	written by Gary Samad & Bill Hawes. It also employs basic
 *	library concepts introduced by Jimm Mackraz (ELib) and
 *	Edwin Hoogerbeets (MkLib). This library was generated using
 *	a customized version of the MkLib utility.
 *
 ****************************************************************************
 *
 *	An exec library compiled with Aztec 3.6a, 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.
 *
 * $Revision Header ********************************************************/
 #define REVISION 6

#include <exec/libraries.h>
#include <devices/keymap.h>
#include <exec/resident.h>
#include <exec/memory.h>
#include <exec/io.h>

#include "RexxHostBase.h"

typedef LONG (*PFL)();	/* pointer to function returning 32-bit int */

	/* Extern version reference. */

extern LONG Version;

	/* Library identifiers. */

char *LibName	= "rexxhost.library";
char *LibId	= "rexxhost.library 1.6 (07 Jan 1990)\r\n";

	/* Some more extern references. */

extern struct Resident	LibRomTag;
extern PFL		LibFuncTab[];
extern LONG		LibInit();
extern APTR		OpenLibrary();

	/* Main rexx library base. */

struct RxsLib *RexxSysBase = NULL;

	/* Library initialization table, used for AUTOINIT libraries. */

struct InitTable
{
	ULONG	 it_DataSize;	/* Library data space size. */
	PFL	*it_FuncTable;	/* Table of entry points. */
	APTR	 it_DataInit;	/* Table of data initializers. */
	PFL	 it_InitFunc;	/* Initialization function to run. */
};

	/* Will do auto-init in LibInit. */

struct InitTable LibInitTab =
{
	sizeof(struct RexxHostBase),
	LibFuncTab,
	NULL,
	LibInit
};

	/* LibMain(LibBase,SegList) :
	 *
	 *	This function is Jimm's C-language library init-routine.
	 *	It is called by LibInit() after register saves and
	 *	small model initialization is done.
	 */

LONG
LibMain(LibBase,SegList)
struct RexxHostBase *LibBase;
ULONG SegList;
{
	register struct RexxHostBase *TmpLibBase = LibBase;

		/* Magic cookie. */

	TmpLibBase -> rhb_SegList = SegList;

		/* Init. library structure (since I don't do
		 * automatic data init).
		 */

	TmpLibBase -> rhb_Lib . lib_Node . ln_Type	= NT_LIBRARY;
	TmpLibBase -> rhb_Lib . lib_Node . ln_Name	= (char *)LibName;
	TmpLibBase -> rhb_Lib . lib_Flags		= LIBF_SUMUSED | LIBF_CHANGED;
	TmpLibBase -> rhb_Lib . lib_Version		= LibRomTag . rt_Version;
	TmpLibBase -> rhb_Lib . lib_Revision		= Version;
	TmpLibBase -> rhb_Lib . lib_IdString		= (APTR)LibId;

		/* Leave it zero. */
	
	TmpLibBase -> RexxSysBase			= NULL;
}

	/* RealOpen(LibBase) :
	 *
	 *	Open library. Baseptr in A6, version in D0.
	 */

LONG
RealOpen(LibBase)
struct RexxHostBase *LibBase;
{
		/* Since we can't open the library in the main
		 * init routine (task switching turned off,
		 * as well as interrupt handling) we try to
		 * open it in the open routine.
		 */

	if(!RexxSysBase)
		if(!(RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME,0)))
			return(NULL);

		/* Mark us as having another customer, this is a reentrant
		 * library.
		 */

	LibBase -> rhb_Lib . lib_OpenCnt++;
	LibBase -> rhb_Lib . lib_Flags &= ~LIBF_DELEXP;

		/* Add the rexx library pointer. */

	LibBase -> RexxSysBase = RexxSysBase;

	return((LONG)LibBase);
}

	/* RealExpunge(LibBase) :
	 *
	 *	Expunge library.
	 */

LONG
RealExpunge(LibBase)
struct RexxHostBase *LibBase;
{
	ULONG SegList = 0;
	LONG LibSize;

	if(LibBase -> rhb_Lib . lib_OpenCnt == 0)
	{
			/* Really expunge: remove libbase and freemem. */

		SegList = LibBase -> rhb_SegList;

		Remove(LibBase);

		LibSize = LibBase -> rhb_Lib . lib_NegSize + LibBase -> rhb_Lib .lib_PosSize;
		FreeMem((char *)LibBase - LibBase -> rhb_Lib . lib_NegSize,(LONG)LibSize);

		if(RexxSysBase)
			CloseLibrary(RexxSysBase);
	}
	else
		LibBase -> rhb_Lib . lib_Flags &= LIBF_DELEXP;

		/* Return NULL or real seglist. */

	return((LONG)SegList);
}

	/* RealClose(LibBase) :
	 *
	 *	Close library.
	 */

LONG
RealClose(LibBase)
struct RexxHostBase *LibBase;
{
	LONG ReturnValue = 0;

		/* No more people have me open,
		 * and I have a delayed expunge pending:
		 * return segment list.
		 */

	if((--LibBase -> rhb_Lib . lib_OpenCnt == 0) && (LibBase -> rhb_Lib . lib_Flags & LIBF_DELEXP))
		ReturnValue = RealExpunge();

	return(ReturnValue);
}
