371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov/* $Id: mdoc.c,v 1.256 2015/10/30 19:04:16 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.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.
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.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovconst char *const __mdoc_macronames[MDOC_MAX + 1] = {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovconst char *const __mdoc_argnames[MDOC_ARG_MAX] = {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovconst char * const *mdoc_macronames = __mdoc_macronames + 1;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreconst char * const *mdoc_argnames = __mdoc_argnames;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovstatic int mdoc_ptext(struct roff_man *, int, char *, int);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovstatic int mdoc_pmacro(struct roff_man *, int, char *, int);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Main parse routine. Parses a single line -- really just hands off to
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovmdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Let the roff nS register switch SYNOPSIS mode early,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * such that the parser knows at all times
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * whether this mode is on or off.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Note that this mode is also switched by the Sh macro.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return roff_getcontrol(mdoc->roff, buf, &offs) ?
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovmdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovmdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovmdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovmdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovmdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Parse free-form text, that is, a line that does not begin with the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * control character.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovmdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Divert directly to list processing if we're encountering a
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * (a ROFFT_BODY means it's already open, in which case we should
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * process within its context in the normal way).
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* `Bl' is open without any children. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* `Bl' has block-level `It' children. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Search for the beginning of unescaped trailing whitespace (ws)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * and for the first character not to be output (end).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FIXME: replace with strcspn(). */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (*c) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Always warn about trailing tabs,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * even outside literal context,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * where they should be put on the next line.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Strip trailing tabs in literal context only;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * outside, they affect the next line.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Skip the escaped character, too, if any. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Insert a `sp' in the case of a blank line. Technically,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * blank lines aren't allowed, but enough manuals assume this
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * behaviour that we want to work around it.
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.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Parse a macro line, that is, a line beginning with the control
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankovmdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
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)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Skip a leading escape sequence or tab. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* 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.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ('\0' == buf[offs] && ' ' == buf[offs - 1])
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If an initial macro or a list invocation, divert directly
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * into macro processing.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If the first macro of a `Bl -column', open an `It' block
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * context around the parsed macro.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If we're following a block-level `It' within a `Bl -column'
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * context (perhaps opened in the above block or in ptext()),
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * then open an `It' block context around the parsed macro.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Normal processing of a macro. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* In quick mode (for mandocdb), abort after the NAME section. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ('\0' == p[0])
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (p[0]) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ('\\' != p[0])