mdoc_macro.c revision 260e9a87725c090ba5835b1f9f0b62fa2f96036f
/* $Id: mdoc_macro.c,v 1.183 2015/02/12 12:24:33 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 AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 "mdoc.h"
#include "mandoc.h"
#include "libmdoc.h"
#include "libmandoc.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);
enum mdelim, int);
static void append_delims(struct mdoc *, int, int *, char *);
int, int, const char *);
static int macro_or_word(MACRO_PROT_ARGS, int);
int, int *, char *);
{ 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 mdoc_node *n;
/* Scan for open explicit scopes. */
for ( ; n; n = n->parent)
if (n->type == MDOC_BLOCK &&
/* Rewind to the first. */
}
/*
* Look up the macro at *p called by "from",
* or as a line macro if from == MDOC_MAX.
*/
static enum mdoct
{
res = mdoc_hash_find(p);
return(res);
}
}
return(MDOC_MAX);
}
/*
* Rewind up to and including a specific node.
*/
static void
{
/*
* Save the parent here, because we may delete the
* mdoc->last node in the post-validation phase and reset
* it to mdoc->last->parent, causing a step in the closing
* out to be lost.
*/
}
}
/*
* Rewind up to a specific block, including all blocks that broke it.
*/
static void
{
for (;;) {
switch (n->type) {
case MDOC_HEAD:
return;
case MDOC_BLOCK:
break;
default:
return;
}
if ( ! (n->flags & MDOC_BROKEN))
return;
for (;;) {
return;
if (n->type == MDOC_BLOCK ||
if (n->flags & MDOC_ENDED)
break;
else
return;
}
}
}
}
/*
* For a block closing macro, return the corresponding opening one.
* Otherwise, return the macro itself.
*/
static enum mdoct
{
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);
}
/* NOTREACHED */
}
static void
{
struct mdoc_node *n;
n = n->parent;
}
/*
* 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 &&
mdoc_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;
if (*p == '"')
p++;
return(0);
} else {
return(1);
}
}
/*
*/
static void
{
struct mdoc_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;
for (j = 0; ; j++) {
rew_pending(mdoc, n);
n = NULL;
}
break;
continue;
}
if (n != NULL) {
rew_pending(mdoc, n);
n = NULL;
}
break;
}
if (n != NULL)
rew_pending(mdoc, n);
if (nl)
}
static void
{
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:
/* FALLTHROUGH */
case MDOC_Ar:
/* FALLTHROUGH */
case MDOC_Fl:
/* FALLTHROUGH */
case MDOC_Mt:
/* FALLTHROUGH */
case MDOC_Nm:
/* FALLTHROUGH */
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 (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 mdoc_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 != MDOC_BLOCK)
continue;
"It breaks %s",
}
break;
}
switch (tok) {
case MDOC_Sh:
/* FALLTHROUGH */
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 (;;) {
break;
/*
* This is necessary: if the last token on a
* line is a `Ta' or tab, then we'll get
* ARGS_EOLN, so we must be smart enough to
* reopen our scope if the last parse was a
* phrase or partial phrase.
*/
break;
}
break;
}
break;
}
if (ac == ARGS_PUNCT)
break;
/*
* Emit leading punctuation (i.e., punctuation before
* the MDOC_HEAD) for non-phrase types.
*/
ac != ARGS_PHRASE &&
ac != ARGS_PPHRASE &&
ac != ARGS_QWORD &&
mdoc_isdelim(p) == DELIM_OPEN) {
continue;
}
/* Open a head if one hasn't been opened. */
if (ac == ARGS_PHRASE ||
ac == ARGS_PPHRASE) {
/*
* If we haven't opened a body yet, rewind the
* head; if we have, rewind that instead.
*/
/*
* Process phrases: set whether we're in a
* partial-phrase (this effects line handling)
* then call down into the phrase parser.
*/
if (ac == ARGS_PPHRASE)
continue;
}
break;
}
return;
goto out;
/*
* If there is an open (i.e., unvalidated) sub-block requiring
* explicit close-out, postpone switching the current block from
* head to body until the rew_pending() call closing out that
* sub-block.
*/
if (n->flags & MDOC_ENDED) {
if ( ! (n->flags & MDOC_VALID))
n->flags |= MDOC_BROKEN;
continue;
}
if (n->type == MDOC_BLOCK &&
n->flags = MDOC_BROKEN;
}
}
return;
/* Close out scopes to remain in a consistent state. */
out:
}
}
static void
{
char *p;
struct mdoc_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;
}
/*
* If there is an open sub-block requiring explicit close-out,
* postpone closing out the current block until the
* rew_pending() call closing out the sub-block.
*/
n = n->parent) {
if (n->flags & MDOC_ENDED) {
if ( ! (n->flags & MDOC_VALID))
n->flags |= MDOC_BROKEN;
continue;
}
if (n->type == MDOC_BLOCK &&
n->flags |= MDOC_BROKEN;
mdoc_macronames[n->tok]);
}
}
}
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 state; /* arg#; -1: not yet open; -2: closed */
/*
* 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:
/* FALLTHROUGH */
case MDOC_Ns:
/* FALLTHROUGH */
case MDOC_Ux:
maxargs = 0;
break;
case MDOC_Bx:
/* FALLTHROUGH */
case MDOC_Es:
/* FALLTHROUGH */
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 (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 mdoc_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. */
}