Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ bs(1) — A/UX 3.0.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ed(1)

ksh(1)

sh(1)

tplot(1G)

access(2)

intro(3)

printf(3S)

plot(4)




bs(1) bs(1)
NAME bs - compiles and interprets bs programs SYNOPSIS bs [file [argument]...] ARGUMENTS argument Specifies an optional argument that bs passes to the program when it executes. file Specifies the name of a source file that bs uses as input before reading from the console. By default, bs compiles statements read from file for later execution and immediately executes statements entered from the console. See the compile and execute statements for details. DESCRIPTION bs compiles and interprets programs that are written in a language that is a remote descendant of BASIC, SNOBOL4, and C. The language is designed for programming tasks where program development time is as important as the resulting speed of execution. The language minimizes the formalities of data declaration and file manipulation. Line-at-a-time debugging, the trace and dump keywords, and useful run-time error messages simplify program testing. Furthermore, you can debug incomplete programs, test inner functions before outer functions have been written, and test outer functions before inner functions have been written. Syntax The bs command accepts programs that are made up of input lines. If the last character on a line is a backslash (\), bs interprets the next line as a continuation of the previous line. Lines can be of this form: statement label statement A label is a name followed by a colon (:). (For a definition of name, see ``Expression Syntax'' later in this section.) A label and a variable can have the same name. Statement syntax. A bs statement is either an expression or a keyword followed by zero or more expressions. An expression assigns a value or makes a function call. For details, see ``Expression Syntax'' later in this section. The keywords clear, compile, !, execute, include, ibase, obase, and run are always executed as they are compiled. Here are the possible keywords: January 1992 1



bs(1) bs(1)
! shell-command Causes an immediate escape to the shell to execute shell-command. #string Specifies a comment, which bs ignores. break Exits from the innermost for or while loop. clear Clears the symbol table and compiled statements immediately. compile[expression] Executes a clear and compiles succeeding statements immediately (overriding the immediate execution default). The bs command evaluates the optional expression and uses the result as a filename for further input. continue Transfers to the loop-continuation of the current for or while loop. dump[name] Causes bs to display the name and current value of every nonlocal variable. If you provide an optional name, bs displays only the specified variable. After an error or interrupt, bs displays the number of the last statement and the user-function trace if turned on. execute Changes bs to immediate-execution mode. An interrupt has the same effect. This keyword does not cause internally stored statements to execute. See run for a keyword that does. exit[expression] Exits bs. If you provide an optional expression, bs evaluates expression and uses the result as the exit code. for name=expression expression statement for name=expression expression ... next for expression,expression,expression statement for expression,expression,expression ... next Executes repetitively a statement (first form) or a 2 January 1992



bs(1) bs(1)
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 1 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 expression is the initialization, the second is the test (TRUE to continue), and the third is the loop-continuation action, which is commonly an increment. freturn Signals the failure of a user-written function. For details, see the interrogation operator (?) in ``Expression Syntax'' later in this section. If interrogation is not active, freturn merely returns 0. If interrogation is active, freturn transfers execution to that expression, which may bypass intermediate function returns. fun name([arguments,...])[variables,...] ... nuf Defines a 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 associated with I/O. You cannot nest function definitions. goto name Passes control to the internally stored statement with the matching label specified by name. ibase N Sets the input base to N. The supported values for N are 8, 10 (the default), and 16. You can enter the hexadecimal values for 10-15 as a-f. To enter a number such as f0a, use a leading 0, as in 0f0a. The bs command executes ibase immediately. if expression statement if expression... [else ...] fi Executes if the expression evaluates to a nonzero value. The strings 0 and "" (null) evaluate to 0. In the second form, an optional else allows for a group of statements to be executed when the first group is not. The only keyword permitted on the same line with else is if; the only keywords permitted on the same line with fi are other fi keywords. The bs command also supports elif. Only a single fi is required to close an if...elif...[else...] sequence. January 1992 3



