=================================================================== RCS file: /cvs/mandoc/mandoc.c,v retrieving revision 1.36 retrieving revision 1.39 diff -u -p -r1.36 -r1.39 --- mandoc/mandoc.c 2011/01/03 22:42:37 1.36 +++ mandoc/mandoc.c 2011/03/15 16:23:51 1.39 @@ -1,4 +1,4 @@ -/* $Id: mandoc.c,v 1.36 2011/01/03 22:42:37 schwarze Exp $ */ +/* $Id: mandoc.c,v 1.39 2011/03/15 16:23:51 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons * Copyright (c) 2011 Ingo Schwarze @@ -31,9 +31,11 @@ #include "mandoc.h" #include "libmandoc.h" +#define DATESIZE 32 + static int a2time(time_t *, const char *, const char *); +static char *time2a(time_t); - int mandoc_special(char *p) { @@ -352,7 +354,7 @@ mandoc_getarg(char **cpp, mandocmsg msg, void *data, i while (' ' == *cp) cp++; } - *pos += (cp - start) + (quoted ? 1 : 0); + *pos += (int)(cp - start) + (quoted ? 1 : 0); *cpp = cp; if ('\0' == *cp && msg && (white || ' ' == cp[-1])) @@ -380,38 +382,61 @@ 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, *p; + size_t ssz; + 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); + /* + * Reserve space: + * up to 9 characters for the month (September) + blank + * up to 2 characters for the day + comma + blank + * 4 characters for the year and a terminating '\0' + */ + p = buf = mandoc_malloc(10 + 4 + 4 + 1); - if (MTIME_ISO_8601 & flags) - if (a2time(&t, "%Y-%m-%d", p)) - return(t); + if (0 == (ssz = strftime(p, 10 + 1, "%B ", &tm))) + goto fail; + p += (int)ssz; - if (MTIME_REDUCED & flags) { - if (a2time(&t, "%d, %Y", p)) - return(t); - if (a2time(&t, "%Y", p)) - return(t); - } + if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm.tm_mday))) + goto fail; + p += isz; - return(0); + if (0 == strftime(p, 4 + 1, "%Y", &tm)) + goto fail; + return(buf); + +fail: + free(buf); + return(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(out ? out : mandoc_strdup(in)); }