From:	CSBVAX::MRGATE!@KL.SRI.Com,@wiscvm.wisc.edu,@YMIR.BITNET:PORTIA@ENGVAX.SCG.HAC.COM@SMTP 25-JUL-1987 14:25
To:	EVERHART
Subj:	virtual file editor (VFE) source (2 of 5) (50 blocks)

Received: from wiscvm.wisc.edu by KL.SRI.COM with TCP; Sat 25 Jul 87 10:40:17-PDT
Received: from YMIR.BITNET by wiscvm.wisc.edu ; Sat, 25 Jul 87 12:38:22 CDT
Received: from ENGVAX.SCG.HAC.COM by YMIR.BITNET; Sat, 25 Jul 87 10:28 PDT
Date: Sat, 25 Jul 87 10:14 PDT
From: Portia 616-2635 <@YMIR.BITNET:PORTIA@ENGVAX.SCG.HAC.COM>
Subject: virtual file editor (VFE) source (2 of 5) (50 blocks)
To: info-vax@sri-kl.ARPA
X-VMS-To: INFO-VAX-SUBMIT

$ show default
$ check_sum = 1533430152
$ write sys$output "Creating FILEHDLR.MAR"
$ create FILEHDLR.MAR
$ DECK/DOLLARS="$*$*EOD*$*$"
        .TITLE  FILEHDLR File manipulation routines

        .ENABLE SUPPRESSION

        .LIBRARY  'SYS$LIBRARY:LIB.MLB'
        .LIBRARY  'VFELIB'

        $ATRDEF
        $DCDEF
        $DEVDEF
        $DVIDEF
        $FABDEF
        $FATDEF
        $FIBDEF
        $IODEF
        $MTDEF
        $NAMDEF

        .PSECT  CODE,EXE,NOWRT
        .PAGE
        .SBTTL  GETFILE - Get file name and access it
GETFILE::
        CLRL    FLFLGS
        TSTW    DESC
        BEQL    150$
        BSBW    FPARSE
        BLBC    R0,200$
        TSTL    SETMASK
        BEQL    70$
        CLRL    R1
        BSBW    SETCMD
70$:
        TSTW    FDESC
        BGTR    400$
        BRB     150$
100$:
        CLRL    FLFLGS
150$:
        INPUT   FNQ,FNQ+8,#80,FNAME
        MOVAL   FNAME,DESC+4
        MOVW    TSTATUS+2,DESC
        CLRB    INITFLG
        BSBW    FPARSE
        BLBC    R0,200$
        TSTW    FDESC
        BGTR    400$
200$:
        TSTB    OPNFLG
        BEQL    100$
        BRW     900$
400$:
        CLRB    INITFLG
        BSBW    RELFILE
        CLRB    LOGFLG
        CLRB    SEQFLG
        CLRB    TAPFLG
        MOVL    #IO$_READVBLK,READFNC
        MOVL    #IO$_WRITEVBLK,WRITEFNC
        MOVL    #1,LOBLK
        MOVAL   FABBLK,R2
        MOVB    FDESC,FAB$B_FNS(R2)
        MOVL    FDESC+4,FAB$L_FNA(R2)
        $PARSE  FAB=FABBLK
        BLBS    R0,500$
        CMPL    R0,#RMS$_DNF
        BEQL    500$
        BRW     780$
500$:
        MOVAL   NAMBLK,R6
;
;       This is an unorthodox method to determine if only a device name
;       is specified, but it seems to work.  If the bit definitions
;       of NAM$L_FNB are ever changed, this instruction may require
;       modification.
;
        CMPW    #NAM$M_EXP_DEV,NAM$L_FNB(R6)
;
        BNEQ    520$
        INCB    LOGFLG
        MOVZBL  NAM$B_DEV(R6),CURNAM
        MOVC3   CURNAM,@NAM$L_DEV(R6),CURNAM+2
        ADDW2   #2,CURNAM
        BRB     540$
520$:
        BLBS    R0,540$
        BRW     780$
540$:
        BITL    #NAM$M_WILDCARD,NAM$L_FNB(R6)
        BEQL    550$
        MOVL    #RMS$_FNM,R0
        BRW     780$
550$:
        BITL    #NAM$M_NODE,NAM$L_FNB(R6)
        BEQL    570$
        OUTMSGC NNWMSG
        BRW     100$
570$:
        MOVB    NAM$B_DEV(R6),DISKD
        MOVL    NAM$L_DEV(R6),DISKD+4
        $ASSIGN_S  CHAN=DD,DEVNAM=DISKD
        BLBS    R0,600$
        BRW     780$
600$:
        MOVB    #1,ASSFLG
        $GETDVI_S  EFN=#0,CHAN=DD,ITMLST=DEVLST,IOSB=FSTATUS
        BLBS    R0,610$
        BRW     780$
610$:
        $WAITFR_S  EFN=#0
        BSBW    IOSTAT
        BLBS    R0,620$
        BRW     780$
620$:
        CMPL    #DC$_DISK,DEVCLS
        BNEQ    625$
        BRW     680$
625$:
        CMPL    #DC$_TAPE,DEVCLS
        BEQL    630$
        OUTMSGC ICDMSG
        BRW     790$
630$:
        TSTB    LOGFLG
        BNEQ    640$
        OUTMSGC TAP1MSG
        BRW     790$
640$:
        BITL    #DEV$M_MNT,DEVCHR
        BEQL    645$
        BITL    #DEV$M_FOR,DEVCHR
        BNEQ    650$
645$:
        OUTMSGC TAP2MSG
        BRW     790$
