	include	"src:shadowlord.i"

; V1.01
; Added bigger understanding of PC box characters.
; Now also understands one-wide box chars, and thick corner changed to #,
; single corner to +.

* First: 2632
* Clearing: 2588
* 30-Jan-93
* 6 days of hard coding

testmode	=	1

	if	testmode=1
	lea	test_comline,a0
	move.l	#test_len,d0
	endc

progstart:
	clr.b	-1(a0,d0.l)
	move.l	a0,commandstart
	move.w	d0,commandlen
	add.l	d0,a0
	subq.l	#1,a0
	move.l	a0,commandend

	lea	dosname,a1
	moveq	#0,d0
	move.l	execbase,a6
	jsr	openlibrary(a6)
	move.l	d0,dosbase

	move.l	dosbase,a6
	jsr	output(a6)
	move.l	d0,outstruct

	move.l	#text_cursoroff,d2
	moveq	#5,d3
	bsr	printtext

	move.l	commandstart,a0
	tst.b	(a0)			; if blank commandline, instructions
	beq	credits
	cmp.b	#"?",(a0)
	beq	credits

parse:
	cmp.b	#" ",1(a0)		; at least one space after option
	bne	err_badcomline
	cmp.b	#"p",(a0)
	beq	frompcmode
	cmp.b	#"P",(a0)
	beq	topcmode
	cmp.b	#"s",(a0)
	beq	fromsf7mode
	cmp.b	#"S",(a0)
	beq	tosf7mode
	bra	err_badcomline

frompcmode:
	move.b	#1,mode
	bra	nameparse

fromsf7mode:
	move.b	#2,mode
	bra	nameparse

tosf7mode:
	move.b	#-2,mode
	bra	nameparse

topcmode:
	move.b	#-1,mode
	bra	nameparse

nameparse:
	addq.l	#1,a0
	bsr	skipspace
	cmp.l	#0,a0
	beq	err_badcomline
	move.l	a0,sourcenamestart
	bsr	findend
	cmp.l	#0,a0
	beq	err_badcomline
	move.l	a0,sourcenameend
	addq.l	#1,a0
	sub.l	sourcenamestart,a0
	move.l	a0,sourcenamelen

	move.l	sourcenamestart,a0
	move.l	#sourcename,a1
	move.l	sourcenamelen,d0
	subq.l	#1,d0
	cmp.b	#'"',(a0)
	beq	removequotes

copysourcename:
	move.b	(a0)+,(a1)+
	dbf	d0,copysourcename
	clr.b	(a1)

	move.l	sourcenameend,a0
	addq.l	#1,a0
	cmp.l	commandend,a0
	beq	nodest

	bsr	skipspace
	cmp.l	#0,a0
	beq	err_badcomline
	move.l	a0,destnamestart
	bsr	findend
	cmp.l	#0,a0
	beq	err_badcomline
	move.l	a0,destnameend
	addq.l	#1,a0
	sub.l	destnamestart,a0
	move.l	a0,destnamelen
	move.l	a0,d0

	move.l	destnamestart,a0
	move.l	#destname,a1
	subq.l	#1,d0
	cmp.b	#'"',(a0)
	beq	removequotes1

copydestname:
	move.b	(a0)+,(a1)+
	dbf	d0,copydestname
	bra	openfiles

removequotes:
	addq.l	#1,a0
	subq.l	#2,d0
	bra	copysourcename

removequotes1:
	addq.l	#1,a0
	subq.l	#2,d0
	bra	copydestname

removequotes2:
	addq.l	#1,a0
	subq.l	#2,d0
	bra	copydestname1

nodest:
	move.l	#destname,a1
	move.l	sourcenamestart,a0
	move.l	sourcenamelen,d0
	subq.l	#1,d0
	cmp.b	#'"',(a0)
	beq	removequotes2

copydestname1:
	move.b	(a0)+,(a1)+
	dbf	d0,copydestname1

	cmp.b	#-1,mode
	beq	pcsuffix
	cmp.b	#-2,mode
	beq	sf7suffix

