diff options
-rw-r--r-- | contrib/bc/bc/main.c | 90 | ||||
-rw-r--r-- | contrib/bc/bc/scan.l | 114 | ||||
-rw-r--r-- | contrib/bc/doc/bc.1 | 116 | ||||
-rw-r--r-- | contrib/bc/doc/dc.1 | 55 |
4 files changed, 263 insertions, 112 deletions
diff --git a/contrib/bc/bc/main.c b/contrib/bc/bc/main.c index bd4efcb..71b46a6 100644 --- a/contrib/bc/bc/main.c +++ b/contrib/bc/bc/main.c @@ -1,7 +1,7 @@ /* main.c: The main program for bc. */ /* This file is part of GNU bc. - Copyright (C) 1991, 1992, 1993, 1994, 1997, 1998 Free Software Foundation, Inc. + Copyright (C) 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,10 +15,12 @@ You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to - the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + The Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111 USA You may contact the author by: - e-mail: phil@cs.wwu.edu + e-mail: philnelson@acm.org us-mail: Philip A. Nelson Computer Science Department, 9062 Western Washington University @@ -36,22 +38,17 @@ $FreeBSD$ /* Variables for processing multiple files. */ -char first_file; -extern FILE *yyin; +static char first_file; /* Points to the last node in the file name list for easy adding. */ static file_node *last = NULL; -#ifdef READLINE -/* Readline support. */ -extern char *rl_readline_name; -extern FILE *rl_instream; -#endif - /* long option support */ static struct option long_options[] = { {"compile", 0, &compile_only, TRUE}, + {"help", 0, 0, 'h'}, + {"interactive", 0, 0, 'i'}, {"mathlib", 0, &use_math, TRUE}, {"quiet", 0, &quiet, TRUE}, {"standard", 0, &std_only, TRUE}, @@ -63,6 +60,20 @@ static struct option long_options[] = void +usage (char *progname) +{ + printf ("usage: %s [options] [file ...]\n%s%s%s%s%s%s%s", progname, + " -h --help print this usage and exit\n", + " -i --interactive force interactive mode\n", + " -l --mathlib use the predefine math routnes\n", + " -q --quiet don't print initial banner\n", + " -s --standard non-standard bc constructs are errors\n", + " -w --warn warn about non-standard bc constructs\n", + " -v --version print version information and exit\n"); +} + + +void parse_args (argc, argv) int argc; char **argv; @@ -77,7 +88,7 @@ parse_args (argc, argv) /* Parse the command line */ while (1) { - optch = getopt_long (argc, argv, "lciqsvw", long_options, &long_index); + optch = getopt_long (argc, argv, "chilqswv", long_options, &long_index); if (optch == EOF) /* End of arguments. */ break; @@ -88,14 +99,19 @@ parse_args (argc, argv) compile_only = TRUE; break; - case 'l': /* math lib */ - use_math = TRUE; + case 'h': /* help */ + usage(argv[0]); + exit (0); break; case 'i': /* force interactive */ interactive = TRUE; break; + case 'l': /* math lib */ + use_math = TRUE; + break; + case 'q': /* quiet mode */ quiet = TRUE; break; @@ -105,13 +121,17 @@ parse_args (argc, argv) break; case 'v': /* Print the version. */ - printf ("%s\n", BC_VERSION); + show_bc_version (); exit (0); break; case 'w': /* Non standard features give warnings. */ warn_not_std = TRUE; break; + + default: + usage(argv[0]); + exit (1); } } @@ -215,7 +235,20 @@ main (argc, argv) if (!open_new_file ()) exit (1); -#ifdef READLINE +#if defined(LIBEDIT) + if (interactive) { + /* Enable libedit support. */ + edit = el_init ("bc", stdin, stdout, stderr); + hist = history_init(); + el_set (edit, EL_EDITOR, "emacs"); + el_set (edit, EL_HIST, history, hist); + el_set (edit, EL_PROMPT, null_prompt); + el_source (edit, NULL); + history (hist, &histev, H_SETSIZE, INT_MAX); + } +#endif + +#if defined(READLINE) if (interactive) { /* Readline support. Set both application name and input file. */ rl_readline_name = "bc"; @@ -254,23 +287,9 @@ open_new_file () /* Open the other files. */ if (use_math && first_file) { -#ifdef BC_MATH_FILE - /* Make the first file be the math library. */ - new_file = fopen (BC_MATH_FILE, "r"); - use_math = FALSE; - if (new_file != NULL) - { - new_yy_file (new_file); - return TRUE; - } - else - { - fprintf (stderr, "Math Library unavailable.\n"); - exit (1); - } -#else /* Load the code from a precompiled version of the math libarary. */ - extern char libmath[]; + extern char *libmath[]; + char **mstr; char tmp; /* These MUST be in the order of first mention of each function. That is why "a" comes before "c" even though "a" is defined after @@ -281,8 +300,11 @@ open_new_file () tmp = lookup ("a", FUNCT); tmp = lookup ("c", FUNCT); tmp = lookup ("j", FUNCT); - load_code (libmath); -#endif + mstr = libmath; + while (*mstr) { + load_code (*mstr); + mstr++; + } } /* One of the argv values. */ diff --git a/contrib/bc/bc/scan.l b/contrib/bc/bc/scan.l index 78a145b..d2e19c6 100644 --- a/contrib/bc/bc/scan.l +++ b/contrib/bc/bc/scan.l @@ -1,4 +1,5 @@ %{ +/* $FreeBSD$ */ /* scan.l: the (f)lex description file for the scanner. */ /* This file is part of GNU bc. @@ -16,10 +17,12 @@ You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to - the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + The Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111 USA You may contact the author by: - e-mail: phil@cs.wwu.edu + e-mail: philnelson@acm.org us-mail: Philip A. Nelson Computer Science Department, 9062 Western Washington University @@ -48,6 +51,83 @@ #undef yywrap _PROTOTYPE(int yywrap, (void)); +#if defined(LIBEDIT) +/* Support for the BSD libedit with history for + nicer input on the interactive part of input. */ + +#include <histedit.h> + +/* Have input call the following function. */ +#undef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + bcel_input((char *)buf, &result, max_size) + +/* Variables to help interface editline with bc. */ +static const char *bcel_line = (char *)NULL; +static int bcel_len = 0; + + +/* Required to get rid of that ugly ? default prompt! */ +char * +null_prompt (EditLine *el) +{ + return ""; +} + + +/* bcel_input puts upto MAX characters into BUF with the number put in + BUF placed in *RESULT. If the yy input file is the same as + stdin, use editline. Otherwise, just read it. +*/ + +static void +bcel_input (buf, result, max) + char *buf; + int *result; + int max; +{ + if (!edit || yyin != stdin) + { + while ( (*result = read( fileno(yyin), buf, max )) < 0 ) + if (errno != EINTR) + { + yyerror( "read() in flex scanner failed" ); + exit (1); + } + return; + } + + /* Do we need a new string? */ + if (bcel_len == 0) + { + bcel_line = el_gets(edit, &bcel_len); + if (bcel_line == NULL) { + /* end of file */ + *result = 0; + bcel_len = 0; + return; + } + if (bcel_len != 0) + history (hist, &histev, H_ENTER, bcel_line); + fflush (stdout); + } + + if (bcel_len <= max) + { + strncpy (buf, bcel_line, bcel_len); + *result = bcel_len; + bcel_len = 0; + } + else + { + strncpy (buf, bcel_line, max); + *result = max; + bcel_line += max; + bcel_len -= max; + } +} +#endif + #ifdef READLINE /* Support for the readline and history libraries. This allows nicer input on the interactive part of input. */ @@ -66,9 +146,6 @@ static int rl_len = 0; extern FILE *rl_instream; _PROTOTYPE(char *readline, (char *)); -/* Needed here? */ -extern FILE *yyin; - /* rl_input puts upto MAX characters into BUF with the number put in BUF placed in *RESULT. If the yy input file is the same as rl_instream (stdin), use readline. Otherwise, just read it. @@ -94,20 +171,20 @@ rl_input (buf, result, max) /* Do we need a new string? */ if (rl_len == 0) { - if (rl_line) - free(rl_line); - rl_line = readline (""); - if (rl_line == NULL) { + if (rl_start) + free(rl_start); + rl_start = readline (""); + if (rl_start == NULL) { /* end of file */ *result = 0; rl_len = 0; return; } + rl_line = rl_start; rl_len = strlen (rl_line)+1; if (rl_len != 1) add_history (rl_line); rl_line[rl_len-1] = '\n'; - printf ("\r"); fflush (stdout); } @@ -121,10 +198,14 @@ rl_input (buf, result, max) { strncpy (buf, rl_line, max); *result = max; + rl_line += max; rl_len -= max; } } -#else +#endif + +#if !defined(READLINE) && !defined(LIBEDIT) + /* MINIX returns from read with < 0 if SIGINT is encountered. In flex, we can redefine YY_INPUT to the following. In lex, this does nothing! */ @@ -134,6 +215,7 @@ rl_input (buf, result, max) if (errno != EINTR) \ YY_FATAL_ERROR( "read() in flex scanner failed" ); #endif + %} DIGIT [0-9A-F] LETTER [a-z] @@ -146,7 +228,7 @@ LETTER [a-z] yyerror ("illegal character: #"); } <slcomment>[^\n]* { BEGIN(INITIAL); } -<slcomment>"\n" { line_no++; BEGIN(INITIAL); return(NEWLINE); } +<slcomment>"\n" { line_no++; BEGIN(INITIAL); return(ENDOFLINE); } define return(Define); break return(Break); quit return(Quit); @@ -165,8 +247,8 @@ read return(Read); halt return(Halt); last return(Last); history { -#ifdef READLINE - return(History); +#if defined(READLINE) || defined(LIBEDIT) + return(HistoryVar); #else yylval.s_value = strcopyof(yytext); return(NAME); #endif @@ -206,7 +288,7 @@ limits return(Limits); } ==|\<=|\>=|\!=|"<"|">" { yylval.s_value = strcopyof(yytext); return(REL_OP); } \+\+|-- { yylval.c_value = yytext[0]; return(INCR_DECR); } -"\n" { line_no++; return(NEWLINE); } +"\n" { line_no++; return(ENDOFLINE); } \\\n { line_no++; /* ignore a "quoted" newline */ } [ \t]+ { /* ignore spaces and tabs */ } "/*" { @@ -275,7 +357,7 @@ limits return(Limits); yyerror ("illegal character: ^%c",yytext[0] + '@'); else if (yytext[0] > '~') - yyerror ("illegal character: \\%3d", (int) yytext[0]); + yyerror ("illegal character: \\%03o", (int) yytext[0]); else yyerror ("illegal character: %s",yytext); } diff --git a/contrib/bc/doc/bc.1 b/contrib/bc/doc/bc.1 index b534c5f..908462c 100644 --- a/contrib/bc/doc/bc.1 +++ b/contrib/bc/doc/bc.1 @@ -2,7 +2,7 @@ .\" bc.1 - the *roff document processor source for the bc manual .\" .\" This file is part of GNU bc. -.\" Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc. +.\" Copyright (C) 1991-1994, 1997, 2000 Free Software Foundation, Inc. .\" .\" This program is free software; you can redistribute it and/or modify .\" it under the terms of the GNU General Public License as published by @@ -15,24 +15,27 @@ .\" GNU General Public License for more details. .\" .\" You should have received a copy of the GNU General Public License -.\" along with this program; see the file COPYING. If not, write to -.\" the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. +.\" along with this program; see the file COPYING. If not, write to: +.\" The Free Software Foundation, Inc. +.\" 59 Temple Place, Suite 330 +.\" Boston, MA 02111 USA .\" .\" You may contact the author by: -.\" e-mail: phil@cs.wwu.edu +.\" e-mail: philnelson@acm.org .\" us-mail: Philip A. Nelson .\" Computer Science Department, 9062 .\" Western Washington University .\" Bellingham, WA 98226-9062 .\" +.\" $FreeBSD$ .\" -.TH bc 1 .\" "Command Manual" v1.04 "June 22, 1995" +.TH bc 1 .\" "Command Manual" v1.06 "Sept 12, 2000" .SH NAME bc - An arbitrary precision calculator language .SH SYNTAX -\fBbc\fR [ \fB-lwsqv\fR ] [long-options] [ \fI file ...\fR ] +\fBbc\fR [ \fB-hlwsqv\fR ] [long-options] [ \fI file ...\fR ] .SH VERSION -This man page documents GNU bc version 1.04. +This man page documents GNU bc version 1.06. .SH DESCRIPTION \fBbc\fR is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities @@ -52,25 +55,19 @@ or to be rejected. This document describes the language accepted by this processor. Extensions will be identified as such. .SS OPTIONS -.IP -l +.IP "-h, --help" +Print the usage and exit. +.IP "-i, --interactive" +Force interactive mode. +.IP "-l, --mathlib" Define the standard math library. -.IP -w +.IP "-w, --warn" Give warnings for extensions to POSIX \fBbc\fR. -.IP -s +.IP "-s, --standard" Process exactly the POSIX \fBbc\fR language. -.IP -q +.IP "-q, --quiet" Do not print the normal GNU bc welcome. -.IP -v -Print the version number and copyright and quit. -.IP --mathlib -Define the standard math library. -.IP --warn -Give warnings for extensions to POSIX \fBbc\fR. -.IP --standard -Process exactly the POSIX \fBbc\fR language. -.IP --quiet -Do not print the normal GNU bc welcome. -.IP --version +.IP "-v, --version" Print the version number and copyright and quit. .SS NUMBERS The most basic element in \fBbc\fR is the number. Numbers are @@ -328,8 +325,8 @@ base ten value of "obase-1". Since numbers are of arbitrary precision, some numbers may not be printable on a single output line. These long numbers will be split across lines using the "\e" as the last character on a line. The maximum number of characters printed -per line is 70. Due to the interactive nature of \fBbc\fR printing -a number cause the side effect of assigning the printed value to the +per line is 70. Due to the interactive nature of \fBbc\fR, printing +a number causes the side effect of assigning the printed value to the special variable \fBlast\fR. This allows the user to recover the last value printed without having to retype the expression that printed the number. Assigning to \fBlast\fR is legal and will @@ -405,7 +402,7 @@ not executed. Return the value 0 from a function. (See the section on functions.) .IP "\fBreturn\fR ( expression )" Return the value of the expression from a function. (See the section on -functions.) +functions.) As an extension, the parenthesis are not required. .SS PSEUDO STATEMENTS These statements are not statements in the traditional sense. They are not executed statements. Their function is performed at "compile" time. @@ -460,7 +457,7 @@ popped so that the original value (at the time of the function call) of these variables are restored. The parameters are really auto variables that are initialized to a value provided in the function call. Auto variables are different than traditional local variables -in the fact that if function A calls function B, B may access function +because if function A calls function B, B may access function A's auto variables by just using the same name, unless function B has called them auto variables. Due to the fact that auto variables and parameters are pushed onto a stack, \fBbc\fR supports recursive functions. @@ -481,6 +478,22 @@ constants in the function body will be converted using the value of will be ignored during the execution of the function except for the standard function \fBread\fR, which will always use the current value of \fBibase\fR for conversion of numbers. +.PP +As an extension, the format of the definition has been slightly relaxed. +The standard requires the opening brace be on the same line as the +\fBdefine\fR keyword and all other parts must be on following lines. +This version of \fBbc\fR will allow any number of newlines before and +after the opening brace of the function. For example, the following +definitions are legal. +.nf +.RS +\f(CW +define d (n) { return (2*n); } +define d (n) + { return (2*n); } +\fR +.RE +.fi .SS MATH LIBRARY If \fBbc\fR is invoked with the \fB-l\fR option, a math library is preloaded and the default scale is set to 20. The math functions will calculate their @@ -594,19 +607,21 @@ define f (x) { \fR .RE .fi -.SS READLINE OPTION -GNU \fBbc\fR can be compiled (via a configure option) to use the -GNU \fBreadline\fR input editor library. This allows the user -to do more editing of lines before sending them to \fBbc\fR. -It also allows for a history of previous lines typed. When this -option is selected, \fBbc\fR has one more special variable. -This special variable, \fBhistory\fR is the number of lines of -history retained. A value of -1 means that an unlimited number -of history lines are retained. This is the default value. -Setting the value of \fBhistory\fR to a positive number restricts -the number of history lines to the number given. The value of -0 disables the history feature. For more information, read the -user manuals for the GNU \fBreadline\fR and \fBhistory\fR libraries. +.SS READLINE AND LIBEDIT OPTIONS +GNU \fBbc\fR can be compiled (via a configure option) to use the GNU +\fBreadline\fR input editor library or the BSD \fBlibedit\fR library. +This allows the user to do editing of lines before sending them +to \fBbc\fR. It also allows for a history of previous lines typed. +When this option is selected, \fBbc\fR has one more special variable. +This special variable, \fBhistory\fR is the number of lines of history +retained. For \fBreadline\fR, a value of -1 means that an unlimited +number of history lines are retained. Setting the value of +\fBhistory\fR to a positive number restricts the number of history +lines to the number given. The value of 0 disables the history +feature. The default value is 100. For more information, read the +user manuals for the GNU \fBreadline\fR, \fBhistory\fR and BSD \fBlibedit\fR +libraries. One can not enable both \fBreadline\fR and \fBlibedit\fR +at the same time. .SS DIFFERENCES This version of .B bc @@ -657,12 +672,17 @@ POSIX \fBbc\fR does not have a read function. POSIX \fBbc\fR does not have a print statement . .IP "continue statement" POSIX \fBbc\fR does not have a continue statement. +.IP "return statement" +POSIX \fBbc\fR requires parentheses around the return expression. .IP "array parameters" POSIX \fBbc\fR does not (currently) support array parameters in full. The POSIX grammar allows for arrays in function definitions, but does not provide a method to specify an array as an actual parameter. (This is most likely an oversight in the grammar.) Traditional implementations of \fBbc\fR have only call by value array parameters. +.IP "function format" +POSIX \fBbc\fR requires the opening brace on the same line as the +\fBdefine\fR key word and the \fBauto\fR statement on the next line. .IP "=+, =-, =*, =/, =%, =^" POSIX \fBbc\fR does not require these "old style" assignment operators to be defined. This version may allow these "old style" assignments. Use @@ -732,14 +752,6 @@ digits. The limit on the number of characters in a string is INT_MAX characters. .IP exponent The value of the exponent in the raise operation (^) is limited to LONG_MAX. -.IP multiply -The multiply routine may yield incorrect results if a number -has more than LONG_MAX / 90 total digits. For 32 bit longs, this number is -23,860,929 digits. -.IP "code size" -Each function and the "main" program are limited to 16384 bytes of -compiled byte code each. This limit (BC_MAX_SEGS) can be easily changed -to have more than 16 segments of 1024 bytes. .IP "variable names" The current limit on the number of unique names is 32767 for each of simple variables, arrays and functions. @@ -760,12 +772,6 @@ the user wants defined every time \fBbc\fR is run. This should be an integer specifying the number of characters in an output line for numbers. This includes the backslash and newline characters for long numbers. -.SH FILES -In most installations, \fBbc\fR is completely self-contained. -Where executable size is of importance or the C compiler does -not deal with very long strings, \fBbc\fR will read -the standard math library from the file /usr/local/lib/libmath.b. -(The actual location may vary. It may be /lib/libmath.b.) .SH DIAGNOSTICS If any file on the command line can not be opened, \fBbc\fR will report that the file is unavailable and terminate. Also, there are compile @@ -774,12 +780,12 @@ and run time diagnostics that should be self-explanatory. Error recovery is not very good yet. .PP Email bug reports to -.BR bug-gnu-utils@prep.ai.mit.edu . +.BR bug-bc@gnu.org . Be sure to include the word ``bc'' somewhere in the ``Subject:'' field. .SH AUTHOR .nf Philip A. Nelson -phil@cs.wwu.edu +philnelson@acm.org .fi .SH ACKNOWLEDGEMENTS The author would like to thank Steve Sommars (Steve.Sommars@att.com) for diff --git a/contrib/bc/doc/dc.1 b/contrib/bc/doc/dc.1 index 856b6bb..adaf5d0 100644 --- a/contrib/bc/doc/dc.1 +++ b/contrib/bc/doc/dc.1 @@ -2,7 +2,7 @@ .\" dc.1 - the *roff document processor source for the dc manual .\" .\" This file is part of GNU dc. -.\" Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc. +.\" Copyright (C) 1994, 1997, 1998, 2000 Free Software Foundation, Inc. .\" .\" This program is free software; you can redistribute it and/or modify .\" it under the terms of the GNU General Public License as published by @@ -15,8 +15,10 @@ .\" GNU General Public License for more details. .\" .\" You should have received a copy of the GNU General Public License -.\" along with this program; see the file COPYING. If not, write to -.\" the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. +.\" along with this program; see the file COPYING. If not, write to: +.\" The Free Software Foundation, Inc. +.\" 59 Temple Place, Suite 330 +.\" Boston, MA 02111 USA .\" .\" $FreeBSD$ .\" @@ -26,7 +28,10 @@ .SH NAME dc \- an arbitrary precision calculator .SH SYNOPSIS -dc +dc [-V] [--version] [-h] [--help] + [-e scriptexpression] [--expression=scriptexpression] + [-f scriptfile] [--file=scriptfile] + [file ...] .SH DESCRIPTION .PP \*(Dc is a reverse-polish desk calculator which supports @@ -54,6 +59,43 @@ as it is a binary operator for subtraction instead. To enter two numbers in succession, separate them with spaces or newlines. These have no meaning as commands. +.SH OPTIONS +\*(Dc may be invoked with the following command-line options: +.TP +.B -V +.TP +.B --version +Print out the version of \*(dc that is being run and a copyright notice, +then exit. +.TP +.B -h +.TP +.B --help +Print a usage message briefly summarizing these command-line options +and the bug-reporting address, +then exit. +.TP +.B -e \fIscript\fP +.TP +.BI --expression= script +Add the commands in +.I script +to the set of commands to be run while processing the input. +.TP +.B -f \fIscript-file\fP +.TP +.BI --file= script-file +Add the commands contained in the file +.I script-file +to the set of commands to be run while processing the input. +.PP +If any command-line parameters remain after processing the above, +these parameters are interpreted as the names of input files to +be processed. +A file name of +.B - +refers to the standard input stream. +The standard input will processed if no file names are specified. .PD .SH Printing Commands @@ -417,7 +459,7 @@ Miscellaneous .TP .B ! Will run the rest of the line as a system command. -Note that parsing of the !<, !=, and !> commands take precidence, +Note that parsing of the !<, !=, and !> commands take precedence, so if you want to run a command starting with <, =, or > you will need to add a space after the !. .TP @@ -445,5 +487,4 @@ was later popped. BUGS .PP Email bug reports to -.BR bug-gnu-utils@prep.ai.mit.edu . -Be sure to include the word ``dc'' somewhere in the ``Subject:'' field. +.BR bug-dc@gnu.org . |