bs(1) bs(1)
include expression Includes the file specified by expression. The file must contain bs source statements. Such statements become part of the program being compiled. You cannot nest include keywords. obase N Sets the output base to N. The supported values for N are 8, 10 (the default), and 16. You can enter the hexadecimal values for 10-15 as a-f. To enter a number such as f0a, use a leading 0, as in 0f0a. The bs command executes obase immediately. onintr label onintr Provides program control of interrupts. In the first form, control passes to the specified label, just as if a goto had been executed at the time onintr executed. The effect of onintr is cleared after each interrupt. In the second form, an interrupt causes bs to terminate. return [expression] Returns from a function call. If present, the optional expression is evaluated and the result is passed back as the value of a function call. If no expression is given, the function call returns 0. run Resets the random-number generator. Control is passed to the first internally stored statement. If run is in a file, it should be the last statement in the file. stop Stops execution of internally stored statements and causes bs to revert to immediate mode. trace [expression] Controls function tracing. If the optional expression is null or evaluates to 0, tracing is turned off. Otherwise, bs displays a record of user-function calls and returns. Each return decrements the value of expression. while expression statement while expression ... next Executes repetitively a group of statements. The while keyword is similar to for except that only the conditional expression for loop-continuation is given. Expression syntax. Unless the final operation is an assignment, bs displays the result of an immediate expression statement. Here is the syntax for expressions: 4 January 1992



bs(1) bs(1)
? expression Tests for the success of the expression, rather than its value. The interrogation operator is useful for testing for end-of-file condition (as shown in the ``Examples'' section later in this manual page), checking the result of the eval built-in function, and checking the return from user-written functions (as described in the discussion of the freturn keyword earlier in this section). Execution of an interrogation trap causes an immediate transfer to the most recent interrogation, possibly skipping assignment statements or intervening function levels. - expression Results in the negation of the expression. --name Decrements the value of name, which is a variable name or an array reference. ++name Increments the value of name, which is a variable name or an array reference. ! expression Results in the logical negation of the expression. name Specifies a variable. A name begins with a lowercase or uppercase letter, optionally followed by letters 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 or string values, or can be associated with input and output. For details, see the open function in ``File-handling Functions'' later in this section. name([expression[,expression]...]) Calls a function. Except for built-in functions, which are listed later in this section, name must be defined with a fun statement. The bs command passes arguments to functions by value. name [expression[,expression]...] References arrays or tables. See ``Built-in Table Functions'' later in this section for details. 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 are restricted to values between 0 and 32767. January 1992 5



