1 /* $OpenBSD: dc.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $ */
4 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
5 * Copyright (c) 2009, Gabor Kovesdan <gabor@FreeBSD.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
36 #define DC_VER "1.3-FreeBSD"
38 static void usage(void);
40 extern char *__progname;
42 static struct source src;
44 static const struct option long_options[] =
46 {"expression", required_argument, NULL, 'e'},
47 {"file", required_argument, NULL, 'f'},
48 {"help", no_argument, NULL, 'h'},
49 {"version", no_argument, NULL, 'V'}
55 fprintf(stderr, "usage: %s [-hVx] [-e expression] [file]\n",
61 procfile(char *fname) {
65 file = fopen(fname, "r");
67 err(1, "cannot open file %s", fname);
68 if (fstat(fileno(file), &st) == -1)
70 if (S_ISDIR(st.st_mode)) {
74 src_setstream(&src, file);
81 main(int argc, char *argv[])
84 bool extended_regs = false, preproc_done = false;
86 /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
87 while ((ch = getopt_long(argc, argv, "e:f:hVx", long_options, NULL)) != -1) {
91 init_bmachine(extended_regs);
92 src_setstring(&src, optarg);
99 init_bmachine(extended_regs);
104 extended_regs = true;
107 fprintf(stderr, "%s (BSD bc) %s\n", __progname, DC_VER);
122 init_bmachine(extended_regs);
123 (void)setvbuf(stdout, NULL, _IOLBF, 0);
124 (void)setvbuf(stderr, NULL, _IOLBF, 0);
135 src_setstream(&src, stdin);
136 reset_bmachine(&src);