	INCLUDE "exec/types.i"
	INCLUDE "dos/dostags.i"
	INCLUDE "utility/tagitem.i"
	INCLUDE "lvo/utility_lib.i"

;//=================================================

	XDEF	PATCHSTART
PATCHSTART:

;//=================================================


	XDEF New_CreateProc

New_CreateProc:
	move.l	d4,-(sp)
	
	cmp.l		_MinStack(pc),d4
	bge.s		.MinStackOK
	move.l	_MinStack(pc),d4
	
.MinStackOK:
	add.l		_StackAdd(pc),d4

	pea		.raus(pc)
	move.l	_Old_CreateProc(pc),-(sp)
	rts
	
.raus:
	move.l	(sp)+,d4
	rts


	XDEF New_CreateNewProc

DAT1_proctags = -4
DAT1_newti = -20
DAT1_clonedtaglist = -24
DAT1_len = -24

New_CreateNewProc:	;// d1 = proctags
	link		a5,#DAT1_len
	move.l	d2,-(sp)

	move.l	d1,DAT1_proctags(a5)					;// save proctags
	clr.l		DAT1_clonedtaglist(a5)

	move.l	#NP_StackSize,DAT1_newti(a5)		;// NP_StackSize
	move.l	_MinStack(pc),d0
	add.l		_StackAdd(pc),d0
	move.l	d0,DAT1_newti+4(a5)					;// MinStack + StackAdd
	
	move.l	#TAG_MORE,DAT1_newti+8(a5)			;// TAG_MORE
	move.l	d1,DAT1_newti+12(a5)					;// proctags

	tst.l		d1											;// are there any tags?
	beq.s		.NoTags

	move.l	a6,-(sp)
	move.l	_UtilityBase(pc),a6	
	move.l	#NP_StackSize,d0
	move.l	d1,a0
	jsr		_LVOFindTagItem(a6)
	move.l	(sp)+,a6
	
	tst.l		d0
	beq.s		.NotFound

	move.l	d0,a0
	move.l	ti_Data(a0),d2
	cmp.l		_MinStack(pc),d2			;// ti_Data >= MinStack ?
	bge.s		.MinOK
	
	move.l	_MinStack(pc),d2

.MinOK:
	add.l		_StackAdd(pc),d2

;// we have to change NP_StackSize and will therefore
;// try to clone the taglist or if ...

	movem.l	a0/a6,-(sp)
	move.l	_UtilityBase(pc),a6
	move.l	DAT1_proctags(a5),a0
	jsr		_LVOCloneTagItems(a6)
	movem.l	(sp)+,a0/a6
	tst.l		d0
	bne.s		.CloningOK
	
;// ... cloning failed :( ==> patch the original taglist

	move.l	d2,ti_Data(a0)
	move.l	DAT1_proctags(a5),d1
	bra.s		.GoOld

.CloningOK:
	move.l	d0,DAT1_clonedtaglist(a5)
	move.l	d0,a0

	move.l	a6,-(sp)
	move.l	_UtilityBase(pc),a6
	move.l	#NP_StackSize,d0
	jsr		_LVOFindTagItem(a6)
	move.l	(sp)+,a6
	
;// will not fail this time!?

	move.l	d0,a0
	move.l	d2,ti_Data(a0)
	
	move.l	DAT1_clonedtaglist(a5),d1
	bra.s		.GoOld

;// No Tags supplied ?
	
.NoTags:
	move.l	#TAG_DONE,DAT1_newti+8(a5)		;// terminate taglist

.NotFound:
	lea		DAT1_newti(a5),a0
	move.l	a0,d1										;// pass newti to CreateNewProc

.GoOld:
	pea		.funcexit(pc)
	move.l	_Old_CreateNewProc(pc),-(sp)
	rts

.funcexit:
	move.l	DAT1_clonedtaglist(a5),d1
	beq.s		.noclonedtaglist

;// free cloned taglist

	move.l	d1,a0
	movem.l	d0/a6,-(sp)
	move.l	_UtilityBase(pc),a6
	jsr		_LVOFreeTagItems(a6)
	movem.l	(sp)+,d0/a6
	
.noclonedtaglist:
	move.l	(sp)+,d2									;// restore d2
	unlk		a5
	rts


	XDEF New_SystemTagList

DAT2_proctags = -4
DAT2_command = -8
DAT2_newti = -24
DAT2_clonedtaglist = -28
DAT2_len = -28

New_SystemTagList:	;// d2 = proctags!!!! (and not d1 as in CreatenewProc!!!

	link		a5,#DAT2_len
	move.l	d2,-(sp)

	move.l	d1,DAT2_command(a5)					;// save command
	move.l	d2,DAT2_proctags(a5)					;// save proctags

