zOs/REXX/PIPE

/* copy pipe begin ****************************************************
**********************************************************************/
pipeIni: procedure expose m.
    if m.pipe_ini == 1 then
        return
    m.pipe_ini = 1
    call catIni
    call mapReset v
    m.v_with.0 = 0
    m.v_withMap = ''
    m.v_with.0.map = ''
    m.pipe.0 = 1
    m.pipe.1.in  = m.j.in
    m.pipe.1.out = m.j.out
    call pipe '+'
    return
endProcedure pipeIni

/*-------------------------------
  +-       push pop frame
  PYNFA    ouput: Parent saY Newcat File, Appendtofile
  psf|     input: parent string file oldOut
  old          --> new
  pipeBegin    --> pipe '+N'
  pipeBeLa f   --> pipe '+F'
  pipeLast     --> pipe 'P|'
  pipeLast f   --> pipe 'F|', f
  pipeEnd      --> pipe '-'
--------------------------------*/
pipe: procedure expose m.
parse arg opts, aO
    ox = 1; oc = substr(opts, ox, 1)
    ax = m.pipe.0
    px = ax -1
    if oc == '-' then do
        if px < 2 then
            call err 'pipe pop empty'
        call jClose m.pipe.ax.out
        call jClose m.pipe.ax.in
        ax = px
        m.pipe.0 = ax
        px = ax-1
        ox = ox+1; oc = substr(opts, ox, 1)
        end
    if oc == '+' then do
        px = ax
        ax = ax+ 1
        m.pipe.0 = ax
        m.pipe.ax.in  = jOpen(m.pipe.px.in, '<')
        m.pipe.ax.out = jOpen(m.pipe.px.out, '>')
        ox = ox+1; oc = substr(opts, ox, 1)
        end
    oOut = m.pipe.ax.out
    if pos(oc, 'NYPFA') > 0 then do
        call jClose oOut
        if oc == 'Y' then
            m.pipe.ax.out = jOpen(m.pipe.1.out, '>')
        else if oc == 'P' then
            m.pipe.ax.out = jOpen(m.pipe.px.out, '>')
        else if oc == 'N' then
            m.pipe.ax.out = jOpen(Cat(), '>')
        else if oc == 'F' then
            m.pipe.ax.out = jOpen(o2file(aO), '>')
        else if oc == 'A' then
            m.pipe.ax.out = jOpen(o2file(aO), '>>')
        ox = ox+1; oc = substr(opts, ox, 1)
        end
    m.j.out = m.pipe.ax.out
    if oc \== ' ' then do
        call jClose m.pipe.ax.in
        if substr(opts, ox+1) = '' & oc \== 's' then
            ct = ''
        else
            ct = jOpen(Cat(), '>')
        lx = 3
        do forever
            if oc == 's' then do
                call jWrite ct, arg(lx)
                lx = lx + 1
                end
            else do
                if oc == 'p' then
                    i1 = m.pipe.px.in
                else if oc == '|' then
                    i1 = oOut
                else if oc == 'f' then do
                    i1 = arg(lx)
                    lx = lx + 1
                    end
                else
                    call err 'implement' oc 'in pipe' opts
                if ct \== '' then
                    call jWriteAll ct, o2File(i1)
                end
            ox = ox + 1
            if substr(opts, ox, 1) == ' ' then
                leave
            else if ct == '' then
                call err 'pipe loop but ct empty'
            else
                oc = substr(opts, ox, 1)
            end
        if ct == '' then
            m.pipe.ax.in = jOpen(o2file(i1), '<')
        else
            m.pipe.ax.in = jOpen(jClose(ct), '<')
        if lx > 3 & lx <> arg() + 1 then
            call err 'pipe opts' opts 'but' arg() 'args not' (lx-1)
        end
    m.j.in  = m.pipe.ax.in
    return
endProcedure pipe

/*--- write all from rdr (rsp in) to out, not lazy ----------------*/
pipeWriteNow: procedure expose m.
    parse arg rdr
    call jWriteNow m.j.out, if(rdr == '', m.j.in, rdr)
    return
endProcedure pipeWriteNow

/*--- write all from rdr (rsp in) to out, possibly lazy -----------*/
pipeWriteAll: procedure expose m.
parse arg rdr
    call jWriteAll m.j.out, if(rdr == '', m.j.in, rdr)
    return
endProcedure pipeWriteAll

pipePreSuf: procedure expose m.
parse arg le, ri
    do while in()
        call out le || m.in || ri
        end
    return
endProcedure pipePreSuf

vIsDefined: procedure expose m.
parse arg na
    return   '' \== vAdr(na, 'g')
endProcedure vIsDefined

