Skip to content

Commit

Permalink
Complete import of bc-1.04 by updating dc.
Browse files Browse the repository at this point in the history
  • Loading branch information
phil committed Apr 29, 1997
1 parent 7a2e502 commit 9b0d90e
Show file tree
Hide file tree
Showing 12 changed files with 1,484 additions and 131 deletions.
5 changes: 3 additions & 2 deletions gnu/usr.bin/dc/Makefile
@@ -1,9 +1,10 @@
# $Id: Makefile,v 1.1.1.1 1994/12/01 04:26:47 phil Exp $
# $NetBSD: Makefile,v 1.1.1.2 1997/04/29 00:34:24 phil Exp $

PROG= dc
CFLAGS+=-D_POSIX_SOURCE -I${.CURDIR} -I${.CURDIR}/../bc

SRCS= array.c dc-number.c number.c eval.c misc.c stack.c string.c
SRCS= array.c dc.c eval.c misc.c numeric.c stack.c string.c \
number.c getopt.c getopt1.c

.PATH: ${.CURDIR}/../bc

Expand Down
18 changes: 10 additions & 8 deletions gnu/usr.bin/dc/README.NetBSD
@@ -1,11 +1,13 @@
This is GNU dc. (Taken from the GNU bc 1.03 distribution.)
This is GNU bc 1.04.

The full distribution of GNU bc-1.04 may be found at any GNU software
archive. The master copy is at ftp://prep.ai.mit.edu/pub/gnu/bc-1.04.tar.gz.

Not everything included in the full bc-1.04 distribution is included.
The files included here are only the needed files for building bc on
NetBSD. Their location in the NetBSD tree are different than in the
original bc-1.04 source tree.


ALL other files distributed with GNU dc are available in some form in
/usr/src/gnu/usr.bin/bc. (aka ../bc)

The files array.c, eval.c, misc.c, stack.c and string.c were renamed from
their original distribution names as dc-xxxx.c.

