=================================================================== RCS file: /cvs/mandoc/mdoc.c,v retrieving revision 1.188 retrieving revision 1.191 diff -u -p -r1.188 -r1.191 --- mandoc/mdoc.c 2011/03/28 23:52:13 1.188 +++ mandoc/mdoc.c 2011/07/25 15:37:00 1.191 @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.188 2011/03/28 23:52:13 kristaps Exp $ */ +/* $Id: mdoc.c,v 1.191 2011/07/25 15:37:00 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010 Ingo Schwarze @@ -97,6 +97,7 @@ static struct mdoc_node *node_alloc(struct mdoc *, int enum mdoct, enum mdoc_type); static int node_append(struct mdoc *, struct mdoc_node *); +static int mdoc_preptext(struct mdoc *, int, char *, int); static int mdoc_ptext(struct mdoc *, int, char *, int); static int mdoc_pmacro(struct mdoc *, int, char *, int); @@ -193,14 +194,14 @@ mdoc_free(struct mdoc *mdoc) * Allocate volatile and non-volatile parse resources. */ struct mdoc * -mdoc_alloc(struct regset *regs, struct mparse *parse) +mdoc_alloc(struct roff *roff, struct mparse *parse) { struct mdoc *p; p = mandoc_calloc(1, sizeof(struct mdoc)); p->parse = parse; - p->regs = regs; + p->roff = roff; mdoc_hash_init(); mdoc_alloc1(p); @@ -233,11 +234,11 @@ mdoc_addeqn(struct mdoc *m, const struct eqn *ep) /* No text before an initial macro. */ if (SEC_NONE == m->lastnamed) { - mdoc_pmsg(m, ep->line, ep->pos, MANDOCERR_NOTEXT); + mdoc_pmsg(m, ep->ln, ep->pos, MANDOCERR_NOTEXT); return(1); } - n = node_alloc(m, ep->line, ep->pos, MDOC_MAX, MDOC_EQN); + n = node_alloc(m, ep->ln, ep->pos, MDOC_MAX, MDOC_EQN); n->eqn = ep; if ( ! node_append(m, n)) @@ -290,8 +291,8 @@ mdoc_parseln(struct mdoc *m, int ln, char *buf, int of * whether this mode is on or off. * Note that this mode is also switched by the Sh macro. */ - if (m->regs->regs[(int)REG_nS].set) { - if (m->regs->regs[(int)REG_nS].v.u) + if (roff_regisset(m->roff, REG_nS)) { + if (roff_regget(m->roff, REG_nS)) m->flags |= MDOC_SYNOPSIS; else m->flags &= ~MDOC_SYNOPSIS; @@ -299,7 +300,7 @@ mdoc_parseln(struct mdoc *m, int ln, char *buf, int of return(mandoc_getcontrol(buf, &offs) ? mdoc_pmacro(m, ln, buf, offs) : - mdoc_ptext(m, ln, buf, offs)); + mdoc_preptext(m, ln, buf, offs)); } int @@ -650,6 +651,57 @@ mdoc_node_delete(struct mdoc *m, struct mdoc_node *p) mdoc_node_free(p); } +/* + * Pre-treat a text line. + * Text lines can consist of equations, which must be handled apart from + * the regular text. + * Thus, use this function to step through a line checking if it has any + * equations embedded in it. + * This must handle multiple equations AND equations that do not end at + * the end-of-line, i.e., will re-enter in the next roff parse. + */ +static int +mdoc_preptext(struct mdoc *m, int line, char *buf, int offs) +{ + char *start, *end; + char delim; + + while ('\0' != buf[offs]) { + /* Mark starting position if eqn is set. */ + start = NULL; + if ('\0' != (delim = roff_eqndelim(m->roff))) + if (NULL != (start = strchr(buf + offs, delim))) + *start++ = '\0'; + + /* Parse text as normal. */ + if ( ! mdoc_ptext(m, line, buf, offs)) + return(0); + + /* Continue only if an equation exists. */ + if (NULL == start) + break; + + /* Read past the end of the equation. */ + offs += start - (buf + offs); + assert(start == &buf[offs]); + if (NULL != (end = strchr(buf + offs, delim))) { + *end++ = '\0'; + while (' ' == *end) + end++; + } + + /* Parse the equation itself. */ + roff_openeqn(m->roff, NULL, line, offs, buf); + + /* Process a finished equation? */ + if (roff_closeeqn(m->roff)) + if ( ! mdoc_addeqn(m, roff_eqn(m->roff))) + return(0); + offs += (end - (buf + offs)); + } + + return(1); +} /* * Parse free-form text, that is, a line that does not begin with the