

        SECTION  AMIXPR,CODE	;(c)1992 Gellermann +49/621/715101-0

        incdir   "include/"
        include  "exec/exec_lib.i"
        include  "intuition/intuition.i"
        include  "intuition/intuition_lib.i"
        include  "libraries/dos_lib.i"
        include  "libraries/dosextens.i"
        include  "devices/serial.i"
        include  "devices/timer.i"
        include  "graphics/graphics_lib.i"
        include  "graphics/text.i"


_main
;-------------------------------;save commandline max 256byte
	move.b	#0,CLBUFFER	;kill old string in buffer
	tst.l	d0
	beq	cli_null
	subq.l	#1,d0		;a0 -> String, d0 = number of bytes (256max)
	lea	CLBUFFER,a1	;copy
cli	move.b	(a0)+,(a1)+
	dbra	d0,cli
	move.b	#0,-(a1)	;zero at the end
cli_null

;-------------------------------;OPEN LIBS

        moveq    #0,d0
        lea      int_name(pc),a1
        CALLEXEC OpenLibrary
        tst.l    d0
        beq      ClInt
        lea      _IntuitionBase(pc),a0
        move.l   d0,(a0)
;-------
        moveq    #0,d0
        lea      dos_name(pc),a1
        CALLEXEC OpenLibrary
        tst.l    d0
        beq      ClDos
        lea      _DOSBase(pc),a0
        move.l   d0,(a0)
;-------
        moveq    #0,d0
        lea      graf_name(pc),a1
        CALLEXEC OpenLibrary
        tst.l    d0
        beq      ClGraf
        lea      _GfxBase(pc),a0
        move.l   d0,(a0)


;----------------------------------------------------------------------
;----------------------------------------------------------------------
;----------------------------------------------------------------------
;located here bcuz the doslib must be open for printHELP but
;the xprlib must not be open since we have yet to determine
;the xpr_protocolname
				;decode the switches
	lea	CLBUFFER,a0

	tst.b	(a0)		;"amixpr"
	beq	no2DO
	cmpi.b	#" ",(a0)       ;"amixpr "
	beq	no2DO
;-------
main_loop
	cmpi.b	#"-",(a0)
	bne	noSWITCH	
;-------
	addq.l	#1,a0
	bclr.b	#5,(a0)		;cap. letters
;-------
	cmpi.b	#"D",(a0)	
	bne	noDEVICE
        addq.l	#2,a0		;jump switch+space
	lea	device,a1
device_loop
	bset.b	#5,(a0)		;devicename in small chars
	move.b	(a0)+,(a1)+
	tst.b	(a0)		;break if "amixpr -x blabla",0
	beq	no2DO
	cmpi.b	#" ",(a0)
	bne	device_loop		
	move.b	#0,(a1)		;terminate name with 0
	addq.l	#1,a0		;jump space
	bra	main_loop
noDEVICE
;-------
	cmpi.b	#"P",(a0)	
	bne	noPROTOCOL
        addq.l	#2,a0
	lea	protocol,a1
protocol_loop
	bset.b	#5,(a0)		;libname in small chars
	move.b	(a0)+,(a1)+
	tst.b	(a0)
	beq	no2DO
	cmpi.b	#" ",(a0)
	bne	protocol_loop		
	move.b	#0,(a1)		;terminate name with 0
	addq.l	#1,a0		;jump space
	bra	main_loop
noPROTOCOL
;-------
	cmpi.b	#"L",(a0)	
	bne	noLOG
        addq.l	#2,a0
	lea	logfile,a1
log_loop
	move.b	(a0)+,(a1)+
	tst.b	(a0)
	beq	no2DO
	cmpi.b	#" ",(a0)
	bne	log_loop		
	move.b	#0,(a1)		;terminate name with 0
	addq.l	#1,a0		;jump space
	bra	main_loop
noLOG
;-------
	cmpi.b	#"S",(a0)	;WBENCHSCREEN=DEFAULT	
	bne	noSCREEN
        addq.l	#2,a0
	lea	SPTR,a1		;screenptr UDwindow
	move.w	#CUSTOMSCREEN,STYPE
	bsr	HEXStringULONG	;a0^HEX$(8byte) a1^ULONG(4byte)
	tst.b	(a0)
	beq	no2DO
	addq.l	#1,a0		;jump space
	bra	main_loop
noSCREEN
;-------
	cmpi.b	#"B",(a0)	
	bne	noBATCH
	addq.l	#2,a0
	lea	BATCHPTR,a1	;^list of path/filenames separated by
	bsr	HEXStringULONG	; spaces. terminated with zero.
	tst.b	(a0)
	beq	no2DO
	addq.l	#1,a0		;jump space
	bra	main_loop
noBATCH
;-------
	cmpi.b	#"U",(a0)	
	bne	noUNIT
        addq.l	#2,a0
	move.b	(a0)+,d0
	sub.b	#"0",d0		;ascii
	move.b	d0,unit
	tst.b	(a0)
	beq	no2DO
	addq.l	#1,a0		;jump space
	bra	main_loop