650$:
        TSTW    OVRWRT
        BEQL    655$
        OUTMSGC TAP3MSG
        BRW     790$
655$:
        MOVB    #1,TAPFLG
        MOVL    #IO$_READLBLK,READFNC
        CLRL    LOBLK
        MOVL    #9999999,HIBLK
        MOVL    #9999999,EFBLK
        MOVC5   #0,FILBLK,#^XFF,#1024,FILBLK
        TSTB    POSFLG
        BNEQ    665$
        TSTB    NOREW
        BNEQ    660$
        BSBW    REWIND
        BLBS    R0,670$
        BRW     790$
660$:
        OUTMSGC TAP4MSG
        CLRL    CURFIL
        MNEGL   #1,CURBLK
        MOVL    #1,PARA1
        BRB     668$
665$:
        MOVL    #1,CURFIL
        BSBW    SETPOS
        CLRL    PARA1
668$:
        CLRB    CUREOF
        MOVB    #1,PRTEOF
        BSBW    READTAPE
        BLBS    R0,670$
        BRW     900$
670$:
        BRW     870$
680$:
        CMPL    BLKSIZ,#512
        BEQL    681$
        OUTMSGC NSBMSG
        BRW     790$
681$:
        TSTB    LOGFLG
        BNEQ    682$
        BRW     700$
682$:
        MOVL    #IO$_READLBLK,READFNC
        CLRL    LOBLK
        SUBL3   #1,MAXBLK,HIBLK
        MOVL    HIBLK,EFBLK
        MOVL    #511,FFBYTE
        TSTB    WRTFLG
        BNEQ    685$
        BRW     840$
685$:
        BITL    #DEV$M_FOR,DEVCHR
        BEQL    690$
        MOVL    #IO$_WRITELBLK,WRITEFNC
        OUTMSGC WWED
        BRW     850$
690$:
        OUTMSGC NWLMSG
        BRW     790$
700$:
        $SEARCH FAB=FABBLK
        BLBS    R0,720$
        BRW     780$
720$:
        MOVL    NAM$W_FID(R6),FIB+FIB$W_FID
        MOVW    NAM$W_FID+4(R6),FIB+FIB$W_FID+4
        CLRL    FIB+FIB$L_ACCTL
        TSTB    WRTFLG
        BEQL    750$
        MOVL    #FIB$M_WRITE,FIB+FIB$L_ACCTL
750$:
        $QIOW_S CHAN=DD,FUNC=#IO$_ACCESS!IO$M_ACCESS, -
                IOSB=FSTATUS,P1=FIBD,P5=#ATRLST
        BSBW    IOSTAT
        BLBS    R0,800$
        CMPW    R0,#SS$_ACCONFLICT
        BNEQ    780$
        TSTB    OVRFLG
        BEQL    780$
        MOVL    #FIB$M_NOLOCK,FIB+FIB$L_ACCTL
        $QIOW_S CHAN=DD,FUNC=#IO$_ACCESS!IO$M_ACCESS, -
                IOSB=FSTATUS,P1=FIBD,P5=#ATRLST
        BSBW    IOSTAT
        BLBC    R0,780$
        MOVL    #SS$_ACCONFLICT,R0
        BSBW    ERROUT
        BRB     800$
780$:
        BSBW    ERROUT
790$:
        BRW     100$
800$:
        MOVB    #1,ACCFLG
        MOVW    RECATTR+FAT$W_HIBLKL,HIBLK
        MOVW    RECATTR+FAT$W_HIBLKH,HIBLK+2
        TSTL    HIBLK
        BNEQ    810$
        OUTMSGC NEF
        BRW     100$
810$:
        MOVZBL  NAM$B_RSL(R6),CURNAM
        MOVC3   CURNAM,@NAM$L_RSA(R6),CURNAM+2
        ADDW2   #2,CURNAM
        MOVW    RECATTR+FAT$W_EFBLKL,EFBLK
        MOVW    RECATTR+FAT$W_EFBLKH,EFBLK+2
        CVTWL   RECATTR+FAT$W_FFBYTE,FFBYTE
        CVTWL   RECATTR+FAT$W_RSIZE,RSIZE
        CMPZV   #FAT$V_FILEORG,#4,RECATTR+FAT$B_RTYPE,#FAT$C_SEQUENTIAL
        BNEQ    815$
        INCB    SEQFLG
        TSTL    RSIZE
        BNEQ    812$
        MOVL    #140,RSIZE
812$:
        TSTL    FFBYTE
        BNEQ    815$
        CMPL    EFBLK,#1
        BGTR    815$
        MOVL    HIBLK,EFBLK
        MOVL    #511,FFBYTE
815$:
        CLRB    VFLAG
        CLRL    VFCSIZE
        CMPZV   #FAT$V_RTYPE,#4,RECATTR+FAT$B_RTYPE,#FAT$C_VARIABLE
        BEQL    820$
        CMPZV   #FAT$V_RTYPE,#4,RECATTR+FAT$B_RTYPE,#FAT$C_VFC
        BNEQ    830$
        CVTBL   RECATTR+FAT$B_VFCSIZE,VFCSIZE
820$:
        INCB    VFLAG
830$:
        EXTZV   #FAT$V_NOSPAN,#1,RECATTR+FAT$B_RATTRIB,NOSPNFLG
        TSTB    WRTFLG
        BEQL    840$
        OUTMSGC WWE
        BRB     850$
840$:
        OUTMSG  #ROML,ROM
