zOs/REXX.O08/ENV

/* copy env begin ******************************************************
***********************************************************************/
env: procedure expose m.
     nn = oNew("Env")
     m.nn.toClose = ''
     call envReset nn
     do ax=1 by 2 to arg()-1
         call envAddIo nn, arg(ax), arg(ax+1)
         end
     return nn
endProcedure env

envReset: procedure expose m.
parse arg m
     call envClose m
     m.m.in = ''
     m.m.out = ''
     m.m.lastCat = ''
     do ax=2 by 2 to arg()-1
         call envAddIo m, arg(ax), arg(ax+1)
         end
     return m
endProcedure envReset

envClose: procedure expose m.
parse arg m
     do wx=1 to words(m.m.toClose)
         call jClose word(m.m.toClose, wx)
         end
     m.m.toClose = ''
     return m
endProcedure envClose

envAddIO: procedure expose m.
parse arg m, opt, spec
    contX = pos("+", opt)
    if contX > 0 then do
        opt = left(opt, contX-1)substr(opt,contX+1)
        contX = 1
        if m.m.lastCat == '' then
            m.m.lastCat = cat()
        end
    if m.m.lastCat ^== '' then
        call catWriteAll m.m.lastCat, opt, spec
    else
        oc = catMake(opt, spec)
    if contX then
        return
    if m.m.lastCat ^== '' then do
        oc = m.m.lastCat
        m.m.lastCat = ''
        opt = left(m.oc.opts.1, 1)
        end
    o1 = left(opt, 1)
    if pos(o1, 'r<') > 0 then do
        if m.m.in ^== '' then
            call err 'envAddIo('opt',' spec') duplicate stdIn'
        m.m.in = oc
        end
    else if pos(o1, 'wa>') > 0 then do
        if m.m.out ^== '' then
            call err 'envAddIo('opt',' spec') duplicate stdOut'
        m.m.out = oc
        end
    if pos('-', opt) < 1 then do
        call jOpen oc, catOpt(opt)
        m.m.toClose = m.m.toClose oc
        end
    return m
endProcedure envAddIO

envLink: procedure expose m.
parse arg m, old
    if m.m.lastCat ^== '' then
        call err 'envLink with open cat'
    if m.m.in == '' then
        m.m.in = m.j.jIn
    if m.m.out == '' then
        m.m.out = m.j.jOut
    return m
endProcedure envLink

envReadWrite: procedure expose m.
    parse arg opt, rdr
    if opt = '' then
        call jWriteAll m.j.jOut, '-£', m.j.jIn
    else
        call jWriteAll m.j.jOut, opt, catMake(opt, rdr)
    return
endProcedure envReadWrite

envRead2Buf: procedure expose m.
    b = jBuf()
    call envPush env('>£', b)
    call envReadWrite
    x = envPop()
    return b
endProcedure envRead2Buf

envPreSuf: procedure expose m.
parse arg le, ri
    do while jIn(v)
        call jOut le || m.v || ri
        end
    return
endProcedure envPreSuf

envCatStr: procedure expose m.
parse arg mi, fo
    res = ''
    do while jIn(v)
        res = res || mi || fmt(m.v)
        end
    return substr(res, length(mi))
endProcedure envCatStr

envIsDefined: procedure expose m.
parse arg na
    return symbol('m.env.vars.na') == 'VAR'
endProcedure envIsDefined

envGet: procedure expose m.
parse arg na
    return mapGet(env.vars, na)

envRead: procedure expose m.
parse arg na
    return jIn('ENV.VARS.'na)

envHasKey: procedure expose m.
parse arg na
    return mapHasKey(env.vars, na)

envVia: procedure expose m.
parse arg na
    return mapVia(env.vars, na)

envPut: procedure expose m.
parse arg na, va
    return mapPut(env.vars, na, va)

envRemove: procedure expose m.
parse arg na
    return mapRemove(env.vars, na)

envIni: procedure expose m.
    if m.env.ini == 1 then
        return
    m.env.ini = 1
    call catIni

    call oDecMethods oNewClass("Env", "JRW"),
        , "jOpen  call err 'envOpen('m', 'arg')'",
        , "jReset return envReset(m, arg, arg(3), arg(4), arg(5))",
        , "jClose call envClose m"
    m.env.0 = 1
    call mapReset env.vars
    ex = env()
    m.env.1 = ex
    m.ex.in = m.j.jIn
    m.ex.out = m.j.jOut
    return
endProcedure envIni

envPush: procedure expose m.
parse arg e
    ex = m.env.0
    call envLink e, m.env.ex
    ex = ex + 1
    m.env.0 = ex
    m.env.ex = e
    m.j.jIn = m.e.in
    m.j.jOut = m.e.out
    return e
endProcedure envPush

envPop: procedure expose m.
    ox = m.env.0
    if ox <= 1 then
        call err 'envPop on empty stack' ox
    lazy = 0
    if wordPos(oGetClass(m.j.jOut), 'Cat CatWrite CatRead') > 0 then do
        e = m.env.ox
        lazy = catLazyClose(m.j.jOut, m.e.toClose)
        end
    if lazy then
        m.e.toClose = 'lazyDoNotClosePlease||||'
    else
        call envClose m.env.ox
    ex = ox - 1
    m.env.0 = ex
    e = m.env.ex
    m.j.jIn = m.e.in
    m.j.jOut = m.e.out
    return m.env.ox
endProcedure envPop

envBarBegin: procedure expose m.
    call envPush env('>£', Cat())
    return
endProcedure envBarBegin

envBar: procedure expose m.
    oldEnv = envPop()
    call envPush env('<£', m.oldEnv.out, '>£', Cat())
    return
endProcedure envBar

envBarLast: procedure expose m.
    oldEnv = envPop()
    call envPush env('<£', m.oldEnv.out)
    return
endProcedure envBarLast

envBarEnd: procedure expose m.
    oldEnv = envPop()
    return
endProcedure envBarEnd
/*--- return the output buffer of oRunner m --------------------------*/
envRun: procedure expose m.
    parse arg m
    b = jBuf()
    call envPush env('>£', b)
    call oRun m
    x = envPop()
    return b
endProcedure envRun

/* copy env end *******************************************************/