/* $Id: mdoc_macro.c,v 1.206 2015/10/20 02:01:32 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "mandoc.h"
#include "roff.h"
#include "mdoc.h"
#include "libmandoc.h"
#include "roff_int.h"
#include "libmdoc.h"
static void blk_full(MACRO_PROT_ARGS);
static void blk_exp_close(MACRO_PROT_ARGS);
static void blk_part_exp(MACRO_PROT_ARGS);
static void blk_part_imp(MACRO_PROT_ARGS);
static void ctx_synopsis(MACRO_PROT_ARGS);
static void in_line_eoln(MACRO_PROT_ARGS);
static void in_line_argn(MACRO_PROT_ARGS);
static void in_line(MACRO_PROT_ARGS);
static void phrase_ta(MACRO_PROT_ARGS);
static void append_delims(struct roff_man *, int, int *, char *);
enum mdelim, int);
static int find_pending(struct roff_man *, int, int, int,
struct roff_node *);
static int macro_or_word(MACRO_PROT_ARGS, int);
static int parse_rest(struct roff_man *, int, int, int *, char *);
static int rew_alt(int);
static void rew_pending(struct roff_man *,
const struct roff_node *);
{ in_line_eoln, 0 }, /* Pp */
{ in_line_eoln, 0 }, /* Ex */
{ in_line_eoln, 0 }, /* Fd */
{ in_line_eoln, 0 }, /* Rv */
{ in_line_eoln, 0 }, /* %N */
{ in_line_eoln, 0 }, /* %P */
{ in_line_eoln, 0 }, /* %V */
{ in_line_eoln, 0 }, /* Db */
{ in_line_argn, 0 }, /* Sm */
{ in_line_eoln, 0 }, /* Bt */
{ in_line_eoln, 0 }, /* Hf */
{ in_line_eoln, 0 }, /* Ud */
{ in_line, 0 }, /* Lb */
{ in_line_eoln, 0 }, /* Lp */
{ in_line_eoln, 0 }, /* br */
{ in_line_eoln, 0 }, /* sp */
{ in_line_eoln, 0 }, /* %U */
};
/*
* This is called at the end of parsing. It must traverse up the tree,
* closing out open [implicit] scopes. Obviously, open explicit scopes
* are errors.
*/
void
{
struct roff_node *n;
/* Scan for open explicit scopes. */
for ( ; n; n = n->parent)
if (n->type == ROFFT_BLOCK &&
/* Rewind to the first. */
}
/*
* Look up the macro at *p called by "from",
* or as a line macro if from == TOKEN_NONE.
*/
static int
{
int res;
return TOKEN_NONE;
}
res = mdoc_hash_find(p);
if (res != TOKEN_NONE) {
return res;
}
}
return TOKEN_NONE;
}
/*
* Rewind up to and including a specific node.
*/
static void
{
return;
}
}
/*
* Rewind up to a specific block, including all blocks that broke it.
*/
static void
{
for (;;) {
switch (n->type) {
case ROFFT_HEAD:
n->tok);
return;
case ROFFT_BLOCK:
break;
default:
return;
}
if ( ! (n->flags & MDOC_BROKEN))
return;
} else
for (;;) {
return;
if (n->type == ROFFT_BLOCK ||
n->type == ROFFT_HEAD) {
if (n->flags & MDOC_ENDED)
break;
else
return;
}
}
}
}
/*
* For a block closing macro, return the corresponding opening one.
* Otherwise, return the macro itself.
*/
static int
{
switch (tok) {
case MDOC_Ac:
return MDOC_Ao;
case MDOC_Bc:
return MDOC_Bo;
case MDOC_Brc:
return MDOC_Bro;
case MDOC_Dc:
return MDOC_Do;
case MDOC_Ec:
return MDOC_Eo;
case MDOC_Ed:
return MDOC_Bd;
case MDOC_Ef:
return MDOC_Bf;
case MDOC_Ek:
return MDOC_Bk;
case MDOC_El:
return MDOC_Bl;
case MDOC_Fc:
return MDOC_Fo;
case MDOC_Oc:
return MDOC_Oo;
case MDOC_Pc:
return MDOC_Po;
case MDOC_Qc:
return MDOC_Qo;
case MDOC_Re:
return MDOC_Rs;
case MDOC_Sc:
return MDOC_So;
case MDOC_Xc:
return MDOC_Xo;
default:
return tok;
}
}
static void
{
struct roff_node *n;
if (n->type != ROFFT_ELEM)
n = n->parent;
}
/*
* If there is an open sub-block of the target requiring
* explicit close-out, postpone closing out the target until
* the rew_pending() call closing out the sub-block.
*/
static int
{
struct roff_node *n;
int irc;
irc = 0;
if (n->flags & MDOC_ENDED) {
if ( ! (n->flags & MDOC_VALID))
n->flags |= MDOC_BROKEN;
continue;
}
if (n->type == ROFFT_BLOCK &&
irc = 1;
n->flags = MDOC_BROKEN;
mdoc_macronames[n->tok]);
}
}
}
return irc;
}
/*
* Allocate a word and check whether it's punctuation or not.
* Punctuation consists of those tokens found in mdoc_isdelim().
*/
static void
enum mdelim d, int may_append)
{
if (d == DELIM_MAX)
d = mdoc_isdelim(p);
if (may_append &&
roff_word_append(mdoc, p);
return;
}
/*
* If the word consists of a bare delimiter,
* flag the new node accordingly,
* unless doing so was vetoed by the invoking macro.
* Always clear the veto, it is only valid for one word.
*/
if (d == DELIM_OPEN)
else if (d == DELIM_CLOSE &&
}
static void
{
char *p;
int la;
return;
for (;;) {
break;
/*
* If we encounter end-of-sentence symbols, then trigger
* the double-space.
*
* XXX: it's easy to allow this to propagate outward to
* the last symbol, such that `. )' will cause the
* correct double-spacing. However, (1) groff isn't
* smart enough to do this and (2) it would require
* knowing which symbols break this behaviour, for
* example, `. ;' shouldn't propagate the double-space.
*/
if (mandoc_eos(p, strlen(p)))
}
}
/*
* Parse one word.
* If it is a macro, call it and return 1.
* Otherwise, allocate it and return 0.
*/
static int
{
char *p;
int ntok;
ntok = TOKEN_NONE;
if (*p == '"')
p++;
if (ntok == TOKEN_NONE) {
return 0;
} else {
if (tok == TOKEN_NONE)
return 1;
}
}
/*
*/
static void
{
struct roff_node *n; /* Search back to our block. */
char *p;
switch (tok) {
case MDOC_Ec:
maxargs = 1;
break;
case MDOC_Ek:
/* FALLTHROUGH */
default:
maxargs = 0;
break;
}
/*
* Search backwards for beginnings of blocks,
* both of our own and of pending sub-blocks.
*/
if (n->flags & MDOC_ENDED) {
if ( ! (n->flags & MDOC_VALID))
n->flags |= MDOC_BROKEN;
continue;
}
/* Remember the start of our own body. */
if (n->end == ENDBODY_NOT)
body = n;
continue;
}
continue;
itblk = n;
continue;
}
/*
* Found the start of our own block.
* When there is no pending sub block,
* just proceed to closing out.
*/
break;
/*
* When there is a pending sub block, postpone
* closing out the current block until the
* rew_pending() closing out the sub-block.
* Mark the place where the formatting - but not
* the scope - of the current block ends.
*/
/*
* If a block closing macro taking arguments
* breaks another block, put the arguments
* into the end marker.
*/
if (maxargs)
break;
}
/* Explicit blocks close out description lines. */
continue;
}
/* Breaking an open sub block. */
n->flags |= MDOC_BROKEN;
later = n;
}
/*
* Stray .Ec without previous .Eo:
* Break the output line, keep the arguments.
*/
}
if (maxargs)
}
rew_pending(mdoc, n);
return;
}
n = endbody;
ntok = TOKEN_NONE;
for (j = 0; ; j++) {
break;
if (ntok == TOKEN_NONE) {
continue;
}
if (n != NULL)
break;
}
if (n != NULL) {
target = n;
do
target);
} else
pending = 0;
if ( ! pending)
rew_pending(mdoc, n);
}
if (nl)
}
static void
{
int ntok;
enum mdelim d;
char *p;
/*
* Whether we allow ignored elements (those without content,
* usually because of reserved words) to squeak by.
*/
switch (tok) {
case MDOC_An:
case MDOC_Ar:
case MDOC_Fl:
case MDOC_Mt:
case MDOC_Nm:
case MDOC_Pa:
nc = 1;
break;
default:
nc = 0;
break;
}
d = DELIM_NONE;
firstarg = 1;
mayopen = 1;
/*
* At the end of a macro line,
* opening delimiters do not suppress spacing.
*/
if (d == DELIM_OPEN)
break;
}
/*
* The rest of the macro line is only punctuation,
* to be handled by append_delims().
* If there were no other arguments,
* do not allow the first one to suppress spacing,
* even if it turns out to be a closing one.
*/
if (ac == ARGS_PUNCT) {
break;
}
/*
* In this case, we've located a submacro and must
* execute it. Close out scope, if open. If no
* elements have been generated, either create one (nc)
* or raise a warning.
*/
if (ntok != TOKEN_NONE) {
if (scope)
}
if (nl)
return;
}
/*
* Non-quote-enclosed punctuation. Set up our scope, if
* a word; rewind the scope, if a delimiter; then append
* the word.
*/
if (DELIM_NONE != d) {
/*
* If we encounter closing punctuation, no word
* has been emitted, no scope is open, and we're
* allowed to have an empty element, then start
* a new scope.
*/
if ((d == DELIM_CLOSE ||
scope = 1;
cnt++;
mayopen = 0;
}
/*
* Close out our scope, if one is open, before
* any punctuation.
*/
if (scope)
scope = 0;
mayopen = 0;
scope = 1;
cnt++;
}
/*
* If the first argument is a closing delimiter,
* do not suppress spacing before it.
*/
firstarg = 0;
/*
* `Fl' macros have their scope re-opened with each new
* word so that the `-' can be added to each one without
* having to parse out spaces.
*/
scope = 0;
}
}
if (scope)
/*
* If no elements have been collected and we're allowed to have
* empties (nc), open a scope and close it out. Otherwise,
* raise a warning.
*/
if ( ! cnt) {
if (nc) {
} else {
}
}
if (nl)
}
static void
{
struct roff_node *n;
char *p;
return;
}
/* Here, tok is one of Sh Ss Nm Nd It. */
if (n->flags & MDOC_ENDED) {
if ( ! (n->flags & MDOC_VALID))
n->flags |= MDOC_BROKEN;
continue;
}
if (n->type != ROFFT_BLOCK)
continue;
"It breaks %s",
}
break;
}
switch (tok) {
case MDOC_Sh:
case MDOC_Ss:
"%s breaks %s",
mdoc_macronames[n->tok]);
rew_pending(mdoc, n);
continue;
case MDOC_It:
/* Delay in case it's astray. */
blk = n;
continue;
default:
break;
}
break;
}
/* Here, n is one of Sh Ss Nm Nd It. */
break;
/* Item breaking an explicit block. */
"It breaks %s",
}
/* Close out prior implicit scopes. */
}
/* Skip items outside lists. */
return;
}
}
/*
* This routine accommodates implicitly- and explicitly-scoped
* macro openings. Implicit ones first close out prior scope
* (seen above). Delay opening the head until necessary to
* allow leading punctuation to print. Special consideration
* for `It -column', which has phrase-part syntax instead of
* regular child nodes.
*/
/*
* Exception: Heads of `It' macros in `-diag' lists are not
* parsed, even though `It' macros in general are parsed.
*/
/*
* The `Nd' macro has all arguments in its body: it's a hybrid
* of block partial-explicit and full-implicit. Stupid.
*/
}
for (;;) {
/*
* If we are right after a tab character,
* do not parse the first word for macros.
*/
}
if (lac != ARGS_PHRASE ||
break;
/*
* This line ends in a tab; start the next
* column now, with a leading blank.
*/
break;
}
break;
}
break;
}
if (ac == ARGS_PUNCT)
break;
/*
* Emit leading punctuation (i.e., punctuation before
* the ROFFT_HEAD) for non-phrase types.
*/
ac != ARGS_PHRASE &&
ac != ARGS_QWORD &&
mdoc_isdelim(p) == DELIM_OPEN) {
continue;
}
/* Open a head if one hasn't been opened. */
if (ac == ARGS_PHRASE) {
/*
* If we haven't opened a body yet, rewind the
* head; if we have, rewind that instead.
*/
/* Process to the tab or to the end of the line. */
/* There may have been `Ta' macros. */
continue;
}
break;
}
return;
goto out;
return;
/* Close out scopes to remain in a consistent state. */
out:
}
}
static void
{
char *p;
struct roff_node *n;
/*
* A macro that spans to the end of the line. This is generally
* (but not necessarily) called as the first macro. The block
* has a head as the immediate child, which is always empty,
* followed by zero or more opening punctuation nodes, then the
* body (which may be empty, depending on the macro), then zero
* or more closing punctuation nodes.
*/
/*
* Open the body scope "on-demand", that is, after we've
* processed all our the leading delimiters (open parenthesis,
* etc.).
*/
break;
mdoc_isdelim(p) == DELIM_OPEN) {
continue;
}
break;
}
return;
if (nl)
/* Move trailing .Ns out of scope. */
/* Do nothing. */ ;
mdoc_node_relink(mdoc, n);
}
static void
{
char *p;
/*
* The opening of an explicit macro having zero or more leading
* punctuation nodes; a head with optional single element (the
* case of `Eo'); and a body that may be empty.
*/
for (;;) {
break;
/* Flush out leading punctuation. */
mdoc_isdelim(p) == DELIM_OPEN) {
continue;
}
continue;
}
break;
}
/* Clean-up to leave in a consistent state. */
}
if (nl)
}
static void
{
char *p;
int ntok;
/*
* A line macro that has a fixed number of arguments (maxargs).
* Only open the scope once the first non-leading-punctuation is
* found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
* keep it open until the maximum number of arguments are
* exhausted.
*/
switch (tok) {
case MDOC_Ap:
case MDOC_Ns:
case MDOC_Ux:
maxargs = 0;
break;
case MDOC_Bx:
case MDOC_Es:
case MDOC_Xr:
maxargs = 2;
break;
default:
maxargs = 1;
break;
}
state = -1;
p = NULL;
for (;;) {
mdoc_isdelim(p) == DELIM_OPEN) {
continue;
}
state = 0;
}
p == NULL ? "at eol" : p);
break;
}
state = -2;
}
if (ntok != TOKEN_NONE) {
if (state >= 0) {
state = -2;
}
break;
}
if (ac == ARGS_QWORD ||
mdoc_isdelim(p) == DELIM_NONE) {
if (state == -1) {
state = 1;
} else if (state >= 0)
state++;
} else if (state >= 0) {
state = -2;
}
}
if (state == -1) {
return;
}
if (state >= 0)
if (nl)
}
static void
{
struct roff_node *n;
n = n->parent;
}
return;
}
return;
}
/*
* The simplest argument parser available: Parse the remaining
* words until the end of the phrase or line and return 0
* or until the next macro, call that macro, and return 1.
*/
static int
{
int la;
for (;;) {
return 0;
return 1;
}
}
static void
{
else {
}
}
/*
* Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
* They're unusual because they're basically free-form text until a
* macro is encountered.
*/
static void
{
/* Make sure we are in a column list or ignore this macro. */
if (n->flags & MDOC_ENDED)
continue;
body = n;
break;
}
return;
}
/* Advance to the next column. */
}