=================================================================== RCS file: /cvs/mandoc/roff.c,v retrieving revision 1.267 retrieving revision 1.273 diff -u -p -r1.267 -r1.273 --- mandoc/roff.c 2015/04/19 14:25:41 1.267 +++ mandoc/roff.c 2015/08/29 20:26:04 1.273 @@ -1,4 +1,4 @@ -/* $Id: roff.c,v 1.267 2015/04/19 14:25:41 schwarze Exp $ */ +/* $Id: roff.c,v 1.273 2015/08/29 20:26:04 schwarze Exp $ */ /* * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze @@ -335,6 +335,7 @@ struct roff { int rstacksz; /* current size limit of rstack */ int rstackpos; /* position in rstack */ int format; /* current file in mdoc or man format */ + int argc; /* number of args of the last macro */ char control; /* control character */ }; @@ -397,8 +398,7 @@ static enum rofferr roff_cond_text(ROFF_ARGS); static enum rofferr roff_cond_sub(ROFF_ARGS); static enum rofferr roff_ds(ROFF_ARGS); static enum rofferr roff_eqndelim(struct roff *, struct buf *, int); -static int roff_evalcond(struct roff *r, int, - const char *, int *); +static int roff_evalcond(struct roff *r, int, char *, int *); static int roff_evalnum(struct roff *, int, const char *, int *, int *, int); static int roff_evalpar(struct roff *, int, @@ -412,9 +412,12 @@ static int roff_getnum(const char *, int *, int *, i static int roff_getop(const char *, int *, char *); static int roff_getregn(const struct roff *, const char *, size_t); -static int roff_getregro(const char *name); +static int roff_getregro(const struct roff *, + const char *name); static const char *roff_getstrn(const struct roff *, const char *, size_t); +static int roff_hasregn(const struct roff *, + const char *, size_t); static enum rofferr roff_insec(ROFF_ARGS); static enum rofferr roff_it(ROFF_ARGS); static enum rofferr roff_line_ignore(ROFF_ARGS); @@ -1022,6 +1025,7 @@ roff_node_append(struct roff_man *man, struct roff_nod /* NOTREACHED */ } n->parent->nchild++; + n->parent->last = n; /* * Copy over the normalised-data pointer of our parent. Not @@ -1096,7 +1100,28 @@ roff_word_append(struct roff_man *man, const char *wor man->next = ROFF_NEXT_SIBLING; } +void +roff_elem_alloc(struct roff_man *man, int line, int pos, int tok) +{ + struct roff_node *n; + + n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok); + roff_node_append(man, n); + man->next = ROFF_NEXT_CHILD; +} + struct roff_node * +roff_block_alloc(struct roff_man *man, int line, int pos, int tok) +{ + struct roff_node *n; + + n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok); + roff_node_append(man, n); + man->next = ROFF_NEXT_CHILD; + return(n); +} + +struct roff_node * roff_head_alloc(struct roff_man *man, int line, int pos, int tok) { struct roff_node *n; @@ -1209,6 +1234,52 @@ roff_node_delete(struct roff_man *man, struct roff_nod roff_node_free(n); } +void +deroff(char **dest, const struct roff_node *n) +{ + char *cp; + size_t sz; + + if (n->type != ROFFT_TEXT) { + for (n = n->child; n != NULL; n = n->next) + deroff(dest, n); + return; + } + + /* Skip leading whitespace and escape sequences. */ + + cp = n->string; + while (*cp != '\0') { + if ('\\' == *cp) { + cp++; + mandoc_escape((const char **)&cp, NULL, NULL); + } else if (isspace((unsigned char)*cp)) + cp++; + else + break; + } + + /* Skip trailing whitespace. */ + + for (sz = strlen(cp); sz; sz--) + if ( ! isspace((unsigned char)cp[sz-1])) + break; + + /* Skip empty strings. */ + + if (sz == 0) + return; + + if (*dest == NULL) { + *dest = mandoc_strndup(cp, sz); + return; + } + + mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp); + free(*dest); + *dest = cp; +} + /* --- main functions of the roff parser ---------------------------------- */ /* @@ -2066,8 +2137,10 @@ out: * or string condition. */ static int -roff_evalcond(struct roff *r, int ln, const char *v, int *pos) +roff_evalcond(struct roff *r, int ln, char *v, int *pos) { + char *cp, *name; + size_t sz; int number, savepos, wanttrue; if ('!' == v[*pos]) { @@ -2090,13 +2163,16 @@ roff_evalcond(struct roff *r, int ln, const char *v, i /* FALLTHROUGH */ case 'e': /* FALLTHROUGH */ - case 'r': - /* FALLTHROUGH */ case 't': /* FALLTHROUGH */ case 'v': (*pos)++; return(!wanttrue); + case 'r': + cp = name = v + ++*pos; + sz = roff_getname(r, &cp, ln, *pos); + *pos = cp - v; + return((sz && roff_hasregn(r, name, sz)) == wanttrue); default: break; } @@ -2191,6 +2267,8 @@ roff_cond(ROFF_ARGS) if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') { r->last->endspan = -1; pos += 2; + while (buf->buf[pos] == ' ') + pos++; goto out; } @@ -2499,10 +2577,12 @@ roff_setreg(struct roff *r, const char *name, int val, * were to turn up, another special value would have to be chosen. */ static int -roff_getregro(const char *name) +roff_getregro(const struct roff *r, const char *name) { switch (*name) { + case '$': /* Number of arguments of the last macro evaluated. */ + return(r->argc); case 'A': /* ASCII approximation mode is always off. */ return(0); case 'g': /* Groff compatibility mode is always on. */ @@ -2527,7 +2607,7 @@ roff_getreg(const struct roff *r, const char *name) int val; if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) { - val = roff_getregro(name + 1); + val = roff_getregro(r, name + 1); if (-1 != val) return (val); } @@ -2546,7 +2626,7 @@ roff_getregn(const struct roff *r, const char *name, s int val; if ('.' == name[0] && 2 == len) { - val = roff_getregro(name + 1); + val = roff_getregro(r, name + 1); if (-1 != val) return (val); } @@ -2559,6 +2639,26 @@ roff_getregn(const struct roff *r, const char *name, s return(0); } +static int +roff_hasregn(const struct roff *r, const char *name, size_t len) +{ + struct roffreg *reg; + int val; + + if ('.' == name[0] && 2 == len) { + val = roff_getregro(r, name + 1); + if (-1 != val) + return(1); + } + + for (reg = r->regtab; reg; reg = reg->next) + if (len == reg->key.sz && + 0 == strncmp(name, reg->key.p, len)) + return(1); + + return(0); +} + static void roff_freereg(struct roffreg *reg) { @@ -2988,10 +3088,16 @@ roff_userdef(ROFF_ARGS) * and NUL-terminate them. */ + r->argc = 0; cp = buf->buf + pos; - for (i = 0; i < 9; i++) - arg[i] = *cp == '\0' ? "" : - mandoc_getarg(r->parse, &cp, ln, &pos); + for (i = 0; i < 9; i++) { + if (*cp == '\0') + arg[i] = ""; + else { + arg[i] = mandoc_getarg(r->parse, &cp, ln, &pos); + r->argc = i + 1; + } + } /* * Expand macro arguments.