home *** CD-ROM | disk | FTP | other *** search
/ Amiga Times / AmigaTimes.iso / programme / GoldED / developer / examples / syntax / warpcpp / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-06  |  4.5 KB  |  172 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.  
  23. struct Library *SysBase  = NULL;    // -olsen
  24. struct Library *DOSBase  = NULL;
  25. BPTR            SegList  = 0;
  26.  
  27. /*
  28.  *    The Initialization routine is given only a seglist pointer.  Since
  29.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  30.  *    and return either NULL or the library pointer.  Exec has Forbid()
  31.  *    for us during the call.
  32.  *
  33.  *    We use an extended library structure to allow identification as a
  34.  *    GoldED syntax scanner.
  35.  */
  36.  
  37. LibCall struct Library *
  38. LibInit(__D0 BPTR segment)
  39. {
  40.     struct Library *lib;
  41.  
  42.     static const long Vectors[] = {
  43.  
  44.         (long)ALibOpen,
  45.         (long)ALibClose,
  46.         (long)ALibExpunge,
  47.         (long)ALibReserved,
  48.  
  49.         (long)MountScanner,
  50.         (long)StartScanner,
  51.         (long)CloseScanner,
  52.         (long)FlushScanner,
  53.         (long)SetupScanner,
  54.         (long)BriefScanner,
  55.         (long)ParseLine,
  56.         (long)UnparseLines,
  57.         (long)ParseSection,
  58.         -1
  59.     };
  60.  
  61.     SysBase = *(struct Library **)4;
  62.  
  63.     if (DOSBase = OpenLibrary("dos.library", 0)) {
  64.  
  65.         if (LibBase = lib = MakeLibrary((APTR)Vectors, NULL, NULL, sizeof(struct ParserBase), NULL)) {
  66.  
  67.             lib->lib_Node.ln_Type = NT_LIBRARY;
  68.             lib->lib_Node.ln_Name = LibName;
  69.             lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  70.             lib->lib_Version      = 37;
  71.             lib->lib_Revision     = 0;
  72.             lib->lib_IdString     = (APTR)LibId;
  73.  
  74.             ((struct ParserBase *)lib)->Magic = PARSER_MAGIC;
  75.  
  76.             SegList = segment;
  77.  
  78.             AddLibrary(lib);
  79.  
  80.             if( InitC() )       // -olsen
  81.                 return(lib);
  82.  
  83.             Remove(&lib->lib_Node);
  84.  
  85.             FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  86.         }
  87.  
  88.         CloseLibrary(DOSBase);
  89.         DOSBase = NULL;
  90.     }
  91.  
  92.     return(NULL);
  93. }
  94.  
  95. /*
  96.  *    Open is given the library pointer and the version request.  Either
  97.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  98.  *    Exec has Forbid() for us during the call.
  99.  */
  100.  
  101. LibCall struct Library *
  102. LibOpen(__D0 long version, __A0 struct Library *lib)
  103. {
  104.     ++lib->lib_OpenCnt;
  105.  
  106.     lib->lib_Flags &= ~LIBF_DELEXP;
  107.  
  108.     return(lib);
  109. }
  110.  
  111. /*
  112.  *    Close is given the library pointer and the version request.  Be sure
  113.  *    not to decrement the open count if already zero.  If the open count
  114.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  115.  *    and return the seglist.  Otherwise we return NULL.
  116.  *
  117.  *    Note that this routine never sets LIBF_DELEXP on its own.
  118.  *
  119.  *    Exec has Forbid() for us during the call.
  120.  */
  121.  
  122. LibCall long
  123. LibClose(__A0 struct Library *lib)
  124. {
  125.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  126.         return(NULL);
  127.  
  128.     if (lib->lib_Flags & LIBF_DELEXP)
  129.         return(LibExpunge(lib));
  130.  
  131.     return(NULL);
  132. }
  133.  
  134. /*
  135.  *    We expunge the library and return the Seglist ONLY if the open count
  136.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  137.  *    flag and return NULL.
  138.  *
  139.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  140.  *    might be called from the memory allocator and thus we CANNOT DO A
  141.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  142.  *
  143.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  144.  *    therefore freeze if we called it ourselves.  As far as I can tell
  145.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  146.  *    below.
  147.  */
  148.  
  149. LibCall long
  150. LibExpunge(__A0 struct Library *lib)
  151. {
  152.     if (lib->lib_OpenCnt) {
  153.  
  154.         lib->lib_Flags |= LIBF_DELEXP;
  155.         return(NULL);
  156.     }
  157.  
  158.     ExitC();    // -olsen
  159.  
  160.     Remove(&lib->lib_Node);
  161.  
  162.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  163.  
  164.     if (DOSBase) {
  165.  
  166.         CloseLibrary(DOSBase);
  167.         DOSBase = NULL;
  168.     }
  169.  
  170.     return((long)SegList);
  171. }
  172.