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

Annotation of mandoc/mdocml.c, Revision 1.20

1.20    ! kristaps    1: /* $Id: mdocml.c,v 1.19 2008/12/09 17:09:12 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.17      kristaps   36: #ifdef DEBUG
                     37: #define        CSS             "mdocml.css"
                     38: #else
                     39: #define        CSS             "/usr/local/share/mdocml/mdocml.css"
                     40: #endif
                     41:
1.9       kristaps   42: static void             usage(void);
                     43:
                     44: static int              begin_io(const struct md_args *,
1.5       kristaps   45:                                char *, char *);
1.9       kristaps   46: static int              leave_io(const struct md_buf *,
1.5       kristaps   47:                                const struct md_buf *, int);
1.9       kristaps   48: static int              begin_bufs(const struct md_args *,
1.5       kristaps   49:                                struct md_buf *, struct md_buf *);
                     50: static int              leave_bufs(const struct md_buf *,
                     51:                                const struct md_buf *, int);
1.1       kristaps   52:
1.19      kristaps   53: #ifdef __linux__
                     54: extern int              getsubopt(char **, char *const *, char **);
                     55: #endif
                     56:
1.1       kristaps   57: int
                     58: main(int argc, char *argv[])
                     59: {
                     60:        int              c;
1.18      kristaps   61:        char            *out, *in, *opts, *v;
1.5       kristaps   62:        struct md_args   args;
1.18      kristaps   63: #define ALL             0
                     64: #define ERROR           1
                     65:        char            *toks[] = { "all", "error", NULL };
1.1       kristaps   66:
                     67:        extern char     *optarg;
                     68:        extern int       optind;
                     69:
1.17      kristaps   70:        out = in = NULL;
1.10      kristaps   71:
                     72:        (void)memset(&args, 0, sizeof(struct md_args));
1.17      kristaps   73:
1.20    ! kristaps   74:        args.type = MD_NOOP;
1.17      kristaps   75:
1.18      kristaps   76:        while (-1 != (c = getopt(argc, argv, "c:ef:o:vW:")))
1.1       kristaps   77:                switch (c) {
1.17      kristaps   78:                case ('c'):
                     79:                        if (args.type != MD_HTML)
                     80:                                errx(1, "-c only valid for -fhtml");
                     81:                        args.params.html.css = optarg;
                     82:                        break;
                     83:                case ('e'):
                     84:                        if (args.type != MD_HTML)
                     85:                                errx(1, "-e only valid for -fhtml");
                     86:                        args.params.html.flags |= HTML_CSS_EMBED;
                     87:                        break;
1.16      kristaps   88:                case ('f'):
1.17      kristaps   89:                        if (0 == strcmp(optarg, "html"))
                     90:                                args.type = MD_HTML;
                     91:                        else if (0 == strcmp(optarg, "xml"))
                     92:                                args.type = MD_XML;
1.20    ! kristaps   93:                        else if (0 == strcmp(optarg, "noop"))
        !            94:                                args.type = MD_NOOP;
1.17      kristaps   95:                        else
                     96:                                errx(1, "invalid filter type");
1.16      kristaps   97:                        break;
1.1       kristaps   98:                case ('o'):
                     99:                        out = optarg;
                    100:                        break;
1.13      kristaps  101:                case ('v'):
                    102:                        args.verbosity++;
                    103:                        break;
                    104:                case ('W'):
1.18      kristaps  105:                        opts = optarg;
                    106:                        while (*opts)
                    107:                                switch (getsubopt(&opts, toks, &v)) {
                    108:                                case (ALL):
                    109:                                        args.warnings |= MD_WARN_ALL;
                    110:                                        break;
                    111:                                case (ERROR):
                    112:                                        args.warnings |= MD_WARN_ERROR;
                    113:                                        break;
                    114:                                default:
                    115:                                        usage();
                    116:                                        return(1);
                    117:                                }
1.13      kristaps  118:                        break;
1.1       kristaps  119:                default:
                    120:                        usage();
                    121:                        return(1);
                    122:                }
                    123:
1.17      kristaps  124:        if (MD_HTML == args.type)
                    125:                if (NULL == args.params.html.css)
                    126:                        args.params.html.css = CSS;
                    127:
1.1       kristaps  128:        argv += optind;
1.4       kristaps  129:        argc -= optind;
1.1       kristaps  130:
1.4       kristaps  131:        if (1 == argc)
                    132:                in = *argv++;
1.1       kristaps  133:
1.5       kristaps  134:        return(begin_io(&args, out ? out : "-", in ? in : "-"));
1.1       kristaps  135: }
                    136:
                    137:
1.9       kristaps  138: /*
                    139:  * Close out file descriptors opened in begin_io.  If the descriptor
                    140:  * refers to stdin/stdout, then do nothing.
                    141:  */
1.1       kristaps  142: static int
1.5       kristaps  143: leave_io(const struct md_buf *out,
                    144:                const struct md_buf *in, int c)
1.1       kristaps  145: {
1.4       kristaps  146:        assert(out);
1.1       kristaps  147:        assert(in);
                    148:
1.4       kristaps  149:        if (-1 != in->fd && -1 == close(in->fd)) {
                    150:                assert(in->name);
                    151:                warn("%s", in->name);
                    152:                c = 1;
                    153:        }
                    154:        if (-1 != out->fd && STDOUT_FILENO != out->fd &&
                    155:                        -1 == close(out->fd)) {
                    156:                assert(out->name);
                    157:                warn("%s", out->name);
                    158:                c = 1;
                    159:        }
1.15      kristaps  160:        if (1 == c && STDOUT_FILENO != out->fd)
                    161:                if (-1 == unlink(out->name))
                    162:                        warn("%s", out->name);
1.1       kristaps  163:
1.4       kristaps  164:        return(c);
1.1       kristaps  165: }
                    166:
                    167:
