2 * implement the "dc" Desk Calculator language.
4 * Copyright (C) 1994, 1997, 1998, 2000 Free Software Foundation, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can either send email to this
18 * program's author (see below) or write to:
19 * The Free Software Foundation, Inc.
20 * 59 Temple Place, Suite 330
21 * Boston, MA 02111 USA
24 /* Written with strong hiding of implementation details
25 * in their own specialized modules.
27 /* This module contains the argument processing/main functions.
39 # ifdef HAVE_STRINGS_H
47 #ifndef EXIT_SUCCESS /* C89 <stdlib.h> */
48 # define EXIT_SUCCESS 0
50 #ifndef EXIT_FAILURE /* C89 <stdlib.h> */
51 # define EXIT_FAILURE 1
54 const char *progname; /* basename of program invocation */
57 bug_report_info DC_DECLVOID()
59 printf("Email bug reports to: bug-dc@gnu.org .\n");
63 show_version DC_DECLVOID()
65 printf("dc (GNU %s %s) %s\n", PACKAGE, VERSION, DC_VERSION);
67 This is free software; see the source for copying conditions. There is NO\n\
68 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\n\
69 to the extent permitted by law.\n", DC_COPYRIGHT);
72 /* your generic usage function */
78 Usage: %s [OPTION] [file ...]\n\
79 -e, --expression=EXPR evaluate expression\n\
80 -f, --file=FILE evaluate contents of file\n\
81 -h, --help display this help and exit\n\
82 -V, --version output version information and exit\n\
88 /* returns a pointer to one past the last occurance of c in s,
89 * or s if c does not occur in s.
92 r1bindex DC_DECLARG((s, c))
96 char *p = strrchr(s, c);
104 try_file(const char *filename)
108 if (strcmp(filename, "-") == 0) {
110 } else if ( !(input=fopen(filename, "r")) ) {
111 fprintf(stderr, "Could not open file ");
115 if (dc_evalfile(input))
123 main DC_DECLARG((argc, argv))
125 char **argv DC_DECLEND
127 static struct option const long_opts[] = {
128 {"expression", required_argument, NULL, 'e'},
129 {"file", required_argument, NULL, 'f'},
130 {"help", no_argument, NULL, 'h'},
131 {"version", no_argument, NULL, 'V'},
137 progname = r1bindex(*argv, '/');
139 /* attempt to simplify interaction with applications such as emacs */
140 (void) setvbuf(stdout, NULL, _IOLBF, 0);
147 while ((c = getopt_long(argc, argv, "hVe:f:", long_opts, (int *)0)) != EOF) {
150 { dc_data string = dc_makestring(optarg, strlen(optarg));
151 if (dc_evalstr(string))
153 dc_free_str(&string.v.string);
173 for (; optind < argc; ++optind) {
174 try_file(argv[optind]);
178 /* if no -e commands and no command files, then eval stdin */
179 if (dc_evalfile(stdin))