A Few Examples

Ive described some of the implementation behind scripts. Now, Ill 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, MicroMonitors 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 MicroMonitors command set because it can be built with a script (see Listing 8.10).

Listing 8.10: Building ping .
image from book
 # # 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 
image from book
 

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 MicroMonitors command interpreter to implement simple arrays. All syntax is similar to that of Listing 8.10, but this example demonstrates the use of MicroMonitors nested shell variable capability.

Listing 8.11: Implementing Simple Arrays.
image from book
 # # 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. 
image from book
 

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.
image from book
 # # 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 
image from book
 

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.
image from book
 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 
image from book
 

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



Embedded Systems Firmware Demystified
Embedded Systems Firmware Demystified (With CD-ROM)
ISBN: 1578200997
EAN: 2147483647
Year: 2002
Pages: 118
Authors: Ed Sutter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net