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

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