home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 534.lha / sregexp.library_v11.1 / clibrary.asm < prev    next >
Assembly Source File  |  1991-08-08  |  12KB  |  371 lines

  1.  
  2. ;**************************************************************************
  3. ;*                                      *
  4. ;* sregexp.library -- pattern matching run time library.          *
  5. ;*                                      *
  6. ;*                                      *
  7. ;* Startup module: this is heavily copied from the sample.library      *
  8. ;* in the RKM includes and autodocs.                      *
  9. ;* This also relies on some features of dice (in particular, registered   *
  10. ;* args and features of the linker.)  Be carefull if you make it with      *
  11. ;* something else.                              *
  12. ;*                                      *
  13. ;* Created: April 20, 1991                          *
  14. ;*                                      *
  15. ;**************************************************************************
  16.  
  17.         CODE        sregexp.start
  18.  
  19.         INCLUDE     "sregexpbase.i"
  20.         INCLUDE     "exec/initializers.i"
  21.         INCLUDE     "libraries/dos.i"
  22.         INCLUDE     "exec/resident.i"
  23.         INCLUDE     "exec/lists.i"
  24.         INCLUDE     "exec/alerts.i"
  25.         INCLUDE     "sreg_version.i"
  26.  
  27. ;--------------------------------------------------------------------
  28. ; some generally usefull macros hiding _LVO's
  29. ;--------------------------------------------------------------------
  30. CALLSYS     macro
  31.         jsr        _LVO\1(a6)
  32.         endm
  33.  
  34. XLIB        macro
  35.         xref        _LVO\1
  36.         endm
  37.  
  38. ;--------------------------------------------------------------------
  39. ; Declare the library calls I use. The _LVO's are brought in
  40. ; from amiga.lib
  41. ;--------------------------------------------------------------------
  42.         XLIB        OpenLibrary
  43.         XLIB        CloseLibrary
  44.         XLIB        Alert
  45.         XLIB        FreeMem
  46.         XLIB        Remove
  47.  
  48. ;------ defined in Amiga.lib ------
  49.         xref        _AbsExecBase
  50.  
  51. ;------ defined in the c source, the id string of my library. ------
  52.         xref        _idString
  53.  
  54.  
  55.  
  56.  
  57. ;--------------------------------------------------------------------
  58. ; Make some stuff visible for debuging.
  59. ;--------------------------------------------------------------------
  60.         xdef        Init
  61.         xdef        EndCode
  62.         xdef        RomTag
  63.         xdef        funcTable
  64.         xdef        dataTable
  65.         xdef        InitRoutine
  66.  
  67.         xdef        Open
  68.         xdef        Close
  69.         xdef        Expunge
  70.         xdef        Reserved
  71.  
  72. ;--------------------------------------------------------------------
  73. ; The links to the c code are in the file kludge.asm, because the
  74. ; assembler I use doesn't like to make symbols starting with a @.
  75. ; But thats what DICE starts it's regargs routines with, so...
  76. ; I make the links with das, which comes with dice.
  77. ;
  78. ; my library routines
  79. ;--------------------------------------------------------------------
  80.         xref        parsesregexp
  81.         xref        freesregexp
  82.         xref        matchsregexp
  83.         xref        matchnsregexp
  84.         xref        iswild
  85.         xref        anchorpath
  86.         xref        nextfile
  87.         xref        buildpath
  88.         xref        freespathinfo
  89.  
  90.  
  91.  
  92.  
  93.  
  94. ************************************************************************
  95. * Ok, so much for the preamble, we can actually start to generate      *
  96. * some code now.  The very first thing we should do is return an       *
  97. * error if someone tries to run us like a program, seeing as we        *
  98. * will be a perfectly legal AmigaDos load module.               *
  99. ************************************************************************
  100.  
  101. Start:
  102.         moveq.l     #-1,d0
  103.         rts
  104.  
  105.  
  106. ;--------------------------------------------------------------------
  107. ; Ok, here's what exec is going to look for to tell it that we are
  108. ; a library.
  109. ;--------------------------------------------------------------------
  110. RomTag:
  111.         dc.w        RTC_MATCHWORD    ; magic
  112.         dc.l        RomTag        ; pointer back to magic
  113.         dc.l        EndCode        ; keep looking from here
  114.         dc.b        RTF_AUTOINIT    ; yes, autoinit me.
  115.         dc.b        VERSION        ; version number
  116.         dc.b        NT_LIBRARY        ; I'm a library
  117.         dc.b        0            ; no priority (typical)
  118.         dc.l        sregexpname     ; I have a name
  119.         dc.l        _idString        ; let's see some ID
  120.         dc.l        Init        ; start me up
  121.  
  122.  
  123. ;--------------------------------------------------------------------
  124. ; Ok, heres the table of 4 vectors exec is going to look for
  125. ; to autoinit me.
  126. ;
  127. ; We are word alligned here.
  128. ;--------------------------------------------------------------------
  129. Init:
  130.         dc.l        SregExpBase_SIZE    ; size of library structure
  131.         dc.l        funcTable        ; table of all my routines.
  132.         dc.l        dataTable        ; LibStructure init data
  133.         dc.l        InitRoutine     ; Where to start me.
  134.  
  135.  
  136. ;--------------------------------------------------------------------
  137. ; This is the list of functions in my library.
  138. ;--------------------------------------------------------------------
  139. funcTable:
  140.         dc.l        Open
  141.         dc.l        Close
  142.         dc.l        Expunge
  143.         dc.l        Reserved
  144.  
  145. ; my own little gems.
  146.         dc.l        parsesregexp
  147.         dc.l        freesregexp
  148.         dc.l        matchsregexp
  149.         dc.l        matchnsregexp
  150.         dc.l        iswild
  151.         dc.l        anchorpath
  152.         dc.l        nextfile
  153.         dc.l        buildpath
  154.         dc.l        freespathinfo
  155.         dc.l        -1            ; end of the list.
  156.  
  157. ;--------------------------------------------------------------------
  158. ; heres the stuff to initialise my Library structure.
  159. ;--------------------------------------------------------------------
  160. dataTable:
  161.         INITBYTE    LN_TYPE,NT_LIBRARY
  162.         INITLONG    LN_NAME,sregexpname
  163.         INITBYTE    LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
  164.         INITWORD    LIB_VERSION,VERSION
  165.         INITWORD    LIB_REVISION,REVISION
  166.         INITLONG    LIB_IDSTRING,_idString
  167.         dc.l        0
  168.  
  169.  
  170.  
  171. *******************************************************************
  172. * Ok, so now were really ready to get cooking.              *
  173. * This routine is called when the library is first linked into      *
  174. * the exec LibList.  The Library structure has already been      *
  175. * initialised as per the dataTable instructions.          *
  176. *                                  *
  177. * When called, d0 has our library base and the segment list is      *
  178. * pointed to by a0; a6 is a pointer to SysBase.           *
  179. *******************************************************************
  180.  
  181. InitRoutine:
  182.         move.l        a4,-(sp)                ; put the lib pointer
  183.         move.l        d0,a4            ; in a convinient place
  184.  
  185.         move.l        a6,sb_SysBase(a4)       ; save SysBase
  186.         move.l        a0,sb_SegList(a4)       ; save our seglist
  187.  
  188.         lea.l        dosname(pc),a1          ; try and open up
  189.         moveq.l     #0,d0            ; dos.library
  190.         CALLSYS     OpenLibrary
  191.  
  192.         move.l        d0,sb_DOSBase(a4)       ;save dos
  193.         bne.s        1$
  194.  
  195.  
  196.         ALERT        AG_OpenLib|AO_DOSLib    ; couldn't get dos!!!
  197.  
  198. ;------- That's all there is to do. --------
  199. 1$
  200.         move.l        a4,d0
  201.         move.l        (sp)+,a4
  202.         rts
  203.  
  204.  
  205.  
  206. ***********************************************************************
  207. * So good so far.  Now come the 4 standard library routines that      *
  208. * every good library must have.  Each of these (in fact all of the    *
  209. * library calls) comes with the pointer to our library base in a6.    *
  210. ***********************************************************************
  211.  
  212. ;----------------------------------------------------------------------
  213. ; To open the library, all we have to do is up the count of current
  214. ; users and reset the delayed expunge flag. This returns the library
  215. ; pointer on d0 if we successfully opened (which is always the case).
  216. ;----------------------------------------------------------------------
  217. Open:
  218.         addq.w        #1,LIB_OPENCNT(a6)
  219.         bclr        #LIBB_DELEXP,LIB_FLAGS(a6)
  220.         move.l        a6,d0
  221.         rts
  222.  
  223.  
  224.  
  225.  
  226. ;----------------------------------------------------------------------
  227. ; When we close the library, we decrease the count of current
  228. ; users. If this has reached zeros, then we check the delayed
  229. ; expunge flag and clean up if it is set.
  230. ; If this routine returns 0 then that's that.  If the return value
  231. ; is non-zero, it should be the segment pointer passed to the
  232. ; initialization routine, so we can be unloaded.
  233. ;----------------------------------------------------------------------
  234. Close:
  235.         ;--- set up the return value, which will be altered ---
  236.         ;--- if we decide to do a delayed expunge.        ---
  237.         moveq.l     #0,d0
  238.         subq.w        #1,LIB_OPENCNT(a6)
  239.         bne.s        1$
  240.         btst        #LIBB_DELEXP,LIB_FLAGS(a6)
  241.         beq.s        1$
  242.  
  243. ; --- take a shortcut into the Expunge routine that doesn't ---
  244. ; --- redo the tests we just did.                ---
  245.         bsr.s        ReallyExpunge
  246.  
  247. 1$
  248. ; --- at this point, d0 is zero if we didn't expunge, otherwise ---
  249. ; --- it the address of our segment list.            ---
  250.         rts
  251.  
  252.  
  253.  
  254. ;----------------------------------------------------------------------
  255. ; Ok, so now we want to try to unload.    First check if there
  256. ; are still any current openers, if so, just set the delayed
  257. ; expunge flag, and we'll go away when everyone closes us.
  258. ; Otherwise, clean up my resources and return the seg list to
  259. ; be unloaded
  260. ;----------------------------------------------------------------------
  261. Expunge:
  262.         tst.w        LIB_OPENCNT(a6)
  263.         beq.s        ReallyExpunge
  264.  
  265.         bset        #LIBB_DELEXP,LIB_FLAGS(a6)
  266.         moveq.l     #0,d0            ;don't unload me please
  267.         rts
  268.  
  269. ReallyExpunge:
  270.         movem.l     d2/a5,-(sp)
  271.         move.l        a6,a5
  272.         move.l        sb_SysBase(a5),a6
  273.  
  274.         move.l        sb_SegList(a5),d2       ;save seglist.
  275.  
  276.         move.l        a5,a1            ;remove the library
  277.         REMOVE                    ;from the system list
  278.  
  279.         move.l        sb_DOSBase(a5),a1
  280.         CALLSYS     CloseLibrary        ;close dos.
  281.  
  282.         moveq.l     #0,d0
  283.         move.l        a5,a1
  284.         move.w        LIB_NEGSIZE(a5),d0
  285.         sub.l        d0,a1
  286.         add.w        LIB_POSSIZE(a5),d0
  287.  
  288.         CALLSYS     FreeMem            ;free our library base.
  289.  
  290.         move.l        d2,d0            ;unload us.
  291.  
  292.         movem.l     (sp)+,d2/a5
  293.         rts
  294.  
  295.  
  296. ;----------------------------------------------------------------------
  297. ; This is the reserved vector.    Just return 0.
  298. ;----------------------------------------------------------------------
  299. Reserved:
  300.         moveq.l     #0,d0
  301.         rts
  302.  
  303.  
  304.  
  305. **********************************************************************
  306. * That's all of the standard stuff.  As discussed above, the links   *
  307. * to the c code are in the file kludge.asm                 *
  308. **********************************************************************
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316. ;----------------------------------------------------------------------
  317. ; Here are some stings used in the above.  I put them at the end so
  318. ; so I don't have to worry about alignment.
  319. ;----------------------------------------------------------------------
  320. dosname     DOSNAME
  321.  
  322. sregexpname    SREGEXPNAME
  323.  
  324. ;----------------------------------------------------------------------
  325. ; The end of the code, where exec can start looking for more RomTag
  326. ; structures if it wants to.
  327. ;----------------------------------------------------------------------
  328.         EVEN
  329. EndCode:
  330.  
  331.  
  332.  
  333.  
  334.  
  335. **********************************************************************
  336. *                                     *
  337. * Now comes the clever part.  Dice (and the others, I think)         *
  338. * references there global data from register a4.  What I do         *
  339. * to have easy access to the Library structure data from the         *
  340. * c code, is transfer a6 to a4 on each library call and then         *
  341. * define the various things I want to access as offsets to         *
  342. * this, in effect, tricking dice into thinking they're global        *
  343. * variables.  The real benifit of this is I don't need my own        *
  344. * custom links to the Exec and DOS library routines, because         *
  345. * there exists perfectly valid global variables SysBase and         *
  346. * DosBase.                                 *
  347. *                                     *
  348. **********************************************************************
  349.  
  350.         DATA        data
  351.  
  352. ;---------------------------------------------------------------------
  353. ; This one is a little funny, it is the actual instance of the
  354. ; structure, and not a pointer to it.  It should be declared in
  355. ; the c code as 'extern struct SregExpBase SregExpBase;' and then
  356. ; referenced with the '.' and not the '->'.  This may not work
  357. ; I haven't tried it yet.
  358. ;----------------------------------------------------------------------
  359. _SregExpBase    equ        0
  360.         xdef        _SregExpBase
  361.  
  362. ;--- These are perfectly ordinary pointers ---
  363. _SysBase    equ        sb_SysBase
  364.         xdef        _SysBase
  365.  
  366. _DOSBase    equ        sb_DOSBase
  367.         xdef        _DOSBase
  368.  
  369.         end
  370.  
  371.