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