man.c revision 260e9a87725c090ba5835b1f9f0b62fa2f96036f
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* $Id: man.c,v 1.149 2015/01/30 21:28:46 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
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_macronames;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic void man_breakscope(struct man *, enum mant);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic struct man_node *man_node_alloc(struct man *, int, int,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic void man_node_append(struct man *, struct man_node *);
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);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovman_alloc(struct roff *roff, struct mparse *parse,
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreman_parseln(struct man *man, int ln, char *buf, int offs)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (man->last->type != MAN_EQN || ln > man->last->line)
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 *
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovman_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_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);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovman_word_append(struct man *man, const char *word)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
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).
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov 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. */
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. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Warn if the last un-escaped character is whitespace. Then
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * strip away the remaining spaces (tabs stay!).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Spin back to non-space. */ ;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Jump ahead of escaped whitespace. */
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)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const char *cp;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copy the first word into a nil-terminated buffer.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Stop when a space, tab, escape, or eoln is encountered.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Skip a leading escape sequence or tab. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Jump to the next non-whitespace 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.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Some macros break next-line scopes; otherwise, remember
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * whether we are in next-line scope for a block head.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Call to handler... */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* In quick mode (for mandocdb), abort after the NAME section. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * If we are in a next-line scope for a block head,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * close it out now and switch to the body,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * unless the next-line scope is allowed to continue.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * An element next line scope is open,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * and the new macro is not allowed inside elements.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Delete the element that is being broken.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (man->flags & MAN_ELINE && (tok == MAN_MAX ||
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * A block header next line scope is open,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * and the new macro is not allowed inside block headers.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Delete the block that is being broken.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (man->flags & MAN_BLINE && (tok == MAN_MAX ||
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);*/
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovman_deroff(char **dest, const struct man_node *n)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Skip leading whitespace and escape sequences. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Skip trailing whitespace. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Skip empty strings. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);