-/* $Id: roff.c,v 1.167 2011/07/29 10:16:59 kristaps Exp $ */
+/* $Id: roff.c,v 1.175 2012/11/19 17:57:23 schwarze Exp $ */
/*
- * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
/* Maximum number of nested if-else conditionals. */
#define RSTACK_MAX 128
+/* Maximum number of string expansions per line, to break infinite loops. */
+#define EXPAND_LIMIT 1000
+
enum rofft {
ROFF_ad,
ROFF_am,
ROFF_ami,
ROFF_am1,
+ ROFF_cc,
ROFF_de,
ROFF_dei,
ROFF_de1,
ROFF_so,
ROFF_ta,
ROFF_tr,
+ ROFF_Dd,
+ ROFF_TH,
ROFF_TS,
ROFF_TE,
ROFF_T_,
};
struct roff {
+ enum mparset parsetype; /* requested parse type */
struct mparse *parse; /* parse point */
struct roffnode *last; /* leaf of stack */
enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */
+ char control; /* control character */
int rstackpos; /* position in rstack */
struct reg regs[REG__MAX];
struct roffkv *strtab; /* user-defined strings & macros */
static enum rofferr roff_block_text(ROFF_ARGS);
static enum rofferr roff_block_sub(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_cond(ROFF_ARGS);
static enum rofferr roff_cond_text(ROFF_ARGS);
const char *, size_t);
static enum rofferr roff_line_ignore(ROFF_ARGS);
static enum rofferr roff_nr(ROFF_ARGS);
-static void roff_openeqn(struct roff *, const char *,
+static void roff_openeqn(struct roff *, const char *,
int, int, const char *);
static enum rofft roff_parse(struct roff *, const char *, int *);
static enum rofferr roff_parsetext(char *);
-static void roff_res(struct roff *,
+static enum rofferr roff_res(struct roff *,
char **, size_t *, int, int);
static enum rofferr roff_rm(ROFF_ARGS);
static void roff_setstr(struct roff *,
size_t, const char *, size_t, int);
static enum rofferr roff_so(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_TS(ROFF_ARGS);
static enum rofferr roff_EQ(ROFF_ARGS);
{ "am", 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 },
+ { "cc", roff_cc, NULL, NULL, 0, NULL },
{ "de", 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 },
{ "so", roff_so, NULL, NULL, 0, NULL },
{ "ta", roff_line_ignore, 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 },
{ "TE", roff_TE, NULL, NULL, 0, NULL },
{ "T&", roff_T_, 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. */
#define PREDEFS_MAX 38
static const struct predef predefs[PREDEFS_MAX] = {
roff_free1(r);
+ r->control = 0;
memset(&r->regs, 0, sizeof(struct reg) * REG__MAX);
for (i = 0; i < PREDEFS_MAX; i++)
struct roff *
-roff_alloc(struct mparse *parse)
+roff_alloc(enum mparset type, struct mparse *parse)
{
struct roff *r;
int i;
r = mandoc_calloc(1, sizeof(struct roff));
+ r->parsetype = type;
r->parse = parse;
r->rstackpos = -1;
* is processed.
* This also checks the syntax of regular escapes.
*/
-static void
+static enum rofferr
roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
{
enum mandoc_esc esc;
const char *stnam; /* start of the name, after "[(*" */
const char *cp; /* end of the name, e.g. before ']' */
const char *res; /* the string to be substituted */
- int i, maxl;
+ int i, maxl, expand_count;
size_t nsz;
char *n;
+ expand_count = 0;
+
again:
cp = *bufp + pos;
while (NULL != (cp = strchr(cp, '\\'))) {
*/
if ('\0' == *cp)
- return;
+ return(ROFF_CONT);
if ('*' != *cp) {
res = cp;
mandoc_msg
(MANDOCERR_BADESCAPE, r->parse,
ln, (int)(stesc - *bufp), NULL);
- return;
+ return(ROFF_CONT);
}
cp++;
switch (*cp) {
case ('\0'):
- return;
+ return(ROFF_CONT);
case ('('):
cp++;
maxl = 2;
(MANDOCERR_BADESCAPE,
r->parse, ln,
(int)(stesc - *bufp), NULL);
- return;
+ return(ROFF_CONT);
}
if (0 == maxl && ']' == *cp)
break;
*bufp = n;
*szp = nsz;
- goto again;
+
+ if (EXPAND_LIMIT >= ++expand_count)
+ goto again;
+
+ /* Just leave the string unexpanded. */
+ mandoc_msg(MANDOCERR_ROFFLOOP, r->parse, ln, pos, NULL);
+ return(ROFF_IGN);
}
+ return(ROFF_CONT);
}
/*
static enum rofferr
roff_parsetext(char *p)
{
- char l, r;
size_t sz;
const char *start;
enum mandoc_esc esc;
continue;
}
- l = *(p - 1);
- r = *(p + 1);
- if ('\\' != l &&
- '\t' != r && '\t' != l &&
- ' ' != r && ' ' != l &&
- '-' != r && '-' != l &&
- ! isdigit((unsigned char)l) &&
- ! isdigit((unsigned char)r))
+ if (isalpha((unsigned char)p[-1]) &&
+ isalpha((unsigned char)p[1]))
*p = ASCII_HYPH;
p++;
}
* words to fill in.
*/
- roff_res(r, bufp, szp, ln, pos);
+ e = roff_res(r, bufp, szp, ln, pos);
+ if (ROFF_IGN == e)
+ return(e);
+ assert(ROFF_CONT == e);
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
{
while (r->last) {
- if (--r->last->endspan < 0)
+ if (--r->last->endspan != 0)
break;
roffnode_pop(r);
}
static enum rofferr
roff_cond(ROFF_ARGS)
{
- int sv;
- enum roffrule rule;
+
+ roffnode_push(r, tok, NULL, ln, ppos);
/*
* An `.el' has no conditional body: it will consume the value
* If we're not an `el', however, then evaluate the conditional.
*/
- rule = ROFF_el == tok ?
+ r->last->rule = ROFF_el == tok ?
(r->rstackpos < 0 ?
ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
roff_evalcond(*bufp, &pos);
- sv = pos;
- while (' ' == (*bufp)[pos])
- pos++;
-
- /*
- * Roff is weird. If we have just white-space after the
- * conditional, it's considered the BODY and we exit without
- * really doing anything. Warn about this. It's probably
- * wrong.
- */
-
- if ('\0' == (*bufp)[pos] && sv != pos) {
- mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
- return(ROFF_IGN);
- }
-
- roffnode_push(r, tok, NULL, ln, ppos);
-
- r->last->rule = rule;
-
/*
* An if-else will put the NEGATION of the current evaluated
* conditional into the stack of rules.
r->last->rule = ROFFRULE_DENY;
/*
- * Determine scope. If we're invoked with "\{" trailing the
- * conditional, then we're in a multiline scope. Else our scope
- * expires on the next line.
+ * Determine scope.
+ * If there is nothing on the line after the conditional,
+ * not even whitespace, use next-line scope.
*/
- r->last->endspan = 1;
+ if ('\0' == (*bufp)[pos]) {
+ r->last->endspan = 2;
+ goto out;
+ }
+
+ while (' ' == (*bufp)[pos])
+ pos++;
+
+ /* An opening brace requests multiline scope. */
if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
r->last->endspan = -1;
pos += 2;
+ goto out;
}
/*
- * If there are no arguments on the line, the next-line scope is
- * assumed.
+ * Anything else following the conditional causes
+ * single-line scope. Warn if the scope contains
+ * nothing but trailing whitespace.
*/
if ('\0' == (*bufp)[pos])
- return(ROFF_IGN);
+ mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
- /* Otherwise re-run the roff parser after recalculating. */
+ r->last->endspan = 1;
+out:
*offs = pos;
return(ROFF_RERUN);
}
return(ROFF_IGN);
}
+/* ARGSUSED */
+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)
return(ROFF_IGN);
}
+/* ARGSUSED */
+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)
return(r->last_eqn ? &r->last_eqn->eqn : NULL);
}
-char
-roff_eqndelim(const struct roff *r)
-{
-
- return('\0');
-}
-
/*
* Duplicate an input string, making the appropriate character
* conversations (as stipulated by `tr') along the way.
res[(int)ssz] = '\0';
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);
+}