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

Annotation of mandoc/dummy.c, Revision 1.9

1.9     ! kristaps    1: /* $Id: dummy.c,v 1.8 2008/11/27 14:02:41 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:
1.6       kristaps   28: #ifdef __linux__
1.5       kristaps   29: #define        strlcat         strncat
                     30: #endif
                     31:
                     32: static int             md_dummy_blk_in(int);
                     33: static int             md_dummy_blk_out(int);
                     34: static         int             md_dummy_text_in(int, int *, char **);
                     35: static int             md_dummy_text_out(int);
1.8       kristaps   36: static int             md_dummy_special(int);
                     37: static int             md_dummy_head(void);
                     38: static int             md_dummy_tail(void);
1.9     ! kristaps   39: static void            md_dummy_msg(const struct md_args *,
        !            40:                                enum roffmsg, const char *,
        !            41:                                const char *, const char *,
        !            42:                                int, char *);
1.1       kristaps   43:
1.6       kristaps   44: static void            dbg_prologue(const char *);
                     45: static void            dbg_epilogue(void);
1.4       kristaps   46:
1.5       kristaps   47: static int             dbg_lvl = 0;
1.6       kristaps   48: static char            dbg_line[72];
1.4       kristaps   49:
1.3       kristaps   50: struct md_dummy {
                     51:        struct rofftree *tree;
1.7       kristaps   52:        struct roffcb    cb;
1.3       kristaps   53: };
                     54:
1.8       kristaps   55:
                     56: int
                     57: md_line_dummy(void *arg, char *buf, size_t sz)
                     58: {
                     59:        struct md_dummy *p;
                     60:
                     61:        p = (struct md_dummy *)arg;
                     62:        return(roff_engine(p->tree, buf, sz));
                     63: }
                     64:
                     65:
                     66: int
                     67: md_exit_dummy(void *data, int flush)
                     68: {
                     69:        int              c;
                     70:        struct md_dummy *p;
                     71:
                     72:        p = (struct md_dummy *)data;
                     73:        c = roff_free(p->tree, flush);
                     74:        free(p);
                     75:
                     76:        return(c);
                     77: }
                     78:
                     79:
                     80: void *
                     81: md_init_dummy(const struct md_args *args,
                     82:                struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
                     83: {
                     84:        struct md_dummy *p;
                     85:
                     86:        if (NULL == (p = malloc(sizeof(struct md_dummy)))) {
                     87:                warn("malloc");
                     88:                return(NULL);
                     89:        }
                     90:
                     91:        p->cb.roffhead = md_dummy_head;
                     92:        p->cb.rofftail = md_dummy_tail;
                     93:        p->cb.roffin = md_dummy_text_in;
                     94:        p->cb.roffout = md_dummy_text_out;
                     95:        p->cb.roffblkin = md_dummy_blk_in;
                     96:        p->cb.roffblkout = md_dummy_blk_out;
                     97:        p->cb.roffspecial = md_dummy_special;
1.9     ! kristaps   98:        p->cb.roffmsg = md_dummy_msg;
1.8       kristaps   99:
                    100:        p->tree = roff_alloc(args, mbuf, rbuf, &p->cb);
                    101:
                    102:        if (NULL == p->tree) {
                    103:                free(p);
                    104:                return(NULL);
                    105:        }
                    106:
                    107:        return(p);
                    108: }
                    109:
                    110:
1.4       kristaps  111: static void
1.6       kristaps  112: dbg_prologue(const char *p)
1.4       kristaps  113: {
1.6       kristaps  114:        int              i;
1.5       kristaps  115:
1.6       kristaps  116:        (void)snprintf(dbg_line, sizeof(dbg_line) - 1, "%6s", p);
                    117:        (void)strlcat(dbg_line, ": ", sizeof(dbg_line) - 1);
1.5       kristaps  118:        /* LINTED */
1.4       kristaps  119:        for (i = 0; i < dbg_lvl; i++)
1.6       kristaps  120:                (void)strlcat(dbg_line, "    ", sizeof(dbg_line) - 1);
                    121: }
                    122:
