NEW
  AUTO 3,1
*--------------------------------------
ENVX.hEnv		.BS 1
ENVX.hSID		.BS 1
*--------------------------------------
ENVX.PutEnv.9	lda #E.SYN
				sec
				rts
*--------------------------------------
ENVX.PutEnv		sta ENVX.hEnv
				sty	ENVX.hSID

				ldy #$ff

.1				iny
				jsr SHARED.TXTPTRgY	 NAME=
				beq ENVX.PutEnv.9

				cmp #'='
				bne .1

				tya
				sec					skip =
				adc TXTPTR
				sta FORPNT

				lda #0
				adc TXTPTR+1
				sta FORPNT+1

				jsr ENVX.UnsetEnv.I
				bra ENVX.SetEnv.I
*--------------------------------------
ENVX.SetEnv		jsr ENVX.UnsetEnv
ENVX.SetEnv.I
.1				jsr ENVX.InitEnvP3

				lda #1					\0
				sta ZPPtr4
				stz ZPPtr4+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.TXTPTRgY
				beq .17

				jsr SHARED.IsIDValid
				bcc .16

.17				jsr ENVX.NextEnvP4Y		ZPPtr4 = ENVSIZE + VAR

				ldy #$ff

.19				iny
				jsr SHARED.FORPNTgY
				bne .19

				jsr ENVX.NextEnvP4Y		ZPPtr4 = ENVSIZE + VALUE

*				lda ENVX.hEnv			done by ENVX.InitEnvP3
*				jsr MEM.GetMemByID

				ldy #S.MEM.LEN
				lda (ZPMemMgrSPtr),y
				cmp ZPPtr4
				iny
				lda (ZPMemMgrSPtr),y
				sbc ZPPtr4+1
				bcs .22					env is large enough

				ldy #S.MEM.PTR
				lda (ZPMemMgrSPtr),y
				sta A1L

				iny
				lda (ZPMemMgrSPtr),y
				sta A1L+1

				iny						#S.MEM.LEN
				lda (ZPMemMgrSPtr),y
				eor #$ff
				sta A4L

				iny
				lda (ZPMemMgrSPtr),y
				eor #$ff
				sta A4L+1

				>LDYA ZPPtr4
				jsr MEM.DupA1LnA4L
				bcs .9

				pha
				lda ENVX.hEnv
				jsr K.FreeMem
				pla
				sta ENVX.hEnv

				sta IO.CLRWRITEAUX
				ldy #S.PS.hENV
				sta (pPS),y
				sta IO.SETWRITEAUX
				bra .1				Start over

.22				ldy #$ff

.23				iny
				jsr SHARED.TXTPTRgY
				sta (ZPPtr3),y
				beq .24

				jsr SHARED.IsIDValid
				bcc .23

.24				lda #'='
				sta (ZPPtr3),y

				jsr ENVX.NextEnvP3Y

				ldy #$ff

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

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

.9				rts
*--------------------------------------
ENVX.GetEnv		sta ENVX.hEnv
				sty	ENVX.hSID

				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				jsr ENVX.FindEnvVar
				bcc .11

				jsr ENVX.FindSIDVar
				bcs .9

				>STYA ZPPtr3
				bra .12

.11				jsr ENVX.NextEnvP3Y		Skip NAME=

.12				lda FORPNT+1
				bne .3

				ldy #$ff

.1				iny
				lda (ZPPtr3),y
				bne .1

				iny
				bne .2
				inc

.2				ldx #_GetMem
				jsr JMP.M				!!!	Get a buffer In MAIN Mem !!!

				bcs .99

				>STYA FORPNT

.3				ldy #$ff

.4				iny
				lda (ZPPtr3),y
				jsr SHARED.FORPNTpY
				bne .4

				>LDYA FORPNT

				clc
				rts

.9				lda #E.NOKEY

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

ENVX.UnsetEnv.I	jsr ENVX.FindEnvVar
				bcs .8					not found, quit

				jsr ENVX.DelVarP3		Ptr3 -> NAME=VALUE
.8				clc

				rts
*--------------------------------------
* ENV.FindEnvVar
*  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.FindEnvVar	jsr ENVX.InitEnvP3		Store ENV

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

				ldy #$ff

.2				iny
				jsr SHARED.TXTPTRgY
				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
*--------------------------------------
ENVX.FindSIDVar	>LDYAI ENVX.SIDVars
				>STYA ZPPtr3
				ldx #0

.1				lda (ZPPtr3)
				beq .9

				ldy #$ff

.2				iny
				jsr SHARED.TXTPTRgY
				beq .3

				jsr SHARED.IsIDValid
				bcs .3

				cmp (ZPPtr3),y
				beq .2

				bra .4

.3				lda (ZPPtr3),y
				bne .4

				jmp (ENVX.SIDVarsJmp,x)

.4				inx
				inx
				jsr ENVX.NextEnvP3
				bra .1

.9				sec
				rts
*--------------------------------------
ENVX.SIDVars	.AZ "LOGNAME"
				.AZ "GECOS"
				.AZ "HOME"
				.AZ "SHELL"
				.AZ "UID"
				.AZ "GID"
				.DA #0
*--------------------------------------
ENVX.SIDVarsJmp	.DA ENVX.SIDVarsSTR
				.DA ENVX.SIDVarsSTR
				.DA ENVX.SIDVarsSTR
				.DA ENVX.SIDVarsSTR
				.DA ENVX.SIDVarsUID
				.DA ENVX.SIDVarsGID
*--------------------------------------
ENVX.SIDVarsSTR	jsr ENVX.InitSIDP3

				lda ZPPtr3
				clc
				adc #S.SESSION.NAME
				sta ZPPtr3
				lda ZPPtr3+1
				adc /S.SESSION.NAME
				sta ZPPtr3+1

.1				dex
				dex
				bmi .8

				ldy #$ff

.2				iny
				lda (ZPPtr3),y
				bne .2

				jsr ENVX.NextEnvP3Y
				bra .1

.8				>LDYA ZPPtr3

				clc
				rts
*--------------------------------------
ENVX.SIDVarsUID	ldx #S.SESSION.UID
				.HS 2C
ENVX.SIDVarsGID	ldx #S.SESSION.GID

				jsr ENVX.InitSIDP3
				txa
				tay
				lda (ZPPtr3),y

				jsr MATH.A2STR10NP

				>LDYAI FOUTBuf

				clc
				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
				bra ENVX.InitP3
*--------------------------------------
ENVX.InitSIDP3	lda ENVX.hSID

ENVX.InitP3		jsr K.GetMemPtr
				>STYA ZPPtr3
				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