850$:
        CLRL    CURBLK
        MOVL    LOBLK,PARA1
        BSBW    READDISK
        BLBS    R0,870$
        CMPL    R0,#SS$_NOPRIV
        BNEQ    900$
        BRW     100$
870$:
        BSBW    BLOCK
900$:
        MOVB    #1,OPNFLG
        RSB

        .SBTTL  RELFILE - Deaccess file
RELFILE::
        TSTB    ACCFLG
        BEQL    150$
        $QIOW_S CHAN=DD,FUNC=#IO$_DEACCESS,P1=FIBD
        BLBS    R0,150$
        BRW     ERREXT
150$:
        CLRB    ACCFLG
        TSTB    ASSFLG
        BEQL    170$
        $DASSGN_S  CHAN=DD
        BLBS    R0,170$
        BRW     ERREXT
170$:
        CLRB    ASSFLG
        CLRB    OPNFLG
        RSB
        .PAGE
        .SBTTL  TOP - Go to top of file
TOP::
        TSTB    TAPFLG
        BNEQ    100$
        MOVL    LOBLK,PARA1
        BSBW    READDISK
        BRB     200$
100$:
        TSTB    CUREOF
        BNEQ    200$
        MNEGL   #9999999,PARA1
        CLRB    PRTEOF
        BSBW    READTAPE
200$:
        RSB

        .SBTTL  NEXT - Go to next block
NEXT::
        TSTB    TAPFLG
        BNEQ    200$
        MOVL    BUFFCT,R1
        TSTL    PARA1
        BLEQ    100$
        DIVL3   #512,CURBCT,R1
100$:
        MULL2   R1,PARA1
        ADDL2   CURBLK,PARA1
        BSBW    READDISK
        BRB     300$
200$:
        MOVB    #1,PRTEOF
        BSBW    READTAPE
300$:
        RSB
        .PAGE
        .SBTTL  EOF - Position at end-of-file block
EOF::
        TSTB    TAPFLG
        BEQL    30$
        BRW     100$
30$:
        MOVL    #100,OUTDSC
        MOVAL   EFBLKM,DESC+4
        TSTB    HEXFLG
        BEQL    50$
        MOVAL   EFBLKMX,DESC+4
50$:
        $FAO_S  CTRSTR=@DESC+4,OUTLEN=OUTDSC,OUTBUF=OUTDSC,-
                P1=EFBLK,P2=FFBYTE
        OUTMSG  OUTDSC,OUT_BUFF
        MOVL    EFBLK,PARA1
        CMPL    PARA1,HIBLK
        BLEQ    80$
        MOVL    HIBLK,PARA1
80$:
        BSBW    READDISK
        BRB     200$
100$:
        BSBW    LAST
200$:
        RSB

        .SBTTL  LAST - Position at last block in file
LAST::
        TSTB    TAPFLG
        BNEQ    100$
        MOVL    HIBLK,PARA1
        BSBW    READDISK
        BRB     200$
100$:
        MOVL    #9999999,PARA1
        CLRB    PRTEOF
        BSBW    READTAPE
        CMPL    R0,#SS$_ENDOFFILE
        BNEQ    200$
        MNEGL   #1,PARA1
        MOVB    #1,PRTEOF
        BSBW    READTAPE
200$:
        RSB
        .PAGE
        .SBTTL  READ - Read data
READ::
        TSTB    TAPFLG
        BNEQ    180$
        CMPL    PARA1,HIBLK
        BLEQ    150$
        TSTB    LOGFLG
        BNEQ    130$
        BSBW    READDISK
        BLBS    R0,400$
130$:
        MOVL    HIBLK,PARA1
150$:
        BSBW    READDISK
        BRB     400$
180$:
        TSTL    CURBLK
        BGEQ    200$
        TSTL    PARA1
        BLSS    200$
        OUTMSGC NOABS
        CLRL    R0
        BRB     400$
200$:
        SUBL2   CURBLK,PARA1
        MOVB    #1,PRTEOF
        BSBW    READTAPE
400$:
        RSB

        .SBTTL  READINT - Internal read
;
; R1=Block number, R2=Byte count
;
READINT::
        ADDL3   #BUFF,CURBCT,READLOC
        TSTB    TAPFLG
        BNEQ    100$
        $QIOW_S CHAN=DD,FUNC=READFNC,IOSB=FSTATUS,-
                P1=@READLOC,P2=R2,P3=R1
        BRB     200$
100$:
        $QIOW_S CHAN=DD,FUNC=#IO$_READLBLK,IOSB=FSTATUS,-
                P1=@READLOC,P2=#MAXBCT
200$:
        BSBW    IOSTAT
        MOVZWL  FSTATUS+2,NXTBCT
        RSB
        .PAGE
        .SBTTL  READDISK - Read disk blk, PARA1=address
READDISK:
        MOVL    BUFFCT,R1
        CMPL    PARA1,LOBLK
        BGEQ    100$
        MOVL    LOBLK,PARA1
        BRB     200$
100$:
        TSTB    LOGFLG
        BEQL    200$
        SUBL3   PARA1,HIBLK,R1
        BGEQ    150$
        MOVL    #SS$_ENDOFFILE,R0
        CLRL    R1
        BRB     250$
150$:
        INCL    R1
        CMPL    R1,BUFFCT
        BLEQ    200$
        MOVL    BUFFCT,R1
200$:
        MULL2   #512,R1
        $QIOW_S CHAN=DD,FUNC=READFNC,IOSB=FSTATUS,-
                P1=BUFF,P2=R1,P3=PARA1
        DIVL3   #512,FSTATUS+2,R1
        BSBW    IOSTAT
        BLBS    R0,300$
        TSTL    R1
        BEQL    250$
        CMPL    R0,#SS$_ENDOFFILE
        BEQL    300$
