NEW
  AUTO 3,1
*--------------------------------------
ENVX.hEnv		.BS 1
*--------------------------------------
ENVX.PutEnv.9	lda #E.SYN
				sec
				rts
*--------------------------------------
ENVX.PutEnv		tax 					save hEnv

				ldy #$ff

.1				iny
				jsr SHARED.TXTPTR.GetY	 NAME=
				beq ENVX.PutEnv.9

				cmp #'='
				bne .1

				tya
				sec					skip =
				adc TXTPTR
				sta FORPNT
				
				lda #0
				adc TXTPTR+1
				sta FORPNT+1
				
				txa						get back hEnv
*--------------------------------------
ENVX.SetEnv		jsr ENVX.UnsetEnv
				
.1				jsr ENVX.InitEnvP3

				lda #1
				sta ZPPtr4
				stz ZPPtr4+1			ZPPtr4 = ENVSIZE = 1

.10				lda (ZPPtr3)			End of ENV
				beq .15

				jsr ENVX.GetP3LenY
				jsr ENVX.NextEnvP3Y
				jsr ENVX.NextEnvP4Y
				bra .10

.15				ldy #$ff

.16				iny
				jsr SHARED.TXTPTR.GetY
				beq .17

				jsr SHARED.IsIDValid
				bcc .16

.17				jsr ENVX.NextEnvP4Y		ZPPtr4 = ENVSIZE + VAR

				ldy #$ff

.19				iny
				jsr SHARED.FORPNT.getY
				bne .19

				jsr ENVX.NextEnvP4Y		ZPPtr4 = ENVSIZE + VALUE
				lda ZPPtr4
				beq .20

				inc ZPPtr4+1

.20				lda ENVX.hEnv
				jsr MEM.GetMemByID
				
				lda ZPPtr4+1
				ldy #S.MEM.LEN+1
				cmp (ZPMemMgrSPtr),y
				bcs .22					env is large enough

				sta MEM.NewSize+1
				stz MEM.NewSize
				jsr K.realloc.I
				bcs .9

				stx ENVX.hEnv
				txa
				sta CLRWRITEAUX
				ldy #S.PS.hENV
				lda (pPs),y
				sta SETWRITEAUX
				bra .1				Start over

.22				ldy #$ff
			
.23				iny
				jsr SHARED.TXTPTR.GetY
				sta (ZPPtr3),y
				beq .24
				jsr SHARED.IsIDValid
				bcc .23

.24				lda #'='
				sta (ZPPtr3),y
				
				jsr ENVX.NextEnvP3Y
				ldy #$ff

.25				iny
				jsr SHARED.FORPNT.getY
				sta (ZPPtr3),y
				bne .25

				iny
				sta (ZPPtr3),y			don't forget array ending 0
				clc

.9				rts
*--------------------------------------
ENVX.GetEnv		ldy TXTPTR+1
				bne	.10

				tax						hMem for CopyToMain
				
				jsr MEM.GetMemByID
				
				ldy #S.MEM.LEN
				lda (ZPMemMgrSPtr),y
				pha
				iny
				lda (ZPMemMgrSPtr),y
				ply						YA = Len

				jmp MEM.CopyToMain

.10				sta ENVX.hEnv

				jsr ENVX.FindVar
				bcs .9

				jsr ENVX.NextEnvP3Y		Skip NAME=

				lda FORPNT+1
				bne .3

				ldy #$ff
				
.1				iny
				lda (ZPPtr3),y
				bne .1
				
				iny
				bne .2
				inc

.2				ldx #SYS.GetMem
				jsr K.SYSCALL2.MAIN		!!!	Get a buffer In MAIN Mem !!!
				
				bcs .99
				
				>STYA FORPNT

.3				ldy #$ff

.4				iny
				lda (ZPPtr3),y
				jsr SHARED.FORPNT.PutY
				bne .4
				
				>LDYA FORPNT

				clc
				rts
				
.9				>LDYAI 0

*				sec
.99				rts
*--------------------------------------
ENVX.UnsetEnv	sta ENVX.hEnv

				jsr ENVX.FindVar
				bcs .8					not found, quit

				jsr ENVX.DelVarP3		Ptr3 -> NAME=VALUE
.8				clc
				
				rts
*--------------------------------------
* ENV.FindVar
*  In:
*	 TXTPTR -> NAME
*  Out:
*   CC: Found
*    Y = NAME len
*	 TXTPTR -> NAME
*	 ZPPtr3 -> NAME=VALUE
*	CS: Not Found
*	 TXTPTR -> NAME
*	 ZPPtr3 -> PTR to Ending 0
*--------------------------------------
ENVX.FindVar	jsr ENVX.InitEnvP3		Store ENV

.1				lda (ZPPtr3)
				beq .9					end of ENV

				ldy #$ff

.2				iny
				jsr SHARED.TXTPTR.GetY
				beq .3

				jsr SHARED.IsIDValid
				bcs .3

				cmp	(ZPPtr3),y
				beq .2

				bra .4

.3				lda (ZPPtr3),y
				cmp #'='
				bne .4

				clc
				rts

.4				jsr ENVX.NextEnvP3		Skip NAME=VALUE
				bra .1

.9				sec
				rts	
*--------------------------------------
* ENV.DelVarP3
*  In:
*	ZPPtr3 -> ENV.NAME to Discard
*--------------------------------------
ENVX.DelVarP3	>LDYA ZPPtr3			save actual Ptr
				>STYA ZPPtr4
				jsr ENVX.NextEnvP3		Skip NAME=VALUE

.1				ldy #$ff

.2				iny
				lda (ZPPtr3),y			Move back CSTRs...
				sta (ZPPtr4),y
				bne .2

				tya
				
				beq .8					we moved back ending \0

				jsr ENVX.NextEnvP3Y
				jsr ENVX.NextEnvP4Y
				bra .1
				
.8				rts
*--------------------------------------
ENVX.InitEnvP3	lda ENVX.hEnv
				jsr K.GetMemPtr
				>STYA ZPPtr3			Store ENV

				rts
*--------------------------------------
ENVX.NextEnvP3	jsr ENVX.GetP3LenY
*--------------------------------------
ENVX.NextEnvP3Y	tya
ENVX.NextEnvP3A	sec
				adc ZPPtr3
				sta ZPPtr3
				bcc .8
				inc ZPPtr3+1
.8				rts
*--------------------------------------
ENVX.GetP3LenY	ldy #$ff
.1				iny
				lda (ZPPtr3),y
				bne .1
				rts
*--------------------------------------			
ENVX.NextEnvP4Y	tya
				sec
				adc ZPPtr4
				sta ZPPtr4
				bcc .8
				inc ZPPtr4+1
.8				rts
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.ENVX
LOAD USR/SRC/SYS/KERNEL.S
ASM
