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