$auto$=-2
.enabl lc
;15-aug-80 for Conroy C runtime
        mutask=1 
        .psect edi13
         
        .enabl lc
        .enabl ama
        .nlist sym
         
        .if df mutask
        .MACRO pure
        .psect pur,ro
        .ENDM 
         
        .MACRO impure
        .psect ipur,rw
        .ENDM 
        .iff 
        .MACRO pure
        .ENDM 
         
        .MACRO impure
        .ENDM 
        .endc 
         
         
        .globl nfill    ;Handle the Nf command (fill text)
        .globl hspace   ;character used as a forced space
        .globl fparms   ;5 word parm list, for display
        impure 
         
         
        .mcall dir$,callr
wordb:  .blkb     80.   ;buffer for storing a word
        ewordb=.-1      ;end of buffer
         
i:      .word           ;index
         
fparms: 				;global pointer to parm list
left:   .word 5         ;left margin
right:  .word 70.       ;right margin
indent: .word 0.        ;indent on paragraph
spaces: .word 1.        ;single, double, ...
justfg: .word 1.        ;justify flag 1=yes, 0=no
         
nlines: .word 0         ;input to spacing routine, # of lines to output
nwords: .word 0         ;word in line counter
ip:     .word           ;pointer into wordb
op:     .word           ;pointer into lbuf3 (output buffer)
nullb:  .word 0         ;this is a null word that ip initially points to
outw:   .word 0         ;out width
wwid:   .word 0         ;width of current word
         
addr:   .word           ;current addr on input
peekc:  .word           ;look ahead char
gflag:  .word           ;get char flag
adloc:  .word           ;location we are adding after
         
         
str1:   .asciz/--/ 
        .even 
         
        nl=12 
        cr=15 
        space=40 
        backspace=10            ;
        hspace=2                ;hard space, not delimeter character
        tab=11 
        comma=54 
        pure 
nfill:    
	CMP zero,dot		;IS zero = dot
	BNE Ed1	;Not if NE
            mov #4,r1
            jmp error
Ed1=.			; 
Ed3=.
         
         
        mov #-1,peekc
        mov #nullb,ip           ;initialize
         
	TST adr2		;IS adr2 = #0
	BNE Ed4	;Not if NE
	     mov dot,adr1
             mov dot,adr2
Ed4=.			; 
Ed6=.
	JSR PC,getparms
         
	CMP adr1,zero		;IS adr1 = zero
	BNE Ed7	;Not if NE
	CMP adr1,adr2		;IS adr1 = adr2
	BNE Ed10	;Not if NE
                clr adr2           ;clear for sfacts so it dont shade
	JSR PC,sfacts
                rts pc             ;return
	JMP Ed13
Ed10=.			; 
                mov #4,r1          ;but if multiple addresses and 1
                jmp error          ;of 'em is zero = bad address
Ed13=.			; 
Ed12=.
Ed7=.			; 
Ed9=.
        mov adr1,padr1
        mov adr2,padr2
        mov adr2,adloc          ;add text after here
        mov adr1,addr           ;initialize address to get lines from
        mov #lbuf3,op           ;init
	JSR PC,resetm
	JSR PC,brake1
Ed14=.
	JSR PC,getwd
	BCC Ed16	;Not if CC
	JMP Ed15         ;break
Ed16=.			; 
Ed18=.
	TSTB wordb		;IS wordb = #0
	BNE Ed19	;Not if NE
	JSR PC,brake
	JMP Ed22
Ed19=.			; 
	JSR PC,putwd
Ed22=.			; 
Ed21=.
	JMP Ed14		;Repeat loop
Ed15=.
	JSR PC,flsh
	MOV adloc,addr
	SUB adr2,addr
	ADD adr1,addr
	SUB #2,addr
        mov padr1,-(sp)
        mov padr2,-(sp)
	JSR PC,delete
        mov (sp)+,padr2
        mov (sp)+,padr1
        mov addr,dot
	CMP dot,dol		;IS dot HI dol
	BLOS Ed23	;Not if LOS
            mov dol,dot          ;but not beyond end
Ed23=.			; 
Ed25=.
         
	CMP dot,zero		;IS dot LOS zero
	BHI Ed26	;Not if HI
	MOV zero,dot
	ADD #2,dot
Ed26=.			; 
Ed28=.
        mov adr2,-(sp)
        mov dot,adr2             ;for bcom1
         
	TST smode		;IS smode NE #0
	BEQ Ed29	;Not if EQ
	JSR PC,bcom1
