]> git.gag.com Git - fw/openocd/blob - src/helper/startup.tcl
helper/startup.tcl: fix execution stack frame of wrapped commands
[fw/openocd] / src / helper / startup.tcl
1 # Defines basic Tcl procs that must exist for OpenOCD scripts to work.
2 #
3 # Embedded into OpenOCD executable
4 #
5
6
7 # We need to explicitly redirect this to the OpenOCD command
8 # as Tcl defines the exit proc
9 proc exit {} {
10         ocd_throw exit
11 }
12
13 # All commands are registered with an 'ocd_' prefix, while the "real"
14 # command is a wrapper that calls this function.  Its primary purpose is
15 # to discard 'handler' command output.
16 # Due to the two nested proc calls, this wrapper has to explicitly run
17 # the wrapped command in the stack frame two levels above.
18 proc ocd_bouncer {name args} {
19         set cmd [format "ocd_%s" $name]
20         set type [eval ocd_command type $cmd $args]
21         set errcode error
22         set skiplevel [expr [eval info level] > 1 ? 2 : 1]
23         if {$type == "native"} {
24                 return [uplevel $skiplevel $cmd $args]
25         } else {if {$type == "simple"} {
26                 set errcode [catch {uplevel $skiplevel $cmd $args}]
27                 if {$errcode == 0} {
28                         return ""
29                 } else {
30                         # 'classic' commands output error message as part of progress output
31                         set errmsg ""
32                 }
33         } else {if {$type == "group"} {
34                 catch {eval ocd_usage $name $args}
35                 set errmsg [format "%s: command requires more arguments" \
36                         [concat $name " " $args]]
37         } else {
38                 set errmsg [format "invalid subcommand \"%s\"" $args]
39         }}}
40         return -code $errcode $errmsg
41 }
42
43 # Try flipping / and \ to find file if the filename does not
44 # match the precise spelling
45 proc find {filename} {
46         if {[catch {ocd_find $filename} t]==0} {
47                 return $t
48         }
49         if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
50                 return $t
51         }
52         if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
53                 return $t
54         }
55         # make sure error message matches original input string
56         return -code error "Can't find $filename"
57 }
58 add_usage_text find "<file>"
59 add_help_text find "print full path to file according to OpenOCD search rules"
60
61 # Find and run a script
62 proc script {filename} {
63         uplevel #0 [list source [find $filename]]
64 }
65 add_help_text script "filename of OpenOCD script (tcl) to run"
66 add_usage_text script "<file>"
67
68 #########
69