250$:
        MOVL    R0,SVSTAT
        BSBW    ERROUT
        TSTL    R1
        BGTR    300$
        MOVL    SVSTAT,R0
        CMPL    PARA1,HIBLK
        BLEQ    280$
        BRW     400$
280$:
        MOVL    #512,CURBCT
        BRB     350$
300$:
        MOVL    #1,R0
        TSTL    R1
        BGTR    310$
        MOVL    #1,R1
310$:
        MULL3   #512,R1,CURBCT
        DECL    R1
        ADDL2   PARA1,R1
        CMPL    R1,HIBLK
        BLEQ    350$
        MOVL    R1,HIBLK
        MOVL    R1,EFBLK
        MOVL    #511,FFBYTE
350$:
        MOVL    PARA1,CURBLK
        CLRL    LPTR
        MNEGL   #1,RECPTR
400$:
        RSB

        .SBTTL  WRITEDISK - Write work buffer to disk
WRITEDISK::
        TSTB    WRTFLG
        BNEQ    100$
        OUTMSG  #ROMWL,ROM
        BRB     900$
100$:
        $QIOW_S CHAN=DD,FUNC=WRITEFNC,IOSB=FSTATUS,-
                P1=BUFF,P2=CURBCT,P3=CURBLK
        BSBW    IOSTAT
        BLBS    R0,200$
        BSBW    ERROUT
        BRB     900$
200$:
        OUTMSGC RDY
900$:

        RSB
        .PAGE
        .SBTTL  REWIND - Rewind a tape
REWIND::
        TSTB    TAPFLG
        BNEQ    100$
        OUTMSGC TAPONLY
        BRB     300$
100$:
        $QIOW_S CHAN=DD,FUNC=#IO$_REWIND,IOSB=FSTATUS
        BSBW    IOSTAT
        BLBS    R0,200$
        BSBW    ERROUT
        CLRL    R0
        BRB     300$
200$:
        MOVL    #1,CURFIL
        CLRL    CURBLK
        CLRB    CUREOF
        MOVL    #1,PARA1
        MOVB    #1,PRTEOF
        BSBW    READTAPE
        MOVL    #1,R0
300$:
        RSB

        .SBTTL  BACKSPACE - Move a tape back one block or EOF
BACKSPACE::
        TSTB    TAPFLG
        BEQL    100$
        BITL    #MT$M_LOST,FSTATUS+4
        BNEQ    100$
        $QIOW_S CHAN=DD,FUNC=#IO$_SKIPRECORD,IOSB=FSTATUS,-
                P1=-1
        BSBW    IOSTAT
        BLBS    R0,100$
        CMPL    R0,#SS$_ENDOFFILE
        BEQL    100$
        BSBW    ERROUT
100$:
        RSB
        .PAGE
        .SBTTL  MOVE - Move over 1 or more EOF marks
MOVE::
        TSTB    TAPFLG
        BNEQ    50$
        OUTMSGC TAPONLY
        BRW     900$
50$:
        MOVL    PARA1,R2
        BGTR    100$
        BEQL    70$
        BRW     200$
70$:
        BRW     900$
100$:
        TSTB    STOP
        BNEQ    140$
        $QIOW_S CHAN=DD,FUNC=#IO$_SKIPFILE,IOSB=FSTATUS,-
                P1=1
        BSBW    IOSTAT
        BLBS    R0,120$
        CMPL    R0,#SS$_ENDOFFILE
        BEQL    120$
        BSBW    ERROUT
        CLRL    CURBCT
        BRB     140$
120$:
        INCL    CURFIL
        CLRL    CURBLK
        CLRL    CURBCT
        MOVB    #1,CUREOF
        SOBGTR  R2,100$
140$:
        BRW     900$
200$:
        TSTB    STOP
        BEQL    220$
        BRW     900$
220$:
        $QIOW_S CHAN=DD,FUNC=#IO$_SKIPFILE,IOSB=FSTATUS,-
                P1=-1
        BSBW    IOSTAT
        BLBS    R0,240$
        CMPL    R0,#SS$_ENDOFFILE
        BEQL    240$
        BSBW    ERROUT
        BRB     250$
240$:
        BITL    #MT$M_BOT,FSTATUS+4
        BEQL    270$
        OUTMSGC BOTMSG
        CLRL    CURBLK
        MOVB    #1,CUREOF
250$:
        CLRL    CURBCT
        BRW     900$
270$:
        DECL    CURFIL
        MOVZBL  CURFIL,R1
        MOVL    FILBLK[R1],CURBLK
        CLRB    CUREOF
        CLRL    CURBCT
        AOBLSS  #0,R2,300$
        BRB     400$
300$:
        BRW     200$
400$:
        CLRL    PARA1
        CLRB    PRTEOF
        BSBW    READTAPE
900$:
        RSB
        .PAGE
        .SBTTL  READTAPE - Read tape blk, PARA1 = offset
READTAPE:
        CMPL    PARA1,#1
        BGTR    100$
        BEQL    50$
        BRW     200$
50$:
        BRW     510$
100$:
        SUBL3   #1,PARA1,SKIPTOT
110$:
        MOVL    SKIPTOT,SKIPCT
        SUBL2   SKPINC,SKIPTOT
        BLEQ    120$
        MOVL    SKPINC,SKIPCT
