	TITLE POPEN
	SEARCH MONSYM,MACSYM

T1==1
T2==2
T3==3
T4==4

P==17

;POPEN - create and open a pipe
;Returns +1 failure, T1/ error code
;        +2 success, T1/ JFN on write side
;                    T2/ JFN on read side

POPEN::	STKVAR <<PIPSTR,10>,WRTJFN,REDJFN>      ;Local storage
        MOVX T1,GJ%SHT          ;Using short form
        HRROI T2,[ASCIZ/PIP:/]  ;A pipe with default size buffers
        GTJFN%                  ;Create an instance of a pipe device
         ERJMP R
        MOVEM T1,WRTJFN         ;Save JFN, use it for the write side
        HRROI T1,PIPSTR         ;Write string into this buffer
        MOVE T2,WRTJFN          ;Get back the first JFN
        MOVE T3,[1B2!1B8!JS%PAF] ;Output device and filename w/punc
        JFNS%                   ;Build a second string for GTJFN%
        MOVEI T3,"."            ;Get dot to separate name and extension
        IDPB T3,T1              ;Tack on the dot
        MOVE T2,WRTJFN          ;Get back that JFN
        MOVX T3,1B8             ;Output filename again, no punc.
        JFNS%                   ;Create "PIP:#.#"
        SETZ T3,                ;Get a null byte
        IDPB T3,T1              ;Tie off the second GTJFN% string
        MOVX T1,GJ%SHT          ;Using short form
        HRROI T2,PIPSTR         ;Point to string we just built
        GTJFN%                  ;Get a JFN on the other side of pipe
         ERJMP R
        MOVEM T1,REDJFN         ;Use that as the read side
        MOVE T1,WRTJFN          ;Get the JFN to use for the write side
        MOVE T2,[FLD(7,OF%BSZ)!OF%WR]   ;7-bits, write access
        OPENF%                  ;Create write side
         ERJMP R
        MOVE T1,REDJFN          ;Get the JFN to use for the read side
        MOVE T2,[FLD(7,OF%BSZ)!OF%RD]   ;7-bits, read access
        OPENF%                  ;Create read side
         ERJMP R
        MOVE T1,WRTJFN          ;Return write JFN (input side)
        MOVE T2,REDJFN          ;Return read JFN (output side)
        RETSKP                  ;Skip return to caller

	END
