1 /* $Id: mandoc.c,v 1.10 2010/01/05 19:51:10 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
18 #include <sys/types.h>
27 #include "libmandoc.h"
29 static int a2time(time_t *, const char *, const char *);
33 mandoc_special(const char *p
)
35 int terminator
; /* Terminator for \s. */
36 int lim
; /* Limit for N in \s. */
74 if ('\0' == *++p
|| ! isgraph((u_char
)*p
))
90 } else if (*p
== '[') {
95 } else if (*p
== '(') {
102 if (*p
== '+' || *p
== '-') {
114 } else if (*p
== '[') {
121 } else if (*p
== '(') {
130 /* TODO: needs to handle floating point. */
132 if ( ! isdigit((u_char
)*p
))
135 for (i
= 0; isdigit((u_char
)*p
); i
++) {
142 if (terminator
&& terminator
< 3) {
143 if (1 == terminator
&& *p
!= '\'')
145 if (2 == terminator
&& *p
!= ']')
153 if (0 == *++p
|| ! isgraph((u_char
)*p
))
157 if (0 == *++p
|| ! isgraph((u_char
)*p
))
161 for (c
= 3, p
++; *p
&& ']' != *p
; p
++, c
++)
162 if ( ! isgraph((u_char
)*p
))
164 return(*p
== ']' ? c
: 0);
170 if (0 == *++p
|| ! isgraph((u_char
)*p
))
172 if (0 == *++p
|| ! isgraph((u_char
)*p
))
181 for (c
= 3, p
++; *p
&& ']' != *p
; p
++, c
++)
182 if ( ! isgraph((u_char
)*p
))
185 return(*p
== ']' ? c
: 0);
190 mandoc_calloc(size_t num
, size_t size
)
194 ptr
= calloc(num
, size
);
205 mandoc_malloc(size_t size
)
220 mandoc_realloc(void *ptr
, size_t size
)
223 ptr
= realloc(ptr
, size
);
234 mandoc_strdup(const char *ptr
)
249 a2time(time_t *t
, const char *fmt
, const char *p
)
254 memset(&tm
, 0, sizeof(struct tm
));
256 pp
= strptime(p
, fmt
, &tm
);
257 if (NULL
!= pp
&& '\0' == *pp
) {
267 * Convert from a manual date string (see mdoc(7) and man(7)) into a
268 * date according to the stipulated date type.
271 mandoc_a2time(int flags
, const char *p
)
275 if (MTIME_MDOCDATE
& flags
) {
276 if (0 == strcmp(p
, "$" "Mdocdate$"))
278 if (a2time(&t
, "$" "Mdocdate: %b %d %Y $", p
))
282 if (MTIME_CANONICAL
& flags
|| MTIME_REDUCED
& flags
)
283 if (a2time(&t
, "%b %d, %Y", p
))
286 if (MTIME_ISO_8601
& flags
)
287 if (a2time(&t
, "%Y-%m-%d", p
))
290 if (MTIME_REDUCED
& flags
) {
291 if (a2time(&t
, "%d, %Y", p
))
293 if (a2time(&t
, "%Y", p
))