=================================================================== RCS file: /cvs/mandoc/roff.c,v retrieving revision 1.226 retrieving revision 1.231 diff -u -p -r1.226 -r1.231 --- mandoc/roff.c 2014/08/19 16:52:32 1.226 +++ mandoc/roff.c 2014/10/16 01:28:38 1.231 @@ -1,4 +1,4 @@ -/* $Id: roff.c,v 1.226 2014/08/19 16:52:32 schwarze Exp $ */ +/* $Id: roff.c,v 1.231 2014/10/16 01:28:38 schwarze Exp $ */ /* * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons * Copyright (c) 2010-2014 Ingo Schwarze @@ -60,6 +60,7 @@ enum rofft { ROFF_nh, ROFF_nr, ROFF_ns, + ROFF_pl, ROFF_ps, ROFF_rm, ROFF_rr, @@ -119,9 +120,11 @@ struct roff { struct eqn_node *last_eqn; /* last equation parsed */ struct eqn_node *first_eqn; /* first equation parsed */ struct eqn_node *eqn; /* current equation being parsed */ + int eqn_inline; /* current equation is inline */ int options; /* parse options */ int rstacksz; /* current size limit of rstack */ int rstackpos; /* position in rstack */ + int format; /* current file in mdoc or man format */ char control; /* control character */ }; @@ -181,6 +184,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 enum rofferr roff_eqndelim(struct roff *, + char **, size_t *, int); static int roff_evalcond(const char *, int *); static int roff_evalnum(const char *, int *, int *, int); static int roff_evalpar(const char *, int *, int *); @@ -255,6 +260,7 @@ static struct roffmac roffs[ROFF_MAX] = { { "nh", roff_line_ignore, NULL, NULL, 0, NULL }, { "nr", roff_nr, NULL, NULL, 0, NULL }, { "ns", roff_line_ignore, NULL, NULL, 0, NULL }, + { "pl", roff_line_ignore, NULL, NULL, 0, NULL }, { "ps", roff_line_ignore, NULL, NULL, 0, NULL }, { "rm", roff_rm, NULL, NULL, 0, NULL }, { "rr", roff_rr, NULL, NULL, 0, NULL }, @@ -456,6 +462,7 @@ roff_reset(struct roff *r) { roff_free1(r); + r->format = r->options & (MPARSE_MDOC | MPARSE_MAN); r->control = 0; } @@ -475,6 +482,7 @@ roff_alloc(struct mparse *parse, int options) r = mandoc_calloc(1, sizeof(struct roff)); r->parse = parse; r->options = options; + r->format = options & (MPARSE_MDOC | MPARSE_MAN); r->rstackpos = -1; roffhash_init(); @@ -719,11 +727,18 @@ roff_parseln(struct roff *r, int ln, char **bufp, enum rofferr e; int ppos, ctl; - /* - * Run the reserved-word filter only if we have some reserved - * words to fill in. - */ + /* Handle in-line equation delimiters. */ + if (r->last_eqn != NULL && r->last_eqn->delim && + (r->eqn == NULL || r->eqn_inline)) { + e = roff_eqndelim(r, bufp, szp, pos); + if (e == ROFF_REPARSE) + return(e); + assert(e == ROFF_CONT); + } + + /* Expand some escape sequences. */ + e = roff_res(r, bufp, szp, ln, pos); if (ROFF_IGN == e) return(e); @@ -756,6 +771,15 @@ roff_parseln(struct roff *r, int ln, char **bufp, return(roff_parsetext(bufp, szp, pos, offs)); } + /* Skip empty request lines. */ + + if ((*bufp)[pos] == '"') { + mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse, + ln, pos, NULL); + return(ROFF_IGN); + } else if ((*bufp)[pos] == '\0') + return(ROFF_IGN); + /* * If a scope is open, go to the child handler for that macro, * as it may want to preprocess before doing anything with it. @@ -1776,10 +1800,13 @@ roff_Dd(ROFF_ARGS) { const char *const *cp; - if (0 == ((MPARSE_MDOC | MPARSE_QUICK) & r->options)) + if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0) for (cp = __mdoc_reserved; *cp; cp++) roff_setstr(r, *cp, NULL, 0); + if (r->format == 0) + r->format = MPARSE_MDOC; + return(ROFF_CONT); } @@ -1788,10 +1815,13 @@ roff_TH(ROFF_ARGS) { const char *const *cp; - if (0 == (MPARSE_QUICK & r->options)) + if ((r->options & MPARSE_QUICK) == 0) for (cp = __man_reserved; *cp; cp++) roff_setstr(r, *cp, NULL, 0); + if (r->format == 0) + r->format = MPARSE_MAN; + return(ROFF_CONT); } @@ -1821,14 +1851,48 @@ roff_T_(ROFF_ARGS) return(ROFF_IGN); } -#if 0 -static int -roff_closeeqn(struct roff *r) +/* + * Handle in-line equation delimiters. + */ +static enum rofferr +roff_eqndelim(struct roff *r, char **bufp, size_t *szp, int pos) { + char *cp1, *cp2; - return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0); + /* + * Outside equations, look for an opening delimiter. + * If we are inside an equation, we already know it is + * in-line, or this function wouldn't have been called; + * so look for a closing delimiter. + */ + + cp1 = *bufp + pos; + cp2 = strchr(cp1, r->eqn == NULL ? + r->last_eqn->odelim : r->last_eqn->cdelim); + if (cp2 == NULL) + return(ROFF_CONT); + + /* Found a delimiter; get rid of surrounding blanks. */ + + cp1 = cp2++; + while (cp2[0] == ' ') + cp2++; + while (cp1[-1] == ' ') + cp1--; + *cp1 = '\0'; + + /* Replace the delimiter with an equation macro. */ + + *szp = mandoc_asprintf(&cp1, "%s\n.E%s%s", *bufp, + r->eqn == NULL ? "Q\n" : "N\n\\&", cp2) + 1; + free(*bufp); + *bufp = cp1; + + /* Toggle the in-line state of the eqn subsystem. */ + + r->eqn_inline = r->eqn == NULL; + return(ROFF_REPARSE); } -#endif static void roff_openeqn(struct roff *r, const char *name, int line, @@ -1840,9 +1904,12 @@ roff_openeqn(struct roff *r, const char *name, int lin assert(NULL == r->eqn); e = eqn_alloc(name, offs, line, r->parse); - if (r->last_eqn) + if (r->last_eqn) { r->last_eqn->next = e; - else + e->delim = r->last_eqn->delim; + e->odelim = r->last_eqn->odelim; + e->cdelim = r->last_eqn->cdelim; + } else r->first_eqn = r->last_eqn = e; r->eqn = r->last_eqn = e; @@ -2305,6 +2372,13 @@ roff_strdup(const struct roff *r, const char *p) res[(int)ssz] = '\0'; return(res); +} + +int +roff_getformat(const struct roff *r) +{ + + return(r->format); } /*