Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hashbang support to execute scripts #570

Merged
merged 2 commits into from
Jan 26, 2024
Merged

Add hashbang support to execute scripts #570

merged 2 commits into from
Jan 26, 2024

Conversation

vinc
Copy link
Owner

@vinc vinc commented Jan 24, 2024

The files starting with [0x7F, b'E', b'L', b'F'] are identified as ELF binaries and those starting with [0x7F, b'B', b'I', b'N'] are identified as flat binaries.

With this PR the files starting with a hashbang [b'#', b'!'] will be identified as scripts by the shell and the interpreter following the shebang will be used to run them with the rest of the command line arguments.

See https://en.wikipedia.org/wiki/Shebang_%28Unix%29

@vinc
Copy link
Owner Author

vinc commented Jan 24, 2024

Shell script example:

~
> hello
Hello, World!

~
> read /bin/hello
#!shell

print "Hello, World!"

Lisp script example:

~
> dhcp
ip:  10.0.2.15/24
gw:  10.0.2.2
dns: 10.0.2.3

~
> ntp time.cloudflare.com
1706129474

~
> read /bin/ntp
#!lisp

(load "/lib/lisp/core.lsp")

(var addr (or (host (head args)) (head args)))
(var port 123)
(var socket (socket/connect "udp" addr port))

(var req (map (fun (i) (if (eq? i 0) 0x33 0)) (range 0 48)))
(file/write socket req)
(var res (file/read socket 48))

(var buf (slice res 40 4))
(var time (- (bin->num (concat '(0 0 0 0) buf) "int") 2208988800))
(print time)

And this is the main reason why I chose the symbol # for comments in this lisp dialect to be aligned with the shell.

@vinc vinc changed the title Add shebang support to execute scripts Add hashbang support to execute scripts Jan 25, 2024
@vinc
Copy link
Owner Author

vinc commented Jan 25, 2024

This last commit allows white spaces between the hashbang and the interpreter: #! lisp

@vinc vinc marked this pull request as ready for review January 26, 2024 08:22
@vinc vinc merged commit 0ba8752 into trunk Jan 26, 2024
1 check passed
@vinc vinc deleted the feature/shebang branch January 26, 2024 08:22
@vinc vinc mentioned this pull request Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant