*****************************************************************************
*									    *
*	PROGRAM: SetDTool						    *
*	VERSION: 1.1							    *
*   SOURCE CODE: 10							    *
*	   DATE: 2.4.2000						    *
*      LANGUAGE: Assembler (DevPac V3.14)				    *
*	 SYSTEM: A1200	KS 40.68 WB 40.42 2MB chip 64MB fast 68030/50mhz    *
*									    *
*	 AUTHOR: Joerg Riemer 14167 Berlin/SchottmuellerStr.107/Germany	    *
*	  EMAIL: mission@cs.tu-berlin.de				    *
*									    *
*	COMMENT: cli-command to change the defaulttool defined		    *
*		 in project-icons. 					    *
*									    *
*      TEMPLATE: FILE/A,TOOL/K,QUIET/S					    *
*									    *
*****************************************************************************

*		opt	d+

BSS_Section	set	0

*****************************************************************************

	output	ram:SetDTool

************************************************ exec definitions ***********

_LVOOpenLibrary		equ	-552
_LVOCloseLibrary	equ	-414

************************************************ dos definitions ************

_LVOOutput		equ	-060
_LVOIoErr		equ	-132
_LVOFPutC		equ	-312
_LVOFPuts		equ	-342
_LVOSetIoErr		equ	-462
_LVOPrintFault		equ	-474
_LVOReadArgs		equ	-798
_LVOFreeArgs		equ	-858
_LVOMatchFirst		equ	-822
_LVOMatchNext		equ	-828
_LVOMatchEnd		equ	-834

ap_BreakBits		equ	$008
ap_Flags		equ	$010
ap_Strlen		equ	$012
ap_Info			equ	$014

fib_DirEntryType	equ	$004

SIGBREAKB_CTRL_C	equ	$00C
SIGBREAKB_CTRL_D	equ	$00D
SIGBREAKB_CTRL_E	equ	$00E
SIGBREAKB_CTRL_F	equ	$00F

************************************************ icon definitions ***********

_LVOGetDiskObject	equ	-078
_LVOPutDiskObject	equ	-084
_LVOFreeDiskObject	equ	-090

WBPROJECT		equ	$004
do_Type			equ	$030
do_DefaultTool		equ	$032

************************************************ bss datas ******************

Anchorpath	rs.b	280			;size_of anchorpath
PNameOrg	rs.b	256			;size_of ap_strlen (pathname)
PNameBuf	rs.b	256			;size_of workbuffer
DOSBase		rs.l	1			;ptr. base of library
ICOBase		rs.l	1			;ptr. base of library

