xref: /openbsd/usr.bin/dc/dc.c (revision 73471bf0)
1 /*	$OpenBSD: dc.c,v 1.20 2017/12/06 13:48:05 otto Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/stat.h>
20 #include <err.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include "extern.h"
27 
28 static __dead void	usage(void);
29 
30 extern char		*__progname;
31 
32 static __dead void
33 usage(void)
34 {
35 	(void)fprintf(stderr, "usage: %s [-x] [-e expression] [file]\n",
36 	    __progname);
37 	exit(1);
38 }
39 
40 int
41 dc_main(int argc, char *argv[])
42 {
43 	int		ch;
44 	bool		extended_regs = false;
45 	FILE		*file;
46 	struct source	src;
47 	char		*buf, *p;
48 	struct stat	st;
49 
50 	buf = bstrdup("");
51 	/* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
52 	optind = 1;
53 	optreset = 1;
54 	while ((ch = getopt(argc, argv, "e:x-")) != -1) {
55 		switch (ch) {
56 		case 'e':
57 			p = buf;
58 			if (asprintf(&buf, "%s %s", buf, optarg) == -1)
59 				err(1, NULL);
60 			free(p);
61 			break;
62 		case 'x':
63 			extended_regs = true;
64 			break;
65 		case '-':
66 			break;
67 		default:
68 			usage();
69 		}
70 	}
71 	argc -= optind;
72 	argv += optind;
73 
74 	if (argc == 0) {
75 		if (pledge("stdio", NULL) == -1)
76 			err(1, "pledge");
77 	}
78 
79 	init_bmachine(extended_regs);
80 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
81 	(void)setvbuf(stderr, NULL, _IOLBF, 0);
82 
83 	if (argc > 1)
84 		usage();
85 	if (buf[0] != '\0') {
86 		src_setstring(&src, buf);
87 		reset_bmachine(&src);
88 		eval();
89 		free(buf);
90 		if (argc == 0)
91 			return (0);
92 	}
93 	if (argc == 1) {
94 		file = fopen(argv[0], "r");
95 		if (file == NULL)
96 			err(1, "cannot open file %s", argv[0]);
97 
98 		if (pledge("stdio", NULL) == -1)
99 			err(1, "pledge");
100 
101 		if (fstat(fileno(file), &st) == -1)
102 			err(1, "%s", argv[0]);
103 		if (S_ISDIR(st.st_mode))
104 			errc(1, EISDIR, "%s", argv[0]);
105 		src_setstream(&src, file);
106 		reset_bmachine(&src);
107 		eval();
108 		(void)fclose(file);
109 		/*
110 		 * BSD and Solaris dc(1) continue with stdin after processing
111 		 * the file given as the argument. We follow GNU dc(1).
112 		 */
113 		 return (0);
114 	}
115 
116 	src_setstream(&src, stdin);
117 	reset_bmachine(&src);
118 	eval();
119 
120 	return (0);
121 }
122