noUNIT
;-------
	cmpi.b	#"I",(a0)	
	bne	noINIT
        addq.l	#2,a0		;jump switch+space
	lea	xprinit,a1
init_loop
	move.b	(a0)+,(a1)+
	tst.b	(a0)
	beq	no2DO
	cmpi.b	#" ",(a0)
	bne	init_loop		
	move.b	#0,(a1)		;terminate name with 0
	addq.l	#1,a0		;jump space
	bra	main_loop
noINIT
;-------

no2DO
	bsr	printHELP
        bra	ClXpr		;unknown switch: EXIT

;-------
noSWITCH
	move.l	a0,Pointer	;save current position!!

;----------------------------------------------------------------------
;----------------------------------------------------------------------
;----------------------------------------------------------------------

        moveq    #0,d0
        lea      protocol,a1
        CALLEXEC OpenLibrary
        tst.l    d0
        bne      XPRok
        CALLDOS  Output
        move.l   d0,d1
        move.l   #XPRmsg,d2
        move.l   #XPRmsgLen,d3
        CALLDOS  Write
        bra      ClXpr
XPRok
        move.l   d0,_XPRBase

;---------------------------------------;OPEN FONT
	lea	 MyAttr,a0		;open the topaz8.font bcuz of
	CALLGRAF OpenFont		;possible WB2.0 trouble
	tst.l	 d0
	beq	 ClFont
	move.l	 d0,MyFont

;---------------------------------------;OPEN DEVICES

        lea      SerIO,a1		;IOExtSer Struktur
        clr.l    d0         
        clr.l    d1
	move.b	 unit,d0
        move.b   #$B4,IO_SERFLAGS(a1)	;set radboogie,7wire,(shared=b4)
        lea      device,a0		;exclusive = $94
        CALLEXEC OpenDevice
        tst.l    d0
        bne      ClSer

        sub.l    a1,a1			;Replyport einrichten
        CALLEXEC FindTask		;eigenen Task eintragen
        lea      SerReply,a1
        move.l   d0,MP_SIGTASK(a1) 
        CALLEXEC AddPort		;reply (message-)port einrichten
        lea      SerIO,a1		;replyport eintragen
        move.l   #SerReply,MN_REPLYPORT(a1)

;-------
        lea      TimeIO,a1		;timer.device
        move.l   #UNIT_MICROHZ,d0
        clr.l    d1
        lea      timer_name,a0
        CALLEXEC OpenDevice
        tst.l    d0
        bne      ClTimer

        sub.l    a1,a1
        CALLEXEC FindTask
        lea      TimeReply,a1
        move.l   d0,MP_SIGTASK(a1)
        CALLEXEC AddPort
        lea      TimeIO,a1		;replyport eintragen
        move.l   #TimeReply,MN_REPLYPORT(a1)
 

;---------------------------------------;ALLOC RAM
	move.l	#10000,d0		;Logbuffer
	move.l	#$30001,d1
	CALLEXEC	AllocMem
	tst.l	d0
	beq	noRAM
	move.l	d0,LBMin
	move.l	d0,log_pointer

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
        bsr      MAINEVENT
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

        move.l	LBMin,a1
        move.l	#10000,d0
        CALLEXEC	FreeMem
noRAM
        lea      TimeIO,a1
        CALLEXEC CloseDevice
        lea      TimeReply,a1
        CALLEXEC RemPort
ClTimer
        lea      SerIO,a1
        CALLEXEC CloseDevice
        lea      SerReply,a1
        CALLEXEC RemPort
ClSer
	move.l	 MyFont,a1
	CALLGRAF CloseFont
ClFont
        move.l   _XPRBase,a1
        CALLEXEC CloseLibrary
ClXpr
        move.l   _GfxBase,a1
        CALLEXEC CloseLibrary
ClGraf
        move.l   _DOSBase,a1
        CALLEXEC CloseLibrary
ClDos
        move.l   _IntuitionBase,a1
        CALLEXEC CloseLibrary
ClInt
        move.l  #0,d0			;Return Code 0 = OK
        rts

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
MAINEVENT

	move.l	Pointer,a0
;-------
	bclr.b	#5,(a0)			;change to cap. letters
        cmpi.b  #"R",(a0)
        bne	noRECEIVE

        bsr	Receive
	bsr	save_log
	rts

noRECEIVE
        cmpi.b  #"S",(a0)
        bne	noSEND

        bsr	Send
	bsr	save_log
	rts

noSEND