vWith: procedure expose m.
parse arg fun, o
    if fun == '-' then do
        tBe = m.v_with.0
        tos = tBe - 1
        if tos < 0 then
            call err 'pop empty withStack'
        m.v_with.0 = tos
        m.v_withMap = m.v_with.tos.map
        return m.v_with.tBe.obj
        end
    else if fun \== '+' then
        call err 'bad fun vWith('fun',' o')'
    par = m.v_with.0
    tos = par + 1
    m.v_with.0 = tos
    if symbol('m.v_with.tos.obj') == 'VAR' then
      if objClass(o) == objClass(m.v_with.tos.obj) then do
          m.v_with.tos.obj = o
          m.v_withMap = m.v_with.tos.map
          return
          end
    m.v_with.tos.obj = o
    if par > 0 then
        key = m.v_with.par.classes
    else
        key = ''
    if o \== '' then
        key = strip(key objClass(o))
    m.v_with.tos.classes = key
    if symbol('m.v_withManager.key') == 'VAR' then do
        m.v_with.tos.map = m.v_withManager.key
        m.v_withMap = m.v_withManager.key
        return
        end
    m = mapNew()
    m.v_with.tos.map = m
    m.v_withMap = m
    m.v_withManager.key = m
    do kx=1 to words(key)
        c1 = word(key, kx)
        call vWithAdd m, kx, classMet(c1, 'oFlds')
        call vWithAdd m, kx, classMet(c1, 'stms')
        end
    return
endProcedure vWith

vWithAdd: procedure expose m.
parse arg m, kx, ff
    do fx=1 to m.ff.0
        n1 = m.ff.fx
        dx = pos('.', n1)
        if dx > 1 then
            n1 = left(n1, dx-1)
        else if dx = 1 | n1 = '' then
            iterate
        call mPut m'.'n1, kx
        end
    return
endProcedure vWithAdd

vForWith: procedure expose m.
parse arg var
    call vWith '-'
    if \ vIn(var) then
        return 0
    call vWith '+', m.in
    return 1
endProcedure vForWith

vGet: procedure expose m.
parse arg na
    a = vAdr(na, 'g')
    if a = '' then
        call err 'undefined var' na
    return m.a
endProcedure vGet


vPut: procedure expose m.
parse arg na, val
    a = vAdr(na, 'p')
    m.a = val
    return val
endProcedure vPut

/*--- find the final address
      return f || a with address a and
             f = m -> mapGet(a), o -> obect m.a, s -> string m.a  ---*/
vAdr: procedure expose m.
parse arg na, f
    cx = 0
    cx = verify(na, '&>', 'm')
    if cx > 0 then
        a = left(na, cx-1)
    else do
        a = na
        cx = length(na)+1
        end
    nxt = 0
    do forever
        cy = verify(na, '&>', 'm', cx+1)
        if cy > 0 then
            fld = substr(na, cx+1, cy-cx-1)
        else
            fld = substr(na, cx+1)
        if substr(na, cx, 1) == '>' then do
            if nxt then
                a = vAdrByM(a)
            if fld \== '' then
               a = a'.'fld
            end
        else do
            if nxt then
                a = vAdrByM(a)
            mp = m.v_withMap
            aL = a
            if pos('.', a) > 0 then
                aL = left(a, pos('.', a)-1)
            if mp \== '' & symbol('m.mp.aL') == 'VAR' then do
                wx = m.mp.aL
                a = m.v_with.wx.obj'.'a
                end
            else if cx >= length(na) then
                return mapAdr(v, a, f)
            else
                a = mapAdr(v, a, 'g')
            if fld \== '' then
                a = vAdrByM(a)'.'fld
            end
        if cy < 1 then do
            if f == 'g' then
                if symbol('m.a') \== 'VAR' then
                    return ''
            return a
            end
        cx = cy
        nxt = 1
        end
endProcedure vAdr

vAdrByM:
parse arg axx
    if axx = '' then
        return err('null address at' substr(na, cx) 'in' na)
    if symbol('m.axx') \== 'VAR' then
        return err('undef address' axx 'at' substr(na, cx) 'in' na)
    ayy = m.axx
    if ayy == '' then
          return err('null address at' substr(na, cx) 'in' na)
    return ayy
endProcedure vAdrByM

vIn: procedure expose m.
parse arg na
    if \ in() then
       return 0
    if na \== '' then
       call vPut na, m.in
    return 1
endProcedure vIn

vRead: procedure expose m.    /* old name ????????????? */
parse arg na
    say '||| please use vIn instead fo vIn'
    return vIn(na)

vHasKey: procedure expose m.
parse arg na
    return mapHasKey(v, na)

vRemove: procedure expose m.
parse arg na
    return mapRemove(v, na)
/* copy pipe end *****************************************************/