amigasuffix:
	lea	text_amigasuf,a0
	moveq	#4-1,d0

amigasuffix1:
	move.b	(a0)+,(a1)+
	dbf	d0,amigasuffix1
	bra	openfiles

pcsuffix:
	lea	text_pcsuf,a0
	moveq	#3-1,d0

pcsuffix1:
	move.b	(a0)+,(a1)+
	dbf	d0,pcsuffix1
	bra	openfiles

sf7suffix:
	lea	text_sf7suf,a0
	moveq	#4-1,d0

sf7suffix1:
	move.b	(a0)+,(a1)+
	dbf	d0,sf7suffix1

openfiles:
	clr.b	(a1)			; end of filename
	move.l	#sourcename,d1
	move.l	#1005,d2
	move.l	dosbase,a6
	jsr	open(a6)
	tst.l	d0
	beq	err_nosource
	move.l	d0,sourcehandle

	move.l	#destname,d1
	move.l	#1006,d2
	move.l	dosbase,a6
	jsr	open(a6)
	tst.l	d0
	beq	err_nodest
	move.l	d0,desthandle

readblock:
	move.l	sourcehandle,d1
	move.l	#sourcebuf,d2
	move.l	#1024,d3
	move.l	dosbase,a6
	jsr	read(a6)
	move.l	d0,d1
	beq	closefiles
	subq.l	#1,d1
	moveq	#0,d2

	lea	sourcebuf,a0
	lea	destbuf,a1

morecharacters:
	move.b	(a0)+,d0
	cmp.b	#-2,mode
	beq	tosf7
	cmp.b	#-1,mode
	beq	topc
	cmp.b	#2,mode
	beq	fromsf7

frompc:
	cmp.b	#$84,d0
	beq	ami_lowae
	cmp.b	#$94,d0
	beq	ami_lowoe
	cmp.b	#$8e,d0
	beq	ami_upae
	cmp.b	#$99,d0
	beq	ami_upoe
	cmp.b	#13,d0
	beq	nochar
	cmp.b	#0,d0
	beq	nochar
	cmp.b	#$1a,d0			; ctrl+z
	beq	nochar
	cmp.b	#$c9,d0
	beq	ami_bigcorner
	cmp.b	#$bb,d0
	beq	ami_bigcorner
	cmp.b	#$c8,d0
	beq	ami_bigcorner
	cmp.b	#$bc,d0
	beq	ami_bigcorner
	cmp.b	#$cd,d0
	beq	ami_doubleline
	cmp.b	#$ba,d0
	beq	ami_upline
	cmp.b	#$b3,d0
	beq	ami_upline
	cmp.b	#$b0,d0
	beq	ami_box
	cmp.b	#$b1,d0
	beq	ami_box
	cmp.b	#$b2,d0
	beq	ami_box
	cmp.b	#$c4,d0
	beq	ami_singleline
	cmp.b	#$c0,d0
	beq	ami_smallcorner
	cmp.b	#$d9,d0
	beq	ami_smallcorner
	cmp.b	#$bf,d0
	beq	ami_smallcorner
	cmp.b	#$da,d0
	beq	ami_smallcorner

nextchar:
	move.b	d0,(a1)+
	addq.l	#1,d2

nochar:
	dbf	d1,morecharacters

	move.l	desthandle,d1
	move.l	d2,d3
	move.l	#destbuf,d2
	move.l	dosbase,a6
	jsr	write(a6)
	bra	readblock

topc:
	cmp.b	#"ä",d0
	beq	pc_lowae
	cmp.b	#"ö",d0
	beq	pc_lowoe
	cmp.b	#"Ä",d0
	beq	pc_upae
	cmp.b	#"Ö",d0
	beq	pc_upoe
	cmp.b	#10,d0
	beq	addcr
	cmp.b	#0,d0
	beq	nochar
	cmp.b	#$1a,d0			; ctrl+z
	beq	nochar
	bra	nextchar