;-------
printHELP
        CALLDOS  Output			;print helpfile if no valid cmd
        move.l   d0,d1
        move.l   #HelpTxt,d2
        move.l   #HelpTxtLen,d3
        CALLDOS  Write

	rts

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Receive	;"amixpr r path"
	;        ^Pointer

        lea      XPR_IO,a0
        move.l   #xprinit,(a0)
        move.l   _XPRBase,a6
        jsr      -36(a6)		;Setup general params

	move.l	 Pointer,a2
	addq.l	 #1,a2
	move.b	 #"P",(a2)
        lea      XPR_IO,a0
        move.l   a2,(a0)
        move.l   _XPRBase,a6
        jsr      -36(a6)		;Setup receive path

	bsr	 openWINDOW

        lea      XPR_IO,a0
        move.l   _XPRBase,a6
        jsr      -48(a6)		;Receive

	bsr	 closeWINDOW

        lea      XPR_IO,a0
        move.l   _XPRBase,a6
        jsr      -30(a6)		;Cleanup

        rts

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Send	;amixpr s path/name1 path/name2 ... 256bytes max (use -b !!!)
	;       ^Pointer

	bsr	 openWINDOW

	tst.l	 BATCHPTR		;use -b switch?
	beq	 notBATCH		;no (sniff)
	move.l	 BATCHPTR,a1            ;get batch from -b pointer
	bra	 SendOK
notBATCH
	move.l	 Pointer,a1		;get batch from cmdstring
	addq.l	 #2,a1
SendOK
	lea      XPR_IO,a0
	move.l   a1,(a0)		;give pointer to ffirst/fnext
	move.l   _XPRBase,a6
	jsr      -42(a6)		;Send

	bsr	 closeWINDOW

	lea      XPR_IO,a0
	move.l   _XPRBase,a6
	jsr      -30(a6)		;Cleanup

	rts

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;--------------------------------------XPR CALLBACK ROUTINEN--------------
;
;----------------------------------xpr_fopen
xpr_fopen	;a0^FileName a1^Axxsmode return:d0=handle or 0

	move.l  a0,d1
	move.b	(a1),d0
	bclr.b	#5,d0		;cap. letters
	cmpi.b	#"R",d0
        bne	noMODE_READ
        move.l	#MODE_OLDFILE,d2
        CALLDOS	Open		;d1^name d2=modus
	rts

noMODE_READ
        cmpi.b	#"W",d0
        bne	noMODE_WRITE
        move.l	#MODE_NEWFILE,d2
        CALLDOS	Open
	rts

noMODE_WRITE
        cmpi.b	#"A",d0
        bne	noMODE_APPEND
	move.l	d1,d5           ;save ^name
        move.l	#MODE_OLDFILE,d2
        CALLDOS	Open
        tst.l	d0
        bne	OPENok
	move.l	d5,d1
        move.l	#MODE_NEWFILE,d2
        CALLDOS	Open
	rts
OPENok
	move.l	 d0,d5		;save handle
        move.l   d0,d1		;goto end of file (append)
        move.l   #0,d2
        move.l   #OFFSET_END,d3
        CALLDOS  Seek
	move.l	 d5,d0		;handle in d0 must be returned
	rts

noMODE_APPEND
	clr.l	d0
	rts			;do nothing

;----------------------------------xpr_fclose
xpr_fclose
	move.l	a0,d1
	CALLDOS	Close

	bsr	build_log

	rts

;the fclose is the only callback-routine that gets
;called after every filexfer in a batch
;so we build a string of log entries
;here and save it after the xprlib returns.
;----------------------------------xpr_fread
xpr_fread
             move.l  d1,d3  ;Laenge
             move.l  a0,d2  ;Buffer
             move.l  a1,d1  ;Handle
             CALLDOS Read
             rts
;----------------------------------xpr_fwrite
xpr_fwrite
             move.l  d1,d3  ;Laenge
             move.l  a0,d2  ;Buffer
             move.l  a1,d1  ;Handle
             CALLDOS Write
             rts
;----------------------------------xpr_fseek
xpr_fseek
             move.l  d1,d3  ;Modus offset_beginning/current/end
             move.l  d0,d2  ;Abstand
             move.l  a0,d1  ;Handle
             subq.l  #1,d3
             CALLDOS Seek
             tst.l   d0
             bmi.s   fseek1
             clr.l   d0 ;0=success
fseek1
             rts
;----------------------------------xpr_finfo
xpr_finfo

        MOVE.L  D0,-(SP)
        MOVE.L  A0,D1
        MOVE.L  #-2,D2
        CALLDOS Lock
        MOVE.L  D0,D1
        BEQ     finfo2
        MOVE.L  D0,-(SP)
        MOVE.L  D0,D1
        MOVE.L  #FIB,D2
        CALLDOS Examine
        MOVE.L  (SP)+,D1
        CALLDOS UnLock
        MOVE.L  (SP),D0
        CMP.L   #2,D0
        BEQ     finfo1
        CMP.L   #1,D0
        BNE     finfo2
        lea     FIB,a0
        move.l  124(a0),d0 ;anzahl bytes
        BRA     finfo3
finfo1  CLR.L   D0
        BRA     finfo3
finfo2  CLR.L   D0
finfo3  CLR.L   (SP)+
        RTS
;----------------------------------xpr_update
xpr_update
;the most important routine for isdn: gets called between
;every 1k block transfered.. keep it very simple and fast ore it
;will take away 2000cps.. no `%complete` calculation and stuff..

        move.l  a0,Asave        ;save ptr to xpr_update (build_log)
	move.l	(a0),d5		;update mask (what fields have changed)

	cmpi.l	#-1,SPTR
	beq	update_end	;no-xfer-window mode
