		NOLIST

*AUTO:		genam	<file>.asm

		opt	o+,ow-,ow6+,NODEBUG,NOLINE


;* $Revision Header built automatically *************** (do not edit) ************
;*
;* © Copyright by GuntherSoft
;*
;* File             : SnakeSYS:ASMPrgs/Utils/BreakName.Asm
;* Created on       : Sunday, 20.03.94 14:39:32
;* Created by       : Kai Iske
;* Current revision : V37.2
;*
;*
;* Purpose
;* -------
;*   - Break a cli process by name. Enhancement to C:Break
;*     Only Task/Process name needed; no pathnames required
;*     BreakName is PURE
;*
;* Revision V37.2
;* --------------
;* created on Tuesday, 24.05.94 17:17:05  by  Kai Iske.   LogMessage :
;*  -*-  changed on Tuesday, 24.05.94 17:18:47  by  Kai Iske.   LogMessage :
;*   - Permit was only called when the process was not found
;*     (Reported by: David Jameson)
;*  -*-  created on Tuesday, 24.05.94 17:17:05  by  Kai Iske.   LogMessage :
;*   - Didn`t check for length of name to copy
;*     (Reported by: David Jameson)
;*
;* Revision V37.1
;* --------------
;* created on Monday, 18.04.94 19:57:29  by  Kai Iske.   LogMessage :
;*   - FindTask was called with A0=NULL instead of A1=NULL ;)
;*     Whoops, better check registers next time
;*   - ReturnCode wasn`t set correctly
;*
;* Revision V37.0
;* --------------
;* created on Sunday, 20.03.94 14:39:32  by  Kai Iske.   LogMessage :
;*     --- Initial release ---
;*
;*********************************************************************************
REVISION  MACRO
          dc.b "37.2"
          ENDM
REVDATE   MACRO
          dc.b "24.05.94"
          ENDM
REVTIME   MACRO
          dc.b "17:18:47"
          ENDM
AUTHOR    MACRO
          dc.b "Kai Iske"
          ENDM
VERNUM    EQU  37
REVNUM    EQU  2



		INCLUDE	Exec/Types.i
		INCLUDE	Exec/Memory.i
		INCLUDE	Exec/Exec_Lib.i
		INCLUDE	Dos/DosExtens.i
		INCLUDE	Dos/Dos_Lib.i
		INCLUDE	Utility/Utility_Lib.i
		INCLUDE	Misc/MyMacros.i



**********************************************************************
*                              Let`s go                              *
**********************************************************************
		CARGS	#-(2048+16),_DOSBase.L,_UtilityBase.L,Vecs.L,Args.L,CopiedName.B

START		movem.l	d1-d7/a0-a6,-(sp)

		link	a4,#-(2048+16)				; Get space for vars

		move.l	#((2048+16)>>1),d0			; Clear local areas
		moveq	#0,d1
		move.l	a4,a0
.Clr		move.w	d1,-(a0)
		dbf	d0,.Clr

		sub.l	a1,a1					; Don`t launch from WB
		CALLSYS	FindTask
		move.l	d0,a3
		tst.l	pr_CLI(a3)
		bne.s	.FromCLI
		DOLIB	Forbid

		lea	pr_MsgPort(a3),a0
		DOLIB	WaitPort

		lea	pr_MsgPort(a3),a0
		DOLIB	GetMsg

		move.l	d0,a1
		DOLIB	ReplyMsg

		unlk	a4
		movem.l	(sp)+,d1-d7/a0-a6
		moveq	#0,d0
		rts

.FromCLI	lea	DosName(pc),a1				; Open some libraries
		move.l	#0,d0
		CALLSYS	OpenLibrary
		tst.l	d0
		beq	.EndPrg
		move.l	d0,_DOSBase(a4)

		cmp.w	#37,LIB_VERSION(a6)			; Check system we`re running on
		bge.s	.OSOk
		RELLIB	Output,_DOSBase				; Issue error
		move.l	d0,d1
		lea	OSErr(pc),a0
		move.l	a0,d2
		move.l	#61,d3
		DOLIB	Write 
		move.l	_DOSBase(a4),a1
		CALLSYS	CloseLibrary
		unlk	a4
		movem.l	(sp)+,d1-d7/a0-a6
		moveq	#20,d0
		rts


.OSOk		lea	UtilName(pc),a1
		move.l	#37,d0
		CALLSYS	OpenLibrary
		tst.l	d0
		beq	.EndPrg
		move.l	d0,_UtilityBase(a4)

		move.l	#((F_ARG+1)<<2),d0			; Alloc buffer for Arguments
		move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
		CALLSYS	AllocVec
		tst.l	d0
		beq	.EndPrg
		bne.s	.GotVecs
		move.l	#ERROR_NO_FREE_STORE,d0
		bra	.Error2
.GotVecs	move.l	d0,Vecs(a4)

		lea	Template(pc),a0				; Parse command line
		move.l	a0,d1
		move.l	d0,d2
		moveq	#0,d3
		RELLIB	ReadArgs,_DOSBase
		tst.l	d0
		beq	.Error
		move.l	d0,Args(a4)

		move.l	Vecs(a4),a0				; All flag set ???
		tst.l	(ALL_ARG*4)(a0)
		beq.s	.StartLoop				; Set all other flags regardless
		moveq	#1,d0					; of what was specified
		move.l	d0,(C_ARG*4)(a0)
		move.l	d0,(D_ARG*4)(a0)
		move.l	d0,(E_ARG*4)(a0)
		move.l	d0,(F_ARG*4)(a0)

