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?
Comments
I wonder if I can use JOB to invoke a routine and specify a file as the input device. Does anybody know how to do this?
Not sure if this is quite matching your use case, but OPEN and USE are what allow for redirection of standard Input and Output
Docs are at https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…
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!Thanks for this code.
However when I try to compile the code I get errors with the "labels"
Loading file redirectio.cls as udl
Compiling class utility.redirectio
Compiling routine utility.redirectio.1
ERROR: utility.redirectio.cls(Test+31) #1026: Invalid command : 'rchr(time)' : Offset:6 [zTest+25^utility.redirectio.1]
TEXT: rchr(time) quit "a"
ERROR: utility.redirectio.cls(Test+33) #1026: Invalid command : 'rstr(len,time)' : Offset:6 [zTest+27^utility.redirectio.1]
TEXT: rstr(len,time) quit "xyz"
ERROR: utility.redirectio.cls(Test+35) #1026: Invalid command : 'wchr(s)' : Offset:6 [zTest+29^utility.redirectio.1]
TEXT: wchr(s) do output($char(s)) quit
ERROR: utility.redirectio.cls(Test+37) #1026: Invalid command : 'wff()' : Offset:5 [zTest+31^utility.redirectio.1]
TEXT: wff() do output($char(12)) quit
ERROR: utility.redirectio.cls(Test+39) #1026: Invalid command : 'wnl()' : Offset:5 [zTest+33^utility.redirectio.1]
TEXT: wnl() do output($char(13,10)) quit
ERROR: utility.redirectio.cls(Test+41) #1026: Invalid command : 'wstr(s)' : Offset:6 [zTest+35^utility.redirectio.1]
TEXT: wstr(s) do output(s) quit
ERROR: utility.redirectio.cls(Test+43) #1026: Invalid command : 'wtab(s)' : Offset:6 [zTest+37^utility.redirectio.1]
TEXT: wtab(s) do output($char(9)) quit
ERROR: utility.redirectio.cls(Test+46) #1026: Invalid command : 'output(s)' : Offset:8 [zTest+40^utility.redirectio.1]
TEXT: output(s) set str=str_s quit
Detected 8 errors during load.
How can this be fixed?
check if your lines start with the labels or if you have some leading <blank> or <tab>
if so it is interpreted as function call instead of a label
Thanks.
There was 1 space before each line
Compiles OK now