=================================================================== RCS file: /cvs/mandoc/mandoc.c,v retrieving revision 1.96 retrieving revision 1.105 diff -u -p -r1.96 -r1.105 --- mandoc/mandoc.c 2015/10/13 23:30:50 1.96 +++ mandoc/mandoc.c 2018/08/10 22:12:44 1.105 @@ -1,7 +1,7 @@ -/* $Id: mandoc.c,v 1.96 2015/10/13 23:30:50 schwarze Exp $ */ +/* $Id: mandoc.c,v 1.105 2018/08/10 22:12:44 schwarze Exp $ */ /* * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons - * Copyright (c) 2011-2015 Ingo Schwarze + * Copyright (c) 2011-2015, 2017, 2018 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -28,12 +28,11 @@ #include #include -#include "mandoc.h" #include "mandoc_aux.h" +#include "mandoc.h" +#include "roff.h" #include "libmandoc.h" -#define DATESIZE 32 - static int a2time(time_t *, const char *, const char *); static char *time2a(time_t); @@ -42,7 +41,7 @@ enum mandoc_esc mandoc_escape(const char **end, const char **start, int *sz) { const char *local_start; - int local_sz; + int local_sz, c, i; char term; enum mandoc_esc gly; @@ -97,6 +96,8 @@ mandoc_escape(const char **end, const char **start, in case ',': case '/': return ESCAPE_IGNORE; + case 'p': + return ESCAPE_BREAK; /* * The \z escape is supposed to output the following @@ -177,7 +178,17 @@ mandoc_escape(const char **end, const char **start, in ++*end; return ESCAPE_ERROR; } - gly = ESCAPE_IGNORE; + switch ((*start)[-1]) { + case 'h': + gly = ESCAPE_HORIZ; + break; + case 'l': + gly = ESCAPE_HLINE; + break; + default: + gly = ESCAPE_IGNORE; + break; + } term = **start; *start = ++*end; break; @@ -319,8 +330,26 @@ mandoc_escape(const char **end, const char **start, in } break; case ESCAPE_SPECIAL: - if (1 == *sz && 'c' == **start) - gly = ESCAPE_NOSPACE; + if (**start == 'c') { + if (*sz == 1) { + gly = ESCAPE_NOSPACE; + break; + } + if (*sz < 6 || *sz > 7 || + strncmp(*start, "char", 4) != 0 || + (int)strspn(*start + 4, "0123456789") + 4 < *sz) + break; + c = 0; + for (i = 4; i < *sz; i++) + c = 10 * c + ((*start)[i] - '0'); + if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff) + break; + *start += 4; + *sz -= 4; + gly = ESCAPE_NUMBERED; + break; + } + /* * Unicode escapes are defined in groff as \[u0000] * to \[u10FFFF], where the contained value must be @@ -480,17 +509,27 @@ time2a(time_t t) * 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 (0 == (ssz = strftime(p, 10 + 1, "%B ", tm))) + if ((ssz = strftime(p, 10 + 1, "%B ", tm)) == 0) goto fail; p += (int)ssz; - if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday))) + /* + * The output format is just "%d" here, not "%2d" or "%02d". + * That's also the reason why we can't just format the + * date as a whole with "%B %e, %Y" or "%B %d, %Y". + * Besides, the present approach is less prone to buffer + * overflows, in case anybody should ever introduce the bug + * of looking at LC_TIME. + */ + + if ((isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)) == -1) goto fail; p += isz; - if (0 == strftime(p, 4 + 1, "%Y", tm)) + if (strftime(p, 4 + 1, "%Y", tm) == 0) goto fail; return buf; @@ -500,25 +539,45 @@ fail: } char * -mandoc_normdate(struct mparse *parse, char *in, int ln, int pos) +mandoc_normdate(struct roff_man *man, char *in, int ln, int pos) { - char *out; + char *cp; time_t t; - if (NULL == in || '\0' == *in || - 0 == strcmp(in, "$" "Mdocdate$")) { - mandoc_msg(MANDOCERR_DATE_MISSING, parse, ln, pos, NULL); - time(&t); + /* No date specified: use today's date. */ + + if (in == NULL || *in == '\0' || strcmp(in, "$" "Mdocdate$") == 0) { + mandoc_msg(MANDOCERR_DATE_MISSING, man->parse, ln, pos, NULL); + return time2a(time(NULL)); } - else if (a2time(&t, "%Y-%m-%d", in)) - t = 0; - else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) && - !a2time(&t, "%b %d, %Y", in)) { - mandoc_msg(MANDOCERR_DATE_BAD, parse, ln, pos, in); - t = 0; + + /* Valid mdoc(7) date format. */ + + if (a2time(&t, "$" "Mdocdate: %b %d %Y $", in) || + a2time(&t, "%b %d, %Y", in)) { + cp = time2a(t); + if (t > time(NULL) + 86400) + mandoc_msg(MANDOCERR_DATE_FUTURE, man->parse, + ln, pos, cp); + else if (*in != '$' && strcmp(in, cp) != 0) + mandoc_msg(MANDOCERR_DATE_NORM, man->parse, + ln, pos, cp); + return cp; } - out = t ? time2a(t) : NULL; - return out ? out : mandoc_strdup(in); + + /* In man(7), do not warn about the legacy format. */ + + if (a2time(&t, "%Y-%m-%d", in) == 0) + mandoc_msg(MANDOCERR_DATE_BAD, man->parse, ln, pos, in); + else if (t > time(NULL) + 86400) + mandoc_msg(MANDOCERR_DATE_FUTURE, man->parse, ln, pos, in); + else if (man->macroset == MACROSET_MDOC) + mandoc_vmsg(MANDOCERR_DATE_LEGACY, man->parse, + ln, pos, "Dd %s", in); + + /* Use any non-mdoc(7) date verbatim. */ + + return mandoc_strdup(in); } int