]> git.cameronkatri.com Git - mandoc.git/blob - ml.c
Transition to splitting xml/ml.
[mandoc.git] / ml.c
1 /* $Id: ml.c,v 1.1 2008/12/02 18:26:57 kristaps Exp $ */
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 <stdlib.h>
20 #include <string.h>
21
22 #include "libmdocml.h"
23 #include "private.h"
24
25
26 #define MAXINDENT 8
27
28 static ssize_t ml_puts(struct md_mbuf *, const char *);
29 static ssize_t ml_putchar(struct md_mbuf *, char);
30 static ssize_t ml_putstring(struct md_mbuf *, const char *);
31
32
33 static ssize_t
34 ml_puts(struct md_mbuf *p, const char *buf)
35 {
36
37 return(ml_nputs(p, buf, strlen(buf)));
38 }
39
40
41 static ssize_t
42 ml_putchar(struct md_mbuf *p, char buf)
43 {
44
45 return(ml_nputs(p, &buf, 1));
46 }
47
48
49 static ssize_t
50 ml_putstring(struct md_mbuf *p, const char *buf)
51 {
52
53 return(ml_nputstring(p, buf, strlen(buf)));
54 }
55
56
57 ssize_t
58 ml_begintag(struct md_mbuf *p, const char *name,
59 int *argc, char **argv)
60 {
61 int i;
62 ssize_t res, sz;
63
64 res = 0;
65
66 if (-1 == (sz = ml_nputs(p, "<", 1)))
67 return(-1);
68 res += sz;
69
70 if (-1 == (sz = ml_puts(p, name)))
71 return(-1);
72 res += sz;
73
74 for (i = 0; ROFF_ARGMAX != argc[i]; i++) {
75 if (-1 == (sz = ml_nputs(p, " ", 1)))
76 return(-1);
77 res += sz;
78
79 if (-1 == (sz = ml_puts(p, tokargnames[argc[i]])))
80 return(-1);
81 res += sz;
82
83 if (-1 == (sz = ml_nputs(p, "=\"", 2)))
84 return(-1);
85 res += sz;
86
87 if (-1 == (sz = ml_putstring(p, argv[i] ?
88 argv[i] : "true")))
89 return(-1);
90 res += sz;
91
92 if (-1 == (sz = ml_nputs(p, "\"", 1)))
93 return(-1);
94 res += sz;
95 }
96
97 if (-1 == (sz = ml_nputs(p, ">", 1)))
98 return(-1);
99
100 return(res + sz);
101 }
102
103
104 ssize_t
105 ml_endtag(struct md_mbuf *p, const char *tag)
106 {
107 ssize_t res, sz;
108
109 res = 0;
110
111 if (-1 == (sz = ml_nputs(p, "</", 2)))
112 return(-1);
113 res += sz;
114
115 if (-1 == (sz = ml_puts(p, tag)))
116 return(-1);
117 res += sz;
118
119 if (-1 == (sz = ml_nputs(p, ">", 1)))
120 return(-1);
121
122 return(res + sz);
123 }
124
125
126 ssize_t
127 ml_nputstring(struct md_mbuf *p, const char *buf, size_t bufsz)
128 {
129 int i;
130 ssize_t res, sz;
131
132 res = 0;
133
134 for (i = 0; i < (int)bufsz; i++) {
135 switch (buf[i]) {
136 case ('&'):
137 if (-1 == (sz = ml_nputs(p, "&amp;", 5)))
138 return(-1);
139 break;
140 case ('"'):
141 if (-1 == (sz = ml_nputs(p, "&quot;", 6)))
142 return(-1);
143 break;
144 case ('<'):
145 if (-1 == (sz = ml_nputs(p, "&lt;", 4)))
146 return(-1);
147 break;
148 case ('>'):
149 if (-1 == (sz = ml_nputs(p, "&gt;", 4)))
150 return(-1);
151 break;
152 default:
153 if (-1 == (sz = ml_putchar(p, buf[i])))
154 return(-1);
155 break;
156 }
157 res += sz;
158 }
159 return(res);
160 }
161
162
163 ssize_t
164 ml_nputs(struct md_mbuf *p, const char *buf, size_t sz)
165 {
166
167 return(0 == md_buf_puts(p, buf, sz) ? -1 : (ssize_t)sz);
168 }
169
170
171 ssize_t
172 ml_indent(struct md_mbuf *p, int indent)
173 {
174 size_t i;
175 ssize_t res, sz;
176
177 res = sz 0;
178
179 /* LINTED */
180 for (i = 0; i < MIN(indent, MAXINDENT); i++, res += sz)
181 if (-1 == (sz = ml_nputs(p, " ", 4)))
182 return(-1);
183 return(res);
184 }