* == scriptify.asm =====================================================
*
* Written April, 1988 by William S. Hawes >>> freely distributable <<<
*
* ======================================================================
* Sets the "script bit" (protection bit 6) for a file.

         SECTION  Script,CODE

         INCLUDE  "exec/types.i"
         INCLUDE  "exec/macros.i"

         INCLUDE  "libraries/dos.i"
         INCLUDE  "libraries/dosextens.i"

         IFND     EXEC_EXECBASE_I
ThisTask EQU      276
         ENDC

SCRIPT   EQU      6                    ; script bit

         XREF     _AbsExecBase

         ; DOS library

         XLIB     DeviceProc
         XLIB     Examine
         XLIB     IoErr
         XLIB     Lock
         XLIB     Output
         XLIB     SetProtection
         XLIB     UnLock
         XLIB     Write

         ; Exec library functions

         XLIB     CloseLibrary
         XLIB     GetMsg
         XLIB     OpenLibrary
         XLIB     PutMsg
         XLIB     WaitPort

STACKBF  SET      4+sp_SIZEOF+fib_SIZEOF
start    movea.l  _AbsExecBase,a6
         lea      -STACKBF(sp),sp      ; FileInfoBlock
         movea.l  ThisTask(a6),a4
         movea.l  a0,a3                ; command line

         move.l   d0,d3
         moveq    #0,d6                ; return code
         moveq    #pr_MsgPort,d7       ; processid offset
         add.l    a4,d7                ; our processid

         adda.l   d3,a0                ; end of string
         subq.w   #1,d3                ; loop count

1$:      clr.b    (a0)
         cmpi.b   #' ',-(a0)           ; > blank?
         dbhi     d3,1$                ; loop back
         addq.w   #1,d3                ; restore length

         lea      DOSLib(pc),a1
         moveq    #0,d0
         CALLSYS  OpenLibrary
         move.l   d0,d5                ; "guaranteed"

         cmpi.w   #1,d3                ; one character?
         blt      ShowFmt              ; ... no filename
         bgt.s    2$                   ; > one
         cmpi.b   #'?',(a3)            ; display format?
         beq      ShowFmt              ; yes

         ; Align the StandardPacket to a longword boundary

2$:      move.l   sp,d0
         addq.l   #3,d0
         andi.b   #$FC,d0              ; longword address
         move.l   d0,a5                ; StandardPacket
         lea      sp_SIZEOF(a5),a2     ; FileInfoBlock

         ; Initialize the message packet

         lea      sp_Pkt+dp_Link(a5),a1
         move.l   a1,LN_NAME(a5)
         move.l   a5,(a1)
         move.l   d7,MN_REPLYPORT(a5)
         move.l   d7,sp_Pkt+dp_Port(a5)

         ; Get a lock on the file

         move.l   a3,d1                ; name
         moveq    #ACCESS_READ,d2      ; mode
         exg      d5,a6
         CALLSYS  Lock
         move.l   d0,d4                ; a lock?
         beq.s    3$                   ; no

         ; Fill in the FileInfoBlock

         move.l   d4,d1                ; lock
         move.l   a2,d2                ; FIB
         CALLSYS  Examine              ; D0=success

3$:      move.l   d4,d1                ; lock
         move.l   d0,d4                ; save result
         CALLSYS  UnLock
         exg      d5,a6

         tst.l    d4                   ; success?
         beq.s    Error1               ; no

         move.l   fib_Protection(a2),d2
         bset     #SCRIPT,d2           ; set script
         bne.s    CloseDOS             ; already set

         ; Get the handler process and directory lock

         move.l   a3,d1                ; name
         exg      d5,a6
         CALLSYS  DeviceProc           ; D0=handler
         move.l   d0,d3                ; save handler
         CALLSYS  IoErr                ; D0=lock
         exg      d5,a6

         ; Copy the filename and make a BSTR

         lea      fib_FileName(a2),a0
         movea.l  a2,a1                ; start of FIB
         clr.b    (a1)+                ; clear length

4$:      move.b   (a0)+,(a1)+          ; copy name
         beq.s    5$                   ; at end
         addq.b   #1,(a2)              ; bump length
         bra.s    4$                   ; loop back

5$:      move.l   a2,d1                ; filename
         lsr.l    #2,d1                ; convert to BPTR

         movem.l  d0/d1/d2,sp_Pkt+dp_Arg2(a5)
         move.l   #ACTION_SET_PROTECT,sp_Pkt+dp_Type(a5)

         ; Send the packet ...

         movea.l  d3,a0                ; handler processid
         move.l   a5,a1                ; message
         CALLSYS  PutMsg

         movea.l  d7,a0                ; our processid
         CALLSYS  WaitPort
         movea.l  d7,a0
         CALLSYS  GetMsg

         tst.l    sp_Pkt+dp_Res1(a5)   ; success?
         bne.s    CloseDOS             ; yes
         bra.s    Error1

ShowFmt  lea      Usage(pc),a0         ; usage format
         bsr.s    Display
         bra.s    CloseDOS

Error1   lea      ErrMsg1(pc),a0
         bsr.s    Display
         moveq    #RETURN_FAIL,d6

CloseDOS movea.l  d5,a1
         CALLSYS  CloseLibrary

         move.l   d6,d0                ; return code
         lea      STACKBF(sp),sp
         rts

Display
         movem.l  d2/d3/a6,-(sp)
         movea.l  d5,a6

         move.l   a0,d2
         moveq    #-1,d3
1$:      addq.l   #1,d3
         tst.b    (a0)+
         bne.s    1$

         CALLSYS  Output
         move.l   d0,d1
         CALLSYS  Write

         movem.l  (sp)+,d2/d3/a6
         rts

         ; String constants

DOSLib   dc.b     'dos.library',0
Usage    dc.b     'USAGE: scriptify filename',10,0

ErrMsg1  dc.b     'Error ... script bit not set',10,0
         CNOP     0,2

         END
