%ZTLOAD1 ;SEA/RDS-TaskMan: P I: Queue ;01/16/2007
 ;;8.0;KERNEL;**112,118,127,162,275,363,409,415,425**;Jul 10, 1995;Build 18
 ;
GET ;get task data
 N %X,%Y,X,Y,X1,ZT,ZTC1,ZTC2,ZTA1,ZTA4,ZTA5,ZTINC,ZTGOT,ZTC34P
 K %ZTLOAD
 I ("^"[$G(ZTRTN))!($L($G(ZTRTN),"^")>2) D REJECT^%ZTLOAD2("Bad Routine") G EXIT
 S U="^" I ZTRTN'[U S ZTRTN=U_ZTRTN
 S ZTC1=$G(DUZ),ZTC2=""
 I ZTC1>0 S ZTC2=$P($G(^VA(200,ZTC1,0)),U)
 ;Check Date/Time
1 I $D(ZTDTH)[0 S ZTDTH=""
 I ZTDTH?7N.".".N S ZTDTH=$$FMTH^%ZTLOAD7(ZTDTH)
 I $P($G(XQY0),U,18) D RESTRCT^%ZTLOAD2
 I ZTDTH'="@",ZTDTH'?1.5N1","1.5N D ASK^%ZTLOAD2 I ZTDTH'>0 D REJECT^%ZTLOAD2("Bad Date/Time") G EXIT
 ;
 S ZTA1="R",ZTA4="",ZTA5=""
 I ZTRTN="ZTSK^XQ1" D OPTION^%ZTLOAD2 I ZTA1="" D REJECT^%ZTLOAD2("Bad Option") G EXIT
 I ZTA1="R" D
 . S ZTSAVE("XQY")="",ZTSAVE("XQY0")="",ZTA4=$G(XQY),ZTA5=$P($G(XQY0),U)
 ;
 D GETENV^%ZOSV S ZTC34P=Y
 ;Description
2 I $D(ZTDESC)[0 S ZTDESC="No Description (%ZTLOAD)"
 ;
 I $G(ZTKIL)]"" D ZTKIL^%ZTLOAD2
 S:$G(ZTUCI)["," ZTUCI=$P(ZTUCI,",") S:$G(ZTCPU)["," ZTCPU=$P(ZTCPU,",",2)
DEVICE ;get device data
 I $D(ZTIO)#2,$G(ION)=$P(ZTIO,";"),$G(IOT)="SPL" D SPOOL^%ZTLOAD2
 ;If no ZTIO, build from symbol table
 I $D(ZTIO)[0 S ZTIO=$G(ION) I $L(ZTIO) D
 . S:$G(IOST)]"" $P(ZTIO,";",2)=IOST
 . I $G(IO("DOC"))]"" S ZTIO=ZTIO_";"_IO("DOC")
 . E  I $G(IOM)]"" S ZTIO=ZTIO_";"_IOM I $G(IOSL)]"" S ZTIO=ZTIO_";"_IOSL
 . Q
 ;
 I $E(ZTIO,1)="`" S $P(ZTIO,";")=$P(^%ZIS(1,+$E(ZTIO,2,99),0),"^") ;Convert `IEN format
 S ZTIO(1)=$S($G(ZTIO(1))'="D":"Q",1:"DIRECT")
 I $L(ZTIO) D  ;Skip if no device
 . ;IO("HFSIO") and IOPAR are how %ZIS reports the user selected file name and parameters
 . S:'$D(ZTIO("H")) ZTIO("H")=$G(IO("HFSIO"))
 . S:'$D(ZTIO("P")) ZTIO("P")=$G(IOPAR)
 . I $G(IO("P"))]"",ZTIO'[";/" S ZTIO=ZTIO_";/"_IO("P")
 . I $$NOQ^%ZISUTL($P(ZTIO,";")) D BADDEV^%ZTLOAD2("Restricted Device")
 . I $E(ZTIO,1,9)="P-MESSAGE" S ZTSAVE("^TMP(""XM-MESS"",$J,")=""
 . Q
 ;
 I $D(%ZTLOAD("ERROR")) G EXIT
 ;
 ;See that ^%ZTSK(-1) is set
 I $D(^%ZTSK(-1))[0 S ^%ZTSK(-1)=$S($P($G(^%ZTSK(0)),U,3):$P(^(0),U,3),1:1000)
RECORD ;build record
 S ZTINC=$G(^%ZOSF("$INC"),1) ;Set to 1 if this system has $INCREMENT, otherwise 0.
 S ZTGOT=0
 I 'ZTINC D  ;For System that don't have $INC (GT.M, DTM, MSM)
 . ;Find a free entry, Claim it and Lock it.
 . L +^%ZTSK(-1):0 S ZTSK=^%ZTSK(-1) ;This is just a starting point
 . F  S ZTSK=ZTSK+1 I '$D(^%ZTSK(ZTSK)) D  Q:ZTGOT
 . . L +^%ZTSK(ZTSK):0 Q:'$T  ;Can we lock it
 . . I $D(^%ZTSK(ZTSK)) L -^%ZTSK(ZTSK) ;Already claimed
 . . S ^%Z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