/5 TECO SEARCH ROUTINES

/ 08-APR-79	FIXED END OF BUFFER TEST
/ 10-APR-79	CONDITIONALIZED IN EXTENDED MATCH CONTROL
/		AND BACKWARD SEARCHES

	.ENABLE 7BIT

	.GLOBAL CHR.FB,CHR.FN,CHR.FS,CHR.S,CHR.N,CHR.BA
	.GLOBAL CSWT1,SCHCTQ,SCHINV,SCHSEP,SCHUPA,SEARCH,SRHLEN

	.EXTERNAL GETN,QCHK,STABLE,QUOTST,SCHLST,SCANUP,ERR,SORT,SCHTAB
	.ZTERNAL Z77,P,Z377,ZZ,NMT,N,CAFF,Z40
	.EXTERNAL SCAN,TSTSEP,QSKP,PUSHJ,CIL2,NNEW13,CFSI
	.EXTERNAL QUOTE,TSTA,SCHSRT,TSTD,UPPERC
	.ZTERNAL DVT1,CLNF,ZIREST,NFLG,REND,ZNXTBUF

	.MACRO	.ERROR	ERNUM
	.GLOBAL ERR'ERNUM
ERR'ERNUM:	ERR
	.ENDM

	.MACRO	.SORT	ARG1,ARG2
	SORT;	ARG1;	ARG2-ARG1
	.ENDM

	.XSECT	SRHX
SXR,	0

	BACKW=0		/NO BACKWARD SEARCHES
	ANCHOR=0	/NO ANCHORED SEARCHES
	OVFLPR=0	/NO OVERFLOW PROTECTION
	PPMODE=0	/NO POINTER PRESERVATION MODE
	XTNDED=0	/NO EXTENDED MATCH CONTROL

	.ASECT TECSRH

	*1000

/SEARCH SUBROUTINE - CALLED BY N, S, AND _ COMMANDS