120$:
        $QIOW_S CHAN=DD,FUNC=#IO$_SKIPRECORD,IOSB=FSTATUS,-
                P1=@SKIPCT
        BSBW    IOSTAT
        MOVZWL  FSTATUS+2,R1
        BLBC    R0,140$
        ADDL2   R1,CURBLK
        TSTL    SKIPTOT
        BLEQ    130$
        TSTB    STOP
        BEQL    110$
130$:
        BRW     500$
140$:
        TSTB    BUGFLG
        BNEQ    145$
        DECL    R1
        BLEQ    145$
        ADDL2   R1,CURBLK
145$:
        CMPL    R0,#SS$_ENDOFFILE
        BNEQ    180$
        TSTB    BUGFLG
        BEQL    150$
        CMPL    SKIPCT,#1
        BGTR    160$
150$:
        MOVZBL  CURFIL,R1
        MOVL    CURBLK,FILBLK[R1]
160$:
        BRW     600$
180$:
        BRW     900$
200$:
        TSTB    CUREOF
        BNEQ    220$
        SUBL3   #1,PARA1,SKIPTOT
        BRW     300$
220$:
        TSTL    PARA1
        BNEQ    230$
        MOVL    #1,R0
        BRW     950$
230$:
        $QIOW_S CHAN=DD,FUNC=#IO$_SKIPRECORD,IOSB=FSTATUS,-
                P1=-1
        BSBW    IOSTAT
        BLBC    R0,250$
        BITL    #MT$M_BOT,FSTATUS+4
        BEQL    240$
        BRW     400$
240$:
        DECL    CURBLK
        BRB     270$
250$:
        CMPL    R0,#SS$_ENDOFFILE
        BEQL    260$
        BRW     450$
260$:
        DECL    CURFIL
        MOVZBL  CURFIL,R1
        MOVL    FILBLK[R1],CURBLK
270$:
        CLRB    CUREOF
        MOVL    PARA1,SKIPTOT
300$:
        MOVL    SKIPTOT,SKIPCT
        ADDL2   #50,SKIPTOT
        BGEQ    320$
        MNEGL   #50,SKIPCT
320$:
        $QIOW_S CHAN=DD,FUNC=#IO$_SKIPRECORD,IOSB=FSTATUS,-
                P1=@SKIPCT
        BSBW    IOSTAT
        BLBC    R0,350$
        BITL    #MT$M_BOT,FSTATUS+4
        BNEQ    400$
        MOVZWL  FSTATUS+2,R1
        SUBL2   R1,CURBLK
        TSTL    SKIPTOT
        BGEQ    330$
        TSTB    STOP
        BEQL    300$
330$:
        BRW     500$
350$:
        CMPL    R0,#SS$_ENDOFFILE
        BNEQ    450$
        BITL    #MT$M_BOT,FSTATUS+4
        BNEQ    400$
        DECL    CURFIL
        MOVZBL  CURFIL,R1
        MOVL    FILBLK[R1],CURBLK
        BRW     500$
400$:
        OUTMSGC BOTMSG
        CLRL    CURBLK
        MOVB    #1,CUREOF
        CLRL    CURBCT
        MOVL    #SS$_ENDOFFILE,R0
        BRW     950$
450$:
        MOVZWL  FSTATUS+2,R1
        DECL    R1
        BGTR    460$
        BRW     900$
460$:
        SUBL2   R1,CURBLK
        CLRB    CUREOF
        BRW     900$
500$:
        CLRL    CURBCT
510$:
        $QIOW_S CHAN=DD,FUNC=#IO$_READLBLK,IOSB=FSTATUS,-
                P1=BUFF,P2=#MAXBCT
        BSBW    IOSTAT
        MOVZWL  FSTATUS+2,CURBCT
        BLBC    R0,520$
        INCL    CURBLK
        CLRB    CUREOF
        BRW     950$
520$:
        CMPL    R0,#SS$_ENDOFFILE
        BNEQ    800$
        MOVZBL  CURFIL,R1
        MOVL    CURBLK,FILBLK[R1]
600$:
        TSTB    PRTEOF
        BEQL    620$
        OUTMSGC EOFMSG
620$:
        CLRL    CURBCT
        CLRL    CURBLK
        INCL    CURFIL
        MOVB    #1,CUREOF
        MOVL    #SS$_ENDOFFILE,R0
        BRB     950$
800$:
        TSTL    CURBCT
        BGTR    850$
        BITL    #MT$M_LOST,FSTATUS+4
        BNEQ    900$
850$:
        INCL    CURBLK
900$:
        MOVL    R0,SVSTAT
        BSBW    ERROUT
        MOVL    SVSTAT,R0
950$:
        CLRL    LPTR
        MNEGL   #1,RECPTR
        RSB
        .PAGE
        .SBTTL  SHOFILE - Display current file and block
SHOFILE::
        SUBW3   #2,CURNAM,DESC
        MOVAL   CURNAM+2,DESC+4
        MOVL    #150,OUTDSC
        TSTB    LOGFLG
        BNEQ    100$
        $FAO_S  CTRSTR=WHRMSG,OUTLEN=OUTDSC,OUTBUF=OUTDSC,-
                P1=#DESC,P2=CURBLK
        BRB     200$
100$:
        $FAO_S  CTRSTR=WHDMSG,OUTLEN=OUTDSC,OUTBUF=OUTDSC,-
                P1=#DESC,P2=CURBLK
200$:
        OUTMSG  OUTDSC,OUT_BUFF
        RSB

IOSTAT:
        BLBC    R0,100$
        MOVZWL  FSTATUS,R0
