version 1.253, 2015/10/06 18:32:19 |
version 1.265, 2017/06/07 20:30:40 |
|
|
/* $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-2017 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 |
|
|
#include "roff_int.h" |
#include "roff_int.h" |
#include "libmdoc.h" |
#include "libmdoc.h" |
|
|
const char *const __mdoc_macronames[MDOC_MAX + 1] = { |
|
"text", |
|
"Ap", "Dd", "Dt", "Os", |
|
"Sh", "Ss", "Pp", "D1", |
|
"Dl", "Bd", "Ed", "Bl", |
|
"El", "It", "Ad", "An", |
|
"Ar", "Cd", "Cm", "Dv", |
|
"Er", "Ev", "Ex", "Fa", |
|
"Fd", "Fl", "Fn", "Ft", |
|
"Ic", "In", "Li", "Nd", |
|
"Nm", "Op", "Ot", "Pa", |
|
"Rv", "St", "Va", "Vt", |
|
"Xr", "%A", "%B", "%D", |
|
"%I", "%J", "%N", "%O", |
|
"%P", "%R", "%T", "%V", |
|
"Ac", "Ao", "Aq", "At", |
|
"Bc", "Bf", "Bo", "Bq", |
|
"Bsx", "Bx", "Db", "Dc", |
|
"Do", "Dq", "Ec", "Ef", |
|
"Em", "Eo", "Fx", "Ms", |
|
"No", "Ns", "Nx", "Ox", |
|
"Pc", "Pf", "Po", "Pq", |
|
"Qc", "Ql", "Qo", "Qq", |
|
"Re", "Rs", "Sc", "So", |
|
"Sq", "Sm", "Sx", "Sy", |
|
"Tn", "Ux", "Xc", "Xo", |
|
"Fo", "Fc", "Oo", "Oc", |
|
"Bk", "Ek", "Bt", "Hf", |
|
"Fr", "Ud", "Lb", "Lp", |
|
"Lk", "Mt", "Brq", "Bro", |
|
"Brc", "%C", "Es", "En", |
|
"Dx", "%Q", "br", "sp", |
|
"%U", "Ta", "ll", |
|
}; |
|
|
|
const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
"split", "nosplit", "ragged", |
"split", "nosplit", "ragged", |
"unfilled", "literal", "file", |
"unfilled", "literal", "file", |
Line 80 const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
|
Line 45 const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
|
"width", "compact", "std", |
"width", "compact", "std", |
"filled", "words", "emphasis", |
"filled", "words", "emphasis", |
"symbolic", "nested", "centered" |
"symbolic", "nested", "centered" |
}; |
}; |
|
|
const char * const *mdoc_macronames = __mdoc_macronames + 1; |
|
const char * const *mdoc_argnames = __mdoc_argnames; |
const char * const *mdoc_argnames = __mdoc_argnames; |
|
|
static int mdoc_ptext(struct roff_man *, int, char *, int); |
static int mdoc_ptext(struct roff_man *, int, char *, int); |
Line 119 mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, |
|
Line 82 mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, |
|
void |
void |
mdoc_macro(MACRO_PROT_ARGS) |
mdoc_macro(MACRO_PROT_ARGS) |
{ |
{ |
assert(tok > TOKEN_NONE && tok < MDOC_MAX); |
assert(tok >= MDOC_Dd && 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); |
} |
} |
|
|
void |
void |
mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok) |
mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, enum roff_tok tok) |
{ |
{ |
struct roff_node *p; |
struct roff_node *p; |
|
|
Line 153 mdoc_tail_alloc(struct roff_man *mdoc, int line, int p |
|
Line 97 mdoc_tail_alloc(struct roff_man *mdoc, int line, int p |
|
} |
} |
|
|
struct roff_node * |
struct roff_node * |
mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok, |
mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, |
struct roff_node *body, enum mdoc_endbody end) |
enum roff_tok tok, struct roff_node *body) |
{ |
{ |
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 = ENDBODY_SPACE; |
roff_node_append(mdoc, p); |
roff_node_append(mdoc, p); |
mdoc->next = ROFF_NEXT_SIBLING; |
mdoc->next = ROFF_NEXT_SIBLING; |
return p; |
return p; |
Line 171 mdoc_endbody_alloc(struct roff_man *mdoc, int line, in |
|
Line 115 mdoc_endbody_alloc(struct roff_man *mdoc, int line, in |
|
|
|
struct roff_node * |
struct roff_node * |
mdoc_block_alloc(struct roff_man *mdoc, int line, int pos, |
mdoc_block_alloc(struct roff_man *mdoc, int line, int pos, |
int tok, struct mdoc_arg *args) |
enum roff_tok tok, struct mdoc_arg *args) |
{ |
{ |
struct roff_node *p; |
struct roff_node *p; |
|
|
Line 182 mdoc_block_alloc(struct roff_man *mdoc, int line, int |
|
Line 126 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 202 mdoc_block_alloc(struct roff_man *mdoc, int line, int |
|
Line 142 mdoc_block_alloc(struct roff_man *mdoc, int line, int |
|
|
|
void |
void |
mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos, |
mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos, |
int tok, struct mdoc_arg *args) |
enum roff_tok tok, struct mdoc_arg *args) |
{ |
{ |
struct roff_node *p; |
struct roff_node *p; |
|
|
Line 227 mdoc_node_relink(struct roff_man *mdoc, struct roff_no |
|
Line 167 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 181 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 322 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
Line 253 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
* blank lines aren't allowed, but enough manuals assume this |
* blank lines aren't allowed, but enough manuals assume this |
* 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, ROFF_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; |
} |
} |
|
|
Line 342 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, |
|
Line 273 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; |
|
|
|
for (c = buf + offs; c != NULL; c = strchr(c + 1, '.')) { |
|
if (c - buf < offs + 2) |
|
continue; |
|
if (end - c < 3) |
|
break; |
|
if (isalpha((unsigned char)c[-2]) && |
|
isalpha((unsigned char)c[-1]) && |
|
c[1] == ' ' && |
|
isupper((unsigned char)(c[2] == ' ' ? c[3] : c[2])) && |
|
(c[-2] != 'n' || c[-1] != 'c') && |
|
(c[-2] != 'v' || c[-1] != 's')) |
|
mandoc_msg(MANDOCERR_EOS, mdoc->parse, |
|
line, (int)(c - buf), NULL); |
|
} |
|
|
return 1; |
return 1; |
} |
} |
|
|
Line 355 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
Line 302 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
{ |
{ |
struct roff_node *n; |
struct roff_node *n; |
const char *cp; |
const char *cp; |
int tok; |
size_t sz; |
int i, sv; |
enum roff_tok tok; |
char mac[5]; |
int sv; |
|
|
sv = offs; |
/* Determine the line macro. */ |
|
|
/* |
sv = offs; |
* Copy the first word into a nil-terminated buffer. |
tok = TOKEN_NONE; |
* Stop when a space, tab, escape, or eoln is encountered. |
for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++) |
*/ |
offs++; |
|
if (sz == 2 || sz == 3) |
i = 0; |
tok = roffhash_find(mdoc->mdocmac, buf + sv, sz); |
while (i < 4 && strchr(" \t\\", buf[offs]) == NULL) |
|
mac[i++] = buf[offs++]; |
|
|
|
mac[i] = '\0'; |
|
|
|
tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE; |
|
|
|
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); |
Line 397 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
Line 337 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
|
|
/* Jump to the next non-whitespace word. */ |
/* Jump to the next non-whitespace word. */ |
|
|
while (buf[offs] && ' ' == buf[offs]) |
while (buf[offs] == ' ') |
offs++; |
offs++; |
|
|
/* |
/* |
Line 414 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, |
|
Line 354 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 472 mdoc_isdelim(const char *p) |
|
Line 399 mdoc_isdelim(const char *p) |
|
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: |
Line 506 mdoc_isdelim(const char *p) |
|
Line 425 mdoc_isdelim(const char *p) |
|
return DELIM_MIDDLE; |
return DELIM_MIDDLE; |
|
|
return DELIM_NONE; |
return DELIM_NONE; |
|
} |
|
|
|
void |
|
mdoc_validate(struct roff_man *mdoc) |
|
{ |
|
|
|
mdoc->last = mdoc->first; |
|
mdoc_node_validate(mdoc); |
|
mdoc_state_reset(mdoc); |
} |
} |