SEARCH,	0
	DCA	REPFLG	/AC MAY BE NON-0 TO ALLOW A REPLACE
	.IF EQ BACKW <
	GETN
	SZL SNA
	.ERROR	29	/NEG OR 0 ARG TO SEARCH
	CIA
	DCA	CSN	/GET NUMBER OF OCCURRANCES TO SEARCH FOR
	>
	.IF NE BACKW <
	GETNUM
	SNA
	.ERROR	29	/0 ARGUMENT TO SEARCH
	SZL
	CIA
	DCA	CSN
	CML RTL
	TAD	(-1
	DCA	PINCR
	>
	QCHK		/GET REPLACEMENT FOR ALTMODE, IF ANY
	TAD	(STABLE-1
	DCA	SXR	/INITIALIZE XR
	TAD	SRHLEN
	DCA	CSP
GET,	QUOTST		/GET A CHARACTER FROM THE SEARCH STRING
	JMP	SCHQUO	/OOPS- NO MORE
SORTIT,	.SORT	SCHLST,SCHTAB	/SEE IF IT'S SPECIAL
STORE,	DCA I	SXR	/STORE THE CHAR IN THE SEARCH BUFFER
	ISZ	CSP
	JMP	GET	/LOOP
	.IF NE OVFLPR <
	STA
	TAD	SXR
	DCA	SXR
	DCA I	SXR	/PUT 0 AT END (NEEDED WHEN REPORT ?STL ERROR)
	>
	.ERROR	06	/OOPS - SEARCH BUFFER FULL!

SCHUPA,	SCANUP		/GET THE NEXT CHARACTER (IN UPPER CASE)
	AND	Z77	/CONVERT IT TO A CONTROL CHARACTER
	JMP	SORTIT	/AND STORE IT IN PLACE OF ^

/	PERFORM THE SEARCH

SRHLEN,	-47		/* CHANGED TO -40 ON PDP-12 OR ON VT52 SUPPORT

SCHQUO,	TAD	CSP
	CIA		/A NULL SEARCH STRING MEANS USE THE
	TAD	SRHLEN
	SZA CLA		/PREV CONTENTS OF THE SEARCH BUFFER, ELSE
	DCA I	SXR	/STORE TERMINATING 0 AND BEGIN THE SEARCH
	.IF NE PPMODE <
	NOP		/*ED BECOMES TAD P IF 16ED
	DCA	PSAVE
	>
CSST,	TAD	P
	DCA	CSP
	JMP	CSF1
SCHINV,	TAD	CSNCL	/^N, INVERT SKIP SENSE
	DCA	CSWT

CSL,	TAD I	SXR	/GET A CHAR FROM THE SEARCH BUFFER
	SPA SNA
	JMP	SCCOMD	/NEGATIVE CHARS AND 0 ARE SPECIAL
	CIA
	CDF 10
	TAD I	P
	AND	Z377
CSWT1,	CDF 0
CSWT,	SZA CLA
	JMP	CSF	/FAIL TO MATCH ON THIS CHARACTER
			/ABOVE LINE BECOMES JMP CSZ IF ::
CSWT2,	ISZ	P
CSG,	TAD	CSZCL
	DCA	CSWT	/RESTORE SEARCH TEST
	.IF NE  BACKW <
	TAD	PINCR
	SMA CLA
	>
	TAD	ZZ
	CMA
	TAD	P
CSZCL,	SZA CLA		/CHECK FOR END OF BUFFER
	JMP	CSL	/NO
EOB,
	.IF NE PPMODE <
	TAD	PSAVE
	>
	DCA	P
CSZ,	DCA	NMT
	JMP I	SEARCH

	.IF NE PPMODE <
PSAVE,	0		/SAVE LOCATION OF POINTER BEFORE SEARCH
	>

/SEARCH SUBROUTINE - CONTINUED

SCCOMD,	DCA	1$	/SPECIAL CHARACTERS ARE JUMPS OR 0
	.IF NE	XTNDED <
	TAD	1$
	RAL
	SPA CLA
	JMP I	1$	/BRANCH TO ADDRESS IF .GT. 6000
	>
1$:	HLT		/0 FALLS THROUGH INTO TERMINATION CODE

	ISZ	CSN	/GET NTH OCCURRENCE
	JMP	CSF	/MORE TO GO
	CMA
	JMP	CSZ	/GOT IT
SCHSEP,	CDF	10	/^S, LOOK FOR SEPARATOR
	TAD I	P
	CDF	0
	AND	Z377
	TSTSEP		/SHARED SORTING ROUTINE
	JMP	CSWT2	/CHECK RESULTS OR FALL INTO CSF
	.IF EQ BACKW <
CSF,	ISZ	CSP	/INDEX P
	>
	.IF NE BACKW <
CSF,	TAD	PINCR
	TAD	CSP
	DCA	CSP
	>
CSF1,	TAD	(STABLE-1
	DCA	SXR	/INITIALIZE AUTO - INDEX
	TAD	CSP
	DCA	P
	JMP	CSG

SCHCTQ,	SCAN		/GET THE NEXT CHARACTER
	JMP	STORE

	.IF NE BACKW <
PINCR,	1		/-1 IF SEARCH GOES BACKWARDS
	>

	.SBTTL	Cmd FN

	.SBTTL	Cmd F_

CHR.FB,	CLA IAC
CHR.FN,	DCA	CNXT
	STA
	JMP	CHRN1

	.SBTTL	Cmd FS

CHR.FS,	STA		/CHANGE S TO FS

	.SBTTL	Cmd S

CHR.S,	JMS	SEARCH	/S COMMAND - DO A SEARCH
CHKREP,	ISZ	REPFLG	/WAS THERE A REPLACE SPECIFIED?
	JMP	2$	/NO - CHECK FOR COLON
	QSKP		/COUNT UP STRING 2
	TAD	NMT
	SMA CLA
	JMP	2$	/FAILED, SET VALUE & EXIT
	TAD	CSP	/FIGURE OUT OFFSET TO FAKE OUT "I" ROUTINE
	CIA		/SO THAT WE HAVE THE RIGHT INSERTION COUNT
	TAD	P	/BUT THE SIZE OF THE HOLE WE NEED
	DCA	DVT1	/IS DECREASED BY THE LENGTH OF THE SEARCH STRING.
	TAD	CSP	/RESET
	DCA	P	/TEXT POINTER
	PUSHJ		/INSERT
		CIL2	/STRING 2
2$:	DCA	REPFLG	/CLEAR REPLACE FLAG
	TAD	NMT
	PUSHJ		/FORM NUMBER FROM "NMT"
		NNEW13	/(APPLYING OPERATOR, IF NECESSARY)
	ISZ	CLNF	/WAS THERE A COLON ON THIS SEARCH?
	SKP		/NO
	JMP I	ZIREST	/YES - GO AWAY REGARDLESS OF RESULTS
	DCA	CLNF	/RESET COLON FLAG TO 0
	ISZ	N	/DID WE SUCCEED?
	JMP I	(CFSI	/NO - SIMULATE A SEMICOLON
	DCA	NFLG	/YES - HOWEVER, NO COLON MEANS NO RESULT
	JMP I	ZIREST

/CFSI SHOULD REALLY BE IN THIS MODULE

	.SBTTL	Cmd _

CHR.BA,	CLA IAC		/_ COMMAND

	.SBTTL	Cmd N

CHR.N,	DCA	CNXT	/N COMMAND - SET OUTPUT FLAG
CHRN1,	JMS	SEARCH	/DO A SEARCH
	TAD	REND
	CIA
	TAD	ZZ
CSNCL,	SNA CLA		/HAVE WE REACHED END-OF-FILE?
	JMP	CHKREP	/YES - STOP AND ASSIGN VALUE
	TAD	NMT
	SZA CLA		/HAVE WE SUCCEEDED?
	JMP	CHKREP	/YES - STOP AND ASSIGN VALUE
	TAD	CNXT
	JMS I	ZNXTBUF	/GET NEXT BUFFER
	JMP	CSST	/KEEP SEARCHING - RETURN TO CHR.N+2
CNXT,	0		/OUTPUT FLAG
CSP,	0		/TEMP P
CSN,	0
REPFLG,	0		/REPLACE FLAG (-1 MEANS REPLACE)
	PAGE

	.ASECT	TECMAT

	FIELD 1

	*3200

	RELOC 7200

	.IF EQ 1 <

COVAL,	0
	TAD	NMT
	PUSHJ
		NNEW13
	ISZ	CLNF	/DID COMMAND HAVE : ?
	SKP		/NO
	JMP I	COVAL	/YES
	DCA	CLNF
	DCA	NFLG	/NO : MEANS NO VALUE
	ISZ	N
	ISZ	COVAL	/TAKE RETURN 2 IF N .NE. -1
	JMP I	COVAL

	>

/FAILS TO ALLOW ; TO WORK

/AT END OF CHKREP, AFTER 2$:
/	JMS COVAL
/	JMP I ZIREST
/	JMP I (CFSI

	.IF NE XTNDED <

/STRING BUILD CHARACTERS

	.SBTTL	Str ^Q
	.SBTTL	Str ^R

SCHCTR,	JMS	GETQC
	JMP I	(STORE	/STORE NEXT CHARACTER LITERALLY

	.SBTTL	Str ^

UPARR,	JMS	GETQC
	UPPERC		/CONVERT TO UPPER CASE
	AND	Z77	/CONVERT TO CONTROL CHAR
	JMP I	(SORTIT	/AND PRETEND WE SAW THIS CONTROL CHARACTER

	.SBTTL	Str ^V

SCHCTV,	JMS	GETQC
	UPPERC
	TAD	Z40
	JMP I	(STORE	/STORE LOWER CASE VERSION OF NEXT CHARACTER

	.SBTTL	Str ^W

SCHCTW,	JMS	GETQC
	UPPERC
	JMP I	(STORE	/STORE UPPER CASE VERSION OF NEXT CHARACTER

/CAN REMOVE ^R FROM REGULAR TABLE

/	MATCH CONTROL CONSTRUCTS

	.SBTTL	Str ^N

CTRLEN,	TAD 	(SNA CLA
	DCA I	(CSWT
	JMP I	(CSL
/OR	JMP I	(SCHINV

	.SBTTL	Str ^E

SCHCTE,	JMS	GETQC
	UPPERC
	.SORT	CTELST,CTETAB
	SMA
	.ERROR 99	/39 ILLEGAL ^E ARGUMENT
	JMP I	(STORE

CTELST,	"A
	"B
	"C
	"D
	"L
/	"M
	"N
	"R
/	"S
	"V
	"W
	"X

LFTAB,			/3 NEGATIVE NUMBERS MUST FOLLOW
CTETAB,	CTRLEA
	CTRLEB		/^S
	CTRLEC
	CTRLED
	CTRLEL
/	CTRLEM
	CTRLEN		/^N
	CTRLER
/	CTRLES
	CTRLEV
	CTRLEW
	CTRLEX		/^X

GETQC,	0
	SCAN		/GET NEXT CHARACTER FROM SEARCH STRING
	.SORT QUOTE,(-26.)/SIMULATE QUOTST WHICH CAN'T BE CALLED FROM ABOVE 4000
	SPA
ATEND,	.ERROR 84	/CAN'T QUOTE THE DELIMITER
	JMP I	GETQC

	.SBTTL	Str ^X
	.SBTTL	Str ^EX

CTRLEX,	JMP I	(CSWT1

	.SBTTL	Str ^EA

CTRLEA,	JMS	CTRLE
	TSTA

	.SBTTL	Str ^EB

CTRLEB,	JMS	CTRLE
	TSTDEL

	.SBTTL	Str ^EC
	.SBTTL	Str ^ER

CTRLER,
CTRLEC,	JMS	CTRLE
	SCHSRT

	.SBTTL	Str ^ED

CTRLED,	JMS	CTRLE
	TSTD

	.SBTTL	Str ^EL

CTRLEL,	JMS	CTRLE
	TSTLIN

/	.SBTTL	Str ^ES
/	.SBTTL	Str ^EM

/CTRLES,
/CTRLEM,	JMP	ERR99	/SHOULD BE JMP ERR39

	.SBTTL	Str ^EV

CTRLEV,	JMS	CTRLE
	TSTLC

	.SBTTL	Str ^EW

CTRLEW,	JMS	CTRLE
	TSTUC

TSTSUB,
CTRLE,	0
	TAD I	CTRLE
	DCA	TSTSUB
	DCA	MATC
	CDF	10
	TAD I	P
	CDF	0
	AND	Z377
	JMS I	TSTSUB	/IS CHAR A MATCH?
	ISZ	MATC	/NO
	CLA		/YES
	TAD	MATC
	JMP I	(CSWT

MATC,	0

TSTLIN,	0
	.SORT	CAFF,LFTAB
	SPA CLA
	ISZ	TSTLIN
	JMP I	TSTLIN

TSTUC,	0
	TAD	TSTUC
	DCA	TSTLC
	TAD	Z40
	SKP
TSTLC,	0
	TAD	(-141
	CLL
	TAD	(-26.
	SNL CLA
	ISZ	TSTLC
	JMP I	TSTLC

TSTDEL,	0
	TSTSEP
	ISZ	TSTDEL
	JMP I	TSTDEL
	PAGE
	>
	RELOC
