%ZOSV2 ;ISF/RWF MSC/JDS - More GT.M support routines ;24APR2009
 ;;8.0;KERNEL;**275,MSC**;Jul 10, 1995
 Q
 ;SAVE: DIE open array reference.
 ;      XCN is the starting value to $O from.
SAVE(RN) ;Save a routine
 N %,%F,%I,%N,SP,$ETRAP
 S $ETRAP="S $ECODE="""" Q"
 S %I=$I,SP=" ",%F=$$RTNDIR^%ZOSV()_RN_".m"
 O %F:(newversion:noreadonly:blocksize=2048:recordsize=2044) U %F
 F  S XCN=$O(@(DIE_XCN_")")) Q:XCN'>0  S %=@(DIE_XCN_",0)") Q:$E(%,1)="$"  I $E(%)'=";" W $P(%,SP)_$C(9)_$P(%,SP,2,99999),!
 C %F ;S %N=$$NULL
 ;C %N
 U %I
 ZLINK RN
 Q
NULL() ;Open and use null to hide talking.  Return open name
 ;Doesn't work for compile errors
 N %N S %N=$S($ZV["VMS":"NLA0:",1:"/dev/nul")
 O %N U %N
 Q %N
 ;
DEL(RN) ;Delete a routine file, both source and object.
 ; Since the actual routine may be somewhere in a search path, and may be shared
 ; with other environments, this places a routine in the first source directory in
 ; the search path, which, if executed, generates an error to the effect that the
 ; called routine doesn't exist (i.e., it's a way to effect a deletion without
 ; actually deleting the routine).
 N %N,%DIR,%I,$ETRAP,%F,%O,%S
 S $ETRAP="S $ECODE="""" Q"
 D SILENT^%RSEL(RN) S %S=%ZR(RN) ; %S now has the directory of the source
 D SILENT^%RSEL(RN,"OBJ") S %O=%ZR(RN)	  ; %O now has the directory of the object
 S %DIR=$$RTNDIR^%ZOSV		 ; %DIR now has the first source directory in $ZRO
 S %N=$TR(RN,"%","_")		 ; %N now has the file name for RN (sans extension)
 I $ZPARSE(%S)'=$ZPARSE(%DIR) D
 .S %I=$I,%F=%DIR_%N_".m"
 .O %F:(NEWVERSION) U %F W " ZM 150374338:$P($ZPOS,""^"",2) Q",! U %I C %F
 E  ZSY "rm -f "_%S_%N_".m "_%S_%N_".o"
 Q
 ;LOAD: DIF open array to receive the routine lines.
 ;      XCNP The starting index -1.
LOAD(RN) ;Load a routine
 N %
 S %N=0 F XCNP=XCNP+1:1 S %N=%N+1,%=$T(+%N^@RN) Q:$L(%)=0  S @(DIF_XCNP_",0)")=%
 Q
 ;
LOAD2(RN) ;Load a routine
 N %,%1,%F,%N,$ETRAP
 S %I=$I,%F=$$RTNDIR^%ZOSV()_$TR(RN,"%","_")_".m"
 O %F:(readonly):1 Q:'$T  U %F
 F XCNP=XCNP+1:1 R %1:1 Q:'$T!$ZEOF  S @(DIF_XCNP_",0)")=$TR(%1,$C(9)," ")
 C %F I $L(%I) U %I
 Q
 ;
RSUM(RN) ;Calculate a RSUM value
 N %,DIF,XCNP,%N,Y,$ETRAP K ^TMP("RSUM",$J)
 S $ETRAP="S $ECODE="""" Q"
 S Y=0,DIF="^TMP(""RSUM"",$J,",XCNP=0 D LOAD2(RN)
 F %=1,3:1 S %1=$G(^TMP("RSUM",$J,%,0)),%3=$F(%1," ") Q:'%3  S %3=$S($E(%1,%3)'=";":$L(%1),$E(%1,%3+1)=";":$L(%1),1:%3-2) F %2=1:1:%3 S Y=$A(%1,%2)*%2+Y
 K ^TMP("RSUM",$J)
 Q Y
 ;
TEST(RN) ;Special GT.M Test to see if routine is here.
 D SILENT^%RSEL(RN)
 Q $G(%ZR(RN))

