xref: /freebsd/usr.bin/dc/dc.c (revision 42249ef2)
1 /*	$OpenBSD: dc.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
5  * Copyright (c) 2009, Gabor Kovesdan <gabor@FreeBSD.org>
6  *
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.
10  *
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.
18  */
19 
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22 
23 #include <sys/stat.h>
24 
25 #include <capsicum_helpers.h>
26 #include <ctype.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <getopt.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "extern.h"
37 
38 #define	DC_VER		"1.3-FreeBSD"
39 
40 static void		 usage(void);
41 
42 extern char		*__progname;
43 
44 static struct source	 src;
45 
46 static const struct option long_options[] =
47 {
48 	{"expression",		required_argument,	NULL,	'e'},
49 	{"file",		required_argument,	NULL,	'f'},
50 	{"help",		no_argument,		NULL,	'h'},
51 	{"version",		no_argument,		NULL,	'V'}
52 };
53 
54 static void
55 usage(void)
56 {
57 	fprintf(stderr, "usage: %s [-hVx] [-e expression] [file]\n",
58 	    __progname);
59 	exit(1);
60 }
61 
62 static void
63 procfd(int fd, char *fname) {
64 	struct stat st;
65 	FILE *file;
66 
67 	file = fdopen(fd, "r");
68 	if (file == NULL)
69 		err(1, "cannot open file %s", fname);
70 	if (fstat(fileno(file), &st) == -1)
71 		err(1, "%s", fname);
72 	if (S_ISDIR(st.st_mode)) {
73 		errno = EISDIR;
74 		err(1, "%s", fname);
75 	}
76 	src_setstream(&src, file);
77 	reset_bmachine(&src);
78 	eval();
79 	fclose(file);
80 }
81 
82 int
83 main(int argc, char *argv[])
84 {
85 	int ch, fd;
86 	bool extended_regs = false, preproc_done = false;
87 
88 	/* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
89 	while ((ch = getopt_long(argc, argv, "e:f:hVx", long_options, NULL)) != -1) {
90 		switch (ch) {
91 		case 'e':
92 			if (!preproc_done)
93 				init_bmachine(extended_regs);
94 			src_setstring(&src, optarg);
95 			reset_bmachine(&src);
96 			eval();
97 			preproc_done = true;
98 			break;
99 		case 'f':
100 			if (!preproc_done)
101 				init_bmachine(extended_regs);
102 			fd = open(optarg, O_RDONLY);
103 			if (fd < 0)
104 				err(1, "cannot open file %s", optarg);
105 			procfd(fd, optarg);
106 			preproc_done = true;
107 			break;
108 		case 'x':
109 			extended_regs = true;
110 			break;
111 		case 'V':
112 			fprintf(stderr, "%s (BSD bc) %s\n", __progname, DC_VER);
113 			exit(0);
114 			break;
115 		case '-':
116 			break;
117 		case 'h':
118 			/* FALLTHROUGH */
119 		default:
120 			usage();
121 		}
122 	}
123 	argc -= optind;
124 	argv += optind;
125 
126 	if (!preproc_done)
127 		init_bmachine(extended_regs);
128 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
129 	(void)setvbuf(stderr, NULL, _IOLBF, 0);
130 
131 	if (argc > 1)
132 		usage();
133 	if (argc == 1) {
134 		fd = open(argv[0], O_RDONLY);
135 		if (fd < 0)
136 			err(1, "cannot open file %s", argv[0]);
137 
138 		if (caph_limit_stream(fd, CAPH_READ) < 0 ||
139 		    caph_limit_stdio() < 0 ||
140 		    caph_enter() < 0)
141 			err(1, "capsicum");
142 
143 		procfd(fd, argv[0]);
144 		preproc_done = true;
145 	}
146 	if (preproc_done)
147 		return (0);
148 
149 	if (caph_limit_stdio() < 0 || caph_enter())
150 		err(1, "capsicum");
151 	src_setstream(&src, stdin);
152 	reset_bmachine(&src);
153 	eval();
154 
155 	return (0);
156 }
157