newbietcl
newbietcl

Reputation: 11

Best way to read ERRORLEVEL codes for a windows executable, executed from within a TCL script

I am pretty new to Tcl world, so please excuse me of any naive questions.

I am trying to execute a windows executable from a Tcl procedure. At the same time, I want to read the %errorlevel% outputted from the windows executable and throw some meaningful messages to the Tcl shell.

Ex:

I have a windows executable "test.exe arg1" that outputs various errorcodes based on the interrupts:

0 - If the script successfully executed
1 - If the user interrupted the process manually, and the process exited.
2 - If the user login is not found, process exited.
3 - If the "arg1" is not specified, process exited

In my TCL script, I have the following:

set result [catch {exec cmd /c test.exe arg1}]
if { $result == 3 } { 
    puts "Argument undefined"
} elseif { $result == 2 } {
    puts "Login Failed"
} elseif { $result == 1 } {
    puts "Process Cancelled by user" 
} elseif { $result == 0 } {
    puts "Command successful"
}

It appears that the output of the catch command is either 1 or 0, and it would not read the appropriate %errorlevel% information from the windows terminal. What is the best way to trap the %errorlevel% info from the Windows executable and process appropriate error messages using Tcl?

Upvotes: 0

Views: 138

Answers (1)

Schelte Bron
Schelte Bron

Reputation: 4813

The catch command takes two optional arguments: "resultVarName" and "optionsVarName". If you use those, you can examine the second one for the return code:

catch {exec cmd /c test.exe arg1} output options
puts [dict get $options -errorcode]

That would report something like: CHILDSTATUS 15567 1

The fields represent the error type, process ID, and the exit code. So you should check that error type is "CHILDSTATUS" before taking that last number as the exit code. Other error types will have different data. This is actually more easily done with the try command:

try {
    exec cmd /c test.exe arg1
} on ok {output} {
    puts "Command successful"
} trap {CHILDSTATUS} {output options} {
    set result [lindex [dict get $options -errorcode] end]
    if {$result == 3} {
       puts "Argument undefined"
    } elseif {$result == 2} {
       puts "Login Failed"
    } elseif {$result == 1} {
       puts "Process Cancelled by user"
    }
}

Note: I tested this on linux, but it should work very similar on windows.

Upvotes: 2

Related Questions