1.4       kristaps  123:
1.6       kristaps  124: static void
                    125: dbg_epilogue(void)
                    126: {
                    127:
                    128:        assert(0 != dbg_line[0]);
                    129:        (void)printf("%s\n", dbg_line);
1.4       kristaps  130: }
                    131:
                    132:
                    133: static int
1.8       kristaps  134: md_dummy_head(void)
                    135: {
                    136:
                    137:        return(1);
                    138: }
                    139:
                    140:
                    141: static int
                    142: md_dummy_tail(void)
                    143: {
                    144:
                    145:        return(1);
                    146: }
                    147:
                    148:
                    149: static int
                    150: md_dummy_special(int tok)
                    151: {
                    152:
                    153:        dbg_prologue("noop");
                    154:        (void)strlcat(dbg_line, toknames[tok], sizeof(dbg_line) - 1);
                    155:        dbg_epilogue();
                    156:
                    157:        return(1);
                    158: }
                    159:
                    160:
                    161: static int
1.4       kristaps  162: md_dummy_blk_in(int tok)
                    163: {
                    164:
1.6       kristaps  165:        dbg_prologue("blk");
                    166:        (void)strlcat(dbg_line, toknames[tok], sizeof(dbg_line) - 1);
                    167:        dbg_epilogue();
                    168:
1.4       kristaps  169:        dbg_lvl++;
                    170:        return(1);
                    171: }
                    172:
                    173:
                    174: static int
                    175: md_dummy_blk_out(int tok)
                    176: {
                    177:
                    178:        dbg_lvl--;
                    179:        return(1);
                    180: }
                    181:
                    182:
1.5       kristaps  183: /* ARGSUSED */
1.4       kristaps  184: static int
                    185: md_dummy_text_in(int tok, int *argcp, char **argvp)
                    186: {
                    187:
1.6       kristaps  188:        dbg_prologue("text");
                    189:        (void)strlcat(dbg_line, toknames[tok], sizeof(dbg_line) - 1);
                    190:        (void)strlcat(dbg_line, " ", sizeof(dbg_line) - 1);
                    191:        while (ROFF_ARGMAX != *argcp) {
                    192:                (void)strlcat(dbg_line, "[", sizeof(dbg_line) - 1);
                    193:                (void)strlcat(dbg_line, tokargnames[*argcp],
                    194:                                sizeof(dbg_line) - 1);
                    195:                if (*argvp) {
                    196:                        (void)strlcat(dbg_line, " [",
                    197:                                        sizeof(dbg_line) - 1);
                    198:                        (void)strlcat(dbg_line, *argvp,
                    199:                                        sizeof(dbg_line) - 1);
                    200:                        (void)strlcat(dbg_line, "]",
                    201:                                        sizeof(dbg_line) - 1);
                    202:                }
                    203:                (void)strlcat(dbg_line, "]", sizeof(dbg_line) - 1);
                    204:                argcp++;
                    205:                argvp++;
                    206:        }
                    207:        dbg_epilogue();
1.4       kristaps  208:        return(1);
                    209: }
                    210:
                    211:
                    212: static int
                    213: md_dummy_text_out(int tok)
                    214: {
                    215:
                    216:        return(1);
                    217: }
1.9     ! kristaps  218:
        !           219:
        !           220: static void
        !           221: md_dummy_msg(const struct md_args *args, enum roffmsg lvl,
        !           222:                const char *buf, const char *pos,
        !           223:                const char *name, int line, char *msg)
        !           224: {
        !           225:        char            *p;
        !           226:
        !           227:        switch (lvl) {
        !           228:        case (ROFF_WARN):
        !           229:                p = "warning";
        !           230:                break;
        !           231:        case (ROFF_ERROR):
        !           232:                p = "error";
        !           233:                break;
        !           234:        }
        !           235:
        !           236:        assert(pos >= buf);
        !           237:        (void)fprintf(stderr, "%s:%d: %s: %s\n", name, line, p, msg);
        !           238: }

CVSweb