371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov/* $Id: man_macro.c,v 1.114 2016/01/08 17:48:09 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.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 **);
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 */
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 */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreconst struct man_macro * const man_macros = __man_macros;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovman_unscope(struct roff_man *man, const struct roff_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'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.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Preserve empty paragraphs before RS. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP))
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')
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore for (nn = man->last->parent; nn; nn = nn->parent)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Move a trailing paragraph behind the block. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * ROFFT_BODY contained within a ROFFT_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. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * For macros having optional next-line scope,
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * keep the head open if there were no arguments.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * For `TP', always keep the head open.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Close out the head and open the body. */
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));
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Rewind our element scope. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore for ( ; man->last; man->last = man->last->parent) {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovman_args(struct roff_man *man, int line, int *pos, char *buf, char **v)