1/* $OpenBSD: dc.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $ */ 2 3/* 4 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> 5 * Copyright (c) 2009, Gabor Kovesdan <gabor@FreeBSD.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20#include <sys/cdefs.h> 21__FBSDID("$FreeBSD: stable/10/usr.bin/dc/dc.c 315135 2017-03-12 05:36:31Z pfg $"); 22 23#include <sys/stat.h> 24 25#include <ctype.h> 26#include <err.h> 27#include <errno.h> 28#include <getopt.h> 29#include <stdio.h> 30#include <stdlib.h> 31#include <string.h> 32#include <unistd.h> 33 34#include "extern.h" 35 36#define DC_VER "1.3-FreeBSD" 37 38static void usage(void); 39 40extern char *__progname; 41 42static struct source src; 43 44static const struct option long_options[] = 45{ 46 {"expression", required_argument, NULL, 'e'}, 47 {"file", required_argument, NULL, 'f'}, 48 {"help", no_argument, NULL, 'h'}, 49 {"version", no_argument, NULL, 'V'} 50}; 51 52static void 53usage(void) 54{ 55 fprintf(stderr, "usage: %s [-hVx] [-e expression] [file]\n", 56 __progname); 57 exit(1); 58} 59 60static void 61procfile(char *fname) { 62 struct stat st; 63 FILE *file; 64 65 file = fopen(fname, "r"); 66 if (file == NULL) 67 err(1, "cannot open file %s", fname); 68 if (fstat(fileno(file), &st) == -1) 69 err(1, "%s", fname); 70 if (S_ISDIR(st.st_mode)) { 71 errno = EISDIR; 72 err(1, "%s", fname); 73 } 74 src_setstream(&src, file); 75 reset_bmachine(&src); 76 eval(); 77 fclose(file); 78} 79 80int 81main(int argc, char *argv[]) 82{ 83 int ch; 84 bool extended_regs = false, preproc_done = false; 85 86 /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */ 87 while ((ch = getopt_long(argc, argv, "e:f:hVx", long_options, NULL)) != -1) { 88 switch (ch) { 89 case 'e': 90 if (!preproc_done) 91 init_bmachine(extended_regs); 92 src_setstring(&src, optarg); 93 reset_bmachine(&src); 94 eval(); 95 preproc_done = true; 96 break; 97 case 'f': 98 if (!preproc_done) 99 init_bmachine(extended_regs); 100 procfile(optarg); 101 preproc_done = true; 102 break; 103 case 'x': 104 extended_regs = true; 105 break; 106 case 'V': 107 fprintf(stderr, "%s (BSD bc) %s\n", __progname, DC_VER); 108 exit(0); 109 break; 110 case '-': 111 break; 112 case 'h': 113 /* FALLTHROUGH */ 114 default: 115 usage(); 116 } 117 } 118 argc -= optind; 119 argv += optind; 120 121 if (!preproc_done) 122 init_bmachine(extended_regs); 123 (void)setvbuf(stdout, NULL, _IOLBF, 0); 124 (void)setvbuf(stderr, NULL, _IOLBF, 0); 125 126 if (argc > 1) 127 usage(); 128 if (argc == 1) { 129 procfile(argv[0]); 130 preproc_done = true; 131 } 132 if (preproc_done) 133 return (0); 134 135 src_setstream(&src, stdin); 136 reset_bmachine(&src); 137 eval(); 138 139 return (0); 140} 141