aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mandoc.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2011-03-07 01:35:51 +0000
committerIngo Schwarze <schwarze@openbsd.org>2011-03-07 01:35:51 +0000
commit04cc425ad5987ba914232718ff8bbd5ef418b4d6 (patch)
treefffb227f8453437d1a80c4109593664bef06d4f4 /mandoc.c
parent130d794d1e169870967f8cbb047c5a35891cbfb7 (diff)
downloadmandoc-04cc425ad5987ba914232718ff8bbd5ef418b4d6.tar.gz
mandoc-04cc425ad5987ba914232718ff8bbd5ef418b4d6.tar.zst
mandoc-04cc425ad5987ba914232718ff8bbd5ef418b4d6.zip
Clean up date handling,
as a first step to get rid of the frequent petty warnings in this area: - always store dates as strings, not as seconds since the Epoch - for input, try the three most common formats everywhere - for unrecognized format, just pass the date though verbatim - when there is no date at all, still use the current date Originally triggered by a one-line patch from Tim van der Molen, <tbvdm at xs4all dot nl>, which is included here. Feedback and OK on manual parts from jmc@. "please check this in" kristaps@
Diffstat (limited to 'mandoc.c')
-rw-r--r--mandoc.c75
1 files changed, 47 insertions, 28 deletions
diff --git a/mandoc.c b/mandoc.c
index 4faa5a78..5cebd7e2 100644
--- a/mandoc.c
+++ b/mandoc.c
@@ -1,4 +1,4 @@
-/* $Id: mandoc.c,v 1.36 2011/01/03 22:42:37 schwarze Exp $ */
+/* $Id: mandoc.c,v 1.37 2011/03/07 01:35:51 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
@@ -31,8 +31,10 @@
#include "mandoc.h"
#include "libmandoc.h"
-static int a2time(time_t *, const char *, const char *);
+#define DATESIZE 32
+static int a2time(time_t *, const char *, const char *);
+static char *time2a(time_t);
int
mandoc_special(char *p)
@@ -380,38 +382,55 @@ a2time(time_t *t, const char *fmt, const char *p)
}
-/*
- * Convert from a manual date string (see mdoc(7) and man(7)) into a
- * date according to the stipulated date type.
- */
-time_t
-mandoc_a2time(int flags, const char *p)
+static char *
+time2a(time_t t)
{
- time_t t;
+ struct tm tm;
+ char buf[DATESIZE];
+ char *p;
+ size_t nsz, rsz;
+ int isz;
- if (MTIME_MDOCDATE & flags) {
- if (0 == strcmp(p, "$" "Mdocdate$"))
- return(time(NULL));
- if (a2time(&t, "$" "Mdocdate: %b %d %Y $", p))
- return(t);
- }
+ localtime_r(&t, &tm);
- if (MTIME_CANONICAL & flags || MTIME_REDUCED & flags)
- if (a2time(&t, "%b %d, %Y", p))
- return(t);
+ p = buf;
+ rsz = DATESIZE;
- if (MTIME_ISO_8601 & flags)
- if (a2time(&t, "%Y-%m-%d", p))
- return(t);
+ if (0 == (nsz = strftime(p, rsz, "%B ", &tm)))
+ return(NULL);
- if (MTIME_REDUCED & flags) {
- if (a2time(&t, "%d, %Y", p))
- return(t);
- if (a2time(&t, "%Y", p))
- return(t);
- }
+ p += (int)nsz;
+ rsz -= nsz;
- return(0);
+ if (-1 == (isz = snprintf(p, rsz, "%d, ", tm.tm_mday)))
+ return(NULL);
+
+ p += isz;
+ rsz -= isz;
+
+ return(strftime(p, rsz, "%Y", &tm) ? buf : NULL);
+}
+
+
+char *
+mandoc_normdate(char *in, mandocmsg msg, void *data, int ln, int pos)
+{
+ char *out;
+ time_t t;
+
+ if (NULL == in || '\0' == *in ||
+ 0 == strcmp(in, "$" "Mdocdate$")) {
+ (*msg)(MANDOCERR_NODATE, data, ln, pos, NULL);
+ time(&t);
+ }
+ else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
+ !a2time(&t, "%b %d, %Y", in) &&
+ !a2time(&t, "%Y-%m-%d", in)) {
+ (*msg)(MANDOCERR_BADDATE, data, ln, pos, NULL);
+ t = 0;
+ }
+ out = t ? time2a(t) : NULL;
+ return(mandoc_strdup(out ? out : in));
}