$auto$=-2
.enabl lc
;15-aug-80 for Conroy C runtime
control.a=1		;control a ascii char
	  .psect edi09,rw
 mutask=1
	  .enabl lc
			.if df mutask
				.MACRO pure
                                .psect pur,ro
				.ENDM
				.MACRO impure
                                .psect ipur,rw
				.ENDM
			
			.iff
			
				.MACRO pure
				.ENDM
				.MACRO impure
				.ENDM
			
			.endc
	.globl tcom				;entry for t command
	.globl ycom				;entry for y command
	.globl morecore				;get more memory
	.globl macrop				;macro processor entry
	.globl nonull				;change nulls to 377's
	.globl ranges				;[a-z] type char class check
	.MACRO push this
	mov this,-(sp)
	.ENDM
	.MACRO pop that
	mov (sp)+,that
	.ENDM
	  .mcall extk$,dir$,callr
ex:	  extk$	2		;get	64 words
	  tab	=	11
ipeek:	  .word			;internal get	char peek word
iptr:	  .word			;buffer pointer for the get char
iadr:	  .word			;address of start of macro
char:	.word			;a single char to be putback
                        pure
lowasc:	.byte 2,3,4,5,6		;invisible char table
	.byte 10,13,14,16,17	;The code assumes these are fixed
	.byte 20,21,22,23,24,25,26,27,30 ;since it needs to know how
	.byte 31,32,34,35,36,37,177 ;when a char is one of these
elow=.-lowasc			;This it does by arithmetic below
	  .even
tcom:
	  call nonzero
	  call getc
	  bic #40,r0			;to upper	case
e91=.
	CMP R0,#nl		;Case #nl
	BNE e93
	      mov r0,peek		;put	it back
	      mov adr1,padr1		;set both	 to entered addresses
	      mov adr2,padr2
	  rts pc
e93=.
	CMP R0,#'T		;Case #'T
	BNE e95
	      tst padr1			;must have valid previous addresses
	      bne 2$
	      tst padr2
	      bne 2$			;as long	as non zero, were ok (so far)
1$:	      mov #15.,r1		;error of bad addresses
	      jmp error
2$:
	CMP padr2,padr1		;IS padr2 LO padr1
	BHIS e97	;Not if HIS
		  br 1$
e97=.			; 
e99=.
	CMP adr2,padr2		;IS adr2 HIS padr2
	BLO e910	;Not if LO
		  mov adr2,padr2;yes
	  rts pc
e910=.			; 
e912=.
	CMP adr2,padr1		;IS adr2 LOS padr1
	BHI e913	;Not if HI
		  mov adr2,padr1	;yes
	  rts pc
e913=.			; 
e915=.
	MOV adr2,r0
	SUB padr1,r0
	MOV padr2,r1
	SUB adr2,r1
	CMP r1,r0		;IS r1 LT r0
	BGE e916	;Not if GE
		  mov adr2,padr2
	JMP e919
e916=.			; 
		  mov adr2,padr1
e919=.			; 
e918=.
	  rts pc
e95=.
	CMP R0,#'U		;Case #'U
	BNE e920
	      mov adr2,padr1
	  rts pc
e920=.
	      mov adr2,padr2
	  rts pc
e92=.
e922=.
morecore:
	  dir$ #ex		;extend task
	  bcc 10$		;skip if got	it
5$:	  mov #11.,r1		;else too	many lines error
	  jmp error		;go	to it
10$:	  call newtop		;set up the top of memory	pointer
	bcs 5$			;if ok
	  rts pc
ycom:
	TST r2		;IS r2 = #0
	BNE e923	;Not if NE
	      mov zero,r2	;no address
	      add #2,r2		;so default to line 1
	JMP e926
e923=.			; 
	CMP r2,dol		;IS r2 HI dol
	BLOS e927	;Not if LOS
10$:		  mov #6,r1	;bad - error 6
		  jmp error
e927=.			; 
e929=.
	CMP r2,zero		;IS r2 LOS zero
	BHI e930	;Not if HI
		  br 10$	;also	err 6
e930=.			; 
e932=.
e926=.			; 
e925=.
	  mov (r2),r0		;get seek	address
	  call getlin		;and line	into lbuf1 as asciz
	  mov #-1,ipeek		;init it
	  mov #lbuf1,iptr	;and this
	MOV #1,r2	;Init loop index
e933=.
	CMP r2,#80.	;done loop?
	BGT e934	;yes if GT
	      call igetc		;get	char from macro	line
