+man_unscope(struct man *m, const struct man_node *n, enum merr er)
+{
+
+ assert(n);
+
+ /* LINTED */
+ while (m->last != n) {
+ if ( ! rew_warn(m, m->last, er))
+ return(0);
+ if ( ! man_valid_post(m))
+ return(0);
+ if ( ! man_action_post(m))
+ return(0);
+ m->last = m->last->parent;
+ assert(m->last);
+ }
+
+ if ( ! rew_warn(m, m->last, er))
+ return(0);
+ if ( ! man_valid_post(m))
+ return(0);
+ if ( ! man_action_post(m))
+ return(0);
+
+ m->next = MAN_ROOT == m->last->type ?
+ MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
+
+ return(1);
+}
+
+
+static enum rew
+rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
+{
+
+ if (MAN_BLOCK == type && ntok == n->parent->tok &&
+ MAN_BODY == n->parent->type)
+ return(REW_REWIND);
+ return(ntok == n->tok ? REW_HALT : REW_NOHALT);
+}
+
+
+/*
+ * There are three scope levels: scoped to the root (all), scoped to the
+ * section (all less sections), and scoped to subsections (all less
+ * sections and subsections).
+ */
+static enum rew
+rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
+{
+ enum rew c;
+
+ /* We cannot progress beyond the root ever. */
+ if (MAN_ROOT == n->type)
+ return(REW_HALT);
+
+ assert(n->parent);
+
+ /* Normal nodes shouldn't go to the level of the root. */
+ if (MAN_ROOT == n->parent->type)
+ return(REW_REWIND);
+
+ /* Already-validated nodes should be closed out. */
+ if (MAN_VALID & n->flags)
+ return(REW_NOHALT);
+
+ /* First: rewind to ourselves. */
+ if (type == n->type && tok == n->tok)
+ return(REW_REWIND);
+
+ /*
+ * If we're a roff macro, then we can close out anything that
+ * stands between us and our parent context.
+ */
+ if (MAN_NOCLOSE & man_macros[tok].flags)
+ return(REW_NOHALT);
+
+ /*
+ * Don't clobber roff macros: this is a bit complicated. If the
+ * current macro is a roff macro, halt immediately and don't
+ * rewind. If it's not, and the parent is, then close out the
+ * current scope and halt at the parent.
+ */
+ if (MAN_NOCLOSE & man_macros[n->tok].flags)
+ return(REW_HALT);
+ if (MAN_NOCLOSE & man_macros[n->parent->tok].flags)
+ return(REW_REWIND);
+
+ /*
+ * Next follow the implicit scope-smashings as defined by man.7:
+ * section, sub-section, etc.
+ */
+
+ switch (tok) {
+ case (MAN_SH):
+ break;
+ case (MAN_SS):
+ /* Rewind to a section, if a block. */
+ if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
+ return(c);
+ break;
+ case (MAN_RS):
+ /* Rewind to a subsection, if a block. */
+ if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
+ return(c);
+ /* Rewind to a section, if a block. */
+ if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
+ return(c);
+ break;
+ default:
+ /* Rewind to an offsetter, if a block. */
+ if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
+ return(c);
+ /* Rewind to a subsection, if a block. */
+ if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
+ return(c);
+ /* Rewind to a section, if a block. */
+ if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
+ return(c);
+ break;
+ }
+
+ return(REW_NOHALT);
+}
+
+
+/*
+ * 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 *m, enum mant tok)
+{
+ struct man_node *n;
+ enum rew c;
+
+ /* LINTED */
+ for (n = m->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);
+ if (MAN_NOCLOSE & man_macros[tok].flags)
+ return(man_unscope(m, n, WROFFSCOPE));
+
+ return(man_unscope(m, n, WERRMAX));
+}
+
+
+/*
+ * Closure for dotted macros (de, dei, am, ami, ign). This must handle
+ * any of these as the parent node, so it needs special handling.
+ * Beyond this, it's the same as blk_close().
+ */
+/* ARGSUSED */
+int
+blk_dotted(MACRO_PROT_ARGS)
+{
+ enum mant ntok;
+ struct man_node *nn;
+
+ /* Check for any of the following parents... */
+
+ for (nn = m->last->parent; nn; nn = nn->parent)
+ if (nn->tok == MAN_de || nn->tok == MAN_dei ||
+ nn->tok == MAN_am ||
+ nn->tok == MAN_ami ||
+ nn->tok == MAN_ig) {
+ ntok = nn->tok;
+ break;
+ }
+
+ if (NULL == nn) {
+ if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
+ return(0);
+ return(1);
+ }
+
+ if ( ! rew_scope(MAN_BODY, m, ntok))
+ return(0);
+ if ( ! rew_scope(MAN_BLOCK, m, ntok))
+ return(0);
+
+ /*
+ * Restore flags set when we got here and also stipulate that we
+ * don't post-process the line when exiting the macro op
+ * function in man_pmacro(). See blk_exp().
+ */
+
+ m->flags = m->svflags | MAN_ILINE;
+ m->next = m->svnext;
+ return(1);
+}
+
+
+/*
+ * Close out a generic explicit macro.
+ */
+/* ARGSUSED */
+int
+blk_close(MACRO_PROT_ARGS)
+{
+ enum mant ntok;
+ const struct man_node *nn;
+
+ switch (tok) {
+ case (MAN_RE):
+ ntok = MAN_RS;
+ break;
+ default:
+ abort();
+ /* NOTREACHED */
+ }
+
+ for (nn = m->last->parent; nn; nn = nn->parent)
+ if (ntok == nn->tok)
+ break;
+
+ if (NULL == nn)
+ if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
+ return(0);
+
+ if ( ! rew_scope(MAN_BODY, m, ntok))
+ return(0);
+ if ( ! rew_scope(MAN_BLOCK, m, ntok))
+ return(0);
+
+ return(1);
+}
+
+
+int
+blk_exp(MACRO_PROT_ARGS)