;-------
	btst.l	#1,d5	
	beq	noFileName

        move.l  #11*8,d0	
        move.l  #20,d1		;filename
        bsr     updatemove
	move.l	#Fill,a0
	move.l	#40,d0
	bsr	updatetext
        move.l  #11*8,d0	
        move.l  #20,d1
        bsr     updatemove
        move.l  Asave,a0
        move.l  8(a0),a0	;^Fname
        clr.l   d0
        move.l  a0,a1
update0
	cmpi.b  #15,(a1)+
        bcs     update4
        addq.b  #1,d0
	cmpi.b	#40,d0		;print max 40 chars
	bne	update0
update4
	bsr     updatetext

noFileName
;-------
	btst.l	#2,d5	
	beq	noFileSize

        move.l  Asave,a0
        move.l  12(a0),d0	;filesize
        lea     Ruelps,a0
        bsr     ULONGtoASCII
        move.l  #11*8,d0
        move.l  #30,d1
        bsr     updatemove
        lea     Ruelps,a0
        move.l  #8,d0
        bsr     updatetext
noFileSize
;-------
	btst.l	#3,d5	
	beq	noMSG

        move.l  #1*8,d0	
        move.l  #90,d1
        bsr     updatemove
	move.l	#Fill,a0
	move.l	#50,d0
	bsr	updatetext
        move.l  #1*8,d0	
        move.l  #90,d1
        bsr     updatemove
        move.l  Asave,a0
        move.l  16(a0),a0
        clr.l   d0
        move.l  a0,a1
update00
	cmpi.b  #15,(a1)+
        bcs     update44
        addq.b  #1,d0
	cmpi.b	#50,d0		;print max 50 chars
	bne	update00
update44
	bsr     updatetext
noMSG
;-------
	btst.l	#4,d5
	beq	noERROR

        move.l  #1*8,d0	
        move.l  #100,d1	
        bsr     updatemove
	move.l	#Fill,a0
	move.l	#50,d0
	bsr	updatetext
        move.l  #1*8,d0	
        move.l  #100,d1	
        bsr     updatemove
        move.l  Asave,a0
        move.l  20(a0),a0
        clr.l   d0
        move.l  a0,a1
update000
	cmpi.b  #15,(a1)+
        bcs     update444
        addq.b  #1,d0
	cmpi.b	#50,d0		;print max 50 chars
	bne	update000
update444
	bsr     updatetext
noERROR
;-------
	btst.l	#16,d5
	beq	noCPS

        move.l  Asave,a0
        move.l  68(a0),d0	;cps
        lea     Ruelps,a0
        bsr     ULONGtoASCII
        move.l  #11*8,d0
        move.l  #70,d1
        bsr     updatemove
        lea     Ruelps,a0
        move.l  #8,d0
        bsr     updatetext
noCPS
;-------
	btst.l	#7,d5
	beq	noBYTES

        move.l  Asave,a0	
        move.l  32(a0),d0	;position
        lea     Ruelps,a0
        bsr     ULONGtoASCII
        move.l  #11*8,d0
        move.l  #40,d1
        bsr     updatemove
        lea     Ruelps,a0
        move.l  #8,d0
        bsr     updatetext
noBYTES
;-------
	btst.l	#15,d5
	beq	noELAPSED

        move.l  #11*8,d0
        move.l  #60,d1
        bsr     updatemove
        move.l  Asave,a0
        move.l  64(a0),a0	;^elapsed time
        move.l  #8,d0		;maxlen
        bsr     updatetext
noELAPSED
;-------
	btst.l	#14,d5
	beq	noEXPECT

        move.l  #11*8,d0
        move.l  #50,d1
        bsr     updatemove
        move.l  Asave,a0
        move.l  60(a0),a0	;^expect time
        move.l  #8,d0		;maxlen
        bsr     updatetext
noEXPECT
;-------
update_end
        rts

;----------------------------------xpr_chkabort
xpr_chkabort	;abort if ESCAPE or CLOSEWINDOW or CARRIER LOST
		;also called between every 1k block

        move.l   UDwindowptr,a0	;IDCMP-Windowport holen
        move.l   wd_UserPort(a0),a0
        CALLEXEC GetMsg
        tst.l    d0
        beq      noMESSAGE

        move.l   d0,a1
        move.l   im_Class(a1),d4
        move.w   im_Code(a1),d5 ;.Word!
	CALLEXEC ReplyMsg

        cmpi.l   #CLOSEWINDOW,d4
        beq      yoABORT
	cmpi.l	 #RAWKEY,d4
	bne	 xpr_chkabort
	cmpi.b	 #$45,d5
	beq	 yoABORT	;escape key pressed (only when win active)
        bra	 xpr_chkabort 	;another msg?

noMESSAGE
        bsr	 xpr_squery     ;vielleicht zu lang fuer isdn?
	move.w	 IO_STATUS(a1),d0
        btst	 #5,d0		;CARRIER LOST?
	bne	 yoABORT
noABORT
        clr.l   d0 
        rts