e935=.
	CMP R0,#nl		;Case #nl
	BNE e937
		  br err7		;bad, error	7
e937=.
	TST R0		;Case #0
	BNE e939
		  br err7		;bad, error	7
e939=.
	CMP R0,#'~		;Case #'~
	BNE e941
       BR e942
e941=.
	CMP R0,#tab		;Case #tab
	BNE e943
e942=.
                  br gotit              ;~ or tab, same
e936=.
e943=.
	INC r2
	JMP e933		;back to top
e934=.
err7:	  mov #16.,r1
	  jmp error
gotit:
	  mov iptr,iadr			;save	beginning of string
	  clr r1			;count chars in r1
10$:
	  call igetc
	TST r0		;IS r0 = #0
	BNE e945	;Not if NE
	      br 20$
	JMP e948
e945=.			; 
	      inc r1
	      br 10$
e948=.			; 
e947=.
20$:
	  mov iadr,r0			;found a null
	  dec r1
	  call putbak
	  rts pc
igetc:
	CMP ipeek,#-1		;IS ipeek = #-1
	BNE e949	;Not if NE
	      movb @iptr,r0
	      inc iptr			;get the char from the buffer and inc
	      bic #177600,r0		;remove any funny	stuff
	JMP e952
e949=.			; 
	      mov ipeek,r0		;else put 1	char back
	      bic #177600,r0		;get rid of garbage
	      mov #-1,ipeek		;clear putback char
e952=.			; 
e951=.
	CMP r0,#2		;IS r0 = #2
	BNE e953	;Not if NE
	      mov iptr,r0               ;change control b to nl
              movb #nl,-1(r0)
              mov #nl,r0
e953=.			; 
e955=.
	  rts pc
macrop:
	  call getc		;get the function code
e956=.
	CMP R0,#1		;Case #1
	BNE e958
	      call scom		;macro in progress suppresses a	search fail message
	      clr macflg	;clear	macro in progress flag after the sub
	      sub #2,dot	;go back a line
	  rts pc
e958=.
	CMP R0,#2		;Case #2
	BNE e960
		jmp lcom2	;second entry into l command
e960=.
	      iot		;trouble
e957=.
e962=.
nonull:
	push r0
	push r1
	push r2
	mov #lbuf2,r1		;pointer to string at r0
	MOV #1,r2	;Init loop index
e963=.
	CMP r2,r0	;done loop?
	BGT e964	;yes if GT
	TSTB (r1)		;IS (r1) = #0
	BNE e965	;Not if NE
	movb #177,(r1)		;make all nulls appear as 177
e965=.			; 
e967=.
	inc r1
	INC r2
	JMP e963		;back to top
e964=.
	pop r2
	pop r1
	pop r0
	  rts pc
ranges:
	CMP r0,#control.a		;IS r0 = #control.a
	BNE e968	;Not if NE
	call cchars
	JMP e970
e968=.
	CMP r0,#40		;IS r0 GE #40
	BLT e971	;Not if LT
	CMP r0,#177		;IS r0 NE #177
	BEQ e974	;Not if EQ
	BR e977	;Here IF r0 NE #177    
e974=.
	BR e971		;Here if not
e977=.
	push r0
	call getc			;check for range
	CMP r0,#'-		;IS r0 = #'-
	BNE e978	;Not if NE
	call genrange
	JMP e981
e978=.			; 
	mov r0,peek			;retract 1
e981=.			; 
e980=.
	pop r0
e971=.			; 
e970=.
	  rts pc
cchars:
	push r0
	push r1
	mov #lowasc,r0			;addr
	mov #elow,r1			;len
	call putbak
	pop r1
	pop r0
	  rts pc
genrange:
	push r0
	push r1
	push r2
	push r3
	mov 12(sp),r3	;get first char in range (this is the stack ref)
	inc r3		;but its still gonna be seen in r0
	call getc	;this will be the last char
	mov r0,r2
	MOV r3,char	;Init loop index
e982=.
	CMP char,r2	;done loop?
	BGT e983	;yes if GT
	mov #char,r0	;addr
	mov #1,r1	;len
	call putbak
	CMP char,r2		;IS char = r2
	BNE e984	;Not if NE
	call putbak		;last one twice
e984=.			; 
e986=.
	INC char
	JMP e982		;back to top
e983=.
	pop r3
	pop r2
	pop r1
	pop r0
	  rts pc
	  .end
