$MACRO PRINTS  bufaddr <STRING> p1 p2...
I%NB <#@D> <I%ERR <#@D=too many parms>>
?- 0,,26
?? 14,15
	BR #@F
#@E=.
	.ASCIZ _#@2_
	.EVEN
#@F=.
** P%SH <#@C><#@B><#@A><#@9><#@8><#@7><#@6><#@5><#@4><#@3><##@E><#@1>
	jsr pc,sprint
?+ 1,,26
	ADD ##@1,SP	;clean up stk
$ENDM
;
$MACRO LEN string
?? 2
	mov #@1,r0
#@2=.
	tstb (r0)+
	bne #@2
	sub #@1,r0
	dec r0
$ENDM
;
$MACRO TYPE string-addr types asciz string
LEN #@1
I%IN <#@2> <> <<POKE 28 #40>> <POKE 28 #@2>
?+ 2,,28
;qiow$s #io.wvb,#5,#5,,,,<#BUF,r0,#40>   THIS IS TOO DAMN SLOW
	CLR     -(SP)
	CLR     -(SP)
	CLR     -(SP)
	MOV     #@2,-(SP)
	MOV     r0,-(SP)
	MOV     #@1,-(SP)
	CLR     -(SP)
	CLR     -(SP)
	CLR     -(SP)
	MOVB    #5,(SP)
	MOV     #5,-(SP)
	MOV     #io.wvb,-(SP)
	MOV     (PC)+,-(SP)
	.BYTE   3,12.
	EMT     ^O<377>
$ENDM
$MACRO TTY string-literal [control]
?? 5,4
	br #@4
#@5=.
	.asciz _#@1_
	.even
#@4=.
	TYPE ##@5  #@2
$ENDM
;
$MACRO PRINTF
PUSH R0
PUSH R1
PRINTS #@1 <#@2><#@3><#@4><#@5><#@6><#@7><#@8><#@9><#@A><#@B><#@C><#@D>
TYPE #@1
POP R1
POP R0
$ENDM
$MACRO TTY STRING
?? 2,3
	br #@3
#@2=.
	.asciz /#@1/
	.even
#@3=.
	TYPE ##@2
$ENDM
/
	PROGRAM prv
;+
; prv - allows users to issue certain priv'd commands
;
;       currently: prv -u file          unlock any file
;                  prv -d dev:[uic]     ufd any disk - anytime
;                  prv -a asn /gbl      get global assignments
;
; if the options are specified with a `+' the command is echoed first.
;
;-

	.mcall spwn$,dir$,wtse$,stop$s

j:      .word 0
echo:   .word 0
ucb:	.word 0
luic:	.word 0
uic:	.word 0
cw2:	.word 0
uic1:   .byte 1,1

spn:  spwn$ MCR... ,  ,  , 1 , 1 , 2  ,  ,  , buf , 29.
wait:
         i%dout <wtse$ 2 >

buf:    .blkb 90.
buf2:    .blkb 90.


usage1: .asciz /Usage: prv -u filespec/
usage2: .asciz /       prv -d dev:[directory]/
usage3: .asciz /       prv -a/
	.even

function main[argc,argv]


     LET r2 = argv(r5) + #2
     mov (r2),r2

     IFB (r2) NE #'-  AND (r2) NE #'+
use:
	   TYPE #usage1
	   TYPE #usage2
	   TYPE #usage3
     ELSE
	IFB (r2) = #'+
	   mov #1,echo
        ELSE
           clr echo
        FI

	SELECT case 1(r2) of
	CASEB #'U                                  ;unlock file

              call lockout                            ;lockout terminal, set priv
	      LET j = argv(r5) + #4
	      sprintf[#buf,"pip %s/un",@j]              ;user can /nm himself

	      IF echo NE #0
		TYPE #BUF
	      FI

;call dump

	      LEN #buf
	      mov r0,spn+s.pwcl                        ;stuff length
	      mov #buf,spn+s.pwca

	      dir$ #spn
	      dir$ #wait

              call unlock                             ;restore to previous state
	      BREAK
	CASEB #'A                                  ;get assignments

              call lockout                            ;lockout terminal, set priv
	      sprintf[#buf,"asn /gbl"]

	      IF echo NE #0
		TYPE #BUF
	      FI

	      LEN #buf
	      mov r0,spn+s.pwcl                        ;stuff length
	      mov #buf,spn+s.pwca

	      dir$ #spn
	      dir$ #wait

              call unlock                             ;restore to previous state
	      BREAK
	CASEB #'D                                     ;make a directory
              call lockout                            ;lockout terminal, set priv
	      LET j = argv(r5) + #4
	      sprintf[#buf,"ufd %s",@j]

	      IF echo NE #0
		TYPE #BUF
	      FI
	      LEN #buf
	      mov r0,spn+s.pwcl                        ;stuff length
	      mov #buf,spn+s.pwca

	      dir$ #spn
	      dir$ #wait

              call unlock                             ;restore to previous state

	      BREAK
	DEFAULT
	      jmp use
	ESAC


     FI
     RET #1

;+
;lockout - lock the terminal and make it priv'd and [1,1]
;-


lockout:
	i%dout <emt 376>                                 ;switch to system state
	.word 10$                               ;return here

	mov $tktcb,r0                           ;get tcb address

        mov t.ucb(r0),r0                        ;get ti ucb
5$:     cmp r0,u.red(r0)                        ;follow redirect chain
	beq 7$                                  ;if it is redirected
	mov u.red(r0),r0
        br 5$

7$:     mov r0,ucb                              ;save for later
        mov u.luic(r0),luic                     ;save
        mov u.uic(r0),uic                       ; all
        mov u.cw2(r0),cw2                       ;  of these

	bis #u2.slv!u2.prv,u.cw2(r0)            ;slave and privel it

	mov uic1,u.luic(r0)                     ;make it [1,1]
	mov uic1,u.uic(r0)                      ;make this [1,1] too
 
	mov $headr,r0		;my task header
	mov uic1,h.cuic(r0)
	mov uic1,h.duic(r0)	;make me [1,1] as well

        rts pc                                  ;leave system state

10$:    rts pc                                  ;return

;+
;unlock - unlock the terminal
;-

unlock:
	i%dout <emt 376>                                 ;system state
	.word 10$

	mov ucb,r0                              ;remembered this
        mov luic,u.luic(r0)                     ;restore these
        mov uic,u.uic(r0)                       ;restore
        mov cw2,u.cw2(r0)                       ;restore

        rts pc                                  ;out of system state

10$:    rts pc                                  ;return
dump:

;;mov ucb,r0
;;PRINTF #buf2 <ucb=%o uic=%o luic=%o cw2=%o> ucb #u.uic #u.luic #u.cw2
;;PRINTF #buf2 <u.uic=%o u.luic=%o u.cw2=%o> u.uic(r0) u.luic(r0) u.cw2(r0)
;;PRINTF #buf2 <uic=%o  luic=%o cw2 =%o> uic luic cw2
rts pc
	.end 
