=================================================================== RCS file: /cvs/mandoc/mandoc.c,v retrieving revision 1.37 retrieving revision 1.43 diff -u -p -r1.37 -r1.43 --- mandoc/mandoc.c 2011/03/07 01:35:51 1.37 +++ mandoc/mandoc.c 2011/03/22 14:05:45 1.43 @@ -1,4 +1,4 @@ -/* $Id: mandoc.c,v 1.37 2011/03/07 01:35:51 schwarze Exp $ */ +/* $Id: mandoc.c,v 1.43 2011/03/22 14:05:45 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons * Copyright (c) 2011 Ingo Schwarze @@ -296,7 +296,7 @@ mandoc_strdup(const char *ptr) * or to the null byte terminating the argument line. */ char * -mandoc_getarg(char **cpp, mandocmsg msg, void *data, int ln, int *pos) +mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos) { char *start, *cp; int quoted, pairs, white; @@ -343,8 +343,8 @@ mandoc_getarg(char **cpp, mandocmsg msg, void *data, i } /* Quoted argument without a closing quote. */ - if (1 == quoted && msg) - (*msg)(MANDOCERR_BADQUOTE, data, ln, *pos, NULL); + if (1 == quoted) + mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL); /* Null-terminate this argument and move to the next one. */ if (pairs) @@ -354,16 +354,15 @@ 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])) - (*msg)(MANDOCERR_EOLNSPACE, data, ln, *pos, NULL); + if ('\0' == *cp && (white || ' ' == cp[-1])) + mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL); return(start); } - static int a2time(time_t *t, const char *fmt, const char *p) { @@ -381,59 +380,62 @@ a2time(time_t *t, const char *fmt, const char *p) return(0); } - static char * time2a(time_t t) { struct tm tm; - char buf[DATESIZE]; - char *p; - size_t nsz, rsz; + char *buf, *p; + size_t ssz; int isz; localtime_r(&t, &tm); - p = buf; - rsz = DATESIZE; + /* + * 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 (0 == (nsz = strftime(p, rsz, "%B ", &tm))) - return(NULL); + if (0 == (ssz = strftime(p, 10 + 1, "%B ", &tm))) + goto fail; + p += (int)ssz; - p += (int)nsz; - rsz -= nsz; - - if (-1 == (isz = snprintf(p, rsz, "%d, ", tm.tm_mday))) - return(NULL); - + if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm.tm_mday))) + goto fail; p += isz; - rsz -= isz; - return(strftime(p, rsz, "%Y", &tm) ? buf : NULL); + 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) +mandoc_normdate(struct mparse *parse, char *in, 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); + mandoc_msg(MANDOCERR_NODATE, parse, 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); + mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL); t = 0; } out = t ? time2a(t) : NULL; - return(mandoc_strdup(out ? out : in)); + return(out ? out : mandoc_strdup(in)); } - int mandoc_eos(const char *p, size_t sz, int enclosed) { @@ -477,7 +479,6 @@ mandoc_eos(const char *p, size_t sz, int enclosed) return(found && !enclosed); } - int mandoc_hyph(const char *start, const char *c) { @@ -504,3 +505,4 @@ mandoc_hyph(const char *start, const char *c) return(1); } +