yoABORT
        move.l   #-1,d0		;d0 nonzero = abort
        rts


;----------------------------------xpr_chkmisc
xpr_chkmisc 
	rts
;----------------------------------xpr_sread
xpr_sread

        move.l   a0,Anull	;IO_DATA
        move.l   d0,Dnull       ;IO_LENGTH
        move.l   d1,Deins	;timeout or 0

        tst.l    d1		;timeout gefordert ?
        bne      wantsTIMEOUT	;wenn ja

        bsr      xpr_squery     ;read all bytes available
        tst.l    d0		;IO_ACTUAL
        bne      sumTHERE
        rts                     ;return with count=0

sumTHERE
        cmp.l    Dnull,d0
        bcc      readOK		;more than asked for?
        move.l   d0,Dnull	;read only as much as requested
readOK
        lea      SerIO,a1
        move.w   #CMD_READ,IO_COMMAND(a1)
        move.l   Dnull,IO_LENGTH(a1)
        move.l   Anull,IO_DATA(a1)
        CALLEXEC DoIO
        move.l   IO_ACTUAL(a1),d0
        rts


wantsTIMEOUT
        lea      SerIO,a1
        move.w   #CMD_READ,IO_COMMAND(a1)
        move.l   Dnull,IO_LENGTH(a1)
        move.l   Anull,IO_DATA(a1)
        CALLEXEC SendIO		;DoIO would not return

        lea      TimeIO,a1
        move.w   #TR_ADDREQUEST,IO_COMMAND(a1)
        move.l   Deins,$24(a1)	;timeout in microsecs
        move.l   #0,$20(a1)	;0 secs
        CALLEXEC SendIO

;--------
waitEVENT
        clr.l    d0		;wait: either asked # of bytes arrive or
        clr.l    d1		;timerequest returns (timeout)

        lea      SerReply,a1	;generate waitmaske
        move.b   MP_SIGBIT(a1),d1
        bset.l   d1,d0

        lea      TimeReply,a1
        move.b   MP_SIGBIT(a1),d1
        bset.l   d1,d0

        CALLEXEC Wait		;timeout!enough_bytes

;--------
        lea      SerIO,a1	;ok, iam back who signaled me?
        CALLEXEC CheckIO	;was it the SerReq?
        beq      noSERIAL
        CALLEXEC WaitIO		;SERIAL did it: cleanup serial

        lea      TimeIO,a1	;abort timer
        CALLEXEC AbortIO
        lea      TimeIO,a1
        CALLEXEC WaitIO		;cleanup timer

        lea      SerIO,a1
        move.l   IO_ACTUAL(a1),d0 ;probably the IO_ACTUAL = # of asked bytes
        rts

;-------
noSERIAL
        lea      TimeIO,a1	;timeout?
        CALLEXEC CheckIO
        beq      noTIMER	;something else did it...return to sleep
        CALLEXEC WaitIO		;TIMER did it: cleanup timer

        lea      SerIO,a1	;abort serial
        CALLEXEC AbortIO
        lea      SerIO,a1
        CALLEXEC WaitIO		;cleanup serial

        lea      SerIO,a1
        move.l   IO_ACTUAL(a1),d0 ;probably IO_ACTUAL=0
        rts
;-------
noTIMER
	bra	 waitEVENT

;----------------------------------xpr_swrite
xpr_swrite

        lea      SerIO,a1
        move.w   #CMD_WRITE,IO_COMMAND(a1)
        move.l   d0,IO_LENGTH(a1)
        move.l   a0,IO_DATA(a1)
        CALLEXEC DoIO
        rts
;----------------------------------xpr_sflush
xpr_sflush

        lea      SerIO,a1
        move.w   #CMD_FLUSH,IO_COMMAND(a1)
        CALLEXEC DoIO
        rts
;----------------------------------xpr_squery
xpr_squery

        lea      SerIO,a1
        move.w   #SDCMD_QUERY,IO_COMMAND(a1)
        CALLEXEC DoIO
        move.l   IO_ACTUAL(a1),d0
        rts
;----------------------------------xpr_unlink
xpr_unlink

        MOVE.L  A0,D1
        CALLDOS DeleteFile
        TST.L   D0
        BEQ     unlink1
        MOVEQ   #-$1,D0
        BRA     unlink2
unlink1 CLR.L   D0
unlink2 RTS
;---------------------------------xpr_getptr
xpr_getptr
        CLR.L        D0
        RTS
;---------------------------------xpr_options
xpr_options
        RTS
;---------------------------------xpr_ffirst
xpr_ffirst	;a0^buffer in which we must copy the first filename,0
		;a1^xpr_filename (from lib-call)

	tst.b	(a1)		;"amixpr s ",0
	beq	ffirst_null
	cmpi.b	#" ",(a1)	;"amixpr s  ",0
	beq	ffirst_null

ffirst_loop
	move.b	(a1)+,(a0)+

	tst.b	(a1)
	beq	ffirst_end	

	cmpi.b	#" ",(a1)
	beq	ffirst_end
	bra	ffirst_loop