Ed29=.			; 
Ed31=.
        mov (sp)+,adr2
	CMP padr1,adr1		;IS padr1 = adr1
	BNE Ed32	;Not if NE
	CMP padr2,adr2		;IS padr2 = adr2
	BNE Ed35	;Not if NE
	BR Ed38	;Here IF padr2 = adr2
Ed35=.
	BR Ed32		;Here if not
Ed38=.
            mov dot,padr2        ;readjust tag range to new size of block
Ed32=.			; 
Ed34=.
         
        rts pc
         
putwd:    
        jsr pc,saval
	JSR PC,size
        mov r0,r3                       ;copy length into r3
        mov r1,wwid                     ;save width of word
         
         
	MOV op,r2
	ADD r3,r2
	MOV wwid,r4
	ADD outw,r4
	CMP r4,right		;IS r4 GT right
	BLE Ed42	;Not if LE
	BR Ed40		;condition met
Ed42=.
	CMP r2,#lbuf3+lbs-3		;IS r2 HIS #lbuf3+lbs-3
	BLO Ed39	;Not if LO
Ed40=.
	JSR PC,spread
	JSR PC,outlin
Ed39=.			; 
Ed41=.
         
        add wwid,outw                   ;accumulate width of line
        inc outw                        ;add one for spacing
        inc nwords                      ;count words
        mov #wordb,r1                   ;start of buffer
	MOV #1,r2	;Init loop index
Ed45=.
	CMP r2,r3	;done loop?
	BGT Ed46	;yes if GT
            movb (r1)+,@op               ;copy onto end of line
            inc op                       ;inc pointer
	INC r2
	JMP Ed45		;back to top
Ed46=.
        movb #space,@op                 ;add space at end
        inc op                          ;of line
         
        rts pc
spread:  
        jsr pc,saval
	TST nwords		;IS nwords GT #0
	BLE Ed47	;Not if LE
            dec op                       ;clear last space at end
            dec outw                     ;and lessen width by one
Ed47=.			; 
Ed49=.
         
        movb #nl,@op                    ;add newline at end
        inc op                          ;next
        clrb @op                        ;add null at end also
         
	MOV right,r0
	SUB outw,r0
	MOV op,r1
	SUB #lbuf3-2,r1
        mov r0,-(sp)
        mov r1,-(sp)
        mov (sp)+,r1
        mov (sp)+,r0
        mov nwords,r2                   ;
	TST justfg		;IS justfg NE #0
	BEQ Ed50	;Not if EQ
	JSR PC,fillit
Ed50=.			; 
Ed52=.
        rts pc
flsh:      
         
        movb #nl,@op                    ;add nl and null at end
        inc op
        clrb @op
	JSR PC,outlin
         
        rts pc
outlin:  
	JSR PC,outln1
	MOV spaces,nlines
	DEC nlines
	JSR PC,spacing
        mov #lbuf3,op
	JSR PC,resetm
        rts pc
outln1:  
        jsr pc,saval
	JSR PC,adline
        rts pc
resetm:  
        jsr pc,saval
        clr outw
        clr nwords                      ;init for next time
        mov #lbuf3,r0
	MOV #2,r1	;Init loop index
Ed53=.
	CMP r1,left	;done loop?
	BGT Ed54	;yes if GT
            movb #hspace,(r0)+           ;these are hard spaces
            inc outw
	INC r1
	JMP Ed53		;back to top
Ed54=.
        mov r0,op
        rts pc
brake:   
         
	JSR PC,flsh
        mov spaces,nlines
	JSR PC,spacing
        mov #lbuf3,op
	JSR PC,resetm
brake1:  
        jsr pc,saval
	MOV #1,r3	;Init loop index
Ed55=.
	CMP r3,indent	;done loop?
	BGT Ed56	;yes if GT
            movb #hspace,@op
            inc op
            inc outw
	INC r3
	JMP Ed55		;back to top
Ed56=.
        rts pc
spacing: 
        jsr pc,saval
        mov (pc)+,@(pc)+        ;mov #<nl,0> , lbuf3
        .byte nl,0
        .word lbuf3
	MOV #1,r3	;Init loop index
Ed57=.
	CMP r3,nlines	;done loop?
	BGT Ed58	;yes if GT
	JSR PC,outln1
	INC r3
	JMP Ed57		;back to top
Ed58=.
        rts pc
getwd:    
        jsr pc,saval
        mov #wordb,r0
         
Ed59=.
	JSR PC,getch
	BCC Ed61	;Not if CC
                rts pc
Ed61=.			; 
Ed63=.
                 
	CMP r0,#tab		;IS r0 = #tab
	BNE Ed70	;Not if NE
	BR Ed68		;condition met
