home *** CD-ROM | disk | FTP | other *** search
/ Dream 41 / Amiga_Dream_41.iso / Amiga / Programmation / assembleur / proasm192.lha / ProAsm / routines / startup4.r < prev    next >
Encoding:
Text File  |  1996-06-25  |  18.2 KB  |  966 lines

  1.  
  2. ;---;  startup.r  ;------------------------------------------------------------
  3. *
  4. *    ****    ENHANCED CLI AND WORKBENCH STARTUP WITH DETACH    ****
  5. *
  6. *    Author        Stefan Walter
  7. *    Version        1.17
  8. *    Last Revision    24.06.96
  9. *    Identifier    cws_defined
  10. *       Prefix        cws_    (cli and workbench startup)
  11. *                 »       »         »
  12. *    Functions    AutoDetach, ReplySync, ReplyWBMsg
  13. *
  14. *    Flags        cws_DETACH set 1      : when detaching startup
  15. *            cws_V36PLUSONLY set 1 : when tool for KS2.0 only
  16. *            cws_V37PLUSONLY set 1 : when tool for KS2.0/V37 only
  17. *            cws_CLIONLY set 1     : when tool for CLI usage only
  18. *            cws_PRI equ [pri]     :    if process must have priority
  19. *                            [pri] otherwise the priority
  20. *                            is inherited.
  21. *            cws_FPU set 1         : if FPU reset needed
  22. *            cws_EASYLIB set 1     : if libs to be opened by startup
  23. *            cws_STACKSIZE equ [stacksize] : if process must have a
  24. *                            stacksize of [stacksize]
  25. *                            otherwise the stacksize of the
  26. *                            parent process is taken.
  27. *
  28. *     NOTE:    The startup code must be in the first code segment, along with
  29. *        the other routines it accesses.
  30. *
  31. ;------------------------------------------------------------------------------
  32. *
  33. * Using this include: The main code must contain the symbols 'processname',
  34. * 'clistartup' and 'wbstartup'. The Main program must execute 'JMP AutoDetach'.
  35. * The child process can call ReplySync with a routine in a0. This routine gets
  36. * executed by the parent process under permitted conditions. The child process
  37. * *MUST* call ReplySync once with a0:=0. It waits then for a final message from
  38. * the parent process, which is sent by this under forbid. This guarantees
  39. * that the parent process ends in any case before the child because the memory
  40. * belongs to the child from the moment it recieves the first messy. The parent
  41. * process terminates under forbid.
  42. *
  43. ;------------------------------------------------------------------------------
  44.  
  45. ;------------------
  46.     ifnd    cws_defined
  47. cws_defined    =1
  48.  
  49. ;------------------
  50. cws_oldbase    equ __base
  51.     base    cws_base
  52. cws_base:
  53.  
  54. ;------------------
  55.  
  56. ;------------------------------------------------------------------------------
  57. *
  58. * AutoDetach    Standard startup code.
  59. *
  60. * RESULT:    If the program was started on workbench, this routine gets the
  61. *        workbench message in d0 and jumps to 'WBSTARTUP'. In case of
  62. *        CLI start, this routine autodetaches from the process and the
  63. *        new process starts at 'CLISTARTUP'. A message is sent to the
  64. *        new process to synchronize its creation with the end of the old
  65. *        process. In the message, a routine can be designed to be
  66. *        executed. The label 'PROCESSNAME' points to name.
  67. *
  68. *        The following variables can be accessed by the customer:
  69. *
  70. *        - cws_kick20.b        -1 if V36+, else 0
  71. *        - cws_kick30.b        -1 if V39+, else 0
  72. *        - cws_wbstartup.b    -1 for WB, 0 for CLI
  73. *        - cws_wbmessage.l    Pointer to WB message
  74. *        - cws_launchtask    Pointer to parent task structure
  75. *        - cws_returnvalue    Return value for parent process
  76. *
  77. ;------------------------------------------------------------------------------
  78.  
  79.         IFD     cws_V37PLUSONLY
  80. cws_V36PLUSONLY    SET    1
  81.     ENDIF
  82.  
  83. ;------------------
  84. AutoDetach:
  85.  
  86. ;------------------
  87. ; Get main base and save startup regs for capturing CLI.
  88. ;
  89. \start:
  90.     pea    (a4)
  91.     lea    cws_base(pc),a4
  92.     movem.l    d0-a6,cws_regs(a4)
  93.     move.l    (sp)+,cws_regs+12*4(a4)
  94.     move.l    4.w,a6
  95.  
  96.     IFD    cws_FPU
  97. ;------------------
  98. ; Reset FPU.
  99. ;
  100. \resetfpu:
  101.     MC68882
  102.     move.w    296(a6),d0
  103.     moveq    #$30,d1
  104.     btst    #3,d0        ;68040?
  105.     beq.s    \noforty
  106.     moveq    #$70,d1
  107. \noforty:
  108.     and.w    d1,d0
  109.     beq.s    \findtask
  110.     fmove.l    #0,fpcr
  111.     opt    p=-68882
  112.     ENDIF
  113.  
  114. ;------------------
  115. ; Get task and test if run from WorkBench.
  116. ;
  117. \findtask:
  118.     IFD    V37PLUSONLY
  119.     cmp.w    #$25,20(a6)
  120.     ELSE
  121.     cmp.w    #$24,20(a6)
  122.     ENDIF
  123.     sge    cws_kick20(a4)        ;remember exec version
  124.     cmp.w    #39,20(a6)
  125.     sge    cws_kick30(a4)
  126.     move.l    $114(a6),a0
  127.     move.l    a0,cws_launchtask(a4)    ;remember for child
  128.     move.l    $ac(a0),d6
  129.     seq    cws_wbstartup(a4)    ;remember in flag
  130.     bne    \climode
  131.  
  132. ;------------------
  133. ; Started from WB, get message.
  134. ;
  135. \getwbmsg:
  136.     lea    $5c(a0),a0
  137.     jsr    -384(a6)        ;WaitPort()
  138.     jsr    -372(a6)        ;GetMsg()
  139.     move.l    d0,cws_wbmessage(a4)
  140.  
  141. ;------------------
  142.  
  143.     IFD    cws_V36PLUSONLY
  144. cws_REQUESTER    set    1
  145.  
  146. ;------------------
  147. ; Test if V36+
  148. ;
  149. \testwb20:
  150.     IFND    cws_CLIONLY
  151.     tst.b    cws_kick20(a4)
  152.     bne    \gowb            ;okay!
  153.     bsr.s    \ks20requester
  154.     bra    ReplyWBMsg
  155.     ELSE
  156.     bra    \clirequester
  157.     ENDIF
  158.  
  159. ;------------------
  160. ; Pop up 'KS V36+ only' requester.
  161. ;
  162. \ks20requester:
  163.     lea    \maintext(pc),a1
  164.     lea    \maintext2(pc),a0
  165.     move.l    a0,\pt2-\maintext(a1)
  166.     lea    \text2(pc),a0
  167.     move.l    a0,\pt3-\maintext(a1)
  168.     bra    \requester
  169.  
  170. ;------------------
  171. ; Texts.
  172. ;
  173. \maintext2:
  174.     dc.b    0,0,0,0
  175.     dc.w    10,18
  176.     dc.l    0
  177. \pt3:    dc.l    0    ;text2
  178.     dc.l    0
  179.  
  180.     IFD    V37PLUSONLY
  181. \text2:        dc.b    "needs Kickstart V37+.",0
  182.     ELSE
  183. \text2:        dc.b    "needs Kickstart V36+.",0
  184.     ENDIF
  185.  
  186.     even
  187.  
  188. ;------------------
  189.     ENDIF
  190.  
  191. ;------------------
  192.     ifd    cws_CLIONLY
  193. cws_REQUESTER    set    1
  194.  
  195. ;------------------
  196. ; Pop up 'CLI only' requester.
  197. ;
  198. \clirequester:
  199.     lea    \maintext(pc),a1
  200.     lea    \maintext21(pc),a0
  201.     move.l    a0,\pt2-\maintext(a1)
  202.     lea    \text21(pc),a0
  203.     move.l    a0,\pt5-\maintext(a1)
  204.     bsr    \requester
  205.     bra    ReplyWBMsg
  206.  
  207. ;------------------
  208. ; Texts.
  209. ;
  210. \maintext21:
  211.     dc.b    0,0,0,0
  212.     dc.w    10,18
  213.     dc.l    0
  214. \pt5:    dc.l    0    ;text2
  215.     dc.l    0
  216.  
  217. \text21:    dc.b    "is for CLI usage only.",0
  218.     even
  219.  
  220. ;------------------
  221.     else
  222.  
  223. ;------------------
  224. ; Go to start.
  225. ;    d0=WBMSG
  226. ;
  227. \gowb:    move.l    d0,d7            ;store WBMessage
  228.     IFD    cws_EASYLIB
  229.     bsr    cws_openlibraries    ;all registers unaffected
  230.     beq    ReplyWBMsg
  231.     ENDIF
  232.  
  233.     bsr    OpenDosLib        ;changes D0!!!
  234.     beq.s    \nocd
  235.  
  236.     move.l    d7,a0
  237.     move.l    36(a0),d0        ;sm_ArgList
  238.     beq.s    \cddone
  239.     move.l    d0,a0
  240.     move.l    (a0),d1            ;wa_Lock
  241.     beq.s    \cddone
  242.     jsr    -126(a6)        ;CurrentDir()
  243.  
  244. \cddone:
  245.     bsr    CloseDosLib
  246.  
  247. \nocd:    move.l    cws_wbmessage(pc),d0
  248.  
  249.     IFD    cws_V36PLUSONLY
  250.     move.l    cws_launchtask(a4),a3
  251.     move.l    $bc(a3),cws_homedir(a4)    ;HomeDir...
  252.     ENDIF
  253.  
  254.     move.l    (sp)+,cws_returnaddr(a4)
  255.     jsr    wbstartup(pc)
  256.     move.l    cws_returnaddr(pc),-(sp)
  257.  
  258.     IFD    cws_EASYLIB
  259.     bsr    CloseLibrary
  260.     ENDIF
  261.  
  262.     bra    ReplyWBMsg
  263.  
  264. ;------------------
  265.     endif
  266.  
  267. ;------------------
  268.     ifd    cws_REQUESTER
  269.  
  270. ;------------------
  271. ; Open a requester.
  272. ;    a1=\maintext
  273. ;
  274. \requester:
  275.     lea    \text1(pc),a0
  276.     move.l    a0,\pt1-\maintext(a1)
  277.     lea    \text3(pc),a0
  278.     move.l    a0,\pt4-\maintext(a1)
  279.  
  280.     move.l    4.w,a6
  281.     lea    \intuiname(pc),a1
  282.     jsr    -408(a6)        ;OldOpenLibrary()
  283.     move.l    d0,a6
  284.     tst.l    d0
  285.     beq.s    \requesterend        ;no library, no requester...
  286.  
  287.     suba.l    a0,a0
  288.     moveq    #0,d0
  289.     moveq    #0,d1
  290.     moveq    #30,d2
  291.     lsl.l    #3,d2
  292.     moveq    #64,d3
  293.     lea    \maintext(pc),a1
  294.     suba.l    a2,a2
  295.     lea    \negativetext(pc),a3
  296.     jsr    -348(a6)        ;AutoRequester()
  297.  
  298. \closeintui:
  299.     move.l    a6,a1
  300.     move.l    4.w,a6
  301.     jsr    -414(a6)        ;CloseLibrary()
  302.  
  303. \requesterend:
  304.     rts
  305.  
  306. ;------------------
  307. ; IntuiText structures.
  308. ;
  309. \maintext:
  310.     dc.b    0,0,0,0
  311.     dc.w    10,8
  312.     dc.l    0
  313. \pt1:    dc.l    0    ;text1
  314. \pt2:    dc.l    0    ;maintext2
  315.  
  316. \negativetext:
  317.     dc.b    0,0,0,0
  318.     dc.w    4,3
  319.     dc.l    0
  320. \pt4:    dc.l    0    ;text3
  321.     dc.l    0
  322.  
  323. ;------------------
  324. ; Texts.
  325. ;
  326. \text1:        dc.b    "The program '",gea_progname,"'",0
  327. \text3:        dc.b    " Okay ",0
  328. \intuiname:    dc.b    "intuition.library",0
  329.     even
  330.  
  331. ;------------------
  332.     endif
  333.  
  334. ;------------------
  335.  
  336. ;------------------------------------------------------------------------
  337. ;-    Task Detach                            -
  338. ;------------------------------------------------------------------------
  339.  
  340. ;------------------
  341. ; To the CLI.
  342. ;
  343. \climode:
  344.  
  345. ;------------------
  346.     ifd    cws_V36PLUSONLY
  347.  
  348. ;------------------
  349. ; Test if V36+
  350. ;
  351. \testcli20:
  352.     tst.b    cws_kick20(a4)
  353.     beq    \ks20requester
  354.  
  355. ;------------------
  356.     endif
  357.  
  358. ;------------------
  359.     ifd    cws_DETACH
  360.  
  361. ;------------------
  362. ; Okay, run from CLI or shell: Open dos.library.
  363. ;
  364. \opendos:
  365.     bsr    OpenDosLib
  366.     bne.s    \getcd
  367.     move.w    #fail_nodos,d0
  368.     jsr    Alert(a4)        ;*** failure
  369.     bne.s    \opendos
  370.     bra    \exit
  371.  
  372. ;------------------
  373. ; Get current directory.
  374. ;
  375. \getcd:
  376.     move.l    d0,a6
  377.     moveq    #0,d1
  378.     jsr    -126(a6)        ;CurrentDir()
  379.     move.l    d0,cws_lock(a4)        ;remember for duplock.
  380.     exg.l    d0,d1
  381.     jsr    -126(a6)        ;CurrentDir()
  382.  
  383.     IFD    cws_V36PLUSONLY
  384.     move.l    cws_launchtask(a4),a3
  385.     move.l    $bc(a3),cws_homedir(a4)    ;HomeDir...
  386.     ENDIF
  387.  
  388. ;------------------
  389. ; Count number of segments.
  390. ;
  391. \countsegments:
  392.     lsl.l    #2,d6
  393.  
  394. \againc:moveq    #0,d1
  395.     move.l    d6,a3
  396.     move.l    $3c(a3),d0        ;first segment
  397.     bra.s    \entry
  398.  
  399. \loop:
  400.     addq.l    #1,d1
  401.     move.l    (a0),d0
  402.     beq.s    \gotnumber
  403. \entry:
  404.     lsl.l    #2,d0
  405.     move.l    d0,a0
  406.     bra.s    \loop
  407.  
  408. \gotnumber:
  409.     move.w    d1,cws_segnumber(a4)
  410.  
  411. ;------------------
  412. ; Get enough memory for mementry for all segments.
  413. ;
  414. \getmementrymem:
  415.     addq.l    #2,d1
  416.     lsl.l    #3,d1
  417.     moveq    #1,d0
  418.     exg.l    d0,d1
  419.     move.l    4.w,a6
  420.     jsr    -198(a6)        ;AllocMem()
  421.     move.l    d0,cws_mementry(a4)
  422.     bne.s    \launchproc
  423.     move.w    #fail_nomementry,d0
  424.     jsr    Alert(a4)        ;*** failure
  425.     bne.s    \againc
  426.     bra    \closedos
  427.  
  428. ;------------------
  429. ; Create new process under forbid.
  430. ;
  431. \launchproc:
  432.     jsr    -132(a6)        ;Forbid()
  433.     move.l    cws_launchtask(a4),a2
  434.     lea    $3c(a3),a0
  435.     move.l    (a0),cws_myseg(a4)
  436.     clr.l    (a0)            ;remove all other segments
  437. \againp:pea    \gocli-4(pc)
  438.     move.l    (sp)+,d3        ;routine
  439.     lsr.l    #2,d3
  440.     pea    processname(a4)
  441.     move.l    (sp)+,d1        ;name
  442.     moveq    #0,d2
  443.  
  444.     ifnd    cws_PRI
  445.     move.b    9(a2),d2        ;copy pri of parent
  446.     else
  447.     moveq    #cws_PRI,d2
  448.     endif
  449.  
  450.     ifnd    cws_STACKSIZE
  451.     move.l    $34(a3),d4        ;copy stack longwords
  452.     lsl.l    #2,d4            ;bytes stack
  453.     else
  454.     move.l    #cws_STACKSIZE,d4    ;custom stack size
  455.     endc
  456.     bsr    GetDosBase
  457.     jsr    -138(a6)        ;CreateProc()
  458.  
  459. ;------------------
  460. ; If error occured link mementry in parent process.
  461. ;
  462. \testprocess:
  463.     move.l    d0,a2            ;process ID
  464.     move.l    d0,d3            ;remember this
  465.     bne.s    \insertmem
  466.     move.w    #fail_noprocess,d0
  467.     jsr    Alert(a4)        ;*** failure
  468.     bne.s    \againp
  469.     move.l    cws_launchtask(a4),a2
  470.     lea    $5c(a2),a2        ;link in mementry in own task
  471.  
  472. ;------------------
  473. ; Insert mementry structure.
  474. ;
  475. \insertmem:
  476.     lea    -18(a2),a2        ;start of tc_mementry
  477.     move.l    (a2),a1            ;remember old head
  478.     move.l    cws_mementry(a4),a0
  479.     move.l    a0,(a2)            ;new head in list
  480.     move.l    a0,4(a1)        ;new pred in second node
  481.     move.l    a1,(a0)+        ;next in our node
  482.     move.l    a2,(a0)+        ;pred in our node
  483.     move.w    #$a00,(a0)+        ;type: memory  pri: 0
  484.     clr.l    (a0)+            ;noname
  485.     move.w    cws_segnumber(a4),d7
  486.     move.w    d7,(a0)+        ;#of entries
  487.  
  488. ;------------------
  489. ; Fill it out.
  490. ;
  491. \fillout:
  492.     subq.w    #1,d7
  493.     move.l    cws_myseg(pc),a1
  494.  
  495. \fillloop:
  496.     add.l    a1,a1
  497.     add.l    a1,a1
  498.     subq.l    #4,a1
  499.     move.l    a1,(a0)+        ;address
  500.     move.l    (a1)+,(a0)+        ;length
  501.     move.l    (a1)+,a1
  502.     dbra    d7,\fillloop
  503.  
  504. ;-------------------
  505. ; Now, all went fine(d3<>0)?.
  506. ;
  507. \didwe:
  508.     tst.l    d3
  509.     beq    \noforbid
  510.  
  511. ;------------------
  512. ; Send message to child. Send first message to process port, others
  513. ; to custom port.
  514. ;
  515. \sendmsg:
  516.     st.b    cws_sync(a4)        ;we begin...
  517.     move.l    d3,cws_portptr(a4)
  518.     
  519. \sendmsgloop:
  520.     move.l    cws_launchtask(pc),a2
  521.     lea    $5c(a2),a2
  522.     move.l    cws_portptr(pc),a0
  523.     lea    cws_messy(pc),a1
  524.     move.l    a2,14(a1)        ;replyport...
  525.     move.l    4.w,a6
  526.     jsr    -366(a6)        ;PutMsg()
  527.  
  528. ;------------------
  529. ; Wait for reply.
  530. ;
  531. \wait:
  532.     move.l    a2,a0
  533.     jsr    -384(a6)        ;WaitPort()
  534.     move.l    a2,a0
  535.     jsr    -372(a6)        ;GetMsg()
  536.     move.l    d0,a0
  537.     lea    cws_messy(pc),a1
  538.     move.l    20(a0),d0
  539.     beq.s    \sendend
  540.  
  541. ;------------------
  542. ; Permit, call routine, forbid and wait for nilroutine messy.
  543. ;
  544. \call:
  545.     pea    \rts(pc)
  546.     move.l    d0,-(sp)
  547.     jmp    -138(a6)        ;Permit() and then routine
  548. \rts:
  549.     jsr    -132(a6)
  550.     bra    \sendmsgloop
  551.  
  552. ;------------------
  553. ; Send final message to child.
  554. ;
  555. \sendend:
  556.     move.l    cws_portptr(pc),a0
  557.     lea    cws_messy(pc),a1
  558.     jsr    -366(a6)        ;PutMsg()
  559.  
  560. ;------------------
  561. ; Prepare to Permit().
  562. ;
  563. \noforbid:
  564.     pea    -138(a6)        ;Permit()
  565.  
  566. ;------------------
  567. ; Close dos.library.
  568. ;
  569. \closedos:
  570.     bsr    CloseDosLib
  571.  
  572. ;------------------
  573. ; Leave this code.
  574. ;
  575. \exit:
  576.     movem.l    cws_regs(a4),d0-a5    ;a6=[4] for permit()!
  577.     move.l    cws_returnvalue(pc),d0    ;returnvalue
  578.     rts
  579.  
  580. ;--------------------------------------------------------------------
  581. ; Patch return address, handle locks and go to cli (foreign task territory).
  582. ;
  583. \makebcpl:
  584.     align.l                ;because of BCPL
  585.     dc.l    0,0            ;some monitors crash when this is not here
  586.  
  587. \gocli:    lea    cws_base(pc),a4
  588.     lea    cws_returnaddr(pc),a0
  589.     move.l    (sp)+,(a0)        ;remember real return
  590.     move.l    4.w,a6
  591.     move.l    $114(a6),a0
  592.     move.l    $80(a0),d0
  593.     beq.s    \openport
  594.     lsl.l    #2,d0
  595.     move.l    d0,a0
  596.     move.l    cws_myseg(pc),$c(a0)        ;copy segment list.
  597.  
  598. ;------------------
  599. ; Open new port and fail if not.
  600. ;
  601. \openport:
  602.     lea    cws_port(pc),a0
  603.     bsr    MakePort
  604.     bne.s    \gotport
  605.     move.w    #fail_nostartp,d0
  606.     jsr    Alert(a4)        ;*** failure
  607.     bne.s    \openport
  608.  
  609. \bad:    suba.l    a0,a0
  610.     bsr    ReplySync
  611.     bra    \fexit
  612.  
  613. \gotport:
  614.     lea    cws_dummy(pc),a0
  615.     bsr    ReplySync
  616.  
  617. ;------------------
  618. ; Change directory to the new lock.
  619. ;
  620. \changedir:
  621.     bsr    OpenDosLib        ;the child will free it when ending
  622.     move.l    d0,a6            ;it exists, parent has it still open
  623.     move.l    cws_lock(pc),d1
  624.     beq.s    \lockduped
  625.     jsr    -96(a6)            ;DupLock()
  626.     move.l    d0,d1
  627.     move.l    d0,cws_lock(a4)
  628.     move.l    d0,cws_currentdir(a4)
  629.  
  630. \lockduped:
  631.     jsr    -126(a6)        ;CurrentDir() DOES NOT USE MSGS!
  632.  
  633.     IFD    cws_V36PLUSONLY
  634.     move.l    cws_homedir(pc),d1
  635.     beq.s    \nghd
  636.     jsr    -96(a6)            ;DupLock()
  637.     move.l    d0,cws_homedir(a4)
  638.     move.l    4.w,a6
  639.     move.l    $114(a6),a4
  640.     move.l    d0,$bc(a4)
  641. \nghd:    ENDIF
  642.  
  643.     IFD    cws_EASYLIB
  644.     bsr    cws_openlibraries
  645.     bne.s    \go
  646.     suba.l    a0,a0
  647.     bsr    ReplySync
  648.     bra.s    \unlock
  649.  
  650.     ENDIF
  651.  
  652. \go:    movem.l    cws_regs(pc),d0-a6
  653.     jsr    clistartup(pc)        ;GO!
  654.  
  655.     IFD    cws_EASYLIB
  656.     bsr    CloseLibrary
  657.     ENDIF
  658.  
  659. ;------------------
  660. ; Unlock DupLock()ed struct
  661. ;
  662. \unlock:
  663.     movem.l    d0-a6,-(sp)
  664.  
  665.     IFD    cws_V36PLUSONLY
  666.     move.l    cws_homedir(pc),d1
  667.     beq.s    \nhd
  668.     bsr    GetDosBase
  669.     jsr    -90(a6)            ;Unlock()
  670.     move.l    4.w,a6
  671.     move.l    $114(a6),a4
  672.     clr.l    $bc(a4)
  673.     
  674. \nhd:    ENDIF
  675.  
  676.     move.l    cws_lock(pc),d1
  677.     beq.s    \nlock
  678.     bsr    GetDosBase
  679.     jsr    -90(a6)            ;Unlock()
  680.     moveq    #0,d1
  681.     jsr    -126(a6)        ;ChangeDir()
  682.  
  683. \nlock:    bsr    CloseDosLib
  684.  
  685.     movem.l    (sp)+,d0-a6
  686.  
  687. \fexit:    move.l    cws_returnaddr(pc),-(sp)
  688.     rts
  689.  
  690. ;------------------
  691.  
  692. ;------------------------------------------------------------------------------
  693. *
  694. * ReplySync    Reply to startup message and set routine.
  695. *
  696. * INPUT:    a0    Address of routine to be called
  697. *
  698. * RESULT:    Waits for startup message and replies to it, setting
  699. *        the routine field to d0. Then it waits again for a
  700. *        message. This routine can be called without concideration
  701. *        of WB or CLI start or if ReplySync already terminated.
  702. *
  703. ;------------------------------------------------------------------------------
  704.  
  705. ;------------------
  706. ReplySync:
  707.  
  708. ;------------------
  709. ; Get task address.
  710. ;
  711. \start:
  712.     movem.l    d0-a6,-(sp)
  713.     lea    cws_wbstartup(pc),a3
  714.     tst.b    (a3)            ;from WB?
  715.     bne.s    \exit
  716.     move.b    cws_sync(pc),d0        ;done yet?
  717.     beq.s    \exit
  718.     move.l    a0,a3
  719.     move.l    4.w,a6
  720.     move.l    cws_portptr(pc),a2
  721.  
  722. ;------------------
  723. ; Wait for messy and test if it is 'messy'.
  724. ;
  725. \wait:
  726.     move.l    a2,a0
  727.     jsr    -384(a6)        ;WaitPort()
  728.     move.l    a2,a0
  729.     jsr    -372(a6)        ;GetMsg()
  730.     tst.l    d0
  731.     beq.s    \wait
  732.     move.l    d0,a1
  733.     lea    cws_messy(pc),a0
  734.     cmp.l    a0,a1
  735.     bne.s    \wait
  736.     pea    cws_dummy(pc)
  737.     cmp.l    (sp)+,a3
  738.     bne.s    \nodummy
  739.     lea    cws_portptr(pc),a0
  740.     pea    cws_port(pc)
  741.     move.l    (sp)+,(a0)        ;set new port to use.
  742.  
  743. \nodummy:
  744.     move.l    a3,20(a1)
  745.     jsr    -378(a6)        ;ReplyMsg()
  746.  
  747. ;------------------
  748. ; Now wait again, either for next messy or for final messy.
  749. ; If final messy, get it so DOS can then use port for IO.
  750. ;
  751. \again
  752.     move.l    cws_portptr(pc),a2
  753.     move.l    a2,a0
  754.     jsr    -384(a6)        ;WaitPort()
  755.     move.l    a3,d0
  756.     bne.s    \exit
  757.     move.l    a2,a0
  758.     jsr    -372(a6)        ;GetMsg()
  759.     lea    cws_port(pc),a0
  760.     bsr    UnMakePort
  761.     lea    cws_sync(pc),a0
  762.     clr.b    (a0)
  763.  
  764. ;------------------
  765. ; Exit.
  766. ;
  767. \exit:
  768.     movem.l    (sp)+,d0-a6
  769.     rts
  770.  
  771. ;------------------
  772.  
  773. ;--------------------------------------------------------------------
  774.  
  775. ;------------------
  776. ; Data only used by detaching startup.
  777. ;
  778.  
  779. ;------------------
  780.     include    alert.r
  781.     include    ports.r
  782.  
  783. ;------------------
  784.     AddAlert_    fail_nodos,"Unable to open dos.library"
  785.     AddAlert_    fail_nomementry,"No memory for mementry struct"
  786.     AddAlert_    fail_noprocess,"Unable to create process"
  787.     AddAlert_    fail_nostartp,"Unable to create port"
  788.  
  789. ;------------------
  790. ; Startup message.
  791. ;
  792. cws_messy:    dc.l    0,0
  793.         dc.b    05,0
  794.         dc.l    0
  795.         dc.l    0    ;replyport
  796.         dc.w    24    ;length
  797. cws_routine:    dc.l    0    ;routine to be called
  798.  
  799. cws_dummy:    rts        ;dummy routine for port switch
  800.  
  801. ;------------------
  802. ; Second port.
  803. ;
  804. cws_port:
  805.     ds.b    10,0
  806.     dc.l    0        ;no name
  807.     dc.b    0,0        ;flag,sigbit
  808.     dc.l    0        ;sigtask
  809.     ds.b    14,0        ;MSG list
  810.  
  811. ;------------------
  812. cws_portptr:    dc.l    0
  813. cws_myseg:    dc.l    0
  814. cws_mementry:    dc.l    0
  815. cws_lock:    dc.l    0
  816. cws_currentdir:    dc.l    0
  817. cws_segnumber:    dc.w    0
  818. cws_sync:    dc.b    0
  819.         dc.b    0
  820. cws_returnvalue:dc.l    0
  821.  
  822. ;------------------
  823.  
  824. ;--------------------------------------------------------------------
  825.  
  826. ;------------------
  827.     else    
  828.  
  829. ;------------------
  830. ; Simply go to start.
  831. ;
  832. \gocli:
  833.     IFD    cws_EASYLIB
  834.     moveq    #0,d0
  835.     bsr    cws_openlibraries
  836.     beq.s    \nogo
  837.     ENDIF
  838.  
  839.     IFD    cws_V36PLUSONLY
  840.     move.l    cws_launchtask(a4),a3
  841.     move.l    $bc(a3),cws_homedir(a4)    ;HomeDir...
  842.     ENDIF
  843.  
  844.     move.l    (sp)+,cws_returnaddr(a4)
  845.     movem.l    cws_regs(a4),d0-a6
  846.     jsr    clistartup(pc)
  847.     move.l    cws_returnaddr(pc),-(sp)
  848.  
  849.     IFD    cws_EASYLIB
  850.     bsr    CloseLibrary
  851.     ENDIF
  852.  
  853. \nogo:    rts
  854.  
  855. ;------------------
  856.     endif
  857.  
  858. ;------------------
  859.  
  860. ;------------------------------------------------------------------------------
  861. *
  862. * ReplyWBMsg    Reply to workbench message and go forbid.
  863. *
  864. * RESULT:    Replies to cws_wbmessage and calls Forbid() because the WB
  865. *        imediately frees the process memory. This routine can be
  866. *        called without concideration of WB or CLI start.
  867. *
  868. ;------------------------------------------------------------------------------
  869.  
  870. ;------------------
  871. ReplyWBMsg:
  872.  
  873. ;------------------
  874. ; Start.
  875. ;
  876. \start:
  877.     movem.l    d0-a6,-(sp)
  878.     lea    cws_wbstartup(pc),a0
  879.     tst.b    (a0)            ;from CLI?
  880.     beq.s    \exit
  881.  
  882.     lea    cws_wbmessage(pc),a1
  883.     move.l    (a1),d0
  884.     beq.s    \exit
  885.     clr.l    (a1)
  886.     move.l    d0,a1
  887.  
  888.     move.l    4.w,a6
  889.     jsr    -132(a6)        ;ReplyMsg frees segments!
  890.     jsr    -378(a6)        ;ReplyMsg()
  891.  
  892. ;------------------
  893. ; End.
  894. ;
  895. \exit:
  896.     movem.l    (sp)+,d0-a6
  897.     rts
  898.  
  899. ;------------------
  900. ; Data always used.
  901. ;
  902. cws_wbstartup:    dc.b    0    ;-1 for WB, 0 for CLI
  903. cws_kick20:    dc.b    0    ;-1 for 2.0+, 0 for 1.2/1.3
  904. cws_kick30:    dc.b    0    ;-1 for 3.0+, 0 for lower
  905.         dc.b    0
  906. cws_launchtask:    dc.l    0
  907. cws_wbmessage:    dc.l    0
  908. cws_returnaddr:    dc.l    0
  909. cws_regs:    ds.l    15,0
  910.     IFD    cws_V36PLUSONLY
  911. cws_homedir:    dc.l    0
  912.     ENDIF
  913.  
  914. ;------------------
  915.     include    doslib.r
  916.  
  917. ;------------------
  918.  
  919. ;--------------------------------------------------------------------
  920.  
  921.     IFD    cws_EASYLIB
  922. ;------------------
  923.  
  924. ely_NOALERT    SET    1
  925.     include    easylibrary.r
  926.     include    alert.r
  927.  
  928. cws_alerttext:
  929.     AddAlert_    fail_nolibs,"Could not open                            "
  930.  
  931. ;------------------
  932. ; Open libs wanted and display alert if one fails!
  933. ;
  934. ;    ccr=>EQ if failed
  935. ;
  936. cws_openlibraries:
  937.     movem.l    d0-a6,-(sp)
  938.  
  939. \again:    bsr    OpenLibrary
  940.     tst.l    d0
  941.     bne.s    \done
  942.     bsr    CloseLibrary
  943.     lea    cws_alerttext+15(pc),a1
  944. \loop:    move.b    (a0)+,(a1)+
  945.     bne.s    \loop
  946.     move.w    #fail_nolibs,d0
  947.     bsr    Alert
  948.     bne.s    \again
  949.  
  950. \done:    movem.l    (sp)+,d0-a6
  951.     rts
  952.  
  953. ;------------------
  954.     ENDIF
  955.  
  956. ;--------------------------------------------------------------------
  957.  
  958. ;------------------
  959.     base    cws_oldbase
  960.  
  961. ;------------------
  962.     endif
  963.  
  964.     end
  965.  
  966.