* Echo.Asm
*
*       Written 12/30/86 by C.Heath

*       This program may be freely distributed, so long as it is not
* modified in any way.  If an executable is distributed, use the
* version assembled with Assem, linked with blink 6.7, which should be
* 148 bytes long. ( You need to link with Amiga.lib )

*       This is the first in a series of ADOS program replacements.
* It can be assembled with the Metacomco assembler, Assem, or with
* minor edits with Manx's assembler V3.30E.

*       The goal of writing these command replacements is as follows:
*
*       1) So that people that need to develop alternatives to
* Execute() will only have to worry about CLI and WBench startup
* procedures, and not have to deal with BCPL startup.
*
*       2) So that it will be possible to consider an alternative
* DOS for the Amiga in the future.
*
*       3) So that more familar command line arguements can be used.


*       My hope is that the replacement commands will meet a couple standards:
*
*       1) On whole, the replacements should be no larger than the current
* BCPL programs.  The programs I'll write will be written in assembler,
* and should be smaller.  But C equivalents will be hard to write smaller
* than BCPL because BCPL can use a lot of DOS internal functions.  Perhaps
* to alleviate this, some shared routines such as wild-card expansion
* can be written in assembler and thoroughly debugged, and then used by
* other programs written in C.
*
*       2) The command syntax should be more familiar to people that
* have used MS-DOS and UNIX.  For instance, wild-card expansions should
* use "*" instead of "#?".  Some of the ADOS conventions may need to
* be used, such as allowing keywords like "To" and "All", but this
* should be considered on a command-by-command basis with the grande
* picture in mind.

*       If you are interested in assisting on this mission, or
* perhaps if you've already got some of the pieces done, I'd love to
* hear from you on BIX as cheath, or CIS 74216,2117, or at our
* PO box:   Microsmiths Inc, PO Box 561, Cambridge, MA 02140

******  Equates and publics     ***************************************

MIN_LIB set     $1f                     ; Library minimum rev level!

*** NOTE: If you use Manx's assembler V3.30E, substitute "public" for "xref"

xlib    macro
        xref    _LVO\1
        endm

        xlib    OpenLibrary
        xlib    CloseLibrary

        xlib    Output
        xlib    Write


******  The Program             ***************************************

* NOTE: If you are using Manx's assembler V3.30E, you should uncomment
* the following three lines:

*       entry   .begin
*       public  .begin
*.begin

Start:

*
* Process input string.  Search for first non-space.  If it turns out
* to be a """ character, then delimit the string with it. Otherwise
* just dump the whole line, unlike ADOS which reports an error.

* It is assumed that the input string is terminated with a LINEFEED,
* as has been the case with all released versions of AmigaDOS.

* Register usage:
*       D0,D1,A0,A1     used as scratch
*       ( D0, A0 is the command line length and p_string at startup )

*       D2,D3   DOS function arguements for Write()
*       D4      Number of characters to Write
*               This starts as the input count passed by CLI startup.

*       A2      Points at start of string to echo
*       A6      DOSBase if it is sucessfully opened.

        move.l  D0,D4

        addq    #1,D4
skpspc: subq    #1,D4
        move.b  (A0)+,D0
        cmp.b   #$20,D0
         beq.s  skpspc

        move.l  A0,A2                   ; Set START of input string

        cmp.b   #'"',D0                 ; Is char a double quote?
         beq.s  find_match              ; YES - find matching quote

        subq    #1,A2                   ; Set start back to include
        bra.s   ok_go                   ; the first char if not a """

* Look for matching """
find_match:
        moveq   #0,D4                   ; Start count from scratch
        move.b  #$0a,D1                 ; D0 is """, D1 is LF

ll1:    addq    #1,D4
        cmp.b   (A0),D1
         beq.s  ok_go                   ; GO FOR IT! Found LF
        cmp.b   (A0)+,D0
         bne.s  ll1                     ; Loop till LF or """

        move.b  D1,-1(A0)               ; Substitute a LineFeed for """


***************************************************************************
*
* Here, go ahead and Write the string.
*       It's pointed to in (A2), length is D4
*
***************************************************************************
ok_go:
        move.l  4,a6
        lea     DOSName(pc),A1          ; Open DOS.library
        moveq.l #MIN_LIB,d0
        jsr     _LVOOpenLibrary(A6)
        or.l    D0,D0
         beq.s  BadExit                 ; Can't!! Return error value...

        move.l  D0,A6

        jsr     _LVOOutput(A6)          ; Get Output handle

        move.l  D0,D1                   ; It's a calling arg
        move.l  A2,D2                   ; A2 is p_string
        move.l  D4,D3                   ; D4 is count of chars
        jsr     _LVOWrite(A6)           ; Echo "String"

        move.l  A6,A1
        move.l  4,A6                    ; AbsExecBase
        jsr     _LVOCloseLibrary(A6)

        moveq   #0,D0                           ; Exit(0)
        rts

************************************************************************
*
* BadExit is where to go if there is an error opening the DOS library
*       This should only happen if things are really sick
*
************************************************************************

BadExit:        moveq   #20,D0
                rts

DOSName:        dc.b    'dos.library',0

************************************************************************


        END
