[BACK]Return to dc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / dc

Annotation of src/usr.bin/dc/dc.c, Revision 1.16

1.16    ! deraadt     1: /*     $OpenBSD: dc.c,v 1.15 2015/10/09 01:37:07 deraadt 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:
1.8       otto       19: #include <sys/stat.h>
1.1       otto       20: #include <err.h>
1.8       otto       21: #include <errno.h>
1.5       otto       22: #include <stdlib.h>
1.6       otto       23: #include <string.h>
1.4       otto       24: #include <unistd.h>
1.1       otto       25:
                     26: #include "extern.h"
                     27:
                     28: static __dead void     usage(void);
                     29:
1.2       deraadt    30: extern char            *__progname;
1.1       otto       31:
                     32: static __dead void
                     33: usage(void)
                     34: {
1.10      sobrado    35:        (void)fprintf(stderr, "usage: %s [-x] [-e expression] [file]\n",
                     36:            __progname);
1.1       otto       37:        exit(1);
                     38: }
                     39:
                     40: int
1.16    ! deraadt    41: dc_main(int argc, char *argv[])
1.1       otto       42: {
1.4       otto       43:        int             ch;
                     44:        bool            extended_regs = false;
1.1       otto       45:        FILE            *file;
                     46:        struct source   src;
1.6       otto       47:        char            *buf, *p;
1.8       otto       48:        struct stat     st;
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.13      millert    74:        (void)setvbuf(stdout, NULL, _IOLBF, 0);
                     75:        (void)setvbuf(stderr, NULL, _IOLBF, 0);
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.9       otto       88:                file = fopen(argv[0], "r");
                     89:                if (file == NULL)
                     90:                        err(1, "cannot open file %s", argv[0]);
1.15      deraadt    91:                if (pledge("stdio", NULL) == -1)
                     92:                        err(1, "pledge");
1.9       otto       93:                if (fstat(fileno(file), &st) == -1)
1.8       otto       94:                        err(1, "%s", argv[0]);
1.12      guenther   95:                if (S_ISDIR(st.st_mode))
                     96:                        errc(1, EISDIR, "%s", argv[0]);
1.1       otto       97:                src_setstream(&src, file);
                     98:                reset_bmachine(&src);
                     99:                eval();
1.7       otto      100:                (void)fclose(file);
1.6       otto      101:                /*
                    102:                 * BSD and Solaris dc(1) continue with stdin after processing
                    103:                 * the file given as the argument. We follow GNU dc(1).
                    104:                 */
                    105:                 return (0);
1.1       otto      106:        }
1.15      deraadt   107:        if (pledge("stdio", NULL) == -1)
                    108:                err(1, "pledge");
1.1       otto      109:        src_setstream(&src, stdin);
                    110:        reset_bmachine(&src);
                    111:        eval();
                    112:
1.6       otto      113:        return (0);
1.1       otto      114: }