home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 1 / Mecomp-CD.iso / amiga / tools / system / shutdown / libmain.c < prev    next >
C/C++ Source or Header  |  1992-10-08  |  6KB  |  273 lines

  1. /*
  2. **    Shutdown 2.0 package, LibMain.c module
  3. **
  4. **    Copyright © 1992 by Olaf `Olsen' Barthel
  5. **        All Rights Reserved
  6. */
  7.  
  8. #include "ShutdownGlobal.h"
  9.  
  10. #include "shutdown.library.h"
  11.  
  12. STATIC STRPTR VersTag = VERSTAG;
  13.  
  14.     /* The structure expected by the library loader (auto-init). */
  15.  
  16. struct InitTable
  17. {
  18.     ULONG     it_DataSize;    /* Data size to allocate. */
  19.     APTR    *it_FuncTable;    /* Pointer to function table. */
  20.     APTR     it_DataInit;    /* Pointer to data initializers (remember InitStruct?). */
  21.     APTR     it_InitFunc;    /* The real library init function. */
  22. };
  23.  
  24.     /* Protos for this module. */
  25.  
  26. struct ShutdownBase * __saveds __asm        LibInit(register __d0 struct ShutdownBase *ShutdownBase,register __a0 BPTR SegList);
  27. struct ShutdownBase * __saveds __asm        LibOpen(register __a6 struct ShutdownBase *ShutdownBase);
  28. BPTR __saveds __asm                LibClose(register __a6 struct ShutdownBase *ShutdownBase);
  29. BPTR __saveds __asm                LibExpunge(register __a6 struct ShutdownBase *ShutdownBase);
  30. LONG __saveds                    LibNull(VOID);
  31.  
  32.     /* Pointer to library segment list. */
  33.  
  34. STATIC BPTR    LibSegList;
  35.  
  36.     /* ASCII library ID. */
  37.  
  38. UBYTE __aligned    LibName[]    = "shutdown.library";
  39. UBYTE __aligned    LibID[]        = VSTRING;
  40.  
  41.     /* File version string. */
  42.  
  43. STATIC UBYTE    VersionTag[]    = VERSTAG;
  44.  
  45.     /* The list of library functions. */
  46.  
  47. APTR __aligned    LibFuncTab[] =
  48. {
  49.     LibOpen,        /* Standard library routines. */
  50.     LibClose,
  51.     LibExpunge,
  52.     LibNull,
  53.  
  54.     RexxDispatch,
  55.     AddShutdownInfoTagList,
  56.     RemShutdownInfo,
  57.     Shutdown,
  58.  
  59.     (APTR)-1        /* End marker. */
  60. };
  61.  
  62.     /* The romtag needs this. */
  63.  
  64. struct InitTable LibInitTab =
  65. {
  66.     sizeof(struct ShutdownBase),    /* Lib base. */
  67.     LibFuncTab,            /* Function table. */
  68.     NULL,                /* No data init table (we'll do autoinit). */
  69.     LibInit                /* Lib init routine. */
  70. };
  71.  
  72.     /* LibInit(ShutdownBase,SegList):
  73.      *
  74.      *    Does the main library initialization, expects
  75.      *    all arguments in registers.
  76.      */
  77.  
  78. struct ShutdownBase * __saveds __asm
  79. LibInit(register __d0 struct ShutdownBase *ShutdownBase,register __a0 BPTR SegList)
  80. {
  81.     SysBase = *(struct ExecBase **)4;
  82.  
  83.     if(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",37))
  84.     {
  85.         if(UtilityBase = OpenLibrary("utility.library",37))
  86.         {
  87.             if(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",37))
  88.             {
  89.                 if(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",37))
  90.                 {
  91.                     if(GadToolsBase = OpenLibrary("gadtools.library",37))
  92.                     {
  93.                         RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME,0);
  94.  
  95.                             /* Remember segment list. */
  96.  
  97.                         LibSegList = SegList;
  98.  
  99.                             /* Fill in the library node head. */
  100.  
  101.                         ShutdownBase -> LibNode . lib_Node . ln_Type    = NT_LIBRARY;
  102.                         ShutdownBase -> LibNode . lib_Node . ln_Name    = LibName;
  103.  
  104.                             /* Set the remaining flags. */
  105.  
  106.                         ShutdownBase -> LibNode . lib_Flags        = LIBF_SUMUSED | LIBF_CHANGED;
  107.                         ShutdownBase -> LibNode . lib_Version        = VERSION;
  108.                         ShutdownBase -> LibNode . lib_Revision        = REVISION;
  109.                         ShutdownBase -> LibNode . lib_IdString        = (APTR)LibID;
  110.  
  111.                         InitSemaphore(&ShutdownBase -> BlockLock);
  112.                         InitSemaphore(&ShutdownBase -> DevBlockLock);
  113.                         InitSemaphore(&ShutdownBase -> AccessLock);
  114.                         InitSemaphore(&ShutdownBase -> ShutdownLock);
  115.  
  116.                         NewList((struct List *)&ShutdownBase -> AccessList);
  117.                         NewList((struct List *)&ShutdownBase -> ShutdownList);
  118.  
  119.                         ShutdownBase -> Main = (APTR)Main;
  120.  
  121.                         GlobalBase = ShutdownBase;
  122.  
  123.                             /* Return the result (surprise!). */
  124.  
  125.                         return(ShutdownBase);
  126.                     }
  127.  
  128.                     CloseLibrary(GfxBase);
  129.                 }
  130.  
  131.                 CloseLibrary(IntuitionBase);
  132.             }
  133.  
  134.             CloseLibrary(UtilityBase);
  135.         }
  136.  
  137.         CloseLibrary(DOSBase);
  138.     }
  139.  
  140.     return(NULL);
  141. }
  142.  
  143.     /* LibOpen():
  144.      *
  145.      *    Library open routine.
  146.      */
  147.  
  148. struct ShutdownBase * __saveds __asm
  149. LibOpen(register __a6 struct ShutdownBase *ShutdownBase)
  150. {
  151.         /* Increment open count and prevent delayed
  152.          * expunges.
  153.          */
  154.  
  155.     ShutdownBase -> LibNode . lib_OpenCnt++;
  156.     ShutdownBase -> LibNode . lib_Flags &= ~LIBF_DELEXP;
  157.  
  158.         /* Return base pointer. */
  159.  
  160.     return(ShutdownBase);
  161. }
  162.  
  163.     /* LibClose():
  164.      *
  165.      *    Closes the library.
  166.      */
  167.  
  168. BPTR __saveds __asm
  169. LibClose(register __a6 struct ShutdownBase *ShutdownBase)
  170. {
  171.     BPTR SegList = NULL;
  172.  
  173.         /* Is the library user count ok? */
  174.  
  175.     if(ShutdownBase -> LibNode . lib_OpenCnt)
  176.     {
  177.             /* Decrement user count. */
  178.  
  179.         ShutdownBase -> LibNode . lib_OpenCnt--;
  180.  
  181.             /* Try the expunge. */
  182.  
  183.         SegList = LibExpunge(ShutdownBase);
  184.     }
  185.  
  186.         /* Return the segment list, ramlib will know
  187.          * what to do with it.
  188.          */
  189.  
  190.     return(SegList);
  191. }
  192.  
  193.     /* LibExpunge(ShutdownBase):
  194.      *
  195.      *    Expunge library, careful: this can be called by
  196.      *    ramlib without the rest of the library knowing
  197.      *    about it.
  198.      */
  199.  
  200. BPTR __saveds __asm
  201. LibExpunge(register __a6 struct ShutdownBase *ShutdownBase)
  202. {
  203.     BPTR SegList = NULL;
  204.  
  205.         /* Is the user count zero, the delayed expunge flag
  206.          * set and do we have a valid segment list?
  207.          */
  208.  
  209.     if(!ShutdownBase -> LibNode . lib_OpenCnt && (ShutdownBase -> LibNode . lib_Flags & LIBF_DELEXP) && LibSegList && !ShutdownBase -> Running)
  210.     {
  211.             /* Remember segment list. */
  212.  
  213.         SegList = LibSegList;
  214.  
  215.             /* Set real segment list to zero which will
  216.              * hopefully keep us from getting expunged
  217.              * twice.
  218.              */
  219.  
  220.         LibSegList = NULL;
  221.  
  222.         if(RexxSysBase)
  223.             CloseLibrary(RexxSysBase);
  224.  
  225.         if(GadToolsBase)
  226.             CloseLibrary(GadToolsBase);
  227.  
  228.         if(GfxBase)
  229.             CloseLibrary(GfxBase);
  230.  
  231.         if(IntuitionBase)
  232.             CloseLibrary(IntuitionBase);
  233.  
  234.         if(UtilityBase)
  235.             CloseLibrary(UtilityBase);
  236.  
  237.         if(DOSBase)
  238.             CloseLibrary(DOSBase);
  239.  
  240.             /* Remove library from lib list. */
  241.  
  242.         Remove(&ShutdownBase -> LibNode . lib_Node);
  243.  
  244.             /* Free library/jump table memory. */
  245.  
  246.         FreeMem((BYTE *)((ULONG)ShutdownBase - ShutdownBase -> LibNode . lib_NegSize),ShutdownBase -> LibNode . lib_NegSize + ShutdownBase -> LibNode . lib_PosSize);
  247.     }
  248.     else
  249.     {
  250.         /* In any other case we'll set the delayed
  251.          * expunge flag (so next expunge call will
  252.          * hopefully wipe us from the lib list).
  253.          */
  254.  
  255.         ShutdownBase -> LibNode . lib_Flags |= LIBF_DELEXP;
  256.     }
  257.  
  258.         /* Return segment list pointer. */
  259.  
  260.     return(SegList);
  261. }
  262.  
  263.     /* LibNull():
  264.      *
  265.      *    Null function: do nothing, return NULL.
  266.      */
  267.  
  268. LONG __saveds
  269. LibNull()
  270. {
  271.     return(NULL);
  272. }
  273.