[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.9

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