factor help script command into parts
[openocd.git] / 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 # Help text list. A list of command + help text pairs.
7 #
8 # Commands can be more than one word and they are stored
9 # as "flash banks" "help text x x x"
10
11 proc add_help_text {cmd cmd_help} {
12 global ocd_helptext
13 lappend ocd_helptext [list $cmd $cmd_help]
14 }
15
16 proc get_help_text {} {
17 global ocd_helptext
18 return $ocd_helptext
19 }
20
21
22 # We need to explicitly redirect this to the OpenOCD command
23 # as Tcl defines the exit proc
24 proc exit {} {
25 ocd_throw exit
26 }
27
28 proc cmd_help {cmdname h indent} {
29 set indent [expr $indent * 2]
30
31 set fmt_str [format "%%%ds%%-%ds %%s" $indent [expr 25 - $indent]]
32 set w [expr 50 - $indent]
33 set n 0
34
35 while 1 {
36 if {$n > [string length $h]} {break}
37
38 set next_a [expr $n + $w]
39 if {[string length $h] > $n + $w} \
40 {
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]} \
46 {
47 }
48 #set next_a -1
49 if {$lastpos != -1} {
50 set next_a [expr $lastpos + $n + 1]
51 }
52 }
53
54 puts [format $fmt_str "" $cmdname \
55 [string range $h $n [expr $next_a - 1]] ]
56 set cmdname ""
57 set n [expr $next_a]
58 }
59 }
60
61 #Print help text for a command. Word wrap
62 #help text that is too wide inside column.
63 proc help {args} {
64 global ocd_helptext
65 set cmd $args
66 foreach a [lsort $ocd_helptext] {
67 if {[string length $cmd] == 0 || \
68 [string first $cmd $a] != -1 || \
69 [string first $cmd [lindex $a 1]] != -1} \
70 {
71 cmd_help [lindex $a 0] [lindex $a 1] 0
72 }
73 }
74 }
75
76 add_help_text help "Tcl implementation of help command"
77
78
79 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
80 #
81 # We also support two level commands. "flash banks" is translated to
82 # flash_banks
83 proc unknown {args} {
84 # do the name mangling from "flash banks" to "flash_banks"
85 if {[llength $args]>=2} {
86 set cmd_name "[lindex $args 0]_[lindex $args 1]"
87 if {[catch {info body $cmd_name}]==0} {
88 # the command exists, try it...
89 return [eval "$cmd_name [lrange $args 2 end]"]
90 }
91 }
92 # This really is an unknown command.
93 return -code error "Unknown command: $args"
94 }
95
96 proc new_target_name { } {
97 return [target number [expr [target count] - 1 ]]
98 }
99
100 # Try flipping / and \ to find file if the filename does not
101 # match the precise spelling
102 proc find {filename} {
103 if {[catch {ocd_find $filename} t]==0} {
104 return $t
105 }
106 if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
107 return $t
108 }
109 if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
110 return $t
111 }
112 # make sure error message matches original input string
113 return -code error "Can't find $filename"
114 }
115 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
116
117 # Run script
118 proc script {filename} {
119 source [find $filename]
120 }
121
122 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
123
124 #########
125
126 # catch any exceptions, capture output and return output
127 proc capture_catch {a} {
128 catch {
129 capture {uplevel $a}
130 } result
131 return $result
132 }