
;            NewEx.asm   90-06-11  Version V1.1
;
;            New Executeing program
;
;            Written by Kjell Cederfeldt in June - 90.
;
ExecBase           = 4
OpenLibrary        = -408         ; exec offset       OpenLibrary(libName,version)(A1,D0)
CloseLibrary       = -414         ; exec offset       CloseLibrary(library)(a1)
FindTask           = -294         ; exec offset       FindTask(name)(a1)
WaitPort           = -384         ; exec offset       WaitPort(port)(a0)
GetMsg             = -372         ; exec offset       GetMsg(port)(a0)
ReplayMsg          = -378         ; exec offset       ReplayMsg(message)(a1)
Forbid             = -132         ; exec offset       Forbid()
Open               = -30          ; dos offset        Open(name,accessMode)(D1/D2)
Close              = -36          ; dos offset        Close(file)(D1)
Write              = -48          ; dos offset        Write(file,buffer,length)(d1/d2/d3)
OutPut             = -60          ; dos offset        OutPut()
Lock               = -84          ; dos offset!       Lock(name,mode)(d1,d2)
UnLock             = -90          ; dos offset!       UnLock(lock)(d1)
Examine            = -102         ; dos offset!       Examine(Lock,InfoBlock)(d1,d2)
CurrentDir         = -126         ; dos offset        CurrentDir(lock)(d1)
ParentDir          = -210         ; dos offset!       ParentDir(lock)(d1)
Execute            = -222         ; dos offset        Execute(string,file,file)(D1/D2/D3)
GetDiskObject      = -78          ; icon offset       GetDiskObject(name)(a0)
FindToolType       = -96          ; icon offset       FindToolType(toolTypeArray,typeName)(A0/A1)
FreeDiskObject     = -90          ; icon offset       FreeDiskObject(diskobj)(a0)
Mode_old           = 1005         ;
Mode_new           = 1006         ;
Access_read        = -2

                   move.l sp,Initial_SP               ;Save stackpointer
                   move.l a0,Filename                 ;CLI=true save address to argument
                   sub.l  #1,d0                       ;decrement lenth with 1
                   add.l  d0,a0                       ;Add lenth to address
                   move.b #0,(a0)                     ;clear last byte (CLI=0A)
                   move.l #0,RetCode                  ;clear Returncode

START:             move.l ExecBase,a6
                   suba.l a1,a1                       ;Find this Task
                   jsr    Findtask(a6)                
                   move.l d0,a4                       ;Save strukture in a4
                   tst.l  $ac(a4)                     ;CLI or WB ?
                   bne    CliStart                    ;


;       ** This only when  start from WB


                   move.l #49,RetCode                 ;Set Returncode
                   lea    $5c(a4),a0                  ;WBench message
                   jsr    WaitPort(a6)                ;wait
                   jsr    GetMsg(a6)                  ;load message
                   move.l d0,Message                  ;save pointer to WBMessage
                   beq    Slut                        ;error!

                   move.l #48,RetCode                 ;Set Returncode
                   move.l Message,a0                  ;Pointer to WBMessage
                   move.l $24(a0),a0                  ;Argumentlist
                   beq    Slut2                       ;no agument

CliStart:


;   Open icon.library

                   move.l #47,RetCode                 ;Set Returncode
                   lea    Iconname(pc),a1             ;Load address of IconName in a1
                   clr.l  d0                          ;all version
                   jsr    OpenLibrary(a6)             ;open iconlibrary
                   move.l d0,Iconbase                 ;Save Iconbase
                   beq    Error1                      ;branch if error!


;   open dos.library

                   move.l #46,RetCode                 ;Set Returncode
                   lea    Dosname(pc),a1              ;Load address of DosName in a1
                   clr.l  d0                          ;all dosversion
                   jsr    OpenLibrary(a6)             ;open doslibrary
                   move.l d0,Dosbase                  ;Save Dosbase
                   beq    Error2                      ;branch if error!


                   jsr    PointTOCLI                  ;Get CLI pointer

                   tst.l  Message                     ;if start from CLI
                   beq    InFileOpen                  ;branch to FileOpens

;   set Current directory

                   move.l #45,RetCode                 ;Set Returncode
                   move.l Message,a0                  ;Pointer to WBMessage sm_ArgList
                   move.l $24(a0),a0                  ;move pointer to wa_Lock
                   beq    Error3                      ;Error!

                   move.l (a0),d1                     ;D1 => Lock
                   move.l Dosbase,a6
                   jsr    CurrentDir(a6)              ;Lock Current Dir

