]> git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
Accomodate for groff's crappy behaviour wherein an unrecognised
[mandoc.git] / mandoc.c
1 /* $Id: mandoc.c,v 1.25 2010/07/21 20:35:03 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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 above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "mandoc.h"
31 #include "libmandoc.h"
32
33 static int a2time(time_t *, const char *, const char *);
34
35
36 int
37 mandoc_special(char *p)
38 {
39 int len, i;
40 char term;
41 char *sv;
42
43 len = 0;
44 term = '\0';
45 sv = p;
46
47 assert('\\' == *p);
48 p++;
49
50 switch (*p++) {
51 #if 0
52 case ('Z'):
53 /* FALLTHROUGH */
54 case ('X'):
55 /* FALLTHROUGH */
56 case ('x'):
57 /* FALLTHROUGH */
58 case ('w'):
59 /* FALLTHROUGH */
60 case ('v'):
61 /* FALLTHROUGH */
62 case ('S'):
63 /* FALLTHROUGH */
64 case ('R'):
65 /* FALLTHROUGH */
66 case ('o'):
67 /* FALLTHROUGH */
68 case ('N'):
69 /* FALLTHROUGH */
70 case ('l'):
71 /* FALLTHROUGH */
72 case ('L'):
73 /* FALLTHROUGH */
74 case ('H'):
75 /* FALLTHROUGH */
76 case ('h'):
77 /* FALLTHROUGH */
78 case ('D'):
79 /* FALLTHROUGH */
80 case ('C'):
81 /* FALLTHROUGH */
82 case ('b'):
83 /* FALLTHROUGH */
84 case ('B'):
85 /* FALLTHROUGH */
86 case ('a'):
87 /* FALLTHROUGH */
88 case ('A'):
89 if (*p++ != '\'')
90 return(0);
91 term = '\'';
92 break;
93 #endif
94 case ('s'):
95 if (ASCII_HYPH == *p)
96 *p = '-';
97 if ('+' == *p || '-' == *p)
98 p++;
99
100 i = ('s' != *(p - 1));
101
102 switch (*p++) {
103 case ('('):
104 len = 2;
105 break;
106 case ('['):
107 term = ']';
108 break;
109 case ('\''):
110 term = '\'';
111 break;
112 default:
113 len = 1;
114 p--;
115 break;
116 }
117
118 if (ASCII_HYPH == *p)
119 *p = '-';
120 if ('+' == *p || '-' == *p) {
121 if (i++)
122 return(0);
123 p++;
124 }
125
126 if (0 == i)
127 return(0);
128 break;
129 #if 0
130 case ('Y'):
131 /* FALLTHROUGH */
132 case ('V'):
133 /* FALLTHROUGH */
134 case ('$'):
135 /* FALLTHROUGH */
136 case ('n'):
137 /* FALLTHROUGH */
138 case ('k'):
139 /* FALLTHROUGH */
140 #endif
141 case ('M'):
142 /* FALLTHROUGH */
143 case ('m'):
144 /* FALLTHROUGH */
145 case ('f'):
146 /* FALLTHROUGH */
147 case ('F'):
148 /* FALLTHROUGH */
149 case ('*'):
150 switch (*p++) {
151 case ('('):
152 len = 2;
153 break;
154 case ('['):
155 term = ']';
156 break;
157 default:
158 len = 1;
159 p--;
160 break;
161 }
162 break;
163 case ('('):
164 len = 2;
165 break;
166 case ('['):
167 term = ']';
168 break;
169 default:
170 len = 1;
171 p--;
172 break;
173 }
174
175 if (term) {
176 for ( ; *p && term != *p; p++)
177 if (ASCII_HYPH == *p)
178 *p = '-';
179 return(*p ? (int)(p - sv) : 0);
180 }
181
182 for (i = 0; *p && i < len; i++, p++)
183 if (ASCII_HYPH == *p)
184 *p = '-';
185 return(i == len ? (int)(p - sv) : 0);
186 }
187
188
189 void *
190 mandoc_calloc(size_t num, size_t size)
191 {
192 void *ptr;
193
194 ptr = calloc(num, size);
195 if (NULL == ptr) {
196 perror(NULL);
197 exit(EXIT_FAILURE);
198 }
199
200 return(ptr);
201 }
202
203
204 void *
205 mandoc_malloc(size_t size)
206 {
207 void *ptr;
208
209 ptr = malloc(size);
210 if (NULL == ptr) {
211 perror(NULL);
212 exit(EXIT_FAILURE);
213 }
214
215 return(ptr);
216 }
217
218
219 void *
220 mandoc_realloc(void *ptr, size_t size)
221 {
222
223 ptr = realloc(ptr, size);
224 if (NULL == ptr) {
225 perror(NULL);
226 exit(EXIT_FAILURE);
227 }
228
229 return(ptr);
230 }
231
232
233 char *
234 mandoc_strdup(const char *ptr)
235 {
236 char *p;
237
238 p = strdup(ptr);
239 if (NULL == p) {
240 perror(NULL);
241 exit(EXIT_FAILURE);
242 }
243
244 return(p);
245 }
246
247
248 static int
249 a2time(time_t *t, const char *fmt, const char *p)
250 {
251 struct tm tm;
252 char *pp;
253
254 memset(&tm, 0, sizeof(struct tm));
255
256 pp = strptime(p, fmt, &tm);
257 if (NULL != pp && '\0' == *pp) {
258 *t = mktime(&tm);
259 return(1);
260 }
261
262 return(0);
263 }
264
265
266 /*
267 * Convert from a manual date string (see mdoc(7) and man(7)) into a
268 * date according to the stipulated date type.
269 */
270 time_t
271 mandoc_a2time(int flags, const char *p)
272 {
273 time_t t;
274
275 if (MTIME_MDOCDATE & flags) {
276 if (0 == strcmp(p, "$" "Mdocdate$"))
277 return(time(NULL));
278 if (a2time(&t, "$" "Mdocdate: %b %d %Y $", p))
279 return(t);
280 }
281
282 if (MTIME_CANONICAL & flags || MTIME_REDUCED & flags)
283 if (a2time(&t, "%b %d, %Y", p))
284 return(t);
285
286 if (MTIME_ISO_8601 & flags)
287 if (a2time(&t, "%Y-%m-%d", p))
288 return(t);
289
290 if (MTIME_REDUCED & flags) {
291 if (a2time(&t, "%d, %Y", p))
292 return(t);
293 if (a2time(&t, "%Y", p))
294 return(t);
295 }
296
297 return(0);
298 }
299
300
301 int
302 mandoc_eos(const char *p, size_t sz, int enclosed)
303 {
304 const char *q;
305 int found;
306
307 if (0 == sz)
308 return(0);
309
310 /*
311 * End-of-sentence recognition must include situations where
312 * some symbols, such as `)', allow prior EOS punctuation to
313 * propogate outward.
314 */
315
316 found = 0;
317 for (q = p + (int)sz - 1; q >= p; q--) {
318 switch (*q) {
319 case ('\"'):
320 /* FALLTHROUGH */
321 case ('\''):
322 /* FALLTHROUGH */
323 case (']'):
324 /* FALLTHROUGH */
325 case (')'):
326 if (0 == found)
327 enclosed = 1;
328 break;
329 case ('.'):
330 /* FALLTHROUGH */
331 case ('!'):
332 /* FALLTHROUGH */
333 case ('?'):
334 found = 1;
335 break;
336 default:
337 return(found && (!enclosed || isalnum(*q)));
338 }
339 }
340
341 return(found && !enclosed);
342 }
343
344
345 int
346 mandoc_hyph(const char *start, const char *c)
347 {
348
349 /*
350 * Choose whether to break at a hyphenated character. We only
351 * do this if it's free-standing within a word.
352 */
353
354 /* Skip first/last character of buffer. */
355 if (c == start || '\0' == *(c + 1))
356 return(0);
357 /* Skip first/last character of word. */
358 if ('\t' == *(c + 1) || '\t' == *(c - 1))
359 return(0);
360 if (' ' == *(c + 1) || ' ' == *(c - 1))
361 return(0);
362 /* Skip double invocations. */
363 if ('-' == *(c + 1) || '-' == *(c - 1))
364 return(0);
365 /* Skip escapes. */
366 if ('\\' == *(c - 1))
367 return(0);
368
369 return(1);
370 }