100$:
        RSB

        .SBTTL  SETPOS - Set file, block pos for tape
SETPOS::
        TSTB    TAPFLG
        BNEQ    100$
        OUTMSGC TAPONLY
        BRB     200$
100$:
        MOVL    POSBLK,CURBLK
        MOVZBL  CURFIL,R1
        MOVL    #^XFFFFFFFF,FILBLK[R1]
        CMPL    POSFIL,CURFIL
        BEQL    200$
        MOVL    POSFIL,CURFIL
        MOVC5   #0,FILBLK,#^XFF,#1024,FILBLK
200$:
        RSB
        .PAGE
        .SBTTL  Data definitions

        .PSECT  DATA,WRT,NOEXE,LONG

DISKD:  .WORD   0
        .WORD   ^X010E
        .LONG   0
DD:     .BLKW   1

FSTATUS: .BLKL 2

FDESC:: .WORD   0
        .WORD   ^X010E
        .LONG   0

FNAME:: .BLKB   80
FNQ::   .ASCID  /File name? /

NNWMSG: .ASCIC  /Network operation is unsupported at this time./

ICDMSG: .ASCIC  /Incorrect device type - must be disk or tape./

TAP1MSG: .ASCIC /File-structured operation is unsupported for tape./

TAP2MSG: .ASCIC /Tape is not mounted or is not mounted FOREIGN./

TAP3MSG: .ASCIC "Can not specify /OVER or /WRITE for tape operation."

TAP4MSG: .ASCIC /*Tape position is unknown*/

NSBMSG: .ASCIC  /Disk device has nonstandard block size - can not edit./

NWLMSG: .ASCIC  "Can not specify /WRITE for XQP-device logical I/O."

WWE:    .ASCIC  /File accessed with write enabled/

WWED:   .ASCIC  /Device accessed with write enabled/

ROM:    .ASCII  /Read-only mode/
ROML=.-ROM
        .ASCII  /; WRITE not allowed/
ROMWL=.-ROM

NEF:    .ASCIC  /Can not edit an empty file/

RDY:    .ASCIC  /Ready/

NOABS:  .ASCIC  /Absolute addresses unknown within this file./

TAPONLY: .ASCIC /This command can only be used with a tape device./

BOTMSG: .ASCIC  /**BOT**/

EOFMSG: .ASCIC  /**EOF**/

EFBLKM: .ASCID  /EOF block=!UL, EOF byte=!UL/
EFBLKMX: .ASCID /EOF block=!UL, EOF byte=!4XL/

WHRMSG: .ASCID  /Editing file !AS - Block !UL/
WHDMSG: .ASCID  /Editing device !AS - Block !UL/

        .ALIGN  LONG

FABBLK: $FAB    FOP=NAM,NAM=NAMBLK
NAMBLK: $NAM    RSA=RESSTR,RSS=NAM$C_MAXRSS,-
                ESA=EXPSTR,ESS=NAM$C_MAXRSS
RESSTR: .BLKB   NAM$C_MAXRSS
EXPSTR: .BLKB   NAM$C_MAXRSS

CURNAM:: .WORD  0
        .BLKB   NAM$C_MAXRSS

CURBLK:: .LONG  1
LOBLK:  .LONG   1
HIBLK:: .LONG   0
EFBLK:: .LONG   0
FFBYTE:: .LONG  0
CURFIL:: .LONG  0
CURBCT:: .LONG  512
NXTBCT:: .LONG  0
BUFFCT:: .LONG  1
NBUFCT:: .LONG  0
CUREOF: .BYTE   0
PRTEOF: .BYTE   1
SEQFLG:: .BYTE  0
VFLAG:: .BYTE   0
RSIZE:: .LONG   0
VFCSIZE:: .LONG 0
NOSPNFLG:: .LONG 0
SVSTAT: .LONG   0

ASSFLG: .BYTE   0

ACCFLG: .BYTE   0

OPNFLG: .BYTE   0

TAPFLG:: .BYTE  0

LOGFLG: .BYTE   0

READFNC:  .LONG IO$_READVBLK
WRITEFNC: .LONG IO$_WRITEVBLK

READLOC: .LONG  0
SKIPTOT: .LONG  0
SKIPCT: .LONG   0

FLFLGS:
OVRWRT::
OVRFLG:: .BYTE  0
WRTFLG:: .BYTE  0
NOREW:: .BYTE   0
POSFLG:: .BYTE  0

POSFIL:: .LONG  0
POSBLK:: .LONG  0

BUGFLG:: .BYTE  0
SKPINC:: .LONG  50

DEVLST: .WORD   4
        .WORD   DVI$_DEVCHAR
        .ADDRESS DEVCHR
        .LONG   0
        .WORD   4
        .WORD   DVI$_DEVCLASS
        .ADDRESS DEVCLS
        .LONG   0
        .WORD   4
        .WORD   DVI$_DEVBUFSIZ
        .ADDRESS BLKSIZ
        .LONG   0
        .WORD   4
        .WORD   DVI$_MAXBLOCK
        .ADDRESS MAXBLK
        .LONG   0
        .LONG   0
DEVCHR: .LONG   0
DEVCLS: .LONG   0
BLKSIZ: .LONG   0
MAXBLK: .LONG   0
FIB:    .BLKL   12
FIBD:   .LONG   12
        .LONG   FIB
ATRLST: .WORD   ATR$S_RECATTR
        .WORD   ATR$C_RECATTR
        .ADDRESS  RECATTR
        .LONG   0
RECATTR:  .BLKB ATR$S_RECATTR

        .ALIGN  LONG

