* MkDir.Asm
*
*       Written 1/10/87 by C.Heath
*
*       Copyright (c) 1986 by C.Heath of Microsmiths Inc.
*
*       This program may be freely copied for individual use.  It may
* also be freely distributed with the following restrictions:
*
*       1) No modifications to the source code are made.  If an executable
* version of the program is distributed, use assem and blink 6.7, which
* should result in an executable of 336 bytes. 
*       2) Please verify that the most current version is distributed. 
* This can be verified by contacting "cheath" on BIX, or 74216,2117 on CIS, 
* or write to Microsmiths, Inc., PO Box 561, Cambridge, MA 02140. 
*       3) The ARP series may be distributed with commercial software 
* except as noted in (4) provided it's origin is stated.  Please contact 
* me to insure you have the latest version. 
*       4) This module may NOT be distributed with any commercial software 
* which encourages the BCPL program interface without express written 
* permission.  This restriction includes the Metacomco Shell.

*       This module replaces the ADOS "MakeDir" command.  It matches the
* ADOS prompt syntax, except that names with spaces are allowed without
* quotes. ( Quotes are accepted, also )
*       This command is smaller than the ADOS equivalent so runs a bit faster.

*       This is installment ~9 of the "ARP" series.  The're coming fast
* and furious now, I've lost count.

******  Equates and publics     ***************************************

        INCLUDE "exec/types.i"
        INCLUDE "libraries/dos.i"

MIN_LIB         set     $1f             ; Library minimum rev level!
                                        ; ADOS "stack" command

*** WITH MANX, SUBSTITUTE public FOR xref
xlib    macro   
        xref    _LVO\1
        endm

*** BEGIN MANX code.  Uncomment the next few lines to assemble with MANX
*
*       entry   .begin
*       public  .begin
*.begin
*** END MANX CODE

        xlib    OpenLibrary
        xlib    CloseLibrary

        xlib    Output
        xlib    Write

        xlib    Lock
        xlib    UnLock
        xlib    CreateDir

******  The Program             ***************************************

Start:
        move.l  4,A6                    ; ExecBase
        move.l  A0,A2                   ; Register A2 is cmdline henceforth

        lea     DOSName(pc),A1          ; Open DOS.library
        moveq.l #MIN_LIB,d0
        jsr     _LVOOpenLibrary(A6)
        move.l  D0,A6                   ; A6 is DOSBase henceforth
        or.l    D0,D0
         bne.s  ok_start                ; Continue if library OK,

* fall in to BadExit code if DOS library couldn't open

************************************************************************
*
* BadExit is where to go if there is an error opening the DOS library
*       This should only happen if things are really sick
*
************************************************************************
BadExit:
                moveq   #20,D0
                rts

************************************************************************
*
* ok_start
*       Proceed with normal code, check the input string.
*
************************************************************************
ok_start:

*
* Process the input command string
*
skpspc: move.b  (A2)+,D1                ; Skip spaces 
        cmp.b   #$20,D1
         beq.s  skpspc
        cmp.b   #$0A,D1                 ; check for a NULL string ?
         beq.s  show_cmdline            ; Ok, just show the current dir.

        cmp.b   #'"',D1
         beq.s  do_mkdir                ; String in Quotes, D1 is EOS test
        
        subq    #1,A2                   ; Back up a char
        clr     D1                      ; Clear terminator test char
*
* Here, Create the directory speced by string at A2
*
do_mkdir:
        move.l  A2,A0
        moveq   #0,D0
lla:    move.b  (A0)+,D0
         beq.s  lalala
        cmp.b   D1,D0
         beq.s  lalala                  ; End of quoted string
        cmp.b   #$0A,D0
         bne.s  lla

lalala:
        clr.b   -1(A0)                  ; Turn the terminater into a
                                        ; null terminating string
        move.l  A2,D1
        move.l  #ACCESS_READ,D2
        jsr     _LVOLock(A6)
        move.l  D0,D1
         bne.s  Already_Exists          ; The directory already exists!

        move.l  A2,D1
        jsr     _LVOCreateDir(A6)       ; Create the directory
        move.l  D0,D1
         beq.s  Error_Creating          ; Error if no lock returned

        jsr     _LVOUnLock(A6)          ; Free the lock. It's golden!
        bra.s   CleanUp                 ; And exit

***************************************************************************
*
* show_cmdline
*       MkDir was called with no arguement.
*       Display calling parameters and exit.
*
***************************************************************************
show_cmdline:
        lea     cmd_str(pc),A0
        bsr.s   do_write

        moveq   #0,D0
CleanUp:
        move.l  D0,D2                   ; Save return value
        move.l  A6,A1
        move.l  4,A6                    ; AbsExecBase
        jsr     _LVOCloseLibrary(A6)

        move.l  D2,D0                   ; Restore return value
        rts                             ; and exit()

***************************************************************************
*
* Already_Exists
*       Can't create directory, it already exists!
*
Already_Exists:
        jsr     _LVOUnLock(A6)          ; Free old currentdir Lock

        lea     Already_Exists_str(pc),A0
        bsr.s   do_write

        move.l  A2,A0
        bsr.s   do_write

        lea     term_str(pc),A0
        bra.s   err_mrg

***************************************************************************
*
* Error_Creating
*       Error creating directory.  Just a simple error message
*
***************************************************************************
Error_Creating:
        lea     err_str(pc),A0          ; Just fall into err_mrg

***************************************************************************
*
* err_mrg
*       Merge point for some error messages.  Exits through cleanup
*
***************************************************************************
err_mrg:
        bsr.s   do_write

        moveq   #20,D0                  ; Set to exit(20)
        bra.s   CleanUp

***************************************************************************
*
* do_write
*       Simple write string.
*
*       Assumes A6 = DOSBase
*       Null terminated string is @A0
*       Also assumes a reasonable CLI environment with valid COS
*
*       Zaps D0..D3/A0/A1
*
***************************************************************************
do_write:
        move.l  A0,D2
        moveq   #-1,D3
dwl1:   addq.l  #1,D3
        tst.b   (A0)+
         bne.s  dwl1

        jsr     _LVOOutput(A6)
        move.l  D0,D1
        jmp     _LVOWrite(A6)


***************************************************************************
*
* Following is just constant data used by the program
*
***************************************************************************

DOSName:        dc.b    'dos.library',0

cmd_str:        dc.b    'MkDir command line is "MkDir name"',$0A,0

Already_Exists_str:
                dc.b    'Cannot create directory - ',0

term_str:       dc.b    ' already exists!',$0A,0


err_str:        dc.b    'Error creating directory ',0


        END
