home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 364b.lha / PCQ_v1.1 / Runtime / PCQ / PCQStart.asm < prev    next >
Encoding:
Assembly Source File  |  1990-04-08  |  7.0 KB  |  301 lines

  1.  
  2.     SECTION    PCQStart,CODE
  3.  
  4. *    PCQStart.asm (of PCQ Pascal runtime library)
  5. *    Copyright (c) 1989 Patrick Quaid
  6.  
  7. *    This is the startup and shutdown code for the programs.
  8. *    Note that a few changes have got to take place here before PCQ
  9. *    programs can be run from the WorkBench.
  10.  
  11.     XREF    _AbsExecBase
  12.     XREF    _LVOOpenLibrary
  13.     XREF    _LVOCloseLibrary
  14.     XREF    _LVOFindTask
  15.     XREF    _LVOWaitPort
  16.     XREF    _LVOGetMsg
  17.     XREF    _LVOReplyMsg
  18.     XREF    _LVOForbid
  19.  
  20.     XREF    _LVOInput
  21.     XREF    _LVOOutput
  22.     XREF    _LVOIsInteractive
  23.  
  24.     XREF    _LVOFreeRemember
  25.  
  26.     XREF    newkey
  27.     XREF    filekey
  28.     XREF    _StdInName
  29.     XREF    _StdOutName
  30.  
  31.     XREF    _p%FillBuffer
  32.     XREF    _p%FlushBuffer
  33.     XREF    _p%Close
  34.     XREF    _p%Open
  35.  
  36.     XDEF    _CommandLine
  37.     XDEF    _p%DOSBase
  38.     XDEF    _p%IntuitionBase
  39.     XDEF    _p%MathBase
  40.     XDEF    _p%WorkBenchMsg
  41.     XDEF    _p%IOResult
  42.     XDEF    _Input
  43.     XDEF    _Output
  44.     XDEF    _ExitProc
  45.     XDEF    _ExitCode
  46.     XDEF    _ExitAddr
  47.     
  48.     INCLUDE    ":runtime/FileRec.i"
  49.  
  50. ;    Define entry point
  51.  
  52.     xdef    _p%initialize
  53. _p%initialize
  54.  
  55. ;    Save stack pointer for exit() routine
  56.  
  57.     move.l    sp,StkPtr    ; save stack pointer
  58.     add.l    #4,StkPtr    ; account for this jsr to get to original
  59.  
  60. ;    Save the command line pointer to CommandLine
  61.  
  62.     move.l    a0,_CommandLine
  63.     move.b    #0,0(a0,d0.w)    ; null terminate it
  64.  
  65. ;    Check for WB or CLI
  66.  
  67.     move.l    _AbsExecBase,a6
  68.     suba.l    a1,a1
  69.     jsr    _LVOFindTask(a6)
  70.     move.l    d0,a4
  71.  
  72. ;    are we running from a CLI?
  73.  
  74.     tst.l    172(a4)            ; 172 = pr_CLI
  75.     bne    fromCLI
  76.  
  77.     lea    92(a4),a0        ; 92 = pr_MsgPort
  78.     jsr    _LVOWaitPort(a6)
  79.     lea    92(a4),a0
  80.     jsr    _LVOGetMsg(a6)
  81.     move.l    d0,_p%WorkBenchMsg    ; save the WB message
  82.  
  83.     bra    openLibs        ; do the rest of the startup
  84.  
  85.  
  86. fromCLI:
  87.  
  88. ;    clear the Workbench message
  89.  
  90.     move.l    #0,_p%WorkBenchMsg
  91.  
  92. ;    Open libraries
  93.  
  94. openLibs:
  95.     lea    intuitionname,a1
  96.     moveq    #0,d0
  97.     move.l    _AbsExecBase,a6
  98.     jsr    _LVOOpenLibrary(a6)
  99.     move.l    d0,_p%IntuitionBase
  100.     beq    _p%exit
  101.  
  102.     lea    dosname,a1
  103.     moveq    #0,d0
  104.     jsr    _LVOOpenLibrary(a6)
  105.     move.l    d0,_p%DOSBase
  106.     beq    _p%exit
  107.  
  108.     lea    mathname,a1
  109.     clr    d0
  110.     jsr    _LVOOpenLibrary(a6)
  111.     move.l    d0,_p%MathBase
  112.     beq    _p%exit
  113.  
  114. ;    Find standard file handles
  115.  
  116.     tst.l    _p%WorkBenchMsg        ; run from the Workbench?
  117.     beq    OpenFiles        ; if not, open standard stuff
  118.  
  119.     move.l    _StdInName,d0        ; get input name
  120.     beq.s    OpenStdOut        ; if Nil, skip this
  121.     move.l    d0,-(sp)        ; save the name
  122.     lea    _Input,a0        ; get address of file record
  123.     move.l    #80,MAX(a0)        ; set buffer size = 80
  124.     move.l    a0,-(sp)        ; push the address
  125.     jsr    _p%Open            ; open the file
  126.     addq.l    #8,sp            ; fix the stack
  127.     tst.b    d0            ; did it go OK?
  128.     bne.s    1$            ; if so, skip the following
  129.     move.l    #53,d0            ; set runtime error
  130.     jsr    _p%exit            ; quit the program
  131. 1$    lea    _Input,a0        ; retrieve the file record
  132.     tst.b    INTERACTIVE(a0)        ; is it interactive
  133.     beq.s    OpenStdOut        ; Open a new file
  134.     move.l    _StdInName,d0        ; get input name
  135.     cmp.l    _StdOutName,d0        ; are the names equal?
  136.     bne.s    OpenStdOut        ; if not, skip this
  137.     move.l    HANDLE(a0),d0        ; get the file handle
  138.     lea    _Output,a0        ; get the output file
  139.     move.l    d0,HANDLE(a0)        ; use the same handle for output
  140.     move.b    #-1,INTERACTIVE(a0)    ; set interactive to true
  141.     rts                ; and get back to main
  142. OpenStdOut
  143.     move.l    _StdOutName,d0        ; get output name
  144.     beq    1$            ; if nil, leave
  145.     move.l    d0,-(sp)        ; push the output file name
  146.     move.l    #_Output,-(sp)        ; push the file record
  147.     jsr    _p%Open            ; open the file
  148.     addq.l    #8,sp            ; fix the stack
  149.     tst.b    d0            ; did it work?
  150.     bne.s    1$            ; if so, skip the following
  151.     move.l    #57,d0            ; set runtime error
  152.     jsr    _p%exit            ; and leave
  153. 1$    rts                ; go back to main program
  154. OpenFiles
  155.     move.l    _p%DOSBase,a6
  156.     jsr    _LVOInput(a6)        ; get standard in
  157.     move.l    #_Input,a0        ; get input file record
  158.     move.l    d0,HANDLE(a0)        ; set handle
  159.     beq    _p%exit            ; if zero, quit
  160.     move.l    d0,d1            ; set up for next call
  161.     jsr    _LVOIsInteractive(a6)    ; is it interactive?
  162.     move.l    #_Input,a0        ; get file record again
  163.     move.b    d0,INTERACTIVE(a0)    ; set flag
  164.     beq.s    StdInNotInteractive    ; skip this if not interactive
  165.     move.l    BUFFER(a0),a1        ; get buffer address
  166.     adda.l    #1,a1            ; make end one byte further on
  167.     move.l    a1,MAX(a0)        ; set buffer size
  168.     move.l    a1,CURRENT(a0)        ; will need a read
  169.     bra    OpenStdOutput
  170. StdInNotInteractive
  171.     jsr    _p%FillBuffer        ; fill the buffer
  172. OpenStdOutput
  173.     jsr    _LVOOutput(a6)        ; get ouput file handle
  174.     move.l    #_Output,a0        ; get file record
  175.     move.l    d0,HANDLE(a0)        ; set value
  176.     beq    _p%exit            ; if zero, quit
  177.     move.l    d0,d1            ; set up for call
  178.     jsr    _LVOIsInteractive(a6)    ; is it interactive?
  179.     move.l    #_Output,a0        ; get file record
  180.     move.b    d0,INTERACTIVE(a0)    ; set flag
  181.  
  182. 1$    rts
  183.  
  184. *    Close all the open files, free all new() memory
  185.  
  186. _p%CloseAndFree
  187.  
  188.     move.l    #_Output,a0    ; write any pending output
  189.     jsr    _p%FlushBuffer
  190.  
  191. 1$    move.l    filekey,d0    ; get the current file key
  192.     beq.s    2$        ; if it's empty, skip ahead
  193.     move.l    d0,a0        ; otherwise make the call
  194.     jsr    _p%Close    ; to close the file
  195.     bra.s    1$        ; and loop 'til file list is empty
  196.  
  197. 2$    tst.l    newkey        ; return all allocated memory
  198.     beq.s    3$
  199.     lea    newkey,a0    ; set up for FreeRemember call
  200.     moveq.l    #-1,d0        ; really forget
  201.     move.l    _p%IntuitionBase,a6    ; get library base
  202.     jsr    _LVOFreeRemember(a6)    ; free it all
  203.  
  204. 3$    rts                ; and return to Exit
  205.  
  206.     XDEF    _p%ExitWithAddr
  207. _p%ExitWithAddr
  208.     move.l    (sp),_ExitAddr
  209.  
  210. *    Falls through to the following...
  211.  
  212.     XDEF    _p%exit
  213. _p%exit
  214.     move.l    d0,_ExitCode        ; save the exit code
  215. 1$    move.l    _ExitProc,a0        ; get the first exit proc
  216.     move.l    a0,d0            ; set the z flag
  217.     beq.s    2$            ; if empty, skip around
  218.     move.l    #0,_ExitProc        ; set it to null
  219.     jsr    (a0)            ; call the exit proc
  220.     bra.s    1$            ; loop for next ExitProc
  221. 2$
  222.     move.l    _AbsExecBase,a6        ; get Exec base
  223.     move.l    _p%IntuitionBase,a1    ; get Intuition library
  224.     move.l    a1,d0            ; to set flags
  225.     beq.s    4$            ; if it wasn't open, don't close
  226.     jsr    _LVOCloseLibrary(a6)    ; close Intuition
  227. 4$
  228.     move.l    _p%DOSBase,a1        ; get DOS library base
  229.     move.l    a1,d0            ; set flags
  230.     beq.s    5$            ; if it wasn't open, skip
  231.     jsr    _LVOCloseLibrary(a6)    ; close DOS
  232. 5$
  233.     move.l    _p%MathBase,a1        ; get Math base
  234.     move.l    a1,d0            ; was it open?
  235.     beq.s    6$            ; if not, skip
  236.     jsr    _LVOCloseLibrary(a6)    ; close it
  237. 6$
  238.     tst.l    _p%WorkBenchMsg        ; were we run from Workbench
  239.     beq    7$            ; No.  Skip this
  240.  
  241.     jsr    _LVOForbid(a6)        ; so we won't be unloaded too soon
  242.     move.l    _p%WorkBenchMsg,a1    ; get our message
  243.     jsr    _LVOReplyMsg(a6)    ; return the WB message
  244. 7$
  245.     move.l    _ExitCode,d0        ; get the DOS return code
  246.     move.l    StkPtr,sp        ; get the original stack pos
  247.     rts                ; lay down and die...
  248.  
  249.     XDEF    _IOResult
  250. _IOResult
  251.     move.l    _p%IOResult,d0
  252.     move.l    #0,_p%IOResult
  253.     rts
  254.  
  255.     SECTION    TWO,DATA
  256.  
  257. dosname        dc.b    'dos.library',0
  258. intuitionname    dc.b    'intuition.library',0
  259. mathname    dc.b    'mathffp.library',0
  260.     CNOP    0,2
  261.  
  262. _p%DOSBase    dc.l    0
  263. _p%IntuitionBase dc.l    0
  264. _p%MathBase    dc.l    0
  265. _p%WorkBenchMsg    dc.l    0
  266. _p%IOResult    dc.l    0
  267.  
  268. _Input        dc.l    0    ; Handle
  269.         dc.l    0    ; Next
  270.         dc.l    InBuff    ; Buffer
  271.         dc.l    InBuff    ; Current
  272.         dc.l    InBuff    ; Last
  273.         dc.l    InBuff+80    ; Max
  274.         dc.l    1    ; RecSize
  275.         dc.b    0    ; Interactive
  276.         dc.b    0    ; EOF
  277.         dc.w    1005    ; ModeOldFile
  278.  
  279. _Output        dc.l    0    ; Handle
  280.         dc.l    0    ; Next
  281.         dc.l    OutBuff    ; Buffer
  282.         dc.l    OutBuff    ; Current
  283.         dc.l    OutBuff    ; Last
  284.         dc.l    OutBuff+80    ; Max
  285.         dc.l    1    ; RecSize
  286.         dc.b    0    ; Interactive
  287.         dc.b    0    ; EOF
  288.         dc.w    1006    ; ModeNewFile
  289.  
  290. _CommandLine    dc.l    0
  291. _ExitProc    dc.l    _p%CloseAndFree
  292. _ExitCode    dc.l    0
  293. _ExitAddr    dc.l    0
  294. _
  295. StkPtr        dc.l    0
  296.  
  297.     SECTION    Buffers,BSS
  298. InBuff        ds.b    80
  299. OutBuff        ds.b    80
  300.     END
  301.