Annotation of mandoc/noop.c, Revision 1.2
1.2 ! kristaps 1: /* $Id: noop.c,v 1.1 2008/12/10 14:42:46 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>
20: #include <err.h>
21: #include <stdlib.h>
1.2 ! kristaps 22: #include <string.h>
1.1 kristaps 23:
24: #include "private.h"
25:
26:
27: struct md_noop {
28: const struct md_args *args;
29: const struct md_rbuf *rbuf;
30: struct rofftree *tree;
31: };
32:
33:
34: static int noop_roffmsg(void *arg,
35: enum roffmsg, const char *,
36: const char *, const char *);
37: static int noop_roffhead(void *, const struct tm *,
38: const char *, const char *,
39: enum roffmsec, enum roffvol);
40: static int noop_rofftail(void *, const struct tm *,
41: const char *, const char *,
42: enum roffmsec, enum roffvol);
43: static int noop_roffin(void *, int,
44: int *, const char **);
45: static int noop_roffdata(void *, int,
46: const char *, const char *);
47: static int noop_roffout(void *, int);
48: static int noop_roffblkin(void *, int, int *,
49: const char **);
50: static int noop_roffblkout(void *, int);
51: static int noop_roffspecial(void *, int,
52: const char *, const int *,
53: const char **, const char **);
54: static int noop_roffblkheadin(void *, int,
55: int *, const char **);
56: static int noop_roffblkheadout(void *, int);
57: static int noop_roffblkbodyin(void *, int,
58: int *, const char **);
59: static int noop_roffblkbodyout(void *, int);
60:
61: #ifdef __linux__
62: extern size_t strlcat(char *, const char *, size_t);
63: extern size_t strlcpy(char *, const char *, size_t);
64: #endif
65:
66:
67: int
68: md_exit_noop(void *data, int flush)
69: {
70: struct md_noop *noop;
71: int c;
72:
73: noop = (struct md_noop *)data;
74: c = roff_free(noop->tree, flush);
75: free(noop);
76: return(c);
77: }
78:
79:
80: int
81: md_line_noop(void *data, char *buf)
82: {
83: struct md_noop *noop;
84:
85: noop = (struct md_noop *)data;
86: return(roff_engine(noop->tree, buf));
87: }
88:
89:
1.2 ! kristaps 90: /* ARGSUSED */
1.1 kristaps 91: void *
92: md_init_noop(const struct md_args *args,
93: struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
94: {
95: struct roffcb cb;
96: struct md_noop *noop;
97:
98: if (NULL == (noop = calloc(1, sizeof(struct md_noop))))
99: err(1, "calloc");
100:
101: noop->args = args;
102: noop->rbuf = rbuf;
103:
104: cb.roffhead = noop_roffhead;
105: cb.rofftail = noop_rofftail;
106: cb.roffin = noop_roffin;
107: cb.roffout = noop_roffout;
108: cb.roffblkin = noop_roffblkin;
109: cb.roffblkheadin = noop_roffblkheadin;
110: cb.roffblkheadout = noop_roffblkheadout;
111: cb.roffblkbodyin = noop_roffblkbodyin;
112: cb.roffblkbodyout = noop_roffblkbodyout;
113: cb.roffblkout = noop_roffblkout;
114: cb.roffspecial = noop_roffspecial;
115: cb.roffmsg = noop_roffmsg;
116: cb.roffdata = noop_roffdata;
117:
118: if (NULL == (noop->tree = roff_alloc(&cb, noop))) {
119: free(noop);
120: return(NULL);
121: }
122: return(noop);
123: }
124:
125:
126: /* ARGSUSED */
127: static int
128: noop_roffhead(void *arg, const struct tm *tm, const char *os,
129: const char *title, enum roffmsec sec, enum roffvol vol)
130: {
131:
132: return(1);
133: }
134:
135:
136: /* ARGSUSED */
137: static int
138: noop_rofftail(void *arg, const struct tm *tm, const char *os,
139: const char *title, enum roffmsec sec, enum roffvol vol)
140: {
141:
142: return(1);
143: }
144:
145:
146: /* ARGSUSED */
147: static int
148: noop_roffspecial(void *arg, int tok, const char *start,
149: const int *argc, const char **argv, const char **more)
150: {
151:
152: return(1);
153: }
154:
155:
156: /* ARGSUSED */
157: static int
158: noop_roffblkin(void *arg, int tok,
159: int *argc, const char **argv)
160: {
161:
162: return(1);
163: }
164:
165:
166: /* ARGSUSED */
167: static int
168: noop_roffblkout(void *arg, int tok)
169: {
170:
171: return(1);
172: }
173:
174:
175: /* ARGSUSED */
176: static int
177: noop_roffblkbodyin(void *arg, int tok,
178: int *argc, const char **argv)
179: {
180:
181: return(1);
182: }
183:
184:
185: /* ARGSUSED */
186: static int
187: noop_roffblkbodyout(void *arg, int tok)
188: {
189:
190: return(1);
191: }
192:
193:
194: /* ARGSUSED */
195: static int
196: noop_roffblkheadin(void *arg, int tok,
197: int *argc, const char **argv)
198: {
199:
200: return(1);
201: }
202:
203:
204: /* ARGSUSED */
205: static int
206: noop_roffblkheadout(void *arg, int tok)
207: {
208:
209: return(1);
210: }
211:
212:
213: /* ARGSUSED */
214: static int
215: noop_roffin(void *arg, int tok, int *argc, const char **argv)
216: {
217:
218: return(1);
219: }
220:
221:
222: /* ARGSUSED */
223: static int
224: noop_roffout(void *arg, int tok)
225: {
226:
227: return(1);
228: }
229:
230:
231: /* ARGSUSED */
232: static int
233: noop_roffdata(void *arg, int tok,
234: const char *start, const char *buf)
235: {
236:
237: return(1);
238: }
239:
240:
241: static int
242: noop_roffmsg(void *arg, enum roffmsg lvl,
243: const char *buf, const char *pos, const char *msg)
244: {
245: struct md_noop *p;
246: char *level;
247: char b[256];
248: int i;
249:
250: p = (struct md_noop *)arg;
251: assert(p);
252:
253: switch (lvl) {
254: case (ROFF_WARN):
255: level = "warning";
256: if ( ! (MD_WARN_ALL & p->args->warnings))
257: return(1);
258: break;
259: case (ROFF_ERROR):
260: level = "error";
261: break;
262: default:
263: abort();
264: /* NOTREACHED */
265: }
266:
267: if (pos) {
268: assert(pos >= buf);
269: if (0 < p->args->verbosity) {
270: (void)snprintf(b, sizeof(b),
271: "%s:%zu: %s: %s\n",
272: p->rbuf->name, p->rbuf->line,
273: level, msg);
274: (void)strlcat(b, "Error at: ", sizeof(b));
275: (void)strlcat(b, p->rbuf->linebuf, sizeof(b));
276:
277: (void)strlcat(b, "\n ", sizeof(b));
278: for (i = 0; i < pos - buf; i++)
279: (void)strlcat(b, " ", sizeof(b));
280: (void)strlcat(b, "^", sizeof(b));
281:
282: } else
283: (void)snprintf(b, sizeof(b),
284: "%s:%zu: %s: %s (col %zu)",
285: p->rbuf->name, p->rbuf->line,
286: level, msg, pos - buf);
287: } else
288: (void)snprintf(b, sizeof(b), "%s: %s: %s",
289: p->rbuf->name, level, msg);
290:
291: (void)fprintf(stderr, "%s\n", b);
292: return(lvl == ROFF_WARN ? 1 : 0);
293: }
294:
CVSweb