bs(1) bs(1)
number Represents a constant value. A number is written in Fortran style and contains digits, an optional decimal point, and possibly a scale factor consisting of an e followed by an optionally signed exponent. string Specifies a character string that is delimited by a double quotation mark ( ). The backslash (\) is an escape character that allows the double quotation mark (\"), newline (\n), carriage return (\r), backspace (\b), and tab (\t) characters to appear in a string. Otherwise, the backslash stands for itself. (expression) Alters the normal order of evaluation. See ``Binary Operators'' later in this section for the normal order of evaluation. (expression,expression[,expression...])[expression] Selects, using a bracketed expression as a subscript, a comma-separated expression from the parenthesized list. List elements are numbered from the left, starting at 0. The expression has the value TRUE if a equals b: (FALSE,TRUE)[a == b] expression operator expression Abbreviates the common functions of two arguments by separating the two arguments with 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. The binary operators are listed in order of increasing precedence, as follows: = Makes assignments. The left operand must be a name or an array element. The result is the right operand. Assignment binds right to left, whereas all other operators bind left to right. _ Concatenates. The operator is the underscore character (_). & | Perform logical operations. The result of the logical AND character (&) is 0 if either of its arguments is 0. The result is 1 if both of its arguments are nonzero. The result of the logical OR character (|) is 0 if both of its arguments are 0. The result is 1 if either of its arguments is nonzero. Both operators treat a null 6 January 1992



bs(1) bs(1)
string as 0. < <= > >= == != Perform relational operations. The relational operators are < (less than), <= (less than or equal), > (greater than), >= (greater than or equal), == (equal), and != (not equal). They return 1 if their arguments are in the specified relation. Otherwise, they return 0. The comparison a>b>c is the same as the comparison a>b&b>c. If both operands are strings, bs makes a string comparison. + - Perform addition and subtraction. * / % Perform multiplication, division, and remaindering. ^ Performs exponentiation. Built-in Functions The bs command provides the following built-in functions. Dealing with arguments. These built-in functions manipulate arguments: arg(i) Specifies the value of the ith actual parameter on the current level of function call. At level 0, arg returns the ith command-line argument. For example, arg(0) returns bs. narg() Returns the number of arguments passed. At level 0, bs returns the number of command-line arguments. Mathematical functions. The bs command provides these built-it mathematical functions: abs(x) Returns the absolute value of x. atan(x) Returns the arctangent of x. Its value is between -π/2 and π/2. ceil(x) Returns the smallest integer not less than x. cos(x) Returns in radians the cosine of x. exp(x) January 1992 7



bs(1) bs(1)
Returns the exponential function of 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 0 and 1. sin(x) Returns in radians the sine of x. sqrt(x) Returns the square root of x. String functions. The bs command provides these built-in string functions: format(f, a) Returns the formatted value of a, where f is a format specification in the style of printf. The value of f can be f, e, or s only. index(x, y) Returns the number of the first position in x that any of the characters from y matches. If no match is found, bs returns 0. match(string, pattern) mstring(n) Match strings. The pattern is similar to the regular expression syntax of ed. The characters ., [, ], *, and $ are special. The mstring function returns the nth (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 begin with *. The function returns the number of characters matched. Here is an example: match("a123ab123", ".*\([a-z]\)") == 6 mstring(1) == "b" size(s) Returns the size (length in bytes) of s. substr(s, start, width) Returns the substring of s defined by the start 8 January 1992



bs(1) bs(1)
position and width. trans(s, f, t) 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. File-handling functions. The bs command provides these built-in file handling functions: open(name, file, function) close(name) Perform open and close operations. The name argument must be a bs variable name passed as a string. For open, the file argument can be either (1) a 0 (zero), 1, or 2, representing standard input, output, or error output, respectively; (2) a string representing a filename; or (3) a string beginning with an exclamation point (!) representing a command to be executed by means of sh -c. The function argument must be either r (read), w (write), W (write without newline), or a (append). After a close command, bs treats name as an ordinary variable. The bs command makes these calls on startup: open("get", 0, "r") open("put", 1, "w") open("puterr", 2, "w") See the ``Examples'' section later in this manual page, for sample code. access(s, m) Executes an access(2) system call. ftype(s) Returns a single character that indicates the file type: f for regular file, p for FIFO (that is, named pipe), d for directory, b for block special, or c for character special. Table-handling functions. The bs command provides these built-in table handling functions: table(name, size) Defines a table that is an associatively accessed, single-dimension array. Subscripts (called ``keys'') are strings. Numbers are converted. The name argument must be a bs variable name, passed as a string. The January 1992 9



bs(1) bs(1)
size argument sets the minimum number of elements to be allocated. If a table overflow occurs, bs displays an error message and stops. item(name, i) key() Access table elements. The item function accesses table elements sequentially. (In normal use, there is no orderly progression of key values.) Where the item function accesses values, the key function accesses the subscript of the previous item call. The name argument should not be quoted. Because exact table sizes are not defined, the interrogation operator should be used to detect end-of-table. Here is an example: table("t", 100) ... # If word contains the string "party", # the following expression adds one # to the count of that word: ++t[word] ... # To print out 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 1 for TRUE and 0 for FALSE. Miscellaneous functions. The bs command provides these miscellaneous built-in functions: eval(s) Evaluates the string argument as an expression. The eval function is handy for converting numeric strings to numeric internal form. You can also use eval as a crude form of indirection, as in this example, which increments the variable xyz: name = "xyz" eval("++"_ name) In addition, eval preceded by the interrogation operator permits the user to control bs error conditions. Here is an example that returns 0 if XXX does not exist, instead of halting: ?eval("open(\"X\", \"XXX\", \"r\")") This example executes a goto to the label L: 10 January 1992



bs(1) bs(1)
label="L" if !(?eval("goto "_ label)) puterr = "no label" last() Returns the most recently computed value when in immediate mode. plot(request, args) Produces output on devices recognized by tplot(1G). The value of request can be from 0 to 12. Calls to the plot function use these formats: plot(0, term) Causes further plot output to be piped into tplot(1G) with an argument 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 (counterclockwise) with center (x1,y1) and endpoints (x2,y2) and (x3,y3). plot(6) Not implemented. plot(7,x,y) Makes the current point (x,y). plot(8,x,y) 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. January 1992 11



bs(1) bs(1)
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). Some requests do not apply to all plotters. All requests except 0 and 12 are implemented by the piping of characters to tplot(1G). See plot(4) for more details. EXAMPLES This example uses bs as a calculator: $ 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 ... exit This example is the outline of a typical bs 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 This example demonstrates I/O: # Copy "oldfile" to "newfile". open("read", "oldfile", "r") open("write", "newfile", "w") 12 January 1992



bs(1) bs(1)
... 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") This command line shows a way of running bs: bs program 1 2 3 The example compiles and executes the file named program as well as any statements typed from standard input. The arguments 1, 2, and 3 are passed as arguments to the program when it executes. FILES /bin/bs Executable file SEE ALSO ed(1), ksh(1), sh(1), tplot(1G) access(2), intro(3), printf(3S), plot(4) in A/UX Programmer's Reference January 1992 13

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026