;   Get diskobject (.info file)

                   move.l #44,RetCode                 ;Set Returncode
                   move.l Message,a0
                   move.l $24(a0),a0
                   move.l 4(a0),a0                    ;move pointer to wa_Name
                   move.l a0,Filename
                   move.l Iconbase,a6
                   jsr    GetDiskObject(a6)           ;Get diskobject
                   move.l d0,Diskobj                  ;Save diskobject
                   beq    Error3                      ;Error!
                   move.l d0,a1
                   move.l $36(a1),a1                  ;pointer to ToolType Array
                   move.l a1,toolTypeArray            ;save pointer



;            *** Get argument from tool icon (tooltype)

                   move.l Iconbase,a6
                   move.l toolTypeArray,a0
                   move.l #typeName1,a1
                   jsr    FindToolType(a6)
                   move.l d0,Inname

                   move.l toolTypeArray,a0
                   move.l #typeName2,a1
                   jsr    FindToolType(a6)
                   move.l d0,Outname


;   open outfile
                   move.l #43,RetCode                 ;Set Returncode
                   move.l DosBase,a6                  ;Load dosbase
                   move.l Outname,d1                  ;Get filename
                   move.l #Mode_new,d2                ;1006=NewFile: 1005=Existing file
                   jsr    Open(a6)                    ;open file
                   move.l d0,Outhandle                ;Save filehandler
                   beq    Error3                      ;Branch if error!

;-------
;   open readfile
;-------
InFileOpen:

                   move.l #42,RetCode                 ;Set Returncode
                   move.l Filename,d1                 ;Load filename
                   tst.l  Message                     ;test if Start WB or CLI?
                   beq.s  InopLab                     ;Branch, if CLI
                   move.l Inname,d1                   ;Load filename
InOpLab:           move   #Mode_old,d2                ;1005=Existing file : 1006=NewFile
                   move.l DosBase,a6                  ;Load dosbase
                   jsr    Open(a6)                    ;open file
                   move.l d0,Inhandle                 ;Save filehandlern
                   bne    BldPath                     ;Branch if NOT error!

                   tst.l  Message                     ;test Start WB or CLI?
                   bne.s  Err                         ;Branch, if WB
                   move.l CLIpoint,d1
                   move.l #Textfld,d2
                   move.l #84,d3
                   jsr    Write(a6)
Err:               bra    Error4


;             Get and build pathway.

BldPath:           jsr    GetPath                     ;build path


Skriv:    ; Only for bebuging (not used in program)
                   move.l Dosbase,a6
;                   move.l CLIpoint,d1
;                   tst.l  Message                     ;test if Start WB or CLI?
;                   beq.s  Skr                         ;Branch to End if CLI
;                   move.l Outhandle,d1
;Skr:               move.l #path,d2
;                   move.l #255,d3                      ;string length
;                   jsr    Write(a6)
;--------
;   call Execute (dosfunction)
;--------
                   move.l #0,RetCode                 ;clear Returncode
                   move.l CLIPoint,d3

                   tst.l  Message                     ;test if Start WB or CLI?
                   beq.s  Exlabel                     ;Branch to End if CLI

                   move.l Outhandle,d3                ;Get file handler
Exlabel:           move.l PathStart,d1                ;get pointer of string
                   move.l InHandle,d2                 ;get file handler
                   jsr    Execute(a6)                 ;exectue a scripfile (dos)


;---------
;   Close files
;---------

Cleanup1:          move.l Inhandle,d1
                   jsr    Close(a6)

Error4:            tst.l  Message                     ;test if Start WB or CLI?
                   beq.s  temp                        ;Branch to End if CLI

                   move.l Outhandle,d1
                   jsr    Close(a6)


Error3:            move.l Iconbase,a6                 ;Load Iconbase
                   move.l Diskobj,a0                  ;Get diskobject strukture
                   jsr    FreeDiskobject(a6)          ;Free Diskobjec

temp:              move.l Dosbase,a1                  ;Load Dosbase
                   move.l Execbase,a6
                   jsr    CloseLibrary(a6)            ;Close Doslibrary:

Error2:            move.l Iconbase,a1                 ;Load IconBase
                   move.l Execbase,a6
                   jsr    CloseLibrary(a6)            ;Close Iconlibrary


Error1:            tst.l  Message                     ;Test if Start WB or CLI?
                   beq.s  Slut                        ;Branch to End if CLI
Slut2:             jsr    Forbid(a6)
                   move.l Message,a1
                   jsr    ReplayMsg(a6)               ;drop struktur

Slut:              move.l Initial_SP,sp               ;Load start stack
                   clr.l  d0
                   move.l RetCode,d0                  ;Get returncode to D0
                   rts

;       !!    *********** System DATA **************  !!

