]> git.sur5r.net Git - openocd/blob - src/helper/startup.tcl
improve 'help' command
[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 # 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 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
62 #
63 # We also support two level commands. "flash banks" is translated to
64 # flash_banks
65 proc unknown {args} {
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]"]
72                 }
73         }
74         # This really is an unknown command.
75         return -code error "Unknown command: $args"
76 }
77
78 proc new_target_name { } {
79         return [target number [expr [target count] - 1 ]]
80 }
81
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} {
86                 return $t
87         }
88         if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
89                 return $t
90         }
91         if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
92                 return $t
93         }
94         # make sure error message matches original input string
95         return -code error "Can't find $filename"
96 }
97 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
98
99 # Run script
100 proc script {filename} {
101         source [find $filename]
102 }
103
104 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
105
106 #########
107
108 # catch any exceptions, capture output and return output
109 proc capture_catch {a} {
110         catch {
111                 capture {uplevel $a}
112         } result
113         return $result
114 }