ffirst_end
	move.b	#0,(a0) ;terminate name with zero
	move.l	a1,d0	;wird fnext uebergeben in d0!
        rts

ffirst_null
	clr.l	d0	;no file found
        rts
;---------------------------------xpr_fnext
xpr_fnext	;a0^buffer for the next filename

	move.l	d0,a1		;erwarte in d0 ^ auf ende vom letzten namen
                                ;                   oldname oldname
	tst.b	(a1)+           ;                                  ^
	beq	fnext_null	;"oldname",0
	tst.b	(a1)
	beq	fnext_null	;"oldname ",0
	cmpi.b	#" ",(a1)
	beq	fnext_null      ;"oldname  "

fnext_loop
	move.b	(a1)+,(a0)+

	tst.b	(a1)
	beq	fnext_end	

	cmpi.b	#" ",(a1)	;liest solange bis er ein space findet	
	beq	fnext_end
	bra	fnext_loop

fnext_end
	move.b	#0,(a0)
	move.l	a1,d0
	rts

fnext_null
	clr.l	d0	;no file found
        rts


;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-= PROCEDURES -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


;----------------------------------
updatemove                         ;d0=x;d1=y
        move.l   UDwindowptr,a1
        move.l   wd_RPort(a1),a1
        CALLGRAF Move                
        rts
updatetext                         ;a0=^buffer;d0=anzahl
        move.l   UDwindowptr,a1
        move.l   wd_RPort(a1),a1
        CALLGRAF Text
        rts

;----------------------------------
save_log	;save/append the logbuffer to the logfile

        move.l   #logfile,d1
        move.l   #MODE_OLDFILE,d2
        CALLDOS  Open
        move.l   d0,log_handle
        tst.l 	 d0		;0 = not found
        bne      isOPEN

        move.l   #logfile,d1
        move.l   #MODE_NEWFILE,d2
        CALLDOS  Open
        move.l   d0,log_handle
	tst.l	 d0
	beq	 cannotOPEN
isOPEN
        move.l   log_handle,d1	;goto end of file (append)
        move.l   #0,d2
        move.l   #OFFSET_END,d3
        CALLDOS  Seek
;-------
	move.l	 log_handle,d1
	move.l	 LBMin,d2
	move.l	 log_count,d3
	CALLDOS	 Write
;-------
        move.l   log_handle,d1
        CALLDOS  Close

cannotOPEN
        rts


;---------------------------------------;
build_log
;xfer is aborted/unsuccesful if line2 is not equal line3
;or if no log-entry exists for that transfer

	movem.l	d0-d6/a0-a6,-(sp)

	move.l	 log_pointer,a0
	move.l	 Asave,a3	
line_1
        move.l   8(a3),a2   ;Filename
        bsr      printstring
line_2
        move.l   12(a3),d0  ;FileSize
        add.l	 #9,log_count
        bsr      ULONGtoASCII
	move.b	 #10,(a0)+
line_3
        move.l   32(a3),d0  ;bytes really transfered
        add.l	 #9,log_count
        bsr      ULONGtoASCII
	move.b	 #10,(a0)+
line_4
        move.l   60(a3),a2  ;APX Time        INVALID WHEN XFER ABORTED!!
        bsr      printstring                     (garble in this line)
line_5
        move.l   64(a3),a2  ;Elapsed Time    INVALID WHEN XFER ABORTED!!
        bsr      printstring                         (garbled too)
line_6
        move.l   68(a3),d0  ;CPS RATE
        add.l	 #9,log_count
        bsr      ULONGtoASCII
	move.b	 #10,(a0)+
line_7
        move.l   36(a3),d0  ;number of errors
        add.l	 #9,log_count
        bsr      ULONGtoASCII
	move.b	 #10,(a0)+
line_8
        move.l   40(a3),d0  ;number of timeouts
        add.l	 #9,log_count
        bsr      ULONGtoASCII
	move.b	 #10,(a0)+

	move.l	 a0,log_pointer

	movem.l	(sp)+,d0-d6/a0-a6
        rts

;---------------------------------------;
printstring
	cmpi.b   #15,(a2)
	bcs      print_end

	move.b	 (a2)+,(a0)+
	addq.l   #1,log_count
	bra      printstring
print_end
	move.b   #10,(a0)+		;EOL
	addq.l   #1,log_count
	rts

;---------------------------------------;



;---------------------------------------;
openWINDOW
	cmpi.l	 #-1,SPTR
	beq	 noopenWINDOW		;-1 = no xfer window

	lea	UDwindowdef(pc),a0	;infowindow auf.
	CALLINT	OpenWindow
	move.l	d0,UDwindowptr

	move.l	UDwindowptr,a1
	move.l	wd_RPort(a1),a1
	move.l	#1,d0
	CALLGRAF	SetAPen		;colour of text in window

	move.l	UDwindowptr,a1
	move.l	wd_RPort(a1),a1
	move.l	MyFont,a0
	CALLGRAF	SetFont

	move.l	#1*8,d0	
	move.l	#76,d1			;ypos stripe
	bsr	updatemove

	move	#51*8,d0	
	move	#76,d1	
	move.l  UDwindowptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	Draw

	moveq	#5,d5			;Anzahl Zeilen -1
