/* ARGSUSED */ int blk_close(MACRO_PROT_ARGS) { enum mant ntok; const struct man_node *nn; switch (tok) { case (MAN_RE): ntok = MAN_RS; break; case (MAN_UE): ntok = MAN_UR; break; default: abort(); /* NOTREACHED */ } for (nn = man->last->parent; nn; nn = nn->parent) if (ntok == nn->tok && MAN_BLOCK == nn->type) break; if (NULL == nn) { man_pmsg(man, line, ppos, MANDOCERR_NOSCOPE); if ( ! rew_scope(MAN_BLOCK, man, MAN_PP)) return(0); } else man_unscope(man, nn, MANDOCERR_MAX); return(1); }
void man_endparse(struct roff_man *man) { man_unscope(man, man->first); man->flags &= ~MAN_LITERAL; }
/* * Rewinding entails ascending the parse tree until a coherent point, * for example, the `SH' macro will close out any intervening `SS' * scopes. When a scope is closed, it must be validated and actioned. */ static int rew_scope(enum man_type type, struct man *man, enum mant tok) { struct man_node *n; enum rew c; /* LINTED */ for (n = man->last; n; n = n->parent) { /* * Whether we should stop immediately (REW_HALT), stop * and rewind until this point (REW_REWIND), or keep * rewinding (REW_NOHALT). */ c = rew_dohalt(tok, type, n); if (REW_HALT == c) return(1); if (REW_REWIND == c) break; } /* * Rewind until the current point. Warn if we're a roff * instruction that's mowing over explicit scopes. */ assert(n); return(man_unscope(man, n, MANDOCERR_MAX)); }
/* * Close out a generic explicit macro. */ void blk_close(MACRO_PROT_ARGS) { int ntok; const struct roff_node *nn; char *p; int nrew, target; nrew = 1; switch (tok) { case MAN_RE: ntok = MAN_RS; if ( ! man_args(man, line, pos, buf, &p)) break; for (nn = man->last->parent; nn; nn = nn->parent) if (nn->tok == ntok && nn->type == ROFFT_BLOCK) nrew++; target = strtol(p, &p, 10); if (*p != '\0') mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, line, p - buf, "RE ... %s", p); if (target == 0) target = 1; nrew -= target; if (nrew < 1) { mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse, line, ppos, "RE %d", target); return; } break; case MAN_UE: ntok = MAN_UR; break; default: abort(); } for (nn = man->last->parent; nn; nn = nn->parent) if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew) break; if (nn == NULL) { mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse, line, ppos, man_macronames[tok]); rew_scope(man, MAN_PP); } else { line = man->last->line; ppos = man->last->pos; ntok = man->last->tok; man_unscope(man, nn); /* Move a trailing paragraph behind the block. */ if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) { *pos = strlen(buf); blk_imp(man, ntok, line, ppos, pos, buf); } } }
static void man_descope(struct roff_man *man, int line, int offs) { /* * Co-ordinate what happens with having a next-line scope open: * first close out the element scope (if applicable), then close * out the block scope (also if applicable). */ if (man->flags & MAN_ELINE) { man->flags &= ~MAN_ELINE; man_unscope(man, man->last->parent); } if ( ! (man->flags & MAN_BLINE)) return; man->flags &= ~MAN_BLINE; man_unscope(man, man->last->parent); roff_body_alloc(man, line, offs, man->last->tok); }
static int man_descope(struct man *man, int line, int offs) { /* * Co-ordinate what happens with having a next-line scope open: * first close out the element scope (if applicable), then close * out the block scope (also if applicable). */ if (MAN_ELINE & man->flags) { man->flags &= ~MAN_ELINE; if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX)) return(0); } if ( ! (MAN_BLINE & man->flags)) return(1); man->flags &= ~MAN_BLINE; if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX)) return(0); return(man_body_alloc(man, line, offs, man->last->tok)); }
/* * Rewinding entails ascending the parse tree until a coherent point, * for example, the `SH' macro will close out any intervening `SS' * scopes. When a scope is closed, it must be validated and actioned. */ static void rew_scope(struct roff_man *man, int tok) { struct roff_node *n; /* Preserve empty paragraphs before RS. */ n = man->last; if (tok == MAN_RS && n->child == NULL && (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP)) return; for (;;) { if (n->type == ROFFT_ROOT) return; if (n->flags & MAN_VALID) { n = n->parent; continue; } if (n->type != ROFFT_BLOCK) { if (n->parent->type == ROFFT_ROOT) { man_unscope(man, n); return; } else { n = n->parent; continue; } } if (tok != MAN_SH && (n->tok == MAN_SH || (tok != MAN_SS && (n->tok == MAN_SS || man_macros[n->tok].fp == blk_exp)))) return; man_unscope(man, n); n = man->last; } }
/* * Parse an implicit-block macro. These contain a ROFFT_HEAD and a * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other * scopes, such as `SH' closing out an `SS', are defined in the rew * routines. */ void blk_imp(MACRO_PROT_ARGS) { int la; char *p; struct roff_node *n; rew_scope(man, tok); n = roff_block_alloc(man, line, ppos, tok); if (n->tok == MAN_SH || n->tok == MAN_SS) man->flags &= ~MAN_LITERAL; n = roff_head_alloc(man, line, ppos, tok); /* Add line arguments. */ for (;;) { la = *pos; if ( ! man_args(man, line, pos, buf, &p)) break; roff_word_alloc(man, line, la, p); } /* * For macros having optional next-line scope, * keep the head open if there were no arguments. * For `TP', always keep the head open. */ if (man_macros[tok].flags & MAN_SCOPED && (tok == MAN_TP || n == man->last)) { man->flags |= MAN_BLINE; return; } /* Close out the head and open the body. */ man_unscope(man, n); roff_body_alloc(man, line, ppos, tok); }
void blk_exp(MACRO_PROT_ARGS) { struct roff_node *head; char *p; int la; rew_scope(man, tok); roff_block_alloc(man, line, ppos, tok); head = roff_head_alloc(man, line, ppos, tok); la = *pos; if (man_args(man, line, pos, buf, &p)) roff_word_alloc(man, line, la, p); if (buf[*pos] != '\0') mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, line, *pos, "%s ... %s", roff_name[tok], buf + *pos); man_unscope(man, head); roff_body_alloc(man, line, ppos, tok); }
/* ARGSUSED */ int blk_exp(MACRO_PROT_ARGS) { struct man_node *n; int la; char *p; /* Close out prior implicit scopes. */ if ( ! rew_scope(MAN_BLOCK, man, tok)) return(0); if ( ! man_block_alloc(man, line, ppos, tok)) return(0); if ( ! man_head_alloc(man, line, ppos, tok)) return(0); for (;;) { la = *pos; if ( ! man_args(man, line, pos, buf, &p)) break; if ( ! man_word_alloc(man, line, la, p)) return(0); } assert(man); assert(tok != MAN_MAX); for (n = man->last; n; n = n->parent) { if (n->tok != tok) continue; assert(MAN_HEAD == n->type); man_unscope(man, n, MANDOCERR_MAX); break; } return(man_body_alloc(man, line, ppos, tok)); }
static int man_pmacro(struct man *man, int ln, char *buf, int offs) { int i, ppos; enum mant tok; char mac[5]; struct man_node *n; if ('"' == buf[offs]) { man_pmsg(man, ln, offs, MANDOCERR_BADCOMMENT); return(1); } else if ('\0' == buf[offs]) return(1); ppos = offs; /* * Copy the first word into a nil-terminated buffer. * Stop copying when a tab, space, or eoln is encountered. */ i = 0; while (i < 4 && '\0' != buf[offs] && ' ' != buf[offs] && '\t' != buf[offs]) mac[i++] = buf[offs++]; mac[i] = '\0'; tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX; if (MAN_MAX == tok) { mandoc_vmsg(MANDOCERR_MACRO, man->parse, ln, ppos, "%s", buf + ppos - 1); return(1); } /* The macro is sane. Jump to the next word. */ while (buf[offs] && ' ' == buf[offs]) offs++; /* * Trailing whitespace. Note that tabs are allowed to be passed * into the parser as "text", so we only warn about spaces here. */ if ('\0' == buf[offs] && ' ' == buf[offs - 1]) man_pmsg(man, ln, offs - 1, MANDOCERR_EOLNSPACE); /* * Remove prior ELINE macro, as it's being clobbered by a new * macro. Note that NSCOPED macros do not close out ELINE * macros---they don't print text---so we let those slip by. */ if ( ! (MAN_NSCOPED & man_macros[tok].flags) && man->flags & MAN_ELINE) { n = man->last; assert(MAN_TEXT != n->type); /* Remove repeated NSCOPED macros causing ELINE. */ if (MAN_NSCOPED & man_macros[n->tok].flags) n = n->parent; mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line, n->pos, "%s breaks %s", man_macronames[tok], man_macronames[n->tok]); man_node_delete(man, n); man->flags &= ~MAN_ELINE; } /* * Remove prior BLINE macro that is being clobbered. */ if ((man->flags & MAN_BLINE) && (MAN_BSCOPE & man_macros[tok].flags)) { n = man->last; /* Might be a text node like 8 in * .TP 8 * .SH foo */ if (MAN_TEXT == n->type) n = n->parent; /* Remove element that didn't end BLINE, if any. */ if ( ! (MAN_BSCOPE & man_macros[n->tok].flags)) n = n->parent; assert(MAN_HEAD == n->type); n = n->parent; assert(MAN_BLOCK == n->type); assert(MAN_SCOPED & man_macros[n->tok].flags); mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line, n->pos, "%s breaks %s", man_macronames[tok], man_macronames[n->tok]); man_node_delete(man, n); man->flags &= ~MAN_BLINE; } /* * Save the fact that we're in the next-line for a block. In * this way, embedded roff instructions can "remember" state * when they exit. */ if (MAN_BLINE & man->flags) man->flags |= MAN_BPLINE; /* Call to handler... */ assert(man_macros[tok].fp); if ( ! (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf)) goto err; /* * We weren't in a block-line scope when entering the * above-parsed macro, so return. */ if ( ! (MAN_BPLINE & man->flags)) { man->flags &= ~MAN_ILINE; return(1); } man->flags &= ~MAN_BPLINE; /* * If we're in a block scope, then allow this macro to slip by * without closing scope around it. */ if (MAN_ILINE & man->flags) { man->flags &= ~MAN_ILINE; return(1); } /* * If we've opened a new next-line element scope, then return * now, as the next line will close out the block scope. */ if (MAN_ELINE & man->flags) return(1); /* Close out the block scope opened in the prior line. */ assert(MAN_BLINE & man->flags); man->flags &= ~MAN_BLINE; if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX)) return(0); return(man_body_alloc(man, ln, ppos, man->last->tok)); err: /* Error out. */ man->flags |= MAN_HALT; return(0); }
static int man_pmacro(struct man *man, int ln, char *buf, int offs) { char mac[5]; struct man_node *n; enum mant tok; int i, ppos; int bline; if ('"' == buf[offs]) { mandoc_msg(MANDOCERR_COMMENT_BAD, man->parse, ln, offs, NULL); return(1); } else if ('\0' == buf[offs]) return(1); ppos = offs; /* * Copy the first word into a nil-terminated buffer. * Stop copying when a tab, space, or eoln is encountered. */ i = 0; while (i < 4 && '\0' != buf[offs] && ' ' != buf[offs] && '\t' != buf[offs]) mac[i++] = buf[offs++]; mac[i] = '\0'; tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX; if (MAN_MAX == tok) { mandoc_msg(MANDOCERR_MACRO, man->parse, ln, ppos, buf + ppos - 1); return(1); } /* The macro is sane. Jump to the next word. */ while (buf[offs] && ' ' == buf[offs]) offs++; /* * Trailing whitespace. Note that tabs are allowed to be passed * into the parser as "text", so we only warn about spaces here. */ if ('\0' == buf[offs] && ' ' == buf[offs - 1]) mandoc_msg(MANDOCERR_SPACE_EOL, man->parse, ln, offs - 1, NULL); /* * Remove prior ELINE macro, as it's being clobbered by a new * macro. Note that NSCOPED macros do not close out ELINE * macros---they don't print text---so we let those slip by. */ if ( ! (MAN_NSCOPED & man_macros[tok].flags) && man->flags & MAN_ELINE) { n = man->last; assert(MAN_TEXT != n->type); /* Remove repeated NSCOPED macros causing ELINE. */ if (MAN_NSCOPED & man_macros[n->tok].flags) n = n->parent; mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, n->pos, "%s breaks %s", man_macronames[tok], man_macronames[n->tok]); man_node_delete(man, n); man->flags &= ~MAN_ELINE; } /* * Remove prior BLINE macro that is being clobbered. */ if ((man->flags & MAN_BLINE) && (MAN_BSCOPE & man_macros[tok].flags)) { n = man->last; /* Might be a text node like 8 in * .TP 8 * .SH foo */ if (MAN_TEXT == n->type) n = n->parent; /* Remove element that didn't end BLINE, if any. */ if ( ! (MAN_BSCOPE & man_macros[n->tok].flags)) n = n->parent; assert(MAN_HEAD == n->type); n = n->parent; assert(MAN_BLOCK == n->type); assert(MAN_SCOPED & man_macros[n->tok].flags); mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, n->pos, "%s breaks %s", man_macronames[tok], man_macronames[n->tok]); man_node_delete(man, n); man->flags &= ~MAN_BLINE; } /* Remember whether we are in next-line scope for a block head. */ bline = man->flags & MAN_BLINE; /* Call to handler... */ assert(man_macros[tok].fp); if ( ! (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf)) return(0); /* In quick mode (for mandocdb), abort after the NAME section. */ if (man->quick && MAN_SH == tok) { n = man->last; if (MAN_BODY == n->type && strcmp(n->prev->child->string, "NAME")) return(2); } /* * If we are in a next-line scope for a block head, * close it out now and switch to the body, * unless the next-line scope is allowed to continue. */ if ( ! bline || man->flags & MAN_ELINE || man_macros[tok].flags & MAN_NSCOPED) return(1); assert(MAN_BLINE & man->flags); man->flags &= ~MAN_BLINE; if ( ! man_unscope(man, man->last->parent)) return(0); return(man_body_alloc(man, ln, ppos, man->last->tok)); }
void man_breakscope(struct roff_man *man, int tok) { struct roff_node *n; /* * An element next line scope is open, * and the new macro is not allowed inside elements. * Delete the element that is being broken. */ if (man->flags & MAN_ELINE && (tok < MAN_TH || ! (man_macros[tok].flags & MAN_NSCOPED))) { n = man->last; assert(n->type != ROFFT_TEXT); if (man_macros[n->tok].flags & MAN_NSCOPED) n = n->parent; mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, n->pos, "%s breaks %s", roff_name[tok], roff_name[n->tok]); roff_node_delete(man, n); man->flags &= ~MAN_ELINE; } /* * Weird special case: * Switching fill mode closes section headers. */ if (man->flags & MAN_BLINE && (tok == MAN_nf || tok == MAN_fi) && (man->last->tok == MAN_SH || man->last->tok == MAN_SS)) { n = man->last; man_unscope(man, n); roff_body_alloc(man, n->line, n->pos, n->tok); man->flags &= ~MAN_BLINE; } /* * A block header next line scope is open, * and the new macro is not allowed inside block headers. * Delete the block that is being broken. */ if (man->flags & MAN_BLINE && (tok < MAN_TH || man_macros[tok].flags & MAN_BSCOPE)) { n = man->last; if (n->type == ROFFT_TEXT) n = n->parent; if ( ! (man_macros[n->tok].flags & MAN_BSCOPE)) n = n->parent; assert(n->type == ROFFT_HEAD); n = n->parent; assert(n->type == ROFFT_BLOCK); assert(man_macros[n->tok].flags & MAN_SCOPED); mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, n->pos, "%s breaks %s", roff_name[tok], roff_name[n->tok]); roff_node_delete(man, n); man->flags &= ~MAN_BLINE; } }
static int man_pmacro(struct roff_man *man, int ln, char *buf, int offs) { struct roff_node *n; const char *cp; size_t sz; enum roff_tok tok; int ppos; int bline; /* Determine the line macro. */ ppos = offs; tok = TOKEN_NONE; for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++) offs++; if (sz > 0 && sz < 4) tok = roffhash_find(man->manmac, buf + ppos, sz); if (tok == TOKEN_NONE) { mandoc_msg(MANDOCERR_MACRO, man->parse, ln, ppos, buf + ppos - 1); return 1; } /* Skip a leading escape sequence or tab. */ switch (buf[offs]) { case '\\': cp = buf + offs + 1; mandoc_escape(&cp, NULL, NULL); offs = cp - buf; break; case '\t': offs++; break; default: break; } /* Jump to the next non-whitespace word. */ while (buf[offs] == ' ') offs++; /* * Trailing whitespace. Note that tabs are allowed to be passed * into the parser as "text", so we only warn about spaces here. */ if (buf[offs] == '\0' && buf[offs - 1] == ' ') mandoc_msg(MANDOCERR_SPACE_EOL, man->parse, ln, offs - 1, NULL); /* * Some macros break next-line scopes; otherwise, remember * whether we are in next-line scope for a block head. */ man_breakscope(man, tok); bline = man->flags & MAN_BLINE; /* * If the line in next-line scope ends with \c, keep the * next-line scope open for the subsequent input line. * That is not at all portable, only groff >= 1.22.4 * does it, but *if* this weird idiom occurs in a manual * page, that's very likely what the author intended. */ if (bline) { cp = strchr(buf + offs, '\0') - 2; if (cp >= buf && cp[0] == '\\' && cp[1] == 'c') bline = 0; } /* Call to handler... */ assert(man_macros[tok].fp); (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf); /* In quick mode (for mandocdb), abort after the NAME section. */ if (man->quick && tok == MAN_SH) { n = man->last; if (n->type == ROFFT_BODY && strcmp(n->prev->child->string, "NAME")) return 2; } /* * If we are in a next-line scope for a block head, * close it out now and switch to the body, * unless the next-line scope is allowed to continue. */ if ( ! bline || man->flags & MAN_ELINE || man_macros[tok].flags & MAN_NSCOPED) return 1; assert(man->flags & MAN_BLINE); man->flags &= ~MAN_BLINE; man_unscope(man, man->last->parent); roff_body_alloc(man, ln, ppos, man->last->tok); return 1; }
int man_macroend(struct man *man) { return(man_unscope(man, man->first, MANDOCERR_SCOPEEXIT)); }
static int man_pmacro(struct roff_man *man, int ln, char *buf, int offs) { struct roff_node *n; const char *cp; int tok; int i, ppos; int bline; char mac[5]; ppos = offs; /* * Copy the first word into a nil-terminated buffer. * Stop when a space, tab, escape, or eoln is encountered. */ i = 0; while (i < 4 && strchr(" \t\\", buf[offs]) == NULL) mac[i++] = buf[offs++]; mac[i] = '\0'; tok = (i > 0 && i < 4) ? man_hash_find(mac) : TOKEN_NONE; if (tok == TOKEN_NONE) { mandoc_msg(MANDOCERR_MACRO, man->parse, ln, ppos, buf + ppos - 1); return 1; } /* Skip a leading escape sequence or tab. */ switch (buf[offs]) { case '\\': cp = buf + offs + 1; mandoc_escape(&cp, NULL, NULL); offs = cp - buf; break; case '\t': offs++; break; default: break; } /* Jump to the next non-whitespace word. */ while (buf[offs] && buf[offs] == ' ') offs++; /* * Trailing whitespace. Note that tabs are allowed to be passed * into the parser as "text", so we only warn about spaces here. */ if (buf[offs] == '\0' && buf[offs - 1] == ' ') mandoc_msg(MANDOCERR_SPACE_EOL, man->parse, ln, offs - 1, NULL); /* * Some macros break next-line scopes; otherwise, remember * whether we are in next-line scope for a block head. */ man_breakscope(man, tok); bline = man->flags & MAN_BLINE; /* Call to handler... */ assert(man_macros[tok].fp); (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf); /* In quick mode (for mandocdb), abort after the NAME section. */ if (man->quick && tok == MAN_SH) { n = man->last; if (n->type == ROFFT_BODY && strcmp(n->prev->child->string, "NAME")) return 2; } /* * If we are in a next-line scope for a block head, * close it out now and switch to the body, * unless the next-line scope is allowed to continue. */ if ( ! bline || man->flags & MAN_ELINE || man_macros[tok].flags & MAN_NSCOPED) return 1; assert(man->flags & MAN_BLINE); man->flags &= ~MAN_BLINE; man_unscope(man, man->last->parent); roff_body_alloc(man, ln, ppos, man->last->tok); return 1; }