JCOP Shell Details

This document provides some additional information for those who read the JCOP Shell Tutorial already but want to explore even more exciting JCShell features.

Contents:

JCShell Script Files

The JCShell supports shell scripts. A script file typically contains a sequence of JCShell commands and must have the extension .jcsh. The JCShell maintains an internal special variable (${path}) which defines the path in which it searches for script files. The variable can be set via the /set-var shell command (e.g. /set-var path c:/JCShellScripts). The JCShell always searches the current directory for script files. To execute a script just type the name of the file (without extension) in the JCShell.

Command Line/Script Parsing

The input lines read from a script are processed in the following way by the shell:

  1. Lines ending with a backslash are continued in the next line. These are combined into a single logical line.
  2. Comment lines are identified and ignored. A comment line starts with a `#' character. The command character may be preceded by white space.
  3. Leading labels are identified and are removed. A line may start with a label. The first word in a line is recognized as a label if it ends with a colon.

The resulting lines either contain a control flow statement or they call some command. If the line starts with the keyword of a control flow statement then the rest of the line is split into words. These words are arguments to the control flow statement.

keyword words

A command specifier may be one of the following:

  1. A builtin command identifier: Most of these commands start with a slash (/). They are always available since they are implemented by the shell itsself. Try ? or help to list these comands. These commands can be abbreviated. Each syllable (separated by dashes) of a command name can be abbreviated separately. Legal abbreviations of "/list-vars" are: "/list-v", "/l-vars", or "/l-v". If the abbreviation is ambigious, the shell complains.
  2. A plugin command: if a plugin is selected it offers additional commands specific to the plugin. Try ? or help to list these comands. These commands can be abbreviated in the same way as builtin commands.
  3. If a plugin with nickname p is active then the shell checks also if there is a script file named "p-c.jcsh" in one of the diectories listed by the ${path} variable. The path variable contains a semicolon separated list of directories.
  4. The command specifier c refers to a script if there is a file "c.jcsh" in one of the directories listed by the ${path} variable.
  5. If the command specifier starts with one of the strings "./", "../", or "/" then it refers to a script file with the same name.

White space splits a line into words. Strings enclosed in "..." and '...' become part of words. Characters with special meaning can be quoted using a backslash in strings. The blackslash is removed only if applied to special characters. In other cases the character sequence remains unchanged.

Labels and control flow keywords are not subject to variable evaluation. In strings enclosed in single quotes (') variables are not evaluated. All other words may contain variable references.

Examples:
/set-var x1 Hello
/set-var x2 ${abc}                    x2 has value "Hello"
/set-var x2 "${abc} ${abc}"           x2 has value "Hello Hello"
/set-var x2 '${abc}'"${abc}"          x2 has value "${abc}Hello"
/echo '\$\'\"' "\$\\\""               prints the following: \$'\"$\"

Control Flow Statements

Expression Description
if expression
...
elseif expression
...
else
...
end
The elseif and else part is optional. An elseif part may occur serveral times. The expressions are evaluated to a boolean value.
[label:] while expression
...
end
The label is optional and can be used as a reference in break and continue statements. The expression is evaluated to a boolean value.
[label:] for varname words
...
end
The label is optional and can be used as a reference in break and continue statements. The expression is evaluated to a boolean value.
break [label]
continue [label]
The label is optional and if present must refer to label infront of a while or for statement. If the label is omitted then the statement refers to the innermost while or for loop.
goto label Continue exeution at the specified label. The destination should be in the same block and the same block nesting level. You should not leave or enter an if, while, for, or try block.
try
...
catch expression
...
catch expression
...
end
If a command between the try and the first catch terminates abnormally then execution is resume at the first catch whose expression evaluates to true.

Expressions

Shell expressions consist of a set of words. Each operand and each operator must be a separate word. Subexpressions can be enclosed in parenthesis which also must appear as separate words. The following table lists the supported operators. If nothing is said the listed operators have the same semantics as in the C/Java language. The higher the row the stronger is the operator binding. Operators are usually work on integer values. If an operator works also on strings its meanining is explained.

Unary operators

Operators Description
- ! ~ Purely numeric

Binary operators

Operators Description
* / % Purely numeric
+ - Purely numeric
<< >> >>> Purely numeric
< <= > >= If both operands are numbers then a numberic comparision is performed. Otherwise an alphanumberic comparision is done.
== != =~ The operator =~ checks is the left operand (string) matches regular expression on the right side (string). This operator is only defined on strings. If both operands to == or != are numbers then a numberic comparision is performed. Otherwise an alphanumberic comparision is done.
& Purely numeric
^ Purely numeric
| Purely numeric

All expression evaluation produces strings as result. An expression consisting of a single word evaluates to this word. In a boolean context an expression evaluates to true if it has non-zero length and does not equal "0".

Examples:
( ${num} * 5 ) - 1
${num} & 0xFF
"${str}" == "Zurich Reseach Lab"
"${str}" =~ "*Zurich*"
${num} >= 5 & ${num} <= 10

Variable Evaluation

Variable references have the following form:

${[plugin:]name[;modifier...]}

A variable refeqrence starts with a $ character and the variable body is enclosed in curly braces. An optional plugin nick name refers to a special variable maintained by the plugin. The nick name is separated by the variable name by a colon. The variable name may be followed by a sequence modifiers. Each modifier starts with a semicolon. The variable body inside the curly braces is subject to variable expansion.

Special variables reflect some state of the plugin and the applet on a card.

Currently, there are no modifiers defined.

Examples:
/set-var x1 Hello
/set/var x2 1
/echo ${x1}                    prints Hello
/echo ${x${x2}}                also prints Hello

Special Variables

path
A semicolon separated list of directories. The shell checks these directories for script files.

last.error
The error message of the last command. If the command was successful this variable evaluates to an empty string.

plugin
The nickname of the current plugin or an empty string if no plugin active.

terminal
The opened terminal or an empty string if not connected to a terminal.

0
The name of the script.

1, ..., N
If the varible name is a number then it refers to the positional as passed to the script or shell process.