bs(1) General Commands Manual bs(1)
NAME
bs - a compiler/interpreter for modest-sized programs
SYNOPSIS
[file [args]]
DESCRIPTION
is a remote descendant of BASIC and SNOBOL4 with some C language added. is designed for programming tasks where program development time
is as important as the resulting speed of execution. Formalities of data declaration and file/process manipulation are minimized. Line-
at-a-time debugging, the and statements, and useful run-time error messages all simplify program testing. Furthermore, incomplete programs
can be debugged; inner functions can be tested before outer functions have been written, and vice versa.
If file is specified on the command-line, it is used for input before any input is taken from the keyboard. By default, statements read
from file are compiled for later execution. Likewise, statements entered from the keyboard are normally executed immediately (see and
below). Unless the final operation is assignment, the result of an immediate expression statement is printed.
programs are made up of input lines. If the last character on a line is a the line is continued. accepts lines of the following form:
statement
label statement
A label is a name (see below) followed by a colon. A label and a variable can have the same name.
A statement is either an expression or a keyword followed by zero or more expressions. Some keywords and are always executed as they are
compiled.
Statement Syntax:
expression The expression is executed for its side effects (value, assignment, or function call). The details of expressions follow
the description of statement types below.
exits from the innermost
loop.
Clears the symbol table and compiled statements.
is executed immediately.
Succeeding statements are compiled
(overrides the immediate execution default). The optional expression is evaluated and used as a file name for further
input. A is associated with this latter case. is executed immediately.
transfers to the loop-continuation of the current
loop.
The name and current value of every non-local variable is printed.
Optionally, only the named variable is reported. After an error or interrupt, the number of the last statement is dis-
played. The user-function trace is displayed after an error or that occurred in a function.
A call is made to
the editor selected by the environment variable if it is present, or ed(1) if is undefined or null. If the file argument is
present on the command line, file is passed to the editor as the file to edit (otherwise no file name is used). Upon exit-
ing the editor, a statement (and associated is executed giving that file name as its argument.
Return to system level.
The expression is returned as process status.
Change to immediate execution mode (an interrupt has a similar effect).
This statement does not cause stored statements to execute (see below).
...
...
The statement repetitively executes a statement (first form) or a group of statements (second form) under control of a named
variable. The variable takes on the value of the first expression, then is incremented by one on each loop, not to exceed
the value of the second expression. The third and fourth forms require three expressions separated 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 (nor-
mally an increment).
...
defines the function name, arguments,
and local variables for a user-written function. Up to ten arguments and local variables are allowed. Such names cannot be
arrays, nor can they be I/O associated. Function definitions cannot be nested. Calling an undefined function is permissi-
ble; see function calls below.
A way to signal the failure of a user-written function.
See the interrogation operator below. If interrogation is not present, merely returns zero. When interrogation is active,
transfers to that expression (possibly by-passing intermediate function returns).
Control is passed
to the internally stored statement with the matching label.
sets the input base (radix) to
n. The only supported values for n are the constants (the default), and Hexadecimal values 10-15 are entered as A leading
digit is required (i.e., must be entered as (and discussed below) are executed immediately.
...
The statement (first form)
or group of statements (second form) is executed if the expression evaluates to non-zero. The strings and "" (null) evalu-
ate as zero. In the second form, an optional provides for a second group of statements to be executed when the first group
is not. The only statement permitted on the same line with an is an only other can be on the same line with a The concate-
nation of and into an is supported. Only a single is required to close an sequence.
expression must evaluate to a file name. The file must contain source statements. Such statements become part of the program being
compiled. statements cannot be nested.
sets the output base to
n (see above).
provides program control of interrupts.
In the first form, control passes to the label given, just as if a had been executed at the time was executed. The effect
of the statement is cleared after each interrupt. In the second form, an interrupt causes to terminate.
The expression is evaluated and the result is passed
back as the value of a function call. If no expression is given, zero is returned.
The random number generator is reset.
Control is passed to the first internal statement. If the statement is contained in a file, it should be the last state-
ment.
Execution of internal statements is stopped.
reverts to immediate mode.
The statement controls function tracing. If the expression is null (or evaluates to zero), tracing is turned off. Otherwise, a
record of user-function calls/returns is printed. Each decrements the expression value.
...
is similar to except that only the conditional expression for loop-continuation is given.
An immediate escape to the shell.
This statement is ignored (treated as a comment).
Expression Syntax:
name A name is used to specify a variable. Names are composed of a letter (uppercase or lowercase) optionally followed by let-
ters and digits. Only the first six characters of a name are significant. Except for names declared in fun statements, all
names are global to the program. Names can take on numeric (double float) values, string values, or can be associated with
input/output (see the built-in function below).
name ( [expression [ , expression] ... ] )
Functions can be called by a name followed by the arguments in parentheses separated by commas. Except for built-in func-
tions (listed below), the name must be defined with a fun statement. Arguments to functions are passed by value. If the
function is undefined, the call history to the call of that function is printed, and a request for a return value (as an
expression) is made. The result of that expression is taken to be the result of the undefined function. This permits
debugging programs where not all the functions are yet defined. The value is read from the current input file.
name [ expression [ , expression ] ... ]
This syntax is used to reference either arrays or tables (see built-in table functions below). 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; is the same as The truncated expressions are restricted to values between 0 and 32767.
number A number is used to represent a constant value. A number is written in Fortran style, and contains digits, an optional dec-
imal point, and possibly a scale factor consisting of an followed by a possibly signed exponent.
string Character strings are delimited by " characters. The escape character allows the double quote ("), new-line carriage return
backspace and tab characters to appear in a string. Otherwise, stands for itself.
( expression ) Parentheses are used to alter the normal order of evaluation.
( expression , expression [ , expression ... ] ) [ expression ]
The bracketed expression is used as a subscript to select a comma-separated expression from the parenthesized list. List
elements are numbered from the left, starting at zero.
The expression:
has the value if the comparison is true.
The interrogation operator
tests for the success of the expression rather than its value. At the moment, it is useful for testing end-of-file (see
examples in the Programming Tips section below), the result of the built-in function, and for checking the return from user-
written functions (see An interrogation ``trap'' (end-of-file, etc.) causes an immediate transfer to the most recent inter-
rogation, possibly skipping assignment statements or intervening function levels.
The result is the negation of the expression.
Increments the value of the variable (or array reference).
The result is the new value.
Decrements the value of the variable.
The result is the new value.
The logical negation of the expression.
Watch out for the shell escape command.
expression operator expression Common functions of two arguments are abbreviated by the two arguments separated by an operator denoting
the function. Except for the assignment, concatenation, and relational operators, both operands are converted to numeric
form before the function is applied.
Binary Operators (in increasing precedence):
is the assignment operator.
The left operand must be a name or an array element. The result is the right operand. Assignment binds right to left, all
other operators bind left to right.
(underscore) is the concatenation operator.
(logical AND) has result zero if either of its arguments are zero. It has result one if both of its arguments are non-zero; (logical
OR) has result zero if both of its arguments are zero. It has result one if either of its arguments is non-zero. Both
operators treat a null string as a zero.
The relational operators
less than, less than or equal, greater than, greater than or equal, equal to, not equal to) return one if their arguments
are in the specified relation, or return zero otherwise. Relational operators at the same level extend as follows: is
equivalent to a>b & b>c. A string comparison is made if both operands are strings.
Add and subtract.
Multiply, divide, and remainder.
Exponentiation.
Built-in Functions:
is the value of the
i-th actual parameter on the current level of function call. At level zero, arg returns the i-th command-line argument
(arg(0) returns
returns the number of arguments passed.
At level zero, the command argument count is returned.
is the absolute value of
x.
is the arctangent of
x. Its value is between -n/2 and n/2.
returns the smallest integer not less than x.
is the cosine of
x (radians).
is the exponential function of
x.
returns the largest integer not greater than x.
is the natural logarithm of
x.
is a uniformly distributed random
number between zero and one.
is the sine of x (radians).
is the square root of
x.
the size (length in bytes) of
s is returned.
returns the formatted value of
a. f is assumed to be a format specification in the style of printf(3S). Only the and types are safe. Since it is not
always possible to know whether is a number or a string when the call is coded, coercing to the type required by by either
adding zero (for or format) or concatenating the null string (for format) should be considered.
returns the number of the first position in
x that any of the characters from y matches. No match yields zero.
Translates characters of the source
s from matching characters in f to a character in the same position in t. Source characters that do not appear in f are
copied to the result. If the string f is longer than t, source characters that match in the excess portion of f do not
appear in the result.
returns the sub-string of
s defined by the starting position and width.
The pattern is a regular expression according to the Basic Regular Expression definition (see regexp(5)). returns the n-th (1
<= n <= 10) substring of the subject that occurred between pairs of the pattern symbols and for the most recent call to
match. To succeed, patterns must match the beginning of the string (as if all patterns began with The function returns the
number of characters matched. For example:
name argument must be a variable name (passed as a string). For the the file argument can be:
1. a 0 (zero), 1, or 2 representing standard input, output, or error output, respectively;
2. a string representing a file name; or
3. a string beginning with an representing a command to be executed (via The function argument must be either (read),
(write), (write without new-line), or (append). After a name reverts to being an ordinary variable. If name was a
pipe, a is executed before the close completes (see wait(2)). The command does not do such a wait. The initial
associations are:
Examples are given in the following section.
executes (see access(2)).
returns a single character file type indication:
for regular file, for FIFO (i.e., named pipe), for directory, for block special, or for character special.
Tables
A table in is an associatively accessed, single-dimension array. ``Subscripts'' (called keys) are strings (numbers are converted).
The name argument must be a variable name (passed as a string). The size argument sets the minimum number of elements to be
allocated. prints an error message and stops on table overflow. The result of table is name.
The function accesses table elements sequentially (in normal use, there is no orderly progression of key values). Where the
function accesses values, the function accesses the ``subscript'' of the previous call. It fails (or in the absence of an
operator, returns null) if there was no valid subscript for the previous call. The name argument should not be quoted.
Since exact table sizes are not defined, the interrogation operator should be used to detect end-of-table; for example:
...
...
If the interrogation operator is not used, the result of is null if there are no further elements in the table. Null is,
however, a legal ``subscript''.
tests whether the key
word exists in the table name and returns one for true, zero for false.
The string argument is evaluated as a
expression. The function is handy for converting numeric strings to numeric internal form. can also be used as a crude
form of indirection, as in:
which increments the variable xyz. In addition, preceded by the interrogation operator permits the user to control error
conditions. For example:
returns the value zero if there is no file named (instead of halting the user's program). The following executes a to the
label (if it exists):
If the command is available, the function produces output on devices recognized by The requests are as follows:
Call Function
causes further plot output to be piped into tplot with an argument of term can be up to 40 charac-
ters in length.
``erases'' the plotter.
labels the current point with string.
draws the line between (x1,y1) and (x2,y2).
draws a circle with center (x,y) and radius r.
draws an arc (counterclockwise) with center
(x1,y1) and endpoints (x2,y2) and (x3,y3).
is not implemented.
makes the current point (x,y).
draws a line from the current point to
(x,y).
draws a point at (x,y).
sets the line mode to string.
makes (x1,y1) the lower left corner of the plotting area and (x2,y2) the upper right cor-
ner of the plotting area.
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
Some requests do not apply to all plotters. All requests except zero and twelve are implemented by piping characters to
tplot.
Each statement executed from the keyboard re-invokes making the results unpredictable if a complete picture is not done in a
single operation. Plotting should thus be done either in a function or a complete program, so all the output can be
directed to in a single stream.
in immediate mode,
returns the most recently computed value.
EXTERNAL INFLUENCES
Environment Variables
determines the collating sequence used in evaluating regular expressions.
determines the characters matched by character class expressions in regular expressions.
If or is not specified in the environment or is set to the empty string, the value of is used as a default for each unspecified or empty
variable. If is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of If any internationalization
variable contains an invalid setting, behaves as if all internationalization variables are set to "C". See environ(5).
International Code Set Support
Single-byte character code sets are supported.
EXAMPLES
Using as a calculator is the shell prompt):
$ bs
# Distance (inches) light travels in a nanosecond.
186000 * 5280 * 12 / 1e9
11.78496
...
# Compound interest (6% for 5 years on $1,000).
int = .06 / 4
bal = 1000
for i = 1 5*4 bal = bal + bal*int
bal - 1000
346.855007
...
The outline of a typical program:
# initialize things:
var1 = 1
open("read", "infile", "r")
...
# compute:
while ?(str = read)
...
next
# clean up:
close("read")
...
# last statement executed (exit or stop):
exit
# last input line:
run
Input/Output examples:
# Copy file oldfile to file newfile.
open("read", "oldfile", "r")
open("write", "newfile", "w")
...
while ?(write = read)
...
# close "read" and "write":
close("read")
close("write")
# Pipe between commands.
open("ls", "!ls *", "r")
open("pr", "!pr -2 -h 'List'", "w")
while ?(pr = ls) ...
...
# be sure to close (wait for) these:
close("ls")
close("pr")
WARNINGS
The graphics mode is not particularly useful unless the command is available on your system.
is not tolerant of some errors. For example, mistyping a declaration is difficult to correct because a new definition cannot be made with-
out doing a The best solution in such a case is to start by using the command.
SEE ALSO
ed(1), sh(1), access(2), printf(3S), stdio(3S), lang(5), regexp(5).
See Section(3M) for a further description of the mathematical functions.
is used for exponentiation -- see exp(3M));
uses the Standard I/O package.
bs(1)