Question
· Sep 19

Input Redirection

Hello, I try to develop a REST interface where I need to interact with legacy MUMPS routines. How can I pass in input to a Read without modifying the legacy code?

I think in linux I can execute command < inputfile to read from file, but how does it work in ObjectScript?

Product version: IRIS 2022.3
Discussion (4)3
Log in or sign up to continue

Presuming that you're trying to script input into a menu-driven routine to generate data or perform some action, the best course of action would be to analyze the code of the legacy menu routine and see what it is calling to actually generate data or perform the action based on the menu input and have your endpoint call that code directly with the proper inputs from your REST endpoint, bypassing the need for the menu and any scripted input. 

My recommended approach is to call routines in a silent mode if at all possible, or to do minimal modifications to add silent mode. But here's how you can work with read using input redirection:

ClassMethod Test() [ ProcedureBlock = 0 ]
{
    set tOldIORedirected = ##class(%Device).ReDirectIO()
    set tOldMnemonic = ##class(%Device).GetMnemonicRoutine()
    set tOldIO = $io
    try {
        set str=""
        //Redirect IO to the current routine - makes use of the labels defined below
        use $io::("^"_$ZNAME)

        //Enable redirection
        do ##class(%Device).ReDirectIO(1)

        set x = ..MyLegacyRoutine()
    } catch ex {
        do ex.Log()
    }

    //Return to original redirection/mnemonic routine settings
    if (tOldMnemonic '= "") {
        use tOldIO::("^"_tOldMnemonic)
    } else {
        use tOldIO
    }
    do ##class(%Device).ReDirectIO(tOldIORedirected)
    
    w !,"x is: ",x,!
    w "Routine wrote to device: ", str

    //Labels that allow for IO redirection
    //Read Character
rchr(time)  quit "a"
    //Read a string
rstr(len,time) quit "xyz"
    //Write a character - call the output label
wchr(s)      do output($char(s))  quit
    //Write a form feed - call the output label
wff()        do output($char(12))  quit
    //Write a newline - call the output label
wnl()        do output($char(13,10))  quit
    //Write a string - call the output label
wstr(s)      do output(s)  quit
    //Write a tab - call the output label
wtab(s)      do output($char(9))  quit
    //Output label - this is where you would handle routine device output.
    //in our case, we want to write to str
output(s)    set str=str_s   quit
}

ClassMethod MyLegacyRoutine()
{
    read "Input x: ",x
    write "Hello!"
    return x
}

}

 It outputs:

x is: xyz
Routine wrote to device: Input x: Hello!