[BACK]Return to mdocml.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mdocml.c, Revision 1.10

1.10    ! kristaps    1: /* $Id: mdocml.c,v 1.9 2008/11/23 23:12:47 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19: #include <sys/param.h>
                     20: #include <sys/stat.h>
                     21:
                     22: #include <assert.h>
                     23: #include <err.h>
                     24: #include <fcntl.h>
                     25: #include <getopt.h>
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
                     29: #include <unistd.h>
                     30:
                     31: #include "libmdocml.h"
                     32:
1.9       kristaps   33: #define        BUFFER_IN_DEF   BUFSIZ   /* See begin_bufs. */
                     34: #define        BUFFER_OUT_DEF  BUFSIZ   /* See begin_bufs. */
1.2       kristaps   35:
1.9       kristaps   36: static void             usage(void);
                     37:
                     38: static int              begin_io(const struct md_args *,
1.5       kristaps   39:                                char *, char *);
1.9       kristaps   40: static int              leave_io(const struct md_buf *,
1.5       kristaps   41:                                const struct md_buf *, int);
1.9       kristaps   42: static int              begin_bufs(const struct md_args *,
1.5       kristaps   43:                                struct md_buf *, struct md_buf *);
                     44: static int              leave_bufs(const struct md_buf *,
                     45:                                const struct md_buf *, int);
1.1       kristaps   46:
                     47: int
                     48: main(int argc, char *argv[])
                     49: {
                     50:        int              c;
                     51:        char            *out, *in;
1.5       kristaps   52:        struct md_args   args;
1.1       kristaps   53:
                     54:        extern char     *optarg;
                     55:        extern int       optind;
                     56:
1.4       kristaps   57:        out = in = NULL;
1.10    ! kristaps   58:
        !            59:        (void)memset(&args, 0, sizeof(struct md_args));
1.1       kristaps   60:
1.9       kristaps   61:        while (-1 != (c = getopt(argc, argv, "vo:")))
1.1       kristaps   62:                switch (c) {
                     63:                case ('o'):
                     64:                        out = optarg;
                     65:                        break;
1.9       kristaps   66:                case ('v'):
                     67:                        args.dbg++;
                     68:                        break;
1.1       kristaps   69:                default:
                     70:                        usage();
                     71:                        return(1);
                     72:                }
                     73:
                     74:        argv += optind;
1.4       kristaps   75:        argc -= optind;
1.1       kristaps   76:
1.4       kristaps   77:        if (1 == argc)
                     78:                in = *argv++;
1.1       kristaps   79:
1.10    ! kristaps   80:        args.type = MD_DUMMY;
1.5       kristaps   81:
                     82:        return(begin_io(&args, out ? out : "-", in ? in : "-"));
1.1       kristaps   83: }
                     84:
                     85:
1.9       kristaps   86: /*
                     87:  * Close out file descriptors opened in begin_io.  If the descriptor
                     88:  * refers to stdin/stdout, then do nothing.
                     89:  */
1.1       kristaps   90: static int
1.5       kristaps   91: leave_io(const struct md_buf *out,
                     92:                const struct md_buf *in, int c)
1.1       kristaps   93: {
1.4       kristaps   94:        assert(out);
1.1       kristaps   95:        assert(in);
                     96:
1.4       kristaps   97:        if (-1 != in->fd && -1 == close(in->fd)) {
                     98:                assert(in->name);
                     99:                warn("%s", in->name);
                    100:                c = 1;
                    101:        }
                    102:        if (-1 != out->fd && STDOUT_FILENO != out->fd &&
                    103:                        -1 == close(out->fd)) {
                    104:                assert(out->name);
                    105:                warn("%s", out->name);
                    106:                c = 1;
                    107:        }
1.1       kristaps  108:
1.4       kristaps  109:        return(c);
1.1       kristaps  110: }
                    111:
                    112:
1.9       kristaps  113: /*
                    114:  * Open file descriptors or assign stdin/stdout, if dictated by the "-"
                    115:  * token instead of a filename.
                    116:  */
