man.c revision 698f87a48e2e945bfe5493ce168e0d0ae1cedd5c
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore/* $Id: man.c,v 1.121 2013/11/10 22:54:40 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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'Amoreconst char *const __man_macronames[MAN_MAX] = {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreconst char * const *man_macronames = __man_macronames;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic struct man_node *man_node_alloc(struct man *, int, int,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void man_node_free(struct man_node *);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic int man_ptext(struct man *, int, char *, int);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic int man_pmacro(struct man *, int, char *, int);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic int man_descope(struct man *, int, int);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreman_alloc(struct roff *roff, struct mparse *parse)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_parseln(struct man *man, int ln, char *buf, int offs)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore return (roff_getcontrol(man->roff, buf, &offs) ?
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore memset(&man->meta, 0, sizeof(struct man_meta));
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore man->last = mandoc_calloc(1, sizeof(struct man_node));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreman_node_append(struct man *man, struct man_node *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* NOTREACHED */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (p->type) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (p->type) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic struct man_node *
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_node_alloc(struct man *man, int line, int pos,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p = mandoc_calloc(1, sizeof(struct man_node));
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_elem_alloc(struct man *man, int line, int pos, enum mant tok)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p = man_node_alloc(man, line, pos, MAN_ELEM, tok);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_tail_alloc(struct man *man, int line, int pos, enum mant tok)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p = man_node_alloc(man, line, pos, MAN_TAIL, tok);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_head_alloc(struct man *man, int line, int pos, enum mant tok)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p = man_node_alloc(man, line, pos, MAN_HEAD, tok);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_body_alloc(struct man *man, int line, int pos, enum mant tok)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p = man_node_alloc(man, line, pos, MAN_BODY, tok);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_block_alloc(struct man *man, int line, int pos, enum mant tok)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p = man_node_alloc(man, line, pos, MAN_BLOCK, tok);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_word_alloc(struct man *man, int line, int pos, const char *word)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Free all of the resources held by a node. This does NOT unlink a
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * node from its context; for that, see man_node_unlink().
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_node_delete(struct man *man, struct man_node *p)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_addeqn(struct man *man, const struct eqn *ep)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_addspan(struct man *man, const struct tbl_span *sp)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_descope(struct man *man, int line, int offs)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Co-ordinate what happens with having a next-line scope open:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * first close out the element scope (if applicable), then close
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * out the block scope (also if applicable).
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore return(man_body_alloc(man, line, offs, man->last->tok));
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_ptext(struct man *man, int line, char *buf, int offs)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Literal free-form text whitespace is preserved. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if ( ! man_word_alloc(man, line, offs, buf + offs))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Skip leading whitespace. */ ;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * Blank lines are ignored right after headings
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * but add a single vertical space elsewhere.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Allocate a blank entry. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if ( ! man_elem_alloc(man, line, offs, MAN_sp))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Warn if the last un-escaped character is whitespace. Then
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * strip away the remaining spaces (tabs stay!).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore man_pmsg(man, line, i - 1, MANDOCERR_EOLNSPACE);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Spin back to non-space. */ ;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Jump ahead of escaped whitespace. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if ( ! man_word_alloc(man, line, offs, buf + offs))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * End-of-sentence check. If the last character is an unescaped
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * EOS character, then flag the node as being the end of a
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * sentence. The front-end will know how to interpret this.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_pmacro(struct man *man, int ln, char *buf, int offs)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore man_pmsg(man, ln, offs, MANDOCERR_BADCOMMENT);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copy the first word into a nil-terminated buffer.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Stop copying when a tab, space, or eoln is encountered.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore mandoc_vmsg(MANDOCERR_MACRO, man->parse, ln,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* The macro is sane. Jump to the next word. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Trailing whitespace. Note that tabs are allowed to be passed
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * into the parser as "text", so we only warn about spaces here.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ('\0' == buf[offs] && ' ' == buf[offs - 1])
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore man_pmsg(man, ln, offs - 1, MANDOCERR_EOLNSPACE);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Remove prior ELINE macro, as it's being clobbered by a new
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * macro. Note that NSCOPED macros do not close out ELINE
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * macros---they don't print text---so we let those slip by.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Remove repeated NSCOPED macros causing ELINE. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore n->pos, "%s breaks %s", man_macronames[tok],
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Remove prior BLINE macro that is being clobbered.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Might be a text node like 8 in
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Remove element that didn't end BLINE, if any. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ( ! (MAN_BSCOPE & man_macros[n->tok].flags))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore assert(MAN_SCOPED & man_macros[n->tok].flags);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore n->pos, "%s breaks %s", man_macronames[tok],
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Save the fact that we're in the next-line for a block. In
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * this way, embedded roff instructions can "remember" state
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * when they exit.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Call to handler... */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if ( ! (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * We weren't in a block-line scope when entering the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * above-parsed macro, so return.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If we're in a block scope, then allow this macro to slip by
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * without closing scope around it.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If we've opened a new next-line element scope, then return
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * now, as the next line will close out the block scope.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Close out the block scope opened in the prior line. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore return(man_body_alloc(man, ln, ppos, man->last->tok));
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * Unlink a node from its context. If "man" is provided, the last parse
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * point will also be adjusted accordingly.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_node_unlink(struct man *man, struct man_node *n)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Adjust siblings. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Adjust parent. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore n->parent->child = n->prev ? n->prev : n->next;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Adjust parse point, if applicable. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*XXX: this can occur when bailing from validation. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*assert(NULL == n->next);*/