Ed70=.
	CMP r0,#space		;IS r0 = #space
	BNE Ed67	;Not if NE
Ed68=.
	BR Ed65		;condition met
Ed67=.
	CMP r0,#nl		;IS r0 = #nl
	BNE Ed64	;Not if NE
Ed65=.
	JMP Ed73
Ed64=.			; 
	JMP Ed60         ;break
Ed73=.			; 
Ed66=.
	JMP Ed59		;Repeat loop
Ed60=.
         
	TST r0		;IS r0 = #0
	BNE Ed74	;Not if NE
            clrb wordb                   ;and indicate in word buffer
            clc                          ;not end of range
            rts pc                       ;but done for now
Ed74=.			; 
Ed76=.
         
        mov r0,peekc                    ;else put back the char
         
        mov #wordb,r1                   ;addr of output
Ed77=.
	JSR PC,getch
	BCC Ed79	;Not if CC
                rts pc
Ed79=.			; 
Ed81=.
	CMP r0,#tab		;IS r0 = #tab
	BNE Ed88	;Not if NE
	BR Ed86		;condition met
Ed88=.
	CMP r0,#space		;IS r0 = #space
	BNE Ed85	;Not if NE
Ed86=.
	BR Ed83		;condition met
Ed85=.
	CMP r0,#nl		;IS r0 = #nl
	BNE Ed82	;Not if NE
Ed83=.
                clrb (r1)                 ;these are terminators and were
	JMP Ed78         ;break
Ed82=.			; 
Ed84=.
            movb r0,(r1)+                ;else put the char into the word buf
	CMP r1,#ewordb-1		;IS r1 HIS #ewordb-1
	BLO Ed91	;Not if LO
                clrb (r1)                 ;and truncate it here
	JMP Ed78         ;break
Ed91=.			; 
Ed93=.
	JMP Ed77		;Repeat loop
Ed78=.
         
        rts pc
         
getch:    
         
        clr gflag                       ;
	CMP peekc,#-1		;IS peekc NE #-1
	BEQ Ed94	;Not if EQ
            mov peekc,r0                 ;there is one
            mov #-1,peekc                ;so return it and clear it
            clc                          ;not end of range
            rts pc                       ;
Ed94=.			; 
Ed96=.
         
getch1:  
        movb @ip,r0                     ;get next char in buf
        inc ip                          ;update to next
         
	TST r0		;IS r0 NE #0
	BEQ Ed97	;Not if EQ
            clc                          ;else everything is ok to return
            rts pc                       ;
Ed97=.			; 
Ed99=.
         
	JSR PC,nline
	BCC Ed100	;Not if CC
            rts pc                       ;so return with end of range status
Ed100=.			; 
Ed102=.
         
        clr gflag
        mov #lbuf1,r1                   ;input buffer
        mov #lbuf2,r2                   ;needs to be copied here
        mov r2,ip                       ;also init pointer the beginning
Ed103=.
            movb (r1)+,(r2)              ;mov input to output
	BEQ Ed105	;Not if EQ
	CMPB (r2),#space		;IS (r2) NE #space
	BEQ Ed108	;Not if EQ
	CMPB (r2),#tab		;IS (r2) NE #tab
	BEQ Ed111	;Not if EQ
	CMPB (r2),#nl		;IS (r2) NE #nl
	BEQ Ed114	;Not if EQ
	BR Ed117	;Here IFB (r2) NE #nl    
Ed114=.
	BR Ed111		;Here if not
Ed117=.
	BR Ed118	;Here IFB (r2) NE #tab AND (r2) NE #nl
Ed111=.
	BR Ed108		;Here if not
Ed118=.
                    inc gflag              ;indicate not an essentially blank line
Ed108=.			; 
Ed110=.
	JMP Ed119
Ed105=.			; 
	JMP Ed104         ;break
Ed119=.			; 
Ed107=.
            inc r2                       ;else to next output location
	JMP Ed103		;Repeat loop
Ed104=.
         
	TST gflag		;IS gflag = #0
	BNE Ed120	;Not if NE
            clr r0                       ;yes, so send back a null
            rts pc
	JMP Ed123
Ed120=.			; 
            br getch1                    ;no so go get first char in line
Ed123=.			; 
Ed122=.
         
nline:    
         
	CMP addr,adr2		;IS addr HI adr2
	BLOS Ed124	;Not if LOS
            sec 
	JMP Ed127
Ed124=.			; 
            mov @addr,r0
	JSR PC,getlin
            add #2,addr
            clc 
Ed127=.			; 
Ed126=.
        rts pc
         
size:    
	mov #wordb,r0
