+ * Warn when "n" is an explicit non-roff macro.
+ */
+static void
+rew_warn(struct man *man, struct man_node *n, enum mandocerr er)
+{
+
+ if (er == MANDOCERR_MAX || MAN_BLOCK != n->type)
+ return;
+ if (MAN_VALID & n->flags)
+ return;
+ if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
+ return;
+
+ assert(er < MANDOCERR_FATAL);
+ man_nmsg(man, n, er);
+}
+
+
+/*
+ * Rewind scope. If a code "er" != MANDOCERR_MAX has been provided, it
+ * will be used if an explicit block scope is being closed out.
+ */
+int
+man_unscope(struct man *man, const struct man_node *to,
+ enum mandocerr er)
+{
+ struct man_node *n;
+
+ assert(to);
+
+ man->next = MAN_NEXT_SIBLING;
+
+ /* LINTED */
+ while (man->last != to) {
+ /*
+ * Save the parent here, because we may delete the
+ * man->last node in the post-validation phase and reset
+ * it to man->last->parent, causing a step in the closing
+ * out to be lost.
+ */
+ n = man->last->parent;
+ rew_warn(man, man->last, er);
+ if ( ! man_valid_post(man))
+ return(0);
+ man->last = n;
+ assert(man->last);
+ }
+
+ rew_warn(man, man->last, er);
+ if ( ! man_valid_post(man))
+ return(0);
+
+ 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) {
+ if (MAN_EXPLICIT & man_macros[n->tok].flags)
+ return(REW_HALT);
+ else
+ 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):
+ /* Preserve empty paragraphs before RS. */
+ if (0 == n->nchild && (MAN_P == n->tok ||
+ MAN_PP == n->tok || MAN_LP == n->tok))
+ return(REW_HALT);
+ /* 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.