man_macro.c revision 260e9a87725c090ba5835b1f9f0b62fa2f96036f
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* $Id: man_macro.c,v 1.98 2015/02/06 11:54:36 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2012, 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * copyright notice and this permission notice appear in all copies.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore int *, char *, char **);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic enum rew rew_dohalt(enum mant, enum man_type,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const struct man_node *);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic enum rew rew_block(enum mant, enum man_type,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const struct man_node *);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreconst struct man_macro __man_macros[MAN_MAX] = {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore { blk_imp, MAN_BSCOPE | MAN_SCOPED | MAN_FSCOPED }, /* TP */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore { blk_exp, MAN_BSCOPE | MAN_EXPLICIT }, /* RS */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore { blk_exp, MAN_BSCOPE | MAN_EXPLICIT }, /* UR */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreconst struct man_macro * const man_macros = __man_macros;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovman_unscope(struct man *man, const struct man_node *to)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov while (n != to) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Reached the end of the document? */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov "EOF breaks %s",
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * We might delete the man->last node
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * in the post-validation phase.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Save a pointer to the parent such that
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * we know where to continue the iteration.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * If we ended up at the parent of the node we were
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * supposed to rewind to, that means the target node
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * got deleted, so add the next node we parse as a child
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * of the parent instead of as a sibling of the target.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorerew_block(enum mant ntok, enum man_type type, const struct man_node *n)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (type == MAN_BLOCK && ntok == n->parent->tok &&
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore return(ntok == n->tok ? REW_HALT : REW_NOHALT);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * There are three scope levels: scoped to the root (all), scoped to the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * section (all less sections), and scoped to subsections (all less
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * sections and subsections).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorerew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* We cannot progress beyond the root ever. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Normal nodes shouldn't go to the level of the root. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Already-validated nodes should be closed out. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* First: rewind to ourselves. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (MAN_EXPLICIT & man_macros[n->tok].flags)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Next follow the implicit scope-smashings as defined by man.7:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * section, sub-section, etc.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Rewind to a section, if a block. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore /* Preserve empty paragraphs before RS. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Rewind to a subsection, if a block. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Rewind to a section, if a block. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Rewind to an offsetter, if a block. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Rewind to a subsection, if a block. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Rewind to a section, if a block. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Rewinding entails ascending the parse tree until a coherent point,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * for example, the `SH' macro will close out any intervening `SS'
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * scopes. When a scope is closed, it must be validated and actioned.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amorerew_scope(enum man_type type, struct man *man, enum mant tok)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Whether we should stop immediately (REW_HALT), stop
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * and rewind until this point (REW_REWIND), or keep
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * rewinding (REW_NOHALT).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Rewind until the current point. Warn if we're a roff
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * instruction that's mowing over explicit scopes.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Close out a generic explicit macro.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov for (nn = man->last->parent; nn; nn = nn->parent)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (*p != '\0')
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* NOTREACHED */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore for (nn = man->last->parent; nn; nn = nn->parent)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (nn->tok == ntok && nn->type == MAN_BLOCK && ! --nrew)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Move a trailing paragraph behind the block. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Parse an implicit-block macro. These contain a MAN_HEAD and a
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * scopes, such as `SH' closing out an `SS', are defined in the rew
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Add line arguments. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Close out head and open body (unless MAN_SCOPE). */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* If we're forcing scope (`TP'), keep it open. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (tok == MAN_PD || tok == MAN_ft || tok == MAN_sp)) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * Append MAN_EOS in case the last snipped argument
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * ends with a dot, e.g. `.IR syslog (3).'
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov mandoc_eos(man->last->string, strlen(man->last->string)))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If no arguments are specified and this is MAN_SCOPED (i.e.,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * next-line scoped), then set our mode to indicate that we're
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * waiting for terms to load into our context.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov assert( ! (man_macros[tok].flags & MAN_NSCOPED));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Rewind our element scope. Note that when TH is pruned, we'll
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * be back at the root, so make sure that we don't clobber as
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * its sibling.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore for ( ; man->last; man->last = man->last->parent) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Same here regarding whether we're back at the root.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_args(struct man *man, int line, int *pos, char *buf, char **v)