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