Fix typo in comment.
[dragonfly.git] / usr.bin / dc / dc.c
blob4c47b5b64ba80279add60e62015f4b6e96d2d54f
1 /*
2 * $OpenBSD: dc.c,v 1.6 2004/10/18 07:49:00 otto Exp $
3 * $DragonFly: src/usr.bin/dc/dc.c,v 1.2 2005/04/21 18:50:50 swildner Exp $
4 */
6 /*
7 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <err.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "extern.h"
29 static __dead2 void usage(void);
31 extern char *__progname;
33 static __dead2 void
34 usage(void)
36 fprintf(stderr, "usage: %s [-x] [-e expr] [file]\n", __progname);
37 exit(1);
40 int
41 main(int argc, char *argv[])
43 int ch;
44 bool extended_regs = false;
45 FILE *file;
46 struct source src;
47 char *buf, *p;
49 if ((buf = strdup("")) == NULL)
50 err(1, NULL);
51 /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
52 while ((ch = getopt(argc, argv, "e:x-")) != -1) {
53 switch (ch) {
54 case 'e':
55 p = buf;
56 if (asprintf(&buf, "%s %s", buf, optarg) == -1)
57 err(1, NULL);
58 free(p);
59 break;
60 case 'x':
61 extended_regs = true;
62 break;
63 case '-':
64 break;
65 default:
66 usage();
69 argc -= optind;
70 argv += optind;
72 init_bmachine(extended_regs);
73 setlinebuf(stdout);
74 setlinebuf(stderr);
76 if (argc > 1)
77 usage();
78 if (buf[0] != '\0') {
79 src_setstring(&src, buf);
80 reset_bmachine(&src);
81 eval();
82 free(buf);
83 if (argc == 0)
84 return (0);
86 if (argc == 1) {
87 file = fopen(argv[0], "r");
88 if (file == NULL)
89 err(1, "cannot open file %s", argv[0]);
90 src_setstream(&src, file);
91 reset_bmachine(&src);
92 eval();
93 fclose(file);
95 * BSD and Solaris dc(1) continue with stdin after processing
96 * the file given as the argument. We follow GNU dc(1).
98 return (0);
100 src_setstream(&src, stdin);
101 reset_bmachine(&src);
102 eval();
104 return 0;