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

Annotation of mandoc/libmdocml.c, Revision 1.20

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

CVSweb