]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.19 2010/06/19 20:46:28 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
21 #include <sys/types.h>
31 #include "libmandoc.h"
33 static int a2time(time_t *, const char *, const char *);
34 static int spec_norm(char *, int);
38 * "Normalise" a special string by converting its ASCII_HYPH entries
39 * into actual hyphens.
42 spec_norm(char *p
, int sz
)
46 for (i
= 0; i
< sz
; i
++)
47 if (ASCII_HYPH
== p
[i
])
55 mandoc_special(char *p
)
57 int terminator
; /* Terminator for \s. */
58 int lim
; /* Limit for N in \s. */
65 return(spec_norm(sv
, 0));
101 return(spec_norm(sv
, 2));
104 return(spec_norm(sv
, 2));
115 } else if (*p
== '[') {
120 } else if (*p
== '(') {
127 if (*p
== '+' || *p
== '-') {
134 return(spec_norm(sv
, 0));
139 } else if (*p
== '[') {
141 return(spec_norm(sv
, 0));
146 } else if (*p
== '(') {
148 return(spec_norm(sv
, 0));
155 /* TODO: needs to handle floating point. */
157 if ( ! isdigit((u_char
)*p
))
158 return(spec_norm(sv
, 0));
160 for (i
= 0; isdigit((u_char
)*p
); i
++) {
167 if (terminator
&& terminator
< 3) {
168 if (1 == terminator
&& *p
!= '\'')
169 return(spec_norm(sv
, 0));
170 if (2 == terminator
&& *p
!= ']')
171 return(spec_norm(sv
, 0));
176 return(spec_norm(sv
, c
));
182 if ('\0' == *++p
|| isspace((u_char
)*p
))
183 return(spec_norm(sv
, 0));
186 if ('\0' == *++p
|| isspace((u_char
)*p
))
187 return(spec_norm(sv
, 0));
188 return(spec_norm(sv
, 4));
190 for (c
= 3, p
++; *p
&& ']' != *p
; p
++, c
++)
191 if (isspace((u_char
)*p
))
193 return(spec_norm(sv
, *p
== ']' ? c
: 0));
197 return(spec_norm(sv
, 3));
199 if ('\0' == *++p
|| isspace((u_char
)*p
))
200 return(spec_norm(sv
, 0));
201 if ('\0' == *++p
|| isspace((u_char
)*p
))
202 return(spec_norm(sv
, 0));
203 return(spec_norm(sv
, 4));
207 return(spec_norm(sv
, 0));
210 for (c
= 3, p
++; *p
&& ']' != *p
; p
++, c
++)
211 if (isspace((u_char
)*p
))
214 return(spec_norm(sv
, *p
== ']' ? c
: 0));
219 mandoc_calloc(size_t num
, size_t size
)
223 ptr
= calloc(num
, size
);
234 mandoc_malloc(size_t size
)
249 mandoc_realloc(void *ptr
, size_t size
)
252 ptr
= realloc(ptr
, size
);
263 mandoc_strdup(const char *ptr
)
278 a2time(time_t *t
, const char *fmt
, const char *p
)
283 memset(&tm
, 0, sizeof(struct tm
));
285 pp
= strptime(p
, fmt
, &tm
);
286 if (NULL
!= pp
&& '\0' == *pp
) {
296 * Convert from a manual date string (see mdoc(7) and man(7)) into a
297 * date according to the stipulated date type.
300 mandoc_a2time(int flags
, const char *p
)
304 if (MTIME_MDOCDATE
& flags
) {
305 if (0 == strcmp(p
, "$" "Mdocdate$"))
307 if (a2time(&t
, "$" "Mdocdate: %b %d %Y $", p
))
311 if (MTIME_CANONICAL
& flags
|| MTIME_REDUCED
& flags
)
312 if (a2time(&t
, "%b %d, %Y", p
))
315 if (MTIME_ISO_8601
& flags
)
316 if (a2time(&t
, "%Y-%m-%d", p
))
319 if (MTIME_REDUCED
& flags
) {
320 if (a2time(&t
, "%d, %Y", p
))
322 if (a2time(&t
, "%Y", p
))
331 mandoc_eos(const char *p
, size_t sz
)
338 * End-of-sentence recognition must include situations where
339 * some symbols, such as `)', allow prior EOS punctuation to
344 switch (p
[(int)sz
- 1]) {
354 /* Escaped periods. */
355 if (sz
> 1 && '\\' == p
[(int)sz
- 2])
372 mandoc_hyph(const char *start
, const char *c
)
376 * Choose whether to break at a hyphenated character. We only
377 * do this if it's free-standing within a word.
380 /* Skip first/last character of buffer. */
381 if (c
== start
|| '\0' == *(c
+ 1))
383 /* Skip first/last character of word. */
384 if ('\t' == *(c
+ 1) || '\t' == *(c
- 1))
386 if (' ' == *(c
+ 1) || ' ' == *(c
- 1))
388 /* Skip double invocations. */
389 if ('-' == *(c
+ 1) || '-' == *(c
- 1))
392 if ('\\' == *(c
- 1))