Filename	rs.l	1			;argarray ptr. filename
NewDefTool	rs.l	1			;argarray ptr. newtoolstring
Quiet		rs.b	1			;argarray flag (no output)
EntryType	rs.b	1			;flag (found #?.info)
Flag3		rs.b	1			;pad
Flag4		rs.b	1			;pad

RDArgs		rs.l	1			;ptr. rdargs structure
RCode1		rs.l	1			;errorcode > rc
RCode2		rs.l	1			;errorcode > result2
MatchReturn	rs.l	1			;errorcode (matchnext)
DObject		rs.l	1			;ptr. workbench diskobject
OldDefTool	rs.l	1			;ptr. do_defaulttool
FCount		rs.l	1			;counter (files proccessed)
StrEnd		rs.l	1			;ptr. stringend w/o ".info"

BSS_Size	rs.b	0			;bss_size over all

*****************************************************************************

Start		movem.l	D1-D7/A0-A6,-(SP)	;save registers

		if	BSS_Section		;check assembler condition

		movea.l	Start-4(pc),A5		;get bss_section
		addq.l	#1,A5			;skip pointer (next segment)
		adda.l	A5,A5			;convert bpcl_pointer
		adda.l	A5,A5			;to real_address

		elseif				;otherwise

		lea	DataBase(pc),A5		;set working area

		endc				;end condition

************************************************ open libraries *************

		moveq	#20,D0			;failat level
		move.l	D0,RCode1(A5)		;preset error (no library)

		movea.l	4.w,A6			;set EXEC to call a function

		moveq	#37,D0			;required library version
		lea	DOSName(pc),A1		;set library name
		jsr	_LVOOpenLibrary(A6)	;use EXEC to (openlibrary)
		move.l	D0,DOSBase(A5)		;save base

		beq.b	CloseLibs		;branch if not open

		moveq	#37,D0			;required library version
		lea	ICOName(pc),A1		;set library name
		jsr	_LVOOpenLibrary(A6)	;use EXEC to (openlibrary)
		move.l	D0,ICOBase(A5)		;save base

		beq.b	CloseLibs		;branch if not open

*****************************************************************************

		bsr.b	Main

************************************************ close libraries ************

CloseLibs	movea.l	4.w,A6			;set EXEC to call a function

		move.l	ICOBase(A5),D1		;get base_ptr
		beq.b	CloseDOS		;branch when not open
		movea.l	D1,A1			;set base_ptr
		jsr	_LVOCloseLibrary(A6)	;use EXEC to (closelibrary)

CloseDOS	move.l	DOSBase(A5),D1		;get base_ptr
		beq.b	ExitDOS			;branch when not open
		movea.l	D1,A1			;set base_ptr
		jsr	_LVOCloseLibrary(A6)	;use EXEC to (closelibrary)

ExitDOS		move.l	RCode1(A5),D0		;set returncode
		movem.l	(SP)+,D1-D7/A0-A6	;restore registers
		rts				;go back (shell)

*****************************************************************************

DOSName		dc.b	"dos.library",0
ICOName		dc.b	"icon.library",0

*****************************************************************************

version	dc.b "$VER: SetDTool 1.1 10 (2.4.00) by joerg riemer mar'00",0

*****************************************************************************

		even

*****************************************************************************

Main		clr.l	RCode1(A5)		;clear returncode

		bsr	ReadArgs		;read user_arguments
		bsr	InitAPath		;prepare anchorpath
		bsr	MatchFirst		;search for filename

Loop		bsr	CheckEntryType		;check for dir/file
		bsr	MatchNext		;do matchnext first!!
		bsr	SetDTool		;then edit prematched file

		tst.b	EntryType(A5)		;found a info_file?
		beq.b	Next			;branch if not
		tst.b	Quiet(A5)		;print filename?
		bne.b	Next			;branch if not

		bsr.b	PrintFName		;print "path/file"
		bsr.b	PrintComment		;print "patched!"

Next		move.l	MatchReturn(A5),D1	;check returncode (matchnext)
		beq.b	Loop			;loop again when ok

		cmp.w	#232,D1			;check for 'no more entries'
		bne	Error5			;branch if not

Quit0		bsr	MatchEnd		;free memory (anchorpath)
		bsr	FreeArgs		;free memory (readargs)
		rts				;go back

************************************************ print path/filename ********

PrintFName	move.l	StrEnd(A5),A2		;restore filename
		move.b	#".",(A2)		;restore filename

		movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOOutput(A6)		;search output_handle

		move.l	D0,D1			;set output
		lea	PNameBuf(A5),A2		;get filename to print
		move.l	A2,D2			;prepare dos_fputs
		jsr	_LVOFPuts(A6)		;use dos to fputs

.Ready		rts				;go back

************************************************ print newline character ****

PrintNLine	movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOOutput(A6)		;search output_handle
		move.l	D0,D1			;set output
		moveq	#$A,D2			;set char. to print
		jsr	_LVOFPutC(A6)		;use dos to fputc
		rts				;go back

************************************************ print comment "patched!" ***

PrintComment	movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOOutput(A6)		;search output_handle
		move.l	D0,D1			;set output
		lea	Text(pc),A2		;get filename to print
		move.l	A2,D2			;prepare dos_fputs
		jsr	_LVOFPuts(A6)		;use dos to fputs
		rts				;go back

*****************************************************************************

Text		dc.b	" patched!",$A,0

************************************************ set default tool ***********

SetDTool	tst.b	EntryType(A5)		;found a info_file?
		beq	Ready			;branch if not

*********************** read [#?.info] file ****

		move.l	StrEnd(A5),A2		;remove suffix [.info]
		clr.b	(A2)			;remove suffix [.info]

		lea	PNameBuf(A5),A0		;set filename
		move.l	ICOBase(A5),A6		;set library_base
		jsr	_LVOGetDiskObject(A6)	;use icon to getdiskobject
		move.l	D0,DObject(A5)		;got it?
		beq	Error2			;branch if not

*********************** check for project.info *

		move.l	D0,A0			;set diskobject
		cmp.b	#WBPROJECT,do_Type(A0)	;check for project_icon
		bne	Error4			;branch if not

*********************** set new defaulttool ****

		move.l	do_DefaultTool(A0),OldDefTool(A5) ;save old defauft
		move.l	NewDefTool(A5),do_DefaultTool(A0) ;set new default

		movea.l	A0,A1			;set diskobject
		lea	PNameBuf(A5),A0		;set name used to put
		jsr	_LVOPutDiskObject(A6)	;use icon to putdiskobject
		tst.l	D0			;save file failed?
		beq	Error3			;branch if so

		addq.l	#1,FCount(A5)		;inc. files proccessed

************************************************ free memory (disobject) ****

FreeDObject	move.l	DObject(A5),A0		;get diskobject
		move.l	OldDefTool(A5),do_DefaultTool(A0) ;restore deftool

		movea.l	ICOBase(A5),A6		;set icon_base
		jsr	_LVOFreeDiskObject(A6)	;use icon to freediskobject
Ready		rts				;go back

************************************************ matchnext pattern **********

MatchNext	lea	Anchorpath(A5),A1	;get anchorpath structure
		move.l	A1,D1			;prepare matchnext
		movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOMatchNext(A6)	;use dos to matchnext
		move.l	D0,MatchReturn(A5)	;save return_code
		rts				;go back

************************************************ free memory matchfirst/next

MatchEnd	lea	Anchorpath(A5),A1	;get anchorpath structure
		move.l	A1,D1			;prepare matchend
		movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOMatchEnd(A6)	;use dos to matchend
		rts				;go back

************************************************ test for #?.info file ******

CheckEntryType	lea	ap_Info(A5),A0		;get fileinfoblock
		tst.l	fib_DirEntryType(A0)	;type = file ?
		bpl.b	.Ready			;branch, type is directory

		lea	PNameOrg(A5),A0		;get filename
		lea	PNameBuf(A5),A1		;set buffer
.CopyString	move.b	(A0)+,(A1)+		;copy string
		bne.b	.CopyString		;until end

		subq	#6,A1			;search for suffix '.info'
		cmp.b	#".",(A1)		;first test
		bne.b	.Ready			;branch if fail

		move.l	A1,StrEnd(A5)		;set string_end w/o suffix
		addq	#1,A1			;skip '.'
		move.b	(A1)+,D0		;second test, get char
		bset	#5,D0			;set lowercase
		rol.l	#8,D0			;make room for next char
		move.b	(A1)+,D0		;get char
		bset	#5,D0			;set lowercase
		rol.l	#8,D0			;make room for next char
		move.b	(A1)+,D0		;get char
		bset	#5,D0			;set lowercase
		rol.l	#8,D0			;make room for next char
		move.b	(A1)+,D0		;get char
		bset	#5,D0			;set lowercase
		cmp.l	#"info",D0		;suffix = '.info' ?

.Ready		seq	EntryType(A5)		;set if so
		rts				;go back

************************************************ matchfirst pattern *********

MatchFirst	move.l	Filename(A5),D1		;get user pattern/file
		lea	Anchorpath(A5),A2	;get anchorpath
		move.l	A2,D2			;used for matching
		movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOMatchFirst(A6)	;use dos to matchfirst
		move.l	D0,MatchReturn(A5)	;save returncode
		beq.b	.Ready			;branch if ok

		addq	#4,SP			;clear return_address
		pea	Next(pc)		;set new one

.Ready		rts				;go back

************************************************ prepare anchorpath *********

InitAPath	moveq	#00,D0			;prepare register
		bset	#SIGBREAKB_CTRL_C,D0	;do break on <CTRL><C> !!!
		move.l	D0,ap_BreakBits(A5)	;set bit
		move.w	#256,ap_Strlen(A5)	;set length of string_buffer
		rts				;go back

************************************************ read arguments *************

ReadArgs	lea	Template(pc),A1		;get template
		lea	Filename(A5),A2		;get arg_array
		move.l	A1,D1			;set arg_template
		move.l	A2,D2			;set array
		moveq	#0,D3			;set args (unused)
		movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOReadArgs(A6)	;use dos to readargs
		move.l	D0,RDArgs(A5)		;save returncode
		beq	Error1			;branch on wrong args
		rts				;else, go back

************************************************ free argument buffer *******

FreeArgs	move.l	RDArgs(A5),D1		;get arg_structure
		beq.b	.Ready			;branch if not set
		move.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOFreeArgs(A6)	;use dos to freeargs
.Ready		rts				;go back

************************************************ do a bit error handling ****

FreeDOPrint	bsr	FreeDObject		;free allocated diskobject

		tst.b	Quiet(A5)		;do all quiet ?
		bne.b	.Ready			;branch if so

		bsr	PrintFName		;else, print filename
		bsr	PrintNLine		;print newline

.Ready		rts				;go back

************************************************ error handling *************

Error5		move.l	D1,RCode2(A5)		;set errorcode
		pea	Quit0(pc)		;set return_address
		bra.b	Error			;print error

************************************************

Error4		bsr	FreeDOPrint		;free allocated diskobject
		move.l	#212,RCode2(A5)		;error 'not of required type'
		addq	#4,SP			;clear return_address
		pea	Next(pc)		;set new one
		bra.b	Error			;print error

************************************************

Error3		bsr	FreeDOPrint		;free allocated diskobject

************************************************

Error2		addq	#4,SP			;clear return_address
		pea	Quit0(pc)		;set new one
		bra.b	Error0			;print error

************************************************

Error1		addq	#4,SP			;clear return_address

************************************************

Error0		movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOIoErr(A6)		;use dos to get ioerr
		move.l	D0,RCode2(A5)		;save as pr_result2

************************************************

Error		move.l	RCode2(A5),D1		;get errorcode
		movea.l	DOSBase(A5),A6		;set dos_base
		jsr	_LVOSetIoErr(A6)	;use dos to set pr_result2

		tst.b	Quiet(A5)		;do all quiet ?
		bne.b	.SetRCode1		;branch if so

		move.l	RCode2(A5),D1		;get errorcode
		lea	Header(pc),A2		;get head_text 'ERROR:'
		move.l	A2,D2			;set header
		jsr	_LVOPrintFault(A6)	;use dos to printfault

.SetRCode1	moveq	#5,D0			;set returncode rc=warn
		move.l	D0,RCode1(A5)		;save
		rts				;go back

*****************************************************************************

Template	dc.b	"FILE/A,TOOL/K,QUIET/S",0
Header		dc.b	"ERROR",0

*****************************************************************************

		if	BSS_Section

		SECTION	SetDTool_bss,BSS

		elseif

		cnop	0,4

		endc

*****************************************************************************

DataBase	ds.b	BSS_Size

 end of source **************************************************************
