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