*SOURCELISTING FOR DEVPAC 2.xx or MAsterSeka 1.5x
* AD: for a quick Ascii Dump of any file...
* 19-oct-90
* by GertJan 'Bugs' van Ratingen
*    P. Dijkmansstraat 1c
*    5611 RA  Eindhoven
*
* usage: AD [-p] [file1] [file2] [...]
*
size = 64			;#bytes per line
execbase = 4
thistask = 276
tc_sigrecvd = 26
sigbreakb_ctrlc = 12

* d6 contains flags:
* bit 0 = quotes around filename
*     1 = alternate chars on
*     2 = end of arguments reached

	subq.w	#1,d0
	beq	noargs
	cmpi.b	#'?',(a0)
	beq	noargs
	movea.l	a0,a2		;argstart
	move.b	#0,(a0,d0.w)
	bsr	opendos
	moveq	#2,d6		;default: alternate chars on
	movea.l	a2,a0		;argstart
* get next argument, stop if there are no more
getarg:	bclr	#0,d6		;quotes off
nospace:	tst.b	(a0)
	beq	exit
	cmpi.b	#' ',(a0)+
	beq	nospace
	subq.l	#1,a0
	cmpi.b	#'-',(a0)		;option?
	bne	noopt
	cmpi.b	#'p',1(a0)	;option p?
	beq	opt
	cmpi.b	#'P',1(a0)	;option P?
	bne	noopt
opt:	bclr	#1,d6		;alternate chars off
	addq.l	#2,a0		;skip option
	bra	getarg
noopt:	cmpi.b	#'"',(a0)		;quotes around filename?
	bne	argchar
	bset	#0,d6		;yes
	addq.l	#1,a0
argchar:	movea.l	a0,a2		;argstart
argchar1:	tst.b	(a0)		;NULL?
	beq	lastarg
	cmp.b	#' ',(a0)		;space?
	bne	nxtchar
	btst	#0,d6		;inside quotes?
	beq	argfound		;no
nxtchar:	cmp.b	#'"',(a0)
	bne	nxtchar2
	btst	#0,d6		;inside quotes?
	bne	argfound		;yep
nxtchar2:	addq.l	#1,a0
	bra	argchar1
lastarg:	bset	#2,d6
argfound:	move.b	#0,(a0)		;close filename with NULL
	movea.l	a0,a3		;argend
	cmpa.l	a2,a0		;argstart
	beq	exit
	
* print "File ";
	lea	filetxt(pc),a0
	moveq	#filelen,d3
	bsr	print
	move.l	a2,d2
	move.l	a3,d3		;argend
	sub.l	a2,d3
	bsr	print2
	moveq	#0,d4		;offset
* open file. if error occurs, print "cannot be opened", and get next argument
	move.l	a2,d1
	move.l	#1005,d2
	jsr	-30(a6)		;open
	movea.l	d0,a4		;filehandle
	bne	read
	lea	unabletxt(pc),a0
	moveq	#unablelen,d3
	bsr	print
	bra	nextarg

* read bytes from file
read:	move.l	a4,d1		;filehandle
	lea	buffer(pc),a0
	exg	d2,a0
	moveq	#size,d3
	jsr	-42(a6)		;read
	move.l	d0,d5		;bytesread

* if 0 bytes are read: end of file reached
	beq	closeit

* convert string (turn $00..$30 into '.')
	lea	buffer(pc),a0
	moveq	#size-1,d0
convert:	cmpi.b	#$20,(a0,d0.w)	;char < $20 ?
	blo	replace		;yes
	cmpi.b	#$7f,(a0,d0.w)	;char < $7f ?
	blo	charok		;yes
	btst	#1,d6		;alternate chars allowed?
	beq	replace		;no
	cmpi.b	#$a0,(a0,d0.w)	;char >= $a0 ?
	bhs	charok
replace:	move.b	#'.',(a0,d0.w)
charok:	dbf	d0,convert

* print hex-offset "nnnn: " and string
	addi.l	#size,d4		;offset
	moveq	#4,d2		;5 digits
	lea	hexnr(pc),a0
	move.l	d4,d0		;offset
	rol.l	#8,d0		;skip 12 MSBits
	rol.l	#4,d0
hex:	rol.l	#4,d0
	move.b	d0,d1
	andi.b	#$f,d1		;4 bits only
	addi.b	#$30,d1		;convert to ascii
	cmpi.b	#$3a,d1		;>"9" ?
	blo	hexnext
	addq.b	#$7,d1		;convert to "A".."F"
hexnext:	move.b	d1,(a0)+
	dbf	d2,hex

	lea	line(pc),a0
	move.l	d5,d3		;bytesread
	addq.l	#8,d3	
	bsr	print

	movea.l	execbase,a0
	movea.l	thistask(a0),a0	;same as FindTask(0L)
	move.l	tc_sigrecvd(a0),d0	;signalmask to d0
	btst	#sigbreakb_ctrlc,d0	;test signalbit
	beq	next		;continue if not set
	move.l	a4,d1		;filehandle
	jsr	-36(a6)		;close
	lea	breaktxt(pc),a0
	moveq	#breaklen,d3
	bra	ex1

* read next line of bytes if end of file not reached
next:	cmpi.l	#size,d5		;bytesread
	beq	read

* close file, go to next argument
closeit:	move.l	a4,d1		;filehandle
	jsr	-36(a6)		;close
	bsr	lf
nextarg:	movea.l	a3,a0		;argend
	addq.l	#1,a0
	btst	#2,d6		;last argument found?
	beq	getarg		;no
	bra	exit

noargs:	bsr	opendos
	lea	noargtxt(pc),a0
	move.l	#noarglen,d3
ex1:	bsr	print
	bra	exit

lf:	lea	line(pc),a0
	moveq	#1,d3
print:	exg	d2,a0
print2:	movea.l	a5,a6		;dosbase
	jsr	-60(a6)		;output
	move.l	d0,d1
	jmp	-48(a6)		;write

exit:	movea.l	a5,a1		;dosbase
	movea.l	execbase,a6
	jsr	-414(a6)		;closelibrary
	moveq	#0,d0
to_cli:	rts			;back to cli

opendos:	movea.l	execbase,a6
	lea	dosname(pc),a1
	jsr	-408(a6)		;oldopenlibrary
	move.l	d0,a5		;dosbase
	rts

line:	dc.b 10
hexnr:	dc.b '00000: '
buffer:	dc.b '................................'
	dc.b '................................'

dosname:	dc.b 'dos.library',0
noargtxt:	dc.b 'Usage: AD [-p] [file] [file] [...]',10,10
	dc.b $9b,"3mAsciiDump 1.2 by GJ 'Bugs' van Ratingen",$9b,"0m",10
	dc.b 'Option p disables alternate characters',10
	dc.b '(useful when redirecting to PRT:)',10
noarglen	equ	*-noargtxt
filetxt:	dc.b 10,'File '
filelen	equ	*-filetxt
unabletxt:	dc.b ' cannot be opened!',10
unablelen:	equ	*-unabletxt
breaktxt:	dc.b 10,'<break>',10
breaklen	equ	*-breaktxt