FILBLK: .BLKL   256

        .END
$*$*EOD*$*$
$ checksum FILEHDLR.MAR
$ if checksum$checksum .ne. check_sum then -
$   write sys$output "Checksum failed, file probably corrupted"
$ check_sum = 1869233584
$ write sys$output "Creating TERMHDLR.MAR"
$ create TERMHDLR.MAR
$ DECK/DOLLARS="$*$*EOD*$*$"
        .TITLE TERMHDLR Terminal and log file I/O routines

        .ENABLE SUPPRESSION

        .LIBRARY  'VFELIB'

        $DCDEF
        $DVIDEF
        $FABDEF
        $IODEF
        $NAMDEF
        $RABDEF

        .PSECT  CODE,EXE,NOWRT
        .PAGE
        .SBTTL  TINIT - Intitialize terminal I/O
TINIT::
        $ASSIGN_S  DEVNAM=TERM,CHAN=TT
        BLBC    R0,100$
        $QIOW_S CHAN=TT,FUNC=#IO$_SETMODE!IO$M_CTRLCAST,-
                EFN=#1,P1=CTRLCAST,P3=#3
        BLBS    R0,200$
100$:
        CLRW    TERMFLGS
        BRW     800$
200$:
        $GETDVI_S  EFN=#0,DEVNAM=SYSIN,ITMLST=GETCLS,IOSB=TSTATUS
        BLBS    R0,220$
        BRW     900$
220$:
        $WAITFR_S  EFN=#0
        MOVZWL  TSTATUS,R0
        BLBS    R0,240$
        BRW     900$
240$:
        CMPL    #DC$_TERM,IOCLASS
        BEQL    300$
        CLRB    TERMIN
300$:
        $GETDVI_S  EFN=#0,DEVNAM=SYSOUT,ITMLST=GETCLS,IOSB=TSTATUS
        BLBS    R0,320$
        BRB     900$
320$:
        $WAITFR_S  EFN=#0
        MOVZWL  TSTATUS,R0
        BLBS    R0,340$
        BRB     900$
340$:
        CMPL    #DC$_TERM,IOCLASS
        BEQL    400$
        CLRB    TERMOUT
        BRB     800$
400$:
        $QIOW_S CHAN=TT,FUNC=#IO$_SENSEMODE,IOSB=TSTATUS,-
                P1=MODBUF,P2=#8
        BLBC    R0,900$
        MOVZWL  TSTATUS,R0
        BLBC    R0,900$
        MOVZWL  TT_WID,TERMWD
800$:
        MOVL    #1,R0
900$:
        RSB
        .PAGE
        .SBTTL  LOGON - Turn on logging or switch log file
LOGON::
        BSBW    LOGOFF
        MOVB    DESC,LOGFAB+FAB$B_FNS
        MOVL    DESC+4,LOGFAB+FAB$L_FNA
        $CREATE FAB=LOGFAB
        BLBS    R0,100$
        BSBW    ERROUT
        BRW     900$
100$:
        $CONNECT  RAB=LOGRAB
        BLBS    R0,200$
        BRW     ERREXT
200$:
        CLRB    NOLOG
900$:
        RSB

        .SBTTL  LOGOFF - Turn off logging
LOGOFF::
        TSTB    NOLOG
        BNEQ    900$
        $CLOSE  FAB=LOGFAB
        BLBS    R0,900$
        BRW     ERREXT
900$:
        MOVB    #1,NOLOG
        RSB

        .SBTTL  SHOLOG - Display log file status
SHOLOG::
        TSTB    NOLOG
        BEQL    100$
        OUTMSGC NLGMSG
        BRB     900$
100$:
        MOVL    #100,OUTDSC
        MOVAL   LOGNBK,R6
        MOVB    NAM$B_RSL(R6),DESC
        MOVL    NAM$L_RSA(R6),DESC+4
        $FAO_S  CTRSTR=LOGMSG,OUTLEN=OUTDSC,OUTBUF=OUTDSC,-
                P1=#DESC
        OUTMSG  OUTDSC,OUT_BUFF
900$:
        RSB
        .PAGE
        .SBTTL  CTRLCAST - Handle user interrupts
CTRLCAST:
        .WORD   0
        $QIOW_S CHAN=TT,FUNC=#IO$_SETMODE!IO$M_CTRLCAST,-
                EFN=#1,P1=CTRLCAST,P3=#3
        BLBS    R0,100$
        BRW     ERREXT
100$:
        MOVB    #1,STOP
        RET

        .SBTTL  SETWID - Set terminal width (80/132)
SETWID::
        CMPL    NEWWID,TERMWD
        BNEQ    100$
        BRW     500$
100$:
        CMPL    NEWWID,#80
        BGTR    200$
        MOVL    #80,R2
        MOVAL   SETLOW,DESC+4
        BRB     300$
200$:
        MOVL    #132,R2
        MOVAL   SETHIGH,DESC+4
300$:
        BLBC    R1,400$
        MOVL    R2,NEWWID
400$:
        MOVL    NEWWID,TERMWD
        TSTB    TERMOUT
        BEQL    500$
        MOVW    TERMWD,TT_WID
        $QIOW_S CHAN=TT,FUNC=#IO$_SETMODE,P1=MODBUF,P2=#8
        BLBC    R0,900$
        $QIOW_S CHAN=TT,FUNC=#IO$_WRITEVBLK,P1=@DESC+4,P2=#5
        BLBC    R0,900$
500$:
        MOVL    #1,R0
