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

Annotation of mandoc/dummy.c, Revision 1.4

1.4     ! kristaps    1: /* $Id: dummy.c,v 1.3 2008/11/24 14:24:55 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 <assert.h>
1.3       kristaps   20: #include <err.h>
1.4     ! kristaps   21: #include <stdio.h>
1.1       kristaps   22: #include <stdlib.h>
1.4     ! kristaps   23: #include <string.h>
1.1       kristaps   24:
                     25: #include "libmdocml.h"
                     26: #include "private.h"
                     27:
                     28:
1.4     ! kristaps   29: static roffin          in[ROFF_MAX];
        !            30: static roffout         out[ROFF_MAX];
        !            31: static roffblkin       blkin[ROFF_MAX];
        !            32: static roffblkout      blkout[ROFF_MAX];
        !            33:
        !            34: static int             md_dummy_blk_in(int);
        !            35: static int             md_dummy_blk_out(int);
        !            36: static int             md_dummy_text_in(int, int *, char **);
        !            37: static int             md_dummy_text_out(int);
        !            38:
        !            39: static void            dbg_indent(void);
        !            40:
        !            41: static int             dbg_lvl = 0;
        !            42:
1.3       kristaps   43: struct md_dummy {
                     44:        struct rofftree *tree;
                     45: };
                     46:
                     47:
1.4     ! kristaps   48: static void
        !            49: dbg_indent(void)
        !            50: {
        !            51:        char            buf[128];
        !            52:        int             i;
        !            53:
        !            54:        *buf = 0;
        !            55:        assert(dbg_lvl >= 0);
        !            56:        for (i = 0; i < dbg_lvl; i++)
        !            57:                (void)strncat(buf, "  ", sizeof(buf) - 1);
        !            58:
        !            59:        (void)printf("%s", buf);
        !            60: }
        !            61:
        !            62:
        !            63: static int
        !            64: md_dummy_blk_in(int tok)
        !            65: {
        !            66:
        !            67:        dbg_indent();
        !            68:        (void)printf("+++blk\n");
        !            69:        dbg_lvl++;
        !            70:        return(1);
        !            71: }
        !            72:
        !            73:
        !            74: static int
        !            75: md_dummy_blk_out(int tok)
        !            76: {
        !            77:
        !            78:        assert(dbg_lvl > 0);
        !            79:        dbg_lvl--;
        !            80:        dbg_indent();
        !            81:        (void)printf("---blk\n");
        !            82:        return(1);
        !            83: }
        !            84:
        !            85:
        !            86: static int
        !            87: md_dummy_text_in(int tok, int *argcp, char **argvp)
        !            88: {
        !            89:
        !            90:        dbg_indent();
        !            91:        (void)printf("in: text\n");
        !            92:        return(1);
        !            93: }
        !            94:
        !            95:
        !            96: static int
        !            97: md_dummy_text_out(int tok)
        !            98: {
        !            99:
        !           100:        dbg_indent();
        !           101:        (void)printf("out: text\n");
        !           102:        return(1);
        !           103: }
        !           104:
        !           105:
1.3       kristaps  106: int
                    107: md_line_dummy(void *arg, char *buf, size_t sz)
                    108: {
                    109:        struct md_dummy *p;
                    110:
                    111:        p = (struct md_dummy *)arg;
                    112:        return(roff_engine(p->tree, buf, sz));
                    113: }
                    114:
                    115:
1.1       kristaps  116: int
1.3       kristaps  117: md_exit_dummy(void *data, int flush)
                    118: {
                    119:        int              c;
                    120:        struct md_dummy *p;
                    121:
                    122:        p = (struct md_dummy *)data;
                    123:        c = roff_free(p->tree, flush);
                    124:        free(p);
                    125:
                    126:        return(c);
                    127: }
                    128:
                    129:
                    130: void *
                    131: md_init_dummy(const struct md_args *args,
                    132:                struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
1.1       kristaps  133: {
1.3       kristaps  134:        struct md_dummy *p;
1.4     ! kristaps  135:        int              i;
        !           136:
        !           137:        for (i = 0; i < ROFF_MAX; i++) {
        !           138:                in[i] = md_dummy_text_in;
        !           139:                out[i] = md_dummy_text_out;
        !           140:                blkin[i] = md_dummy_blk_in;
        !           141:                blkout[i] = md_dummy_blk_out;
        !           142:        }
1.1       kristaps  143:
1.3       kristaps  144:        if (NULL == (p = malloc(sizeof(struct md_dummy)))) {
                    145:                warn("malloc");
                    146:                return(NULL);
                    147:        }
                    148:
1.4     ! kristaps  149:        p->tree = roff_alloc
        !           150:                (args, mbuf, rbuf, in, out, blkin, blkout);
        !           151:
        !           152:        if (NULL == p->tree) {
1.3       kristaps  153:                free(p);
                    154:                return(NULL);
                    155:        }
1.1       kristaps  156:
1.3       kristaps  157:        return(p);
1.1       kristaps  158: }

CVSweb