Returning exit code

From BeebWiki
Jump to: navigation, search

A program can return an exit code by setting the user byte with OSBYTE &01. The calling program can then read the exit code. Depending on the called program, the calling program may be returned to at the calling point, or it may be re-run. Consequently, the calling program needs to read the exit code both when it starts and immediately after calling another program.

The value &0 should be returned for no error occured. If the called program does not know about exit codes, and never calls OSBYTE 01, then the calling program will cleanly receive a no error code.

Contents

BASIC code

Calling program:

   at program startup...
   exit%=FNbyte(1,0,0)
   ...
   when calling external command...
   OSCLI command$
   exit%=FNbyte(1,0,0)
   ...
   when calling external BASIC program...
   CHAIN program$
   REM exit code will be read when this program is re-run

Called program:

   at program exit:
   OSCLI "FX1,"+STR$exit%
   IF ASCquit$=42:OSCLI quit$ ELSE IF quit$<>"":CHAIN quit$

6502 code

Calling program:

   LDX #cmd AND 255
   LDY #cmd DIV 256          :\ Point to program to call
   JSR OS_CLI                :\ Run program
   LDA #1:LDX #0:JSR OSBYTE  :\ Read exit code

Called program finishes with:

   LDX #exit_code
   LDA #1:JMP OSBYTE         :\ Set exit code and return

C code

Calling program:

   exitcode=system(program);

Called program finishes with:

   exit(exitcode);

See Also

Jgharston 10:56, 30 April 2008 (BST)