;// **HELP**
;// NP_StackSize commented out because of the C:ICONX and SHELL problem
;// when the icon tooltype "STACK" is used (see "H E L P   M E ! !")

;'	move.l	#NP_StackSize,DAT2_newti(a5)		;// NP_StackSize
	move.l	#TAG_IGNORE,DAT2_newti(a5)			;// NP_StackSize
	clr.l		DAT2_clonedtaglist(a5)

	move.l	_MinStack(pc),d0
	add.l		_StackAdd(pc),d0
	move.l	d0,DAT2_newti+4(a5)					;// MinStack + StackAdd
	
	move.l	#TAG_MORE,DAT2_newti+8(a5)			;// TAG_MORE
	move.l	d2,DAT2_newti+12(a5)					;// proctags

	tst.l		d2											;// are there any tags?
	beq.s		.NoTags

	move.l	a6,-(sp)
	move.l	_UtilityBase(pc),a6	
	move.l	#NP_StackSize,d0
	move.l	d2,a0
	jsr		_LVOFindTagItem(a6)
	move.l	(sp)+,a6
	
	tst.l		d0
	beq.s		.NotFound

	move.l	d0,a0
	move.l	ti_Data(a0),d2
	cmp.l		_MinStack(pc),d2			;// ti_Data>MinData?
	bge.s		.MinOK
	move.l	_MinStack(pc),d2

.MinOK
	add.l		_StackAdd(pc),d2
	
;// we have to change NP_StackSize and will therefore
;// try to clone the taglist, or if ...

	movem.l	a0/a6,-(sp)
	move.l	_UtilityBase(pc),a6
	move.l	DAT2_proctags(a5),a0
	jsr		_LVOCloneTagItems(a6)
	movem.l	(sp)+,a0/a6
	tst.l		d0
	bne.s		.CloningOK
	
;// ... cloning failed :( ==> patch original taglist

	move.l	d2,ti_Data(a0)
	move.l	DAT2_proctags(a5),d2
	bra.s		.GoOld

.CloningOK:
	move.l	d0,DAT2_clonedtaglist(a5)
	move.l	d0,a0

	move.l	a6,-(sp)
	move.l	_UtilityBase(pc),a6
	move.l	#NP_StackSize,d0
	jsr		_LVOFindTagItem(a6)
	move.l	(sp)+,a6
	
;// will not fail this time!?

	move.l	d0,a0
	move.l	d2,ti_Data(a0)
	
	move.l	DAT2_clonedtaglist(a5),d2
	bra.s		.GoOld

;// No Tags supplied ?
	
.NoTags:
	move.l	#TAG_DONE,DAT2_newti+8(a5)			;// terminate taglist

.NotFound:
	lea		DAT2_newti(a5),a0
	move.l	a0,d2										;// pass newti to CreateNewProc

.GoOld:
	move.l	DAT2_command(a5),d1					;// restore command

	pea		.funcexit(pc)
	move.l	_Old_SystemTagList(pc),-(sp)
	rts

.funcexit:
	move.l	DAT2_clonedtaglist(a5),d1
	beq.s		.noclonedtaglist

;// free cloned taglist

	move.l	d1,a0
	movem.l	d0/a6,-(sp)
	move.l	_UtilityBase(pc),a6
	jsr		_LVOFreeTagItems(a6)
	movem.l	(sp)+,d0/a6
	
.noclonedtaglist:
	move.l	(sp)+,d2									;// restore d2
	unlk		a5
	rts


	
	XDEF New_RunCommand

New_RunCommand:
	move.l	d2,-(sp)

	cmp.l		_MinStack(pc),d2
	bge.s		.MinStackOK
	move.l	_MinStack(pc),d2
	
.MinStackOK:
	add.l		_StackAdd(pc),d2

	pea		.raus(pc)
	move.l	_Old_RunCommand(pc),-(sp)
	rts

.raus:
	move.l	(sp)+,d2
	rts




	XDEF	_Old_CreateProc
	XDEF	_Old_CreateNewProc
	XDEF	_Old_SystemTagList
	XDEF	_Old_RunCommand
	XDEF	_MinStack
	XDEF	_StackAdd
	XDEF	_UtilityBase
	XDEF	_CDOSBase
	
;// no ds.l here !!!

_Old_CreateProc:		dc.l 0

_Old_CreateNewProc:	dc.l 0

_Old_SystemTagList:	dc.l 0

_Old_RunCommand:		dc.l 0

_MinStack:				dc.l 4000

_StackAdd:				dc.l 0
	
_UtilityBase:			dc.l 0

_CDOSBase:				dc.l 0


	dc.b 'GS97'

;//=================================================

	XDEF	PATCHEND
PATCHEND:

;//=================================================

	END

