;
; clcom v1.0
; written by Mauro Panigada
;
; $VER: clcom.a v1.0 (c) 1997 by Mauro Panigada
;
; Opened: 1997-04-10
; Closed: 1997-04-10
;
; History: 1997-04-10   v1.0 finished.
;          1997-04-11   fixed behavior when QUIET flag specified, now
;                       always print the error.
;
; Note: it is pure. It needs OS3.0
;
; Purpose: it clone a comment from a file to anothers. Useful when
;   you want give the same comment to many files (or 1).
;
; Template: FROM/A,TO/A/M,C=COMMENT/K,QUIET/S
;
; FROM/A: the file which the comment will be cloned from.
; TO/A/M: the file(s) which the comment will be setted to.
; C=COMMENT/K: the comment itself. If this keyword is written, the
;              <FROM> file and the <TO> file(s) will become the same
;              thing, and to all these that comment will be setted.
; QUIET/S: do it silently
;
; Strange things: it have not the same behavior for all cases you can
;   give. However, this is not a bug, it works well. (you can understand
;   if you understand the piece of code i have marked. It is about
;   QUIET flag)
;
; What can be made better: the error message can be more. Now only
;   few 'error-points' are really treated and the user can know them.
;   In other case you ask: why does it exit?
;
; * To do: patternmatching in the multiple args (and in <from> file
;          only if the comment keywords is selected)
;

		incdir	"dh0:progr/assem/include/"
		include	"exec/types.i"
		include	"exec/libraries.i"
		include	"exec/memory.i"
		include	"exec/exec_lib.i"
		include	"dos/dos.i"
		include	"dos/dos_lib.i"


stov		MACRO
		move.\0	\1,_\2(a5)
		ENDM

putv		MACRO
		move.\0	_\1(a5),\2
		ENDM

clrv		MACRO
		clr.\0	_\1(a5)
		ENDM

tstv		MACRO
		tst.\0	_\1(a5)
		ENDM


openlib		MACRO
		lea	\1name(pc),a1
		moveq	#\2,d0
		call	OpenLibrary
		stov.l	d0,\1base
		beq	exit
		ENDM

closelib	MACRO
		putv.l	\1base,a1
		call	CloseLibrary
		ENDM


call		MACRO
		IFEQ	NARG-1
		jsr	_LVO\1(a6)
		ELSE
		putv.l	\2base,a6
		jsr	_LVO\1(a6)
		ENDC
		ENDM




	STRUCTURE reserved,0
		LONG	_execbase
		LONG	_dosbase

		LABEL	_array
		LONG	_fromT
		LONG	_toT
		LONG	_commentT
		LONG	_quiteT

		LONG	_comment

		LABEL	reserved_size


	SECTION	clcom,CODE

		moveq	#0,d7
		move.l	4.w,a6
		moveq	#reserved_size,d0
		move.l	#MEMF_ANY|MEMF_CLEAR,d1
		call	AllocMem
		move.l	d0,a5
		tst.l	d0
		beq.s	fexit
		move.l	a6,(a5)

		bsr	start

		move.l	a5,a1
		moveq	#reserved_size,d0
		call	FreeMem,exec
		move.l	d7,d0
		rts
fexit		moveq	#RETURN_FAIL,d0
		rts

		dc.b	"$VER: "
		dc.b	"clcom v1.0 (c) 1997 by Mauro Panigada",10,13
		dc.b	0
		even

start
		moveq	#0,d6
		moveq	#0,d5
		moveq	#0,d4

		openlib	dos,39

		move.l	d0,a6
		lea	template(pc),a0
		move.l	a0,d1
		move.l	a5,d2
		addq.l	#_array,d2
		moveq	#0,d3
		call	ReadArgs
		move.l	d0,d6		; d6 =arg (rd arg)
		bne.s	givenarg

error0		call	IoErr
		move.l	d0,d1		; really IoErr fill also d1, but
		lea	tasky(pc),a0	; is not documented
		move.l	a0,d2
		call	PrintFault
		moveq	#RETURN_ERROR,d7
		bra	exit

givenarg	tstv.l	commentT
		beq.s	normal		; not a comment

		putv.l	commentT,d2
		stov.l	d2,comment
		putv.l	fromT,d1
		call	SetComment
		tst.l	d0
		bne.s	scok		; *****
		call	IoErr		; *****
		move.l	d0,d1		; *****
		putv.l	fromT,d2	; *****
		call	PrintFault	; *****
		moveq	#RETURN_WARN,d7	; *****
scok		tstv.l	quiteT		; *****
		bne.s	doit		; *****
		putv.l	fromT,d1
		call	PutStr
		lea	oktxt(pc),a0
		move.l	a0,d1
		call	PutStr
		bra.s	doit

normal		putv.l	fromT,d1
		moveq	#SHARED_LOCK,d2
		call	Lock
		move.l	d0,d5		; d5=<from>lock
		beq.s	error0

oklock		moveq	#DOS_FIB,d1
		moveq	#0,d2
		call	AllocDosObject
		move.l	d0,d4		; d4=dos_fib
		beq	exit

		move.l	d4,d2
		move.l	d5,d1
		call	Examine
		tst.l	d0
		beq	error0

		move.l	d4,d0
		add.l	#fib_Comment,d0
		stov.l	d0,comment

doit		putv.l	toT,a3
		putv.l	comment,d3
loop		move.l	#SIGBREAKF_CTRL_C,d1
		call	CheckSignal
		and.l	#SIGBREAKF_CTRL_C,d0
		bne.s	break
		move.l	(a3)+,d1
		beq.s	ended
		move.l	d3,d2
		call	SetComment
		tst.l	d0
		beq.s	error2
		tstv.l	quiteT		; *****)
		bne.s	loop		; *****)
		move.l	-4(a3),d1	; *****)
		call	PutStr		; *****)
		lea	oktxt(pc),a0	; *****)
		move.l	a0,d1		; *****)
		call	PutStr		; *****)
		bra.s	loop
error2		moveq	#RETURN_WARN,d7
		;tstv.l	quiteT		; *****
		;bne.s	loop		; *****
		call	IoErr		; *****
		move.l	d0,d1		; idem, see above
		move.l	-4(a3),d2	; *****
		call	PrintFault	; *****
		bra.s	loop

break		lea	breakt(pc),a0
		move.l	a0,d1
		call	PutStr
		moveq	#0,d7
ended

exit
		move.l	d4,d1
		beq.s	ex2
		call	FreeDosObject,dos
ex2

		move.l	d5,d1
		beq.s	ex1
		call	UnLock,dos
ex1

		move.l	d6,d1
		beq.s	ex0
		call	FreeArgs,dos
ex0
		move.l	(a5),a6
		closelib dos
		rts



dosname		dc.b	"dos.library",0
		even
template	dc.b	"FROM/A,TO/A/M,C=COMMENT/K,QUIET/S",0
		even
tasky		dc.b	"clcom",0
		even
breakt		dc.b	"*** USER BREAK",10,0
		even
oktxt		dc.b	" ...done",10,0
		even


		END