|
Packit |
70b277 |
/*
|
|
Packit |
70b277 |
* implement the "dc" Desk Calculator language.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* Copyright (C) 1994, 1997, 1998, 2000, 2003, 2006, 2008, 2013
|
|
Packit |
70b277 |
* Free Software Foundation, Inc.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
70b277 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
70b277 |
* the Free Software Foundation; either version 3, or (at your option)
|
|
Packit |
70b277 |
* any later version.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* This program is distributed in the hope that it will be useful,
|
|
Packit |
70b277 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
70b277 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
70b277 |
* GNU General Public License for more details.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
70b277 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Written with strong hiding of implementation details
|
|
Packit |
70b277 |
* in their own specialized modules.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
/* This module contains the argument processing/main functions.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#include "config.h"
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#include <stdio.h>
|
|
Packit |
70b277 |
#ifdef HAVE_STDLIB_H
|
|
Packit |
70b277 |
# include <stdlib.h>
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
#ifdef HAVE_STRING_H
|
|
Packit |
70b277 |
# include <string.h>
|
|
Packit |
70b277 |
#else
|
|
Packit |
70b277 |
# ifdef HAVE_STRINGS_H
|
|
Packit |
70b277 |
# include <strings.h>
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
#ifdef HAVE_FSTAT
|
|
Packit |
70b277 |
# include <sys/types.h>
|
|
Packit |
70b277 |
# include <sys/stat.h>
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
#include <getopt.h>
|
|
Packit |
70b277 |
#include "dc.h"
|
|
Packit |
70b277 |
#include "dc-proto.h"
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#ifndef EXIT_SUCCESS /* C89 <stdlib.h> */
|
|
Packit |
70b277 |
# define EXIT_SUCCESS 0
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
#ifndef EXIT_FAILURE /* C89 <stdlib.h> */
|
|
Packit |
70b277 |
# define EXIT_FAILURE 1
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
const char *progname; /* basename of program invocation */
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
static void
|
|
Packit |
70b277 |
bug_report_info DC_DECLVOID()
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
printf("Email bug reports to: bug-dc@gnu.org .\n");
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
static void
|
|
Packit |
70b277 |
show_version DC_DECLVOID()
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
printf("dc (GNU %s %s) %s\n", PACKAGE, VERSION, DC_VERSION);
|
|
Packit |
70b277 |
printf("\n%s\n\
|
|
Packit |
70b277 |
This is free software; see the source for copying conditions. There is NO\n\
|
|
Packit |
70b277 |
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\n\
|
|
Packit |
70b277 |
to the extent permitted by law.\n", DC_COPYRIGHT);
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* your generic usage function */
|
|
Packit |
70b277 |
static void
|
|
Packit |
70b277 |
usage DC_DECLARG((f))
|
|
Packit |
70b277 |
FILE *f DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
fprintf(f, "\
|
|
Packit |
70b277 |
Usage: %s [OPTION] [file ...]\n\
|
|
Packit |
70b277 |
-e, --expression=EXPR evaluate expression\n\
|
|
Packit |
70b277 |
-f, --file=FILE evaluate contents of file\n\
|
|
Packit |
70b277 |
-h, --help display this help and exit\n\
|
|
Packit |
70b277 |
-V, --version output version information and exit\n\
|
|
Packit |
70b277 |
\n\
|
|
Packit |
70b277 |
", progname);
|
|
Packit |
70b277 |
bug_report_info();
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* returns a pointer to one past the last occurance of c in s,
|
|
Packit |
70b277 |
* or s if c does not occur in s.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
static char *
|
|
Packit |
70b277 |
r1bindex DC_DECLARG((s, c))
|
|
Packit |
70b277 |
char *s DC_DECLSEP
|
|
Packit |
70b277 |
int c DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
char *p = strrchr(s, c);
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (p == NULL)
|
|
Packit |
70b277 |
return s;
|
|
Packit |
70b277 |
return p + 1;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
static void
|
|
Packit |
70b277 |
try_file(const char *filename)
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
FILE *input;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (strcmp(filename, "-") == 0) {
|
|
Packit |
70b277 |
input = stdin;
|
|
Packit |
70b277 |
} else if ( (input=fopen(filename, "r")) == NULL ) {
|
|
Packit |
70b277 |
fprintf(stderr, "%s: Could not open file %s\n", progname, filename);
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
/* Several complaints have been filed about dc's silence
|
|
Packit |
70b277 |
* when a "cd" typo is made. I really wanted to avoid
|
|
Packit |
70b277 |
* this mess, but I guess it really should be added...
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
#ifndef HAVE_FSTAT
|
|
Packit |
70b277 |
/* non-POSIXish system; this code _might_ notice a directory */
|
|
Packit |
70b277 |
int c = getc(input);
|
|
Packit |
70b277 |
if (c == EOF && ferror(input)) {
|
|
Packit |
70b277 |
perror(filename);
|
|
Packit |
70b277 |
goto close;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
ungetc(c, input);
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#else /* HAVE_FSTAT */
|
|
Packit |
70b277 |
/* If HAVE_FSTAT and no S_IS*() macros, it must be a pre-POSIX
|
|
Packit |
70b277 |
* Unix-ish system?
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
# ifndef S_ISREG
|
|
Packit |
70b277 |
# ifdef S_IFREG
|
|
Packit |
70b277 |
# define S_ISREG(m) (((m)&S_IFMT)==S_IFREG)
|
|
Packit |
70b277 |
# else
|
|
Packit |
70b277 |
# define S_ISREG(m) 0
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# ifndef S_ISCHR
|
|
Packit |
70b277 |
# ifdef S_IFCHR
|
|
Packit |
70b277 |
# define S_ISCHR(m) (((m)&S_IFMT)==S_IFCHR)
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# ifndef S_ISFIFO
|
|
Packit |
70b277 |
# ifdef S_IFIFO
|
|
Packit |
70b277 |
# define S_ISFIFO(m) (((m)&S_IFMT)==S_IFIFO)
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# ifndef S_ISSOCK
|
|
Packit |
70b277 |
# ifdef S_IFSOCK
|
|
Packit |
70b277 |
# define S_ISSOCK(m) (((m)&S_IFMT)==S_IFSOCK)
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# ifndef S_ISDIR
|
|
Packit |
70b277 |
# ifdef S_IFDIR
|
|
Packit |
70b277 |
# define S_ISDIR(m) (((m)&S_IFMT)==S_IFDIR)
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# ifndef S_ISBLK
|
|
Packit |
70b277 |
# ifdef S_IFBLK
|
|
Packit |
70b277 |
# define S_ISBLK(m) (((m)&S_IFMT)==S_IFBLK)
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
struct stat s;
|
|
Packit |
70b277 |
if (fstat(fileno(input), &s) == -1) {
|
|
Packit |
70b277 |
/* "can't happen" */
|
|
Packit |
70b277 |
fprintf(stderr, "%s: Could not fstat file ", progname);
|
|
Packit |
70b277 |
perror(filename);
|
|
Packit |
70b277 |
goto close;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#ifdef S_ISDIR
|
|
Packit |
70b277 |
if (S_ISDIR(s.st_mode)) {
|
|
Packit |
70b277 |
fprintf(stderr,
|
|
Packit |
70b277 |
"%s: Will not attempt to process directory %s\n",
|
|
Packit |
70b277 |
progname, filename);
|
|
Packit |
70b277 |
goto close;
|
|
Packit |
70b277 |
} else
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
#ifdef S_ISBLK
|
|
Packit |
70b277 |
if (S_ISBLK(s.st_mode)) {
|
|
Packit |
70b277 |
fprintf(stderr,
|
|
Packit |
70b277 |
"%s: Will not attempt to process block-special file %s\n",
|
|
Packit |
70b277 |
progname, filename);
|
|
Packit |
70b277 |
goto close;
|
|
Packit |
70b277 |
} else
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
if (!S_ISREG(s.st_mode)
|
|
Packit |
70b277 |
# ifdef S_ISCHR
|
|
Packit |
70b277 |
/* typically will be /dev/null or some sort of tty */
|
|
Packit |
70b277 |
&& !S_ISCHR(s.st_mode)
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# ifdef S_ISFIFO
|
|
Packit |
70b277 |
&& !S_ISFIFO(s.st_mode)
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
# ifdef S_ISSOCK
|
|
Packit |
70b277 |
&& !S_ISSOCK(s.st_mode)
|
|
Packit |
70b277 |
# endif
|
|
Packit |
70b277 |
) {
|
|
Packit |
70b277 |
fprintf(stderr,
|
|
Packit |
70b277 |
"%s: Will not attempt to process file of unusual type: %s\n",
|
|
Packit |
70b277 |
progname, filename);
|
|
Packit |
70b277 |
goto close;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
#endif /* HAVE_FSTAT */
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (dc_evalfile(input) != DC_SUCCESS)
|
|
Packit |
70b277 |
exit(EXIT_FAILURE);
|
|
Packit |
70b277 |
close:
|
|
Packit |
70b277 |
if (input != stdin)
|
|
Packit |
70b277 |
fclose(input);
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Check to see if there were any output errors; if so, then give
|
|
Packit |
70b277 |
* an error message (if stderr is not known to be unhappy), and
|
|
Packit |
70b277 |
* ensure that the program exits with an error indication.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
static int
|
|
Packit |
70b277 |
flush_okay DC_DECLVOID()
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
const char *errmsg = NULL;
|
|
Packit |
70b277 |
int r = EXIT_SUCCESS;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (ferror(stdout))
|
|
Packit |
70b277 |
errmsg = "error writing to stdout";
|
|
Packit |
70b277 |
else if (fflush(stdout))
|
|
Packit |
70b277 |
errmsg = "error flushing stdout";
|
|
Packit |
70b277 |
else if (fclose(stdout))
|
|
Packit |
70b277 |
errmsg = "error closing stdout";
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (errmsg) {
|
|
Packit |
70b277 |
fprintf(stderr, "%s: ", progname);
|
|
Packit |
70b277 |
perror(errmsg);
|
|
Packit |
70b277 |
r = EXIT_FAILURE;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (ferror(stderr) || fclose(stderr))
|
|
Packit |
70b277 |
r = EXIT_FAILURE;
|
|
Packit |
70b277 |
return r;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
int
|
|
Packit |
70b277 |
main DC_DECLARG((argc, argv))
|
|
Packit |
70b277 |
int argc DC_DECLSEP
|
|
Packit |
70b277 |
char **argv DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
static struct option const long_opts[] = {
|
|
Packit |
70b277 |
{"expression", required_argument, NULL, 'e'},
|
|
Packit |
70b277 |
{"file", required_argument, NULL, 'f'},
|
|
Packit |
70b277 |
{"help", no_argument, NULL, 'h'},
|
|
Packit |
70b277 |
{"version", no_argument, NULL, 'V'},
|
|
Packit |
70b277 |
{NULL, 0, NULL, 0}
|
|
Packit |
70b277 |
};
|
|
Packit |
70b277 |
int did_eval = 0;
|
|
Packit |
70b277 |
int c;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
progname = r1bindex(*argv, '/');
|
|
Packit |
70b277 |
dc_math_init();
|
|
Packit |
70b277 |
dc_string_init();
|
|
Packit |
70b277 |
dc_register_init();
|
|
Packit |
70b277 |
dc_array_init();
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
while ((c = getopt_long(argc, argv, "hVe:f:", long_opts, (int *)0)) != EOF) {
|
|
Packit |
70b277 |
switch (c) {
|
|
Packit |
70b277 |
case 'e':
|
|
Packit |
70b277 |
{ dc_data string = dc_makestring(optarg, strlen(optarg));
|
|
Packit |
70b277 |
if (dc_evalstr(&string) != DC_SUCCESS)
|
|
Packit |
70b277 |
return flush_okay();
|
|
Packit |
70b277 |
dc_free_str(&string.v.string);
|
|
Packit |
70b277 |
did_eval = 1;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
break;
|
|
Packit |
70b277 |
case 'f':
|
|
Packit |
70b277 |
try_file(optarg);
|
|
Packit |
70b277 |
did_eval = 1;
|
|
Packit |
70b277 |
break;
|
|
Packit |
70b277 |
case 'h':
|
|
Packit |
70b277 |
usage(stdout);
|
|
Packit |
70b277 |
return flush_okay();
|
|
Packit |
70b277 |
case 'V':
|
|
Packit |
70b277 |
show_version();
|
|
Packit |
70b277 |
return flush_okay();
|
|
Packit |
70b277 |
default:
|
|
Packit |
70b277 |
usage(stderr);
|
|
Packit |
70b277 |
return EXIT_FAILURE;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
for (; optind < argc; ++optind) {
|
|
Packit |
70b277 |
try_file(argv[optind]);
|
|
Packit |
70b277 |
did_eval = 1;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (did_eval == 0) {
|
|
Packit |
70b277 |
/* if no -e commands and no command files, then eval stdin */
|
|
Packit |
70b277 |
if (dc_evalfile(stdin) != DC_SUCCESS)
|
|
Packit |
70b277 |
return EXIT_FAILURE;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
return flush_okay();
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/*
|
|
Packit |
70b277 |
* Local Variables:
|
|
Packit |
70b277 |
* mode: C
|
|
Packit |
70b277 |
* tab-width: 4
|
|
Packit |
70b277 |
* End:
|
|
Packit |
70b277 |
* vi: set ts=4 :
|
|
Packit |
70b277 |
*/
|