/*
 *  Amiga Shared library code for the GNU regular expression package.
 *  Edwin Hoogerbeets 18/07/89
 *
 *  This file may be copied and distributed under the GNU Public
 *  Licence. See the comment at the top of regex.c for details.
 *
 *  Adapted from Elib by Jim Mackraz, mklib by Edwin Hoogerbeets, and the
 *  GNU regular expression package by the Free Software Foundation.
 */


/* 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
 */
#define NULL 0L
#include "library.h"
#include <exec/memory.h>

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

LONG    myExpunge();

struct InitTable myInitTab =  {
  sizeof (struct RegexBase),
  libfunctab,
  NULL,           /* will initialize my data in funkymain()       */
  funkyInit
};

#define MYREVISION      0   /* would be nice to auto-increment this     */

extern char myname[];
extern char myid[];

extern struct Resident  myRomTag;

/*
 * this function is my C-language library initRoutine.  It is called
 * by funkyInit() after register saves and small model initialization is
 * done.
 */

LONG
funkymain(libbase, seglist)
struct  RegexBase  *libbase;
ULONG                   seglist;
{
  register        struct RegexBase *base;

  /* cookie       */
  base = libbase;
  base->rb_Cookie = 0xDEAD1234;   /* debug kind of stuff                  */
  base->rb_SegList = seglist;

  /* init. library structure (since I don't do automatic data init.)      */
  base->rb_Lib.lib_Node.ln_Type = NT_LIBRARY;
  base->rb_Lib.lib_Node.ln_Name = (char *) myname;
  base->rb_Lib.lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
  base->rb_Lib.lib_Version = myRomTag.rt_Version;
  base->rb_Lib.lib_Revision = MYREVISION;
  base->rb_Lib.lib_IdString = (APTR) myid;

  return(0);
}

LONG
myOpen(base)    /* baseptr in A6, version in D0 */
struct  RegexBase *base;
{
  /* mark us as having another customer                                   */
  base->rb_Lib.lib_OpenCnt++;

  /* prevent delayed expunges (standard procedure)                */
  base->rb_Lib.lib_Flags &= ~LIBF_DELEXP;

  return ((LONG) base);
}

LONG
myClose(base)
struct  RegexBase *base;
{
  LONG    retval = 0;

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

  return (retval);
}

LONG
myExpunge(base)
struct  RegexBase  *base;
{
  ULONG seglist = 0;
  LONG  libsize;

  if (base->rb_Lib.lib_OpenCnt == 0)
  {
    /* really expunge: remove libbase and freemem   */

    seglist = base->rb_SegList;

    Remove(base);

    libsize = base->rb_Lib.lib_NegSize + base->rb_Lib.lib_PosSize;
    FreeMem((char *) base - base->rb_Lib.lib_NegSize, (LONG) libsize);
  }
  else
  {
    base->rb_Lib.lib_Flags &= LIBF_DELEXP;
  }


  /* return NULL or real seglist */
  return ((LONG) seglist);
}

