uCon HomePage

Script Cmd: CATCH


CATCH {off | show | tagon | tagoff}
CATCH 
[-afhn] {catch_string1} [catch_string2] [catch_string3] [...] {catch_script}

Options:

-a        run the catch-script asynchronously (see note3 below).
-f        prior to populating CATCHSTRING with the result, check for special characters (see note6 below).
-h        don't halt running scripts when CATCH script is run (see note7 below).
-n        no catch-script is specified, just populate the CATCHSTRING shell variable.


This turns on (or off) uCon's ability to "catch" an arbitrary string sent from the target and optionally run a script. The "catch" facility in uCon (new as of May 2008) allows uCon to run normally, but with an added bit of processing on the incoming characters from the target... If the target sends back to the uCon console one of the (up to 32) strings specified by "catch_stringN" above, then uCon will halt all active scripts asynchronously, and run the script specified by "catch_script" above. uCon will complete the catch_script prior to continuing with any futher stream processing (see note2 below)

Note that only one script can be specified, so, to allow the script to know which string caused it to run, the shell variable CATCHSTRING will be loaded with the string that caused the trigger; thus allowing the script to react to the string it caught with a simple set of "if" statements (see examples below).

New as of  June 2014:  
The "catch string" specified may contain dots and a special case "*\n" to deal with catching some initial string following by anything up to the next end-of-line.  This means you can specifiy an "any-character" position(s) within the catchstring; and when caught, the resulting value of the CATCHSTRING shell variable will contain the actual values caught.  This is in addition to the ability to handle any non-printable character using "\b, \r, \n & \xHH".