1.1       kristaps  117: static int
1.5       kristaps  118: begin_io(const struct md_args *args, char *out, char *in)
1.1       kristaps  119: {
1.5       kristaps  120:        struct md_buf    fi;
                    121:        struct md_buf    fo;
1.4       kristaps  122:
                    123: #define        FI_FL   O_RDONLY
                    124: #define        FO_FL   O_WRONLY|O_CREAT|O_TRUNC
1.1       kristaps  125:
1.5       kristaps  126:        assert(args);
1.1       kristaps  127:        assert(out);
                    128:        assert(in);
                    129:
1.5       kristaps  130:        bzero(&fi, sizeof(struct md_buf));
                    131:        bzero(&fo, sizeof(struct md_buf));
1.1       kristaps  132:
1.4       kristaps  133:        fi.fd = STDIN_FILENO;
                    134:        fo.fd = STDOUT_FILENO;
1.1       kristaps  135:
1.4       kristaps  136:        fi.name = in;
                    137:        fo.name = out;
1.1       kristaps  138:
1.4       kristaps  139:        if (0 != strncmp(fi.name, "-", 1))
                    140:                if (-1 == (fi.fd = open(fi.name, FI_FL, 0))) {
                    141:                        warn("%s", fi.name);
                    142:                        return(leave_io(&fo, &fi, 1));
                    143:                }
1.1       kristaps  144:
1.4       kristaps  145:        if (0 != strncmp(fo.name, "-", 1))
                    146:                if (-1 == (fo.fd = open(fo.name, FO_FL, 0644))) {
                    147:                        warn("%s", fo.name);
                    148:                        return(leave_io(&fo, &fi, 1));
                    149:                }
1.1       kristaps  150:
1.5       kristaps  151:        return(leave_io(&fo, &fi, begin_bufs(args, &fo, &fi)));
1.4       kristaps  152: }
1.1       kristaps  153:
                    154:
1.9       kristaps  155: /*
                    156:  * Free buffers allocated in begin_bufs.
                    157:  */
1.4       kristaps  158: static int
1.5       kristaps  159: leave_bufs(const struct md_buf *out,
                    160:                const struct md_buf *in, int c)
1.4       kristaps  161: {
                    162:        assert(out);
                    163:        assert(in);
                    164:        if (out->buf)
                    165:                free(out->buf);
                    166:        if (in->buf)
                    167:                free(in->buf);
1.1       kristaps  168:        return(c);
                    169: }
                    170:
                    171:
1.9       kristaps  172: /*
                    173:  * Allocate buffers to the maximum of either the input file's blocksize
                    174:  * or BUFFER_IN_DEF/BUFFER_OUT_DEF, which should be around BUFSIZE.
                    175:  */
1.1       kristaps  176: static int
1.5       kristaps  177: begin_bufs(const struct md_args *args,
                    178:                struct md_buf *out, struct md_buf *in)
1.1       kristaps  179: {
                    180:        struct stat      stin, stout;
1.5       kristaps  181:        int              c;
1.1       kristaps  182:
1.5       kristaps  183:        assert(args);
1.1       kristaps  184:        assert(in);
                    185:        assert(out);
                    186:
                    187:        if (-1 == fstat(in->fd, &stin)) {
1.3       kristaps  188:                warn("%s", in->name);
1.1       kristaps  189:                return(1);
1.8       kristaps  190:        } else if (0 == stin.st_size) {
                    191:                warnx("%s: empty file", in->name);
                    192:                return(1);
1.1       kristaps  193:        } else if (-1 == fstat(out->fd, &stout)) {
1.3       kristaps  194:                warn("%s", out->name);
1.1       kristaps  195:                return(1);
                    196:        }
                    197:
1.3       kristaps  198:        in->bufsz = MAX(stin.st_blksize, BUFFER_IN_DEF);
                    199:        out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF);
1.1       kristaps  200:
1.3       kristaps  201:        if (NULL == (in->buf = malloc(in->bufsz))) {
1.1       kristaps  202:                warn("malloc");
1.4       kristaps  203:                return(leave_bufs(out, in, 1));
1.3       kristaps  204:        } else if (NULL == (out->buf = malloc(out->bufsz))) {
1.1       kristaps  205:                warn("malloc");
1.4       kristaps  206:                return(leave_bufs(out, in, 1));
1.1       kristaps  207:        }
                    208:
1.5       kristaps  209:        c = md_run(args, out, in);
                    210:        return(leave_bufs(out, in, -1 == c ? 1 : 0));
1.1       kristaps  211: }
                    212:
                    213:
                    214: static void
                    215: usage(void)
                    216: {
                    217:        extern char     *__progname;
                    218:
1.9       kristaps  219:        (void)printf("usage: %s [-v] [-o outfile] [infile]\n",
                    220:                        __progname);
1.1       kristaps  221: }

CVSweb