* === cdtitle.asm ======================================================
*
* Copyright (c) 1987 by William S. Hawes (all rights reserved)
*
* ======================================================================
* Installs the current directory as the window title.

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

         INCLUDE  "intuition/brief.i"

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

         XREF     _AbsExecBase

         ; DOS library functions

         XLIB     Close
         XLIB     Open

         ; Exec library functions

         XLIB     CloseLibrary
         XLIB     GetMsg
         XLIB     OpenLibrary
         XLIB     PutMsg
         XLIB     WaitPort

         ; Intuition library functions

         XLIB     SetWindowTitles

         IFND     EXECBASE_I
ThisTask EQU      $114
         ENDC

STACKBF  SET      sp_SIZEOF+fib_SIZEOF+4
start:   movea.l  _AbsExecBase,a6
         lea      -STACKBF(sp),sp
         movea.l  ThisTask(a6),a4

         moveq    #pr_MsgPort,d7
         add.l    a4,d7

         ; Make sure we're a CLI ...

         moveq    #RETURN_FAIL,d6      ; error
         move.l   pr_CLI(a4),d0        ; a CLI?
         beq      Exit                 ; no

         ; Save the prior return codes

         lsl.l    #2,d0
         movea.l  d0,a3                ; CLI structure
         move.l   cli_Result2(a3),d4
         move.l   cli_ReturnCode(a3),d6

         ; Open the DOS library

         lea      DOSLib(pc),a1
         moveq    #LIBRARY_VERSION,d0
         CALLSYS  OpenLibrary
         move.l   d0,d5

         ; Open the Intuition library

         lea      IntuiLib(pc),a1
         moveq    #LIBRARY_VERSION,d0
         CALLSYS  OpenLibrary
         tst.l    d0                   ; success?
         beq      CloseDOS             ; no
         movea.l  d0,a5

         ; Initialize a message packet on the stack.

         move.l   sp,d0
         addq.l   #3,d0
         andi.b   #$FC,d0
         movea.l  d0,a2                ; message packet

         lea      (sp_Pkt+dp_Link)(a2),a1
         move.l   a1,LN_NAME(a2)
         move.l   d7,MN_REPLYPORT(a2)
         move.l   a2,(a1)
         move.l   d7,(sp_Pkt+dp_Port)(a2)
         move.l   #ACTION_DISK_INFO,(sp_Pkt+dp_Type)(a2)

         lea      sp_SIZEOF(a2),a0     ; FileInfoBlock
         move.l   a0,d0
         lsr.l    #2,d0                ; BPTR to FileInfoBlock
         move.l   d0,(sp_Pkt+dp_Arg1)(a2)

         ; Open the filehandle on the "*" handler

         lea      Star(pc),a1          ; "*" handler
         move.l   a1,d1
         move.l   #MODE_NEWFILE,d2
         exg      d5,a6
         CALLSYS  Open                 ; D0=filehandle
         exg      d5,a6
         move.l   d0,d3                ; save filehandle
         beq.s    CloseLib

         ; Make sure it's an interactive filehandle.

         lsl.l    #2,d0
         movea.l  d0,a0
         tst.l    fh_Interactive(a0)   ; interactive?
         beq.s    CloseFH              ; no

         ; Send the message to the handler

         movea.l  fh_Type(a0),a0       ; handler processid
         movea.l  a2,a1                ; message
         CALLSYS  PutMsg

         movea.l  d7,a0
         CALLSYS  WaitPort
         movea.l  d7,a0
         CALLSYS  GetMsg               ; D0=message

         ; Get the pointer to the current directory string.

         move.l   cli_SetName(a3),d1   ; BSTR to directory
         lsl.l    #2,d1
         movea.l  d1,a1                ; BCPL string
         moveq    #0,d1                ; clear length
         move.b   (a1)+,d1             ; string length
         clr.b    0(a1,d1.w)           ; null byte

         ; Install the new window title.

         lea      sp_SIZEOF(a2),a0     ; FileInfoBlock
         movea.l  id_VolumeNode(a0),a0 ; window pointer
         suba.l   a2,a2                ; no screen title
         exg      a5,a6
         CALLSYS  SetWindowTitles
         exg      a5,a6

         ; Close the filehandle

CloseFH  move.l   d3,d1
         exg      d5,a6
         CALLSYS  Close
         exg      d5,a6

         ; Close the Intuition library

CloseLib movea.l  a5,a1
         CALLSYS  CloseLibrary

CloseDOS movea.l  d5,a1
         CALLSYS  CloseLibrary

         ; Restore the prior return codes

         move.l   d4,pr_Result2(a4)    ; secondary result

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

         ; String constants

DOSLib   dc.b     'dos.library',0
IntuiLib dc.b     'intuition.library',0
Star     dc.b     '*',0
         CNOP     0,2

         END
