home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / utilities / monitors / snoopdos / src / tiny.a < prev   
Text File  |  1992-12-15  |  6KB  |  239 lines

  1. *vi:ts=8
  2. ****************************************************************************
  3. *                                       *
  4. *    TINY.A                  (C) Copyright Eddy Carroll 1992  *
  5. *    ~~~~~~                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
  6. *                                       *
  7. *    Replacement startup code for Lattice C V5.10a. Use instead of c.o. *
  8. *    This has many features stripped out to allow small utilities to       *
  9. *    have as small a filesize as possible. If you use this, don't call  *
  10. *    any stdio functions.                           *
  11. *                                       *
  12. ****************************************************************************
  13.  
  14.     INCLUDE "exec/types.i"
  15.     INCLUDE "exec/alerts.i"
  16.     INCLUDE "exec/nodes.i"
  17.     INCLUDE "exec/lists.i"
  18.     INCLUDE "exec/ports.i"
  19.     INCLUDE "exec/libraries.i"
  20.     INCLUDE "exec/tasks.i"
  21.     INCLUDE "libraries/dos.i"
  22.     INCLUDE "libraries/dosextens.i"
  23.     INCLUDE "workbench/startup.i"
  24.     INCLUDE "exec/funcdef.i"
  25.     INCLUDE "exec/exec_lib.i"
  26.     INCLUDE "libraries/dos_lib.i"
  27.  
  28. MAXARGS        EQU 100    ; Maximum number of command line arguments from CLI
  29. AbsExecBase EQU 4    ; Welcome to the only fixed point in the universe
  30.  
  31. * A useful macro to let us call library routines
  32. callsys macro
  33.     CALLLIB _LVO\1
  34.     endm
  35.  
  36.     xdef    XCEXIT
  37.     xdef    exit
  38.     xref    LinkerDB
  39.     xref    _BSSBAS
  40.     xref    _BSSLEN
  41.  
  42.     csect    text,0,0,1,2        * xref's after this are 16-bit reloc
  43.     xref    main            * Name of C program to start with.
  44.  
  45. start:
  46.     movem.l d1-d6/a0-a6,-(a7)
  47. REGSIZE EQU    (6+7)*4
  48.     lea    REGSIZE(a7),A5        * Determine old stack pointer
  49.     move.l    a0,a2            * Save command pointer
  50.     move.l    d0,d2            * and command length
  51.     lea    LinkerDB,a4        * Load base register
  52.  
  53.     move.l    AbsExecBase.W,a6
  54.     move.l    a6,SysBase(A4)
  55.     move.l    a7,_StackPtr(A4)    * Save stack ptr
  56.  
  57.     suba.l    a1,a1
  58.     callsys    FindTask        * Find out our task ID
  59.     move.l    d0,a3
  60.  
  61.     move.l    a5,D0              * get top of stack
  62.     sub.l    4(a5),D0        * compute bottom
  63.     add.l    #128,D0         * allow for parms overflow
  64.     move.l    D0,_base(A4)        * save for stack checking
  65.  
  66.     lea    DOSName(A4),A1
  67.     moveq.l    #0,D0
  68.     callsys    OpenLibrary
  69.     move.l    D0,DOSBase(A4)
  70.     bne    getcom
  71. noDOS:
  72.     moveq.l #100,d0
  73.     bra    exitToDOS
  74.  
  75. *------ find command name:
  76. getcom:
  77.     move.l  pr_CLI(a3),a0
  78.     add.l    a0,a0
  79.     add.l    a0,a0
  80.     move.l    cli_CommandName(a0),a1
  81.     add.l    a1,a1
  82.     add.l    a1,a1
  83.  
  84. *------ collect parameters:
  85.     move.l    d2,d0            * get command line length
  86.     moveq.l #0,d1
  87.     move.b    (a1)+,d1
  88.     move.l    a1,_ProgramName(A4)
  89.     add.l    d1,d0            * add length of command name
  90.     addq.l    #1,d0            * allow for space after command
  91.  
  92.     clr.w    -(A7)            * set null terminator for command line
  93.     addq.l    #1,D0            * force to even number of bytes
  94.     andi.w    #$fffe,D0        * (round up)
  95.     sub.l    D0,A7            * make room on stack for command line
  96.     subq.l    #2,D0
  97.     clr.w    0(A7,D0)
  98.  
  99. *------ copy command line onto stack
  100.     move.l    d2,d0            * get command line length
  101.     subq.l    #1,d0
  102.     add.l    d1,d2
  103.  
  104. copy_line:
  105.     move.b    0(A2,D0.W),0(A7,D2.W)    * copy command line to stack
  106.     subq.l    #1,d2
  107.     dbf    d0,copy_line
  108.     move.l    d2,d0            * save offset to argv[1] - 1
  109.     clr.b    0(a7,d2.w)         * Insert command name terminator
  110.     subq.l    #1,d2
  111.  
  112. copy_cmd:
  113.     move.b    0(a1,d2.w),0(a7,d2.w)    * copy command name to stack
  114.     dbf    d2,copy_cmd
  115.     lea    1(a7,d0.w),a1        * get pointer to start of arguments
  116.     move.l    a7,a2            * get pointer for argv[0]
  117.  
  118.     sub.l    #(MAXARGS*4),a7        * Reserve space for argv[]
  119.     move.l    a7,a3            * Save ptr to base of argv (&argv[0])
  120.     move.l    a2,(a7)            * Setup argv[0] to point to cmd line
  121.     lea    4(a7),a2        * Init base into array at argv[1]
  122.     moveq    #1,d2            * Initialise argc
  123.  
  124. *
  125. * From here on down, A1 is pointer into command line
  126. *
  127. build_argv:
  128.     bsr.s    getnext            * Read next character from line
  129.     bcs.s    doquote            * If quote, handle
  130.     beq.s    build_argv        * If white space, skip over it
  131.  
  132.     lea    -1(a1),a0        * Get address of this parameter
  133.     bsr.s    bumpargv        * Store it to argv[] array
  134. build_2:
  135.     bsr.s    getnext            * Get next character
  136.     bne.s    build_2            * If not white space, keep looking
  137.     clr.b    -1(a1)            * Zero-terminate current argument
  138.     bra.s    build_argv        * And go back to get next argument
  139.  
  140. doquote:
  141.     move.l    a1,a0            * Get pointer to this argument
  142.     bsr.s    bumpargv        * Output it to argv[]
  143. quote_2:
  144.     bsr.s    getnext            * Get next character
  145.     bcc.s    quote_2            * If not quote, keep looking
  146.     clr.b    -1(a1)            * Zero-terminate current argument
  147. quote_3:
  148.     bsr.s    getnext            * Get next character
  149.     bne.s    quote_3            * Skip until space reached
  150.     beq.s    build_argv        * Go back and read next argument
  151.  
  152. bumpargv:
  153.     move.l    a0,(a2)+        * Output ptr to current argument
  154.     addq    #1,d2            * Increment argc
  155.     cmpi    #MAXARGS,d2        * Used up all our arguments yet?
  156.     bls.s    qrts            * If not, then return
  157.     moveq    #110,d0            * Else set return code
  158.     bra.s    exit2            * And exit
  159.  
  160. *
  161. * Reads next character from command line. If zero, never returns, but
  162. * drops into call to main. Else, returns, with C=1 if character is quote,
  163. * Z=1 if character is white space.
  164. *
  165. getnext:
  166.     move.b    (a1)+,d0        * Get character from command line
  167.     beq.s    get_2            * Exit if end of line
  168.     cmp.b    #34,d0            * Check if quote
  169.     beq.s    isquote            *
  170.     cmp.b    #32,d0            * Check if space
  171.     beq.s    isspace            *
  172.     cmp.b    #9,d0            * Or tab
  173.     beq.s    isspace            *
  174.     cmp.b    #10,d0            * Or end of line
  175. isspace:
  176.     andi    #$1E,ccr        * Clear carry flag, retaining Z
  177. qrts    rts
  178.  
  179. isquote:
  180.     ori    #1,ccr            * Set carry flag
  181.     andi    #$FB,ccr        * Clear zero flag
  182.     rts                * And return
  183.  
  184. get_2:
  185.     move.l    a3,-(a7)        * Push argv onto stack
  186.     move.l    d2,-(a7)        * Push argc onto stack
  187.  
  188.     lea    _BSSBAS,a3        * get base of BSS
  189.     moveq    #0,d1
  190.     move.l    #_BSSLEN,d0        * get length of BSS in longwords
  191.     bra.s    clr_lp            * and clear for length given
  192. clr_bss move.l    d1,(a3)+
  193. clr_lp    dbf    d0,clr_bss
  194.  
  195. domain:
  196.     jsr    main(PC)        * Call main(argc,argv)
  197.     moveq.l #0,d0            * Set successful status
  198.     bra.s    exit2
  199.  
  200. exit:
  201. _exit:
  202. XCEXIT:
  203.     move.l    4(SP),d0        * Extract return code
  204. exit2:
  205.     move.l    d0,-(a7)
  206.     move.l    AbsExecBase.W,a6
  207.     move.l    DOSBase(A4),a1
  208.     callsys CloseLibrary        * Close Dos library
  209.  
  210. *------ this rts sends us back to DOS:
  211.     MOVE.L    (A7)+,D0
  212. exitToDOS:
  213.     movea.l _StackPtr(a4),SP    * Restore stack ptr
  214.     movem.l (a7)+,d1-d6/a0-a6
  215.     rts
  216.  
  217. *-----------------------------------------------------------------------
  218. * Global definitions
  219. *
  220.     csect    __MERGED,1,,2,2
  221.  
  222.     xdef    NULL,SysBase,LoadAddress,DOSBase
  223.     xdef    _oserr,_OSERR,_ONBREAK
  224.     xdef    _ProgramName,_StackPtr,_base
  225.  
  226. NULL           dc.l    0
  227. _base           dc.l    0
  228. _oserr           equ     *
  229. _OSERR           dc.l    0
  230. _ONBREAK       dc.l    0
  231. SysBase        dc.l    0
  232. LoadAddress    dc.l    0
  233. _StackPtr      dc.l    0
  234. DOSBase        dc.l    0
  235. _ProgramName   dc.l    0
  236. DOSName        dc.b    'dos.library',0
  237.  
  238.     END
  239.