version 1.173, 2012/05/31 22:41:19 |
version 1.177, 2013/06/27 09:49:47 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2010, 2011, 2012, 2013 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 |
|
|
ROFF_am, |
ROFF_am, |
ROFF_ami, |
ROFF_ami, |
ROFF_am1, |
ROFF_am1, |
|
ROFF_cc, |
ROFF_de, |
ROFF_de, |
ROFF_dei, |
ROFF_dei, |
ROFF_de1, |
ROFF_de1, |
|
|
ROFF_so, |
ROFF_so, |
ROFF_ta, |
ROFF_ta, |
ROFF_tr, |
ROFF_tr, |
|
ROFF_Dd, |
|
ROFF_TH, |
ROFF_TS, |
ROFF_TS, |
ROFF_TE, |
ROFF_TE, |
ROFF_T_, |
ROFF_T_, |
|
|
}; |
}; |
|
|
struct roff { |
struct roff { |
|
enum mparset parsetype; /* requested parse type */ |
struct mparse *parse; /* parse point */ |
struct mparse *parse; /* parse point */ |
struct roffnode *last; /* leaf of stack */ |
struct roffnode *last; /* leaf of stack */ |
enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */ |
enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */ |
|
char control; /* control character */ |
int rstackpos; /* position in rstack */ |
int rstackpos; /* position in rstack */ |
struct reg regs[REG__MAX]; |
struct reg regs[REG__MAX]; |
struct roffkv *strtab; /* user-defined strings & macros */ |
struct roffkv *strtab; /* user-defined strings & macros */ |
Line 169 static enum rofferr roff_block(ROFF_ARGS); |
|
Line 174 static enum rofferr roff_block(ROFF_ARGS); |
|
static enum rofferr roff_block_text(ROFF_ARGS); |
static enum rofferr roff_block_text(ROFF_ARGS); |
static enum rofferr roff_block_sub(ROFF_ARGS); |
static enum rofferr roff_block_sub(ROFF_ARGS); |
static enum rofferr roff_cblock(ROFF_ARGS); |
static enum rofferr roff_cblock(ROFF_ARGS); |
|
static enum rofferr roff_cc(ROFF_ARGS); |
static enum rofferr roff_ccond(ROFF_ARGS); |
static enum rofferr roff_ccond(ROFF_ARGS); |
static enum rofferr roff_cond(ROFF_ARGS); |
static enum rofferr roff_cond(ROFF_ARGS); |
static enum rofferr roff_cond_text(ROFF_ARGS); |
static enum rofferr roff_cond_text(ROFF_ARGS); |
Line 195 static void roff_setstrn(struct roffkv **, const cha |
|
Line 201 static void roff_setstrn(struct roffkv **, const cha |
|
size_t, const char *, size_t, int); |
size_t, const char *, size_t, int); |
static enum rofferr roff_so(ROFF_ARGS); |
static enum rofferr roff_so(ROFF_ARGS); |
static enum rofferr roff_tr(ROFF_ARGS); |
static enum rofferr roff_tr(ROFF_ARGS); |
|
static enum rofferr roff_Dd(ROFF_ARGS); |
|
static enum rofferr roff_TH(ROFF_ARGS); |
static enum rofferr roff_TE(ROFF_ARGS); |
static enum rofferr roff_TE(ROFF_ARGS); |
static enum rofferr roff_TS(ROFF_ARGS); |
static enum rofferr roff_TS(ROFF_ARGS); |
static enum rofferr roff_EQ(ROFF_ARGS); |
static enum rofferr roff_EQ(ROFF_ARGS); |
Line 215 static struct roffmac roffs[ROFF_MAX] = { |
|
Line 223 static struct roffmac roffs[ROFF_MAX] = { |
|
{ "am", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "am", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
|
{ "cc", roff_cc, NULL, NULL, 0, NULL }, |
{ "de", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "de", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
{ "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL }, |
Line 234 static struct roffmac roffs[ROFF_MAX] = { |
|
Line 243 static struct roffmac roffs[ROFF_MAX] = { |
|
{ "so", roff_so, NULL, NULL, 0, NULL }, |
{ "so", roff_so, NULL, NULL, 0, NULL }, |
{ "ta", roff_line_ignore, NULL, NULL, 0, NULL }, |
{ "ta", roff_line_ignore, NULL, NULL, 0, NULL }, |
{ "tr", roff_tr, NULL, NULL, 0, NULL }, |
{ "tr", roff_tr, NULL, NULL, 0, NULL }, |
|
{ "Dd", roff_Dd, NULL, NULL, 0, NULL }, |
|
{ "TH", roff_TH, NULL, NULL, 0, NULL }, |
{ "TS", roff_TS, NULL, NULL, 0, NULL }, |
{ "TS", roff_TS, NULL, NULL, 0, NULL }, |
{ "TE", roff_TE, NULL, NULL, 0, NULL }, |
{ "TE", roff_TE, NULL, NULL, 0, NULL }, |
{ "T&", roff_T_, NULL, NULL, 0, NULL }, |
{ "T&", roff_T_, NULL, NULL, 0, NULL }, |
Line 244 static struct roffmac roffs[ROFF_MAX] = { |
|
Line 255 static struct roffmac roffs[ROFF_MAX] = { |
|
{ NULL, roff_userdef, NULL, NULL, 0, NULL }, |
{ NULL, roff_userdef, NULL, NULL, 0, NULL }, |
}; |
}; |
|
|
|
const char *const __mdoc_reserved[] = { |
|
"Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At", |
|
"Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq", |
|
"Brc", "Bro", "Brq", "Bsx", "Bt", "Bx", |
|
"Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq", |
|
"Ds", "Dt", "Dv", "Dx", "D1", |
|
"Ec", "Ed", "Ef", "Ek", "El", "Em", "em", |
|
"En", "Eo", "Eq", "Er", "Es", "Ev", "Ex", |
|
"Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx", |
|
"Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp", "LP", |
|
"Me", "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx", |
|
"Oc", "Oo", "Op", "Os", "Ot", "Ox", |
|
"Pa", "Pc", "Pf", "Po", "Pp", "PP", "pp", "Pq", |
|
"Qc", "Ql", "Qo", "Qq", "Or", "Rd", "Re", "Rs", "Rv", |
|
"Sc", "Sf", "Sh", "SH", "Sm", "So", "Sq", |
|
"Ss", "St", "Sx", "Sy", |
|
"Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr", |
|
"%A", "%B", "%D", "%I", "%J", "%N", "%O", |
|
"%P", "%Q", "%R", "%T", "%U", "%V", |
|
NULL |
|
}; |
|
|
|
const char *const __man_reserved[] = { |
|
"AT", "B", "BI", "BR", "BT", "DE", "DS", "DT", |
|
"EE", "EN", "EQ", "EX", "HF", "HP", "I", "IB", "IP", "IR", |
|
"LP", "ME", "MT", "OP", "P", "PD", "PP", "PT", |
|
"R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS", "SY", |
|
"TE", "TH", "TP", "TQ", "TS", "T&", "UC", "UE", "UR", "YS", |
|
NULL |
|
}; |
|
|
/* Array of injected predefined strings. */ |
/* Array of injected predefined strings. */ |
#define PREDEFS_MAX 38 |
#define PREDEFS_MAX 38 |
static const struct predef predefs[PREDEFS_MAX] = { |
static const struct predef predefs[PREDEFS_MAX] = { |
Line 351 roffnode_push(struct roff *r, enum rofft tok, const ch |
|
Line 393 roffnode_push(struct roff *r, enum rofft tok, const ch |
|
static void |
static void |
roff_free1(struct roff *r) |
roff_free1(struct roff *r) |
{ |
{ |
struct tbl_node *t; |
struct tbl_node *tbl; |
struct eqn_node *e; |
struct eqn_node *e; |
int i; |
int i; |
|
|
while (NULL != (t = r->first_tbl)) { |
while (NULL != (tbl = r->first_tbl)) { |
r->first_tbl = t->next; |
r->first_tbl = tbl->next; |
tbl_free(t); |
tbl_free(tbl); |
} |
} |
|
|
r->first_tbl = r->last_tbl = r->tbl = NULL; |
r->first_tbl = r->last_tbl = r->tbl = NULL; |
Line 392 roff_reset(struct roff *r) |
|
Line 434 roff_reset(struct roff *r) |
|
|
|
roff_free1(r); |
roff_free1(r); |
|
|
|
r->control = 0; |
memset(&r->regs, 0, sizeof(struct reg) * REG__MAX); |
memset(&r->regs, 0, sizeof(struct reg) * REG__MAX); |
|
|
for (i = 0; i < PREDEFS_MAX; i++) |
for (i = 0; i < PREDEFS_MAX; i++) |
Line 409 roff_free(struct roff *r) |
|
Line 452 roff_free(struct roff *r) |
|
|
|
|
|
struct roff * |
struct roff * |
roff_alloc(struct mparse *parse) |
roff_alloc(enum mparset type, struct mparse *parse) |
{ |
{ |
struct roff *r; |
struct roff *r; |
int i; |
int i; |
|
|
r = mandoc_calloc(1, sizeof(struct roff)); |
r = mandoc_calloc(1, sizeof(struct roff)); |
|
r->parsetype = type; |
r->parse = parse; |
r->parse = parse; |
r->rstackpos = -1; |
r->rstackpos = -1; |
|
|
Line 611 roff_parseln(struct roff *r, int ln, char **bufp, |
|
Line 655 roff_parseln(struct roff *r, int ln, char **bufp, |
|
assert(ROFF_CONT == e); |
assert(ROFF_CONT == e); |
|
|
ppos = pos; |
ppos = pos; |
ctl = mandoc_getcontrol(*bufp, &pos); |
ctl = roff_getcontrol(r, *bufp, &pos); |
|
|
/* |
/* |
* First, if a scope is open and we're not a macro, pass the |
* First, if a scope is open and we're not a macro, pass the |
Line 984 roff_cond_sub(ROFF_ARGS) |
|
Line 1028 roff_cond_sub(ROFF_ARGS) |
|
|
|
rr = r->last->rule; |
rr = r->last->rule; |
roffnode_cleanscope(r); |
roffnode_cleanscope(r); |
|
t = roff_parse(r, *bufp, &pos); |
|
|
/* |
/* |
* If the macro is unknown, first check if it contains a closing |
* Fully handle known macros when they are structurally |
* delimiter `\}'. If it does, close out our scope and return |
* required or when the conditional evaluated to true. |
* the currently-scoped rule (ignore or continue). Else, drop |
|
* into the currently-scoped rule. |
|
*/ |
*/ |
|
|
if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) { |
if ((ROFF_MAX != t) && |
ep = &(*bufp)[pos]; |
(ROFF_ccond == t || ROFFRULE_ALLOW == rr || |
for ( ; NULL != (ep = strchr(ep, '\\')); ep++) { |
ROFFMAC_STRUCT & roffs[t].flags)) { |
ep++; |
assert(roffs[t].proc); |
if ('}' != *ep) |
return((*roffs[t].proc)(r, t, bufp, szp, |
continue; |
ln, ppos, pos, offs)); |
|
} |
|
|
/* |
/* Always check for the closing delimiter `\}'. */ |
* Make the \} go away. |
|
* This is a little haphazard, as it's not quite |
|
* clear how nroff does this. |
|
* If we're at the end of line, then just chop |
|
* off the \} and resize the buffer. |
|
* If we aren't, then conver it to spaces. |
|
*/ |
|
|
|
if ('\0' == *(ep + 1)) { |
ep = &(*bufp)[pos]; |
*--ep = '\0'; |
while (NULL != (ep = strchr(ep, '\\'))) { |
*szp -= 2; |
if ('}' != *(++ep)) |
} else |
continue; |
*(ep - 1) = *ep = ' '; |
|
|
|
roff_ccond(r, ROFF_ccond, bufp, szp, |
/* |
ln, pos, pos + 2, offs); |
* If we're at the end of line, then just chop |
break; |
* off the \} and resize the buffer. |
} |
* If we aren't, then convert it to spaces. |
return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT); |
*/ |
} |
|
|
|
/* |
if ('\0' == *(ep + 1)) { |
* A denied conditional must evaluate its children if and only |
*--ep = '\0'; |
* if they're either structurally required (such as loops and |
*szp -= 2; |
* conditionals) or a closing macro. |
} else |
*/ |
*(ep - 1) = *ep = ' '; |
|
|
if (ROFFRULE_DENY == rr) |
roff_ccond(r, ROFF_ccond, bufp, szp, |
if ( ! (ROFFMAC_STRUCT & roffs[t].flags)) |
ln, pos, pos + 2, offs); |
if (ROFF_ccond != t) |
break; |
return(ROFF_IGN); |
} |
|
return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT); |
assert(roffs[t].proc); |
|
return((*roffs[t].proc)(r, t, bufp, szp, |
|
ln, ppos, pos, offs)); |
|
} |
} |
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
Line 1265 roff_rm(ROFF_ARGS) |
|
Line 1297 roff_rm(ROFF_ARGS) |
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static enum rofferr |
static enum rofferr |
|
roff_Dd(ROFF_ARGS) |
|
{ |
|
const char *const *cp; |
|
|
|
if (MPARSE_MDOC != r->parsetype) |
|
for (cp = __mdoc_reserved; *cp; cp++) |
|
roff_setstr(r, *cp, NULL, 0); |
|
|
|
return(ROFF_CONT); |
|
} |
|
|
|
/* ARGSUSED */ |
|
static enum rofferr |
|
roff_TH(ROFF_ARGS) |
|
{ |
|
const char *const *cp; |
|
|
|
if (MPARSE_MDOC != r->parsetype) |
|
for (cp = __man_reserved; *cp; cp++) |
|
roff_setstr(r, *cp, NULL, 0); |
|
|
|
return(ROFF_CONT); |
|
} |
|
|
|
/* ARGSUSED */ |
|
static enum rofferr |
roff_TE(ROFF_ARGS) |
roff_TE(ROFF_ARGS) |
{ |
{ |
|
|
Line 1343 roff_EN(ROFF_ARGS) |
|
Line 1401 roff_EN(ROFF_ARGS) |
|
static enum rofferr |
static enum rofferr |
roff_TS(ROFF_ARGS) |
roff_TS(ROFF_ARGS) |
{ |
{ |
struct tbl_node *t; |
struct tbl_node *tbl; |
|
|
if (r->tbl) { |
if (r->tbl) { |
mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL); |
mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL); |
tbl_end(&r->tbl); |
tbl_end(&r->tbl); |
} |
} |
|
|
t = tbl_alloc(ppos, ln, r->parse); |
tbl = tbl_alloc(ppos, ln, r->parse); |
|
|
if (r->last_tbl) |
if (r->last_tbl) |
r->last_tbl->next = t; |
r->last_tbl->next = tbl; |
else |
else |
r->first_tbl = r->last_tbl = t; |
r->first_tbl = r->last_tbl = tbl; |
|
|
r->tbl = r->last_tbl = t; |
r->tbl = r->last_tbl = tbl; |
return(ROFF_IGN); |
return(ROFF_IGN); |
} |
} |
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static enum rofferr |
static enum rofferr |
|
roff_cc(ROFF_ARGS) |
|
{ |
|
const char *p; |
|
|
|
p = *bufp + pos; |
|
|
|
if ('\0' == *p || '.' == (r->control = *p++)) |
|
r->control = 0; |
|
|
|
if ('\0' != *p) |
|
mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL); |
|
|
|
return(ROFF_IGN); |
|
} |
|
|
|
/* ARGSUSED */ |
|
static enum rofferr |
roff_tr(ROFF_ARGS) |
roff_tr(ROFF_ARGS) |
{ |
{ |
const char *p, *first, *second; |
const char *p, *first, *second; |
Line 1756 roff_strdup(const struct roff *r, const char *p) |
|
Line 1831 roff_strdup(const struct roff *r, const char *p) |
|
|
|
res[(int)ssz] = '\0'; |
res[(int)ssz] = '\0'; |
return(res); |
return(res); |
|
} |
|
|
|
/* |
|
* Find out whether a line is a macro line or not. |
|
* If it is, adjust the current position and return one; if it isn't, |
|
* return zero and don't change the current position. |
|
* If the control character has been set with `.cc', then let that grain |
|
* precedence. |
|
* This is slighly contrary to groff, where using the non-breaking |
|
* control character when `cc' has been invoked will cause the |
|
* non-breaking macro contents to be printed verbatim. |
|
*/ |
|
int |
|
roff_getcontrol(const struct roff *r, const char *cp, int *ppos) |
|
{ |
|
int pos; |
|
|
|
pos = *ppos; |
|
|
|
if (0 != r->control && cp[pos] == r->control) |
|
pos++; |
|
else if (0 != r->control) |
|
return(0); |
|
else if ('\\' == cp[pos] && '.' == cp[pos + 1]) |
|
pos += 2; |
|
else if ('.' == cp[pos] || '\'' == cp[pos]) |
|
pos++; |
|
else |
|
return(0); |
|
|
|
while (' ' == cp[pos] || '\t' == cp[pos]) |
|
pos++; |
|
|
|
*ppos = pos; |
|
return(1); |
} |
} |