fromsf7:
	cmp.b	#"{",d0
	beq	ami_lowae
	cmp.b	#"|",d0
	beq	ami_lowoe
	cmp.b	#"[",d0
	beq	ami_upae
	cmp.b	#"\",d0
	beq	ami_upoe
	cmp.b	#13,d0
	beq	nochar
	cmp.b	#0,d0
	beq	nochar
	cmp.b	#$1a,d0			; ctrl+z
	beq	nochar
	bra	nextchar

tosf7:
	cmp.b	#"ä",d0
	beq	sf7_lowae
	cmp.b	#"ö",d0
	beq	sf7_lowoe
	cmp.b	#"Ä",d0
	beq	sf7_upae
	cmp.b	#"Ö",d0
	beq	sf7_upoe
	cmp.b	#10,d0
	beq	addcr
	cmp.b	#0,d0
	beq	nochar
	cmp.b	#$1a,d0			; ctrl+z
	beq	nochar
	bra	nextchar

ami_singleline:
	move.b	#"-",d0
	bra	nextchar

ami_box:
	move.b	#$7f,d0
	bra	nextchar

ami_upline:
	move.b	#"|",d0
	bra	nextchar

ami_doubleline:
	move.b	#"=",d0
	bra	nextchar

ami_smallcorner:
	move.b	#"+",d0
	bra	nextchar

ami_bigcorner:
	move.b	#"#",d0
	bra	nextchar

ami_lowae:
	move.b	#"ä",d0
	bra	nextchar

ami_lowoe:
	move.b	#"ö",d0
	bra	nextchar

ami_upae:
	move.b	#"Ä",d0
	bra	nextchar

ami_upoe:
	move.b	#"Ö",d0
	bra	nextchar

pc_lowae:
	move.b	#$84,d0
	bra	nextchar

pc_lowoe:
	move.b	#$94,d0
	bra	nextchar

pc_upae:
	move.b	#$8e,d0
	bra	nextchar

pc_upoe:
	move.b	#$99,d0
	bra	nextchar

sf7_lowae:
	move.b	#"{",d0
	bra	nextchar

sf7_lowoe:
	move.b	#"|",d0
	bra	nextchar

sf7_upae:
	move.b	#"[",d0
	bra	nextchar

sf7_upoe:
	move.b	#"\",d0
	bra	nextchar

addcr:
	move.b	#13,(a1)+
	addq.l	#1,d2
	bra	nextchar

closefiles:
	move.l	sourcehandle,d1
	move.l	dosbase,a6
	jsr	close(a6)
	move.l	desthandle,d1
	move.l	dosbase,a6
	jsr	close(a6)
	bra	endprogram1

credits:
	move.l	#text_credits,d2
	move.l	#len_credits,d3
	bsr	printtext
	bra	endprogram1

err_nosource:
	move.l	#text_nosource,d2
	moveq	#36,d3
	bsr	printtext
	bra	endprogram1

err_nomemory:
	move.l	#text_nomemory,d2
	moveq	#26,d3
	bsr	printtext
	bra	endprogram1

err_badcomline:
	move.l	#text_badcomline,d2
	moveq	#25,d3
	bsr	printtext
	bra	endprogram1

err_nodest:
	move.l	#text_nodest,d2
	moveq	#41,d3
	bsr	printtext
	bra	endprogram2

endprogram1:
	move.l	#text_cursoron,d2
	moveq	#5,d3
	bsr	printtext

	move.l	execbase,a6
	move.l	dosbase,a1
	jsr	closelibrary(a6)
	moveq	#0,d0
	rts

endprogram2:
	move.l	sourcehandle,d1
	move.l	dosbase,a6
	jsr	close(a6)
	bra	endprogram1

**********
** SUBS **
**********

printtext:
	if	testmode=1
	rts
	endc
	move.l	outstruct,d1
	move.l	dosbase,a6
	jsr	write(a6)
	rts

skipspace:
	cmp.b	#" ",(a0)
	beq	skipspace1
	rts

