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

Annotation of mandoc/libmdocml.c, Revision 1.19

1.19    ! kristaps    1: /* $Id: libmdocml.c,v 1.18 2008/12/03 14:39:59 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:  */
1.2       kristaps   19: #include <assert.h>
1.10      kristaps   20: #include <ctype.h>
1.2       kristaps   21: #include <fcntl.h>
                     22: #include <err.h>
                     23: #include <stdio.h>
1.1       kristaps   24: #include <stdlib.h>
1.2       kristaps   25: #include <string.h>
                     26: #include <unistd.h>
1.1       kristaps   27:
                     28: #include "libmdocml.h"
1.5       kristaps   29: #include "private.h"
1.2       kristaps   30:
1.5       kristaps   31: static int      md_run_enter(const struct md_args *,
                     32:                        struct md_mbuf *, struct md_rbuf *, void *);
                     33: static int      md_run_leave(const struct md_args *, struct md_mbuf *,
                     34:                        struct md_rbuf *, int, void *);
1.7       kristaps   35:
1.5       kristaps   36: static ssize_t  md_buf_fill(struct md_rbuf *);
                     37: static int      md_buf_flush(struct md_mbuf *);
1.2       kristaps   38:
                     39:
1.3       kristaps   40: static ssize_t
1.2       kristaps   41: md_buf_fill(struct md_rbuf *in)
                     42: {
                     43:        ssize_t          ssz;
                     44:
                     45:        assert(in);
                     46:        assert(in->buf);
                     47:        assert(in->bufsz > 0);
                     48:        assert(in->name);
                     49:
                     50:        if (-1 == (ssz = read(in->fd, in->buf, in->bufsz)))
                     51:                warn("%s", in->name);
                     52:
                     53:        return(ssz);
                     54: }
                     55:
                     56:
1.13      kristaps   57: static int md_buf_flush(struct md_mbuf *buf)
1.2       kristaps   58: {
                     59:        ssize_t          sz;
                     60:
                     61:        assert(buf);
                     62:        assert(buf->buf);
                     63:        assert(buf->name);
                     64:
                     65:        if (0 == buf->pos)
                     66:                return(1);
                     67:
                     68:        sz = write(buf->fd, buf->buf, buf->pos);
                     69:
                     70:        if (-1 == sz) {
                     71:                warn("%s", buf->name);
                     72:                return(0);
                     73:        } else if ((size_t)sz != buf->pos) {
                     74:                warnx("%s: short write", buf->name);
                     75:                return(0);
                     76:        }
                     77:
                     78:        buf->pos = 0;
                     79:        return(1);
                     80: }
                     81:
                     82:
1.5       kristaps   83: int
1.2       kristaps   84: md_buf_putchar(struct md_mbuf *buf, char c)
                     85: {
1.14      kristaps   86:
                     87:        assert(buf);
1.2       kristaps   88:        return(md_buf_puts(buf, &c, 1));
                     89: }
                     90:
                     91:
1.5       kristaps   92: int
1.4       kristaps   93: md_buf_putstring(struct md_mbuf *buf, const char *p)
                     94: {
1.14      kristaps   95:
                     96:        assert(buf);
1.4       kristaps   97:        return(md_buf_puts(buf, p, strlen(p)));
                     98: }
                     99:
                    100:
1.5       kristaps  101: int
1.2       kristaps  102: md_buf_puts(struct md_mbuf *buf, const char *p, size_t sz)
                    103: {
                    104:        size_t           ssz;
                    105:
                    106:        assert(p);
                    107:        assert(buf);
                    108:        assert(buf->buf);
                    109:
                    110:        /* LINTED */
                    111:        while (buf->pos + sz > buf->bufsz) {
                    112:                ssz = buf->bufsz - buf->pos;
                    113:                (void)memcpy(/* LINTED */
                    114:                                buf->buf + buf->pos, p, ssz);
                    115:                p += (long)ssz;
                    116:                sz -= ssz;
                    117:                buf->pos += ssz;
                    118:
                    119:                if ( ! md_buf_flush(buf))
                    120:                        return(0);
                    121:        }
                    122:
                    123:        (void)memcpy(/* LINTED */
                    124:                        buf->buf + buf->pos, p, sz);
                    125:        buf->pos += sz;
                    126:        return(1);
                    127: }
                    128:
                    129:
1.3       kristaps  130: static int
1.5       kristaps  131: md_run_leave(const struct md_args *args, struct md_mbuf *mbuf,
                    132:                struct md_rbuf *rbuf, int c, void *data)
