1 # Defines basic Tcl procs that must exist for OpenOCD scripts to work.
3 # Embedded into OpenOCD executable
6 # Help text list. A list of command + help text pairs.
8 # Commands can be more than one word and they are stored
9 # as "flash banks" "help text x x x"
11 proc add_help_text {cmd cmd_help} {
13 lappend ocd_helptext [list $cmd $cmd_help]
16 proc get_help_text {} {
22 # We need to explicitly redirect this to the OpenOCD command
23 # as Tcl defines the exit proc
28 proc cmd_help {cmdname h indent} {
29 set indent [expr $indent * 2]
31 set fmt_str [format "%%%ds%%-%ds %%s" $indent [expr 25 - $indent]]
32 set w [expr 50 - $indent]
36 if {$n > [string length $h]} {break}
38 set next_a [expr $n + $w]
39 if {[string length $h] > $n + $w} \
41 set xxxx [string range $h $n [expr $n + $w]]
42 for {set lastpos [expr [string length $xxxx] - 1]} \
43 {$lastpos >= 0 && [string compare \
44 [string range $xxxx $lastpos $lastpos] " "] != 0} \
45 {set lastpos [expr $lastpos - 1]} \
50 set next_a [expr $lastpos + $n + 1]
54 puts [format $fmt_str "" $cmdname \
55 [string range $h $n [expr $next_a - 1]] ]
61 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
63 # We also support two level commands. "flash banks" is translated to
66 # do the name mangling from "flash banks" to "flash_banks"
67 if {[llength $args]>=2} {
68 set cmd_name "[lindex $args 0]_[lindex $args 1]"
69 if {[catch {info body $cmd_name}]==0} {
70 # the command exists, try it...
71 return [eval "$cmd_name [lrange $args 2 end]"]
74 # This really is an unknown command.
75 return -code error "Unknown command: $args"
78 proc new_target_name { } {
79 return [target number [expr [target count] - 1 ]]
82 # Try flipping / and \ to find file if the filename does not
83 # match the precise spelling
84 proc find {filename} {
85 if {[catch {ocd_find $filename} t]==0} {
88 if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
91 if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
94 # make sure error message matches original input string
95 return -code error "Can't find $filename"
97 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
100 proc script {filename} {
101 source [find $filename]
104 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
108 # catch any exceptions, capture output and return output
109 proc capture_catch {a} {