Windows scripts Marc Feeley (11 Mar 2001 17:27 UTC)
Re: Windows scripts Eli Barzilay (11 Mar 2001 18:41 UTC)
Re: Windows scripts sperber@xxxxxx (20 Mar 2001 10:45 UTC)

Windows scripts Marc Feeley 11 Mar 2001 17:27 UTC

I've looked into the syntax of Windows scripts and the compatibility
with the SRFI 22 proposal.  First here is some information on Windows
scripts.

Windows scripts (usually called "batch files") are files with a ".bat"
or ".cmd" extension.  This extension can be omitted when invoking a
script.  The script is composed of commands, there is no need for a
special "#!..." header.  Commands are echoed to standard output as
they are executed, except if they are prefixed with an at-sign (@) or
if the command "@echo off" has been executed in the script.  The
script name and arguments are named %0, %1, ..., %9.

So it seems that Unix scripts, which must start with a header line
"#!....", can't be directly used as Windows scripts, because the
header line will be echoed to standard output.  So the best that can
be expected is that the top of the script is platform dependent and
the Scheme code in the script is identical under Windows and UNIX.

The best I have come up with is to structure Windows scripts with
a single header line like this:

    @;scheme-script %0 --call main %1 %2 %3 %4 %5 %6 %7 %8 %9
    (define (main arg1 arg2)
      (write (+ (string->number arg1) (string->number arg2)))
      (newline))

There are a couple of requirements for this to work properly.

There must exist a **batch** file "scheme-script.bat" in the PATH that
will be called by the Scheme script.  It must be a batch file to
exploit a misfeature of Windows...  When an executable program (that
is not a batch file) is launched from a batch file, execution will
continue at the next line (as you would expect).  But when a batch
file is launched from a batch file, execution will not continue at the
next line (i.e.  it is like an implicit UNIX "exec").  You must
use "call <batch_file>" to continue at the next line.

For Gambit the batch file "scheme-script.bat" could be something like
this:

    @shift
    @gsi -e "(define @ #f)(load \"%0\")(apply %2 (cdddr (argv)))(exit)" %3 %4 %5 %6 %7 %8 %9

Note the definition of the "@" as a variable before the script is
loaded.  When the script is loaded by the interpreter, it will see a
reference to the variable "@" (which is defined so it doesn't generate
an error and does nothing) and then a semicolon which starts a comment
(so the rest of the line is also ignored).

So it seems that the only requirement that should be added to SRFI 22
to allow Windows scripts, is that the Scheme implementation supply
a predefined variable "@".  Note that the R5RS does not allow the
at-sign as the first character of an identifier.  So alternatively,
SRFI 22 could require that the Scheme implementation define "@" as
a token with the same meaning as "#!", or as a token that
evaluates to #f, or whatever (but not an error).

Marc