900$:
        RSB
        .PAGE
        .SBTTL  TERMIO - Control I/O to the terminal
TERMIO::
        .WORD   ^M<R2,R3,R4,R5,R6,R7>
        MOVC3   8(AP),@4(AP),TERMBUFF
        ADDL3   #1,8(AP),R6
        CMPL    (AP),#3
        BLEQ    100$
        BRW     200$
100$:
        CMPL    (AP),#2
        BGTR    120$
        TSTW    TERMON
        BEQL    160$
120$:
        TSTB    TERMOUT
        BEQL    130$
        MOVB    #CR,TERMBUFF-1[R6]
        INCL    R6
        $QIOW_S CHAN=TT,FUNC=#IO$_WRITEVBLK,P1=TERMLF,P2=R6
        BLBS    R0,150$
        BRW     ERREXT
130$:
        MOVW    8(AP),TERMDSC
        PUSHAQ  TERMDSC
        CALLS   #1,G^LIB$PUT_OUTPUT
        BLBS    R0,150$
        BRW     ERREXT
150$:
        TSTB    NOLOG
        BNEQ    170$
160$:
        MOVL    8(AP),R6
        BRW     600$
170$:
        BRW     900$
200$:
        MOVAL   TERMLF,R7
220$:
        TSTB    TERMIN
        BEQL    450$
        $QIOW_S CHAN=TT,FUNC=#IO$_READPROMPT,IOSB=TSTATUS,-
                P1=@12(AP),P2=16(AP),P5=R7,P6=R6
        BLBS    R0,300$
        BRW     ERREXT
300$:
        CMPW    TSTATUS+4,#CR
        BEQL    340$
        TSTB    HLPON
        BEQL    350$
        CVTWL   TSTATUS+2,R7
        CMPL    R7,16(AP)
        BGEQ    340$
        ADDL2   12(AP),R7
        MOVB    TSTATUS+4,(R7)
        INCW    TSTATUS+2
340$:
        BRW     500$
350$:
        CMPW    TSTATUS+4,#CZ
        BNEQ    400$
        BRW     EXIT
400$:
        MOVAL   TERMCR,R7
        ADDL3   #2,8(AP),R6
        BRW     220$
450$:
        MOVW    8(AP),TERMDSC
        PUSHAQ  TERMDSC
        CALLS   #1,G^LIB$PUT_OUTPUT
        BLBC    R0,460$
        MOVW    16(AP),DESC
        MOVL    12(AP),DESC+4
        PUSHAW  TSTATUS+2
        PUSHL   #0
        PUSHAQ  DESC
        CALLS   #3,G^LIB$GET_INPUT
        BLBS    R0,470$
        CMPL    R0,#RMS$_EOF
        BNEQ    460$
        BRW     EXIT
460$:
        BRW     ERREXT
470$:
        MOVW    #CR,TSTATUS+4
500$:
        TSTB    NOLOG
        BNEQ    900$
        MOVL    8(AP),R6
        TSTW    TSTATUS+2
        BLEQ    600$
        ADDL3   #TERMBUFF,R6,R7
        MOVC3   TSTATUS+2,@12(AP),(R7)
        ADDW2   TSTATUS+2,R6
600$:
        MOVW    R6,LOGRAB+RAB$W_RSZ
        $PUT    RAB=LOGRAB
        BLBS    R0,900$
        BRW     ERREXT
900$:
        RET
        .PAGE
        .SBTTL  Data definitions

        .PSECT  DATA,WRT,NOEXE,LONG

CR=^X0D
LF=^X0A
CZ=^X1A
ESC=^X1B

TERM:   .ASCID  /TT/
TT:     .BLKW   1

TSTATUS:: .BLKL 2

NLGMSG: .ASCIC  /        NOLOG/
LOGMSG: .ASCID  /        LOG=!AS/

SYSIN:  .ASCID  /SYS$INPUT/

SYSOUT: .ASCID  /SYS$OUTPUT/

SETLOW: .ASCII  <ESC>/[?3l/
SETHIGH: .ASCII <ESC>/[?3h/

MODBUF:
TT_CLASS: .BLKB 1
TT_TYPE: .BLKB  1
TT_WID: .BLKB   2
TT_CHAR: .BLKB  3
TT_LEN: .BLKB   1

TERMFLGS:
TERMIN: .BYTE   1
TERMOUT:: .BYTE 1

NEWWID:: .LONG  0
TERMWD:: .LONG  132

TERMCR: .BYTE   CR
TERMLF: .BYTE   LF
TERMBUFF:  .BLKB  200
TERMDSC: .WORD  0
        .WORD   ^X010E
        .ADDRESS TERMBUFF

GETCLS: .WORD   4
        .WORD   DVI$_DEVCLASS
        .ADDRESS IOCLASS
        .LONG   0
        .LONG   0
IOCLASS: .LONG  0

        .ALIGN  LONG

LOGFAB: $FAB    FAC=PUT,FOP=<MXV,SQO,TEF>,ORG=SEQ,RAT=CR,RFM=VAR,SHR=NIL,-
                NAM=LOGNBK
LOGNBK: $NAM    RSA=RESLOG,RSS=NAM$C_MAXRSS
LOGRAB: $RAB    FAB=LOGFAB,RBF=TERMBUFF,RAC=SEQ

RESLOG: .BLKB   NAM$C_MAXRSS

        .END
$*$*EOD*$*$
$ checksum TERMHDLR.MAR
$ if checksum$checksum .ne. check_sum then -
$   write sys$output "Checksum failed, file probably corrupted"
$ exit