An original distribution of GNU bc/dc can be regenerated from the files
in the directories bc and dc. (It will require GNU autoconf 2.0 to
regenerate some files.)
9 changes: 8 additions & 1 deletion gnu/usr.bin/dc/dc-proto.h
@@ -1,7 +1,7 @@
/*
* prototypes of all externally visible dc functions
*
* Copyright (C) 1994 Free Software Foundation, Inc.
* Copyright (C) 1994, 1997 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
Expand All @@ -26,6 +26,10 @@ extern void *dc_malloc DC_PROTO((size_t));
extern void dc_array_set DC_PROTO((int, int, dc_data));
extern void dc_array_init DC_PROTO((void));
extern void dc_binop DC_PROTO((int (*)(dc_num, dc_num, int, dc_num *), int));
extern void dc_binop2 DC_PROTO((int (*)(dc_num, dc_num, int,
dc_num *, dc_num *), int));
extern void dc_triop DC_PROTO((int (*)(dc_num, dc_num, dc_num, int,
dc_num *), int));
extern void dc_clear_stack DC_PROTO((void));
extern void dc_free_num DC_PROTO((dc_num *));
extern void dc_free_str DC_PROTO((dc_str *));
Expand All @@ -46,6 +50,7 @@ extern void dc_string_init DC_PROTO((void));
extern int dc_cmpop DC_PROTO((void));
extern int dc_compare DC_PROTO((dc_num, dc_num));
extern int dc_evalfile DC_PROTO((FILE *));
extern int dc_evalstr DC_PROTO((dc_data));
extern int dc_num2int DC_PROTO((dc_num, dc_boolean));
extern int dc_numlen DC_PROTO((dc_num));
extern int dc_pop DC_PROTO((dc_data *));
Expand All @@ -69,7 +74,9 @@ extern dc_data dc_readstring DC_PROTO((FILE *, int , int));

extern int dc_add DC_PROTO((dc_num, dc_num, int, dc_num *));
extern int dc_div DC_PROTO((dc_num, dc_num, int, dc_num *));
extern int dc_divrem DC_PROTO((dc_num, dc_num, int, dc_num *, dc_num *));
extern int dc_exp DC_PROTO((dc_num, dc_num, int, dc_num *));
extern int dc_modexp DC_PROTO((dc_num, dc_num, dc_num, int, dc_num *));
extern int dc_mul DC_PROTO((dc_num, dc_num, int, dc_num *));
extern int dc_rem DC_PROTO((dc_num, dc_num, int, dc_num *));
extern int dc_sub DC_PROTO((dc_num, dc_num, int, dc_num *));
Expand Down
68 changes: 57 additions & 11 deletions gnu/usr.bin/dc/dc.1
@@ -1,14 +1,31 @@
.TH DC 1 "07 Apr 1994" "GNU Project"
.\"
.\" dc.1 - the *roff document processor source for the dc manual
.\"
.\" This file is part of GNU dc.
.\" Copyright (C) 1994, 1997 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
.\" the Free Software Foundation; either version 2 of the License , or
.\" (at your option) any later version.
.\"
.\" This program is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" 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.
.\"
.TH DC 1 "1997-03-25" "GNU Project"
.ds dc \fIdc\fP
.ds Dc \fIDc\fP
.SH
NAME
.SH NAME
dc \- an arbitrary precision calculator
.SH
SYNOPSIS
.SH SYNOPSIS
dc
.SH
DESCRIPTION
.SH DESCRIPTION
.PP
\*(Dc is a reverse-polish desk calculator which supports
unlimited precision arithmetic.
Expand Down Expand Up @@ -94,6 +111,15 @@ The division is done with as many fraction digits
as the precision value specifies,
and the remainder is also computed with that many fraction digits.
.TP
.B ~
Pops two values,
divides the second one popped from the first one popped.
The quotient is pushed first, and the remainder is pushed next.
The number of fraction digits used in the division
is specified by the precision value.
(The sequence \fBSdSn lnld/ LnLd%\fP could also accomplish
this function, with slightly different error checking.)
.TP
.B ^
Pops two values and exponentiates,
using the first value popped as the exponent
Expand All @@ -102,6 +128,20 @@ The fraction part of the exponent is ignored.
The precision value specifies the number of fraction
digits in the result.
.TP
.B |
Pops three values and computes a modular exponentiation.
The first value popped is used as the reduction modulus;
this value must be a non-zero number,
and should be an integer.
The second popped is used as the exponent;
this value must be a non-negative number,
and any fractional part of this exponent will be ignored.
The third value popped is the base which gets exponentiated.
The precision value specifies the number of fraction
digits in the result.
For small numbers this is like the sequence \fBSm lble^ Lm%\fP,
but, unlike \fB^\fP, this command will work with arbritrarily large exponents.
.TP
.B v
Pops one value,
computes its square root,
Expand Down Expand Up @@ -131,6 +171,9 @@ Clears the stack, rendering it empty.
Duplicates the value on the top of the stack,
pushing another copy of it.
Thus, ``4d*p'' computes 4 squared and prints it.
.TP
.B r
Reverses the order of (swaps) the top two values on the stack.
.SH
Registers
.PP
Expand Down Expand Up @@ -253,6 +296,13 @@ prints the characters
.B foo
(with no newline).
.TP
.B a
The top-of-stack is popped.
If it was a number, then the low-order byte of this number
is converted into a string and pushed onto the stack.
Otherwise the top-of-stack was a string,
and the first character of that string is pushed back.
.TP
.B x
Pops a value off the stack and executes it as a macro.
Normally it should be a string;
Expand Down Expand Up @@ -378,10 +428,6 @@ are usually only used by traditional implementations of
(The GNU
.I bc
is self contained and does not need \*(dc to run.)
The comment operator
.B #
is a new command not found in traditional implementations of
.IR dc .
.SH
BUGS
.PP
Expand Down
173 changes: 173 additions & 0 deletions gnu/usr.bin/dc/dc.c
@@ -0,0 +1,173 @@
/*
* implement the "dc" Desk Calculator language.
*
* Copyright (C) 1994, 1997 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
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can either send email to this
* program's author (see below) or write to: The Free Software Foundation,
* Inc.; 675 Mass Ave. Cambridge, MA 02139, USA.
*/

/* Written with strong hiding of implementation details
* in their own specialized modules.
*/
/* This module contains the argument processing/main functions.
*/

#include "config.h"

