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

Annotation of mandoc/libmdocml.c, Revision 1.14

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

CVSweb