Tool Command Language: 6.2 Coding: Examples

Up: GEOS SDK TechDocs | Up | Prev: 6.1 Swat Data Structure Commands | Next: 7 Using a New Command

This section will contain a few examples of Tcl code for Swat commands, showing the use of some of included Tcl commands. A good way to view the code for a particular procedure is to type:

info body <procname>

on the Swat command line. This will print out the body of the given <procname> . One thing to watch out for, however, is the case when a procedure has not been loaded into Swat yet (i.e. it has not been used yet). If this is the case, Swat will have no information about the procedure and will thus print nothing. The command must be loaded into Swat either with the load command, or by just typing the command name which will usually autoload the command. (See Using a New Command .) Then the info body <procname> command can be used.

Some code examples:

The Whatat Command

[ defcommand whatat {addr} output
{Given an address, print the name of the variable at that address}
{
  var a [ sym faddr var $addr]
	 if {[ null $a]}{
	  echo *nil*
	} else {
	 echo [sym name $a]
 }
}]

This example shows the code of the whatat command. Note the use of the sym (an abbreviation for symbol ) command to find the address of the given variable <addr> of class <var>.

The Bytes Command

1	 var addr [ get-address $addr ds:si]
2	var base [ index [addr-parse $addr] 1]
3	 echo {Addr: +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f}
4	#fetch the bytes themselves
5	var bytes [ value fetch $addr [  type make array $num [type byte]]]
6	#
	# $s is the index of the first byte to display on this row, $e is the
	# index of the last one. $e can get > $num. The loop handles this case.
	#
	var s 0 e [ expr 16-($base&0xf)-1]
	#
	# $pre can only be non-zero for the first line, so set it once here.
	# We'll set it to zero when done with the first line.
	# $post can be non-zero only for the last line, but we can't just
	# set it to zero and let the loop handle it, as the first may be the
	# last, so...
	#
	var pre [expr 16-($e-$s)-1]
	if {$e > $num} {
	var post [expr $e-($num-1)]
	} else {
	var post 0
	} 

	[ for {var start [expr {$base&~0xf}]}
	{$s < $num}
	{var start [expr $start+16]}
	{
28	#extract the bytes we want
29	var bs [ range $bytes $s $e]

30	echo [ format {%04xh: %*s%s%*s "%*s%s%*s"} $start
	[expr $pre*3] {}
	[ map i $bs {format %02x $i}]
	[expr $post*3] {}
	$pre {}
	[ mapconcat i $bs {
	if {$i >= 32 && $i < 127} {
	format %c $i
	} else {
	format .
	}
	}]
	$post {}]
	var s [expr $e+1] e [expr $e+16] pre 0
	if {$e >= $num} {
	var post [expr $e-($num-1)]
	}
	}]
	 set-address $addr+$num-1
	 set-repeat [format {$0 {%s} $2} $addr+$num]

This example shows the code for the bytes commands. Notice the use of the type command on the fifth line, and the range command on the twenty-ninth line.


Up: GEOS SDK TechDocs | Up | Prev: 6.1 Swat Data Structure Commands | Next: 7 Using a New Command