#include "config.h"
#include <sys/types.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 *);
static void dword(struct roff_man *, int, int, const char *,
enum mdelim, int);
static int find_pending(struct roff_man *, enum roff_tok,
int, int, struct roff_node *);
static int lookup(struct roff_man *, int, int, int, const char *);
static int macro_or_word(MACRO_PROT_ARGS, char *, int);
static void break_intermediate(struct roff_node *,
struct roff_node *);
static int parse_rest(struct roff_man *, enum roff_tok,
int, int *, char *);
static enum roff_tok rew_alt(enum roff_tok);
static void rew_elem(struct roff_man *, enum roff_tok);
static void rew_last(struct roff_man *, const struct roff_node *);
static void rew_pending(struct roff_man *,
const struct roff_node *);
static const struct mdoc_macro mdoc_macros[MDOC_MAX - MDOC_Dd] = {
{ in_line_eoln, MDOC_PROLOGUE | MDOC_JOIN },
{ in_line_eoln, MDOC_PROLOGUE },
{ in_line_eoln, MDOC_PROLOGUE },
{ blk_full, MDOC_PARSED | MDOC_JOIN },
{ blk_full, MDOC_PARSED | MDOC_JOIN },
{ in_line_eoln, 0 },
{ blk_part_imp, MDOC_PARSED | MDOC_JOIN },
{ blk_part_imp, MDOC_PARSED | MDOC_JOIN },
{ blk_full, MDOC_EXPLICIT },
{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN },
{ blk_full, MDOC_EXPLICIT },
{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN },
{ blk_full, MDOC_PARSED | MDOC_JOIN },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
MDOC_IGNDELIM | MDOC_JOIN },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_eoln, 0 },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_eoln, 0 },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ blk_full, MDOC_JOIN },
{ ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_eoln, 0 },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, 0 },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, 0 },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, 0 },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_full, MDOC_EXPLICIT },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_eoln, 0 },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT },
{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN },
{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
MDOC_IGNDELIM | MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN },
{ blk_full, MDOC_EXPLICIT },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line_argn, 0 },
{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT },
{ blk_full, MDOC_EXPLICIT | MDOC_CALLABLE },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_full, MDOC_EXPLICIT },
{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN },
{ in_line_eoln, 0 },
{ in_line_eoln, 0 },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_eoln, 0 },
{ in_line, 0 },
{ in_line_eoln, 0 },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ in_line, MDOC_CALLABLE | MDOC_PARSED },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
MDOC_EXPLICIT | MDOC_JOIN },
{ in_line_eoln, MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED },
{ in_line_eoln, MDOC_JOIN },
{ in_line_eoln, 0 },
{ phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN },
{ in_line_eoln, 0 },
};
const struct mdoc_macro *
mdoc_macro(enum roff_tok tok)
{
assert(tok >= MDOC_Dd && tok < MDOC_MAX);
return mdoc_macros + (tok - MDOC_Dd);
}
void
mdoc_endparse(struct roff_man *mdoc)
{
struct roff_node *n;
n = mdoc->last->flags & NODE_VALID ?
mdoc->last->parent : mdoc->last;
for ( ; n; n = n->parent)
if (n->type == ROFFT_BLOCK &&
mdoc_macro(n->tok)->flags & MDOC_EXPLICIT)
mandoc_msg(MANDOCERR_BLK_NOEND,
n->line, n->pos, "%s", roff_name[n->tok]);
rew_last(mdoc, mdoc->meta.first);
}
static int
lookup(struct roff_man *mdoc, int from, int line, int ppos, const char *p)
{
enum roff_tok res;
if (mdoc->flags & MDOC_PHRASEQF) {
mdoc->flags &= ~MDOC_PHRASEQF;
return TOKEN_NONE;
}
if (from == TOKEN_NONE || mdoc_macro(from)->flags & MDOC_PARSED) {
res = roffhash_find(mdoc->mdocmac, p, 0);
if (res != TOKEN_NONE) {
if (mdoc_macro(res)->flags & MDOC_CALLABLE)
return res;
mandoc_msg(MANDOCERR_MACRO_CALL, line, ppos, "%s", p);
}
}
return TOKEN_NONE;
}
static void
rew_last(struct roff_man *mdoc, const struct roff_node *to)
{
if (to->flags & NODE_VALID)
return;
while (mdoc->last != to) {
mdoc_state(mdoc, mdoc->last);
mdoc->last->flags |= NODE_VALID | NODE_ENDED;
mdoc->last = mdoc->last->parent;
}
mdoc_state(mdoc, mdoc->last);
mdoc->last->flags |= NODE_VALID | NODE_ENDED;
mdoc->next = ROFF_NEXT_SIBLING;
}
static void
rew_pending(struct roff_man *mdoc, const struct roff_node *n)
{
for (;;) {
rew_last(mdoc, n);
if (mdoc->last == n) {
switch (n->type) {
case ROFFT_HEAD:
roff_body_alloc(mdoc, n->line, n->pos,
n->tok);
if (n->tok == MDOC_Ss)
mdoc->flags &= ~ROFF_NONOFILL;
break;
case ROFFT_BLOCK:
break;
default:
return;
}
if ( ! (n->flags & NODE_BROKEN))
return;
} else
n = mdoc->last;
for (;;) {
if ((n = n->parent) == NULL)
return;
if (n->type == ROFFT_BLOCK ||
n->type == ROFFT_HEAD) {
if (n->flags & NODE_ENDED)
break;
else
return;
}
}
}
}
static enum roff_tok
rew_alt(enum roff_tok tok)
{
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
rew_elem(struct roff_man *mdoc, enum roff_tok tok)
{
struct roff_node *n;
n = mdoc->last;
if (n->type != ROFFT_ELEM)
n = n->parent;
assert(n->type == ROFFT_ELEM);
assert(tok == n->tok);
rew_last(mdoc, n);
}
static void
break_intermediate(struct roff_node *n, struct roff_node *breaker)
{
if (n != breaker &&
n->type != ROFFT_BLOCK && n->type != ROFFT_HEAD &&
(n->type != ROFFT_BODY || n->end != ENDBODY_NOT))
n = n->parent;
while (n != breaker) {
if ( ! (n->flags & NODE_VALID))
n->flags |= NODE_BROKEN;
n = n->parent;
}
}
static int
find_pending(struct roff_man *mdoc, enum roff_tok tok, int line, int ppos,
struct roff_node *target)
{
struct roff_node *n;
int irc;
if (target->flags & NODE_VALID)
return 0;
irc = 0;
for (n = mdoc->last; n != NULL && n != target; n = n->parent) {
if (n->flags & NODE_ENDED)
continue;
if (n->type == ROFFT_BLOCK &&
mdoc_macro(n->tok)->flags & MDOC_EXPLICIT) {
irc = 1;
break_intermediate(mdoc->last, target);
if (target->type == ROFFT_HEAD)
target->flags |= NODE_ENDED;
else if ( ! (target->flags & NODE_ENDED)) {
mandoc_msg(MANDOCERR_BLK_NEST,
line, ppos, "%s breaks %s",
roff_name[tok], roff_name[n->tok]);
mdoc_endbody_alloc(mdoc, line, ppos,
tok, target);
}
}
}
return irc;
}
static void
dword(struct roff_man *mdoc, int line, int col, const char *p,
enum mdelim d, int may_append)
{
if (d == DELIM_MAX)
d = mdoc_isdelim(p);
if (may_append &&
! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) &&
d == DELIM_NONE && mdoc->last->type == ROFFT_TEXT &&
mdoc_isdelim(mdoc->last->string) == DELIM_NONE) {
roff_word_append(mdoc, p);
return;
}
roff_word_alloc(mdoc, line, col, p);
if (d == DELIM_OPEN)
mdoc->last->flags |= NODE_DELIMO;
else if (d == DELIM_CLOSE &&
! (mdoc->flags & MDOC_NODELIMC) &&
mdoc->last->parent->tok != MDOC_Fd)
mdoc->last->flags |= NODE_DELIMC;
mdoc->flags &= ~MDOC_NODELIMC;
}
static void
append_delims(struct roff_man *mdoc, int line, int *pos, char *buf)
{
char *p;
int la;
enum margserr ac;
if (buf[*pos] == '\0')
return;
for (;;) {
la = *pos;
ac = mdoc_args(mdoc, line, pos, buf, TOKEN_NONE, &p);
if (ac == ARGS_EOLN)
break;
dword(mdoc, line, la, p, DELIM_MAX, 1);
if (mandoc_eos(p, strlen(p)))
mdoc->last->flags |= NODE_EOS;
if (ac == ARGS_ALLOC)
free(p);
}
}
static int
macro_or_word(MACRO_PROT_ARGS, char *p, int parsed)
{
int ntok;
ntok = buf[ppos] == '"' || parsed == 0 ||
mdoc->flags & MDOC_PHRASELIT ? TOKEN_NONE :
lookup(mdoc, tok, line, ppos, p);
if (ntok == TOKEN_NONE) {
dword(mdoc, line, ppos, p, DELIM_MAX, tok == TOKEN_NONE ||
mdoc_macro(tok)->flags & MDOC_JOIN);
return 0;
} else {
if (tok != TOKEN_NONE &&
mdoc_macro(tok)->fp == in_line_eoln)
rew_elem(mdoc, tok);
(*mdoc_macro(ntok)->fp)(mdoc, ntok, line, ppos, pos, buf);
if (tok == TOKEN_NONE)
append_delims(mdoc, line, pos, buf);
return 1;
}
}
static void
blk_exp_close(MACRO_PROT_ARGS)
{
struct roff_node *body;
struct roff_node *endbody;
struct roff_node *itblk;
struct roff_node *later;
struct roff_node *n;
struct roff_node *target;
int j, lastarg, maxargs, nl, pending;
enum margserr ac;
enum roff_tok atok, ntok;
char *p;
nl = MDOC_NEWLINE & mdoc->flags;
switch (tok) {
case MDOC_Ec:
maxargs = 1;
break;
case MDOC_Ek:
mdoc->flags &= ~MDOC_KEEP;
default:
maxargs = 0;
break;
}
atok = rew_alt(tok);
body = NULL;
for (n = mdoc->last; n; n = n->parent) {
if (n->flags & NODE_ENDED || n->tok != atok ||
n->type != ROFFT_BODY || n->end != ENDBODY_NOT)
continue;
body = n;
break;
}
endbody = itblk = later = NULL;
for (n = mdoc->last; n; n = n->parent) {
if (n->flags & NODE_ENDED)
continue;
if (body == NULL || n->type != ROFFT_BLOCK)
continue;
if (n->tok == MDOC_Nm) {
if (later != NULL)
n->flags |= NODE_BROKEN | NODE_ENDED;
continue;
}
if (n->tok == MDOC_It) {
itblk = n;
continue;
}
if (atok == n->tok) {
if (later == NULL ||
(tok == MDOC_El && itblk == NULL))
break;
mandoc_msg(MANDOCERR_BLK_NEST,
line, ppos, "%s breaks %s",
roff_name[atok], roff_name[later->tok]);
endbody = mdoc_endbody_alloc(mdoc, line, ppos,
atok, body);
if (tok == MDOC_El)
itblk->flags |= NODE_ENDED | NODE_BROKEN;
if (maxargs)
mdoc->next = ROFF_NEXT_CHILD;
break;
}
if (n->tok == MDOC_Nd) {
if (later != NULL)
n->flags |= NODE_BROKEN | NODE_ENDED;
else
rew_last(mdoc, n);
continue;
}
break_intermediate(mdoc->last, body);
n->flags |= NODE_BROKEN;
if (later == NULL)
later = n;
}
if (body == NULL) {
mandoc_msg(MANDOCERR_BLK_NOTOPEN, line, ppos,
"%s", roff_name[tok]);
if (maxargs && endbody == NULL) {
roff_elem_alloc(mdoc, line, ppos, ROFF_br);
rew_elem(mdoc, ROFF_br);
}
} else if (endbody == NULL) {
rew_last(mdoc, body);
if (maxargs)
mdoc_tail_alloc(mdoc, line, ppos, atok);
}
if ((mdoc_macro(tok)->flags & MDOC_PARSED) == 0) {
if (buf[*pos] != '\0')
mandoc_msg(MANDOCERR_ARG_SKIP, line, ppos,
"%s %s", roff_name[tok], buf + *pos);
if (endbody == NULL && n != NULL)
rew_pending(mdoc, n);
if (tok == MDOC_Ed && body != NULL) {
if (body->flags & NODE_NOFILL)
mdoc->flags |= ROFF_NOFILL;
else
mdoc->flags &= ~ROFF_NOFILL;
}
return;
}
if (endbody != NULL)
n = endbody;
ntok = TOKEN_NONE;
for (j = 0; ; j++) {
lastarg = *pos;
if (j == maxargs && n != NULL)
rew_last(mdoc, n);
ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
break;
ntok = lookup(mdoc, tok, line, lastarg, p);
if (ntok == TOKEN_NONE) {
dword(mdoc, line, lastarg, p, DELIM_MAX,
mdoc_macro(tok)->flags & MDOC_JOIN);
if (ac == ARGS_ALLOC)
free(p);
continue;
}
if (ac == ARGS_ALLOC)
free(p);
if (n != NULL)
rew_last(mdoc, n);
mdoc->flags &= ~MDOC_NEWLINE;
(*mdoc_macro(ntok)->fp)(mdoc, ntok, line, lastarg, pos, buf);
break;
}
if (n != NULL) {
pending = 0;
if (ntok != TOKEN_NONE && n->flags & NODE_BROKEN) {
target = n;
do
target = target->parent;
while ( ! (target->flags & NODE_ENDED));
pending = find_pending(mdoc, ntok, line, ppos, target);
}
if ( ! pending)
rew_pending(mdoc, n);
}
if (nl)
append_delims(mdoc, line, pos, buf);
}
static void
in_line(MACRO_PROT_ARGS)
{
int la, scope, cnt, firstarg, mayopen, nc, nl;
enum roff_tok ntok;
enum margserr ac;
enum mdelim d;
struct mdoc_arg *arg;
char *p;
nl = MDOC_NEWLINE & mdoc->flags;
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;
}
mdoc_argv(mdoc, line, tok, &arg, pos, buf);
d = DELIM_NONE;
firstarg = 1;
mayopen = 1;
for (cnt = scope = 0;; ) {
la = *pos;
ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
if (ac == ARGS_EOLN) {
if (d == DELIM_OPEN)
mdoc->last->flags &= ~NODE_DELIMO;
break;
}
if (ac == ARGS_PUNCT) {
if (cnt == 0 && (nc == 0 || tok == MDOC_An))
mdoc->flags |= MDOC_NODELIMC;
break;
}
ntok = (tok == MDOC_Fn && !cnt) ?
TOKEN_NONE : lookup(mdoc, tok, line, la, p);
if (ntok != TOKEN_NONE) {
if (scope)
rew_elem(mdoc, tok);
if (nc && ! cnt) {
mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
rew_last(mdoc, mdoc->last);
} else if ( ! nc && ! cnt) {
mdoc_argv_free(arg);
mandoc_msg(MANDOCERR_MACRO_EMPTY,
line, ppos, "%s", roff_name[tok]);
}
(*mdoc_macro(ntok)->fp)(mdoc, ntok,
line, la, pos, buf);
if (nl)
append_delims(mdoc, line, pos, buf);
if (ac == ARGS_ALLOC)
free(p);
return;
}
if ((d = mdoc_isdelim(p)) != DELIM_NONE) {
if ((d == DELIM_CLOSE ||
(d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
!cnt && !scope && nc && mayopen) {
mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
scope = 1;
cnt++;
if (tok == MDOC_Nm)
mayopen = 0;
}
if (scope && tok != MDOC_Lk) {
rew_elem(mdoc, tok);
scope = 0;
if (tok == MDOC_Fn)
mayopen = 0;
}
} else if (mayopen && !scope) {
mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
scope = 1;
cnt++;
}
dword(mdoc, line, la, p, d,
mdoc_macro(tok)->flags & MDOC_JOIN);
if (ac == ARGS_ALLOC)
free(p);
if (firstarg && d == DELIM_CLOSE && !nc)
mdoc->last->flags &= ~NODE_DELIMC;
firstarg = 0;
if (scope && tok == MDOC_Fl) {
rew_elem(mdoc, tok);
scope = 0;
}
}
if (scope && tok != MDOC_Lk) {
rew_elem(mdoc, tok);
scope = 0;
}
if ( ! cnt) {
if (nc) {
mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
rew_last(mdoc, mdoc->last);
} else {
mdoc_argv_free(arg);
mandoc_msg(MANDOCERR_MACRO_EMPTY,
line, ppos, "%s", roff_name[tok]);
}
}
if (nl)
append_delims(mdoc, line, pos, buf);
if (scope)
rew_elem(mdoc, tok);
}
static void
blk_full(MACRO_PROT_ARGS)
{
struct mdoc_arg *arg;
struct roff_node *blk;
struct roff_node *head;
struct roff_node *body;
struct roff_node *n;
char *p;
size_t iarg;
int done, la, nl, parsed;
enum margserr ac, lac;
nl = MDOC_NEWLINE & mdoc->flags;
if (buf[*pos] == '\0' && (tok == MDOC_Sh || tok == MDOC_Ss)) {
mandoc_msg(MANDOCERR_MACRO_EMPTY,
line, ppos, "%s", roff_name[tok]);
return;
}
if ((mdoc_macro(tok)->flags & MDOC_EXPLICIT) == 0) {
blk = NULL;
for (n = mdoc->last; n != NULL; n = n->parent) {
if (n->flags & NODE_ENDED) {
if ( ! (n->flags & NODE_VALID))
n->flags |= NODE_BROKEN;
continue;
}
if (n->type != ROFFT_BLOCK)
continue;
if (tok == MDOC_It && n->tok == MDOC_Bl) {
if (blk != NULL) {
mandoc_msg(MANDOCERR_BLK_BROKEN,
line, ppos, "It breaks %s",
roff_name[blk->tok]);
rew_pending(mdoc, blk);
}
break;
}
if (mdoc_macro(n->tok)->flags & MDOC_EXPLICIT) {
switch (tok) {
case MDOC_Sh:
case MDOC_Ss:
mandoc_msg(MANDOCERR_BLK_BROKEN,
line, ppos,
"%s breaks %s", roff_name[tok],
roff_name[n->tok]);
rew_pending(mdoc, n);
n = mdoc->last;
continue;
case MDOC_It:
blk = n;
continue;
default:
break;
}
break;
}
if (tok != MDOC_Sh && (n->tok == MDOC_Sh ||
(tok != MDOC_Ss && (n->tok == MDOC_Ss ||
(tok != MDOC_It && n->tok == MDOC_It)))))
break;
if (blk != NULL) {
mandoc_msg(MANDOCERR_BLK_BROKEN, line, ppos,
"It breaks %s", roff_name[blk->tok]);
rew_pending(mdoc, blk);
blk = NULL;
}
rew_pending(mdoc, n);
}
if (tok == MDOC_It && (n == NULL || n->tok != MDOC_Bl)) {
mandoc_msg(MANDOCERR_IT_STRAY,
line, ppos, "It %s", buf + *pos);
roff_elem_alloc(mdoc, line, ppos, ROFF_br);
rew_elem(mdoc, ROFF_br);
return;
}
}
switch (tok) {
case MDOC_Sh:
mdoc->flags &= ~ROFF_NOFILL;
break;
case MDOC_Ss:
mdoc->flags |= ROFF_NONOFILL;
break;
default:
break;
}
mdoc_argv(mdoc, line, tok, &arg, pos, buf);
blk = mdoc_block_alloc(mdoc, line, ppos, tok, arg);
head = body = NULL;
parsed = tok != MDOC_It ||
mdoc->last->parent->tok != MDOC_Bl ||
mdoc->last->parent->norm->Bl.type != LIST_diag;
if (tok == MDOC_Nd) {
head = roff_head_alloc(mdoc, line, ppos, tok);
rew_last(mdoc, head);
body = roff_body_alloc(mdoc, line, ppos, tok);
}
if (tok == MDOC_Bk)
mdoc->flags |= MDOC_KEEP;
ac = ARGS_EOLN;
for (;;) {
if (mdoc->flags & MDOC_PHRASEQN) {
mdoc->flags &= ~MDOC_PHRASEQN;
mdoc->flags |= MDOC_PHRASEQF;
}
la = *pos;
lac = ac;
ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
if (ac == ARGS_EOLN) {
if (lac != ARGS_PHRASE ||
! (mdoc->flags & MDOC_PHRASEQF))
break;
if (body != NULL)
rew_last(mdoc, body);
body = roff_body_alloc(mdoc, line, ppos, tok);
roff_word_alloc(mdoc, line, ppos, "\\&");
break;
}
if (tok == MDOC_Bd || tok == MDOC_Bk) {
mandoc_msg(MANDOCERR_ARG_EXCESS, line, la,
"%s ... %s", roff_name[tok], buf + la);
if (ac == ARGS_ALLOC)
free(p);
break;
}
if (tok == MDOC_Rs) {
mandoc_msg(MANDOCERR_ARG_SKIP,
line, la, "Rs %s", buf + la);
if (ac == ARGS_ALLOC)
free(p);
break;
}
if (ac == ARGS_PUNCT)
break;
if (head == NULL &&
ac != ARGS_PHRASE &&
mdoc_isdelim(p) == DELIM_OPEN) {
dword(mdoc, line, la, p, DELIM_OPEN, 0);
if (ac == ARGS_ALLOC)
free(p);
continue;
}
if (head == NULL)
head = roff_head_alloc(mdoc, line, ppos, tok);
if (ac == ARGS_PHRASE) {
rew_last(mdoc, body == NULL ? head : body);
body = roff_body_alloc(mdoc, line, ppos, tok);
mdoc->flags |= MDOC_PHRASE;
parse_rest(mdoc, TOKEN_NONE, line, &la, buf);
mdoc->flags &= ~MDOC_PHRASE;
while (body->next != NULL)
body = body->next;
continue;
}
done = macro_or_word(mdoc, tok, line, la, pos, buf, p, parsed);
if (ac == ARGS_ALLOC)
free(p);
if (done)
break;
}
if (blk->flags & NODE_VALID)
return;
if (head == NULL)
head = roff_head_alloc(mdoc, line, ppos, tok);
if (nl && tok != MDOC_Bd && tok != MDOC_Bl && tok != MDOC_Rs)
append_delims(mdoc, line, pos, buf);
if (body != NULL)
goto out;
if (find_pending(mdoc, tok, line, ppos, head))
return;
rew_last(mdoc, head);
body = roff_body_alloc(mdoc, line, ppos, tok);
if (tok == MDOC_Ss)
mdoc->flags &= ~ROFF_NONOFILL;
if (tok == MDOC_Bd && arg != NULL) {
for (iarg = 0; iarg < arg->argc; iarg++) {
switch (arg->argv[iarg].arg) {
case MDOC_Unfilled:
case MDOC_Literal:
mdoc->flags |= ROFF_NOFILL;
break;
case MDOC_Filled:
case MDOC_Ragged:
case MDOC_Centred:
mdoc->flags &= ~ROFF_NOFILL;
break;
default:
continue;
}
break;
}
}
out:
if (mdoc->flags & MDOC_FREECOL) {
rew_last(mdoc, body);
rew_last(mdoc, blk);
mdoc->flags &= ~MDOC_FREECOL;
}
}
static void
blk_part_imp(MACRO_PROT_ARGS)
{
int done, la, nl;
enum margserr ac;
char *p;
struct roff_node *blk;
struct roff_node *body;
struct roff_node *n;
nl = MDOC_NEWLINE & mdoc->flags;
blk = mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
for (body = NULL; ; ) {
la = *pos;
ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
if (ac == ARGS_EOLN || ac == ARGS_PUNCT)
break;
if (body == NULL && mdoc_isdelim(p) == DELIM_OPEN) {
dword(mdoc, line, la, p, DELIM_OPEN, 0);
if (ac == ARGS_ALLOC)
free(p);
continue;
}
if (body == NULL)
body = roff_body_alloc(mdoc, line, ppos, tok);
done = macro_or_word(mdoc, tok, line, la, pos, buf, p, 1);
if (ac == ARGS_ALLOC)
free(p);
if (done)
break;
}
if (body == NULL)
body = roff_body_alloc(mdoc, line, ppos, tok);
if (find_pending(mdoc, tok, line, ppos, body))
return;
rew_last(mdoc, body);
if (nl)
append_delims(mdoc, line, pos, buf);
rew_pending(mdoc, blk);
for (n = body->child; n && n->next; n = n->next)
;
if (n && n->tok == MDOC_Ns)
roff_node_relink(mdoc, n);
}
static void
blk_part_exp(MACRO_PROT_ARGS)
{
int done, la, nl;
enum margserr ac;
struct roff_node *head;
char *p;
nl = MDOC_NEWLINE & mdoc->flags;
roff_block_alloc(mdoc, line, ppos, tok);
head = NULL;
for (;;) {
la = *pos;
ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
break;
if (head == NULL && mdoc_isdelim(p) == DELIM_OPEN) {
dword(mdoc, line, la, p, DELIM_OPEN, 0);
if (ac == ARGS_ALLOC)
free(p);
continue;
}
if (head == NULL) {
head = roff_head_alloc(mdoc, line, ppos, tok);
if (tok == MDOC_Eo)
dword(mdoc, line, la, p, DELIM_MAX, 0);
rew_last(mdoc, head);
roff_body_alloc(mdoc, line, ppos, tok);
if (tok == MDOC_Eo) {
if (ac == ARGS_ALLOC)
free(p);
continue;
}
}
done = macro_or_word(mdoc, tok, line, la, pos, buf, p, 1);
if (ac == ARGS_ALLOC)
free(p);
if (done)
break;
}
if (head == NULL) {
rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
roff_body_alloc(mdoc, line, ppos, tok);
}
if (nl)
append_delims(mdoc, line, pos, buf);
}
static void
in_line_argn(MACRO_PROT_ARGS)
{
struct mdoc_arg *arg;
char *p;
enum margserr ac;
enum roff_tok ntok;
int state;
int la, maxargs, nl;
nl = mdoc->flags & MDOC_NEWLINE;
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;
}
mdoc_argv(mdoc, line, tok, &arg, pos, buf);
state = -1;
p = NULL;
for (;;) {
la = *pos;
ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
if ((ac == ARGS_WORD || ac == ARGS_ALLOC) && state == -1 &&
(mdoc_macro(tok)->flags & MDOC_IGNDELIM) == 0 &&
mdoc_isdelim(p) == DELIM_OPEN) {
dword(mdoc, line, la, p, DELIM_OPEN, 0);
if (ac == ARGS_ALLOC)
free(p);
continue;
}
if (state == -1 && tok != MDOC_In &&
tok != MDOC_St && tok != MDOC_Xr) {
mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
state = 0;
}
if (ac == ARGS_PUNCT || ac == ARGS_EOLN) {
if (abs(state) < 2 && tok == MDOC_Pf)
mandoc_msg(MANDOCERR_PF_SKIP,
line, ppos, "Pf %s",
p == NULL ? "at eol" : p);
break;
}
if (state == maxargs) {
rew_elem(mdoc, tok);
state = -2;
}
ntok = (tok == MDOC_Pf && state == 0) ?
TOKEN_NONE : lookup(mdoc, tok, line, la, p);
if (ntok != TOKEN_NONE) {
if (state >= 0) {
rew_elem(mdoc, tok);
state = -2;
}
(*mdoc_macro(ntok)->fp)(mdoc, ntok,
line, la, pos, buf);
if (ac == ARGS_ALLOC)
free(p);
break;
}
if (mdoc_macro(tok)->flags & MDOC_IGNDELIM ||
mdoc_isdelim(p) == DELIM_NONE) {
if (state == -1) {
mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
state = 1;
} else if (state >= 0)
state++;
} else if (state >= 0) {
rew_elem(mdoc, tok);
state = -2;
}
dword(mdoc, line, la, p, DELIM_MAX,
mdoc_macro(tok)->flags & MDOC_JOIN);
if (ac == ARGS_ALLOC)
free(p);
p = mdoc->last->string;
}
if (state == -1) {
mandoc_msg(MANDOCERR_MACRO_EMPTY,
line, ppos, "%s", roff_name[tok]);
return;
}
if (state == 0 && tok == MDOC_Pf)
append_delims(mdoc, line, pos, buf);
if (state >= 0)
rew_elem(mdoc, tok);
if (nl)
append_delims(mdoc, line, pos, buf);
}
static void
in_line_eoln(MACRO_PROT_ARGS)
{
struct roff_node *n;
struct mdoc_arg *arg;
if ((tok == MDOC_Pp || tok == MDOC_Lp) &&
! (mdoc->flags & MDOC_SYNOPSIS)) {
n = mdoc->last;
if (mdoc->next == ROFF_NEXT_SIBLING)
n = n->parent;
if (n->tok == MDOC_Nm)
rew_last(mdoc, n->parent);
}
if (buf[*pos] == '\0' &&
(tok == MDOC_Fd || *roff_name[tok] == '%')) {
mandoc_msg(MANDOCERR_MACRO_EMPTY,
line, ppos, "%s", roff_name[tok]);
return;
}
mdoc_argv(mdoc, line, tok, &arg, pos, buf);
mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
if (parse_rest(mdoc, tok, line, pos, buf))
return;
rew_elem(mdoc, tok);
}
static int
parse_rest(struct roff_man *mdoc, enum roff_tok tok,
int line, int *pos, char *buf)
{
char *p;
int done, la;
enum margserr ac;
for (;;) {
la = *pos;
ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
if (ac == ARGS_EOLN)
return 0;
done = macro_or_word(mdoc, tok, line, la, pos, buf, p, 1);
if (ac == ARGS_ALLOC)
free(p);
if (done)
return 1;
}
}
static void
ctx_synopsis(MACRO_PROT_ARGS)
{
if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE))
in_line(mdoc, tok, line, ppos, pos, buf);
else if (tok == MDOC_Nm)
blk_full(mdoc, tok, line, ppos, pos, buf);
else {
assert(tok == MDOC_Vt);
blk_part_imp(mdoc, tok, line, ppos, pos, buf);
}
}
static void
phrase_ta(MACRO_PROT_ARGS)
{
struct roff_node *body, *n;
body = NULL;
for (n = mdoc->last; n != NULL; n = n->parent) {
if (n->flags & NODE_ENDED)
continue;
if (n->tok == MDOC_It && n->type == ROFFT_BODY)
body = n;
if (n->tok == MDOC_Bl && n->end == ENDBODY_NOT)
break;
}
if (n == NULL || n->norm->Bl.type != LIST_column) {
mandoc_msg(MANDOCERR_TA_STRAY, line, ppos, "Ta");
return;
}
rew_last(mdoc, body);
roff_body_alloc(mdoc, line, ppos, MDOC_It);
parse_rest(mdoc, TOKEN_NONE, line, pos, buf);
}