Whoops!

The copy of #!REBOL.BAT I included had 2 bugs and one
slower part, all fixed in the version I've included
with this message. My bad!

At 09:32 AM 2/15/00 -0600, I wrote:
>REBOL starts in the REBOL home directory, not the current
>directory. If your script is in a different directory, you
>have to specify the whole path to the script on the command
>line to REBOL.

Apparently this is different with /View. #!REBOL.BAT still
works with /View, but I should take /View into account when
it is released.

Brian Hawley
@ECHO OFF
shift
rem *** The folowing call must be on ONE line ***
C:\Lang\Rebol\REBOL.exe --do "application-dirs: parse/all {%PATH%} {;}" --script 
C:\Utilities\bin\#!REBOL.BAT %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
goto end-of-REBOL-launcher

REBOL [
    Title: "#!REBOL Script Launcher"
    Date: 15-Feb-2000
    File: %#!REBOL.BAT
    Author: "Brian Hawley"
    Email: [EMAIL PROTECTED]
    Rights: "Copyright (C) Brian Hawley 2000, rights under GPL"
    Purpose: "Make it easy to run a REBOL script as a DOS batch file."
    Usage: trim/auto {
        #!REBOL.BAT helps you to express a REBOL script as a DOS batch
        file. The first argument is the name of the calling batch file
        (expressed best as {%0} to handle spaces, etc.). A REBOL batch
        file that uses #!REBOL.BAT looks something like this:
        
        @echo off
        #!REBOL {%0} -q %1 %2 %3 %4 %5 %6 %7 %8 %9
        
        REBOL [
            Title: "Hello World"
        ]
        print "Hello World!"
        print system/script/args
        
        if confirm "Do you want to quit? (Y or N) " [quit]
    }
    Comments: trim/auto {
        In order to work, you must name this file #!REBOL.BAT so that
        it will be treated as a batch file. The .BAT is essential, but
        the #! is only there as part of the #!language script calling
        convention from Unix, useful on other OS's too. #!REBOL can
        now handle batch files that aren't on the path or whose name
        can change by passing {%0} as the batch name. You must put
        #!REBOL.BAT somewhere on the OS path and change the call to
        REBOL.exe above to reflect the location of the files REBOL.exe
        and #!REBOL.BAT.
        This script incorporates the function to-rebol-file which is
        seperately licensed under the LGPL.
    }
    Category: [file script]
]