skipspace1:
	add.l	#1,a0
	cmp.l	commandend,a0
	bcs	skipspace
	move.l	#0,a0
	rts

findend:
	cmp.b	#'"',(a0)
	beq	findend_quote

findend_space:
	addq.l	#1,a0
	cmp.b	#" ",(a0)
	beq	findend_space1
	cmp.b	#0,(a0)
	beq	findend_space1
	cmp.l	commandend,a0
	bls	findend_space
	move.l	#0,a0
	rts

findend_space1:
	subq.l	#1,a0
	rts

findend_quote:
	addq.l	#1,a0
	cmp.b	#'"',(a0)
	bne	findend_quote1
	rts

findend_quote1:
	cmp.l	commandend,a0
	bls	findend_quote
	move.l	#0,a0
	rts


**********
** DATA **
**********

	section	fastdata,data

text_credits:
	dc.b	27,"[1m"
	dc.b	"Port V1.01  (31-Jan-93)",10
	dc.b	"Copyright © 1993 by Henrik Tikanvaara",10
	dc.b	27,"[0m",10
	dc.b	10
	dc.b	"Usage: Port <Mode> <Source> [Dest]",10
	dc.b	10
	dc.b	27,"[1m"
	dc.b	"  Mode"
	dc.b	27,"[0m"
	dc.b	" Conversion mode.",10
	dc.b	"       s = SF7->Amiga",10
	dc.b	"       p = PC ->Amiga",10
	dc.b	"       S = Amiga->SF7",10
	dc.b	"       P = Amiga->PC",10
	dc.b	27,"[1m"
	dc.b	"Source"
	dc.b	27,"[0m"
	dc.b	" The source filename to be converted.",10
	dc.b	27,"[1m"
	dc.b	"  Dest"
	dc.b	27,"[0m"
	dc.b	" The optional destination filename for the converted",10
	dc.b	"       text.  Otherwise the filename will have a suffix",10
	dc.b	"       related to the mode.",10
	dc.b	10
	dc.b	"If you have an idea of something you'd want to see in this",10
	dc.b	"program, please send me a note.",10
	dc.b	10
	dc.b	"InterNet: sl@field.ichaos.nullnet.fi",10

text_cursoroff:
	dc.b	27,"[0 p"

text_cursoron:
	dc.b	27,"[1 p"

text_badcomline:
	dc.b	"ERROR!  Bad commandline.",10

text_nomemory:
	dc.b	"ERROR!  Not enough memory.",10

text_badsource:
	dc.b	"ERROR!  Bad sourcefile.",10

text_nosource:
	dc.b	"ERROR!  Could not open source file.",10

text_nodest:
	dc.b	"ERROR!  Could not open destination file.",10

dosname:
	dc.b	"dos.library",0

text_amigasuf:
	dc.b	".AMI"

text_pcsuf:
	dc.b	".PC"

text_sf7suf:
	dc.b	".SF7"

	if	testmode=1

test_comline:
	dc.b	'a    "dh1:tes. txt"   "dh1:tes su.xx"',10
;	dc.b	"a dh1:tests.txt dh1:testd.txt",10

test_comlineend:

test_len	=	test_comlineend-test_comline
	endc

	section	fastblank,bss

sourcehandle:
	ds.l	1

desthandle:
	ds.l	1

outstruct:
	ds.l	1

destnamestart:
	ds.l	1

destnameend:
	ds.l	1

destnamelen:
	ds.l	1

sourcenamestart:
	ds.l	1

sourcenameend:
	ds.l	1

sourcenamelen:
	ds.l	1

commandstart:
	ds.l	1

commandend:
	ds.l	1

commandlen:
	ds.l	1

dosbase:
	ds.l	1

mode:
	ds.b	1

sourcename:
	ds.b	256

destname:
	ds.b	256

sourcebuf:
	ds.b	1024

destbuf:
	ds.b	2048

len_credits	=	text_cursoroff-text_credits