1.9       kristaps  168: /*
                    169:  * Open file descriptors or assign stdin/stdout, if dictated by the "-"
                    170:  * token instead of a filename.
                    171:  */
1.1       kristaps  172: static int
1.5       kristaps  173: begin_io(const struct md_args *args, char *out, char *in)
1.1       kristaps  174: {
1.5       kristaps  175:        struct md_buf    fi;
                    176:        struct md_buf    fo;
1.4       kristaps  177:
                    178: #define        FI_FL   O_RDONLY
                    179: #define        FO_FL   O_WRONLY|O_CREAT|O_TRUNC
1.1       kristaps  180:
1.5       kristaps  181:        assert(args);
1.1       kristaps  182:        assert(out);
                    183:        assert(in);
                    184:
1.5       kristaps  185:        bzero(&fi, sizeof(struct md_buf));
                    186:        bzero(&fo, sizeof(struct md_buf));
1.1       kristaps  187:
1.4       kristaps  188:        fi.fd = STDIN_FILENO;
                    189:        fo.fd = STDOUT_FILENO;
1.1       kristaps  190:
1.4       kristaps  191:        fi.name = in;
                    192:        fo.name = out;
1.1       kristaps  193:
1.4       kristaps  194:        if (0 != strncmp(fi.name, "-", 1))
                    195:                if (-1 == (fi.fd = open(fi.name, FI_FL, 0))) {
                    196:                        warn("%s", fi.name);
                    197:                        return(leave_io(&fo, &fi, 1));
                    198:                }
1.1       kristaps  199:
1.4       kristaps  200:        if (0 != strncmp(fo.name, "-", 1))
                    201:                if (-1 == (fo.fd = open(fo.name, FO_FL, 0644))) {
                    202:                        warn("%s", fo.name);
                    203:                        return(leave_io(&fo, &fi, 1));
                    204:                }
1.1       kristaps  205:
1.5       kristaps  206:        return(leave_io(&fo, &fi, begin_bufs(args, &fo, &fi)));
1.4       kristaps  207: }
1.1       kristaps  208:
                    209:
1.9       kristaps  210: /*
                    211:  * Free buffers allocated in begin_bufs.
                    212:  */
1.4       kristaps  213: static int
1.5       kristaps  214: leave_bufs(const struct md_buf *out,
                    215:                const struct md_buf *in, int c)
1.4       kristaps  216: {
                    217:        assert(out);
                    218:        assert(in);
                    219:        if (out->buf)
                    220:                free(out->buf);
                    221:        if (in->buf)
                    222:                free(in->buf);
1.1       kristaps  223:        return(c);
                    224: }
                    225:
                    226:
1.9       kristaps  227: /*
                    228:  * Allocate buffers to the maximum of either the input file's blocksize
                    229:  * or BUFFER_IN_DEF/BUFFER_OUT_DEF, which should be around BUFSIZE.
                    230:  */
1.1       kristaps  231: static int
1.5       kristaps  232: begin_bufs(const struct md_args *args,
                    233:                struct md_buf *out, struct md_buf *in)
1.1       kristaps  234: {
                    235:        struct stat      stin, stout;
1.5       kristaps  236:        int              c;
1.1       kristaps  237:
1.5       kristaps  238:        assert(args);
1.1       kristaps  239:        assert(in);
                    240:        assert(out);
                    241:
                    242:        if (-1 == fstat(in->fd, &stin)) {
1.3       kristaps  243:                warn("%s", in->name);
1.1       kristaps  244:                return(1);
1.12      kristaps  245:        } else if (STDIN_FILENO != in->fd && 0 == stin.st_size) {
1.8       kristaps  246:                warnx("%s: empty file", in->name);
                    247:                return(1);
1.1       kristaps  248:        } else if (-1 == fstat(out->fd, &stout)) {
1.3       kristaps  249:                warn("%s", out->name);
1.1       kristaps  250:                return(1);
                    251:        }
                    252:
1.3       kristaps  253:        in->bufsz = MAX(stin.st_blksize, BUFFER_IN_DEF);
                    254:        out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF);
1.1       kristaps  255:
1.3       kristaps  256:        if (NULL == (in->buf = malloc(in->bufsz))) {
1.1       kristaps  257:                warn("malloc");
1.4       kristaps  258:                return(leave_bufs(out, in, 1));
1.3       kristaps  259:        } else if (NULL == (out->buf = malloc(out->bufsz))) {
1.1       kristaps  260:                warn("malloc");
1.4       kristaps  261:                return(leave_bufs(out, in, 1));
1.1       kristaps  262:        }
                    263:
1.5       kristaps  264:        c = md_run(args, out, in);
                    265:        return(leave_bufs(out, in, -1 == c ? 1 : 0));
1.1       kristaps  266: }
                    267:
                    268:
                    269: static void
                    270: usage(void)
                    271: {
                    272:        extern char     *__progname;
                    273:
1.19      kristaps  274:        (void)fprintf(stderr, "usage: %s [-v] [-Wwarn...]  "
                    275:                        "[-f filter] [-o outfile] [infile]\n",
                    276:                        __progname);
1.1       kristaps  277: }
1.18      kristaps  278:

CVSweb