/*(( "Kopf" */
/* -----------------------------------------------------------------------------

   $Id: StartUp.c,v 1.1 1997/06/11 19:03:52 mshopf Exp mshopf $

   GoldED API client, ©1997 Matthias Hopf.
   Compiled with SasC.


   Library startup code.

   ------------------------------------------------------------------------------
*/

/*)) */
/*(( "Includes" */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <exec/execbase.h>
#include <exec/resident.h>
#include <exec/initializers.h>

#ifdef __MAXON__
#include <pragma/exec_lib.h>
#include <linkerfunc.h>
#else
#include <proto/exec.h>    // all other compilers
#endif
#include "compiler.h"

#include "smartindent.h"

#include "LibFuncs.h"

/*)) */
/*(( "Prototypes" */

extern ULONG __saveds __stdargs L_OpenLibs(void);
extern void  __saveds __stdargs L_CloseLibs(void);

struct SmartindentBase * __saveds ASM InitLib( register __a6 struct ExecBase *sysbase GNUCREG("a6"),
					       register __a0 APTR           *seglist GNUCREG("a0"),
					       register __d0 struct SmartindentBase *srm GNUCREG("d0"));
struct SmartindentBase * __saveds ASM OpenLib( register __a6 struct SmartindentBase *SmartindentBase GNUCREG("a6"));
APTR __saveds ASM CloseLib( register __a6 struct SmartindentBase *SmartindentBase GNUCREG("a6"));
APTR __saveds ASM ExpungeLib( register __a6 struct SmartindentBase *srm GNUCREG("a6"));
ULONG ASM ExtFuncLib(void);

extern APTR FuncTab [];
extern struct MyDataInit DataTab;
extern struct SmartindentBase *SmartindentBase;

/*)) */

/*(( "LibStart ()" */

LONG ASM LibStart(void)
{
    return(-1);
}


/*)) */
/*(( "InitTable + FuncTab" */

struct InitTable                        /* do not change */
{
    ULONG              LibBaseSize;
    APTR              *FunctionTable;
    struct MyDataInit *DataTable;
    APTR               InitLibTable;
} InitTab =
{
    sizeof(struct SmartindentBase),
    &FuncTab[0],
    &DataTab,
    InitLib
};

APTR FuncTab [] =
{
    OpenLib,
    CloseLib,
    ExpungeLib,
    ExtFuncLib,
    L_MountClient,
    L_CloseClient,
    L_BriefClient,
    L_Free,

    (APTR) ((LONG)-1)
};


/*)) */
/*(( "InitLib ()" */

struct SmartindentBase * __saveds ASM InitLib( register __a6 struct ExecBase        *sysbase GNUCREG("a6"),
					       register __a0 APTR                   *seglist GNUCREG("a0"),
					       register __d0 struct SmartindentBase *srm     GNUCREG("d0"))
{
    SmartindentBase = srm;

    SmartindentBase->SegList = seglist;
    SmartindentBase->API.Magic = API_MAGIC;

    if(L_OpenLibs()) return(SmartindentBase);

    L_CloseLibs();

    return(NULL);
}


/*)) */
/*(( "OpenLib ()" */

struct SmartindentBase * __saveds ASM OpenLib( register __a6 struct SmartindentBase *SmartindentBase GNUCREG("a6"))
{
#ifdef __MAXON__
    GetBaseReg();
    InitModules();
#endif

    SmartindentBase->API.Library.lib_OpenCnt++;

    SmartindentBase->API.Library.lib_Flags &= ~LIBF_DELEXP;

    return(SmartindentBase);
}


/*)) */
/*(( "CloseLib ()" */

APTR __saveds ASM CloseLib( register __a6 struct SmartindentBase *SmartindentBase GNUCREG("a6"))
{
    SmartindentBase->API.Library.lib_OpenCnt--;

    if(!SmartindentBase->API.Library.lib_OpenCnt)
    {
	if(SmartindentBase->API.Library.lib_Flags & LIBF_DELEXP)
	{
	    return( ExpungeLib(SmartindentBase) );
	}
    }

    return(NULL);
}


/*)) */
/*(( "ExpungeLib ()" */

APTR __saveds ASM ExpungeLib( register __a6 struct SmartindentBase *srm GNUCREG("a6"))
{
    struct SmartindentBase *SmartindentBase = srm;
    APTR seglist;

    if(!SmartindentBase->API.Library.lib_OpenCnt)
    {
	ULONG negsize, possize, fullsize;
	UBYTE *negptr = (UBYTE *) SmartindentBase;

	seglist = SmartindentBase->SegList;

	Remove((struct Node *)SmartindentBase);

	L_CloseLibs();

	negsize  = SmartindentBase->API.Library.lib_NegSize;
	possize  = SmartindentBase->API.Library.lib_PosSize;
	fullsize = negsize + possize;
	negptr  -= negsize;

	FreeMem(negptr, fullsize);

#ifdef __MAXON__
	CleanupModules();
#endif

	return(seglist);
    }
  
    SmartindentBase->API.Library.lib_Flags |= LIBF_DELEXP;

    return(NULL);
}


/*)) */
/*(( "ExtFuncLib ()" */

ULONG ASM ExtFuncLib(void)
{
    return(NULL);
}


/*)) */

struct SmartindentBase *SmartindentBase = NULL;

/*(( "SasC Hacks" */

#ifdef __SASC

#ifdef ARK_OLD_STDIO_FIX

ULONG XCEXIT       = NULL;              /* these symbols may be referenced by */
ULONG _XCEXIT      = NULL;              /* some functions of sc.lib, but should */
ULONG ONBREAK      = NULL;              /* never be used inside a shared library */
ULONG _ONBREAK     = NULL;
ULONG base         = NULL;
ULONG _base        = NULL;
ULONG ProgramName  = NULL;
ULONG _ProgramName = NULL;
ULONG StackPtr     = NULL;
ULONG _StackPtr    = NULL;
ULONG oserr        = NULL;
ULONG _oserr       = NULL;
ULONG OSERR        = NULL;
ULONG _OSERR       = NULL;

#endif /* ARK_OLD_STDIO_FIX */

void __regargs __chkabort(void) { }     /* a shared library cannot be */
void __regargs _CXBRK(void) { }         /* CTRL-C aborted when doing I/O */

#endif /* __SASC */

/*)) */