Initial_SP:        dc.l   0
RetCode:           dc.l   0
Iconbase:          dc.l   0
Dosbase:           dc.l   0
Inhandle:          dc.l   0
Outhandle:         dc.l   0
Message:           dc.l   0
Diskobj:           dc.l   0
CLIpoint:          dc.l   0
Filename:          dc.l   0
NewLock:           dc.l   0
ParLock:           dc.l   0
Inname:            dc.l   0
Outname:           dc.l   0
Pathstart:         dc.l   0
Dosname:           dc.b   'dos.library',0
                   align
Iconname:          dc.b   'icon.library',0
                   align
TempNamn:          blk.b  32,0
                   align
Path:              blk.b  255,0
                   align
typeName1:         dc.b   "FILENAME",0,0
                   align
typeName2:         dc.b   "WINDOW",0,0
                   align
Textfld:           dc.b   27,"[33mNewEx Version 1.1 by Kjell Cederfeldt",13,10,10
                   dc.b   "No file name or file not found!",27,"[31m",13,10,10
                   align
toolTypeArray:     dc.l   0
IconText:          dc.l   0
FileInfoblock:     blk.b  260,0
                   align

;                                 ReturnCode 47 = icon.library not open
;                                 ReturnCode 46 = dos.library  not open
;                                 ReturnCode 45 = currentdir not lock
;                                 ReturnCode 44 = diskobjekt not found
;                                 ReturnCode 43 = outfile (device) not found
;                                 ReturnCode 42 = infile (device) not found

;********************   Subrutiner   ***************************************

PointTOCLI:

; Output functionen point to cli-window already open if start from CLI

                   move.l Dosbase,a6                  ;pointer doslibrary
                   jsr    OutPut(a6)                  ;get output pointer to Dos!
                   move.l d1,CLIpoint                 ;Save pointer
                   rts

;***************************************************************************

GetPath:
                   move.l #Path,d2                    ;peek on address to Path
                   add.l  #253,d2                     ;peek on end -2
                   move.l d2,PathStart                ;Get start of pathstring

                   move.l Dosbase,a6                  ;Load dosbase
                   move.l #Filename,d1                ;Load pointer to filename
                   move.l Access_read,d2              ;read-mode only
                   jsr    Lock(a6)                    ;lock
                   tst.l  d0
                   beq    EndLoop                     ;No File found
                   move.l d0,NewLock                  ;save lock

MainLoop:          move.l NewLock,d1                  ;Get lock
                   move.l #Fileinfoblock,d2           ;Get address to block
                   jsr    Examine(a6)                 ;Get fileinfo
                   tst.l  d0
                   beq    EndLoop

                   move.l #Fileinfoblock,d2
                   add.l  #8,d2                       ;Add offset to address

                   clr.l  d0                          ;Clear counter
                   move.l d2,a0                       ;move filepointer to A0
                   move.l #Tempnamn,a1                ;move pathpointer to A1
Loop:              move.b (a0)+,(a1)+                 ;move value A0 => A1
                   add.l  #1,d0                       ;decriment counter

                   tst.b  (a0)                        ;Name end? (0 terminering)
                   bne.s  Loop                        ;No branch to loop

                   move.b #47,(a1)+                   ;add '/'
                   add.l  #1,d0                       ;decriment counter

                   move.l Pathstart,d2                ;get Path start
                   move.l d2,a0

Loop2:             move.b -(a1),-(a0)                 ;save path-stukturen to Path
                   sub.l  #1,d0                       ;increment counter
                   bne.s  Loop2

                   move.l a0,Pathstart                ;save pathstart

                   move.l NewLock,d1                  ;get lock
                   jsr    ParentDir(a6)               ;go to parent dir
                   move.l d0,ParLock                  ;save lock
                   move.l NewLock,d1                  ;get lock
                   jsr    UnLock(a6)                  ;relese lock
                   move.l ParLock,d0                  ;get lock
                   move.l d0,NewLock                  ;move lock
                   tst.l  d0                          ;are we in root ?
                   bne    MainLoop                    ;No branch

                   move.l PathStart,a1                ;get pathstart
Kolon:             cmp.b  #47,(a1)+                   ;found first '/'
                   bne.s  kolon
                   move.b #58,-(a1)                   ;Change this to ':'

                   move.l PathStart,a1                ;get pathstart
                   move.b #34,-(a1)                   ;add '"'
                   move.b #32,-(a1)                   ;add ' '
                   move.b #68,-(a1)                   ;add 'D'
                   move.b #67,-(a1)                   ;add 'C'
                   move.l a1,PathStart                ;save the new start                   move.l #Path,a1
                   add.l  #253,a1
                   move.b #34,(a1)

Endloop:           rts

;***************************************************************************
                   end
