[squeak-dev] Running Squeak fro ma unix shell script file with #! squeak...

K K Subbu kksubbu.ml at gmail.com
Tue May 7 06:27:28 UTC 2019


On 07/05/19 5:11 AM, David T. Lewis wrote:
> The most common implementation is for the unix exec machinery to take the first
> part of the shebang line (/usr/bin/squeak) and let it be argv[0], then any
> remaining stuff on the line is argv[1], and the name of the invoking script
> (./hello.sq) is passed as argv[2]. This is just enough cleverness to allow the
> script (/usr/bin/squeak or /bin/sh or whatever) to receive a single argument
> (such as --) and figure out what to do from there.

Right! The linux code is in [1].

The argv[0]/argv[1] pair was introduced to support path detection when 
the interpreter location could vary between different systems.

   #!/usr/bin/env squeak

etc.. It is fortuitous that we use it to pass the default image (--).

The script handler [1] is one of many image handlers in the kernel. The 
linux kernel uses a clever indirection to remain small but extensible. 
Instead of trying to load a executable directly, the kernel chains 
through a list of handlers (aka binfmt) [2] to find  a matching 
interpreter. The trivial case is native ELF file that the processor can 
execute directly. In general, each binfmt defines an association between 
a matching rule and interpreter path

  :name  :type:offset:magic:mask :interpreter :flags

where the type/offset/magic/mask fields are parsing rules to detect file 
format and "interpreter" is the full path to the interpreter. These 
associations can even be edited at run time [3]. For instance, classic 
Squeak images can be registered with:

$ echo ':squeakv3:M::\x66\x19\x00\x00::/usr/local/bin/squeak:' | sudo tee
/proc/sys/fs/binfmt_misc/register
$ ./squeak.image hello.st

and removed with

$ echo -1 | sudo tee /proc/sys/fs/binfmt_misc/squeakv3

This match detection does not extend to argument syntax because kernel 
modules must not hard code option syntax. It is the interpreter that 
should parse its args.

A second point in this discussion is that most script interpreters use 
$# character as comment leader in their script, so embedding #! works 
out fine for them. But Squeak uses $" for its comment leader. If we wish 
to execute .st file directly, we need to create a binfmt rule that 
depends on Squeak comment character (say $" $! $"):

    :squeak:M::\x22\x5c\x22::/usr/local/lib/squeak.image:

----- hello.sq --------
"!"
FileSystem stdout .....
-----------------------

[1] https://elixir.bootlin.com/linux/latest/source/fs/binfmt_script.c
[1] See binfmt_*.c in https://elixir.bootlin.com/linux/latest/source/fs
[2] https://www.kernel.org/doc/Documentation/admin-guide/binfmt-misc.rst

Regards .. Subbu


More information about the Squeak-dev mailing list