1 /* $Id: mandoc.c,v 1.8 2009/11/05 10:16:01 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.
17 #if defined(__linux__) || defined(__MINT__)
18 # define _GNU_SOURCE /* strptime() */
21 #include <sys/types.h>
30 #include "libmandoc.h"
32 static int a2time(time_t *, const char *, const char *);
36 mandoc_special(const char *p)
38 int terminator; /* Terminator for \s. */
39 int lim; /* Limit for N in \s. */
79 if ('\0' == *++p || ! isgraph((u_char)*p))
95 } else if (*p == '[') {
100 } else if (*p == '(') {
107 if (*p == '+' || *p == '-') {
119 } else if (*p == '[') {
126 } else if (*p == '(') {
135 /* TODO: needs to handle floating point. */
137 if ( ! isdigit((u_char)*p))
140 for (i = 0; isdigit((u_char)*p); i++) {
147 if (terminator && terminator < 3) {
148 if (1 == terminator && *p != '\'')
150 if (2 == terminator && *p != ']')
158 if (0 == *++p || ! isgraph((u_char)*p))
162 if (0 == *++p || ! isgraph((u_char)*p))
166 for (c = 3, p++; *p && ']' != *p; p++, c++)
167 if ( ! isgraph((u_char)*p))
169 return(*p == ']' ? c : 0);
175 if (0 == *++p || ! isgraph((u_char)*p))
177 if (0 == *++p || ! isgraph((u_char)*p))
186 for (c = 3, p++; *p && ']' != *p; p++, c++)
187 if ( ! isgraph((u_char)*p))
190 return(*p == ']' ? c : 0);
195 mandoc_calloc(size_t num, size_t size)
199 ptr = calloc(num, size);
210 mandoc_malloc(size_t size)
225 mandoc_realloc(void *ptr, size_t size)
228 ptr = realloc(ptr, size);
239 mandoc_strdup(const char *ptr)
254 a2time(time_t *t, const char *fmt, const char *p)
259 memset(&tm, 0, sizeof(struct tm));
261 pp = strptime(p, fmt, &tm);
262 if (NULL != pp && '\0' == *pp) {
272 * Convert from a manual date string (see mdoc(7) and man(7)) into a
273 * date according to the stipulated date type.
276 mandoc_a2time(int flags, const char *p)
280 if (MTIME_MDOCDATE & flags) {
281 if (0 == strcmp(p, "$" "Mdocdate$"))
283 if (a2time(&t, "$" "Mdocdate: %b %d %Y $", p))
287 if (MTIME_CANONICAL & flags || MTIME_REDUCED & flags)
288 if (a2time(&t, "%b %d, %Y", p))
291 if (MTIME_ISO_8601 & flags)
292 if (a2time(&t, "%Y-%m-%d", p))
295 if (MTIME_REDUCED & flags) {
296 if (a2time(&t, "%d, %Y", p))
298 if (a2time(&t, "%Y", p))