Ed128=.
	tstb (r0)+
	bne Ed128
	sub #wordb,r0
	dec r0
         
        clr r1                          ;clear width count
        mov #wordb-1,r4                 ;
        add r0,r4                       ;last char in word
         
	MOV #wordb,r2	;Init loop index
Ed129=.
	CMP r2,r4	;done loop?
	BHI Ed130	;yes if HI
Ed131=.
	CMPB (r2),#nl		;Case #nl
	BNE Ed133
	JMP Ed132         ;break
                 
Ed133=.
	CMPB (r2),#backspace		;Case #backspace
	BNE Ed135
                dec r1                 ;backspace = -1
	JMP Ed132         ;break
                 
Ed135=.
                inc r1                 ;else +1
Ed132=.
Ed137=.
	INC r2
	JMP Ed129		;back to top
Ed130=.
        rts pc
         
         
getparms:  
	JSR PC,getc
	CMP r0,#'.		;IS r0 = #'.
	BNE Ed138	;Not if NE
	JSR PC,getlpm
            rts pc
	JMP Ed141
Ed138=.			; 
            mov r0,peek
Ed141=.			; 
Ed140=.
         
         
	MOV #left,i	;Init loop index
Ed142=.
	CMP i,#justfg	;done loop?
	BGT Ed143	;yes if GT
10$:
	JSR PC,getc
	CMP r0,#space		;IS r0 = #space
	BNE Ed144	;Not if NE
                br 10$
Ed144=.			; 
Ed146=.
	CMP r0,#nl		;IS r0 = #nl
	BNE Ed147	;Not if NE
	JMP Ed143         ;break
Ed147=.			; 
Ed149=.
	CMP r0,#comma		;IS r0 NE #comma
	BEQ Ed150	;Not if EQ
	JSR PC,getnum
                mov r1,@i
	JSR PC,getc
	CMP r0,#nl		;IS r0 = #nl
	BNE Ed153	;Not if NE
                    mov r0,peek        ;put it back only if a nl
Ed153=.			; 
Ed155=.
Ed150=.			; 
Ed152=.
	ADD #2,i
	JMP Ed142		;back to top
Ed143=.
         
        rts pc
         
         
         
adline: 				;add line in lbuf3 to file
        jsr pc,saval
        clrb lbuf3+lbs-1
        mov #lbuf3,r0
        mov #lbuf1,r1
         
5$:      
        movb (r0)+,r2
	CMPB r2,#hspace		;IS r2 = #hspace
	BNE Ed156	;Not if NE
            mov #space,r2
Ed156=.			; 
Ed158=.
        movb r2,(r1)+
        bne 5$
         
        mov adloc,r5
        mov #10$,r4
	JSR PC,append
        add #2,adloc
        rts pc
         
         
         
10$:    mov #20$,r4
        clc 
        rts pc
         
20$:    sec 
        rts pc
         
getlpm:  
         
	JSR PC,getc
	CMP r0,#nl		;IS r0 NE #nl
	BEQ Ed159	;Not if EQ
            mov #2,r1
            jmp error
Ed159=.			; 
Ed161=.
         
	CMP adr1,zero		;IS adr1 = zero
	BNE Ed162	;Not if NE
            mov @dot,r0
	JMP Ed165
Ed162=.			; 
            mov @adr1,r0
Ed165=.			; 
Ed164=.
         
	JSR PC,getline
	JSR PC,expand
	JSR PC,findend
	MOV r0,r1
	SUB #text,r1
         
        mov #text,r0
Ed166=.
	CMP r0,#etext		;IS r0 HIS #etext
	BLO Ed168	;Not if LO
	JMP Ed167         ;break
Ed168=.			; 
Ed170=.
	CMPB (r0),#space		;IS (r0) NE #space
	BEQ Ed171	;Not if EQ
	JMP Ed167         ;break
Ed171=.			; 
Ed173=.
            inc r0
	JMP Ed166		;Repeat loop
Ed167=.
         
        sub #text,r0
        inc r0
	CMP r1,r0		;IS r1 LT r0
	BGE Ed180	;Not if GE
	BR Ed178		;condition met
Ed180=.
	TST r1		;IS r1 LE #0
	BGT Ed177	;Not if GT
Ed178=.
	BR Ed175		;condition met
Ed177=.
	CMP r0,#140.		;IS r0 GT #140.
	BLE Ed174	;Not if LE
Ed175=.
            mov #2,r1
            jmp error
	JMP Ed183
Ed174=.			; 
            mov r0,left
            mov r1,right
Ed183=.			; 
Ed176=.
         
        rts pc
         
         
        .end 
