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