New as of  May 2015:  
The catch string can now simply trigger an end-of-line line tag (currently, this tag is the 4-character string: <<**.  This can accompany a script execution or it can be used in place of a script.   This just allows the user to have uCon place a line tag at the far-right edge of the console window on the same line that that triggered the catch.  This just allows a user to more easily notice a trigger.  Line tagging mode is established with the new "tagon | tagoff" arguments, or it can be specified by the "Line Tagging" checkbox within the dialog (see below).  Check out Example 5 below.

NOTE1:
The state of the catch facility (script/strings) is shown through the Config->Environment->Display dialog. If catch is disabled nothing is dumped; if enabled, it will display the script and strings.

NOTE2:
If the -n option is specified, then no catch script is specified; hence, no script is executed if a catch string is caught.  Instead,  the CATCHSTRING is populated and the equivalent of "CATCH off" is automatically executed.

NOTE3:
Normally (with -a not specified), uCon runs the catch-script in a blocking mode; meaning that it holds off on processing any additional input from the comport until the catch-script completes.  There are many cases where this is not ideal; hence if the -a option is specified, the script will be started at the same trigger point; however, the comport thread will not be blocked while the catch-script runs.  Also note that when -a is used, when a catch-script is triggered future triggers are immediately disabled (until the next CATCH script command is invoked).  This is because of the fact that the catch-script is now running while additional comport activity is being received and there would otherwise be potential for a subsequent catch-trigger to override (kill) the running catch-script.  

NOTE4:
The CATCHSTRING shell variable is likely to contain whitespace, so be aware of the need to put the shell variable within double quotes as needed to make sure that the string is parsed as a single token.

NOTE5:
Basic 'catch' setup can also be done with the Scripts->Catch dialog box.

This dialog provides a subset of what the CATCH script command provides.  It only allows the user to specify one "catch string".  For convenience, the "Clear" button can be used to quickly disable any prior CATCH setup (similar to "CATCH off" script command).

NOTE6:
When using the "*\n" capability in CATCH, its possible that the caught text will include characters that (when pushed into the CATCHSTRING shell variable) will cause confusion on the command line.  The '-f' (fixup) option will automatically insert a '\' in front of a double qoute or a backslash so that the script command line will properly parse the CATCHSTRING shell variable. 

NOTE7:
This "catch" facility runs essentially in the "background". All other normal uCon facilities are availalble to the user. The only thing to be aware of is if a script is active when the incoming "catch_string" is detected, that script will be asyncrhonously halted to allow the "catch_script" to run (assuming -n was not specified).  As of June 2014, the -h (don't halt) option allows the user to "carefully" run the catch script while other scripts are left active.  Typically, the other script will be waiting on a dialog box or doing something rather limited in scope.  This option is made available with the understanding that there are cases where running two scripts in parallel will cause one to step on the other; so use it at your own risk!

EXAMPLE1:

Use 2 scripts, one for setup, one for the catch...
First the setup script:

CATCH off
CATCH "panic" "alert" catch_script
EXIT

Now the catch script:

IF "$CATCHSTRING" seq panic goto CATCH_PANIC
IF "$CATCHSTRING" seq alert goto CATCH_ALERT
# CATCH_PANIC:
ECHO "PANIC condition detected"
EXIT
# CATCH_ALERT:
ECHO "ALERT condition detected"
EXIT

Run the setup script to initialize the catch mechanism to look for strings "panic" and "alert", so that in either case the script called "catch_script" is executed as a result of the catch. When "catch_script" runs, it looks at the content of the CATCHSTRING shell variable to determine where to branch.

EXAMPLE2:
Use just 1 script, for setup and catch...
Assume the file "catch_script" as follows.

IF "$CATCHSTRING" seq panic goto CATCH_PANIC
IF "$CATCHSTRING" seq alert goto CATCH_ALERT

# CATCH_SETUP:

CATCH off
CATCH "panic" "alert" "${THISSCRIPTDIR}/${THISSCRIPTNAME}"
EXIT

# CATCH_PANIC:

ECHO "PANIC condition detected"
SET CATCHSTRING
EXIT

# CATCH_ALERT:

ECHO "ALERT condition detected"
SET CATCHSTRING
EXIT

In this case, we have to assume that if CATCHSTRING is not populated, then the script is being called for setup.  At startup, this is certanily the case; however, if unsure, just click on the 'Clear' button in the dialog box shown above. To keep the state of CATCHSTRING organized, we clear the CATCHSTRING shell variable whenever the script is responding to a trigger.

EXAMPLE3:

Use just 1 script, for setup and catch, but this time just make a noise if the catch trigger occurs...
Assume the file "catch_script" as follows.

IF "$CATCHSTRING" sne \$CATCHSTRING GOTO CATCH_ALL
# CATCH_SETUP:
CATCH off
CATCH "panic" "alert" "${THISSCRIPTDIR}/${THISSCRIPTNAME}"
EXIT
# CATCH_ALL:
ECHO "PANIC condition detected"
SET SOUND C:\windows\system32\sndrec32
SET MEDIA C:\windows\media\tada.wav
SYSTEM -pw "$<}>/embedding /play /close \"$<{>MEDIA<}>\""
SET CATCHSTRING
EXIT

EXAMPLE4:

Again using one script, this demonstrates the use of 'dots' and "*\n" in the catch string as well
as the use of  $THISSCRIPTNAME...
Use of the dots...
This will catch the line that contains "U-Boot:" followed by one space character and the next 4 characters (4 dots below) regardless of their value.  That caught string will then be loaded into the CATCHSTRING shell variable.
Use of the "*\n"...
This will catch the line that contains "Linux Kernel" followed by one space character anything up to the next carriage return or line feed regardless of their value.  That caught string will then be loaded into the CATCHSTRING shell variable.
Use of the shell variables THISSCRIPTDIR & THISSCRIPTNAME...
Note that the CATCH command sets up to use the content of $THISSCRIPTNAME as the script to run when the string is caught; hence, the name of the script is independent of the script content.  Using $THISSCRIPTDIR is only necessary if the script is not in the default ucon scripts directory.  The double quotes are important because the path may have whitespace, and we want this to be seen as a single token.


SET CS2prefix "U-Boot:"
SET CS3prefix "Linux Kernel"
SET CS2 "${CS2prefix} ...."
SET CS3 "${CS3prefix} *\n"

IF "$CATCHSTRING" contains "$CS2prefix" goto CAUGHT_CS2
IF "$CATCHSTRING" contains "$CS3prefix" goto CAUGHT_CS3

# CATCH_SETUP:
CATCH off
CATCH "$CS1" "$CS2" "$CS3" 
"${THISSCRIPTDIR}/${THISSCRIPTNAME}"
EXIT

# CAUGHT_CS2:
STRING length "${CS2prefix}"
SET LEN $STRING
STRING precut "$CATCHSTRING" $LEN
ECHO "\r\nU-Boot Version: " $STRING
GOTO CLEANUP

# CAUGHT_CS3:
STRING length "${CS3prefix}"
SET LEN $STRING
STRING precut "$CATCHSTRING" $LEN
ECHO "\r\nKernel Version: " $STRING
GOTO CLEANUP

# CLEANUP
SET CATCHSTRING
EXIT

EXAMPLE5:

Lets see what the "Line Tagging" feature gives us...
Without line tagging enabled, output would be normal (assume "ls -l" as shown)...

$ ls -l /
total 4284
drwxr-xr-x   2 root   root    4096 Dec 12  2014 bin
drwxr-xr-x   3 root   root    4096 Dec 15  2014 boot
drwxr-xr-x   3 root   root    4096 Dec 15  2014 debian
drwxr-xr-x  14 root   root    5680 Jan  1 03:06 dev
drwxr-xr-x 123 root   root    4096 Jan  1 03:06 etc
drwxr-xr-x   3 root   root    4096 Dec 12  2014 home
drwxr-xr-x  19 root   root    4096 Dec 15  2014 lib
drwx------   2 root   root    4096 Jan  1 00:00 lost+found
-rw-r--r--   1 root   root 4314738 Dec 12  2014 md5sum.txt
drwxr-xr-x   4 root   root    4096 Jan  1 00:16 media
drwxr-xr-x   2 root   root    4096 Oct 16  2014 mnt
drwxr-xr-x   2 root   root    4096 Dec 12  2014 opt
dr-xr-xr-x 219 root   root       0 Jan  1 00:00 proc
drwx------   6 root   root    4096 Jan  1 00:20 root
drwxr-xr-x  21 root   root     720 Jan  1 03:06 run
drwxr-xr-x   2 root   root    4096 Dec 12  2014 sbin
drwxr-xr-x   2 root   root    4096 Dec 12  2014 srv
dr-xr-xr-x  12 root   root       0 Jan  1 03:06 sys
drwxrwxrwx   2 nobody root    4096 Jan  1 00:18 tftpboot
drwxrwxrwt   4 root   root    4096 Jan  1 04:17 tmp
drwxr-xr-x  10 root   root    4096 Dec 15  2014 usr
drwxr-xr-x  12 root   root    4096 Dec 12  2014 var
$ ls -l                                   

Now, lets assume we use the menu item Scripts->Catch dialog box to catch the string "drwx------" with Line Tagging enabled.
The same output would look like this:

$ ls -l /
total 4284
drwxr-xr-x   2 root   root    4096 Dec 12  2014 bin
drwxr-xr-x   3 root   root    4096 Dec 15  2014 boot
drwxr-xr-x   3 root   root    4096 Dec 15  2014 debian
drwxr-xr-x  14 root   root    5680 Jan  1 03:06 dev
drwxr-xr-x 123 root   root    4096 Jan  1 03:06 etc
drwxr-xr-x   3 root   root    4096 Dec 12  2014 home
drwxr-xr-x  19 root   root    4096 Dec 15  2014 lib
drwx------   2 root   root    4096 Jan  1 00:00 lost+found                      <<**
-rw-r--r--   1 root   root 4314738 Dec 12  2014 md5sum.txt
drwxr-xr-x   4 root   root    4096 Jan  1 00:16 media
drwxr-xr-x   2 root   root    4096 Oct 16  2014 mnt
drwxr-xr-x   2 root   root    4096 Dec 12  2014 opt
dr-xr-xr-x 219 root   root       0 Jan  1 00:00 proc
drwx------   6 root   root    4096 Jan  1 00:20 root                            <<**
drwxr-xr-x  21 root   root     720 Jan  1 03:06 run
drwxr-xr-x   2 root   root    4096 Dec 12  2014 sbin
drwxr-xr-x   2 root   root    4096 Dec 12  2014 srv
dr-xr-xr-x  12 root   root       0 Jan  1 03:06 sys
drwxrwxrwx   2 nobody root    4096 Jan  1 00:18 tftpboot
drwxrwxrwt   4 root   root    4096 Jan  1 04:17 tmp
drwxr-xr-x  10 root   root    4096 Dec 15  2014 usr
drwxr-xr-x  12 root   root    4096 Dec 12  2014 var
$ ls -l