version 1.53, 2009/03/08 11:41:22 |
version 1.270, 2018/12/04 02:53:51 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> |
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
|
* Copyright (c) 2010, 2012-2018 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 |
* purpose with or without fee is hereby granted, provided that the above |
* above copyright notice and this permission notice appear in all |
* copyright notice and this permission notice appear in all copies. |
* copies. |
|
* |
* |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* PERFORMANCE OF THIS SOFTWARE. |
|
*/ |
*/ |
|
#include "config.h" |
|
|
|
#include <sys/types.h> |
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <err.h> |
|
#include <stdarg.h> |
#include <stdarg.h> |
#include <stdlib.h> |
|
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
#include <time.h> |
|
|
#include "private.h" |
#include "mandoc_aux.h" |
|
#include "mandoc.h" |
|
#include "roff.h" |
|
#include "mdoc.h" |
|
#include "libmandoc.h" |
|
#include "roff_int.h" |
|
#include "libmdoc.h" |
|
|
/* |
const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
* Main caller in the libmdoc library. This begins the parsing routine, |
|
* handles allocation of data, and so forth. Most of the "work" is done |
|
* in macro.c and validate.c. |
|
*/ |
|
|
|
static struct mdoc_node *mdoc_node_alloc(const struct mdoc *); |
|
static int mdoc_node_append(struct mdoc *, |
|
struct mdoc_node *); |
|
|
|
static int parsetext(struct mdoc *, int, char *); |
|
static int parsemacro(struct mdoc *, int, char *); |
|
|
|
|
|
const char *const __mdoc_macronames[MDOC_MAX] = { |
|
"\\\"", "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", |
|
/* LINTED */ |
|
"Xr", "\%A", "\%B", "\%D", |
|
/* LINTED */ |
|
"\%I", "\%J", "\%N", "\%O", |
|
/* LINTED */ |
|
"\%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", |
|
}; |
|
|
|
const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
|
"split", "nosplit", "ragged", |
"split", "nosplit", "ragged", |
"unfilled", "literal", "file", |
"unfilled", "literal", "file", |
"offset", "bullet", "dash", |
"offset", "bullet", "dash", |
"hyphen", "item", "enum", |
"hyphen", "item", "enum", |
"tag", "diag", "hang", |
"tag", "diag", "hang", |
"ohang", "inset", "column", |
"ohang", "inset", "column", |
"width", "compact", "std", |
"width", "compact", "std", |
"filled", "words", "emphasis", |
"filled", "words", "emphasis", |
"symbolic" |
"symbolic", "nested", "centered" |
}; |
}; |
|
|
const char * const *mdoc_macronames = __mdoc_macronames; |
|
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_pmacro(struct roff_man *, int, char *, int); |
|
|
const struct mdoc_node * |
|
mdoc_node(const struct mdoc *mdoc) |
/* |
|
* Main parse routine. Parses a single line -- really just hands off to |
|
* the macro (mdoc_pmacro()) or text parser (mdoc_ptext()). |
|
*/ |
|
int |
|
mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs) |
{ |
{ |
|
|
return(mdoc->first); |
if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line) |
} |
mdoc->flags |= MDOC_NEWLINE; |
|
|
|
/* |
|
* Let the roff nS register switch SYNOPSIS mode early, |
|
* such that the parser knows at all times |
|
* whether this mode is on or off. |
|
* Note that this mode is also switched by the Sh macro. |
|
*/ |
|
if (roff_getreg(mdoc->roff, "nS")) |
|
mdoc->flags |= MDOC_SYNOPSIS; |
|
else |
|
mdoc->flags &= ~MDOC_SYNOPSIS; |
|
|
const struct mdoc_meta * |
return roff_getcontrol(mdoc->roff, buf, &offs) ? |
mdoc_meta(const struct mdoc *mdoc) |
mdoc_pmacro(mdoc, ln, buf, offs) : |
{ |
mdoc_ptext(mdoc, ln, buf, offs); |
|
|
return(&mdoc->meta); |
|
} |
} |
|
|
|
|
void |
void |
mdoc_free(struct mdoc *mdoc) |
mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, enum roff_tok tok) |
{ |
{ |
|
struct roff_node *p; |
|
|
if (mdoc->first) |
p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok); |
mdoc_node_freelist(mdoc->first); |
roff_node_append(mdoc, p); |
if (mdoc->htab) |
mdoc->next = ROFF_NEXT_CHILD; |
mdoc_tokhash_free(mdoc->htab); |
|
if (mdoc->meta.title) |
|
free(mdoc->meta.title); |
|
if (mdoc->meta.os) |
|
free(mdoc->meta.os); |
|
if (mdoc->meta.name) |
|
free(mdoc->meta.name); |
|
if (mdoc->meta.arch) |
|
free(mdoc->meta.arch); |
|
if (mdoc->meta.vol) |
|
free(mdoc->meta.vol); |
|
|
|
free(mdoc); |
|
} |
} |
|
|
|
struct roff_node * |
struct mdoc * |
mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, |
mdoc_alloc(void *data, const struct mdoc_cb *cb) |
enum roff_tok tok, struct roff_node *body) |
{ |
{ |
struct mdoc *p; |
struct roff_node *p; |
|
|
p = xcalloc(1, sizeof(struct mdoc)); |
body->flags |= NODE_ENDED; |
|
body->parent->flags |= NODE_ENDED; |
|
p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok); |
|
p->body = body; |
|
p->norm = body->norm; |
|
p->end = ENDBODY_SPACE; |
|
roff_node_append(mdoc, p); |
|
mdoc->next = ROFF_NEXT_SIBLING; |
|
return p; |
|
} |
|
|
p->data = data; |
struct roff_node * |
if (cb) |
mdoc_block_alloc(struct roff_man *mdoc, int line, int pos, |
(void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); |
enum roff_tok tok, struct mdoc_arg *args) |
|
{ |
|
struct roff_node *p; |
|
|
p->last = xcalloc(1, sizeof(struct mdoc_node)); |
p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok); |
p->last->type = MDOC_ROOT; |
p->args = args; |
p->first = p->last; |
if (p->args) |
|
(args->refcnt)++; |
|
|
p->next = MDOC_NEXT_CHILD; |
switch (tok) { |
p->htab = mdoc_tokhash_alloc(); |
case MDOC_Bd: |
|
case MDOC_Bf: |
return(p); |
case MDOC_Bl: |
|
case MDOC_En: |
|
case MDOC_Rs: |
|
p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); |
|
break; |
|
default: |
|
break; |
|
} |
|
roff_node_append(mdoc, p); |
|
mdoc->next = ROFF_NEXT_CHILD; |
|
return p; |
} |
} |
|
|
|
void |
int |
mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos, |
mdoc_endparse(struct mdoc *mdoc) |
enum roff_tok tok, struct mdoc_arg *args) |
{ |
{ |
|
struct roff_node *p; |
|
|
if (MDOC_HALT & mdoc->flags) |
p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok); |
return(0); |
p->args = args; |
if (NULL == mdoc->first) |
if (p->args) |
return(1); |
(args->refcnt)++; |
|
|
assert(mdoc->last); |
switch (tok) { |
if ( ! macro_end(mdoc)) { |
case MDOC_An: |
mdoc->flags |= MDOC_HALT; |
p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); |
return(0); |
break; |
|
default: |
|
break; |
} |
} |
return(1); |
roff_node_append(mdoc, p); |
|
mdoc->next = ROFF_NEXT_CHILD; |
} |
} |
|
|
|
|
/* |
/* |
* Main parse routine. Parses a single line -- really just hands off to |
* Parse free-form text, that is, a line that does not begin with the |
* the macro or text parser. |
* control character. |
*/ |
*/ |
int |
static int |
mdoc_parseln(struct mdoc *m, int ln, char *buf) |
mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs) |
{ |
{ |
|
struct roff_node *n; |
|
const char *cp, *sp; |
|
char *c, *ws, *end; |
|
|
/* If in error-mode, then we parse no more. */ |
n = mdoc->last; |
|
|
if (MDOC_HALT & m->flags) |
/* |
return(0); |
* If a column list contains plain text, assume an implicit item |
|
* macro. This can happen one or more times at the beginning |
|
* of such a list, intermixed with non-It mdoc macros and with |
|
* nodes generated on the roff level, for example by tbl. |
|
*/ |
|
|
return('.' == *buf ? parsemacro(m, ln, buf) : |
if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY && |
parsetext(m, ln, buf)); |
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_macro(MDOC_It)->fp)(mdoc, MDOC_It, |
|
line, offs, &offs, buf); |
|
return 1; |
|
} |
|
|
|
/* |
|
* Search for the beginning of unescaped trailing whitespace (ws) |
|
* and for the first character not to be output (end). |
|
*/ |
|
|
void |
/* FIXME: replace with strcspn(). */ |
mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...) |
ws = NULL; |
{ |
for (c = end = buf + offs; *c; c++) { |
char buf[256]; |
switch (*c) { |
va_list ap; |
case ' ': |
|
if (NULL == ws) |
|
ws = c; |
|
continue; |
|
case '\t': |
|
/* |
|
* Always warn about trailing tabs, |
|
* even outside literal context, |
|
* where they should be put on the next line. |
|
*/ |
|
if (NULL == ws) |
|
ws = c; |
|
/* |
|
* Strip trailing tabs in literal context only; |
|
* outside, they affect the next line. |
|
*/ |
|
if (MDOC_LITERAL & mdoc->flags) |
|
continue; |
|
break; |
|
case '\\': |
|
/* Skip the escaped character, too, if any. */ |
|
if (c[1]) |
|
c++; |
|
/* FALLTHROUGH */ |
|
default: |
|
ws = NULL; |
|
break; |
|
} |
|
end = c + 1; |
|
} |
|
*end = '\0'; |
|
|
if (NULL == mdoc->cb.mdoc_msg) |
if (ws) |
return; |
mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse, |
|
line, (int)(ws-buf), NULL); |
|
|
va_start(ap, fmt); |
/* |
(void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap); |
* Blank lines are allowed in no-fill mode |
va_end(ap); |
* and cancel preceding \c, |
(*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf); |
* but add a single vertical space elsewhere. |
} |
*/ |
|
|
|
if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) { |
|
switch (mdoc->last->type) { |
|
case ROFFT_TEXT: |
|
sp = mdoc->last->string; |
|
cp = end = strchr(sp, '\0') - 2; |
|
if (cp < sp || cp[0] != '\\' || cp[1] != 'c') |
|
break; |
|
while (cp > sp && cp[-1] == '\\') |
|
cp--; |
|
if ((end - cp) % 2) |
|
break; |
|
*end = '\0'; |
|
return 1; |
|
default: |
|
break; |
|
} |
|
mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse, |
|
line, (int)(c - buf), NULL); |
|
roff_elem_alloc(mdoc, line, offs, ROFF_sp); |
|
mdoc->last->flags |= NODE_VALID | NODE_ENDED; |
|
mdoc->next = ROFF_NEXT_SIBLING; |
|
return 1; |
|
} |
|
|
int |
roff_word_alloc(mdoc, line, offs, buf+offs); |
mdoc_verr(struct mdoc *mdoc, int ln, int pos, |
|
const char *fmt, ...) |
|
{ |
|
char buf[256]; |
|
va_list ap; |
|
|
|
if (NULL == mdoc->cb.mdoc_err) |
if (mdoc->flags & MDOC_LITERAL) |
return(0); |
return 1; |
|
|
va_start(ap, fmt); |
/* |
(void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap); |
* End-of-sentence check. If the last character is an unescaped |
va_end(ap); |
* EOS character, then flag the node as being the end of a |
return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf)); |
* sentence. The front-end will know how to interpret this. |
} |
*/ |
|
|
|
assert(buf < end); |
|
|
int |
if (mandoc_eos(buf+offs, (size_t)(end-buf-offs))) |
mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, |
mdoc->last->flags |= NODE_EOS; |
enum mdoc_warn type, const char *fmt, ...) |
|
{ |
|
char buf[256]; |
|
va_list ap; |
|
|
|
if (NULL == mdoc->cb.mdoc_warn) |
for (c = buf + offs; c != NULL; c = strchr(c + 1, '.')) { |
return(0); |
if (c - buf < offs + 2) |
|
continue; |
|
if (end - c < 3) |
|
break; |
|
if (c[1] != ' ' || |
|
isalnum((unsigned char)c[-2]) == 0 || |
|
isalnum((unsigned char)c[-1]) == 0 || |
|
(c[-2] == 'n' && c[-1] == 'c') || |
|
(c[-2] == 'v' && c[-1] == 's')) |
|
continue; |
|
c += 2; |
|
if (*c == ' ') |
|
c++; |
|
if (*c == ' ') |
|
c++; |
|
if (isupper((unsigned char)(*c))) |
|
mandoc_msg(MANDOCERR_EOS, mdoc->parse, |
|
line, (int)(c - buf), NULL); |
|
} |
|
|
va_start(ap, fmt); |
return 1; |
(void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap); |
|
va_end(ap); |
|
return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf)); |
|
} |
} |
|
|
|
/* |
int |
* Parse a macro line, that is, a line beginning with the control |
mdoc_macro(struct mdoc *m, int tok, |
* character. |
int ln, int pp, int *pos, char *buf) |
*/ |
{ |
|
|
|
/* FIXME - these should happen during validation. */ |
|
|
|
if (MDOC_PROLOGUE & mdoc_macros[tok].flags && |
|
SEC_PROLOGUE != m->lastnamed) |
|
return(mdoc_perr(m, ln, pp, |
|
"disallowed in document body")); |
|
|
|
if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) && |
|
SEC_PROLOGUE == m->lastnamed) |
|
return(mdoc_perr(m, ln, pp, |
|
"disallowed in prologue")); |
|
|
|
if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) |
|
return(mdoc_perr(m, ln, pp, "not callable")); |
|
|
|
return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf)); |
|
} |
|
|
|
|
|
static int |
static int |
mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p) |
mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs) |
{ |
{ |
|
struct roff_node *n; |
|
const char *cp; |
|
size_t sz; |
|
enum roff_tok tok; |
|
int sv; |
|
|
assert(mdoc->last); |
/* Determine the line macro. */ |
assert(mdoc->first); |
|
assert(MDOC_ROOT != p->type); |
|
|
|
switch (mdoc->next) { |
sv = offs; |
case (MDOC_NEXT_SIBLING): |
tok = TOKEN_NONE; |
mdoc->last->next = p; |
for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++) |
p->prev = mdoc->last; |
offs++; |
p->parent = mdoc->last->parent; |
if (sz == 2 || sz == 3) |
break; |
tok = roffhash_find(mdoc->mdocmac, buf + sv, sz); |
case (MDOC_NEXT_CHILD): |
if (tok == TOKEN_NONE) { |
mdoc->last->child = p; |
mandoc_msg(MANDOCERR_MACRO, mdoc->parse, |
p->parent = mdoc->last; |
ln, sv, buf + sv - 1); |
break; |
return 1; |
default: |
|
abort(); |
|
/* NOTREACHED */ |
|
} |
} |
|
|
if ( ! mdoc_valid_pre(mdoc, p)) |
/* Skip a leading escape sequence or tab. */ |
return(0); |
|
|
|
switch (p->type) { |
switch (buf[offs]) { |
case (MDOC_HEAD): |
case '\\': |
assert(MDOC_BLOCK == p->parent->type); |
cp = buf + offs + 1; |
p->parent->head = p; |
mandoc_escape(&cp, NULL, NULL); |
|
offs = cp - buf; |
break; |
break; |
case (MDOC_TAIL): |
case '\t': |
assert(MDOC_BLOCK == p->parent->type); |
offs++; |
p->parent->tail = p; |
|
break; |
break; |
case (MDOC_BODY): |
|
assert(MDOC_BLOCK == p->parent->type); |
|
p->parent->body = p; |
|
break; |
|
default: |
default: |
break; |
break; |
} |
} |
|
|
mdoc->last = p; |
/* Jump to the next non-whitespace word. */ |
return(1); |
|
} |
|
|
|
|
while (buf[offs] == ' ') |
|
offs++; |
|
|
static struct mdoc_node * |
/* |
mdoc_node_alloc(const struct mdoc *mdoc) |
* Trailing whitespace. Note that tabs are allowed to be passed |
{ |
* into the parser as "text", so we only warn about spaces here. |
struct mdoc_node *p; |
*/ |
|
|
p = xcalloc(1, sizeof(struct mdoc_node)); |
if ('\0' == buf[offs] && ' ' == buf[offs - 1]) |
p->sec = mdoc->lastsec; |
mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse, |
|
ln, offs - 1, NULL); |
|
|
return(p); |
/* |
} |
* If an initial macro or a list invocation, divert directly |
|
* into macro processing. |
|
*/ |
|
|
|
n = mdoc->last; |
|
if (n == NULL || tok == MDOC_It || tok == MDOC_El) { |
|
(*mdoc_macro(tok)->fp)(mdoc, tok, ln, sv, &offs, buf); |
|
return 1; |
|
} |
|
|
int |
/* |
mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok) |
* If a column list contains a non-It macro, assume an implicit |
{ |
* item macro. This can happen one or more times at the |
struct mdoc_node *p; |
* beginning of such a list, intermixed with text lines and |
|
* with nodes generated on the roff level, for example by tbl. |
|
*/ |
|
|
assert(mdoc->first); |
if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY && |
assert(mdoc->last); |
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_macro(MDOC_It)->fp)(mdoc, MDOC_It, ln, sv, &sv, buf); |
|
return 1; |
|
} |
|
|
p = mdoc_node_alloc(mdoc); |
/* Normal processing of a macro. */ |
|
|
p->line = line; |
(*mdoc_macro(tok)->fp)(mdoc, tok, ln, sv, &offs, buf); |
p->pos = pos; |
|
p->type = MDOC_TAIL; |
|
p->tok = tok; |
|
|
|
return(mdoc_node_append(mdoc, p)); |
/* In quick mode (for mandocdb), abort after the NAME section. */ |
} |
|
|
|
|
if (mdoc->quick && MDOC_Sh == tok && |
|
SEC_NAME != mdoc->last->sec) |
|
return 2; |
|
|
int |
return 1; |
mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok) |
|
{ |
|
struct mdoc_node *p; |
|
|
|
assert(mdoc->first); |
|
assert(mdoc->last); |
|
|
|
p = mdoc_node_alloc(mdoc); |
|
|
|
p->line = line; |
|
p->pos = pos; |
|
p->type = MDOC_HEAD; |
|
p->tok = tok; |
|
|
|
return(mdoc_node_append(mdoc, p)); |
|
} |
} |
|
|
|
enum mdelim |
int |
mdoc_isdelim(const char *p) |
mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok) |
|
{ |
{ |
struct mdoc_node *p; |
|
|
|
assert(mdoc->first); |
if ('\0' == p[0]) |
assert(mdoc->last); |
return DELIM_NONE; |
|
|
p = mdoc_node_alloc(mdoc); |
if ('\0' == p[1]) |
|
switch (p[0]) { |
|
case '(': |
|
case '[': |
|
return DELIM_OPEN; |
|
case '|': |
|
return DELIM_MIDDLE; |
|
case '.': |
|
case ',': |
|
case ';': |
|
case ':': |
|
case '?': |
|
case '!': |
|
case ')': |
|
case ']': |
|
return DELIM_CLOSE; |
|
default: |
|
return DELIM_NONE; |
|
} |
|
|
p->line = line; |
if ('\\' != p[0]) |
p->pos = pos; |
return DELIM_NONE; |
p->type = MDOC_BODY; |
|
p->tok = tok; |
|
|
|
return(mdoc_node_append(mdoc, p)); |
if (0 == strcmp(p + 1, ".")) |
} |
return DELIM_CLOSE; |
|
if (0 == strcmp(p + 1, "fR|\\fP")) |
|
return DELIM_MIDDLE; |
|
|
|
return DELIM_NONE; |
int |
|
mdoc_root_alloc(struct mdoc *mdoc) |
|
{ |
|
struct mdoc_node *p; |
|
|
|
p = mdoc_node_alloc(mdoc); |
|
|
|
p->type = MDOC_ROOT; |
|
|
|
return(mdoc_node_append(mdoc, p)); |
|
} |
} |
|
|
|
|
int |
|
mdoc_block_alloc(struct mdoc *mdoc, int line, int pos, |
|
int tok, struct mdoc_arg *args) |
|
{ |
|
struct mdoc_node *p; |
|
|
|
p = mdoc_node_alloc(mdoc); |
|
|
|
p->pos = pos; |
|
p->line = line; |
|
p->type = MDOC_BLOCK; |
|
p->tok = tok; |
|
p->args = args; |
|
|
|
if (args) |
|
(args->refcnt)++; |
|
|
|
return(mdoc_node_append(mdoc, p)); |
|
} |
|
|
|
|
|
int |
|
mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos, |
|
int tok, struct mdoc_arg *args) |
|
{ |
|
struct mdoc_node *p; |
|
|
|
p = mdoc_node_alloc(mdoc); |
|
|
|
p->line = line; |
|
p->pos = pos; |
|
p->type = MDOC_ELEM; |
|
p->tok = tok; |
|
p->args = args; |
|
|
|
if (args) |
|
(args->refcnt)++; |
|
|
|
return(mdoc_node_append(mdoc, p)); |
|
} |
|
|
|
|
|
int |
|
mdoc_word_alloc(struct mdoc *mdoc, |
|
int line, int pos, const char *word) |
|
{ |
|
struct mdoc_node *p; |
|
|
|
p = mdoc_node_alloc(mdoc); |
|
|
|
p->line = line; |
|
p->pos = pos; |
|
p->type = MDOC_TEXT; |
|
p->string = xstrdup(word); |
|
|
|
return(mdoc_node_append(mdoc, p)); |
|
} |
|
|
|
|
|
void |
void |
mdoc_node_free(struct mdoc_node *p) |
mdoc_validate(struct roff_man *mdoc) |
{ |
{ |
|
|
if (p->string) |
mdoc->last = mdoc->first; |
free(p->string); |
mdoc_node_validate(mdoc); |
if (p->args) |
mdoc_state_reset(mdoc); |
mdoc_argv_free(p->args); |
|
free(p); |
|
} |
|
|
|
|
|
void |
|
mdoc_node_freelist(struct mdoc_node *p) |
|
{ |
|
|
|
if (p->child) |
|
mdoc_node_freelist(p->child); |
|
if (p->next) |
|
mdoc_node_freelist(p->next); |
|
|
|
mdoc_node_free(p); |
|
} |
|
|
|
|
|
/* |
|
* Parse free-form text, that is, a line that does not begin with the |
|
* control character. |
|
*/ |
|
static int |
|
parsetext(struct mdoc *mdoc, int line, char *buf) |
|
{ |
|
|
|
if (SEC_PROLOGUE == mdoc->lastnamed) |
|
return(mdoc_perr(mdoc, line, 0, |
|
"text disallowed in prologue")); |
|
|
|
if ( ! mdoc_word_alloc(mdoc, line, 0, buf)) |
|
return(0); |
|
|
|
mdoc->next = MDOC_NEXT_SIBLING; |
|
return(1); |
|
} |
|
|
|
|
|
/* |
|
* Parse a macro line, that is, a line beginning with the control |
|
* character. |
|
*/ |
|
int |
|
parsemacro(struct mdoc *m, int ln, char *buf) |
|
{ |
|
int i, c; |
|
char mac[5]; |
|
|
|
/* Comments are quickly ignored. */ |
|
|
|
if (buf[1] && '\\' == buf[1]) |
|
if (buf[2] && '\"' == buf[2]) |
|
return(1); |
|
|
|
/* Copy the first word into a nil-terminated buffer. */ |
|
|
|
for (i = 1; i < 5; i++) { |
|
if (0 == (mac[i - 1] = buf[i])) |
|
break; |
|
else if (isspace((unsigned char)buf[i])) |
|
break; |
|
} |
|
|
|
mac[i - 1] = 0; |
|
|
|
if (i == 5 || i <= 2) { |
|
(void)mdoc_perr(m, ln, 1, "unknown macro: %s%s", |
|
mac, i == 5 ? "..." : ""); |
|
goto err; |
|
} |
|
|
|
if (MDOC_MAX == (c = mdoc_tokhash_find(m->htab, mac))) { |
|
(void)mdoc_perr(m, ln, 1, "unknown macro: %s", mac); |
|
goto err; |
|
} |
|
|
|
/* The macro is sane. Jump to the next word. */ |
|
|
|
while (buf[i] && isspace((unsigned char)buf[i])) |
|
i++; |
|
|
|
/* Begin recursive parse sequence. */ |
|
|
|
if ( ! mdoc_macro(m, c, ln, 1, &i, buf)) |
|
goto err; |
|
|
|
return(1); |
|
|
|
err: /* Error out. */ |
|
|
|
m->flags |= MDOC_HALT; |
|
return(0); |
|
} |
} |