version 1.251, 2015/04/23 15:35:59 |
version 1.258, 2017/01/10 13:47:00 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2010, 2012-2016 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
Line 111 mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, |
|
Line 111 mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, |
|
else |
else |
mdoc->flags &= ~MDOC_SYNOPSIS; |
mdoc->flags &= ~MDOC_SYNOPSIS; |
|
|
return(roff_getcontrol(mdoc->roff, buf, &offs) ? |
return roff_getcontrol(mdoc->roff, buf, &offs) ? |
mdoc_pmacro(mdoc, ln, buf, offs) : |
mdoc_pmacro(mdoc, ln, buf, offs) : |
mdoc_ptext(mdoc, ln, buf, offs)); |
mdoc_ptext(mdoc, ln, buf, offs); |
} |
} |
|
|
void |
void |
Line 121 mdoc_macro(MACRO_PROT_ARGS) |
|
Line 121 mdoc_macro(MACRO_PROT_ARGS) |
|
{ |
{ |
assert(tok > TOKEN_NONE && tok < MDOC_MAX); |
assert(tok > TOKEN_NONE && tok < MDOC_MAX); |
|
|
if (mdoc->flags & MDOC_PBODY) { |
|
if (tok == MDOC_Dt) { |
|
mandoc_vmsg(MANDOCERR_DT_LATE, |
|
mdoc->parse, line, ppos, |
|
"Dt %s", buf + *pos); |
|
return; |
|
} |
|
} else if ( ! (mdoc_macros[tok].flags & MDOC_PROLOGUE)) { |
|
if (mdoc->meta.title == NULL) { |
|
mandoc_vmsg(MANDOCERR_DT_NOTITLE, |
|
mdoc->parse, line, ppos, "%s %s", |
|
mdoc_macronames[tok], buf + *pos); |
|
mdoc->meta.title = mandoc_strdup("UNTITLED"); |
|
} |
|
if (NULL == mdoc->meta.vol) |
|
mdoc->meta.vol = mandoc_strdup("LOCAL"); |
|
mdoc->flags |= MDOC_PBODY; |
|
} |
|
(*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf); |
(*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf); |
} |
} |
|
|
Line 158 mdoc_endbody_alloc(struct roff_man *mdoc, int line, in |
|
Line 140 mdoc_endbody_alloc(struct roff_man *mdoc, int line, in |
|
{ |
{ |
struct roff_node *p; |
struct roff_node *p; |
|
|
body->flags |= MDOC_ENDED; |
body->flags |= NODE_ENDED; |
body->parent->flags |= MDOC_ENDED; |
body->parent->flags |= NODE_ENDED; |
p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok); |
p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok); |
p->body = body; |
p->body = body; |
p->norm = body->norm; |
p->norm = body->norm; |
p->end = end; |
p->end = end; |
roff_node_append(mdoc, p); |
roff_node_append(mdoc, p); |
mdoc->next = ROFF_NEXT_SIBLING; |
mdoc->next = ROFF_NEXT_SIBLING; |
return(p); |
return p; |
} |
} |
|
|
struct roff_node * |
struct roff_node * |
Line 182 mdoc_block_alloc(struct roff_man *mdoc, int line, int |
|
Line 164 mdoc_block_alloc(struct roff_man *mdoc, int line, int |
|
|
|
switch (tok) { |
switch (tok) { |
case MDOC_Bd: |
case MDOC_Bd: |
/* FALLTHROUGH */ |
|
case MDOC_Bf: |
case MDOC_Bf: |
/* FALLTHROUGH */ |
|
case MDOC_Bl: |
case MDOC_Bl: |
/* FALLTHROUGH */ |
|
case MDOC_En: |
case MDOC_En: |
/* FALLTHROUGH */ |
|
case MDOC_Rs: |
case MDOC_Rs: |
p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); |
p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); |
break; |
break; |
Line 197 mdoc_block_alloc(struct roff_man *mdoc, int line, int |
|
Line 175 mdoc_block_alloc(struct roff_man *mdoc, int line, int |
|
} |
} |
roff_node_append(mdoc, p); |
roff_node_append(mdoc, p); |
mdoc->next = ROFF_NEXT_CHILD; |
mdoc->next = ROFF_NEXT_CHILD; |
return(p); |
return p; |
} |
} |
|
|
void |
void |
Line 227 mdoc_node_relink(struct roff_man *mdoc, struct roff_no |
|
Line 205 mdoc_node_relink(struct roff_man *mdoc, struct roff_no |
|
{ |
{ |
|
|
roff_node_unlink(mdoc, p); |
roff_node_unlink(mdoc, p); |
|
p->prev = p->next = NULL; |
roff_node_append(mdoc, p); |
roff_node_append(mdoc, p); |
} |
} |
|
|
Line 240 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
Line 219 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
struct roff_node *n; |
struct roff_node *n; |
char *c, *ws, *end; |
char *c, *ws, *end; |
|
|
assert(mdoc->last); |
|
n = mdoc->last; |
n = mdoc->last; |
|
|
/* |
/* |
* Divert directly to list processing if we're encountering a |
* If a column list contains plain text, assume an implicit item |
* columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry |
* macro. This can happen one or more times at the beginning |
* (a ROFFT_BODY means it's already open, in which case we should |
* of such a list, intermixed with non-It mdoc macros and with |
* process within its context in the normal way). |
* nodes generated on the roff level, for example by tbl. |
*/ |
*/ |
|
|
if (n->tok == MDOC_Bl && n->type == ROFFT_BODY && |
if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY && |
n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) { |
n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) || |
/* `Bl' is open without any children. */ |
(n->parent != NULL && n->parent->tok == MDOC_Bl && |
|
n->parent->norm->Bl.type == LIST_column)) { |
mdoc->flags |= MDOC_FREECOL; |
mdoc->flags |= MDOC_FREECOL; |
mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf); |
mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf); |
return(1); |
return 1; |
} |
} |
|
|
if (n->tok == MDOC_It && n->type == ROFFT_BLOCK && |
|
NULL != n->parent && |
|
MDOC_Bl == n->parent->tok && |
|
LIST_column == n->parent->norm->Bl.type) { |
|
/* `Bl' has block-level `It' children. */ |
|
mdoc->flags |= MDOC_FREECOL; |
|
mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf); |
|
return(1); |
|
} |
|
|
|
/* |
/* |
* Search for the beginning of unescaped trailing whitespace (ws) |
* Search for the beginning of unescaped trailing whitespace (ws) |
* and for the first character not to be output (end). |
* and for the first character not to be output (end). |
Line 323 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
Line 292 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
* behaviour that we want to work around it. |
* behaviour that we want to work around it. |
*/ |
*/ |
roff_elem_alloc(mdoc, line, offs, MDOC_sp); |
roff_elem_alloc(mdoc, line, offs, MDOC_sp); |
|
mdoc->last->flags |= NODE_VALID | NODE_ENDED; |
mdoc->next = ROFF_NEXT_SIBLING; |
mdoc->next = ROFF_NEXT_SIBLING; |
mdoc_valid_post(mdoc); |
return 1; |
return(1); |
|
} |
} |
|
|
roff_word_alloc(mdoc, line, offs, buf+offs); |
roff_word_alloc(mdoc, line, offs, buf+offs); |
|
|
if (mdoc->flags & MDOC_LITERAL) |
if (mdoc->flags & MDOC_LITERAL) |
return(1); |
return 1; |
|
|
/* |
/* |
* End-of-sentence check. If the last character is an unescaped |
* End-of-sentence check. If the last character is an unescaped |
Line 342 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
Line 311 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
assert(buf < end); |
assert(buf < end); |
|
|
if (mandoc_eos(buf+offs, (size_t)(end-buf-offs))) |
if (mandoc_eos(buf+offs, (size_t)(end-buf-offs))) |
mdoc->last->flags |= MDOC_EOS; |
mdoc->last->flags |= NODE_EOS; |
return(1); |
return 1; |
} |
} |
|
|
/* |
/* |
Line 377 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
Line 346 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
if (tok == TOKEN_NONE) { |
if (tok == TOKEN_NONE) { |
mandoc_msg(MANDOCERR_MACRO, mdoc->parse, |
mandoc_msg(MANDOCERR_MACRO, mdoc->parse, |
ln, sv, buf + sv - 1); |
ln, sv, buf + sv - 1); |
return(1); |
return 1; |
} |
} |
|
|
/* Skip a leading escape sequence or tab. */ |
/* Skip a leading escape sequence or tab. */ |
Line 414 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
Line 383 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
* into macro processing. |
* into macro processing. |
*/ |
*/ |
|
|
if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) { |
n = mdoc->last; |
|
if (n == NULL || tok == MDOC_It || tok == MDOC_El) { |
mdoc_macro(mdoc, tok, ln, sv, &offs, buf); |
mdoc_macro(mdoc, tok, ln, sv, &offs, buf); |
return(1); |
return 1; |
} |
} |
|
|
n = mdoc->last; |
|
assert(mdoc->last); |
|
|
|
/* |
/* |
* If the first macro of a `Bl -column', open an `It' block |
* If a column list contains a non-It macro, assume an implicit |
* context around the parsed macro. |
* item macro. This can happen one or more times at the |
|
* beginning of such a list, intermixed with text lines and |
|
* with nodes generated on the roff level, for example by tbl. |
*/ |
*/ |
|
|
if (n->tok == MDOC_Bl && n->type == ROFFT_BODY && |
if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY && |
n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) { |
n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) || |
|
(n->parent != NULL && n->parent->tok == MDOC_Bl && |
|
n->parent->norm->Bl.type == LIST_column)) { |
mdoc->flags |= MDOC_FREECOL; |
mdoc->flags |= MDOC_FREECOL; |
mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf); |
mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf); |
return(1); |
return 1; |
} |
} |
|
|
/* |
|
* If we're following a block-level `It' within a `Bl -column' |
|
* context (perhaps opened in the above block or in ptext()), |
|
* then open an `It' block context around the parsed macro. |
|
*/ |
|
|
|
if (n->tok == MDOC_It && n->type == ROFFT_BLOCK && |
|
NULL != n->parent && |
|
MDOC_Bl == n->parent->tok && |
|
LIST_column == n->parent->norm->Bl.type) { |
|
mdoc->flags |= MDOC_FREECOL; |
|
mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf); |
|
return(1); |
|
} |
|
|
|
/* Normal processing of a macro. */ |
/* Normal processing of a macro. */ |
|
|
mdoc_macro(mdoc, tok, ln, sv, &offs, buf); |
mdoc_macro(mdoc, tok, ln, sv, &offs, buf); |
Line 457 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
Line 413 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
|
|
if (mdoc->quick && MDOC_Sh == tok && |
if (mdoc->quick && MDOC_Sh == tok && |
SEC_NAME != mdoc->last->sec) |
SEC_NAME != mdoc->last->sec) |
return(2); |
return 2; |
|
|
return(1); |
return 1; |
} |
} |
|
|
enum mdelim |
enum mdelim |
Line 467 mdoc_isdelim(const char *p) |
|
Line 423 mdoc_isdelim(const char *p) |
|
{ |
{ |
|
|
if ('\0' == p[0]) |
if ('\0' == p[0]) |
return(DELIM_NONE); |
return DELIM_NONE; |
|
|
if ('\0' == p[1]) |
if ('\0' == p[1]) |
switch (p[0]) { |
switch (p[0]) { |
case '(': |
case '(': |
/* FALLTHROUGH */ |
|
case '[': |
case '[': |
return(DELIM_OPEN); |
return DELIM_OPEN; |
case '|': |
case '|': |
return(DELIM_MIDDLE); |
return DELIM_MIDDLE; |
case '.': |
case '.': |
/* FALLTHROUGH */ |
|
case ',': |
case ',': |
/* FALLTHROUGH */ |
|
case ';': |
case ';': |
/* FALLTHROUGH */ |
|
case ':': |
case ':': |
/* FALLTHROUGH */ |
|
case '?': |
case '?': |
/* FALLTHROUGH */ |
|
case '!': |
case '!': |
/* FALLTHROUGH */ |
|
case ')': |
case ')': |
/* FALLTHROUGH */ |
|
case ']': |
case ']': |
return(DELIM_CLOSE); |
return DELIM_CLOSE; |
default: |
default: |
return(DELIM_NONE); |
return DELIM_NONE; |
} |
} |
|
|
if ('\\' != p[0]) |
if ('\\' != p[0]) |
return(DELIM_NONE); |
return DELIM_NONE; |
|
|
if (0 == strcmp(p + 1, ".")) |
if (0 == strcmp(p + 1, ".")) |
return(DELIM_CLOSE); |
return DELIM_CLOSE; |
if (0 == strcmp(p + 1, "fR|\\fP")) |
if (0 == strcmp(p + 1, "fR|\\fP")) |
return(DELIM_MIDDLE); |
return DELIM_MIDDLE; |
|
|
return(DELIM_NONE); |
return DELIM_NONE; |
} |
} |
|
|
void |
void |
mdoc_deroff(char **dest, const struct roff_node *n) |
mdoc_validate(struct roff_man *mdoc) |
{ |
{ |
char *cp; |
|
size_t sz; |
|
|
|
if (n->type != ROFFT_TEXT) { |
mdoc->last = mdoc->first; |
for (n = n->child; n; n = n->next) |
mdoc_node_validate(mdoc); |
mdoc_deroff(dest, n); |
mdoc_state_reset(mdoc); |
return; |
|
} |
|
|
|
/* Skip leading whitespace. */ |
|
|
|
for (cp = n->string; '\0' != *cp; cp++) |
|
if (0 == isspace((unsigned char)*cp)) |
|
break; |
|
|
|
/* Skip trailing whitespace. */ |
|
|
|
for (sz = strlen(cp); sz; sz--) |
|
if (0 == isspace((unsigned char)cp[sz-1])) |
|
break; |
|
|
|
/* Skip empty strings. */ |
|
|
|
if (0 == sz) |
|
return; |
|
|
|
if (NULL == *dest) { |
|
*dest = mandoc_strndup(cp, sz); |
|
return; |
|
} |
|
|
|
mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp); |
|
free(*dest); |
|
*dest = cp; |
|
} |
} |