;
;     TellEd 2.0          by Ed Mackey
;        (Communicate        PO Box 475
;         with EdPlayer)     Wayne, PA 19087

;  Here is the source code, assembled with Assempro by Abacus, the same
;  assembler used to create Ball, MegaBall, and EdPlayer itself.

;  This program should be distributed with EdPlayer.  See EdPlayer.DOC
;  for usage info.

;  After assembling, type "PROTECT TellEd +P" at a CLI so that the PURE
;  flag is set.  This program is PURE so that you can make it resident
;  with "resident" or "Ares" if you have it.

;  V2.0 note: This version added some error checking that should have
;             been here all along, like what happens if there are no
;             args.  I THOUGHT I had that in 1.0, but I never tested
;             it, and it didn't work.

       Include 'includes:IncludeMe'
       Include 'includes:exec/types.i'
       Include 'includes:exec/lists.i'
       Include 'includes:exec/memory.i'

       ILABEL  'includes:Amiga.L'

       move.l  #21,d7    ;Error retcode in case of early exit
       move.l  d0,d5          ;Length of CLI arguments in d5
       cmp.l   #3,d5
       ble     MemKicked      ;If no arguments, just exit.
       cmp.l   #300,d5
       bge     MemKicked
       cmp.l   #0,a0
       beq     MemKicked
       move.l  EBase,a6
       move.l  #384,d0
       move.l  #MEMF_CLEAR+MEMF_PUBLIC,d1
       move.l  a0,a3       ;Save the args in a3
       jsr     _LVOAllocMem(a6)  ;Dynamic allocation for PURE program!
       move.l  d0,a4   ;0(a4) = MsgPort, 40(a4) = Message,
       tst.l   d0      ;84(a4) = CopyArgs buffer
       beq     MemKicked
       move.b  #4,8(a4)   ;Type = MsgPort
       move.b  #1,9(a4)   ;Pri = 1
       move.b  #5,48(a4)  ;Type = message
       move.l  a3,a0       ;Bring the CLI args back to a0
       move.l  d5,d0
       subq    #1,d0       ;Arg length minus 1
       lea     84(a4),a2      ;I copy the args to a buffer here,
MoreCp:move.b  (a0)+,(a2)+    ;but I probably don't need to.
       dbra    d0,MoreCp
       move.l  d5,d0
       subq.l  #1,d0
       lea     84(a4),a0      ;Trim off funny chars from the args
       move.l  a0,a1
KillJ: tst.b   (a0)
       beq     RmSpc
       cmp.b   #$a,(a0)+      ;Remove any 0x0A characters
       beq     NoJ
       dbra    d0,KillJ
NoJ:   subq.l  #1,a0
RmSpc: clr.b   (a0)          ;Remove trailing spaces
       cmp.l   a0,a1
       bhs     BOL         ;Don't delete past Beginning Of Line
       cmp.b   #' ',-(a0)
       beq     RmSpc
BOL:   lea     20(a4),a0      ;Now:  Prepare the replyport!
       NEWLIST a0
       sub.l   a1,a1
       move.l  EBase,a6
       jsr     _LVOFindTask(a6)  ;My task # in replyport
       move.l  a4,a2
       move.l  d0,16(a2)
       lea     40(a4),a3   ;PORT is in A2, MESSAGE is in A3
       move.l  a2,14(a3)
       move.w  #44,18(a3)  ;LENGTH IS .W!
       lea     84(a4),a5
       move.l  a5,40(a3)   ;Put arg pointer in message
       move.l  #-1,d0
       jsr     _LVOAllocSignal(a6)  ;Get a signal for replyport
       cmp.b   #-1,d0
       beq     SigKicked
       move.b  d0,15(a2)
       moveq.l #1,d1
       and.l   #$ff,d0
       lsl.l   d0,d1
       move.l  d1,d6       ;Save signal in expanded (mask) form
       jsr     _LVOForbid(a6)  ;Forbid before using FindPort()
       lea     FindMe,a1
       jsr     _LVOFindPort(a6)  ;Find EdPlayer!
       tst.l   d0
       beq     NoPrt
       move.l  d0,a0
       lea     40(a4),a1
       jsr     _LVOPutMsg(a6)   ;Send message to EdPlayer!
       jsr     _LVOPermit(a6)   ;Don't forget to Permit() AFTER msg sent.
TryAgn:move.l  a4,a0
       jsr     _LVOGetMsg(a6)   ;Look for returned message
       tst.l   d0
       bne     GotIt
       move.l  d6,d0
       jsr     _LVOWait(a6)     ;Or Wait() for returned message
       bra     TryAgn           ;and try again!
GotIt: move.l  d0,a5
       move.l  32(a5),d7        ;Load return code.
ende:
       move.l  EBase,a6
       moveq.l #0,d0
       move.b  15(a4),d0
       jsr     _LVOFreeSignal(a6)   ;Deallocate stuff.
SigKicked:
       move.l  EBase,a6
       move.l  #384,d0
       move.l  a4,a1
       jsr     _LVOFreeMem(a6)
MemKicked:
       move.l  d7,d0
       rts                          ;that's all folks!

NoPrt: jsr     _LVOPermit(a6)
       move.l  #21,d7           ;Failure:  EdPlayer not found.
       bra     ende

       ;data section!
FindMe:
       dc.b    'EDPLAYER',0   ;Find this port

       dc.b    'Telled v2.0 by Ed Mackey',0  ;Just identification
       align

       END
