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 #Print help text for a command. Word wrap
29 #help text that is too wide inside column.
33 foreach a [lsort $ocd_helptext] {
34 if {[string length $cmd] == 0 || \
35 [string first $cmd $a] != -1 || \
36 [string first $cmd [lindex $a 1]] != -1} \
39 set cmdname [lindex $a 0]
43 if {$n > [string length $h]} {break}
45 set next_a [expr $n + $w]
46 if {[string length $h] > $n + $w} \
48 set xxxx [string range $h $n [expr $n + $w]]
49 for {set lastpos [expr [string length $xxxx] - 1]} \
50 {$lastpos >= 0 && [string compare \
51 [string range $xxxx $lastpos $lastpos] " "] != 0} \
52 {set lastpos [expr $lastpos - 1]} \
57 set next_a [expr $lastpos + $n + 1]
61 puts [format "%-25s %s" $cmdname \
62 [string range $h $n [expr $next_a-1]] ]
70 add_help_text help "Tcl implementation of help command"
73 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
75 # We also support two level commands. "flash banks" is translated to
78 # do the name mangling from "flash banks" to "flash_banks"
79 if {[llength $args]>=2} {
80 set cmd_name "[lindex $args 0]_[lindex $args 1]"
81 if {[catch {info body $cmd_name}]==0} {
82 # the command exists, try it...
83 return [eval "$cmd_name [lrange $args 2 end]"]
86 # This really is an unknown command.
87 return -code error "Unknown command: $args"
90 proc new_target_name { } {
91 return [target number [expr [target count] - 1 ]]
94 # Try flipping / and \ to find file if the filename does not
95 # match the precise spelling
96 proc find {filename} {
97 if {[catch {ocd_find $filename} t]==0} {
100 if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
103 if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
106 # make sure error message matches original input string
107 return -code error "Can't find $filename"
109 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
112 proc script {filename} {
113 source [find $filename]
116 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
120 # catch any exceptions, capture output and return output
121 proc capture_catch {a} {