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

Annotation of mandoc/dummy.c, Revision 1.6

1.6     ! kristaps    1: /* $Id: dummy.c,v 1.5 2008/11/25 12:14:02 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.1       kristaps   36:
1.6     ! kristaps   37: static void            dbg_prologue(const char *);
        !            38: static void            dbg_epilogue(void);
1.4       kristaps   39:
1.5       kristaps   40: static int             dbg_lvl = 0;
1.6     ! kristaps   41: static char            dbg_line[72];
1.4       kristaps   42:
1.3       kristaps   43: struct md_dummy {
                     44:        struct rofftree *tree;
                     45: };
                     46:
1.4       kristaps   47: static void
1.6     ! kristaps   48: dbg_prologue(const char *p)
1.4       kristaps   49: {
1.6     ! kristaps   50:        int              i;
1.5       kristaps   51:
1.6     ! kristaps   52:        (void)snprintf(dbg_line, sizeof(dbg_line) - 1, "%6s", p);
        !            53:        (void)strlcat(dbg_line, ": ", sizeof(dbg_line) - 1);
1.5       kristaps   54:        /* LINTED */
1.4       kristaps   55:        for (i = 0; i < dbg_lvl; i++)
1.6     ! kristaps   56:                (void)strlcat(dbg_line, "    ", sizeof(dbg_line) - 1);
        !            57: }
        !            58:
1.4       kristaps   59:
1.6     ! kristaps   60: static void
        !            61: dbg_epilogue(void)
        !            62: {
        !            63:
        !            64:        assert(0 != dbg_line[0]);
        !            65:        (void)printf("%s\n", dbg_line);
1.4       kristaps   66: }
                     67:
                     68:
                     69: static int
                     70: md_dummy_blk_in(int tok)
                     71: {
                     72:
1.6     ! kristaps   73:        dbg_prologue("blk");
        !            74:        (void)strlcat(dbg_line, toknames[tok], sizeof(dbg_line) - 1);
        !            75:        dbg_epilogue();
        !            76:
1.4       kristaps   77:        dbg_lvl++;
                     78:        return(1);
                     79: }
                     80:
                     81:
                     82: static int
                     83: md_dummy_blk_out(int tok)
                     84: {
                     85:
                     86:        dbg_lvl--;
                     87:        return(1);
                     88: }
                     89:
                     90:
1.5       kristaps   91: /* ARGSUSED */
1.4       kristaps   92: static int
                     93: md_dummy_text_in(int tok, int *argcp, char **argvp)
                     94: {
                     95:
1.6     ! kristaps   96:        dbg_prologue("text");
        !            97:        (void)strlcat(dbg_line, toknames[tok], sizeof(dbg_line) - 1);
        !            98:        (void)strlcat(dbg_line, " ", sizeof(dbg_line) - 1);
        !            99:        while (ROFF_ARGMAX != *argcp) {
        !           100:                (void)strlcat(dbg_line, "[", sizeof(dbg_line) - 1);
        !           101:                (void)strlcat(dbg_line, tokargnames[*argcp],
        !           102:                                sizeof(dbg_line) - 1);
        !           103:                if (*argvp) {
        !           104:                        (void)strlcat(dbg_line, " [",
        !           105:                                        sizeof(dbg_line) - 1);
        !           106:                        (void)strlcat(dbg_line, *argvp,
        !           107:                                        sizeof(dbg_line) - 1);
        !           108:                        (void)strlcat(dbg_line, "]",
        !           109:                                        sizeof(dbg_line) - 1);
        !           110:                }
        !           111:                (void)strlcat(dbg_line, "]", sizeof(dbg_line) - 1);
        !           112:                argcp++;
        !           113:                argvp++;
        !           114:        }
        !           115:        dbg_epilogue();
1.4       kristaps  116:        return(1);
                    117: }
                    118:
                    119:
                    120: static int
                    121: md_dummy_text_out(int tok)
                    122: {
                    123:
                    124:        return(1);
                    125: }
                    126:
                    127:
1.3       kristaps  128: int
                    129: md_line_dummy(void *arg, char *buf, size_t sz)
                    130: {
                    131:        struct md_dummy *p;
                    132:
                    133:        p = (struct md_dummy *)arg;
                    134:        return(roff_engine(p->tree, buf, sz));
                    135: }
                    136:
                    137:
1.1       kristaps  138: int
1.3       kristaps  139: md_exit_dummy(void *data, int flush)
                    140: {
                    141:        int              c;
                    142:        struct md_dummy *p;
                    143:
                    144:        p = (struct md_dummy *)data;
                    145:        c = roff_free(p->tree, flush);
                    146:        free(p);
                    147:
                    148:        return(c);
                    149: }
                    150:
                    151:
                    152: void *
                    153: md_init_dummy(const struct md_args *args,
                    154:                struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
1.1       kristaps  155: {
1.3       kristaps  156:        struct md_dummy *p;
1.1       kristaps  157:
1.3       kristaps  158:        if (NULL == (p = malloc(sizeof(struct md_dummy)))) {
                    159:                warn("malloc");
                    160:                return(NULL);
                    161:        }
                    162:
1.5       kristaps  163:        p->tree = roff_alloc(args, mbuf, rbuf,
                    164:                        md_dummy_text_in, md_dummy_text_out,
                    165:                        md_dummy_blk_in, md_dummy_blk_out);
1.4       kristaps  166:
                    167:        if (NULL == p->tree) {
1.3       kristaps  168:                free(p);
                    169:                return(NULL);
                    170:        }
1.1       kristaps  171:
1.3       kristaps  172:        return(p);
1.1       kristaps  173: }

CVSweb