        $MACRO return   ;these MUST be lower case
        rts pc
        $ENDM 
;
        $MACRO push this
        mov #@1,-(sp)
        $ENDM 
;
        $MACRO pop that
        mov (sp)+,#@1
        $ENDM 
/
        PROGRAM Ec      ;Utility routines module 3
         mutask=1
;       .psect edi12
         
        .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 ncom     ;Handle the New commands (2 letter commands)
	.globl autostart ;automatic y command on entry

; - - -	 macro library calls
        .mcall dir$,callr
; - - -	 system	macros
; - - -	 local data and	symbols
         
        impure 
         
        nl=12 
        cr=15 
         
key:    .blkb   40.             ;40 char key max
         
        pure 
str1:   .ascii/00/ 
execy:  .ascii/1y/
        .byte 12
auto:   .ascii /autoexecute~/
autoe=.-1
	.even
;+
;
; ncom - N command processor. Reads next char and
;        dispatches if valid, else jmp directly to error with #2
;-
         
ncom:    
        call getc
        IFB R0 HIS #'a     AND    R0 LOS #'z
            bic #40,r0                              ;make it lower case
        FI 
         
        SELECT case R0 of
        CASE #'C                     ;CRYPT
            call crypt                ;encript file using key
            BREAK 
        CASE #'S	;New Substitute command (ns/this/that)
            call defall     ;1,$ default
            mov #str1,r0
            mov #1,r1
            call putbak
            call scom        ;callr scom
            IF smode NE #0
                call bcom1    ;redraw screen
            FI 
            BREAK 
        CASE #'F        ;Fill text and justify
            call nfill
            BREAK 
        DEFAULT 
            mov #2,r1
            jmp error
        ESAC 
        rts pc
         
crypt:   
        call defall
        mov #key,r1
        clr r3
        DO FOREVER
            call getc
            IF r1 HI  #key+39.  OR r0 = #nl
                BREAK 
            FI 
            bic #177600,r0          ;this had better be 7 bit ascii
            movb r0,(r1)+
            inc r3                  ;note count of chars in key
        OD 
         
        IF r3 LE #0                ;key must be present
            mov #2,r1
            jmp error
        FI 
         
        clrb (r1)                  ;asciz it
        mov #key,r2                ;start key at beginning
        LOOP for dot = adr1 THRU adr2 STEP #2
            mov @dot,r0
            call getlin
            mov #lbuf1,r1
             
            DO FOREVER
                IFB (r1) = #12  OR  (r1) = #0
                    BREAK 
                FI 
                call encode             ;do actual encoding here
                inc r1                  ;next char in lbuf
                inc r2
                IFB (r2) = #0
                    mov #key,r2          ;recycle pointer
                FI 
            OD 
            call putlin
            mov r0,@dot
        NEXT dot
        mov adr2,dot                  ;end at last line
        rts pc
;+
; encode - encode char
; input: r1 points to next char to encode
;        r2 points to next key char
; output: if the char becomes a 40 or below, bit 8 is also set.
;         if it had bit 8 set, then it is stripped first
;         all this is because of asciz usage.
;-
encode:  
        push r0
        push r3
        movb (r1),r0            ;get char
        bit #200,r0
        bic #177600,r0          ;just want 7 bits
        movb (r2),r3            ;r0 is the char, r3 is key char
        xor r3,r0               ;r0 is encoded now
        bic #177600,r0          ;just low byte
         
        IF r0 = #0 OR r0 = #nl OR r0 = #cr ;control char not allowed?
            bis #200,r0          ;yes, set 8'th bit
        FI                      ;
         
        movb r0,(r1)            ;replace it now
         
        pop r3
        pop r0
        rts pc
         
autostart:
;	TTY <Entering autostart>
	IF zero LO dol          ;make sure its got at least one line
	BEGIN
;	TTY <zero not lo dol>
           LET r0 = zero + #2
           mov (r0),r0
	   call getlin             ;get line into lbuf1
           mov #lbuf1,r0

           DO FOREVER
	      BREAK IFB (r0) = #'a
              IFB (r0) = #0
;		 PRINTF #BUF <leaving loop 1 with a null %o> r0
                 rts pc

              FI
              inc r0
           OD

           LOOP for r1 = #auto THRU #autoe
              IFB (r1) NE (r0) OR (r0) = #0
;		 PRINTF #BUF <leaving loop 2 %o> r0
                 rts pc
              FI
              inc r0
           NEXT r1
           mov #execy,r0
           mov #3,r1
           call putbak
	END
        FI

        rts pc

;buf:    .blkb 100.
        .end 
