]> git.cameronkatri.com Git - mandoc.git/blob - ml.c
*** empty log message ***
[mandoc.git] / ml.c
1 /* $Id: ml.c,v 1.7 2008/12/05 19:45:15 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 <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "libmdocml.h"
24 #include "private.h"
25 #include "ml.h"
26
27 #ifdef __linux__
28 extern size_t strlcat(char *, const char *, size_t);
29 extern size_t strlcpy(char *, const char *, size_t);
30 #endif
31
32
33 int
34 ml_putstring(struct md_mbuf *p, const char *buf, size_t *pos)
35 {
36
37 return(ml_nputstring(p, buf, strlen(buf), pos));
38 }
39
40
41 int
42 ml_nputstring(struct md_mbuf *p,
43 const char *buf, size_t sz, size_t *pos)
44 {
45 int i, v;
46 const char *seq;
47 size_t ssz;
48
49 for (i = 0; i < (int)sz; i++) {
50 switch (buf[i]) {
51
52 /* Escaped value. */
53 case ('\\'):
54 if (-1 == (v = rofftok_scan(buf, &i)))
55 return(0);
56
57 switch (v) {
58 case (ROFFTok_Sp_A):
59 seq = "\\a";
60 ssz = 2;
61 break;
62 case (ROFFTok_Sp_B):
63 seq = "\\b";
64 ssz = 2;
65 break;
66 case (ROFFTok_Sp_F):
67 seq = "\\f";
68 ssz = 2;
69 break;
70 case (ROFFTok_Sp_N):
71 seq = "\\n";
72 ssz = 2;
73 break;
74 case (ROFFTok_Sp_R):
75 seq = "\\r";
76 ssz = 2;
77 break;
78 case (ROFFTok_Sp_T):
79 seq = "\\t";
80 ssz = 2;
81 break;
82 case (ROFFTok_Sp_V):
83 seq = "\\v";
84 ssz = 2;
85 break;
86 case (ROFFTok_Sp_0):
87 seq = "\\0";
88 ssz = 2;
89 break;
90 case (ROFFTok_Space):
91 seq = "&nbsp;";
92 ssz = 6;
93 break;
94 case (ROFFTok_Hyphen):
95 seq = "&#8208;";
96 ssz = 7;
97 break;
98 case (ROFFTok_Em):
99 seq = "&#8212;";
100 ssz = 7;
101 break;
102 case (ROFFTok_En):
103 seq = "&#8211;";
104 ssz = 7;
105 break;
106 case (ROFFTok_Ge):
107 seq = "&#8805;";
108 ssz = 7;
109 break;
110 case (ROFFTok_Le):
111 seq = "&#8804;";
112 ssz = 7;
113 break;
114 case (ROFFTok_Rquote):
115 seq = "&#8221;";
116 ssz = 7;
117 break;
118 case (ROFFTok_Lquote):
119 seq = "&#8220;";
120 ssz = 7;
121 break;
122 case (ROFFTok_Uparrow):
123 seq = "&#8593;";
124 ssz = 7;
125 break;
126 case (ROFFTok_Acute):
127 seq = "&#180;";
128 ssz = 6;
129 break;
130 case (ROFFTok_Grave):
131 seq = "&#96;";
132 ssz = 5;
133 break;
134 case (ROFFTok_Pi):
135 seq = "&#960;";
136 ssz = 6;
137 break;
138 case (ROFFTok_Ne):
139 seq = "&#8800;";
140 ssz = 7;
141 break;
142 case (ROFFTok_Lt):
143 seq = "&lt;";
144 ssz = 4;
145 break;
146 case (ROFFTok_Gt):
147 seq = "&gt;";
148 ssz = 4;
149 break;
150 case (ROFFTok_Plusmin):
151 seq = "&#177;";
152 ssz = 6;
153 break;
154 case (ROFFTok_Infty):
155 seq = "&#8734;";
156 ssz = 7;
157 break;
158 case (ROFFTok_Bar):
159 seq = "&#124;";
160 ssz = 6;
161 break;
162 case (ROFFTok_Nan):
163 seq = "Nan";
164 ssz = 3;
165 break;
166 case (ROFFTok_Quote):
167 seq = "&quot;";
168 ssz = 6;
169 break;
170 case (ROFFTok_Slash):
171 seq = "\\";
172 ssz = 1;
173 break;
174 case (ROFFTok_Null):
175 seq = "";
176 ssz = 0;
177 break;
178 default:
179 return(0);
180 }
181 break;
182
183 /* Ampersand ml-escape. */
184 case ('&'):
185 seq = "&amp;";
186 ssz = 5;
187 break;
188
189 /* Quotation ml-escape. */
190 case ('"'):
191 seq = "&quot;";
192 ssz = 6;
193 break;
194
195 /* Lt ml-escape. */
196 case ('<'):
197 seq = "&lt;";
198 ssz = 4;
199 break;
200
201 /* Gt ml-escape. */
202 case ('>'):
203 seq = "&gt;";
204 ssz = 4;
205 break;
206
207 default:
208 seq = &buf[i];
209 ssz = 1;
210 break;
211 }
212
213 if (ssz > 0 && ! ml_nputs(p, seq, ssz, pos))
214 return(-1);
215 }
216 return(1);
217 }
218
219
220 int
221 ml_nputs(struct md_mbuf *p, const char *buf, size_t sz, size_t *pos)
222 {
223
224 if (0 == sz)
225 return(1);
226
227 if ( ! md_buf_puts(p, buf, sz))
228 return(0);
229
230 if (pos)
231 *pos += sz;
232 return(1);
233 }
234
235
236 int
237 ml_puts(struct md_mbuf *p, const char *buf, size_t *pos)
238 {
239 size_t sz;
240
241 if (0 == (sz = strlen(buf)))
242 return(1);
243
244 if ( ! md_buf_puts(p, buf, sz))
245 return(0);
246
247 if (pos)
248 *pos += sz;
249 return(1);
250 }
251
252
253 int
254 ml_putchars(struct md_mbuf *p, char buf, size_t count, size_t *pos)
255 {
256 size_t i;
257
258 for (i = 0; i < count; i++)
259 if ( ! ml_nputs(p, &buf, 1, pos))
260 return(0);
261
262 return(1);
263 }