#include <stdio.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#else
# ifdef HAVE_STRINGS_H
# include <strings.h>
# endif
#endif
#include <getopt.h>
#include "dc.h"
#include "dc-proto.h"

#include "version.h"

#ifndef EXIT_SUCCESS /* C89 <stdlib.h> */
# define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE /* C89 <stdlib.h> */
# define EXIT_FAILURE 1
#endif

const char *progname; /* basename of program invocation */

/* your generic usage function */
static void
usage DC_DECLARG((f))
FILE *f DC_DECLEND
{
fprintf(f, "\
Usage: %s [OPTION] [file ...]\n\
-e, --expression=EXPR evaluate expression\n\
-f, --file=FILE evaluate contents of file\n\
-h, --help display this help and exit\n\
-V, --version output version information and exit\n\
\n\
Report bugs to @\n\
", progname);
}

static void
show_version DC_DECLVOID()
{
printf("%s\n\n", DC_VERSION);
printf("Email bug reports to: bug-gnu-utils@prep.ai.mit.edu .\n");
printf("Be sure to include the word ``dc'' \
somewhere in the ``Subject:'' field.\n");
}

/* returns a pointer to one past the last occurance of c in s,
* or s if c does not occur in s.
*/
static char *
r1bindex DC_DECLARG((s, c))
char *s DC_DECLSEP
int c DC_DECLEND
{
char *p = strrchr(s, c);

if (!p)
return s;
return p + 1;
}

static void
try_file(const char *filename) {
FILE *input;

if ( !(input=fopen(filename, "r")) ) {
fprintf(stderr, "Could not open file ");
perror(filename);
exit(EXIT_FAILURE);
}
if (dc_evalfile(input))
exit(EXIT_FAILURE);
fclose(input);
}


int
main DC_DECLARG((argc, argv))
int argc DC_DECLSEP
char **argv DC_DECLEND
{
static struct option const long_opts[] = {
{"expression", required_argument, NULL, 'e'},
{"file", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
int did_eval = 0;
int c;

progname = r1bindex(*argv, '/');
dc_math_init();
dc_string_init();
dc_register_init();
dc_array_init();

while ((c = getopt_long(argc, argv, "hVe:", long_opts, (int *)0)) != EOF) {
switch (c) {
case 'e':
{ dc_data string = dc_makestring(optarg, strlen(optarg));
if (dc_evalstr(string))
return EXIT_SUCCESS;
dc_free_str(&string.v.string);
did_eval = 1;
}
break;
case 'f':
try_file(optarg);
did_eval = 1;
break;
case 'h':
usage(stdout);
return EXIT_SUCCESS;
case 'V':
show_version();
return EXIT_SUCCESS;
default:
usage(stderr);
return EXIT_FAILURE;
}
}

for (; optind < argc; ++optind) {
if (strcmp(argv[optind], "-") == 0) {
if (dc_evalfile(stdin))
return EXIT_FAILURE;
} else {
try_file(argv[optind]);
}
did_eval = 1;
}
if (!did_eval) {
/* if no -e commands and no command files, then eval stdin */
if (dc_evalfile(stdin))
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
13 changes: 5 additions & 8 deletions gnu/usr.bin/dc/dc.h
@@ -1,7 +1,7 @@
/*
* Header file for dc routines
*
* Copyright (C) 1994 Free Software Foundation, Inc.
* Copyright (C) 1994, 1997 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
Expand Down Expand Up @@ -53,14 +53,11 @@ typedef enum {DC_FALSE, DC_TRUE} dc_boolean;
/* type discriminant for dc_data */
typedef enum {DC_UNINITIALIZED, DC_NUMBER, DC_STRING} dc_value_type;

/* generic pointer for information hiding */
typedef void *Opaque;
/* only numeric.c knows what dc_num's *really* look like */
typedef struct dc_number *dc_num;

/* only dc-math.c knows what dc_num's *really* look like */
typedef Opaque dc_num;

/* only dc-string.c knows what dc_str's *really* look like */
typedef Opaque dc_str;
/* only string.c knows what dc_str's *really* look like */
typedef struct dc_string *dc_str;


/* except for the two implementation-specific modules, all
Expand Down

0 comments on commit 9b0d90e

Please sign in to comment.