freebsd-dev/contrib/bc/dc/dc.c

184 lines
4.2 KiB
C
Raw Normal View History

1998-04-29 21:53:01 +00:00
/*
* implement the "dc" Desk Calculator language.
*
2001-02-26 07:13:00 +00:00
* Copyright (C) 1994, 1997, 1998, 2000 Free Software Foundation, Inc.
1998-04-29 21:53:01 +00:00
*
* 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
2001-02-26 07:13:00 +00:00
* program's author (see below) or write to:
* The Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111 USA
1998-04-29 21:53:01 +00:00
*/
/* 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"
#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 */
2001-02-26 07:13:00 +00:00
static void
bug_report_info DC_DECLVOID()
{
printf("Email bug reports to: bug-dc@gnu.org .\n");
}
static void
show_version DC_DECLVOID()
{
printf("dc (GNU %s %s) %s\n", PACKAGE, VERSION, DC_VERSION);
printf("\n%s\n\
This is free software; see the source for copying conditions. There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\n\
to the extent permitted by law.\n", DC_COPYRIGHT);
}
1998-04-29 21:53:01 +00:00
/* 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\
", progname);
2001-02-26 07:13:00 +00:00
bug_report_info();
1998-04-29 21:53:01 +00:00
}
/* 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
2000-01-16 10:01:07 +00:00
try_file(const char *filename)
{
1998-04-29 21:53:01 +00:00
FILE *input;
2000-01-16 10:01:07 +00:00
if (strcmp(filename, "-") == 0) {
input = stdin;
} else if ( !(input=fopen(filename, "r")) ) {
1998-04-29 21:53:01 +00:00
fprintf(stderr, "Could not open file ");
perror(filename);
exit(EXIT_FAILURE);
}
if (dc_evalfile(input))
exit(EXIT_FAILURE);
2000-01-16 10:01:07 +00:00
if (input != stdin)
fclose(input);
1998-04-29 21:53:01 +00:00
}
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, '/');
2000-01-16 10:01:07 +00:00
#ifdef HAVE_SETVBUF
/* attempt to simplify interaction with applications such as emacs */
(void) setvbuf(stdout, NULL, _IOLBF, 0);
#endif
1998-04-29 21:53:01 +00:00
dc_math_init();
dc_string_init();
dc_register_init();
dc_array_init();
2000-01-16 10:01:07 +00:00
while ((c = getopt_long(argc, argv, "hVe:f:", long_opts, (int *)0)) != EOF) {
1998-04-29 21:53:01 +00:00
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) {
2000-01-16 10:01:07 +00:00
try_file(argv[optind]);
1998-04-29 21:53:01 +00:00
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;
}