1 /* 2 * $OpenBSD: dc.c,v 1.5 2004/01/13 08:17:41 otto Exp $ 3 * $DragonFly: src/usr.bin/dc/dc.c,v 1.1 2004/09/20 04:20:39 dillon Exp $ 4 */ 5 6 /* 7 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> 8 * 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. 12 * 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. 20 */ 21 22 #include <err.h> 23 #include <stdlib.h> 24 #include <unistd.h> 25 26 #include "extern.h" 27 28 static void usage(void); 29 30 extern char *__progname; 31 32 static void 33 usage(void) 34 { 35 fprintf(stderr, "usage: %s [-x] [file]\n", __progname); 36 exit(1); 37 } 38 39 int 40 main(int argc, char *argv[]) 41 { 42 int ch; 43 bool extended_regs = false; 44 FILE *file; 45 struct source src; 46 47 /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */ 48 while ((ch = getopt(argc, argv, "x-")) != -1) { 49 switch (ch) { 50 case 'x': 51 extended_regs = true; 52 break; 53 case '-': 54 break; 55 default: 56 usage(); 57 } 58 } 59 argc -= optind; 60 argv += optind; 61 62 init_bmachine(extended_regs); 63 setlinebuf(stdout); 64 setlinebuf(stderr); 65 66 if (argc > 1) 67 usage(); 68 else if (argc == 1) { 69 file = fopen(argv[0], "r"); 70 if (file == NULL) 71 err(1, "cannot open file %s", argv[0]); 72 src_setstream(&src, file); 73 reset_bmachine(&src); 74 eval(); 75 fclose(file); 76 } 77 /* 78 * BSD dc and Solaris dc continue with stdin after processing 79 * the file given as the argument. 80 */ 81 src_setstream(&src, stdin); 82 reset_bmachine(&src); 83 eval(); 84 85 return 0; 86 } 87