diff options
| author | Gabor Kovesdan <gabor@FreeBSD.org> | 2010-01-20 21:30:52 +0000 |
|---|---|---|
| committer | Gabor Kovesdan <gabor@FreeBSD.org> | 2010-01-20 21:30:52 +0000 |
| commit | fdf1f88bcecce7bedef674be598a8cf89c3a3f1c (patch) | |
| tree | 96e08d56eaa68b31f8e98a726fe2be11d38299d2 /usr.bin/bc | |
| parent | 32fc554ec6f84b8597d61d15f33f06713224123b (diff) | |
Notes
Diffstat (limited to 'usr.bin/bc')
| -rw-r--r-- | usr.bin/bc/Makefile | 17 | ||||
| -rw-r--r-- | usr.bin/bc/USD.doc/Makefile | 13 | ||||
| -rw-r--r-- | usr.bin/bc/USD.doc/bc | 1241 | ||||
| -rw-r--r-- | usr.bin/bc/bc.1 | 400 | ||||
| -rw-r--r-- | usr.bin/bc/bc.library | 263 | ||||
| -rw-r--r-- | usr.bin/bc/bc.y | 1179 | ||||
| -rw-r--r-- | usr.bin/bc/extern.h | 38 | ||||
| -rw-r--r-- | usr.bin/bc/pathnames.h | 21 | ||||
| -rw-r--r-- | usr.bin/bc/scan.l | 288 |
9 files changed, 3460 insertions, 0 deletions
diff --git a/usr.bin/bc/Makefile b/usr.bin/bc/Makefile new file mode 100644 index 000000000000..e29155514ac2 --- /dev/null +++ b/usr.bin/bc/Makefile @@ -0,0 +1,17 @@ +# $FreeBSD$ +# $OpenBSD: Makefile,v 1.4 2006/06/30 19:02:28 otto Exp $ + +PROG= bc +SRCS= bc.y scan.l +CFLAGS+= -I. -I${.CURDIR} +WARNS?= 6 +#SUBDIR+= USD.doc + +FILES+= bc.library +FILESDIR= ${SHAREDIR}/misc + +#beforeinstall: +# install -c -o ${BINOWN} -g ${BINGRP} -m 444 ${.CURDIR}/bc.library \ +# ${DESTDIR}/usr/share/misc + +.include <bsd.prog.mk> diff --git a/usr.bin/bc/USD.doc/Makefile b/usr.bin/bc/USD.doc/Makefile new file mode 100644 index 000000000000..44265d22374c --- /dev/null +++ b/usr.bin/bc/USD.doc/Makefile @@ -0,0 +1,13 @@ +# $FreeBSD$ +# $OpenBSD: Makefile,v 1.3 2004/02/01 15:18:01 jmc Exp $ + +DOC= bc +DIR= usd/06.bc +SRCS= bc +MACROS= -ms +BINDIR= /usr/share/doc/papers + +paper.txt: ${SRCS} + ${ROFF} -Tascii ${SRCS} > ${.TARGET} + +.include <bsd.doc.mk> diff --git a/usr.bin/bc/USD.doc/bc b/usr.bin/bc/USD.doc/bc new file mode 100644 index 000000000000..c4e68c679a6b --- /dev/null +++ b/usr.bin/bc/USD.doc/bc @@ -0,0 +1,1241 @@ +.\" $FreeBSD$ +.\" $OpenBSD: bc,v 1.9 2004/07/09 10:23:05 jmc Exp $ +.\" +.\" Copyright (C) Caldera International Inc. 2001-2002. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code and documentation must retain the above +.\" copyright notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed or owned by Caldera +.\" International, Inc. +.\" 4. Neither the name of Caldera International, Inc. nor the names of other +.\" contributors may be used to endorse or promote products derived from +.\" this software without specific prior written permission. +.\" +.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA +.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, +.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.\" @(#)bc 6.2 (Berkeley) 4/17/91 +.\" +.if n \{\ +.po 5n +.ll 70n +.\} +.EH 'USD:6-%''BC \- An Arbitrary Precision Desk-Calculator Language' +.OH 'BC \- An Arbitrary Precision Desk-Calculator Language''USD:6-%' +.\".RP +.TL +BC \- An Arbitrary Precision Desk-Calculator Language +.AU +Lorinda Cherry +.AU +Robert Morris +.AI +.\" .MH +.AB +BC is a language and a compiler for doing arbitrary precision arithmetic +on the PDP-11 under the +.UX +time-sharing +system. The output of the compiler is interpreted and executed by +a collection of routines which can input, output, and do +arithmetic on indefinitely large integers and on scaled fixed-point +numbers. +.PP +These routines are themselves based on a dynamic storage allocator. +Overflow does not occur until all available core storage +is exhausted. +.PP +The language has a complete control structure as well as immediate-mode +operation. Functions can be defined and saved for later execution. +.PP +Two five hundred-digit numbers can be multiplied to give a +thousand digit result in about ten seconds. +.PP +A small collection of library functions is also available, +including sin, cos, arctan, log, exponential, and Bessel functions of +integer order. +.PP +Some of the uses of this compiler are +.IP \- +to do computation with large integers, +.IP \- +to do computation accurate to many decimal places, +.IP \- +conversion of numbers from one base to another base. +.AE +.PP +.SH +Introduction +.PP +BC is a language and a compiler for doing arbitrary precision +arithmetic on the +.UX +time-sharing system [1]. +The compiler was written to make conveniently available a +collection of routines (called DC [5]) which are capable of doing +arithmetic on integers of arbitrary size. The compiler +is by no means intended to provide a complete programming +language. +It is a minimal language facility. +.PP +There is a scaling provision that permits the +use of decimal point notation. +Provision is made for input and output in bases other than +decimal. Numbers can be converted from decimal to octal by +simply setting the output base to equal 8. +.PP +The actual limit on the number of digits that can +be handled depends on the amount of storage available on the machine. +Manipulation of numbers with many hundreds of digits +is possible even on the smallest versions of +.UX . +.PP +The syntax of BC has been deliberately selected to agree +substantially with the C language [2]. Those who +are familiar with C will find few surprises in this language. +.SH +Simple Computations with Integers +.PP +The simplest kind of statement is an arithmetic expression +on a line by itself. +For instance, if you type in the line: +.DS +.ft B +142857 + 285714 +.ft P +.DE +the program responds immediately with the line +.DS +.ft B +428571 +.ft P +.DE +The operators \-, *, /, %, and ^ can also be used; they +indicate subtraction, multiplication, division, remaindering, and +exponentiation, respectively. Division of integers produces an +integer result truncated toward zero. +Division by zero produces an error +comment. +.PP +Any term in an expression may be prefixed by a minus sign to +indicate that it is to be negated (the `unary' minus sign). +The expression +.DS +.ft B +7+\-3 +.ft P +.DE +is interpreted to mean that \-3 is to be added to 7. +.PP +More complex expressions with several operators and with +parentheses are interpreted just as in +Fortran, with ^ having the greatest binding +power, then * and % and /, and finally + and \-. +Contents of parentheses are evaluated before material +outside the parentheses. +Exponentiations are +performed from right to left and the other operators +from left to right. +The two expressions +.DS +.ft B +a^b^c and a^(b^c) +.ft P +.DE +are equivalent, as are the two expressions +.DS +.ft B +a*b*c and (a*b)*c +.ft P +.DE +BC shares with Fortran and C the undesirable convention that +.DS +\fBa/b*c\fP is equivalent to \fB(a/b)*c\fP +.ft P +.DE +.PP +Internal storage registers to hold numbers have single lower-case +letter names. The value of an expression can be assigned to +a register in the usual way. The statement +.DS +.ft B +x = x + 3 +.ft P +.DE +has the effect of increasing by three the value of the contents of the +register named x. +When, as in this case, the outermost operator is an =, the +assignment is performed but the result is not printed. +Only 26 of these named storage registers are available. +.PP +There is a built-in square root function whose +result is truncated to an integer (but see scaling below). +The lines +.DS +.ft B +x = sqrt(191) +x +.ft P +.DE +produce the printed result +.DS +.ft B +13 +.ft P +.DE +.SH +Bases +.PP +There are special internal quantities, called `ibase' and `obase'. +The contents of `ibase', initially set to 10, +determines the base used for interpreting numbers read in. +For example, the lines +.DS +.ft B +ibase = 8 +11 +.ft P +.DE +will produce the output line +.DS +.ft B +9 +.ft P +.DE +and you are all set up to do octal to decimal conversions. +Beware, however of trying to change the input base back +to decimal by typing +.DS +.ft B +ibase = 10 +.ft P +.DE +Because the number 10 is interpreted as octal, this statement will +have no effect. +For those who deal in hexadecimal notation, +the characters A\-F are permitted in numbers +(no matter what base is in effect) +and are +interpreted as digits having values 10\-15 respectively. +The statement +.DS +.ft B +ibase = A +.ft P +.DE +will change you back to decimal input base no matter what the +current input base is. +Negative and large positive input bases are +permitted but useless. +No mechanism has been provided for the input of arbitrary +numbers in bases less than 1 and greater than 16. +.PP +The contents of `obase', initially set to 10, are used as the base for output +numbers. The lines +.DS +.ft B +obase = 16 +1000 +.ft P +.DE +will produce the output line +.DS +.ft B +3E8 +.ft P +.DE +which is to be interpreted as a 3-digit hexadecimal number. +Very large output bases are permitted, and they are sometimes useful. +For example, large numbers can be output in groups of five digits +by setting `obase' to 100000. +Strange (i.e. 1, 0, or negative) output bases are +handled appropriately. +.PP +Very large numbers are split across lines with 70 characters per line. +Lines which are continued end with \\. +Decimal output conversion is practically instantaneous, but output +of very large numbers (i.e., more than 100 digits) with other bases +is rather slow. +Non-decimal output conversion of +a one hundred digit number takes about +three seconds. +.PP +It is best to remember that `ibase' and `obase' have no effect +whatever on the course of internal computation or +on the evaluation of expressions, but only affect input and +output conversion, respectively. +.SH +Scaling +.PP +A third special internal quantity called `scale' is +used to determine the scale of calculated +quantities. +Numbers may have +up to a specific number of decimal digits after the decimal point. +This fractional part is retained in further computations. +We refer to the number of digits after the decimal point of +a number as its scale. +The current implementation allows scales to be as large as can be +represented by a 32-bit unsigned number minus one. +This is a non-portable extension. +The original implementation allowed for a maximum scale of 99. +.PP +When two scaled numbers are combined by +means of one of the arithmetic operations, the result +has a scale determined by the following rules. For +addition and subtraction, the scale of the result is the larger +of the scales of the two operands. In this case, +there is never any truncation of the result. +For multiplications, the scale of the result is never +less than the maximum of the two scales of the operands, +never more than the sum of the scales of the operands +and, subject to those two restrictions, +the scale of the result is set equal to the contents of the internal +quantity `scale'. +The scale of a quotient is the contents of the internal +quantity `scale'. The scale of a remainder is +the sum of the scales of the quotient and the divisor. +The result of an exponentiation is scaled as if +the implied multiplications were performed. +An exponent must be an integer. +The scale of a square root is set to the maximum of the scale +of the argument and the contents of `scale'. +.PP +All of the internal operations are actually carried out in terms +of integers, with digits being discarded when necessary. +In every case where digits are discarded, truncation and +not rounding is performed. +.PP +The contents of +`scale' must be no greater than +4294967294 and no less than 0. It is initially set to 0. +.PP +The internal quantities `scale', `ibase', and `obase' can be +used in expressions just like other variables. +The line +.DS +.ft B +scale = scale + 1 +.ft P +.DE +increases the value of `scale' by one, and the line +.DS +.ft B +scale +.ft P +.DE +causes the current value of `scale' to be printed. +.PP +The value of `scale' retains its meaning as a +number of decimal digits to be retained in internal +computation even when `ibase' or `obase' are not equal to 10. +The internal computations (which are still conducted in decimal, +regardless of the bases) are performed to the specified number +of decimal digits, never hexadecimal or octal or any +other kind of digits. +.SH +Functions +.PP +The name of a function is a single lower-case letter. +Function names are permitted to collide with simple +variable names. +Twenty-six different defined functions are permitted +in addition to the twenty-six variable names. +The line +.DS +.ft B + define a(x){ +.ft P +.DE +begins the definition of a function with one argument. +This line must be followed by one or more statements, +which make up the body of the function, ending +with a right brace }. +Return of control from a function occurs when a return +statement is executed or when the end of the function is reached. +The return statement can take either +of the two forms +.DS +.ft B +return +return(x) +.ft P +.DE +In the first case, the value of the function is 0, and in +the second, the value of the expression in parentheses. +.PP +Variables used in the function can be declared as automatic +by a statement of the form +.DS +.ft B +auto x,y,z +.ft P +.DE +There can be only one `auto' statement in a function and it must +be the first statement in the definition. +These automatic variables are allocated space and initialized +to zero on entry to the function and thrown away on return. The +values of any variables with the same names outside the function +are not disturbed. +Functions may be called recursively and the automatic variables +at each level of call are protected. +The parameters named in a function definition are treated in +the same way as the automatic variables of that function +with the single exception that they are given a value +on entry to the function. +An example of a function definition is +.DS +.ft B + define a(x,y){ + auto z + z = x*y + return(z) + } +.ft P +.DE +The value of this function, when called, will be the +product of its +two arguments. +.PP +A function is called by the appearance of its name +followed by a string of arguments enclosed in +parentheses and separated by commas. +The result +is unpredictable if the wrong number of arguments is used. +.PP +Functions with no arguments are defined and called using +parentheses with nothing between them: b(). +.PP +If the function +.ft I +a +.ft +above has been defined, then the line +.DS +.ft B +a(7,3.14) +.ft P +.DE +would cause the result 21.98 to be printed and the line +.DS +.ft B +x = a(a(3,4),5) +.ft P +.DE +would cause the value of x to become 60. +.SH +Subscripted Variables +.PP +A single lower-case letter variable name +followed by an expression in brackets is called a subscripted +variable (an array element). +The variable name is called the array name and the expression +in brackets is called the subscript. +Only one-dimensional arrays are +permitted. The names of arrays are permitted to +collide with the names of simple variables and function names. +Any fractional +part of a subscript is discarded before use. +Subscripts must be greater than or equal to zero and +less than or equal to 2047. +.PP +Subscripted variables may be freely used in expressions, in +function calls, and in return statements. +.PP +An array name may be used as an argument to a function, +or may be declared as automatic in +a function definition by the use of empty brackets: +.DS +.ft B +f(a[\|]) +define f(a[\|]) +auto a[\|] +.ft P +.DE +When an array name is so used, the whole contents of the array +are copied for the use of the function, and thrown away on exit +from the function. +Array names which refer to whole arrays cannot be used +in any other contexts. +.SH +Control Statements +.PP +The `if', the `while', and the `for' statements +may be used to alter the flow within programs or to cause iteration. +The range of each of them is a statement or +a compound statement consisting of a collection of +statements enclosed in braces. +They are written in the following way +.DS +.ft B +if(relation) statement +if(relation) statement else statement +while(relation) statement +for(expression1; relation; expression2) statement +.ft P +.DE +or +.DS +.ft B +if(relation) {statements} +if(relation) {statements} else {statements} +while(relation) {statements} +for(expression1; relation; expression2) {statements} +.ft P +.DE +.PP +A relation in one of the control statements is an expression of the form +.DS +.ft B +x>y +.ft P +.DE +where two expressions are related by one of the six relational +operators `<', `>', `<=', `>=', `==', or `!='. +The relation `==' +stands for `equal to' and `!=' stands for `not equal to'. +The meaning of the remaining relational operators is +clear. +.PP +BEWARE of using `=' instead of `==' in a relational. Unfortunately, +both of them are legal, so you will not get a diagnostic +message, but `=' really will not do a comparison. +.PP +The `if' statement causes execution of its range +if and only if the relation is true. +Then control passes to the next statement in sequence. +If an `else' branch is present, the statements in this branch are +executed if the relation is false. +The `else' keyword is a non-portable extension. +.PP +The `while' statement causes execution of its range +repeatedly as long as the relation +is true. The relation is tested before each execution +of its range and if the relation +is false, control passes to the next statement beyond the range +of the while. +.PP +The `for' statement begins +by executing `expression1'. Then the relation is tested +and, if true, the statements in the range of the `for' are executed. +Then `expression2' is executed. The relation is tested, and so on. +The typical use of the `for' statement is for a controlled iteration, +as in the statement +.DS +.ft B +for(i=1; i<=10; i=i+1) i +.ft P +.DE +which will print the integers from 1 to 10. +Here are some examples of the use of the control statements. +.DS +.ft B +define f(n){ +auto i, x +x=1 +for(i=1; i<=n; i=i+1) x=x*i +return(x) +} +.ft P +.DE +The line +.DS +.ft B + f(a) +.ft P +.DE +will print +.ft I +a +.ft +factorial if +.ft I +a +.ft +is a positive integer. +Here is the definition of a function which will +compute values of the binomial coefficient +(m and n are assumed to be positive integers). +.DS +.ft B +define b(n,m){ +auto x, j +x=1 +for(j=1; j<=m; j=j+1) x=x*(n\-j+1)/j +return(x) +} +.ft P +.DE +The following function computes values of the exponential function +by summing the appropriate series +without regard for possible truncation errors: +.DS +.ft B +scale = 20 +define e(x){ + auto a, b, c, d, n + a = 1 + b = 1 + c = 1 + d = 0 + n = 1 + while(1==1){ + a = a*x + b = b*n + c = c + a/b + n = n + 1 + if(c==d) return(c) + d = c + } +} +.ft P +.DE +.SH +Some Details +.PP +There are some language features that every user should know +about even if he will not use them. +.PP +Normally statements are typed one to a line. It is also permissible +to type several statements on a line separated by semicolons. +.PP +If an assignment statement is parenthesized, it then has +a value and it can be used anywhere that an expression can. +For example, the line +.DS +.ft B +(x=y+17) +.ft P +.DE +not only makes the indicated assignment, but also prints the +resulting value. +.PP +Here is an example of a use of the value of an +assignment statement even when it is not parenthesized. +.DS +.ft B +x = a[i=i+1] +.ft P +.DE +causes a value to be assigned to x and also increments i +before it is used as a subscript. +.PP +The following constructs work in BC in exactly the same manner +as they do in the C language. Consult the appendix or the +C manuals [2] for their exact workings. +.DS +.ft B +.ta 2i +x=y=z is the same as x=(y=z) +x += y x = x+y +x \-= y x = x\-y +x *= y x = x*y +x /= y x = x/y +x %= y x = x%y +x ^= y x = x^y +x++ (x=x+1)\-1 +x\-\- (x=x\-1)+1 +++x x = x+1 +\-\-x x = x\-1 +.ft P +.DE +Even if you don't intend to use the constructs, +if you type one inadvertently, something correct but unexpected +may happen. +.SH +Three Important Things +.PP +1. To exit a BC program, type `quit'. +.PP +2. There is a comment convention identical to that of C and +of PL/I. Comments begin with `/*' and end with `*/'. +As a non-portable extension, comments may also start with a `#' and end with +a newline. +The newline is not part of the comment. +.PP +3. There is a library of math functions which may be obtained by +typing at command level +.DS +.ft B +bc \-l +.ft P +.DE +This command will load a set of library functions +which, at the time of writing, consists of sine (named `s'), +cosine (`c'), arctangent (`a'), natural logarithm (`l'), +exponential (`e') and Bessel functions of integer order (`j(n,x)'). Doubtless more functions will be added +in time. +The library sets the scale to 20. You can reset it to something +else if you like. +The design of these mathematical library routines +is discussed elsewhere [3]. +.PP +If you type +.DS +.ft B +bc file ... +.ft P +.DE +BC will read and execute the named file or files before accepting +commands from the keyboard. In this way, you may load your +favorite programs and function definitions. +.SH +Acknowledgement +.PP +The compiler is written in YACC [4]; its original +version was written by S. C. Johnson. +.SH +References +.IP [1] +K. Thompson and D. M. Ritchie, +.ft I +UNIX Programmer's Manual, +.ft +Bell Laboratories, +1978. +.IP [2] +B. W. Kernighan and +D. M. Ritchie, +.ft I +The C Programming Language, +.ft +Prentice-Hall, 1978. +.IP [3] +R. Morris, +.ft I +A Library of Reference Standard Mathematical Subroutines, +.ft +Bell Laboratories internal memorandum, 1975. +.IP [4] +S. C. Johnson, +.ft I +YACC \(em Yet Another Compiler-Compiler. +.ft +Bell Laboratories Computing Science Technical Report #32, 1978. +.IP [5] +R. Morris and L. L. Cherry, +.ft I +DC \- An Interactive Desk Calculator. +.ft +.LP +.bp +.ft B +.DS C +Appendix +.DE +.ft +.NH +Notation +.PP +In the following pages syntactic categories are in \fIitalics\fP; +literals are in \fBbold\fP; material in brackets [\|] is optional. +.NH +Tokens +.PP +Tokens consist of keywords, identifiers, constants, operators, +and separators. +Token separators may be blanks, tabs or comments. +Newline characters or semicolons separate statements. +.NH 2 +Comments +.PP +Comments are introduced by the characters /* and terminated by +*/. +As a non-portable extension, comments may also start with a # and +end with a newline. +The newline is not part of the comment. +.NH 2 +Identifiers +.PP +There are three kinds of identifiers \- ordinary identifiers, array identifiers +and function identifiers. +All three types consist of single lower-case letters. +Array identifiers are followed by square brackets, possibly +enclosing an expression describing a subscript. +Arrays are singly dimensioned and may contain up to 2048 +elements. +Indexing begins at zero so an array may be indexed from 0 to 2047. +Subscripts are truncated to integers. +Function identifiers are followed by parentheses, possibly enclosing arguments. +The three types of identifiers do not conflict; +a program can have a variable named \fBx\fP, +an array named \fBx\fP and a function named \fBx\fP, all of which are separate and +distinct. +.NH 2 +Keywords +.PP +The following are reserved keywords: +.ft B +.ta .5i 1.0i +.nf + ibase if + obase break + scale define + sqrt auto + length return + while quit + for continue + else last + print +.fi +.ft +.NH 2 +Constants +.PP +Constants consist of arbitrarily long numbers +with an optional decimal point. +The hexadecimal digits \fBA\fP\-\fBF\fP are also recognized as digits with +values 10\-15, respectively. +.NH 1 +Expressions +.PP +The value of an expression is printed unless the main +operator is an assignment. +The value printed is assigned to the special variable \fBlast\fP. +A single dot may be used as a synonym for \fBlast\fP. +This is a non-portable extension. +Precedence is the same as the order +of presentation here, with highest appearing first. +Left or right associativity, where applicable, is +discussed with each operator. +.bp +.NH 2 +Primitive expressions +.NH 3 +Named expressions +.PP +Named expressions are +places where values are stored. +Simply stated, +named expressions are legal on the left +side of an assignment. +The value of a named expression is the value stored in the place named. +.NH 4 +\fIidentifiers\fR +.PP +Simple identifiers are named expressions. +They have an initial value of zero. +.NH 4 +\fIarray-name\fP\|[\|\fIexpression\fP\|] +.PP +Array elements are named expressions. +They have an initial value of zero. +.NH 4 +\fBscale\fR, \fBibase\fR and \fBobase\fR +.PP +The internal registers +\fBscale\fP, \fBibase\fP and \fBobase\fP are all named expressions. +\fBscale\fP is the number of digits after the decimal point to be +retained in arithmetic operations. +\fBscale\fR has an initial value of zero. +\fBibase\fP and \fBobase\fP are the input and output number +radix respectively. +Both \fBibase\fR and \fBobase\fR have initial values of 10. +.NH 3 +Function calls +.NH 4 +\fIfunction-name\fB\|(\fR[\fIexpression\fR\|[\fB,\|\fIexpression\|\fR.\|.\|.\|]\|]\fB) +.PP +A function call consists of a function name followed by parentheses +containing a comma-separated list of +expressions, which are the function arguments. +A whole array passed as an argument is specified by the +array name followed by empty square brackets. +All function arguments are passed by +value. +As a result, changes made to the formal parameters have +no effect on the actual arguments. +If the function terminates by executing a return +statement, the value of the function is +the value of the expression in the parentheses of the return +statement or is zero if no expression is provided +or if there is no return statement. +.NH 4 +sqrt\|(\|\fIexpression\fP\|) +.PP +The result is the square root of the expression. +The result is truncated in the least significant decimal place. +The scale of the result is +the scale of the expression or the +value of +.ft B +scale, +.ft +whichever is larger. +.NH 4 +length\|(\|\fIexpression\fP\|) +.PP +The result is the total number of significant decimal digits in the expression. +The scale of the result is zero. +.NH 4 +scale\|(\|\fIexpression\fP\|) +.PP +The result is the scale of the expression. +The scale of the result is zero. +.NH 3 +Constants +.PP +Constants are primitive expressions. +.NH 3 +Parentheses +.PP +An expression surrounded by parentheses is +a primitive expression. +The parentheses are used to alter the +normal precedence. +.NH 2 +Unary operators +.PP +The unary operators +bind right to left. +.NH 3 +\-\|\fIexpression\fP +.PP +The result is the negative of the expression. +.NH 3 +++\|\fInamed-expression\fP +.PP +The named expression is +incremented by one. +The result is the value of the named expression after +incrementing. +.NH 3 +\-\-\|\fInamed-expression\fP +.PP +The named expression is +decremented by one. +The result is the value of the named expression after +decrementing. +.NH 3 +\fInamed-expression\fP\|++ +.PP +The named expression is +incremented by one. +The result is the value of the named expression before +incrementing. +.NH 3 +\fInamed-expression\fP\|\-\- +.PP +The named expression is +decremented by one. +The result is the value of the named expression before +decrementing. +.NH 2 +Exponentiation operator +.PP +The exponentiation operator binds right to left. +.NH 3 +\fIexpression\fP ^ \fIexpression\fP +.PP +The result is the first +expression raised to the power of the +second expression. +The second expression must be an integer. +If \fIa\fP +is the scale of the left expression +and \fIb\fP is the absolute value +of the right expression, +then the scale of the result is: +.PP +min\|(\|\fIa\(mub\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP\|)\|) +.NH 2 +Multiplicative operators +.PP +The operators *, /, % bind left to right. +.NH 3 +\fIexpression\fP * \fIexpression\fP +.PP +The result is the product +of the two expressions. +If \fIa\fP and \fIb\fP are the +scales of the two expressions, +then the scale of the result is: +.PP +min\|(\|\fIa+b\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP,\|\fIb\fP\|)\|) +.NH 3 +\fIexpression\fP / \fIexpression\fP +.PP +The result is the quotient of the two expressions. +The scale of the result is the value of \fBscale\fR. +.NH 3 +\fIexpression\fP % \fIexpression\fP +.PP +The % operator produces the remainder of the division +of the two expressions. +More precisely, +\fIa\fP%\fIb\fP is \fIa\fP\-\fIa\fP/\fIb\fP*\fIb\fP. +.PP +The scale of the result is the sum of the scale of +the divisor and the value of +.ft B +scale +.ft +.NH 2 +Additive operators +.PP +The additive operators bind left to right. +.NH 3 +\fIexpression\fP + \fIexpression\fP +.PP +The result is the sum of the two expressions. +The scale of the result is +the maximum of the scales of the expressions. +.NH 3 +\fIexpression\fP \- \fIexpression\fP +.PP +The result is the difference of the two expressions. +The scale of the result is the +maximum of the scales of the expressions. +.NH 2 +assignment operators +.PP +The assignment operators bind right to left. +.NH 3 +\fInamed-expression\fP = \fIexpression\fP +.PP +This expression results in assigning the value of the expression +on the right +to the named expression on the left. +.NH 3 +\fInamed-expression\fP += \fIexpression\fP +.NH 3 +\fInamed-expression\fP \-= \fIexpression\fP +.NH 3 +\fInamed-expression\fP *= \fIexpression\fP +.NH 3 +\fInamed-expression\fP /= \fIexpression\fP +.NH 3 +\fInamed-expression\fP %= \fIexpression\fP +.NH 3 +\fInamed-expression\fP ^= \fIexpression\fP +.PP +The result of the above expressions is equivalent +to ``named expression = named expression OP expression'', +where OP is the operator after the = sign. +.NH 1 +Relations +.PP +Unlike all other operators, the relational operators +are only valid as the object of an \fBif\fP, \fBwhile\fP, +or inside a \fBfor\fP statement. +.NH 2 +\fIexpression\fP < \fIexpression\fP +.NH 2 +\fIexpression\fP > \fIexpression\fP +.NH 2 +\fIexpression\fP <= \fIexpression\fP +.NH 2 +\fIexpression\fP >= \fIexpression\fP +.NH 2 +\fIexpression\fP == \fIexpression\fP +.NH 2 +\fIexpression\fP != \fIexpression\fP +.NH 1 +Storage classes +.PP +There are only two storage classes in BC, global and automatic +(local). +Only identifiers that are to be local to a function need be +declared with the \fBauto\fP command. +The arguments to a function +are local to the function. +All other identifiers are assumed to be global +and available to all functions. +All identifiers, global and local, have initial values +of zero. +Identifiers declared as \fBauto\fP are allocated on entry to the function +and released on returning from the function. +They therefore do not retain values between function calls. +\fBauto\fP arrays are specified by the array name followed by empty square brackets. +.PP +Automatic variables in BC do not work in exactly the same way +as in either C or PL/I. On entry to a function, the old values of +the names that appear as parameters and as automatic +variables are pushed onto a stack. +Until return is made from the function, reference to these +names refers only to the new values. +.NH 1 +Statements +.PP +Statements must be separated by semicolon or newline. +Except where altered by control statements, execution +is sequential. +.NH 2 +Expression statements +.PP +When a statement is an expression, unless +the main operator is an assignment, the value +of the expression is printed, followed by a newline character. +.NH 2 +Compound statements +.PP +Statements may be grouped together and used when one statement is expected +by surrounding them with { }. +.NH 2 +Quoted string statements +.PP +"any string" +.sp .5 +This statement prints the string inside the quotes. +.NH 2 +If statements +.sp .5 +\fBif\|(\|\fIrelation\fB\|)\|\fIstatement\fR +.PP +The substatement is executed if the relation is true. +.NH 2 +If-else statements +.sp .5 +\fBif\|(\|\fIrelation\fB\|)\|\fIstatement\fB\|else\|\fIstatement\fR +.PP +The first substatement is executed if the relation is true, the second +substatement if the relation is false. +The \fBif-else\fR statement is a non-portable extension. +.NH 2 +While statements +.sp .5 +\fBwhile\|(\|\fIrelation\fB\|)\|\fIstatement\fR +.PP +The statement is executed while the relation +is true. +The test occurs before each execution of the statement. +.NH 2 +For statements +.sp .5 +\fBfor\|(\|\fIexpression\fB; \fIrelation\fB; \fIexpression\fB\|)\|\fIstatement\fR +.PP +The \fBfor\fR statement is the same as +.nf +.ft I + first-expression + \fBwhile\|(\fPrelation\|\fB) {\fP + statement + last-expression + } +.ft R +.fi +.PP +All three expressions may be left out. +This is a non-portable extension. +.NH 2 +Break statements +.sp .5 +\fBbreak\fP +.PP +\fBbreak\fP causes termination of a \fBfor\fP or \fBwhile\fP statement. +.NH 2 +Continue statements +.sp .5 +\fBcontinue\fP +.PP +\fBcontinue\fP causes the next iteration of a \fBfor\fP or \fBwhile\fP +statement to start, skipping the remainder of the loop. +For a \fBwhile\fP statement, execution continues with the evaluation +of the condition. +For a \fBfor\fP statement, execution continues with evaluation of +the last-expression. +The \fBcontinue\fP statement is a non-portable extension. +.NH 2 +Auto statements +.sp .5 +\fBauto \fIidentifier\fR\|[\|\fB,\fIidentifier\fR\|] +.PP +The \fBauto\fR statement causes the values of the identifiers to be pushed down. +The identifiers can be ordinary identifiers or array identifiers. +Array identifiers are specified by following the array name by empty square +brackets. +The auto statement must be the first statement +in a function definition. +.NH 2 +Define statements +.sp .5 +.nf +\fBdefine(\|\fR[\fIparameter\|\fR[\fB\|,\|\fIparameter\|.\|.\|.\|\fR]\|]\|\fB)\|{\fI + statements\|\fB}\fR +.fi +.PP +The \fBdefine\fR statement defines a function. +The parameters may +be ordinary identifiers or array names. +Array names must be followed by empty square brackets. +As a non-portable extension, the opening brace may also appear on the +next line. +.NH 2 +Return statements +.sp .5 +\fBreturn\fP +.sp .5 +\fBreturn(\fI\|expression\|\fB)\fR +.PP +The \fBreturn\fR statement causes termination of a function, +popping of its auto variables, and +specifies the result of the function. +The first form is equivalent to \fBreturn(0)\fR. +The result of the function is the result of the expression +in parentheses. +Leaving out the expression between parentheses is equivalent to +\fBreturn(0)\fR. +As a non-portable extension, the parentheses may be left out. +.NH 2 +Print +.PP +The \fBprint\fR statement takes a list of comma-separated expressions. +Each expression in the list is evaluated and the computed +value is printed and assigned to the variable `last'. +No trailing newline is printed. +The expression may also be a string enclosed in double quotes. +Within these strings the following escape sequences may be used: +\ea +for bell (alert), +`\eb' +for backspace, +`\ef' +for formfeed, +`\en' +for newline, +`\er' +for carriage return, +`\et' +`for tab, +`\eq' +for double quote and +`\e\e' +for backslash. +Any other character following a backslash will be ignored. +Strings will not be assigned to `last'. +The \fBprint\fR statement is a non-portable extension. +.NH 2 +Quit +.PP +The \fBquit\fR statement stops execution of a BC program and returns +control to UNIX when it is first encountered. +Because it is not treated as an executable statement, +it cannot be used +in a function definition or in an +.ft B +if, for, +.ft +or +.ft B +while +.ft +statement. diff --git a/usr.bin/bc/bc.1 b/usr.bin/bc/bc.1 new file mode 100644 index 000000000000..a623b9c85bc9 --- /dev/null +++ b/usr.bin/bc/bc.1 @@ -0,0 +1,400 @@ +.\" $FreeBSD$ +.\" $OpenBSD: bc.1,v 1.25 2010/01/02 19:48:56 schwarze Exp $ +.\" +.\" Copyright (C) Caldera International Inc. 2001-2002. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code and documentation must retain the above +.\" copyright notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed or owned by Caldera +.\" International, Inc. +.\" 4. Neither the name of Caldera International, Inc. nor the names of other +.\" contributors may be used to endorse or promote products derived from +.\" this software without specific prior written permission. +.\" +.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA +.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, +.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.\" @(#)bc.1 6.8 (Berkeley) 8/8/91 +.\" +.Dd May 31 2007 +.Dt BC 1 +.Os +.Sh NAME +.Nm bc +.Nd arbitrary-precision arithmetic language and calculator +.Sh SYNOPSIS +.Nm bc +.Op Fl cl +.Op Fl e Ar expression +.Op Ar file ... +.Sh DESCRIPTION +.Nm +is an interactive processor for a language which resembles +C but provides unlimited precision arithmetic. +It takes input from any expressions on the command line and +any files given, then reads the standard input. +.Pp +Options available: +.Bl -tag -width Ds +.It Fl c +.It Fl d +.It Fl Fl debug +.Nm +is actually a preprocessor for +.Xr dc 1 , +which it invokes automatically, unless the +.Fl c +.Pq compile only +option is present. +In this case the generated +.Xr dc 1 +instructions are sent to the standard output, +instead of being interpreted by a running +.Xr dc 1 +process. +.It Fl e Ar exp +.It Fl Fl expression Ar exp +Evaluate +.Ar expression . +If multiple +.Fl e +options are specified, they are processed in the order given, +separated by newlines. +.It Fl h +.It Fl Fl help +Prints usage information. +.It Fl l +.It Fl Fl mathlib +Allow specification of an arbitrary precision math library. +The definitions in the library are available to command line +expressions. +.It Fl v +.It Fl Fl version +Prints version information. +.El +.Pp +The syntax for +.Nm +programs is as follows: +.Sq L +means letter a-z; +.Sq E +means expression; +.Sq S +means statement. +As a non-portable extension, it is possible to use long names +in addition to single letter names. +A long name is a sequence starting with a lowercase letter +followed by any number of lowercase letters and digits. +The underscore character +.Pq Sq _ +counts as a letter. +.Pp +Comments +.Bd -unfilled -offset indent -compact +are enclosed in /* and */ +are enclosed in # and the next newline +.Ed +.Pp +The newline is not part of the line comment, +which in itself is a non-portable extension. +.Pp +Names +.Bd -unfilled -offset indent -compact +simple variables: L +array elements: L [ E ] +The words `ibase', `obase', and `scale' +The word `last' or a single dot +.Ed +.Pp +Other operands +.Bd -unfilled -offset indent -compact +arbitrarily long numbers with optional sign and decimal point +( E ) +sqrt ( E ) +length ( E ) number of significant decimal digits +scale ( E ) number of digits right of decimal point +L ( E , ... , E ) +.Ed +.Pp +The sequence +.Sq \e<newline><whitespace> +is ignored within numbers. +.Pp +Operators +.Pp +The following arithmetic and logical operators can be used. +The semantics of the operators is the same as in the C language. +They are listed in order of decreasing precedence. +Operators in the same group have the same precedence. +.Bl -column -offset indent "= += \-= *= /= %= ^=" "Associativity" \ +"multiply, divide, modulus" +.It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description" +.It "++ \-\-" Ta "none" Ta "increment, decrement" +.It "\-" Ta "none" Ta "unary minus" +.It "^" Ta "right" Ta "power" +.It "* / %" Ta "left" Ta "multiply, divide, modulus" +.It "+ \-" Ta "left" Ta "plus, minus" +.It "= += -= *= /= %= ^=" Ta "right" Ta "assignment" +.It "== <= >= != < >" Ta "none" Ta "relational" +.It "!" Ta "none" Ta "boolean not" +.It "&&" Ta "left" Ta "boolean and" +.It "||" Ta "left" Ta "boolean or" +.El +.Pp +Note the following: +.Bl -bullet -offset indent +.It +The relational operators may appear in any expression. +The +.St -p1003.2 +standard only allows them in the conditional expression of an +.Sq if , +.Sq while +or +.Sq for +statement. +.It +The relational operators have a lower precedence than the assignment +operators. +This has the consequence that the expression +.Sy a = b < c +is interpreted as +.Sy (a = b) < c , +which is probably not what the programmer intended. +.It +In contrast with the C language, the relational operators all have +the same precedence, and are non-associative. +The expression +.Sy a < b < c +will produce a syntax error. +.It +The boolean operators (!, && and ||) are non-portable extensions. +.It +The boolean not +(!) operator has much lower precedence than the same operator in the +C language. +This has the consequence that the expression +.Sy !a < b +is interpreted as +.Sy !(a < b) . +Prudent programmers use parentheses when writing expressions involving +boolean operators. +.El +.Pp +Statements +.Bd -unfilled -offset indent -compact +E +{ S ; ... ; S } +if ( E ) S +if ( E ) S else S +while ( E ) S +for ( E ; E ; E ) S +null statement +break +continue +quit +a string of characters, enclosed in double quotes +print E ,..., E +.Ed +.Pp +A string may contain any character, except double quote. +The if statement with an else branch is a non-portable extension. +All three E's in a for statement may be empty. +This is a non-portable extension. +The continue and print statements are also non-portable extensions. +.Pp +The print statement takes a list of comma-separated expressions. +Each expression in the list is evaluated and the computed +value is printed and assigned to the variable `last'. +No trailing newline is printed. +The expression may also be a string enclosed in double quotes. +Within these strings the following escape sequences may be used: +.Sq \ea +for bell (alert), +.Sq \eb +for backspace, +.Sq \ef +for formfeed, +.Sq \en +for newline, +.Sq \er +for carriage return, +.Sq \et +for tab, +.Sq \eq +for double quote and +.Sq \e\e +for backslash. +Any other character following a backslash will be ignored. +Strings will not be assigned to `last'. +.Pp +Function definitions +.Bd -unfilled -offset indent +define L ( L ,..., L ) { + auto L, ... , L + S; ... S + return ( E ) +} +.Ed +.Pp +As a non-portable extension, the opening brace of the define statement +may appear on the next line. +The return statement may also appear in the following forms: +.Bd -unfilled -offset indent +return +return () +return E +.Ed +.Pp +The first two are equivalent to the statement +.Dq return 0 . +The last form is a non-portable extension. +Not specifying a return statement is equivalent to writing +.Dq return (0) . +.Pp +Functions available in the math library, which is loaded by specifying the +.Fl l +flag on the command line +.Pp +.Bl -tag -width j(n,x) -offset indent -compact +.It s(x) +sine +.It c(x) +cosine +.It e(x) +exponential +.It l(x) +log +.It a(x) +arctangent +.It j(n,x) +Bessel function +.El +.Pp +All function arguments are passed by value. +.Pp +The value of a statement that is an expression is printed +unless the main operator is an assignment. +The value printed is assigned to the special variable `last'. +This is a non-portable extension. +A single dot may be used as a synonym for `last'. +Either semicolons or newlines may separate statements. +Assignment to +.Ar scale +influences the number of digits to be retained on arithmetic +operations in the manner of +.Xr dc 1 . +Assignments to +.Ar ibase +or +.Ar obase +set the input and output number radix respectively. +.Pp +The same letter may be used as an array, a function, +and a simple variable simultaneously. +All variables are global to the program. +`Auto' variables are pushed down during function calls. +When using arrays as function arguments +or defining them as automatic variables, +empty square brackets must follow the array name. +.Pp +For example +.Bd -literal -offset indent +scale = 20 +define e(x){ + auto a, b, c, i, s + a = 1 + b = 1 + s = 1 + for(i=1; 1==1; i++){ + a = a*x + b = b*i + c = a/b + if(c == 0) return(s) + s = s+c + } +} +.Ed +.Pp +defines a function to compute an approximate value of +the exponential function and +.Pp +.Dl for(i=1; i<=10; i++) e(i) +.Pp +prints approximate values of the exponential function of +the first ten integers. +.Bd -literal -offset indent +$ bc -l -e 'scale = 500; 2 * a(2^10000)' -e quit +.Ed +.Pp +prints an approximation of pi. +.Sh FILES +.Bl -tag -width /usr/share/misc/bc.library -compact +.It Pa /usr/share/misc/bc.library +math library, read when the +.Fl l +option is specified on the command line. +.El +.Sh SEE ALSO +.Xr dc 1 +.Pp +"BC \- An Arbitrary Precision Desk-Calculator Language", +.Pa /usr/share/doc/usd/06.bc/ . +.Sh STANDARDS +The +.Nm +utility is compliant with the +.St -p1003.1-2008 +specification. +.Pp +The flags +.Op Fl ce +are extensions to that specification. +.Sh HISTORY +The +.Nm +first command appeared in +.At v6 . +A complete rewrite of the +.Nm +command first appeared in +.Ox 3.5 . +.Sh AUTHORS +.An -nosplit +The original version of the +.Nm +command was written by +.An Robert Morris +and +.An Lorinda Cherry . +The current version of the +.Nm +utility was written by +.An Otto Moerbeek . +.Sh BUGS +.Ql Quit +is interpreted when read, not when executed. +.Pp +Some non-portable extensions, as found in the GNU version of the +.Nm +utility are not implemented (yet). diff --git a/usr.bin/bc/bc.library b/usr.bin/bc/bc.library new file mode 100644 index 000000000000..c3145ab9d33a --- /dev/null +++ b/usr.bin/bc/bc.library @@ -0,0 +1,263 @@ +/* $FreeBSD$ */ +/* $OpenBSD: bc.library,v 1.3 2007/02/03 21:15:06 otto Exp $ */ + +/* + * Copyright (C) Caldera International Inc. 2001-2002. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code and documentation must retain the above + * copyright notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed or owned by Caldera + * International, Inc. + * 4. Neither the name of Caldera International, Inc. nor the names of other + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA + * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, + * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * @(#)bc.library 5.1 (Berkeley) 4/17/91 + */ + +scale = 20 +define e(x) { + auto a, b, c, d, e, g, t, w, y, r + + r = ibase + ibase = A + t = scale + scale = t + .434*x + 1 + + w = 0 + if (x < 0) { + x = -x + w = 1 + } + y = 0 + while (x > 2) { + x = x/2 + y = y + 1 + } + + a = 1 + b = 1 + c = b + d = 1 + e = 1 + for (a = 1; 1 == 1; a++) { + b = b*x + c = c*a + b + d = d*a + g = c/d + if (g == e) { + g = g/1 + while (y--) { + g = g*g + } + scale = t + ibase = r + if (w == 1) return (1/g) + return (g/1) + } + e = g + } +} + +define l(x) { + auto a, b, c, d, e, f, g, u, s, t, r + r = ibase + ibase = A + if (x <= 0) { + a = (1 - 10^scale) + ibase = r + return (a) + } + t = scale + + f = 1 + scale = scale + scale(x) - length(x) + 1 + s = scale + while (x > 2) { + s = s + (length(x) - scale(x))/2 + 1 + if (s > 0) scale = s + x = sqrt(x) + f = f*2 + } + while (x < .5) { + s = s + (length(x) - scale(x))/2 + 1 + if (s > 0) scale = s + x = sqrt(x) + f = f*2 + } + + scale = t + length(f) - scale(f) + 1 + u = (x - 1)/(x + 1) + + scale = scale + 1.1*length(t) - 1.1*scale(t) + s = u*u + b = 2*f + c = b + d = 1 + e = 1 + for (a = 3; 1 == 1 ; a = a + 2) { + b = b*s + c = c*a + d*b + d = d*a + g = c/d + if (g == e) { + scale = t + ibase = r + return (u*c/d) + } + e = g + } +} + +define s(x) { + auto a, b, c, s, t, y, p, n, i, r + r = ibase + ibase = A + t = scale + y = x/.7853 + s = t + length(y) - scale(y) + if (s < t) s = t + scale = s + p = a(1) + + scale = 0 + if (x >= 0) n = (x/(2*p) + 1)/2 + if (x < 0) n = (x/(2*p) - 1)/2 + x = x - 4*n*p + if (n % 2 != 0) x = -x + + scale = t + length(1.2*t) - scale(1.2*t) + y = -x*x + a = x + b = 1 + s = x + for (i =3 ; 1 == 1; i = i + 2) { + a = a*y + b = b*i*(i - 1) + c = a/b + if (c == 0) { + scale = t + ibase = r + return (s/1) + } + s = s + c + } +} + +define c(x) { + auto t, r + r = ibase + ibase = A + t = scale + scale = scale + 1 + x = s(x + 2*a(1)) + scale = t + ibase = r + return (x/1) +} + +define a(x) { + auto a, b, c, d, e, f, g, s, t, r + if (x == 0) return(0) + + r = ibase + ibase = A + if (x == 1) { + if (scale < 52) { + a = .7853981633974483096156608458198757210492923498437764/1 + ibase = r + return (a) + } + } + t = scale + f = 1 + while (x > .5) { + scale = scale + 1 + x = -(1 - sqrt(1. + x*x))/x + f = f*2 + } + while (x < -.5) { + scale = scale + 1 + x = -(1 - sqrt(1. + x*x))/x + f = f*2 + } + s = -x*x + b = f + c = f + d = 1 + e = 1 + for (a = 3; 1 == 1; a = a + 2) { + b = b*s + c = c*a + d*b + d = d*a + g = c/d + if (g == e) { + ibase = r + scale = t + return (x*c/d) + } + e = g + } +} + +define j(n,x) { + auto a, b, c, d, e, g, i, s, k, t, r + + r = ibase + ibase = A + t = scale + k = 1.36*x + 1.16*t - n + k = length(k) - scale(k) + if (k > 0) scale = scale + k + + s = -x*x/4 + if (n < 0) { + n = -n + x = -x + } + a = 1 + c = 1 + for (i = 1; i <= n; i++) { + a = a*x + c = c*2*i + } + b = a + d = 1 + e = 1 + for (i = 1; 1; i++) { + a = a*s + b = b*i*(n + i) + a + c = c*i*(n + i) + g = b/c + if (g == e) { + ibase = r + scale = t + return (g/1) + } + e = g + } +} diff --git a/usr.bin/bc/bc.y b/usr.bin/bc/bc.y new file mode 100644 index 000000000000..6492168266e9 --- /dev/null +++ b/usr.bin/bc/bc.y @@ -0,0 +1,1179 @@ +%{ +/* $OpenBSD: bc.y,v 1.33 2009/10/27 23:59:36 deraadt Exp $ */ + +/* + * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * This implementation of bc(1) uses concepts from the original 4.4 + * BSD bc(1). The code itself is a complete rewrite, based on the + * Posix defined bc(1) grammar. Other differences include type safe + * usage of pointers to build the tree of emitted code, typed yacc + * rule values, dynamic allocation of all data structures and a + * completely rewritten lexical analyzer using lex(1). + * + * Some effort has been made to make sure that the generated code is + * the same as the code generated by the older version, to provide + * easy regression testing. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/types.h> +#include <sys/wait.h> + +#include <ctype.h> +#include <err.h> +#include <errno.h> +#include <getopt.h> +#include <limits.h> +#include <search.h> +#include <signal.h> +#include <stdarg.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> + +#include "extern.h" +#include "pathnames.h" + +#define BC_VER "1.0-FreeBSD" +#define END_NODE ((ssize_t) -1) +#define CONST_STRING ((ssize_t) -2) +#define ALLOC_STRING ((ssize_t) -3) + +extern char *yytext; +extern FILE *yyin; + +struct tree { + ssize_t index; + union { + char *astr; + const char *cstr; + } u; +}; + +int yyparse(void); +int yywrap(void); + +int fileindex; +int sargc; +const char **sargv; +const char *filename; +char *cmdexpr; + +static void grow(void); +static ssize_t cs(const char *); +static ssize_t as(const char *); +static ssize_t node(ssize_t, ...); +static void emit(ssize_t); +static void emit_macro(int, ssize_t); +static void free_tree(void); +static ssize_t numnode(int); +static ssize_t lookup(char *, size_t, char); +static ssize_t letter_node(char *); +static ssize_t array_node(char *); +static ssize_t function_node(char *); + +static void add_par(ssize_t); +static void add_local(ssize_t); +static void warning(const char *); +static void init(void); +static void usage(void); +static char *escape(const char *); + +static ssize_t instr_sz = 0; +static struct tree *instructions = NULL; +static ssize_t current = 0; +static int macro_char = '0'; +static int reset_macro_char = '0'; +static int nesting = 0; +static int breakstack[16]; +static int breaksp = 0; +static ssize_t prologue; +static ssize_t epilogue; +static bool st_has_continue; +static char str_table[UCHAR_MAX][2]; +static bool do_fork = true; +static u_short var_count; +static pid_t dc; + +static void sigchld(int); + +extern char *__progname; + +#define BREAKSTACK_SZ (sizeof(breakstack)/sizeof(breakstack[0])) + +/* These values are 4.4BSD bc compatible */ +#define FUNC_CHAR 0x01 +#define ARRAY_CHAR 0xa1 + +/* Skip '\0', [, \ and ] */ +#define ENCODE(c) ((c) < '[' ? (c) : (c) + 3); +#define VAR_BASE (256-4) +#define MAX_VARIABLES (VAR_BASE * VAR_BASE) + +const struct option long_options[] = +{ + {"debug", no_argument, NULL, 'd'}, + {"expression", required_argument, NULL, 'e'}, + {"help", no_argument, NULL, 'h'}, + {"mathlib", no_argument, NULL, 'l'}, + /* compatibility option */ + {"quiet", no_argument, NULL, 'q'}, + {"version", no_argument, NULL, 'v'}, + {NULL, no_argument, NULL, 0} +}; + +%} + +%start program + +%union { + ssize_t node; + struct lvalue lvalue; + const char *str; + char *astr; +} + +%token COMMA SEMICOLON LPAR RPAR LBRACE RBRACE LBRACKET RBRACKET DOT +%token NEWLINE +%token <astr> LETTER +%token <str> NUMBER STRING +%token DEFINE BREAK QUIT LENGTH +%token RETURN FOR IF WHILE SQRT +%token SCALE IBASE OBASE AUTO +%token CONTINUE ELSE PRINT + +%left BOOL_OR +%left BOOL_AND +%nonassoc BOOL_NOT +%nonassoc EQUALS LESS_EQ GREATER_EQ UNEQUALS LESS GREATER +%right <str> ASSIGN_OP +%left PLUS MINUS +%left MULTIPLY DIVIDE REMAINDER +%right EXPONENT +%nonassoc UMINUS +%nonassoc INCR DECR + +%type <lvalue> named_expression +%type <node> argument_list +%type <node> alloc_macro +%type <node> expression +%type <node> function +%type <node> function_header +%type <node> input_item +%type <node> opt_argument_list +%type <node> opt_expression +%type <node> opt_relational_expression +%type <node> opt_statement +%type <node> print_expression +%type <node> print_expression_list +%type <node> relational_expression +%type <node> return_expression +%type <node> semicolon_list +%type <node> statement +%type <node> statement_list + +%% + +program : /* empty */ + | program input_item + ; + +input_item : semicolon_list NEWLINE + { + emit($1); + macro_char = reset_macro_char; + putchar('\n'); + free_tree(); + st_has_continue = false; + } + | function + { + putchar('\n'); + free_tree(); + st_has_continue = false; + } + | error NEWLINE + { + yyerrok; + } + | error QUIT + { + yyerrok; + } + ; + +semicolon_list : /* empty */ + { + $$ = cs(""); + } + | statement + | semicolon_list SEMICOLON statement + { + $$ = node($1, $3, END_NODE); + } + | semicolon_list SEMICOLON + ; + +statement_list : /* empty */ + { + $$ = cs(""); + } + | statement + | statement_list NEWLINE + | statement_list NEWLINE statement + { + $$ = node($1, $3, END_NODE); + } + | statement_list SEMICOLON + | statement_list SEMICOLON statement + { + $$ = node($1, $3, END_NODE); + } + ; + + +opt_statement : /* empty */ + { + $$ = cs(""); + } + | statement + ; + +statement : expression + { + $$ = node($1, cs("ps."), END_NODE); + } + | named_expression ASSIGN_OP expression + { + if ($2[0] == '\0') + $$ = node($3, cs($2), $1.store, + END_NODE); + else + $$ = node($1.load, $3, cs($2), $1.store, + END_NODE); + } + | STRING + { + $$ = node(cs("["), as($1), + cs("]P"), END_NODE); + } + | BREAK + { + if (breaksp == 0) { + warning("break not in for or while"); + YYERROR; + } else { + $$ = node( + numnode(nesting - + breakstack[breaksp-1]), + cs("Q"), END_NODE); + } + } + | CONTINUE + { + if (breaksp == 0) { + warning("continue not in for or while"); + YYERROR; + } else { + st_has_continue = true; + $$ = node(numnode(nesting - + breakstack[breaksp-1] - 1), + cs("J"), END_NODE); + } + } + | QUIT + { + sigset_t mask; + + putchar('q'); + fflush(stdout); + if (dc) { + sigprocmask(SIG_BLOCK, NULL, &mask); + sigsuspend(&mask); + } else + exit(0); + } + | RETURN return_expression + { + if (nesting == 0) { + warning("return must be in a function"); + YYERROR; + } + $$ = $2; + } + | FOR LPAR alloc_macro opt_expression SEMICOLON + opt_relational_expression SEMICOLON + opt_expression RPAR opt_statement pop_nesting + { + ssize_t n; + + if (st_has_continue) + n = node($10, cs("M"), $8, cs("s."), + $6, $3, END_NODE); + else + n = node($10, $8, cs("s."), $6, $3, + END_NODE); + + emit_macro($3, n); + $$ = node($4, cs("s."), $6, $3, cs(" "), + END_NODE); + } + | IF LPAR alloc_macro pop_nesting relational_expression RPAR + opt_statement + { + emit_macro($3, $7); + $$ = node($5, $3, cs(" "), END_NODE); + } + | IF LPAR alloc_macro pop_nesting relational_expression RPAR + opt_statement ELSE alloc_macro pop_nesting opt_statement + { + emit_macro($3, $7); + emit_macro($9, $11); + $$ = node($5, $3, cs("e"), $9, cs(" "), + END_NODE); + } + | WHILE LPAR alloc_macro relational_expression RPAR + opt_statement pop_nesting + { + ssize_t n; + + if (st_has_continue) + n = node($6, cs("M"), $4, $3, END_NODE); + else + n = node($6, $4, $3, END_NODE); + emit_macro($3, n); + $$ = node($4, $3, cs(" "), END_NODE); + } + | LBRACE statement_list RBRACE + { + $$ = $2; + } + | PRINT print_expression_list + { + $$ = $2; + } + ; + +alloc_macro : /* empty */ + { + $$ = cs(str_table[macro_char]); + macro_char++; + /* Do not use [, \ and ] */ + if (macro_char == '[') + macro_char += 3; + /* skip letters */ + else if (macro_char == 'a') + macro_char = '{'; + else if (macro_char == ARRAY_CHAR) + macro_char += 26; + else if (macro_char == 255) + fatal("program too big"); + if (breaksp == BREAKSTACK_SZ) + fatal("nesting too deep"); + breakstack[breaksp++] = nesting++; + } + ; + +pop_nesting : /* empty */ + { + breaksp--; + } + ; + +function : function_header opt_parameter_list RPAR opt_newline + LBRACE NEWLINE opt_auto_define_list + statement_list RBRACE + { + int n = node(prologue, $8, epilogue, + cs("0"), numnode(nesting), + cs("Q"), END_NODE); + emit_macro($1, n); + reset_macro_char = macro_char; + nesting = 0; + breaksp = 0; + } + ; + +function_header : DEFINE LETTER LPAR + { + $$ = function_node($2); + free($2); + prologue = cs(""); + epilogue = cs(""); + nesting = 1; + breaksp = 0; + breakstack[breaksp] = 0; + } + ; + +opt_newline : /* empty */ + | NEWLINE + ; + +opt_parameter_list + : /* empty */ + | parameter_list + ; + + +parameter_list : LETTER + { + add_par(letter_node($1)); + free($1); + } + | LETTER LBRACKET RBRACKET + { + add_par(array_node($1)); + free($1); + } + | parameter_list COMMA LETTER + { + add_par(letter_node($3)); + free($3); + } + | parameter_list COMMA LETTER LBRACKET RBRACKET + { + add_par(array_node($3)); + free($3); + } + ; + + + +opt_auto_define_list + : /* empty */ + | AUTO define_list NEWLINE + | AUTO define_list SEMICOLON + ; + + +define_list : LETTER + { + add_local(letter_node($1)); + free($1); + } + | LETTER LBRACKET RBRACKET + { + add_local(array_node($1)); + free($1); + } + | define_list COMMA LETTER + { + add_local(letter_node($3)); + free($3); + } + | define_list COMMA LETTER LBRACKET RBRACKET + { + add_local(array_node($3)); + free($3); + } + ; + + +opt_argument_list + : /* empty */ + { + $$ = cs(""); + } + | argument_list + ; + + +argument_list : expression + | argument_list COMMA expression + { + $$ = node($1, $3, END_NODE); + } + | argument_list COMMA LETTER LBRACKET RBRACKET + { + $$ = node($1, cs("l"), array_node($3), + END_NODE); + free($3); + } + ; + +opt_relational_expression + : /* empty */ + { + $$ = cs(" 0 0="); + } + | relational_expression + ; + +relational_expression + : expression EQUALS expression + { + $$ = node($1, $3, cs("="), END_NODE); + } + | expression UNEQUALS expression + { + $$ = node($1, $3, cs("!="), END_NODE); + } + | expression LESS expression + { + $$ = node($1, $3, cs(">"), END_NODE); + } + | expression LESS_EQ expression + { + $$ = node($1, $3, cs("!<"), END_NODE); + } + | expression GREATER expression + { + $$ = node($1, $3, cs("<"), END_NODE); + } + | expression GREATER_EQ expression + { + $$ = node($1, $3, cs("!>"), END_NODE); + } + | expression + { + $$ = node($1, cs(" 0!="), END_NODE); + } + ; + + +return_expression + : /* empty */ + { + $$ = node(cs("0"), epilogue, + numnode(nesting), cs("Q"), END_NODE); + } + | expression + { + $$ = node($1, epilogue, + numnode(nesting), cs("Q"), END_NODE); + } + | LPAR RPAR + { + $$ = node(cs("0"), epilogue, + numnode(nesting), cs("Q"), END_NODE); + } + ; + + +opt_expression : /* empty */ + { + $$ = cs(" 0"); + } + | expression + ; + +expression : named_expression + { + $$ = node($1.load, END_NODE); + } + | DOT { + $$ = node(cs("l."), END_NODE); + } + | NUMBER + { + $$ = node(cs(" "), as($1), END_NODE); + } + | LPAR expression RPAR + { + $$ = $2; + } + | LETTER LPAR opt_argument_list RPAR + { + $$ = node($3, cs("l"), + function_node($1), cs("x"), + END_NODE); + free($1); + } + | MINUS expression %prec UMINUS + { + $$ = node(cs(" 0"), $2, cs("-"), + END_NODE); + } + | expression PLUS expression + { + $$ = node($1, $3, cs("+"), END_NODE); + } + | expression MINUS expression + { + $$ = node($1, $3, cs("-"), END_NODE); + } + | expression MULTIPLY expression + { + $$ = node($1, $3, cs("*"), END_NODE); + } + | expression DIVIDE expression + { + $$ = node($1, $3, cs("/"), END_NODE); + } + | expression REMAINDER expression + { + $$ = node($1, $3, cs("%"), END_NODE); + } + | expression EXPONENT expression + { + $$ = node($1, $3, cs("^"), END_NODE); + } + | INCR named_expression + { + $$ = node($2.load, cs("1+d"), $2.store, + END_NODE); + } + | DECR named_expression + { + $$ = node($2.load, cs("1-d"), + $2.store, END_NODE); + } + | named_expression INCR + { + $$ = node($1.load, cs("d1+"), + $1.store, END_NODE); + } + | named_expression DECR + { + $$ = node($1.load, cs("d1-"), + $1.store, END_NODE); + } + | named_expression ASSIGN_OP expression + { + if ($2[0] == '\0') + $$ = node($3, cs($2), cs("d"), $1.store, + END_NODE); + else + $$ = node($1.load, $3, cs($2), cs("d"), + $1.store, END_NODE); + } + | LENGTH LPAR expression RPAR + { + $$ = node($3, cs("Z"), END_NODE); + } + | SQRT LPAR expression RPAR + { + $$ = node($3, cs("v"), END_NODE); + } + | SCALE LPAR expression RPAR + { + $$ = node($3, cs("X"), END_NODE); + } + | BOOL_NOT expression + { + $$ = node($2, cs("N"), END_NODE); + } + | expression BOOL_AND alloc_macro pop_nesting expression + { + ssize_t n = node(cs("R"), $5, END_NODE); + emit_macro($3, n); + $$ = node($1, cs("d0!="), $3, END_NODE); + } + | expression BOOL_OR alloc_macro pop_nesting expression + { + ssize_t n = node(cs("R"), $5, END_NODE); + emit_macro($3, n); + $$ = node($1, cs("d0="), $3, END_NODE); + } + | expression EQUALS expression + { + $$ = node($1, $3, cs("G"), END_NODE); + } + | expression UNEQUALS expression + { + $$ = node($1, $3, cs("GN"), END_NODE); + } + | expression LESS expression + { + $$ = node($3, $1, cs("("), END_NODE); + } + | expression LESS_EQ expression + { + $$ = node($3, $1, cs("{"), END_NODE); + } + | expression GREATER expression + { + $$ = node($1, $3, cs("("), END_NODE); + } + | expression GREATER_EQ expression + { + $$ = node($1, $3, cs("{"), END_NODE); + } + ; + +named_expression + : LETTER + { + $$.load = node(cs("l"), letter_node($1), + END_NODE); + $$.store = node(cs("s"), letter_node($1), + END_NODE); + free($1); + } + | LETTER LBRACKET expression RBRACKET + { + $$.load = node($3, cs(";"), + array_node($1), END_NODE); + $$.store = node($3, cs(":"), + array_node($1), END_NODE); + free($1); + } + | SCALE + { + $$.load = cs("K"); + $$.store = cs("k"); + } + | IBASE + { + $$.load = cs("I"); + $$.store = cs("i"); + } + | OBASE + { + $$.load = cs("O"); + $$.store = cs("o"); + } + ; + +print_expression_list + : print_expression + | print_expression_list COMMA print_expression + { + $$ = node($1, $3, END_NODE); + } + +print_expression + : expression + { + $$ = node($1, cs("ds.n"), END_NODE); + } + | STRING + { + char *p = escape($1); + $$ = node(cs("["), as(p), cs("]n"), END_NODE); + free(p); + } +%% + + +static void +grow(void) +{ + struct tree *p; + size_t newsize; + + if (current == instr_sz) { + newsize = instr_sz * 2 + 1; + p = realloc(instructions, newsize * sizeof(*p)); + if (p == NULL) { + free(instructions); + err(1, NULL); + } + instructions = p; + instr_sz = newsize; + } +} + +static ssize_t +cs(const char *str) +{ + grow(); + instructions[current].index = CONST_STRING; + instructions[current].u.cstr = str; + return (current++); +} + +static ssize_t +as(const char *str) +{ + grow(); + instructions[current].index = ALLOC_STRING; + instructions[current].u.astr = strdup(str); + if (instructions[current].u.astr == NULL) + err(1, NULL); + return (current++); +} + +static ssize_t +node(ssize_t arg, ...) +{ + va_list ap; + ssize_t ret; + + va_start(ap, arg); + + ret = current; + grow(); + instructions[current++].index = arg; + + do { + arg = va_arg(ap, ssize_t); + grow(); + instructions[current++].index = arg; + } while (arg != END_NODE); + + va_end(ap); + return (ret); +} + +static void +emit(ssize_t i) +{ + if (instructions[i].index >= 0) + while (instructions[i].index != END_NODE) + emit(instructions[i++].index); + else + fputs(instructions[i].u.cstr, stdout); +} + +static void +emit_macro(int nodeidx, ssize_t code) +{ + putchar('['); + emit(code); + printf("]s%s\n", instructions[nodeidx].u.cstr); + nesting--; +} + +static void +free_tree(void) +{ + ssize_t i; + + for (i = 0; i < current; i++) + if (instructions[i].index == ALLOC_STRING) + free(instructions[i].u.astr); + current = 0; +} + +static ssize_t +numnode(int num) +{ + const char *p; + + if (num < 10) + p = str_table['0' + num]; + else if (num < 16) + p = str_table['A' - 10 + num]; + else + errx(1, "internal error: break num > 15"); + return (node(cs(" "), cs(p), END_NODE)); +} + + +static ssize_t +lookup(char * str, size_t len, char type) +{ + ENTRY entry, *found; + u_short num; + u_char *p; + + /* The scanner allocated an extra byte already */ + if (str[len-1] != type) { + str[len] = type; + str[len+1] = '\0'; + } + entry.key = str; + found = hsearch(entry, FIND); + if (found == NULL) { + if (var_count == MAX_VARIABLES) + errx(1, "too many variables"); + p = malloc(4); + if (p == NULL) + err(1, NULL); + num = var_count++; + p[0] = 255; + p[1] = ENCODE(num / VAR_BASE + 1); + p[2] = ENCODE(num % VAR_BASE + 1); + p[3] = '\0'; + + entry.data = (char *)p; + entry.key = strdup(str); + if (entry.key == NULL) + err(1, NULL); + found = hsearch(entry, ENTER); + if (found == NULL) + err(1, NULL); + } + return (cs(found->data)); +} + +static ssize_t +letter_node(char *str) +{ + size_t len; + + len = strlen(str); + if (len == 1 && str[0] != '_') + return (cs(str_table[(int)str[0]])); + else + return (lookup(str, len, 'L')); +} + +static ssize_t +array_node(char *str) +{ + size_t len; + + len = strlen(str); + if (len == 1 && str[0] != '_') + return (cs(str_table[(int)str[0] - 'a' + ARRAY_CHAR])); + else + return (lookup(str, len, 'A')); +} + +static ssize_t +function_node(char *str) +{ + size_t len; + + len = strlen(str); + if (len == 1 && str[0] != '_') + return (cs(str_table[(int)str[0] - 'a' + FUNC_CHAR])); + else + return (lookup(str, len, 'F')); +} + +static void +add_par(ssize_t n) +{ + prologue = node(cs("S"), n, prologue, END_NODE); + epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE); +} + +static void +add_local(ssize_t n) +{ + prologue = node(cs("0S"), n, prologue, END_NODE); + epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE); +} + +void +yyerror(const char *s) +{ + char *str, *p; + int n; + + if (yyin != NULL && feof(yyin)) + n = asprintf(&str, "%s: %s:%d: %s: unexpected EOF", + __progname, filename, lineno, s); + else if (isspace(yytext[0]) || !isprint(yytext[0])) + n = asprintf(&str, + "%s: %s:%d: %s: ascii char 0x%02x unexpected", + __progname, filename, lineno, s, yytext[0]); + else + n = asprintf(&str, "%s: %s:%d: %s: %s unexpected", + __progname, filename, lineno, s, yytext); + if (n == -1) + err(1, NULL); + + fputs("c[", stdout); + for (p = str; *p != '\0'; p++) { + if (*p == '[' || *p == ']' || *p =='\\') + putchar('\\'); + putchar(*p); + } + fputs("]pc\n", stdout); + free(str); +} + +void +fatal(const char *s) +{ + errx(1, "%s:%d: %s", filename, lineno, s); +} + +static void +warning(const char *s) +{ + warnx("%s:%d: %s", filename, lineno, s); +} + +static void +init(void) +{ + unsigned int i; + + for (i = 0; i < UCHAR_MAX; i++) { + str_table[i][0] = i; + str_table[i][1] = '\0'; + } + if (hcreate(1 << 16) == 0) + err(1, NULL); +} + + +static void +usage(void) +{ + fprintf(stderr, "usage: %s [-cdhlqv] [-e expression] [file ...]\n", + __progname); + exit(1); +} + +static char * +escape(const char *str) +{ + char *ret, *p; + + ret = malloc(strlen(str) + 1); + if (ret == NULL) + err(1, NULL); + + p = ret; + while (*str != '\0') { + /* + * We get _escaped_ strings here. Single backslashes are + * already converted to double backslashes + */ + if (*str == '\\') { + if (*++str == '\\') { + switch (*++str) { + case 'a': + *p++ = '\a'; + break; + case 'b': + *p++ = '\b'; + break; + case 'f': + *p++ = '\f'; + break; + case 'n': + *p++ = '\n'; + break; + case 'q': + *p++ = '"'; + break; + case 'r': + *p++ = '\r'; + break; + case 't': + *p++ = '\t'; + break; + case '\\': + *p++ = '\\'; + break; + } + str++; + } else { + *p++ = '\\'; + *p++ = *str++; + } + } else + *p++ = *str++; + } + *p = '\0'; + return (ret); +} + +/* ARGSUSED */ +void +sigchld(int signo) +{ + pid_t pid; + int status; + + switch (signo) { + default: + for (;;) { + pid = waitpid(dc, &status, WCONTINUED); + if (pid == -1) { + if (errno == EINTR) + continue; + _exit(0); + } + if (WIFEXITED(status) || WIFSIGNALED(status)) + _exit(0); + else + break; + } + } +} + +int +main(int argc, char *argv[]) +{ + int i, ch; + int p[2]; + char *q; + + init(); + setlinebuf(stdout); + + sargv = malloc(argc * sizeof(char *)); + if (sargv == NULL) + err(1, NULL); + + if ((cmdexpr = strdup("")) == NULL) + err(1, NULL); + /* The d debug option is 4.4 BSD bc(1) compatible */ + while ((ch = getopt_long(argc, argv, "cde:hlqv", + long_options, NULL)) != -1) { + switch (ch) { + case 'c': + case 'd': + do_fork = false; + break; + case 'e': + q = cmdexpr; + if (asprintf(&cmdexpr, "%s%s\n", cmdexpr, optarg) == -1) + err(1, NULL); + free(q); + break; + case 'h': + usage(); + break; + case 'l': + sargv[sargc++] = _PATH_LIBB; + break; + case 'q': + /* compatibility option */ + break; + case 'v': + fprintf(stderr, "%s (BSD bc) %s\n", __progname, BC_VER); + exit(0); + break; + default: + usage(); + } + } + + argc -= optind; + argv += optind; + + interactive = isatty(STDIN_FILENO); + for (i = 0; i < argc; i++) + sargv[sargc++] = argv[i]; + + if (do_fork) { + if (pipe(p) == -1) + err(1, "cannot create pipe"); + dc = fork(); + if (dc == -1) + err(1, "cannot fork"); + else if (dc != 0) { + signal(SIGCHLD, sigchld); + close(STDOUT_FILENO); + dup(p[1]); + close(p[0]); + close(p[1]); + } else { + close(STDIN_FILENO); + dup(p[0]); + close(p[0]); + close(p[1]); + execl(_PATH_DC, "dc", "-x", (char *)NULL); + err(1, "cannot find dc"); + } + } + yywrap(); + return (yyparse()); +} diff --git a/usr.bin/bc/extern.h b/usr.bin/bc/extern.h new file mode 100644 index 000000000000..a99c46c99faf --- /dev/null +++ b/usr.bin/bc/extern.h @@ -0,0 +1,38 @@ +/* $FreeBSD$ */ +/* $OpenBSD: extern.h,v 1.6 2006/03/18 20:44:43 otto Exp $ */ + +/* + * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stdio.h> + +struct lvalue { + ssize_t load; + ssize_t store; +}; + +int yylex(void); +void yyerror(const char *); +void fatal(const char *); +void abort_line(int); + +extern int lineno; +extern int fileindex; +extern int sargc; +extern const char **sargv; +extern const char *filename; +extern char *cmdexpr; +bool interactive; diff --git a/usr.bin/bc/pathnames.h b/usr.bin/bc/pathnames.h new file mode 100644 index 000000000000..defb766f7169 --- /dev/null +++ b/usr.bin/bc/pathnames.h @@ -0,0 +1,21 @@ +/* $FreeBSD$ */ +/* $OpenBSD: pathnames.h,v 1.1 2003/09/25 19:32:44 otto Exp $ */ + +/* + * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#define _PATH_LIBB "/usr/share/misc/bc.library" +#define _PATH_DC "/usr/bin/dc" diff --git a/usr.bin/bc/scan.l b/usr.bin/bc/scan.l new file mode 100644 index 000000000000..9c21fc20bf69 --- /dev/null +++ b/usr.bin/bc/scan.l @@ -0,0 +1,288 @@ +%{ +/* $OpenBSD: scan.l,v 1.23 2009/10/27 23:59:36 deraadt Exp $ */ + +/* + * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <err.h> +#include <errno.h> +#include <signal.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> + +#include "extern.h" +#include "bc.h" +#include "pathnames.h" + +int lineno; + +static char *strbuf = NULL; +static size_t strbuf_sz = 1; +static bool dot_seen; + +static void init_strbuf(void); +static void add_str(const char *); + +%} + +%option always-interactive + +DIGIT [0-9A-F] +ALPHA [a-z_] +ALPHANUM [a-z_0-9] + +%x comment string number + +%% + +"/*" BEGIN(comment); +<comment>{ + "*/" BEGIN(INITIAL); + \n lineno++; + \* ; + [^*\n]+ ; + <<EOF>> fatal("end of file in comment"); +} + +\" BEGIN(string); init_strbuf(); +<string>{ + [^"\n\\\[\]]+ add_str(yytext); + \[ add_str("\\["); + \] add_str("\\]"); + \\ add_str("\\\\"); + \n add_str("\n"); lineno++; + \" BEGIN(INITIAL); yylval.str = strbuf; return STRING; + <<EOF>> fatal("end of file in string"); +} + +{DIGIT}+ { + BEGIN(number); + dot_seen = false; + init_strbuf(); + add_str(yytext); + } +\. { + BEGIN(number); + dot_seen = true; + init_strbuf(); + add_str("."); + } +<number>{ + {DIGIT}+ add_str(yytext); + \. { + if (dot_seen) { + BEGIN(INITIAL); + yylval.str = strbuf; + unput('.'); + return (NUMBER); + } else { + dot_seen = true; + add_str("."); + } + } + \\\n[ \t]* lineno++; + [^0-9A-F\.] { + BEGIN(INITIAL); + unput(yytext[0]); + if (strcmp(strbuf, ".") == 0) + return (DOT); + else { + yylval.str = strbuf; + return (NUMBER); + } + } +} + +"auto" return (AUTO); +"break" return (BREAK); +"continue" return (CONTINUE); +"define" return (DEFINE); +"else" return (ELSE); +"ibase" return (IBASE); +"if" return (IF); +"last" return (DOT); +"for" return (FOR); +"length" return (LENGTH); +"obase" return (OBASE); +"print" return (PRINT); +"quit" return (QUIT); +"return" return (RETURN); +"scale" return (SCALE); +"sqrt" return (SQRT); +"while" return (WHILE); + +"^" return (EXPONENT); +"*" return (MULTIPLY); +"/" return (DIVIDE); +"%" return (REMAINDER); + +"!" return (BOOL_NOT); +"&&" return (BOOL_AND); +"||" return (BOOL_OR); + +"+" return (PLUS); +"-" return (MINUS); + +"++" return (INCR); +"--" return (DECR); + +"=" yylval.str = ""; return (ASSIGN_OP); +"+=" yylval.str = "+"; return (ASSIGN_OP); +"-=" yylval.str = "-"; return (ASSIGN_OP); +"*=" yylval.str = "*"; return (ASSIGN_OP); +"/=" yylval.str = "/"; return (ASSIGN_OP); +"%=" yylval.str = "%"; return (ASSIGN_OP); +"^=" yylval.str = "^"; return (ASSIGN_OP); + +"==" return (EQUALS); +"<=" return (LESS_EQ); +">=" return (GREATER_EQ); +"!=" return (UNEQUALS); +"<" return (LESS); +">" return (GREATER); + +"," return (COMMA); +";" return (SEMICOLON); + +"(" return (LPAR); +")" return (RPAR); + +"[" return (LBRACKET); +"]" return (RBRACKET); + +"{" return (LBRACE); +"}" return (RBRACE); + +{ALPHA}{ALPHANUM}* { + /* alloc an extra byte for the type marker */ + char *p = malloc(yyleng + 2); + if (p == NULL) + err(1, NULL); + strlcpy(p, yytext, yyleng + 1); + yylval.astr = p; + return (LETTER); + } + +\\\n lineno++; +\n lineno++; return (NEWLINE); + +#[^\n]* ; +[ \t] ; +<<EOF>> return (QUIT); +. yyerror("illegal character"); + +%% + +static void +init_strbuf(void) +{ + if (strbuf == NULL) { + strbuf = malloc(strbuf_sz); + if (strbuf == NULL) + err(1, NULL); + } + strbuf[0] = '\0'; +} + +static void +add_str(const char *str) +{ + size_t arglen; + + arglen = strlen(str); + + if (strlen(strbuf) + arglen + 1 > strbuf_sz) { + size_t newsize; + char *p; + + newsize = strbuf_sz + arglen + 1; + p = realloc(strbuf, newsize); + if (p == NULL) { + free(strbuf); + err(1, NULL); + } + strbuf_sz = newsize; + strbuf = p; + } + strlcat(strbuf, str, strbuf_sz); +} + +/* ARGSUSED */ +void +abort_line(int sig) +{ + static const char str[] = "[\n]P\n"; + int save_errno; + + switch (sig) { + default: + save_errno = errno; + YY_FLUSH_BUFFER; /* XXX signal race? */ + write(STDOUT_FILENO, str, sizeof(str) - 1); + errno = save_errno; + } +} + +int +yywrap(void) +{ + static int state; + static YY_BUFFER_STATE buf; + + if (fileindex == 0 && sargc > 0 && strcmp(sargv[0], _PATH_LIBB) == 0) { + filename = sargv[fileindex++]; + yyin = fopen(filename, "r"); + lineno = 1; + if (yyin == NULL) + err(1, "cannot open %s", filename); + return (0); + } + if (state == 0 && cmdexpr[0] != '\0') { + buf = yy_scan_string(cmdexpr); + state++; + lineno = 1; + filename = "command line"; + return (0); + } else if (state == 1) { + yy_delete_buffer(buf); + free(cmdexpr); + state++; + } + if (yyin != NULL && yyin != stdin) + fclose(yyin); + if (fileindex < sargc) { + filename = sargv[fileindex++]; + yyin = fopen(filename, "r"); + lineno = 1; + if (yyin == NULL) + err(1, "cannot open %s", filename); + return (0); + } else if (fileindex == sargc) { + fileindex++; + yyin = stdin; + if (interactive) + signal(SIGINT, abort_line); + lineno = 1; + filename = "stdin"; + return (0); + } + return (1); +} + |
