	.even
/"case" handles printing and substituting of one or both case fields
case:
	mov	r1,-(sp)		/save regs
	mov	r2,-(sp)
	mov	(r5)+,r2
	bne	1f			/branch if only 1 case
	tst	token2			/"/" present?
	beq	0f
	jsr	r5,mesg
	<3ILLEGAL USE OF "CS"4\n\0>
	.even
	br	8f
0:
	jsr	r5,wcase
	j.case1
	mov	$1,r0
	sys	write; nl; 1
	jsr	r5,wcase
	j.case2
	br	9f
1:					/only 1 case involved
	tst	token2			/"/" present?
	bne	1f			/branch if yes
	mov	r2,0f
	jsr	r5,wcase
0:	..
	br	9f
1:					/modification being done
	tst	token3			/"*" present?
	beq	1f			/branch if no
	mov	r2,r0
	add	$10.,r0
0:
	clr	(r2)+			/clear appropriate case field
	cmp	r2,r0
	blo	0b
	movb	$1,cflag		/indicate core changed
	br	9f
1:
	mov	token2,r0
	inc	r0
	add	$10.,r0
	cmp	lastchar,r0
	blos	0f
	jsr	r5,mesg
	<3BAD CASE4\n\0>
	.even
	br	8f
0:
	mov	token4,r1
0:
	clrb	(r1)+			/null-out rest of 10 chars
	cmp	r1,r0
	blo	0b
	inc	token2
	mov	token2,0f		/set ptr to new case
	jsr	r5,rcase
0:	..
	tstb	eflag
	beq	7f
	jsr	r5,mesg
	<3BAD CASE4\n\0>
	.even
8:
	movb	$1,eflag
	br	9f
7:
	mov	token2,r0
	mov	r0,r1
	add	$10.,r1
7:
	movb	(r0)+,(r2)+		/change case field
	cmp	r0,r1
	blo	7b
	movb	$1,cflag		/indicate core changed
9:
	mov	(sp)+,r2		/restore regs
	mov	(sp)+,r1
	rts	r5			/return


/"rcase" reads case table to validate a case
/	a case may be 10 chars in length
/inputs:
/	(r5) -> addr of chars to be matched
/outputs:
/	eflag set if error
/
rcase:
	mov	r1,-(sp)		/save regs
	mov	r2,-(sp)
	mov	r3,-(sp)
	mov	(r5)+,r3		/retrieve argument
	clrb	eflag
	sys	open; f.cases; 0
	mov	r0,temp
0:
	mov	$temp+2,r0
	mov	r0,r2
1:
	clr	(r0)+			/clear field first
	cmp	r0,$temp+12.
	blo	1b
1:
	mov	r2,2f
	mov	temp,r0
	sys	read; 2:..; 1		/read one case char
	tst	r0			/EOF?
	beq	7f
	cmpb	(r2),$',		/at end particular case?
	beq	2f
	cmpb	(r2),$'\n
	beq	2f
	inc	r2
	cmp	r2,$temp+12.		/case over 10 chars?
	blo	1b
	jsr	r5,mesg
	<3CASE FILE BAD AROUND "\0>
	.even
	mov	$1,r0
	sys	write; temp+2; 10.
	jsr	r5,mesg
	<"4\n\0>
	.even
	jsr	r5,ioerror
	<51>				/BAD CASE FILE ENTRY
2:
	clrb	(r2)			/clear "," or "\n" from field
	mov	$temp+2,r0
	mov	r3,r1
2:
	cmpb	(r0)+,(r1)+
	bne	0b
	cmp	r0,$temp+12.
	blo	2b
	br	9f			/successful match
7:
	mov	$temp+2,r0
	mov	r3,r1
7:
	cmpb	(r0)+,(r1)+
	bne	7f
	cmp	r0,$temp+12.
	blo	7b
	br	9f			/successful match
7:
	movb	$1,eflag		/set error indicator
9:
	mov	temp,r0
	sys	close			/close "f.cases" file
	mov	(sp)+,r3		/restore regs
	mov	(sp)+,r2
	mov	(sp)+,r1
	rts	r5			/return


/"wcase" writes a case
/input:
/	(r5) -> addr of case to be written
/
wcase:
	mov	(r5)+,0f		/retrieve arg
	mov	$1,r0
	sys	write; 0:..; 10.	/write up to 10 chars of case
	rts	r5
