Module Name: src Committed By: christos Date: Fri Mar 22 01:24:47 UTC 2013
Modified Files: src/external/bsd/mdocml/dist: libmandoc.h read.c roff.c Log Message: move the code to expand variables in one place and use it to expand variables inside conditionals. still conditionals with variables don't work. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/external/bsd/mdocml/dist/libmandoc.h cvs rdiff -u -r1.7 -r1.8 src/external/bsd/mdocml/dist/read.c cvs rdiff -u -r1.8 -r1.9 src/external/bsd/mdocml/dist/roff.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/external/bsd/mdocml/dist/libmandoc.h diff -u src/external/bsd/mdocml/dist/libmandoc.h:1.2 src/external/bsd/mdocml/dist/libmandoc.h:1.3 --- src/external/bsd/mdocml/dist/libmandoc.h:1.2 Thu Mar 21 17:42:16 2013 +++ src/external/bsd/mdocml/dist/libmandoc.h Thu Mar 21 21:24:46 2013 @@ -73,7 +73,8 @@ void roff_reset(struct roff *); enum rofferr roff_parseln(struct roff *, int, char **, size_t *, int, int *); void roff_endparse(struct roff *); -size_t roff_expand_nr(struct roff *, const char *, char *, size_t); +void roff_expand_nr(struct roff *, const char *, int *, size_t, + char **, int *, size_t *); int roff_regisset(const struct roff *, enum regs); unsigned int roff_regget(const struct roff *, enum regs); void roff_regunset(struct roff *, enum regs); Index: src/external/bsd/mdocml/dist/read.c diff -u src/external/bsd/mdocml/dist/read.c:1.7 src/external/bsd/mdocml/dist/read.c:1.8 --- src/external/bsd/mdocml/dist/read.c:1.7 Thu Mar 21 17:42:16 2013 +++ src/external/bsd/mdocml/dist/read.c Thu Mar 21 21:24:46 2013 @@ -354,19 +354,8 @@ mparse_buf_r(struct mparse *curp, struct } if ('\\' == blk.buf[i] && 'n' == blk.buf[i + 1]) { - int j, k; - i += 2; - if ('(' == blk.buf[i]) /* ) */ - i++; - resize_buf(&ln, 256); - for (j = i, k = pos; i < j + 256 - && i < (int)blk.sz - && !isspace((unsigned char)blk.buf[i]);) - ln.buf[k++] = blk.buf[i++]; - - ln.buf[k] = '\0'; - pos += roff_expand_nr(curp->roff, - ln.buf + pos, ln.buf + pos, 256); + roff_expand_nr(curp->roff, + blk.buf, &i, blk.sz, &ln.buf, &pos, &ln.sz); } /* Index: src/external/bsd/mdocml/dist/roff.c diff -u src/external/bsd/mdocml/dist/roff.c:1.8 src/external/bsd/mdocml/dist/roff.c:1.9 --- src/external/bsd/mdocml/dist/roff.c:1.8 Thu Mar 21 17:42:16 2013 +++ src/external/bsd/mdocml/dist/roff.c Thu Mar 21 21:24:46 2013 @@ -177,6 +177,8 @@ static enum rofferr roff_cond(ROFF_ARGS static enum rofferr roff_cond_text(ROFF_ARGS); static enum rofferr roff_cond_sub(ROFF_ARGS); static enum rofferr roff_ds(ROFF_ARGS); +static void roff_expand_nr_inplace(struct roff *, char **, int *, + size_t *); static enum roffrule roff_evalcond(const char *, int *); static void roff_free1(struct roff *); static void roff_freestr(struct roffkv *); @@ -1053,6 +1055,12 @@ roff_cond_text(ROFF_ARGS) ep = &(*bufp)[pos]; for ( ; NULL != (ep = strchr(ep, '\\')); ep++) { ep++; + if (*ep == 'n') { + int i = ep - *bufp - 1; + roff_expand_nr_inplace(r, bufp, &i, szp); + ep = *bufp + i; + continue; + } if ('}' != *ep) continue; *ep = '&'; @@ -1308,17 +1316,76 @@ roff_nr(ROFF_ARGS) return(ROFF_IGN); } -size_t -roff_expand_nr(struct roff *r, const char *key, char *lp, size_t lpl) +void +roff_expand_nr(struct roff *r, const char *src, int *sp, size_t slen, + char **dst, int *dp, size_t *dlenp) { uint32_t hv; struct roff_nr *h; + int l, s, d; + char e, *key; - if ((h = hash_find(r, key, &hv)) == NULL) - return 0; + s = *sp + 2; /* skip \\\n */ + d = *dp; + + if ('[' == src[s]) { /* XXX: Support builtins */ + s++; + e = ']'; + } else + e = '\0'; + + for (l = s; l < (int)slen; l++) { + if (e) { + if (src[l] == e) + break; + } else { + if (isspace((unsigned char)src[l])) + break; + } + } + *sp = l; + l -= s; + key = mandoc_malloc(l + 1); + memcpy(key, src + s, l); + key[l] = '\0'; + + if ((h = hash_find(r, key, &hv)) == NULL) { + free(key); + return; + } + if (*dst == NULL || *dlenp - *dp < 256) + *dst = mandoc_realloc(*dst, *dlenp += 256); /* XXX: support .af */ - return snprintf(lp, lpl, "%jd", h->val); + *dp += snprintf(*dst + *dp, *dlenp - *dp, "%jd", h->val); +} + +static void +roff_expand_nr_inplace(struct roff *r, char **src, int *sp, size_t *slenp) +{ + int j, i, k; + size_t dlen; + char *dst; + + k = i = *sp; + + dst = NULL; + j = 0; + dlen = 0; + + roff_expand_nr(r, *src, &i, *slenp, &dst, &j, &dlen); + + if (j) { + int l = j - (i - k); + if (l > 0) { + *slenp += l; + *src = mandoc_realloc(*src, *slenp); + } + memmove(*src + j + k, *src + i, *slenp - i); + memcpy(*src + k, dst, j); + free(dst); + } + *sp = k + j; } /* ARGSUSED */