Annotation of src/usr.bin/dc/dc.c, Revision 1.6
1.6 ! otto 1: /* $OpenBSD: dc.c,v 1.5 2004/01/13 08:17:41 otto Exp $ */
1.1 otto 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: #ifndef lint
1.6 ! otto 20: static const char rcsid[] = "$OpenBSD: dc.c,v 1.5 2004/01/13 08:17:41 otto Exp $";
1.1 otto 21: #endif /* not lint */
22:
23: #include <err.h>
1.5 otto 24: #include <stdlib.h>
1.6 ! otto 25: #include <string.h>
1.4 otto 26: #include <unistd.h>
1.1 otto 27:
28: #include "extern.h"
29:
30: static __dead void usage(void);
31:
1.2 deraadt 32: extern char *__progname;
1.1 otto 33:
34: static __dead void
35: usage(void)
36: {
1.6 ! otto 37: fprintf(stderr, "usage: %s [-x] [-e expr] [file]\n", __progname);
1.1 otto 38: exit(1);
39: }
40:
41: int
42: main(int argc, char *argv[])
43: {
1.4 otto 44: int ch;
45: bool extended_regs = false;
1.1 otto 46: FILE *file;
47: struct source src;
1.6 ! otto 48: char *buf, *p;
1.1 otto 49:
1.6 ! otto 50: if ((buf = strdup("")) == NULL)
! 51: err(1, NULL);
1.4 otto 52: /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
1.6 ! otto 53: while ((ch = getopt(argc, argv, "e:x-")) != -1) {
1.4 otto 54: switch (ch) {
1.6 ! otto 55: case 'e':
! 56: p = buf;
! 57: if (asprintf(&buf, "%s %s", buf, optarg) == -1)
! 58: err(1, NULL);
! 59: free(p);
! 60: break;
1.4 otto 61: case 'x':
62: extended_regs = true;
63: break;
64: case '-':
65: break;
66: default:
67: usage();
68: }
69: }
70: argc -= optind;
71: argv += optind;
1.1 otto 72:
1.4 otto 73: init_bmachine(extended_regs);
1.1 otto 74: setlinebuf(stdout);
75: setlinebuf(stderr);
1.4 otto 76:
77: if (argc > 1)
78: usage();
1.6 ! otto 79: if (buf[0] != '\0') {
! 80: src_setstring(&src, buf);
! 81: reset_bmachine(&src);
! 82: eval();
! 83: free(buf);
! 84: if (argc == 0)
! 85: return (0);
! 86: }
! 87: if (argc == 1) {
1.4 otto 88: file = fopen(argv[0], "r");
1.1 otto 89: if (file == NULL)
1.4 otto 90: err(1, "cannot open file %s", argv[0]);
1.1 otto 91: src_setstream(&src, file);
92: reset_bmachine(&src);
93: eval();
94: fclose(file);
1.6 ! otto 95: /*
! 96: * BSD and Solaris dc(1) continue with stdin after processing
! 97: * the file given as the argument. We follow GNU dc(1).
! 98: */
! 99: return (0);
1.1 otto 100: }
101: src_setstream(&src, stdin);
102: reset_bmachine(&src);
103: eval();
104:
1.6 ! otto 105: return (0);
1.1 otto 106: }