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