scryer-prolog icon indicating copy to clipboard operation
scryer-prolog copied to clipboard

#! prolog

Open mhemery opened this issue 2 years ago • 10 comments

The rather typical shebang kills scryer prolog.

my minimal example is a file with just : #! prolog

hemery@portablepret:prolog$ ~/scryer-prolog shebang.pl 
   error(syntax_error(incomplete_reduction),read_term/3:1).

mhemery avatar Nov 17 '23 14:11 mhemery

Invalid syntax. What else to expect?

UWN avatar Nov 17 '23 17:11 UWN

Sometimes languages will add an exception to allow shebang syntax on the first line. Mechanically, this would mean ignoring the first line of the file if it starts with #!, treating it as a comment. With this feature, users on the terminal could mark the file as executable with chmod +x program.pl and execute it directly like ./program.pl without having to name the interpreter explicitly every time. Shells that execute a file that starts with a shebang will start the identified interpreter with the filename as the first argument instead.

This can be nice for integrating into unix-y systems. While it works out of the box with the many scripting languages that use # as a comment symbol (python, perl, ruby) some languages that use different comment symbols will allow shebang on the first line as a specific exception. RosettaCode.org lists a number of examples of this: https://rosettacode.org/wiki/Native_shebang

This may or may not be a good fit for scryer-prolog, but it would certainly be useful for interacting with scryer-prolog in a shell environment.


I played around with it and actually found a very hacky way to do this with scryer-prolog in bash today:

$ cat test.pl
/*usr/bin/env scryer-prolog "$0" "$@" ; exit */

run :- write(hello),nl,halt.
:- initialization(run).
$ chmod +x test.pl
$ ./test.pl
hello

The way this works is that this test.pl is both a valid shell file and prolog file. So when executing as a shell file the first line finds and executes /usr/bin/env after searching for the glob pattern /*usr/bin/env. env then executes scryer-prolog "test.pl" which runs the prolog file as a module and halts; of course it ignores the first line as a comment /* ... */. Then the shell continues and executes the next command after ;, which is exit which stops execution so the rest of the file (which is not valid shell) is ignored.

I don't know what to think about this.

infogulch avatar Nov 21 '23 21:11 infogulch

As far as I can tell, that's ingenious!

It remains valid Prolog syntax, and can also be used as a shell file.

triska avatar Nov 21 '23 21:11 triska

Great summary, @infogulch. I would much prefer a non-hacky solution, like having scryer-prolog ignore the shebang, similar to what SWI-Prolog does.

friedrich avatar Jan 15 '24 21:01 friedrich

@infogulch, one minor improvement: Instead of /*/bin/env rather use /*usr/bin/env which should reduce the search overhead.

UWN avatar Jan 16 '24 16:01 UWN

... and avoids exploits with /tmp/bin/env ...

UWN avatar Jan 16 '24 18:01 UWN