home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d101 / startups.lha / StartUps / WBStartup.asm < prev    next >
Assembly Source File  |  1987-09-05  |  6KB  |  227 lines

  1.  
  2. *    WBStartup.obj
  3. *
  4. *    For Workbench programs, or CLI programs that require no command
  5. *    line parameters.
  6. *    Modified from Astartup.asm by Bryce Nesbitt.  Release 1.0
  7. *
  8. ******* Included Files *************************************************
  9.  
  10.     NOLIST
  11.     INCLUDE "exec/types.i"
  12.     IFND EXEC_ABLES_I
  13.     INCLUDE "exec/ables.i"
  14.     ENDC
  15.     IFND EXEC_EXECBASE_I
  16.     INCLUDE "exec/execbase.i"
  17.     ENDC
  18.     IFND LIBRARIES_DOS_I
  19.     INCLUDE "libraries/dos.i"
  20.     ENDC
  21.     IFND LIBRARIES_DOESEXTENS_I
  22.     INCLUDE "libraries/dosextens.i"
  23.     ENDC
  24.     IFND WORKBENCH_STARTUP_I
  25.     INCLUDE "workbench/startup.i"
  26.     ENDC
  27.     LIST
  28.  
  29. ******* Imported *******************************************************
  30.  
  31.     xref    _Input
  32.     xref    _Output
  33.     xref    _main            ; C code entry point
  34.  
  35. callsys macro
  36.     xref    _LVO\1
  37.     jsr    _LVO\1(a6)
  38.     endm
  39.  
  40. blink    macro
  41.     bchg.b    #1,$bfe001
  42.     endm
  43.  
  44. ******* Exported *******************************************************
  45.  
  46.     xdef    _SysBase
  47.     xdef    _DOSBase
  48.  
  49.     xdef    _errno
  50.     xdef    _stdin
  51.     xdef    _stdout
  52.     xdef    _stderr
  53.  
  54.     xdef    _exit            ; standard C exit function
  55.  
  56. ************************************************************************
  57. *
  58. *    Standard Program Entry Point
  59. *
  60. *    main (argc, argv)
  61. *       int    argc;        ;Passed as 32 bits. 0=from Workbench
  62. *       char *argv[];    ;Passed as 32 bits. If from workbench,
  63. *                ;then this is the WB message pointer.
  64. *
  65. ;
  66. ;a3 - frame pointer
  67. ;a4 - ThisTask
  68. ;a5-  DOSBase
  69. ;
  70. startup:    lea.l    ss,a3        ; set up "frame pointer"
  71.         clr.l    (a3)+        ; _errno
  72.         move.l    sp,(a3)+    ; initailSP
  73.  
  74.     ;------ get Exec's library base pointer:
  75.         move.l    4,a6        ; get _AbsExecBase
  76.         move.l    a6,(a3)+    ; _SysBase
  77.  
  78.     ;------ Open the DOS library:
  79.  
  80.         lea.l    DOSName(pc),A1
  81.         callsys OldOpenLibrary    ; Look ma, no error check!
  82.         move.l    d0,(a3)+    ; _DOSBase
  83.         move.l    d0,a5
  84.  
  85.     ;------ get the address of our task
  86.         move.l    ThisTask(a6),a4
  87.  
  88.     ;------ are we running as a son of Workbench?
  89.         tst.l    pr_CLI(A4)
  90.         beq    fromWorkbench
  91.         clr.l    (a3)+        ; returnMsg
  92.  
  93. ;=======================================================================
  94. ;====== CLI Startup Code ===============================================
  95. ;=======================================================================
  96.  
  97.     ;------ find command name:
  98. fromCLI     move.l    pr_CLI(a4),a0
  99.         add.l    a0,a0        ; bcpl pointer conversion
  100.         add.l    a0,a0
  101.         move.l    cli_CommandName(a0),a0
  102.         add.l    a0,a0        ; bcpl pointer conversion
  103.         add.l    a0,a0
  104.  
  105.     ;------ create buffer and array:
  106.         ;[a0=BCPL command name]
  107.         lea.l    argvBuffer-_stdin(a3),a1
  108.         lea.l    argvArray-_stdin(a3),a2
  109.         move.l    a1,(a2)     ; ptr to command name
  110.  
  111.     ;------ fetch command name:
  112.         moveq    #0,d0
  113.         move.b    (a0)+,d0    ; size of command name
  114.  
  115.         cmp.w    #31,d0        ; restrict size
  116.         bhi.s    enter
  117.         move.w    #30,d0        ; 30 + 1 loops
  118.  
  119. lpback        move.b    (a0)+,(a1)+
  120. enter        dbf    d0,lpback
  121.  
  122.         clr.b    (a1)+        ; null terminate name
  123.         move.l    a2,-(sp)    ; pointer to argvArrary
  124.         moveq    #1,d0        ; argc always = 1.
  125.         move.l    d0,-(sp)    ; this version only passes the name
  126.  
  127.     ;------ get standard input handle:
  128.         move.l    a5,a6        ; Get DOSBase
  129.         callsys Input
  130.         move.l    d0,(a3)+    ; _stdin
  131.  
  132.     ;------ get standard output handle:
  133.         callsys Output
  134.         move.l    d0,(a3)+    ; _stdout
  135.         move.l    d0,(a3)+    ; _stderr
  136.  
  137.     ;------ call C main entry point
  138.         bra    domain
  139.  
  140. ;=======================================================================
  141. ;====== Workbench Startup Code =========================================
  142. ;=======================================================================
  143.  
  144.     ;------ we are now set up.  wait for a message from our starter
  145. fromWorkbench:    ;[a4=ThisTask]
  146.         ;[a5=DOSBase]
  147.         lea.l    pr_MsgPort(A4),a0    ; our process base
  148.         callsys WaitPort
  149.         lea.l    pr_MsgPort(A4),a0    ; our process base
  150.         callsys GetMsg
  151.  
  152.     ;------ save the message so we can return it later
  153.         move.l    d0,(a3)+    ; set returnMsg. NOTE: no ReplyMsg yet!
  154.  
  155.     ;------ push argc and argv.  if argc = 0 program came from
  156.     ;------ Workbench, and argv will have the WB message.
  157.         move.l    d0,-(SP)    ; set argv to the WB message
  158.         clr.l    -(SP)        ; set argc to 0
  159.  
  160.     ;------ get the first argument
  161.         ;[d0=WBMessage]
  162.         move.l    a5,a6        ;Get DOSBase
  163.         move.l    d0,a2
  164.         move.l    sm_ArgList(a2),d0
  165.         beq.s    docons
  166.  
  167.     ;------ and set the current directory to the same directory
  168.         move.l    d0,a0
  169.         move.l    wa_Lock(a0),d1
  170.         callsys CurrentDir
  171.  
  172. docons: ;------ ignore the toolwindow argument
  173. domain:     jsr    _main
  174.         moveq.l #0,d0        ; Successful return code
  175.         bra.s    exit2
  176.  
  177. ************************************************************************
  178. *
  179. *    C Program exit(returncode) Function
  180. *
  181. *  Warning: no resources are deallocated.  If you took it, you must
  182. *  have already given it back!!
  183. *
  184. *
  185. _exit:        move.l    4(SP),d0     ; extract return code
  186. exit2:        lea.l    ss,a3         ; set "frame pointer" back up
  187.         move.l    initialSP-ss(a3),SP ; restore stack pointer
  188.         move.l    d0,-(SP)     ; save return code
  189.  
  190.     ;------ close DOS library:
  191.         move.l    4,a6
  192.         move.l    _DOSBase-ss(a3),a1
  193.         callsys CloseLibrary
  194.  
  195.     ;------ if we ran from CLI, skip workbench cleanup:
  196.         move.l    returnMsg-ss(a3),d0
  197.         beq.s    exitToDOS
  198.  
  199.     ;------ return the startup message to our parent
  200.     ;------ we forbid so workbench can't UnLoadSeg() us
  201.     ;------ before we are done:
  202.         FORBID
  203.         ;[d0=returnMsg]
  204.         move.l    d0,a1
  205.         callsys ReplyMsg
  206.  
  207.     ;------ this rts sends us back to DOS:
  208. exitToDOS:    move.l    (SP)+,d0
  209.         rts
  210. DOSName     dc.b 'dos.library',0
  211.         cnop 0,2
  212.  
  213. ******* BSS ***********************************************************
  214.         BSS
  215. ss            ; placemarker
  216. _errno        ds.l 1    ; error number from OS routines
  217. initialSP    ds.l 1    ; initial stack pointer
  218. _SysBase    ds.l 1    ; exec library base pointer
  219. _DOSBase    ds.l 1    ; dos library base pointer
  220. returnMsg    ds.l 1    ; Workbench message, or zero for CLI startup
  221. _stdin        ds.l 1
  222. _stdout     ds.l 1
  223. _stderr     ds.l 1
  224. argvArray    ds.l 1    ; pointer to arg array
  225. argvBuffer    ds.b 32 ; Max 32 character command name
  226.         END
  227.