Reputation: 13
I've been trying to use to use a catch and exec to run a compiled C program that returns an int at the end, but so far I can only get the return values of 0 and 1, 0 returning when the c program returns 0 and 1 for anything else. Is there a way to get any return value (eg. 5) from the C program?
Upvotes: 0
Views: 599
Reputation: 5723
Yes. Use the third argument to the catch
command to retrieve the return options:
set returnvalue 0
if { [catch { exec ./myprogram } result retopts] } {
lassign [dict get $retopts -errorcode] class pid retcode
set returnvalue 1
if { $class eq "CHILDSTATUS" } {
set returnvalue $retcode
}
}
A try / on error
block can also be used:
try {
exec ./myprogram
set returnvalue 0
} on error {result retopts} {
lassign [dict get $retopts -errorcode] class pid retcode
set returnvalue 1
if { $class eq "CHILDSTATUS" } {
set returnvalue $retcode
}
}
Edit: try / trap
example:
set returnvalue 1
try {
exec ./myprogram
set returnvalue 0
} trap {CHILDSTATUS} {result retopts} {
lassign [dict get $retopts -errorcode] class pid retcode
set returnvalue $retcode
}
References: catch errorCode try
Upvotes: 2