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