1.3       kristaps  133: {
                    134:        assert(args);
                    135:        assert(mbuf);
                    136:        assert(rbuf);
                    137:
                    138:        /* Run exiters. */
                    139:        switch (args->type) {
1.18      kristaps  140:        case (MD_HTML):
                    141:                if ( ! md_exit_html(data, -1 == c ? 0 : 1))
1.9       kristaps  142:                        c = -1;
1.3       kristaps  143:                break;
1.9       kristaps  144:        default:
1.16      kristaps  145:                if ( ! md_exit_xml(data, -1 == c ? 0 : 1))
1.9       kristaps  146:                        c = -1;
1.3       kristaps  147:                break;
                    148:        }
                    149:
                    150:        /* Make final flush of buffer. */
1.17      kristaps  151:        if (-1 != c && ! md_buf_flush(mbuf))
1.3       kristaps  152:                return(-1);
                    153:
                    154:        return(c);
                    155: }
                    156:
                    157:
                    158: static int
1.5       kristaps  159: md_run_enter(const struct md_args *args, struct md_mbuf *mbuf,
                    160:                struct md_rbuf *rbuf, void *p)
1.2       kristaps  161: {
                    162:        ssize_t          sz, i;
                    163:        size_t           pos;
1.19    ! kristaps  164:        char             line[MD_LINE];
1.3       kristaps  165:        md_line          fp;
1.2       kristaps  166:
1.3       kristaps  167:        assert(args);
                    168:        assert(mbuf);
                    169:        assert(rbuf);
                    170:
                    171:        /* Function ptrs to line-parsers. */
                    172:        switch (args->type) {
1.18      kristaps  173:        case (MD_HTML):
                    174:                fp = md_line_html;
1.3       kristaps  175:                break;
1.7       kristaps  176:        default:
1.16      kristaps  177:                fp = md_line_xml;
1.3       kristaps  178:                break;
                    179:        }
1.2       kristaps  180:
1.7       kristaps  181:        pos = 0;
1.2       kristaps  182:
1.7       kristaps  183: again:
                    184:        if (-1 == (sz = md_buf_fill(rbuf))) {
                    185:                return(md_run_leave(args, mbuf, rbuf, -1, p));
                    186:        } else if (0 == sz && 0 != pos) {
                    187:                warnx("%s: no newline at end of file", rbuf->name);
                    188:                return(md_run_leave(args, mbuf, rbuf, -1, p));
                    189:        } else if (0 == sz)
                    190:                return(md_run_leave(args, mbuf, rbuf, 0, p));
1.2       kristaps  191:
1.7       kristaps  192:        for (i = 0; i < sz; i++) {
                    193:                if ('\n' != rbuf->buf[i]) {
1.19    ! kristaps  194:                        if (pos < MD_LINE) {
1.2       kristaps  195:                                /* LINTED */
1.19    ! kristaps  196:                                rbuf->linebuf[pos++] = rbuf->buf[i];
1.2       kristaps  197:                                continue;
                    198:                        }
                    199:                        warnx("%s: line %zu too long",
1.3       kristaps  200:                                        rbuf->name, rbuf->line);
1.5       kristaps  201:                        return(md_run_leave(args, mbuf, rbuf, -1, p));
1.2       kristaps  202:                }
1.7       kristaps  203:
1.19    ! kristaps  204:                rbuf->linebuf[(int)pos] = 0;
        !           205:                (void)memcpy(line, rbuf->linebuf, sizeof(line));
1.14      kristaps  206:                if ( ! (*fp)(p, line))
1.7       kristaps  207:                        return(md_run_leave(args, mbuf, rbuf, -1, p));
                    208:                rbuf->line++;
                    209:                pos = 0;
1.2       kristaps  210:        }
                    211:
1.7       kristaps  212:        goto again;
                    213:        /* NOTREACHED */
1.3       kristaps  214: }
                    215:
                    216:
                    217: int
                    218: md_run(const struct md_args *args,
                    219:                const struct md_buf *out, const struct md_buf *in)
                    220: {
                    221:        struct md_mbuf   mbuf;
                    222:        struct md_rbuf   rbuf;
1.5       kristaps  223:        void            *data;
1.3       kristaps  224:
                    225:        assert(args);
                    226:        assert(in);
                    227:        assert(out);
                    228:
                    229:        (void)memcpy(&mbuf, out, sizeof(struct md_buf));
                    230:        (void)memcpy(&rbuf, in, sizeof(struct md_buf));
                    231:
                    232:        mbuf.pos = 0;
                    233:        rbuf.line = 1;
                    234:
                    235:        /* Run initialisers. */
                    236:        switch (args->type) {
1.18      kristaps  237:        case (MD_HTML):
                    238:                data = md_init_html(args, &mbuf, &rbuf);
1.3       kristaps  239:                break;
1.9       kristaps  240:        default:
1.16      kristaps  241:                data = md_init_xml(args, &mbuf, &rbuf);
1.3       kristaps  242:                break;
                    243:        }
1.2       kristaps  244:
1.3       kristaps  245:        /* Go into mainline. */
1.5       kristaps  246:        return(md_run_enter(args, &mbuf, &rbuf, data));
1.3       kristaps  247: }

CVSweb