How can I exit Forth with a non-zero exit status?
Asked Answered
C

1

5

I would like to exit a Forth program (using Gforth 0.7.3) with a non-zero exit status.

I've tried:

1 bye

But the 1 is not interpreted as an argument to bye (and I didn't expect this to work anyway; I couldn't find any hint in the documentation that bye would accept an argument).

Note that I do not want to trigger an exception, as that also prints an error message (unless there is a way to suppress the error message of the exception from within the Forth program itself).

So, how do I exit a Forth program back to the hosted environment/OS providing a non-zero exit status? (Basically, I'm looking for the equivalent of return EXIT_FAILURE; // from main() (C) or exit(EXIT_FAILURE); (C) or System.exit(1); (Java).)

Contribution answered 25/3, 2022 at 12:21 Comment(0)
A
7

In Gforth, the internal word (bye) ( n -- ) can be used to return the exit status to the OS.

For example, the following command in Bash:

gforth -e '123 (bye)' ; echo $?

prints "123".

In general, a method to return a non-zero exit status is not standardized yet.

Acute answered 26/3, 2022 at 7:20 Comment(1)
Works, that was exactly what I was looking for, lovely, thank you!Contribution

© 2022 - 2024 — McMap. All rights reserved.