Wednesday, November 11, 2009

A Few Examples










































A Few Examples



I’ve described some of the implementation behind
scripts. Now, I’ll present a few examples that illustrate how scripts
can be used. Some of the command details in the examples are not
documented in this text; refer to the CD for complete details.




Example #1: ping


Instead of providing gobs of commands in the
command table, MicroMonitor’s command set philosophy is to provide
low-level capabilities that can be combined through scripting to form a
variety of higher-level capabilities. This script is an example of that
philosophy. It builds on the capabilities of the icmp command to create a ping command. The icmp command supports some ICMP features at the MicroMonitor command line. The echo argument to the icmp command is basically a ping; however, the command ping is not in MicroMonitor’s command set because it can be built with a script (see Listing 8.10).



Listing 8.10: Building ping.







#
# PING:
# Script using "icmp echo" and "argv" commands:
# Syntax: ping IP_ADDRESS [optional ping count]
#
argv -v
if $ARGC eq 2 goto PING_1
if $ARGC eq 3 goto PING_N
echo $ARG0: requires IP address
exit

# PING_1:
icmp echo $ARG1
exit

# PING_N:
icmp -c $ARG2 echo $ARG1
exit














The argv –v command retrieves command line arguments passed to ping and populates shell variables appropriately. For this example, if the script was invoked as


ping 135.3.94.1 6


$ARG0 would contain ping, $ARG1 would contain 135.3.94.1, $ARG2 would contain 6, and $ARGC would contain 3. Notice the use of comments, conditional branching, and goto tags. The PING_1 and PING_N
tags mark the start of the two different logical paths taken depending
on the command line argument count. The rest is pretty easy to
understand.





Example #2: Shell Arrays



Listing 8.11
uses the nested shell variable capability in MicroMonitor’s command
interpreter to implement simple arrays. All syntax is similar to that
of Listing 8.10, but this example demonstrates the use of MicroMonitor’s nested shell variable capability.




Listing 8.11: Implementing Simple Arrays.






#
# Build a list (or array) of vegetables:
#
set VEG_1 Lettuce
set VEG_2 Broccoli
set VEG_3 Carrot
set VEG_4 Corn

set idx 1
set max 4

#
# Now print the list of vegetables using the 'idx' shell variable
# as an index:
#
# TOP:
if $idx gt $max exit # Exit loop when idx is greater than max.
echo ${VEG_${idx}} # Use nested shell var to print $VEG_N.
set -i idx # Increment content of idx.
goto TOP # Do it again.















The output of the script of Listing 8.11 would be


Lettuce

Brocoli

Carrot

Corn

The set command assigns a value to a shell variable. When the CLI processor parses the line


echo ${VEG_${idx}}

it makes two passes through the shell variable processing stage. The first pass converts ${idx} to its value, and the second pass converts ${VEG_N} (where N is the value of $idx) to its value. Note that the idx shell variable is used like an index into an array of names, where the array is called VEG_.





Example #3: Subroutines, Conditional Branching, and TFS, etc.


The script in Listing 8.12 further demonstrates the use of conditional branching and subroutines. Listing 8.12 uses some of the features of the tfs
command as part of the demonstration. The script loads an executable
file from TFS flash memory to DRAM. The script informs the user if the
file is compressed.



Listing 8.12: Using a Script to Load an Executable.






#
# Load file:
# Script syntax: fload {filename}
#
argv -v
if $ARGC ne 2 goto USAGE

# See if file exists...
tfs size $ARG1 FSIZE
if $FSIZE seq \$FSIZE goto NOFILE

# See if file is compressed...
if -t iscmp $ARG1 gosub COMPRESSED else gosub NCOMPRESSED

# Verbosely load the file to dram...
tfs -v ld $ARG1
exit

#
# COMPRESSED:
#
echo File $ARG1 is compressed.
echo
return

#
# NCOMPRESSED:
#
echo File $ARG1 is not compressed.
echo
return

#
# NOFILE:
#
echo File $ARG1 not found.
exit

#
# USAGE:
#
echo Usage: $ARG0 {filename}
exit













After basic argument testing, the script uses the tfs size command to populate the shell variable FSIZE with the size of the file. If the file is present, FSIZE will be set accordingly; otherwise FSIZE will be cleared. If the FSIZE variable is cleared, the CLI processor leaves the $FSIZE string untouched, so $FSIZE and \$FSIZE
would then appear as identical strings because the backslash in the
second string would be removed by the CLI parsing. The second if statement (if $FSIZE seq ...) deals with this comparison. The third if demonstrates the additional testing capability of if using the –t option. In this test, the if is testing the file to determine whether or not it is compressed. The COMPRESSED tag points to a subroutine, and NOFILE and USAGE
are simple branch destinations, each of which prints out some user
message. Finally, the file is loaded. The output of the script when
passed the name of a non-compressed file (in this case, file abc) is shown in Listing 8.13.



Listing 8.13: The Load Statistics.






File abc is not compressed.

.text : copy 852808 bytes from 0xf0142fdc to 0x00080000
.rodata : copy 68964 bytes from 0xf0213324 to 0x00150348
.data : copy 61984 bytes from 0xf022408c to 0x001610b0
.ctors : copy 12 bytes from 0xf02332ac to 0x001702d0
.dtors : copy 12 bytes from 0xf02332b8 to 0x001702dc
.got : copy 16 bytes from 0xf02332c4 to 0x001702e8
.sbss : set 916 bytes at 0x001702f8 to 0x00
.bss : set 91252 bytes at 0x00170690 to 0x00
.comment : 17160 bytes not processed (tot=17160)
.shstrtab : 101 bytes not processed (tot=17261)
entrypoint: 0x80000













The preceding scripts demonstrate the scripting
capability of the script runner. The scripts also show the versatility
of some of the commands built into MicroMonitor’s CLI table. For full
documentation of the command set, refer to the CD.





































No comments:

Post a Comment