Does Chicken Scheme have an equivalent to Perl's $0?
Asked Answered
C

3

5

How can I reliably get the script name in Chicken Scheme?

It seems that -ss eats up the script name, so it's not visible unless I use dot slash to run my scripts.

scriptedmain.scm:

#!/usr/bin/env csi -q

(display (command-line-arguments))
(display "\n")
(exit)

Trace:

$ ./scriptedmain.scm 
(-q ./scriptedmain.scm)
wonko:Desktop andrew$ csi -ss scriptedmain.scm 
()
Copywriter answered 4/3, 2011 at 0:46 Comment(0)
R
6

This is a late response, so may not be of use to the original poster. But to any others who may come across this question, the simple answer is to use the parameter:

(program-name)

This should return the correct name for all situations. Docs here.

Rabblerouser answered 11/9, 2011 at 22:58 Comment(6)
Better late than never. Thanks dude!Copywriter
My latest version works for all cases except compiled. For some reason, it just sits there.Copywriter
@Copywriter : Not sure what you mean by "all cases except compiled". I looked at your example scriptedmain.scm below - with program-name, and it works as an interpreted script, or as a compiled native executable. BTW, I wouldn't bother with that scaffolding (bash -> csi). Its not really needed#!/usr/bin/env chicken-scheme #!/usr/bin/csi -sRabblerouser
BTW, I wouldn't bother with that scaffolding (bash -> csi). Its not really needed. Put this in your ~/.csirc: (register-feature! 'in-repl) Start your scripts with either: #!/usr/bin/env chicken-scheme #!/usr/bin/csi -s And end them with the following block: (cond-expand (in-repl ; do something here) (else (main (command-line-arguments)))) Now your code will just work, whether you load the file into the repl and interactively develop, execute the file as a script through the interpreter, or compile it into a native executable and run it.Rabblerouser
Sorry about the code formatting. I guess the comments don't respect inline code :(Rabblerouser
I'll remove the bash -> csi scaffolding as soon as I can get this working in compiled mode. For some reason, on my system the compiled version never runs (main).Copywriter
E
1

(argv) should do the job. Example:

#!/usr/local/bin/csi -script

(display (argv)) (newline) (exit)

prints (/usr/local/bin/csi -script ./test.scm)

Emir answered 4/3, 2011 at 7:18 Comment(2)
Almost! You have to add code for the special case where the script is compiled with csc; then the program is (list-ref (argv) 0).Copywriter
With "script" i mean "not compiled". There are other solutions to distinguish between interpreted and compiled programs.Emir
C
1

scriptedmain.scm will run (main) and print the program name in the following cases:

Run from the interpreter:

csi -ss scriptedmain.scm

Run from the interpreter using shebangs:

./scriptedmain.scm

Compiled:

csc -o scriptedmain scriptedmain.scm
./scriptedmain

Added to GitHub.

#!/bin/sh
#|
exec csi -ss $0 ${1+"$@"}
exit
|#

(define (main)
    (display (format "Program: ~a\n" (program-name)))
    (exit))

(if (not (equal? (program-name) "csi"))
    (main))
Copywriter answered 5/3, 2011 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.