if not value? 'to-rebol-file [
; The wierd bit with init here is to work around a GC bug :(
use [init] [
init: make object! [
    alpha: charset [#"A" - #"Z" #"a" - #"z"]
    dos-char: complement charset [
        #"^(0)" - #"^(1f)" {\/:*?<>|"}
    ]
    amiga-char: complement charset ":/"
]
    
to-rebol-file: func [
    "Changes a valid platform filename into a rebol file."
    file-name [string!] "A platform-format filename."
    /default "Return this file instead of none when not valid."
    default-file [file!]
    /dir "Add a trailing /, if needed, to indicate a directory."
    /platform "Follow the filename conventions of a given platform."
    which [integer!] "A REBOL major platform number"
    /local
    DOS Amiga MacOS Unix rebol-file tmp
] head insert bind/copy [
    ; Initialize the transformation rules
    DOS: [
        rebol-file: make file! length? file-name
        if not parse/all file-name [
            [ ; Prefix, if any
                ; Drive letter
                copy tmp alpha ":\" (
                    insert insert insert rebol-file #"/" tmp #"/"
                ) | copy tmp alpha ":" (
                    if (to-char tmp) <> (second what-dir) [
                        insert insert insert rebol-file #"/" tmp #"/"
                    ]
                ) |
                ; UNC path (no single-letter computer names!)
                "\\" (insert rebol-file #"/") |
                ; Root dir of current drive
                "\" (insert rebol-file "//") |
                none
            ] [ ; File path, if any
                copy tmp some dos-char (insert tail rebol-file tmp)
                any ["\" copy tmp some dos-char (
                    insert insert tail rebol-file #"/" tmp
                ) ]
                ["\" (insert tail rebol-file #"/") | none] |
                none
            ]
        ] [ ; Parse failed
            rebol-file: none
        ]
    ]
    Amiga: [
        rebol-file: make file! length? file-name
        if not parse/all file-name [
            ; Device
            [copy tmp 0 30 amiga-char ":" (
                insert insert insert rebol-file #"/" any [tmp ""] #"/"
            ) | none]
            ; Directory path
            copy tmp any "/" (
                if tmp [insert/dup rebol-file "../" length? tmp]
            )
            any [ 
                copy tmp 1 30 amiga-char "/" (
                    insert insert tail rebol-file tmp #"/"
                )
                copy tmp any "/" (
                    if tmp [
                        insert/dup tail rebol-file "../" length? tmp
                    ]
                ) 
            ]
            ; Filename
            copy tmp 0 30 amiga-char (
                if tmp [insert tail rebol-file tmp]
            )
        ] [ ; Parse failed
            rebol-file: none
        ]
    ]
    MacOS: [
        ; REBOL doesn't support "/" in MacOS file names
        if not find file-name "/" [
            rebol-file: parse/all file-name ":"
            either (first file-name) = #":" [
                change rebol-file "./"
            ] [
                if (length? rebol-file) > 1 [insert rebol-file "/"]
            ]
            rebol-file: to-file rebol-file
        ]
    ]
    Unix: [
        rebol-file: to-file file-name
    ]
    
    ; Perform the transformation
    either any [(none? file-name) (file-name = "")] [
        rebol-file: none
    ] [
        rebol-file: none
        if not platform [which: fourth system/version]
        switch/default which reduce [
            1 Amiga 2 MacOS 3 DOS 4 Unix 5 Unix 6 Unix
            7 Unix 8 Unix 9 Unix 10 Unix 11 Unix 12 Unix
            13 DOS 15 DOS 16 DOS 17 Unix 19 Unix 20 Unix
            22 Unix 23 Unix 24 Unix 26 Unix
            ; I don't know the syntax for the following:
            ; 14 PalmOS 18 OS400 21 VMS 25 Netware
        ] [
            rebol-file: to-file parse/all file-name ":\/"
        ]
    ]
    ; Apply dir or default as appropriate
    either rebol-file [
        if all [(dir) ((last rebol-file) <> #"/")] [
            insert tail rebol-file #"/"
        ]
    ] [
        if file? default-file [rebol-file: copy default-file]
    ]
    ; Return rebol-file
    head rebol-file
] in init 'self init

] ; end use
] ; end if

; Change the application dirs to rebol format
use [tmp] [
    while [not tail? application-dirs] [
        tmp: to-rebol-file/dir first application-dirs
        application-dirs: insert remove application-dirs any [tmp []]
    ]
]
; Prepend the path that #!REBOL is called from
application-dirs:
    head insert head application-dirs system/script/parent/path

; Find the script on the path and do it if found
either (none? system/script/args) [
    print [newline system/script/header/usage]
    if confirm "Do you want to quit? (Y or N) " [quit]
] [
    ; Find and run the script
    do/args use [arg args dir s has-ext] [
        ; Initialize the locals
        (dir: arg: s: none) (has-ext: false)
        either error? s: try [
            change-dir first application-dirs
            ; Retrieve the first arg and skip past it
            parse/all system/script/args [any " "
                ["{" copy arg to "}" skip | copy arg [to " " | to end]]
                any " " args: any skip (system/script/args: args)
            ]
            parse/all (to-rebol-file arg) [
                [copy dir some [thru "/"] (dir: to-file dir) | none]
                arg: [some [thru "."] (has-ext: true) | none] thru end
            ]
            ; print [mold dir mold arg mold join dir arg]
            either (tail? arg) [s: none] [
                foreach dir (
                    either (dir) [reduce [dir]] [application-dirs]
                ) [
                    dir: change-dir dir
                    either (
                        either has-ext [exists? s: join dir arg] [
                            any [
                                (exists? s: join dir [arg ".bat"])
                                (exists? s: join dir [arg ".cmd"])
                                (exists? s: join dir [arg ".r"])
                            ]
                        ]
                    ) [break] [s: none]
                ]
            ]
            script? s
        ] [none] [s]
    ] system/script/args
    ; Reset the args
    system/script/args: head system/script/args
]

; if confirm "Do you want to quit? (Y or N) " [quit]
end-of-REBOL-launcher: none
:end-of-REBOL-launcher

Reply via email to