;*************************** AMUS Program Label ******************************
; Filename: NUMBER.M68                                      Date: 01/12/89
; Category: UTIL         Hash Code: 506-013-441-625      Version: 1.1(102)
; Initials: PHI/AM       Name: JIM DULANEY
; Company: SYSTEMS TECHNOLOGY                      Telephone #: 
; Related Files: FILER.M68 (MACRO FILES USED IN NUMBER)
; Min. Op. Sys.:                               Expertise Level: BEG
; Special: Modified by IRW/AM (Irwin Goldstein) 4/1/92 to handle ++INCLUDE lines
; Description: Renumber, number, or strip numbers from a basic file.
; (.NUMBER FILENAME [start# incr#]) Defaults: start=100, inc=10
; start=0 will strip numbers.
;*****************************************************************************
;Systems Technology
;09/02/81
;11/01/82 - [100] Convert to L
;02/26/83 - [101] Handle continuation lines
;04/01/92 - [102] Handle ++INCLUDE lines
;	USAGE:	NUMBER filspec [start# incr#]
;		defaults: start#=100 incr#=10
;		start#=0 will strip
;	start# <> current line# will renumber
;Removes line numbers from any file, default extention is .BAS

	SEARCH	SYS
	SEARCH	SYSSYM
	SEARCH	FILER

	VMAJOR=1
	VMINOR=1
	VEDIT=102.


;define data offsets
	DSECT
.=0
INDDB:	BLKB	D.DDB
BAKNAM:	BLKW	3
NUMDDB:	BLKB	D.DDB
NUMNAM:	BLKW	3
LINE:	BLKL	1
INCR:	BLKL	1
CONT:	BLKW	1
PRECON:	BLKW	1
INCL:	BLKW	1
STRBUF:	BLKB	10
LINBUF:	BLKB	512.

MEMREQ	=	.
	PSECT

NUMBER:	PHDR	-1,0,PH$REE!PH$REU
	CRLF
	TYPE	<== AM-100>
	MOV	#'/,D1
	TTY
	TYPESP	<L Number Basic File Utility Version>
	VCVT	NUMBER+2,OT$TRM
	TYPECR	< ==>
	CRLF
	BYP				; check to see if we need help
	LIN
	JNE	1$
	TYPECR	<USAGE:	NUMBER filspec [start# incr#]>
	TYPECR	<	defaults: start#=100 incr#=10>
	TYPECR	<	start#=0 will strip>
	TYPECR	<	start# <> current line# will renumber>
	EXIT

1$:
	GETIMP	MEMREQ,A5
	PUSH	A2			; save index for output file
	OPNINP	INDDB(A5),BAS
	POP	A2
	OPNOUT	NUMDDB(A5),,BAK		; will force output to be a bak
	MOV	#100.,LINE(A5)		; default line number
	MOV	#10.,INCR(A5)		; default extent
	CLRW	PRECON(A5)
	CLRW	CONT(A5)
	BYP
	LIN
	BEQ	10$			; no args is default
	GTDEC
	MOV	D1,LINE(A5)		; else get the line number
	BNE	5$			; is real number 
	CLR	INCR(A5)		; else is strip operation
	BR	10$

5$:	BYP
	LIN				; check for incr
	BEQ	10$			; no, br
	GTDEC				; else getit
	MOV	D1,INCR(A5)
10$:

LOOP:	TYPE	<.>			; let user know how fast we are
	CTRLC	ABORT			; let him out
	LEA	A2,LINBUF(A5)		; point to line buffer
	CALL	GETLIN			; gets a line w/o cr/lf
	PUSH	A2
	TSTW	PRECON(A5)		; skip adding number if in cont
	BNE	5$
	TSTW	INCL(A5)		; skip adding number if ++INCLUDE [102]
	BNE	5$			; [102]

	LEA	A2,STRBUF(A5)		; point to buf for svc call
	MOV	LINE(A5),D1		; get the line
	BEQ	5$			; zero line is bypas
	MOV	INCR(A5),D7
	ADD	D7,LINE(A5)	; add incrment
	DCVT	0,50			; convert with trailing space
	CLRB	@A2
	LEA	A2,STRBUF(A5)
	PUTSTR	NUMDDB(A5)
5$:	POP	A2
	PUTSTR	NUMDDB(A5)		; line output macro
	MOVB	#CR,D1
	PUTCHR	NUMDDB(A5)
	MOVB	#LF,D1
	PUTCHR	NUMDDB(A5)

	MOVW	CONT(A5),PRECON(A5)
	CLRW	INCL(A5)		; reset ++INCLUDE flag [102]
	JMP	LOOP
DONE:
	CLOSE	INDDB(A5)		; close the files
	CLOSE	NUMDDB(A5)
	MOV	INDDB+D.FIL(A5),D0	; set up names after ddbs for DSKREN
	MOVW	INDDB+D.EXT(A5),D1
	MOV	D0,BAKNAM(A5)
	MOVW	#[TMP],BAKNAM+4(A5)
	DSKREN	INDDB(A5)		; converts INPUT.ORG to INPUT.TMP

	MOV	D0,NUMNAM(A5)
	MOVW	D1,NUMNAM+4(A5)
	DSKREN	NUMDDB(A5)		; INPUT.BAK to INPUT.ORG
	MOVW	#[BAK],BAKNAM+4(A5)
	DSKREN	INDDB(A5)		; INPUT.TMP to INPUT.BAK
	CRLF
	TYPECR	<DONE.>
	EXIT

ABORT:	CLOSE	NUMDDB(A5)		; get rid of output on control c
	DSKDEL	NUMDDB(A5)
	EXIT

;get a line into A2
GETLIN:	MOV	A2,A3
10$:	GETCHR	INDDB(A5)		; get a char
;	BEQ	DONE
	JEQ	DONE			; ???: 12/18/87 JDL
	CMPB	D1,#CR			; bypass cr
	BEQ	10$
	CMPB	D1,#LF			; line feed terminates 
	BNE	18$
12$:	CLRB	@A3			; else terminate line
	BR	60$

18$:	MOVB	D1,(A3)+		; store char in line
	BR	10$

;a3 now points to end of line, strip trailing spaces, find & if present
;a2 points to start of line
60$:	MOVB	-(A3),D1
	CMPB	D1,#BLANK
	BEQ	66$
	CMPB	D1,#HT
	BEQ	66$
	BR	70$


66$:	CMP	A2,A3
	BNE	60$
	RTN				; null line, return with it
	
70$:
;test for continuation line, D1 has last non-blank char of line
	MOVW	#0,CONT(A5)
	CMPB	D1,#'&
	BNE	71$
	MOVW	#-1,CONT(A5)
	BR	75$
71$:	
;test to see if line is a ++INCLUDE line
	PUSH	A2			; save start of line [102]
	BYP				; skip past any blanks or tabs [102]
	CMPB	(A2)+,#'+		; line starts with a +? [102]
	BNE	74$			; no - all is well [102]
	CMPB	@A2,#'+			; yes - is there a second +? [102]
	BNE	74$			;    no - all is well [102]
	MOVW	#-1,INCL(A5)		;    yes - mark it as an include line
74$:	POP	A2			; restore start of line pointer [102]

;test to see if line has numbers first
75$:	BYP				; discard leading spaces only on nums
	NUM
	BEQ	110$			; has a line number, so strip
	LEA	A2,LINBUF(A5)		; else , reindex line and send it
	BR	130$
110$:	INC	A2
	NUM				; bypass numeric chars
	BEQ	110$			; keep skipping numeric chars
	INC	A2			; skip trailing space
130$:
	RTN

	END