text    moveq	#1*8,d0			;Text ausgeben
	move.l	yps,d1
	move.l	UDwindowptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	Move		;Cursor setzen
	move.l	UDwindowptr,a1
	move.l	wd_RPort(a1),a1
	move.l	tex,a0
	moveq	#50,d0
	CALLGRAF	Text
	add.l	#10,yps			;1 Zeile =10 Pixel hoch
	add.l	#50,tex			;1 Zeile =20 Zeichen
	dbra	d5,text

noopenWINDOW
	rts
;---------------------------------------;
closeWINDOW
	cmpi.l	 #-1,SPTR
	beq	 nocloseWINDOW		;-1 = no xfer window
        move.l   UDwindowptr,a0
        CALLINT  CloseWindow        
nocloseWINDOW
	rts
;---------------------------------------;



;--------------------------------------------
ULONGtoASCII	;a0->8byte Buffer for the String; d0=ULONG to work on
                ;this is ULONGtoDecimalstring not ULONGtoHEX$

		movem.l	d1/d2/a1,-(a7)

		moveq	#7,d2	;8 digits...
		lea.l	Potenzen,a1
next
		move	#"0",d1
make1
		addq	#1,d1
		sub.l	(a1),d0
		bcc	make1
		subq	#1,d1
		add.l	(a1),d0
		move.b	d1,(a0)+
		tst.l	(a1)+
		dbra	d2,next

		movem.l	(a7)+,d1/d2/a1
		rts

Potenzen	dc.l	10000000
		dc.l	1000000
		dc.l	100000
		dc.l	10000
		dc.l	1000
		dc.l	100
		dc.l	10
		dc.l	1
		dc.l	0
		cnop	0,4


;---------------------------------------;
HEXStringULONG	;a0^HEX$(8byte) a1^ULONG(4byte)

	movem.l	d0/d1/d2/a2,-(a7)	;save the registers we use
	move.l	#3,d1			;not a0! (must count on)
HEX00
	move.b	(a0)+,d0	;get first asciibyte
	bset.b	#5,d0		;only small chars. digits unchanged
	move.l	#15,d2          ;0123456789abcde or f
	lea	HEXTAB(pc),a2
HEX02
	cmp.b	(a2,d2),d0
	beq	HEX01
	dbra	d2,HEX02
HEX01
	mulu	#16,d2		;d2=position in hextab (0-15)
	move.b	d2,(a1)		;high nibble (*16)
	move.b	(a0)+,d0	;same with the low nibble
	bset.b	#5,d0		;change to small char
	move.l	#15,d2
HEX03
	cmp.b	(a2,d2),d0
	beq	HEX04
	dbra	d2,HEX03
HEX04
        add.b	d2,(a1)+	;add to highnib
	dbra	d1,HEX00	;4 times (e.g.: 00 c0 12 34)
	movem.l	(a7)+,d0/d1/d2/a2
	rts

HEXTAB	dc.b	"0123456789abcdef"
	cnop	0,4

;---------------------------------------;

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;-=-=-=-= DATA -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

XPR_IO
	dc.l	0
	dc.l	xpr_fopen	;a pointer to this struct must be given
	dc.l	xpr_fclose	;whenever calling a xprlib function
	dc.l	xpr_fread
	dc.l	xpr_fwrite
	dc.l	xpr_sread
	dc.l	xpr_swrite
	dc.l	xpr_sflush
	dc.l	xpr_update
	dc.l	xpr_chkabort
	dc.l	xpr_chkmisc
	dc.l	0		;xpr_gets	;not implemented
	dc.l	0		;xpr_setserial	;not implemented
	dc.l	xpr_ffirst
	dc.l	xpr_fnext
	dc.l	xpr_finfo
	dc.l	xpr_fseek
	dc.l	4		;alle 4 EXT fields programmiert
	dc.l	0
	dc.l	xpr_options	;EXT1
	dc.l	xpr_unlink	;EXT2
	dc.l	xpr_squery	;EXT3
	dc.l	xpr_getptr	;EXT4
	dc.l	0,0,0,0
	cnop	0,4

;----------------------------------------------
SerIO		ds.b	96	;IOExtSer  Alloc for Message and Replyports
SerReply	ds.b	48
;----------------------------------------------
TimeIO		ds.b	48
TimeReply	ds.b	48
;----------------------------------------------
		cnop	0,4

UDwindowdef
		dc.w	14*8,5*8	;coordinates of left-top edge
		dc.w	52*8,14*8       ;x/y size
		dc.b	-1,-1
		dc.l	CLOSEWINDOW!RAWKEY
		dc.l	UDW_Gadgets!UDW_Extras
		dc.l	0
		dc.l	0
		dc.l	UDW_Title
SPTR		dc.l	0		;SCREENPOINTER
		dc.l	0
		dc.w	0,0
		dc.w	0,0
