home *** CD-ROM | disk | FTP | other *** search
/ Dream 41 / Amiga_Dream_41.iso / Amiga / Programmation / envWRD44.lha / envWRD44 / source / words / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-17  |  5.9 KB  |  221 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "defs.h"
  15.  
  16. Prototype LibCall struct Library *LibInit   (__D0 BPTR);
  17. Prototype LibCall struct Library *LibOpen   (__D0 long, __A0 struct Library *);
  18. Prototype LibCall long            LibClose  (__A0 struct Library *);
  19. Prototype LibCall long            LibExpunge(__A0 struct Library *);
  20.  
  21. struct Library  *LibBase       = NULL;
  22. struct Library  *IntuitionBase = NULL;
  23. struct ExecBase *SysBase       = NULL;
  24. struct Library  *DOSBase       = NULL;
  25. struct Library  *DiskfontBase  = NULL;
  26. struct Library  *RexxSysBase   = NULL;
  27. struct GfxBase  *GfxBase       = NULL;
  28. struct Library  *GadToolsBase  = NULL;
  29. struct Library  *ReqToolsBase  = NULL;
  30.  
  31. BPTR SegList = 0;
  32.  
  33. /*
  34.  *    The Initialization routine is given only a seglist pointer.  Since
  35.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  36.  *    and return either NULL or the library pointer.  Exec has Forbid()
  37.  *    for us during the call.
  38.  *
  39.  *    We use an extended library structure to allow identification as an
  40.  *    API client
  41.  */
  42.  
  43. LibCall struct Library *
  44. LibInit(__D0 BPTR segment)
  45. {
  46.     struct Library *lib;
  47.  
  48.     static const long Vectors[] = {
  49.  
  50.         (long)ALibOpen,
  51.         (long)ALibClose,
  52.         (long)ALibExpunge,
  53.         (long)ALibReserved,
  54.         (long)APIMountClient,
  55.         (long)APICloseClient,
  56.         (long)APIBriefClient,
  57.         (long)APIFree,
  58.         -1
  59.     };
  60.  
  61.     SysBase = (struct ExecBase *)*(long *)4;
  62.  
  63.     if (DOSBase = OpenLibrary("dos.library", 0)) {
  64.  
  65.         if (DiskfontBase = OpenLibrary("diskfont.library", 36)) {
  66.  
  67.             if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 36)) {
  68.  
  69.                 if (IntuitionBase = OpenLibrary("intuition.library", 36)) {
  70.  
  71.                     if (RexxSysBase = OpenLibrary("rexxsyslib.library", 36)) {
  72.  
  73.                         if (GadToolsBase = OpenLibrary("gadtools.library", 36)) {
  74.  
  75.                             if (ReqToolsBase = OpenLibrary("reqtools.library", 36)) {
  76.  
  77.                                 if (InitC()) {
  78.  
  79.                                     if (LibBase = lib = MakeLibrary((APTR)Vectors, NULL, NULL, sizeof(struct APIBase), (BPTR)NULL)) {
  80.  
  81.                                         lib->lib_Node.ln_Type = NT_LIBRARY;
  82.                                         lib->lib_Node.ln_Name = LibName;
  83.                                         lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  84.                                         lib->lib_Version      = 37;
  85.                                         lib->lib_Revision     = 0;
  86.                                         lib->lib_IdString     = (APTR)LibId;
  87.  
  88.                                         ((struct APIBase *)lib)->Magic = API_MAGIC;
  89.  
  90.                                         SegList = segment;
  91.  
  92.                                         AddLibrary(lib);
  93.  
  94.                                         return(lib);
  95.                                     }
  96.                                 }
  97.                             }
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104.  
  105.     return(NULL);
  106. }
  107.  
  108. /*
  109.  *    Open is given the library pointer and the version request.  Either
  110.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  111.  *    Exec has Forbid() for us during the call.
  112.  */
  113.  
  114. LibCall struct Library *
  115. LibOpen(__D0 long version, __A0 struct Library *lib)
  116. {
  117.     ++lib->lib_OpenCnt;
  118.  
  119.     lib->lib_Flags &= ~LIBF_DELEXP;
  120.  
  121.     return(lib);
  122. }
  123.  
  124. /*
  125.  *    Close is given the library pointer and the version request.  Be sure
  126.  *    not to decrement the open count if already zero.  If the open count
  127.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  128.  *    and return the seglist.  Otherwise we return NULL.
  129.  *
  130.  *    Note that this routine never sets LIBF_DELEXP on its own.
  131.  *
  132.  *    Exec has Forbid() for us during the call.
  133.  */
  134.  
  135. LibCall long
  136. LibClose(__A0 struct Library *lib)
  137. {
  138.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  139.         return(NULL);
  140.  
  141.     if (lib->lib_Flags & LIBF_DELEXP)
  142.         return(LibExpunge(lib));
  143.  
  144.     return(NULL);
  145. }
  146.  
  147. /*
  148.  *    We expunge the library and return the Seglist ONLY if the open count
  149.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  150.  *    flag and return NULL.
  151.  *
  152.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  153.  *    might be called from the memory allocator and thus we CANNOT DO A
  154.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  155.  *
  156.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  157.  *    therefore freeze if we called it ourselves.  As far as I can tell
  158.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  159.  *    below.
  160.  */
  161.  
  162. LibCall long
  163. LibExpunge(__A0 struct Library *lib)
  164. {
  165.     if (lib->lib_OpenCnt) {
  166.  
  167.         lib->lib_Flags |= LIBF_DELEXP;
  168.         return(NULL);
  169.     }
  170.  
  171.     ExitC();
  172.  
  173.     Remove(&lib->lib_Node);
  174.  
  175.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  176.  
  177.     if (ReqToolsBase) {
  178.  
  179.         CloseLibrary(ReqToolsBase);
  180.         ReqToolsBase = NULL;
  181.     }
  182.  
  183.     if (GadToolsBase) {
  184.  
  185.         CloseLibrary(GadToolsBase);
  186.         GadToolsBase = NULL;
  187.     }
  188.  
  189.     if (RexxSysBase) {
  190.  
  191.         CloseLibrary(RexxSysBase);
  192.         RexxSysBase = NULL;
  193.     }
  194.  
  195.     if (IntuitionBase) {
  196.  
  197.         CloseLibrary(IntuitionBase);
  198.         IntuitionBase = NULL;
  199.     }
  200.  
  201.     if (GfxBase) {
  202.  
  203.         CloseLibrary(GfxBase);
  204.         GfxBase = NULL;
  205.     }
  206.  
  207.     if (DiskfontBase) {
  208.  
  209.         CloseLibrary(DiskfontBase);
  210.         DiskfontBase = NULL;
  211.     }
  212.  
  213.     if (DOSBase) {
  214.  
  215.         CloseLibrary(DOSBase);
  216.         DOSBase = NULL;
  217.     }
  218.  
  219.     return((long)SegList);
  220. }
  221.