]> git.sur5r.net Git - openocd/blob - src/helper/startup.tcl
split startup.tcl file across modules
[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 #Print help text for a command. Word wrap
29 #help text that is too wide inside column.
30 proc help {args} {
31         global ocd_helptext
32         set cmd $args
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} \
37                 {
38                         set w 50
39                         set cmdname [lindex $a 0]
40                         set h [lindex $a 1]
41                         set n 0
42                         while 1 {
43                                 if {$n > [string length $h]} {break}
44
45                                 set next_a [expr $n + $w]
46                                 if {[string length $h] > $n + $w} \
47                                 {
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]} \
53                                         {
54                                         }
55                                         #set next_a -1
56                                         if {$lastpos != -1} {
57                                                 set next_a [expr $lastpos + $n + 1]
58                                         }
59                                 }
60
61                                 puts [format "%-25s %s" $cmdname \
62                                                 [string range $h $n [expr $next_a-1]] ]
63                                 set cmdname ""
64                                 set n [expr $next_a]
65                         }
66                 }
67         }
68 }
69
70 add_help_text help "Tcl implementation of help command"
71
72
73 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
74 #
75 # We also support two level commands. "flash banks" is translated to
76 # flash_banks
77 proc unknown {args} {
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]"]
84                 }
85         }
86         # This really is an unknown command.
87         return -code error "Unknown command: $args"
88 }
89
90 proc new_target_name { } {
91         return [target number [expr [target count] - 1 ]]
92 }
93
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} {
98                 return $t
99         }
100         if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
101                 return $t
102         }
103         if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
104                 return $t
105         }
106         # make sure error message matches original input string
107         return -code error "Can't find $filename"
108 }
109 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
110
111 # Run script
112 proc script {filename} {
113         source [find $filename]
114 }
115
116 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
117
118 #########
119
120 # catch any exceptions, capture output and return output
121 proc capture_catch {a} {
122         catch {
123                 capture {uplevel $a}
124         } result
125         return $result
126 }