.StartLoop	CALLSYS	Forbid					; Please no other tasks at the moment

		RELLIB	MaxCli,_DOSBase				; Get number of CLI processes
		move.l	d0,d7

.LoopProc	move.l	d7,d1					; Get Process for this CLI number
		RELLIB	FindCliProc,_DOSBase
		tst.l	d0
		beq	.NoProc
		move.l	d0,a3					; Copy address of process

		move.l	pr_CLI(a3),d0				; Get pointer to CLI structure
		lsl.l	#2,d0
		move.l	d0,d1
		beq	.NoProc
		move.l	d0,a1

		move.l	cli_CommandName(a1),d0			; Get it`s commandname
		lsl.l	#2,d0
		move.l	d0,d1
		beq	.NoProc
		move.l	d0,a1

		moveq	#0,d0
		move.b	(a1)+,d0				; Get length of name of process to check with
		beq	.NoProc
		subq.w	#1,d0
		lea	CopiedName(a4),a0			; Get Address of Name buffer

.CopyName	move.b	(a1)+,(a0)+				; Copy Name
		dbf	d0,.CopyName
		move.b	#0,(a0)

		lea	CopiedName(a4),a1			; Check name (absolute)
		move.l	Vecs(a4),a0				; Get name of process to break
		move.l	(PROC_ARG*4)(a0),a0
		RELLIB	Stricmp,_UtilityBase			; Check names
		tst.l	d0
		beq.s	.GotProc

		lea	CopiedName(a4),a0			; Check name (only filepart)
		move.l	a0,d1
		RELLIB	FilePart,_DOSBase
		move.l	d0,d1					; Store pointer of filepart
		beq	.NoProc
		move.l	d0,a1

		move.l	Vecs(a4),a0				; Get name of process to break
		move.l	(PROC_ARG*4)(a0),a0

		RELLIB	Stricmp,_UtilityBase			; Check names
		tst.l	d0
		bne.s	.NoProc

.GotProc	move.l	Vecs(a4),a5				; Which signals to send ???

		tst.l	(C_ARG*4)(a5)				; Send C ???
		bne.s	.DoC
		tst.l	(D_ARG*4)(a5)				; Send D ???
		bne.s	.NoC
		tst.l	(E_ARG*4)(a5)				; Send E ???
		bne.s	.NoC
		tst.l	(F_ARG*4)(a5)				; Send F ???
		bne.s	.NoC

.DoC		move.l	a3,a1
		move.l	#SIGBREAKF_CTRL_C,d0
		CALLSYS	Signal

.NoC		tst.l	(D_ARG*4)(a5)
		beq.s	.NoD
		move.l	a3,a1
		move.l	#SIGBREAKF_CTRL_D,d0
		CALLSYS	Signal

.NoD		tst.l	(E_ARG*4)(a5)
		beq.s	.NoE
		move.l	a3,a1
		move.l	#SIGBREAKF_CTRL_E,d0
		CALLSYS	Signal

.NoE		tst.l	(F_ARG*4)(a5)
		beq.s	.Done
		move.l	a3,a1
		move.l	#SIGBREAKF_CTRL_F,d0
		CALLSYS	Signal
		bra.s	.Done

.NoProc		subq.l	#1,d7					; Loop
		bne	.LoopProc

.Done		CALLSYS	Permit
		moveq	#0,d7					; Set returncode
		bra.s	.EndPrg2

.Error		RELLIB	IoErr,_DOSBase				; Issue error
		move.l	d0,d1
.Error2		lea	FaultStr(pc),a0
		move.l	a0,d2
		RELLIB	PrintFault,_DOSBase

.EndPrg		moveq	#20,d7					; Set returncode

.EndPrg2	tst.l	Args(a4)				; Args there ???
		beq.s	.NoArgs
		move.l	Args(a4),d1
		RELLIB	FreeArgs,_DOSBase

.NoArgs		tst.l	Vecs(a4)				; Vecs there ???
		beq.s	.NoVecs
		move.l	Vecs(a4),a1
		CALLSYS	FreeVec

.NoVecs		tst.l	_UtilityBase(a4)			; Close libs
		beq.s	.NoUtil
		move.l	_UtilityBase(a4),a1
		CALLSYS	CloseLibrary

.NoUtil		tst.l	_DOSBase(a4)
		beq.s	.NoDOS
		move.l	_DOSBase(a4),a1
		CALLSYS	CloseLibrary

.NoDOS		unlk	a4
		move.l	d7,d0
		movem.l	(sp)+,d1-d7/a0-a6
		rts





**********************************************************************
*                          Our data section                          *
**********************************************************************
DosName		dc.b	"dos.library",0
UtilName	dc.b	"utility.library",0
FaultStr	dc.b	"BreakName ",0
OSErr		dc.b	"You must use KickStart 2.04 (37.175) or higher for BreakName",10,0
		EVEN



**********************************************************************
*                           Version string                           *
**********************************************************************
Ver		dc.b	0,"$VER: BreakName "
		REVISION
		dc.b	" ("
		REVDATE
		dc.b	")",0
		EVEN

**********************************************************************
*                         Template settings                          *
**********************************************************************
Template	dc.b	"PROCESS/A,ALL/S,C/S,D/S,E/S,F/S",0
PROC_ARG	EQU	0
ALL_ARG		EQU	1
C_ARG		EQU	2
D_ARG		EQU	3
E_ARG		EQU	4
F_ARG		EQU	5
		END
