bs
PURPOSE
Compiles and interprets modest-sized programs.
SYNOPSIS
bs [ file [ args ] ]
DESCRIPTION
This compiler/interpreter provides interactive program
development and debugging. To simplify program testing,
it minimizes formal data declaration and file manipu-
lation, allows line-at-a-time debugging, and provides
trace and dump facilities and run-time error messages.
The optional command line parameter file specifies a file
of program statements that the compiler reads before it
reads from the standard input. By default, statements
read from this file are compiled for later execution.
Likewise, statements entered from the standard input are
normally executed immediately (see the compile keyword on
page and the execute keyword on page ). Unless the
final operation is assignment, the result of an immediate
expression statement is displayed.
Additional command line flags can be passed to the
program using the built-in functions arg and narg
(explained in more detail on page ).
Program lines must conform to one of the following
formats:
statement
label statement
The interpreter accepts labeled statements only when it
is compiling statements. A label is a name immediately
followed by a colon. A label and a variable can have the
same name. If the last character of a line is a \ (back-
slash), the statement continues on the following physical
line.
A statement consists of either an expression or a keyword
followed by zero or more expressions.
Statement Syntax
break Exits the innermost for or while loop.
clear Clears the symbol table and removes com-
piled statements from memory. A clear
is always executed immediately.
compile [expr] Causes succeeding statements to be com-
piled (overrides the immediate execution
default). The optional expression is
evaluated and used as a file name for
further input. In this latter case, the
symbol table and memory are cleared
first. compile is always executed imme-
diately.
continue Transfers control to the loop-
continuation test of the current for or
while loop.
dump [name] Displays the name and current value of
every global variable or, optionally, of
the named variable. After an error or
interrupt, dump displays the number of
the last statement and (possibly) the
user-function trace.
exit [expr] Returns to the system level. The
expression is returned as process
status.
execute Changes to immediate execution mode
(pressing INTERRUPT [Alt-Pause] has the
same effect). This statement does not
cause stored statements to execute (see
run on page ).
for name=expr expr statement
for name=expr expr
statement . . .
next
for expr, expr, expr statement
for expr, expr, expr
statement . . .
next Repetitively performs, under the control
of a named variable, a statement (first
format) or a group of statements (second
format). The variable takes on the
value of the first expression, then is
increased by one on each loop until it
exceeds the value of the second
expression. The third and fourth
formats require three expressions sepa-
rated by commas. The first of these is
the initialization, the second is the
test (true to continue), and the third
is the loop-continuation action.
fun f ([a, . . . ]) [v, . . . ]
statement . . .
nuf Defines the function name (f), parame-
ters (a), and local variables (v) for a
user-written function. Up to 10 parame-
ters and local variables are allowed.
Such names cannot be arrays, nor can
they be I/O associated. Function defi-
nitions may not be nested.
freturn Signals the failure of a user-written
function. Without interrogation,
freturn returns zero. (See the unary
interrogation operator ? discussed on
page .) With interrogation, freturn
transfers to the interrogated
expression, possibly bypassing interme-
diate function returns.
goto name Passes control to the compiled statement
with the matching label.
ibase n Sets the input base to n. The only sup-
ported values for n are 8, 10 (the
default), and 16. Hexadecimal values
10-15 are entered as alphabetic charac-
ters a-f. A leading digit is required
when a hexadecimal number begins with an
alphabetic character (for example, "f0a"
must be entered as "0f0a"). ibase is
always executed immediately.
if expr statement
if expr
statement . . .
[else
statement . . . ]
fi Performs a statement (first format) or
group of statements (second format) if
the expression evaluates to nonzero.
The strings 0 and "" (null) evaluate as
zero. In the second format, an optional
else allows a group of statements to be
performed when the first group is not.
The only statement permitted on the same
line with an else is an if; only other
fis can be on the same line with a fi.
You can combine else and if into elif.
Only a single fi is required to close an
if . . . elif . . . [else . . . ]
sequence.
include expr The expression must evaluate to the name
of a file containing program statements.
Such statements become part of the
program being compiled. include state-
ments may not be nested, and are always
executed immediately.
obase n Sets the output base to n. The only
supported values for n are 8, 10 (the
default), and 16. Hexadecimal values
10-15 are entered as alphabetic charac-
ters a-f. A leading digit is required
when a hexadecimal number begins with an
alphabetic character (that is, "f0a"
must be entered as "0f0a"). Like ibase,
obase is always executed immediately.
onintr label
onintr Provides program control of interrupts.
In the first format, control passes to
the label given, just as if a goto had
been performed when onintr was executed.
The effect of the onintr statement is
cleared after each interrupt. In the
second format, pressing INTERRUPT
(Alt-Pause) ends bs.
return [expr] Evaluates the expression and passes the
result back as the value of a function
call. If you do not provide an
expression, the function returns zero.
run Passes control to the first compiled
statement. The random number generator
is reset. If a file contains a run
statement, it should be the last state-
ment; run is always executed imme-
diately.
stop Stops execution of compiled statements
and returns to immediate mode.
trace [expr] Controls function tracing. If you do
not provide an expression or if it eval-
uates to zero, tracing is turned off.
Otherwise, a record of user-function
calls/returns will be written. Each
return decreases by one the trace
expression value.
while expr statement
while expr
statement . . .
next while is similar to for except that only
the conditional expression for loop con-
tinuation is given.
! AIXcmd Runs an AIX command, then returns
control to bs.
#comment Inserts a comment line.
Expression Syntax
name Specifies a variable or, when followed
immediately by a colon, a label. Names
are composed of a letter (uppercase or
lowercase) optionally followed by
letters and digits. Only the first six
characters of a name are significant.
Except for names declared locally in fun
statements, all names are global. Names
can take on numeric (double float)
values or string values or be associated
with input/output (see the built-in
function open on page ).
name([expr[, expr] . . . )
Calls function name and passes to it the
parameters in parentheses. Except for
built-in functions (listed in the fol-
lowing text), name must be defined in a
fun statement. Function parameters are
passed by value.
name[expr[, expr] . . . ]
References either arrays or tables (see
built-in function table on page ). For
arrays, each expression is truncated to
an integer and used as a specifier for
the name. The resulting array reference
is syntactically identical to a name;
"a[1,2]" is the same as "a[1][2]". The
truncated expressions must be values
between 0 and 32767.
number Represents a constant numerical value.
This number can be expressed in integer,
decimal, or scientific notation (it can
contain digits, an optional decimal
point, and an optional e followed by a
possibly signed exponent).
string Character string delimited by " "
(double quotation marks). The \ (back-
slash) is an escape character that
allows the double quotation mark (\"),
new-line character (\n), carriage return
(\r), backspace (\b), and tab (\t) char-
acters to appear in a string. When not
immediately followed by these special
characters, \ stands for itself.
(expr) Parentheses alter the normal order of
evaluation.
(expr, expr[, expr] . . . ) [expr]
The bracketed expression outside the
parentheses functions as a subscript to
the list of expressions within the
parentheses. List elements are numbered
from the left, starting at zero. The
expression:
(False, True) [ a == b ]
has the value True if the comparison is
true.
expr op expr Except for the assignment, concat-
enation, and relational operators, both
operands are converted to numeric form
before the operator is applied.
Unary Operators
?expr The interrogation operator ("?") tests
for the success of the expression rather
than its value. It is useful for
testing end of file, for testing the
result of the eval built-in function,
and for checking the return from user-
written functions (see freturn on page
). An interrogation trap ( end of file,
for example), causes an immediate
transfer to the most recent interro-
gation, possibly skipping assignment
statements or intervening function
levels.
-expr Negates the expression.
++name Increases by one the value of the vari-
able (or array reference).
--name Decreases by one the value of the vari-
able.
!expr The logical negation of the expression.
Binary Operators (in increasing precedence)
= The assignment operator. The left
operand must be a name or an array
element. It acquires the value of the
right operand. Assignment binds right
to left; all other operators bind left
to right.
_ The concatenation operator (the under-
line character).
& | Logical AND, logical OR. The result of:
expr & expr
is 1 (true) only if both of its parame-
ters are nonzero (true); it is 0 (false)
if one or both of its parameters are 0
(false).
The result of:
expr | expr
is 1 (true) if one or both of its
expressions are nonzero (true); it is 0
(false) only if both of its expressions
are 0 (false). Both operators treat a
null string as a zero.
< <= > >= == !=
The relational operators (< less than,
.<= less than or equal to, > greater
than, >= greater than or equal to, ==
equal to, != not equal to) return 1 if
the specified relation is True. They
return 0 (false) otherwise. Relational
operators at the same level extend as
follows: a>b>c is the same as a>b &
b>c. A string comparison is made if
both operands are strings. The compar-
ison is based on the collating sequence
specified in the environment variable
NLCTAB.
+ - Addition and subtraction.
* / % Multiplication, division, and remainder.
^ Exponentiation.
Functions Dealing With Arguments
arg(i) Returns the value of the i-th actual
argument at the current function call
level. At level zero, arg returns the
i-th command-line argument. For
example, arg(0) returns "bs".
narg( ) Returns the number of arguments passed.
At level zero, it returns the command
line argument count.
Mathematical Functions
abs(x) Returns the absolute value of x.
atan(x) Returns the arctangent of x.
ceil(x) Returns the smallest integer not less
than x.
cos(x) Returns the cosine of x.
exp(x) Returns e raised to the power x.
floor(x) Returns the largest integer not greater
than x.
log(x) Returns the natural logarithm of x.
rand( ) Returns a uniformly distributed random
number between zero and one.
sin(x) Returns the sine of x.
sqrt(x) Returns the square root of x.
String Functions
size(s) Returns the size (length in bytes) of s.
format(f, a) Returns the formatted value of a, f
being a format specification string in
the style of the printf subroutine. Use
only the %...f, %...e, and %...s
formats.
index(x, y) Returns a number that is the first posi-
tion in x containing a character that
any of the characters in y matches. If
there is no match, index yields zero.
For two-byte extended characters, the
index functions returns the location of
the first byte.
trans(s, f, t) Translates characters in the source
string s which match characters in f
into characters having the same position
in t. Source characters that do not
appear in f are copied unchanged into
the translated string. If string f is
longer than t, source characters that
match characters found in the excess
portion of f do not appear in the trans-
lated string.
subst(s, start, length)
Returns the substring of s defined by
starting position and length.
match(string, pattern)
mstring(n) This function returns the number of
characters in string that match pattern.
The characters ., "*", "?" [, ], ^ (when
inside square brackets), \( and \) have
the following special meanings (see "ed"
for a more detailed discussion of this
special notation):
. Matches any character except the
_
new-line character.
* Matches zero or more occurrences
_
of the pattern element that it
follows (for example, ".*"
matches zero or more occurrences
of any character except the new-
line character).
$ Specifies the end of the line.
_
[.-.]
[ . . . ]
Matches any one character in the
specified range ([-.]) or list ([
. . . ]), including the first and
last characters.
[^.-.]
[^ . . . ]
Matches any character except the
new-line character and the
remaining characters in the range
or list. A circumflex (^) has
this special meaning only when it
immediately follows the left
bracket.
[].-.]
[] . . . ]
Matches ] or any character in the
list. The right square bracket
does not terminate such a list
when it is the first character
within it (after an initial ^, if
any).
\( . . . \)
Marks a substring and matches it
exactly.
To succeed, a pattern must match from
the beginning of the string. It also
matches the longest possible string.
Consider, for example:
match('a123ab123',".*\([a-z]\)") == 6
In this instance, ".*" matches "a123a"
(the longest string that precedes a
character in the range a-z); "\([a-z]\)"
matches "b", giving a total of six char-
acters matched in the string. In an
expression such as [a-z], the minus
means "through" according to the current
collating sequence. A collating
sequence may define equivalence classes
for use in character ranges. See the
"Overview of International Character
Support" in Managing the AIX Operating
System for more information on collating
sequences and equivalence classes.
The mstring function returns the nth
substring in the last call to match (n
must be between 1 and 10 inclusive).
File-Handling Functions
open(name, file, mode)
close(name) The name parameter must be a legal vari-
able name (passed as a string). For
open, the file parameter may be:
o A 0, 1, or 2 for standard input,
output, or error output, respec-
tively
o A string representing a file name
o A string beginning with an !,
representing a command to be run
(via "sh -c").
The mode flag must be either r (read), w
(write), W (write without new-line char-
acter), or a (append). After a close,
the name becomes an ordinary variable.
The initial associations are:
open("get", 0, "r")
open("put", 1, "w")
open("puterr", 2, "w")
access(p, m) Performs the access system call. Param-
eter p is the path name of a file; m is
a bit pattern representing the requested
mode of access. This function returns a
0 if the request is permitted, -1 if it
is denied. (See AIX Operating System
Technical Reference for a more extensive
discussion of this system call.)
ftype(s) Returns a single character indicating
file type: f for regular file, p for
FIFO (named pipe), d for directory, b
for block special, or c for character
special.
Table Functions
table(name, size) A table in bs is an associatively
accessed, one-dimensional array. "Sub-
scripts" (called keys) are strings
(numbers are converted). The name
parameter must be a bs variable name
(passed as a string). The size param-
eter sets the minimum number of elements
to be allocated. On table overflow, bs
writes an error message.
item(name, i)
key( ) The item function accesses table ele-
ments sequentially (in normal use, there
is an orderly progression of key
values). Where the item function
accesses values, the key function
accesses the "subscript" of the previous
item call. The name parameter should
not be quoted. Since exact table sizes
are not defined, the interrogation oper-
ator should be used to detect end-of-
table; for example:
table("t",100)
.
#If word contains "party", the following expression
#adds one to the count of that word:
++t[word]
.
# To display the key/value pairs:
for i=0, ?(s=item(t, i)), ++i if key() put=key()_":"_s
iskey(name, word)
Tests whether the key word exists in the
table name and returns one for true,
zero for false.
Miscellaneous Functions
eval(string) The string parameter is evaluated as an
expression. The function is handy for
converting numeric strings to numbers.
eval can also be used as a crude form of
indirection, as in:
name = "xyz"
eval("++"_name)
which increments the variable "xyz".
In addition, eval preceded by the inter-
rogation operator permits you to control
bs error conditions. For example:
?eval("open(\"X\",\"XXX\", \"r\")")
returns the value zero if there is no
file named ""XXX"" (instead of halting
your program). The following performs a
goto to the label "L:" (if it exists):
label="L:"
if!(?eval("goto"_label))puterr="no label"
plot(request, args)
The plot function produces output on
devices recognized by the tplot command.
Some requests do not apply to all plot-
ters. All requests except 0 and 12 are
implemented by piping characters to
tplot. The requests are as follows:
Call Function
plot(0, term) Causes further plot
output to be piped
into tplot with a flag
of -Tterm.
plot(1) "Erases" the plotter.
plot (2, string) Labels the current
point with string.
plot(3, x1, y1, x2, y2)
Draws the line between
(x1, y1) and (x2, y2).
plot(4, x, y, r)
Draws a circle with
center (x, y) and
radius r.
plot(5, x1, y1, x2, y2, x3, y3)
Draws an arc (counter-
clockwise) with center
(x1, y1) and endpoints
(x2, y2) and (x3, y3).
plot(6) Not implemented.
plot(7, x, y) Makes the current
point at (x, y).
plot(8, xy) Draws a line from the
current point to (x,
y).
plot(9, x, y) Draws a point at (x,
y).
plot(10, string) Sets the line mode to
string.
plot(11, x1, y1, x2, y2)
Makes (x1, y1) the
lower left corner of
the plotting area and
(x2, y2) the upper
right corner of the
plotting area.
plot(12, x1, y1, x2, y2)
Causes subsequent x
(y) coordinates to be
multiplied by x1 (y1)
and then added to x2
(y2) before they are
plotted. The initial
scaling is plot(12,
1.0, 1.0, 0.0, 0.0).
last() In immediate mode, last returns the most
recently computed value.
RELATED INFORMATION
The following commands: "ed," "sh," and "tplot."
The access system call, the printf subroutine, and the
plot file in AIX Operating System Technical Reference.
"Overview of International Character Support" in Managing
the AIX Operating System.