mdoc_macro.c revision 698f87a48e2e945bfe5493ce168e0d0ae1cedd5c
/* $Id: mdoc_macro.c,v 1.125 2013/12/24 20:45:27 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010, 2012, 2013 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#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"
enum rew { /* see rew_dohalt() */
};
static int blk_full(MACRO_PROT_ARGS);
static int blk_exp_close(MACRO_PROT_ARGS);
static int blk_part_exp(MACRO_PROT_ARGS);
static int blk_part_imp(MACRO_PROT_ARGS);
static int ctx_synopsis(MACRO_PROT_ARGS);
static int in_line_eoln(MACRO_PROT_ARGS);
static int in_line_argn(MACRO_PROT_ARGS);
static int in_line(MACRO_PROT_ARGS);
static int obsolete(MACRO_PROT_ARGS);
static int phrase_ta(MACRO_PROT_ARGS);
enum mdelim, int);
static int append_delims(struct mdoc *,
int, int *, char *);
static enum mdoct lookup_raw(const char *);
struct mdoc *, int, int);
const struct mdoc_node *);
const struct mdoc_node *);
enum mdoct, int, int);
{ in_line_eoln, 0 }, /* Pp */
{ in_line_eoln, 0 }, /* Ex */
{ in_line_eoln, 0 }, /* Fd */
{ obsolete, 0 }, /* Ot */
{ 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_eoln, 0 }, /* Sm */
{ in_line_eoln, 0 }, /* Bt */
{ in_line_eoln, 0 }, /* Hf */
{ obsolete, 0 }, /* Fr */
{ in_line_eoln, 0 }, /* Ud */
{ in_line, 0 }, /* Lb */
{ in_line_eoln, 0 }, /* Lp */
{ obsolete, 0 }, /* Es */
{ obsolete, 0 }, /* En */
{ 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.
*/
int
{
struct mdoc_node *n;
/* Scan for open explicit scopes. */
for ( ; n; n = n->parent)
if (MDOC_BLOCK == n->type &&
/* Rewind to the first. */
}
/*
* Look up a macro from within a subsequent context.
*/
static enum mdoct
{
return(MDOC_MAX);
return(lookup_raw(p));
}
/*
* Lookup a macro following the initial line macro.
*/
static enum mdoct
lookup_raw(const char *p)
{
return(MDOC_MAX);
return(res);
return(MDOC_MAX);
}
static int
{
/* LINTED */
/*
* 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.
*/
if ( ! mdoc_valid_post(mdoc))
return(0);
}
return(mdoc_valid_post(mdoc));
}
/*
* 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 */
}
/*
* Rewinding to tok, how do we have to handle *p?
* REWIND_NONE: *p would delimit tok, but no tok scope is open
* inside *p, so there is no need to rewind anything at all.
* REWIND_THIS: *p matches tok, so rewind *p and nothing else.
* REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
* REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
* REWIND_LATER: *p is explicit and still open, postpone rewinding.
* REWIND_ERROR: No tok block is open at all.
*/
static enum rew
const struct mdoc_node *p)
{
/*
* No matching token, no delimiting block, no broken block.
* This can happen when full implicit macros are called for
* the first time but try to rewind their previous
* instance anyway.
*/
return(MDOC_BLOCK == type &&
/*
* When starting to rewind, skip plain text
* and nodes that have already been rewound.
*/
return(REWIND_MORE);
/*
* The easiest case: Found a matching token.
* This applies to both blocks and elements.
*/
return(p->end ? REWIND_NONE :
/*
* While elements do require rewinding for themselves,
* they never affect rewinding of other nodes.
*/
return(REWIND_MORE);
/*
* Blocks delimited by our target token get REWIND_MORE.
* Blocks delimiting our target token get REWIND_NONE.
*/
switch (tok) {
case (MDOC_Bl):
return(REWIND_MORE);
break;
case (MDOC_It):
return(REWIND_NONE);
break;
/*
* XXX Badly nested block handling still fails badly
* when one block is breaking two blocks of the same type.
* This is an incomplete and extremely ugly workaround,
* required to let the OpenBSD tree build.
*/
case (MDOC_Oo):
return(REWIND_MORE);
break;
case (MDOC_Nm):
return(REWIND_NONE);
case (MDOC_Nd):
/* FALLTHROUGH */
case (MDOC_Ss):
return(REWIND_NONE);
/* FALLTHROUGH */
case (MDOC_Sh):
return(REWIND_MORE);
break;
default:
break;
}
/*
* Default block rewinding rules.
* In particular, always skip block end markers,
* and let all blocks rewind Nm children.
*/
(MDOC_BLOCK == p->type &&
return(REWIND_MORE);
/*
* By default, closing out full blocks
* forces closing of broken explicit blocks,
* while closing out partial blocks
* allows delayed rewinding by default.
*/
}
static int
{
struct mdoc_node *n;
n = n->parent;
}
/*
* We are trying to close a block identified by tok,
* but the child block *broken is still open.
* Thus, postpone closing the tok block
* until the rew_sub call closing *broken.
*/
static int
{
/*
* Iterate backwards, searching for the block matching tok,
* that is, the block breaking the *broken block.
*/
/*
* If the *broken block had already been broken before
* and we encounter its breaker, make the tok block
* pending on the inner breaker.
* Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
* becomes "[A broken=[B [C->B B] tok=A] C]"
* and finally "[A [B->A [C->B B] A] C]".
*/
continue;
}
continue;
/*
* Found the breaker.
* If another, outer breaker is already pending on
* the *broken block, we must not clobber the link
* to the outer breaker, but make it pending on the
* new, now inner breaker.
* Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
* becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
* and finally "[A [B->A [C->B A] B] C]".
*/
/*
* If the breaker had also been broken before,
* it cannot take on the outer breaker itself,
* but must hand it on to its own breakers.
* Graphically, this is the following situation:
* "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
* "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
*/
}
return(1);
}
/*
* Found no matching block for tok.
* Are you trying to close a block that is not open?
*/
return(0);
}
static int
{
struct mdoc_node *n;
while (n) {
switch (rew_dohalt(tok, t, n)) {
case (REWIND_NONE):
return(1);
case (REWIND_THIS):
break;
case (REWIND_FORCE):
mdoc_macronames[n->tok]);
/* FALLTHROUGH */
case (REWIND_MORE):
n = n->parent;
continue;
case (REWIND_LATER):
MDOC_BLOCK != t)
return(1);
/* FALLTHROUGH */
case (REWIND_ERROR):
return(1);
}
break;
}
assert(n);
return(0);
/*
* The current block extends an enclosing block.
* Now that the current block ends, close the enclosing block, too.
*/
return(0);
return(0);
}
return(1);
}
/*
* Allocate a word and check whether it's punctuation or not.
* Punctuation consists of those tokens found in mdoc_isdelim().
*/
static int
enum mdelim d, int may_append)
{
if (DELIM_MAX == d)
d = mdoc_isdelim(p);
if (may_append &&
mdoc_word_append(mdoc, p);
return(1);
}
return(0);
if (DELIM_OPEN == d)
/*
* Closing delimiters only suppress the preceding space
* when they follow something, not when they start a new
* block or element, and not when they follow `No'.
*
* XXX Explicitly special-casing MDOC_No here feels
* like a layering violation. Find a better way
* and solve this in the code related to `No'!
*/
return(1);
}
static int
{
int la;
char *p;
return(1);
for (;;) {
if (ARGS_ERROR == ac)
return(0);
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), 0))
}
return(1);
}
/*
*/
static int
{
struct mdoc_node *n; /* For searching backwards. */
char *p;
switch (tok) {
case (MDOC_Ec):
maxargs = 1;
break;
case (MDOC_Ek):
default:
maxargs = 0;
break;
}
/*
* Search backwards for beginnings of blocks,
* both of our own and of pending sub-blocks.
*/
if (MDOC_VALID & n->flags)
continue;
/* Remember the start of our own body. */
if (ENDBODY_NOT == n->end)
body = n;
continue;
}
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_sub() closing out the sub-block.
*/
/*
* Mark the place where the formatting - but not
* the scope - of the current block ends.
*/
return(0);
break;
}
/*
* When finding an open sub block, remember the last
* open explicit block, or, in case there are only
* implicit ones, the first open implicit block.
*/
if (later &&
continue;
later = n;
}
/* FIXME: do this in validate */
return(0);
}
return(0);
return(0);
for (flushed = j = 0; ; j++) {
return(0);
flushed = 1;
}
if (ARGS_ERROR == ac)
return(0);
if (ARGS_PUNCT == ac)
break;
break;
return(0);
continue;
}
if ( ! flushed) {
return(0);
flushed = 1;
}
return(0);
break;
}
return(0);
if ( ! nl)
return(1);
}
static int
{
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;
}
break;
}
break;
continue;
return(0);
}
if (ARGS_ERROR == ac)
return(0);
break;
if (ARGS_PUNCT == ac)
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.
*/
return(0);
return(0);
return(0);
}
return(0);
if ( ! nl)
return(1);
}
/*
* 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 omitted, no scope is open, and we're
* allowed to have an empty element, then start
* a new scope. `Ar', `Fl', and `Li', only do
* this once per invocation. There may be more
* of these (all of them?).
*/
DELIM_CLOSE == d && ! scope) {
return(0);
cnt++;
scope = 1;
}
/*
* Close out our scope, if one is open, before
* any punctuation.
*/
return(0);
scope = 0;
} else if ( ! scope) {
return(0);
scope = 1;
}
if (DELIM_NONE == d)
cnt++;
return(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.
*/
return(0);
scope = 0;
}
}
return(0);
/*
* 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.
*/
return(0);
return(0);
}
if ( ! nl)
return(1);
}
static int
{
struct mdoc_node *n;
char *p;
/* Close out prior implicit scope. */
return(0);
return(0);
}
/*
* 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.
*/
break;
}
break;
continue;
return(0);
}
return(0);
/*
* 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.
*/
return(0);
return(0);
return(0);
}
ac = ARGS_ERROR;
for ( ; ; ) {
/* Initialise last-phrase-type with ARGS_PEND. */
if (ARGS_PUNCT == ac)
break;
if (ARGS_ERROR == ac)
return(0);
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.
*/
return(0);
return(0);
break;
}
/*
* Emit leading punctuation (i.e., punctuation before
* the MDOC_HEAD) for non-phrase types.
*/
ARGS_PHRASE != ac &&
ARGS_PPHRASE != ac &&
ARGS_QWORD != ac &&
DELIM_OPEN == mdoc_isdelim(p)) {
return(0);
continue;
}
/* Open a head if one hasn't been opened. */
return(0);
}
if (ARGS_PHRASE == ac ||
ARGS_PPHRASE == ac) {
/*
* If we haven't opened a body yet, rewind the
* head; if we have, rewind that instead.
*/
return(0);
/* Then allocate our body context. */
return(0);
/*
* Process phrases: set whether we're in a
* partial-phrase (this effects line handling)
* then call down into the phrase parser.
*/
if (ARGS_PPHRASE == ac)
return(0);
continue;
}
return(0);
continue;
}
return(0);
break;
}
return(0);
}
return(0);
/* If we've already opened our body, exit now. */
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_sub() call closing out that
* sub-block.
*/
if (MDOC_BLOCK == n->type &&
! (MDOC_VALID & n->flags)) {
return(1);
}
}
/* Close out scopes to remain in a consistent state. */
return(0);
return(0);
out:
return(1);
return(0);
return(0);
return(1);
}
static int
{
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.
*/
return(0);
return(0);
return(0);
/*
* Open the body scope "on-demand", that is, after we've
* processed all our the leading delimiters (open parenthesis,
* etc.).
*/
if (ARGS_ERROR == ac)
return(0);
break;
if (ARGS_PUNCT == ac)
break;
DELIM_OPEN == mdoc_isdelim(p)) {
return(0);
continue;
}
return(0);
}
return(0);
continue;
}
return(0);
break;
}
/* Clean-ups to leave in a consistent state. */
return(0);
}
/* Do nothing. */ ;
/*
* End of sentence spacing: if the last node is a text node and
* has a trailing period, then mark it as being end-of-sentence.
*/
/* Up-propagate the end-of-space flag. */
}
/*
* If there is an open sub-block requiring explicit close-out,
* postpone closing out the current block
* until the rew_sub() call closing out the sub-block.
*/
n = n->parent) {
if (MDOC_BLOCK == n->type &&
! (MDOC_VALID & n->flags)) {
return(0);
return(1);
}
}
/*
* If we can't rewind to our body, then our scope has already
* been closed by another macro (like `Oc' closing `Op'). This
* is ugly behaviour nodding its head to OpenBSD's overwhelming
* crufty use of `Op' breakage.
*/
if (n != body)
return(0);
/* Standard appending of delimiters. */
return(0);
/* Rewind scope, if applicable. */
return(0);
/* Move trailing .Ns out of scope. */
/* Do nothing. */ ;
mdoc_node_relink(mdoc, n);
return(1);
}
static int
{
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.
*/
return(0);
if (ARGS_ERROR == ac)
return(0);
if (ARGS_PUNCT == ac)
break;
break;
/* Flush out leading punctuation. */
DELIM_OPEN == mdoc_isdelim(p)) {
return(0);
continue;
}
return(0);
}
/*
* `Eo' gobbles any data into the head, but most other
* macros just immediately close out and begin the body.
*/
/* No check whether it's a macro! */
return(0);
return(0);
return(0);
continue;
}
return(0);
continue;
}
return(0);
break;
}
/* Clean-up to leave in a consistent state. */
return(0);
return(0);
return(0);
}
/* Standard appending of delimiters. */
if ( ! nl)
return(1);
}
/* ARGSUSED */
static int
{
char *p;
/*
* 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_No):
/* FALLTHROUGH */
case (MDOC_Ns):
/* FALLTHROUGH */
case (MDOC_Ux):
maxargs = 0;
break;
case (MDOC_Bx):
/* FALLTHROUGH */
case (MDOC_Xr):
maxargs = 2;
break;
default:
maxargs = 1;
break;
}
break;
}
break;
continue;
return(0);
}
for (flushed = j = 0; ; ) {
if (ARGS_ERROR == ac)
return(0);
if (ARGS_PUNCT == ac)
break;
break;
ARGS_QWORD != ac && 0 == j &&
DELIM_OPEN == mdoc_isdelim(p)) {
return(0);
continue;
} else if (0 == j)
return(0);
return(0);
flushed = 1;
}
return(0);
flushed = 1;
return(0);
j++;
break;
}
ARGS_QWORD != ac &&
! flushed &&
DELIM_NONE != mdoc_isdelim(p)) {
return(0);
flushed = 1;
}
return(0);
j++;
}
return(0);
/* Close out in a consistent state. */
return(0);
if ( ! nl)
return(1);
}
static int
{
int la;
char *p;
/* Parse macro arguments. */
break;
}
break;
continue;
return(0);
}
/* Open element scope. */
return(0);
/* Parse argument terms. */
for (;;) {
if (ARGS_ERROR == ac)
return(0);
break;
return(0);
continue;
}
return(0);
}
/* Close out (no delimiters). */
}
/* ARGSUSED */
static int
{
int nl;
/* If we're not in the SYNOPSIS, go straight to in-line. */
/* If we're a nested call, same place. */
if ( ! nl)
/*
* XXX: this will open a block scope; however, if later we end
* up formatting the block scope, then child nodes will inherit
* the formatting. Be careful.
*/
}
/* ARGSUSED */
static int
{
return(1);
}
/*
* 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 int
{
char *p;
if (ARGS_ERROR == ac)
return(0);
break;
return(0);
continue;
}
return(0);
}
return(1);
}
/* ARGSUSED */
static int
{
struct mdoc_node *n;
int la;
char *p;
/* Make sure we are in a column list or ignore this macro. */
n = n->parent;
return(1);
}
/* Advance to the next column. */
return(0);
return(0);
for (;;) {
if (ARGS_ERROR == ac)
return(0);
break;
return(0);
continue;
}
return(0);
}
return(1);
}