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