summaryrefslogtreecommitdiff
path: root/dc/dc.c
blob: 6a2bb2639235f8595988f2d0553c5a72f0fc5d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * implement the "dc" Desk Calculator language.
 *
 * Copyright (C) 1994, 1997, 1998, 2000, 2003, 2006, 2008, 2013
 * 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 3, 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, see <http://www.gnu.org/licenses/>.
 *
 */

/* 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
#ifdef HAVE_FSTAT
# include <sys/types.h>
# include <sys/stat.h>
#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 */

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);
}

/* 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);
	bug_report_info();
}

/* 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 == NULL)
		return s;
	return p + 1;
}

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

	if (strcmp(filename, "-") == 0) {
		input = stdin;
	} else if ( (input=fopen(filename, "r")) == NULL ) {
		fprintf(stderr, "%s: Could not open file %s\n", progname, filename);
		return;
	}
	{
	    /* Several complaints have been filed about dc's silence
	     * when a "cd" typo is made.  I really wanted to avoid
	     * this mess, but I guess it really should be added...
	     */
#ifndef HAVE_FSTAT
	    /* non-POSIXish system; this code _might_ notice a directory */
	    int c = getc(input);
	    if (c == EOF && ferror(input)) {
			perror(filename);
			goto close;
	    }
	    ungetc(c, input);

#else /* HAVE_FSTAT */
  /* If HAVE_FSTAT and no S_IS*() macros, it must be a pre-POSIX
   * Unix-ish system?
   */
# ifndef S_ISREG
#  ifdef S_IFREG
#    define S_ISREG(m)	(((m)&S_IFMT)==S_IFREG)
#   else
#    define S_ISREG(m)	0
#  endif
# endif
# ifndef S_ISCHR
#  ifdef S_IFCHR
#   define S_ISCHR(m)	(((m)&S_IFMT)==S_IFCHR)
#  endif
# endif
# ifndef S_ISFIFO
#  ifdef S_IFIFO
#   define S_ISFIFO(m)	(((m)&S_IFMT)==S_IFIFO)
#  endif
# endif
# ifndef S_ISSOCK
#  ifdef S_IFSOCK
#   define S_ISSOCK(m)	(((m)&S_IFMT)==S_IFSOCK)
#  endif
# endif
# ifndef S_ISDIR
#  ifdef S_IFDIR
#   define S_ISDIR(m)	(((m)&S_IFMT)==S_IFDIR)
#  endif
# endif
# ifndef S_ISBLK
#  ifdef S_IFBLK
#   define S_ISBLK(m)	(((m)&S_IFMT)==S_IFBLK)
#  endif
# endif
	    struct stat s;
	    if (fstat(fileno(input), &s) == -1) {
			/* "can't happen" */
			fprintf(stderr, "%s: Could not fstat file ", progname);
			perror(filename);
			goto close;
	    }

#ifdef S_ISDIR
		if (S_ISDIR(s.st_mode)) {
			fprintf(stderr,
				"%s: Will not attempt to process directory %s\n",
				progname, filename);
			goto close;
		} else
#endif
#ifdef S_ISBLK
		if (S_ISBLK(s.st_mode)) {
			fprintf(stderr,
				"%s: Will not attempt to process block-special file %s\n",
				progname, filename);
			goto close;
		} else
#endif
	    if (!S_ISREG(s.st_mode)
# ifdef S_ISCHR
			/* typically will be /dev/null or some sort of tty */
			&& !S_ISCHR(s.st_mode)
# endif
# ifdef S_ISFIFO
			&& !S_ISFIFO(s.st_mode)
# endif
# ifdef S_ISSOCK
			&& !S_ISSOCK(s.st_mode)
# endif
				) {
			fprintf(stderr,
				"%s: Will not attempt to process file of unusual type: %s\n",
				progname, filename);
			goto close;
	    }
#endif /* HAVE_FSTAT */
	}
	if (dc_evalfile(input) != DC_SUCCESS)
		exit(EXIT_FAILURE);
close:
	if (input != stdin)
		fclose(input);
}



/* Check to see if there were any output errors; if so, then give
 * an error message (if stderr is not known to be unhappy), and
 * ensure that the program exits with an error indication.
 */
static int
flush_okay DC_DECLVOID()
{
	const char *errmsg = NULL;
	int r = EXIT_SUCCESS;

	if (ferror(stdout))
		errmsg = "error writing to stdout";
	else if (fflush(stdout))
		errmsg = "error flushing stdout";
	else if (fclose(stdout))
		errmsg = "error closing stdout";

	if (errmsg) {
		fprintf(stderr, "%s: ", progname);
		perror(errmsg);
		r = EXIT_FAILURE;
	}

	if (ferror(stderr) || fclose(stderr))
		r = EXIT_FAILURE;
	return r;
}


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:f:", long_opts, (int *)0)) != EOF) {
		switch (c) {
		case 'e':
			{	dc_data string = dc_makestring(optarg, strlen(optarg));
				if (dc_evalstr(&string) != DC_SUCCESS)
					return flush_okay();
				dc_free_str(&string.v.string);
				did_eval = 1;
			}
			break;
		case 'f':
			try_file(optarg);
			did_eval = 1;
			break;
		case 'h':
			usage(stdout);
			return flush_okay();
		case 'V':
			show_version();
			return flush_okay();
		default:
			usage(stderr);
			return EXIT_FAILURE;
		}
	}

	for (; optind < argc; ++optind) {
		try_file(argv[optind]);
		did_eval = 1;
	}
	if (did_eval == 0) {
		/* if no -e commands and no command files, then eval stdin */
		if (dc_evalfile(stdin) != DC_SUCCESS)
			return EXIT_FAILURE;
	}
	return flush_okay();
}


/*
 * Local Variables:
 * mode: C
 * tab-width: 4
 * End:
 * vi: set ts=4 :
 */