STYPE		dc.w	WBENCHSCREEN	;SCREENTYPE
		cnop	0,4

UDW_Gadgets	equ	WINDOWDRAG!WINDOWCLOSE
UDW_Extras	equ	SMART_REFRESH!ACTIVATE
		cnop	0,4

_DOSBase	dc.l	0
_XPRBase	dc.l	0
_IntuitionBase	dc.l	0
_GfxBase	dc.l	0
UDwindowptr	dc.l	0
log_handle	dc.l	0
Pointer		dc.l	CLBUFFER
Asave		dc.l	0
Anull		dc.l	0
Aeins		dc.l	0
Dnull		dc.l	0
Deins		dc.l	0
BATCHPTR	dc.l	0
LBMin		dc.l	0	;^start of allocated logbuffer
log_pointer	dc.l	0	;^same
log_count	dc.l	0	;number of chars in logbuffer
		cnop	0,4

;-------
MyAttr		dc.l	MyFontName
		dc.w	TOPAZ_EIGHTY
		dc.b	FS_NORMAL
		dc.b	FPF_ROMFONT
		cnop	0,4
MyFontName	dc.b	"topaz.font",0
		cnop	0,4
MyFont		dc.l	0
;-------

Ruelps	dc.b	"........",0
	cnop	0,4
Fill	dc.b	"                                                  " ;LEN=50
	cnop	0,4
;-------
msg	dc.b	"FileName:                                         " ;20
	dc.b	"FileSize:          :                              " ;30
	dc.b	"Position:          :                              " ;40
	dc.b	"APX Time:          :                              " ;50
	dc.b	" Elapsed:          :                              " ;60
	dc.b	"     CPS:          :                              " ;70
	cnop	0,4
tex	dc.l	msg		;write-pointer
yps	dc.l	20		;y-Startposition
;-------

XPRmsg		dc.b	"can't find the xpr.library",13,10
XPRmsgLen	equ	*-XPRmsg
		cnop	0,4

UDW_Title	dc.b	"         AmiXpress Transfer Window               ",0
		cnop	0,4
int_name	INTNAME
		cnop	0,4
timer_name	TIMERNAME
		cnop	0,4
dos_name	DOSNAME
		cnop	0,4
graf_name	GRAFNAME
		cnop	0,4

xprinit		dc.b	"TN,AY,OR,KY,SN,RN,DN,F0,B32",0	;defaults
		dc.b	"u can patch dizzzzzzzzzzzzzzzzzzzzzzzzz"
		cnop	0,4

device		dc.b	"serial.device",0
		dc.b	"u can patch dizzzzzzzzzzzzzzzzzzzzzzzzz"
		cnop	0,4

unit		dc.b	0
		cnop	0,4

protocol	dc.b	"xprzmodem.library",0
		dc.b	"u can patch dizzzzzzzzzzzzzzzzzzzzzzzzz"
		cnop	0,4

logfile		dc.b	"ram:amixpr.log",0
		dc.b	"u can patch dizzzzzzzzzzzzzzzzzzzzzzzzz"
		cnop	0,4

CLBUFFER	dc.b	"r ram:",0	;commandline buffer
		ds.b	256
		cnop	0,4

FIB		ds.b	260		;file-info-block alloc
		cnop	0,4

HelpTxt	
 dc.b "amixpr -p protocol................xprzmodem.library=DEFAULT  ",13,10
 dc.b "v0.92  -d device..................serial.device=DEFAULT      ",13,10
 dc.b "       -u unit(UBYTE->ASCII)......0=DEFAULT                  ",13,10
 dc.b "       -l logfile.................ram:amixpr.log=DEFAULT     ",13,10
 dc.b "       -b batchptr(ULONG->HEX$)...^list of files separated by",13,10
 dc.b "                                  spaces terminated by zero  ",13,10
 dc.b "       -s screenptr(ULONG->HEX$)..^screen to open on         ",13,10
 dc.b "                                  -s ffffffff =noXferWindow  ",13,10
 dc.b "       -i initstring..............TN,AY,OR,KY,SN,RN,DN,F0,B32",13,10
 dc.b "       r path.....................receive file(s)            ",13,10
 dc.b "       s path/name1 path/name2....send file(s)               ",13,10,10

 dc.b "e.g.: `amixpr -p xprymodem.library s file1 file2 file3`      ",13,10
 dc.b "      batchsend 3 files using xprymodem. cmdline=256char max!",13,10
 dc.b "      if -b is used the cmdline filenames will be ignored.   ",13,10
 dc.b "      the HEX$ must be exactly 8 ascii-chars: ` -s 00c3a0ee `",13,10,10

 dc.b "logfile: line1: filename 2: filesize 3: transfered bytes     ",13,10
 dc.b "             4: expected 5: elapsed time (invalid if aborted)",13,10
 dc.b "             6: cpsrate  7: #errors  8: #timeouts            ",13,10
 dc.b "             9: start of next log (batch xfer)               ",13,10

HelpTxtLen	equ *-HelpTxt
		cnop	0,4

;---------------------------------------------------------------------
             END
;---------------------------------------------------------------------


