MiniM Knowledge Base | Sep 26, 2010
How to Intercept Output using STORE Device
Some tasks are required to store all output made by write commands for next transformation. This article describe an example how to intercept output using STORE device.
The STORE device write (or read) data of write and read commands into local or global variable. If device use text mode, each line is stored in separate subscript and output is stored line-by-line.
To get output into local or global variable for next transformation we must create the STORE device and specify variable name and first subscript to count subscripts. If this device is current, all output saves into variable was specified.
After end of interception code can use data of local or global variables as an ordinal variables.
Next we create an example for this interception to show routine compilation result in the HTML page. Main task is compilation result interception with following HTML entity replacing for correct HTML syntax. For example, if routine contains any syntax error, it is required to replace angle brackets in the diagnostic messages to appropriate HTML entities (< >).
To simplify example we move compilation, interception and transformation code from MWA page into separate routine with name STOREEXAM:
; example how to use STORE device compile(rouname) n saveio=$io,result n dev="|STORE|"_$na(result("")),ok,i o dev:("rwt") u dev ; next output is made into STORE device s ok=$v("rou","c",rouname) u saveio ; next output is made into prior device c dev s i="" f s i=$o(result(i)) q:i="" d . w $zcvt(result(i),"o","XML"),! q
This subroutine stores current device name, creates device with STORE type to intercept output into local variable result, executes routine compilation, restores current device and outputs compilation result with transformed HTML entities..
This example shows compilation result on the MWA page compile.mwa with the following content:
<html> <head> <title>Intercept routine compilation using STORE device</title> </head> <body> <table align=center width=570><tr><td> <?if $d(%KEY("routine"))?> <b>Compiling routine <?eval $g(%KEY("routine",1,1))?>:</b> <pre> <?x d compile^STOREEXAM($g(%KEY("routine",1,1)))?> </pre> <?endif?> <B>Available routines:</B> <P><?x s name=$o(^$R(""))?> <?while name'=""?> <a href="compile.mwa?routine=<?eval name?>"><?eval name?></a><br> <?endwhile s name=$o(^$R(name))?> <p>Click routine name to compile. </td></tr></table> </body> </html>
In this page the MWA tag if check an existance of page parameter routine and if one is present, outputs line about routine compilation and call subroutine for compile with interception and transformation.
Next this MWA page outputs all available in the current database routine names and for each routine make a hiperlink to pass a routine name as a page parameter.
On click by this hiperlink this routine name is passed as a page parameter, next MWA page pass routine nmae to subroutine for compilation with interception, subroutine pass all output into local variable result and after compilation outputs diagnostic messages into current MWA page device with HTML entities replacing. And finally this MWA page show correct HTML code with compilation result.
The STORE device can work in text as such as in binary mode. Current example use STORE device in text mode.Eugene Karataev