home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / envsof20 / source / syntax / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-01  |  6.6 KB  |  273 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.  * Copyright 1999 Thomas Aglassinger and others, see file "forum.txt"
  14.  */
  15.  
  16. #include "defs.h"
  17.  
  18. Prototype LibCall struct Library *LibInit   (__D0 BPTR);
  19. Prototype LibCall struct Library *LibOpen   (__D0 long, __A0 struct Library *);
  20. Prototype LibCall long            LibClose  (__A0 struct Library *);
  21. Prototype LibCall long            LibExpunge(__A0 struct Library *);
  22.  
  23. struct Library *LibBase = NULL;
  24. struct Library *SysBase = NULL;
  25. struct Library *DOSBase = NULL;
  26. struct Library *IntuitionBase = NULL;
  27.  
  28. BPTR SegList  = 0;
  29.  
  30. /*
  31.  *    The Initialization routine is given only a seglist pointer.  Since
  32.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  33.  *    and return either NULL or the library pointer.  Exec has Forbid()
  34.  *    for us during the call.
  35.  *
  36.  *    We use an extended library structure to allow identification as a
  37.  *    GoldED syntax scanner.
  38.  */
  39.  
  40. LibCall struct Library *
  41. LibInit(__D0 BPTR segment)
  42. {
  43.    struct Library *lib;
  44.  
  45.    static const long Vectors[] =
  46.    {
  47.  
  48.       (long) ALibOpen,
  49.       (long) ALibClose,
  50.       (long) ALibExpunge,
  51.       (long) ALibReserved,
  52.  
  53.       (long) MountScanner,
  54.       (long) StartScanner,
  55.       (long) CloseScanner,
  56.       (long) FlushScanner,
  57.       (long) SetupScanner,
  58.       (long) BriefScanner,
  59.       (long) ParseLine,
  60.       (long) UnparseLines,
  61.       (long) ParseSection,
  62.       -1
  63.    };
  64.  
  65.    SysBase = *(struct Library **) 4;
  66.  
  67.    if (DOSBase = OpenLibrary("dos.library", 0)) {
  68.  
  69.       if (IntuitionBase = OpenLibrary("intuition.library", 0)) {
  70.  
  71.          if (LibBase = lib = MakeLibrary((APTR) Vectors, NULL, NULL, sizeof(struct ParserBase), (BPTR) NULL)) {
  72.  
  73.             lib->lib_Node.ln_Type = NT_LIBRARY;
  74.             lib->lib_Node.ln_Name = LibName;
  75.             lib->lib_Flags = LIBF_CHANGED | LIBF_SUMUSED;
  76.             lib->lib_Version = 38;
  77.             lib->lib_Revision = 0;
  78.             lib->lib_IdString = (APTR) LibId;
  79.  
  80.             ((struct ParserBase *) lib)->Magic = PARSER_MAGIC;
  81.  
  82.             SegList = segment;
  83.  
  84.             AddLibrary(lib);
  85.  
  86.             if (InitC())
  87.                return (lib);
  88.  
  89.             Remove(&lib->lib_Node);
  90.  
  91.             FreeMem((char *) lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  92.          }
  93.          CloseLibrary(IntuitionBase);
  94.          IntuitionBase = NULL;
  95.       }
  96.       CloseLibrary(DOSBase);
  97.       DOSBase = NULL;
  98.    }
  99.    return (NULL);
  100. }
  101.  
  102.  
  103. #if 0
  104. LibCall struct Library *
  105. LibInit(__D0 BPTR segment)
  106. {
  107.     struct Library *lib;
  108.  
  109.     static const long Vectors[] = {
  110.  
  111.         (long)ALibOpen,
  112.         (long)ALibClose,
  113.         (long)ALibExpunge,
  114.         (long)ALibReserved,
  115.  
  116.         (long)MountScanner,
  117.         (long)StartScanner,
  118.         (long)CloseScanner,
  119.         (long)FlushScanner,
  120.         (long)SetupScanner,
  121.         (long)BriefScanner,
  122.         (long)ParseLine,
  123.         (long)UnparseLines,
  124.         (long)ParseSection,
  125.         -1
  126.     };
  127.  
  128.     SysBase = *(long *)4;
  129.  
  130.     if (DOSBase = (long)OpenLibrary("dos.library", 0)) {
  131.  
  132.         if (IntuitionBase = (long) OpenLibrary("intuition.library", 0)) {
  133.  
  134.             if (LibBase = lib = MakeLibrary((APTR)Vectors, NULL, NULL, sizeof(struct ParserBase), NULL)) {
  135.  
  136.                 lib->lib_Node.ln_Type = NT_LIBRARY;
  137.                 lib->lib_Node.ln_Name = LibName;
  138.                 lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  139.                 lib->lib_Version      = 37;
  140.                 lib->lib_Revision     = 0;
  141.                 lib->lib_IdString     = (APTR)LibId;
  142.  
  143.                 ((struct ParserBase *)lib)->Magic = PARSER_MAGIC;
  144.  
  145.                 SegList = segment;
  146.  
  147.                 AddLibrary(lib);
  148.  
  149.                 InitC();
  150.  
  151.                 return(lib);
  152.             }
  153.         }
  154.     }
  155.  
  156.     return(NULL);
  157. }
  158. #endif
  159.  
  160. /*
  161.  *    Open is given the library pointer and the version request.  Either
  162.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  163.  *    Exec has Forbid() for us during the call.
  164.  */
  165.  
  166. LibCall struct Library *
  167. LibOpen(__D0 long version, __A0 struct Library *lib)
  168. {
  169.     ++lib->lib_OpenCnt;
  170.  
  171.     lib->lib_Flags &= ~LIBF_DELEXP;
  172.  
  173.     return(lib);
  174. }
  175.  
  176. /*
  177.  *    Close is given the library pointer and the version request.  Be sure
  178.  *    not to decrement the open count if already zero.  If the open count
  179.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  180.  *    and return the seglist.  Otherwise we return NULL.
  181.  *
  182.  *    Note that this routine never sets LIBF_DELEXP on its own.
  183.  *
  184.  *    Exec has Forbid() for us during the call.
  185.  */
  186.  
  187. LibCall long
  188. LibClose(__A0 struct Library *lib)
  189. {
  190.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  191.         return(NULL);
  192.  
  193.     if (lib->lib_Flags & LIBF_DELEXP)
  194.         return(LibExpunge(lib));
  195.  
  196.     return(NULL);
  197. }
  198.  
  199. /*
  200.  *    We expunge the library and return the Seglist ONLY if the open count
  201.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  202.  *    flag and return NULL.
  203.  *
  204.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  205.  *    might be called from the memory allocator and thus we CANNOT DO A
  206.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  207.  *
  208.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  209.  *    therefore freeze if we called it ourselves.  As far as I can tell
  210.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  211.  *    below.
  212.  */
  213.  
  214. LibCall long
  215. LibExpunge(__A0 struct Library *lib)
  216. {
  217.     if (lib->lib_OpenCnt) {
  218.  
  219.         lib->lib_Flags |= LIBF_DELEXP;
  220.         return(NULL);
  221.     }
  222.  
  223.     ExitC();
  224.  
  225.     Remove(&lib->lib_Node);
  226.  
  227.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  228.  
  229.     if (IntuitionBase) {
  230.  
  231.         CloseLibrary(IntuitionBase);
  232.         IntuitionBase = NULL;
  233.     }
  234.  
  235.     if (DOSBase) {
  236.  
  237.         CloseLibrary(DOSBase);
  238.         DOSBase = NULL;
  239.     }
  240.  
  241.     return((long)SegList);
  242. }
  243.  
  244. #if 0
  245. LibCall long
  246. LibExpunge(__A0 struct Library *lib)
  247. {
  248.     if (lib->lib_OpenCnt) {
  249.  
  250.         lib->lib_Flags |= LIBF_DELEXP;
  251.         return(NULL);
  252.     }
  253.  
  254.     Remove(&lib->lib_Node);
  255.  
  256.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  257.  
  258.     if (DOSBase) {
  259.  
  260.         CloseLibrary((struct Library *)DOSBase);
  261.         DOSBase = NULL;
  262.     }
  263.  
  264.     if (IntuitionBase) {
  265.  
  266.         CloseLibrary((struct Library *)IntuitionBase);
  267.         IntuitionBase = NULL;
  268.     }
  269.  
  270.     return((long)SegList);
  271. }
  272. #endif
  273.