summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorschwarze <schwarze@openbsd.org>2014-04-07 15:05:12 +0000
committerschwarze <schwarze@openbsd.org>2014-04-07 15:05:12 +0000
commitb6a36a9bc60eb9c24cf8a3f5b23975ab8fb3f6ba (patch)
tree051e565762e337f01b0d62579003ce161d77047c
parentsave-buffer needs to use O_TRUNC. (diff)
downloadwireguard-openbsd-b6a36a9bc60eb9c24cf8a3f5b23975ab8fb3f6ba.tar.xz
wireguard-openbsd-b6a36a9bc60eb9c24cf8a3f5b23975ab8fb3f6ba.zip
Almost complete implementation of roff(7) numerical expressions.
Support all binary operators except ';' (scale conversion). Fully support chained operations and nested parentheses. Use this for the .nr, .if, and .ie requests. While here, fix parsing of integer numbers in roff_getnum().
-rw-r--r--regress/usr.bin/mandoc/roff/cond/numeric.in30
-rw-r--r--regress/usr.bin/mandoc/roff/cond/numeric.out_ascii6
-rw-r--r--regress/usr.bin/mandoc/roff/nr/Makefile4
-rw-r--r--regress/usr.bin/mandoc/roff/nr/eval.in67
-rw-r--r--regress/usr.bin/mandoc/roff/nr/eval.out_ascii33
-rw-r--r--share/man/man7/roff.799
-rw-r--r--usr.bin/mandoc/roff.c266
7 files changed, 425 insertions, 80 deletions
diff --git a/regress/usr.bin/mandoc/roff/cond/numeric.in b/regress/usr.bin/mandoc/roff/cond/numeric.in
index 88e048a271b..ab154d61575 100644
--- a/regress/usr.bin/mandoc/roff/cond/numeric.in
+++ b/regress/usr.bin/mandoc/roff/cond/numeric.in
@@ -1,4 +1,4 @@
-.TH COND-NUMERIC 1 "October 3, 2013" OpenBSD
+.TH COND-NUMERIC 1 "April 7, 2014" OpenBSD
.SH NAME
cond-numeric \- roff conditions involving numbers
.SH DESCRIPTION
@@ -98,3 +98,31 @@ mp
pm
.ie 1>-2 (t)
.el (f)
+.PP
+operator "&":
+00
+.ie 0&0 (t)
+.el (f)
+01
+.ie 0&1 (t)
+.el (f)
+10
+.ie 1&0 (t)
+.el (f)
+11
+.ie 1&1 (t)
+.el (f)
+.PP
+operator ":":
+00
+.ie 0:0 (t)
+.el (f)
+01
+.ie 0:1 (t)
+.el (f)
+10
+.ie 1:0 (t)
+.el (f)
+11
+.ie 1:1 (t)
+.el (f)
diff --git a/regress/usr.bin/mandoc/roff/cond/numeric.out_ascii b/regress/usr.bin/mandoc/roff/cond/numeric.out_ascii
index 6db189ac75b..4874f2146b5 100644
--- a/regress/usr.bin/mandoc/roff/cond/numeric.out_ascii
+++ b/regress/usr.bin/mandoc/roff/cond/numeric.out_ascii
@@ -26,6 +26,10 @@ DDEESSCCRRIIPPTTIIOONN
with negative numbers: mm (t) mp (t) pm (t)
+ operator "&": 00 (f) 01 (f) 10 (f) 11 (t)
+ operator ":": 00 (f) 01 (t) 10 (t) 11 (t)
-OpenBSD October 3, 2013 COND-NUMERIC(1)
+
+
+OpenBSD April 7, 2014 COND-NUMERIC(1)
diff --git a/regress/usr.bin/mandoc/roff/nr/Makefile b/regress/usr.bin/mandoc/roff/nr/Makefile
index 19e66eb2ae0..e642dadeb26 100644
--- a/regress/usr.bin/mandoc/roff/nr/Makefile
+++ b/regress/usr.bin/mandoc/roff/nr/Makefile
@@ -1,5 +1,5 @@
-# $OpenBSD: Makefile,v 1.4 2014/04/05 20:33:38 schwarze Exp $
+# $OpenBSD: Makefile,v 1.5 2014/04/07 15:05:12 schwarze Exp $
-REGRESS_TARGETS = int argc predef rr
+REGRESS_TARGETS = argc eval int predef rr
.include <bsd.regress.mk>
diff --git a/regress/usr.bin/mandoc/roff/nr/eval.in b/regress/usr.bin/mandoc/roff/nr/eval.in
new file mode 100644
index 00000000000..16bf24e3b5d
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/nr/eval.in
@@ -0,0 +1,67 @@
+.TH NR-EVAL 1 "April 7, 2014" OpenBSD
+.SH NAME
+nr-eval \- numeric expressions in assignments to number registers
+.SH DESCRIPTION
+.nr mr 1
+1: \n(mr
+.br
+.nr mr nonumber
+nonumber: \n(mr
+.br
+.nr mr 3X
+3X: \n(mr
+.br
+.nr mr 4+
+4+: \n(mr
+.br
+.nr mr 2+3
+2+3: \n(mr
+.br
+.nr mr 1+1:
+1+1:: \n(mr
+.br
+.nr mr 10-3
+10-3: \n(mr
+.br
+.nr mr 4*2
+4*2: \n(mr
+.br
+.nr mr 27/3
+27/3: \n(mr
+.br
+.nr mr 58%16
+58%16: \n(mr
+.br
+.nr mr 11<?20
+11<?20: \n(mr
+.br
+.nr mr 30<?12
+30<?12: \n(mr
+.br
+.nr mr 7>?13
+7>?13: \n(mr
+.br
+.nr mr 14>?6
+14>?6: \n(mr
+.br
+.nr mr 2+3*3
+2+3*3: \n(mr
+.br
+.nr mr 16+(
+para at eol: \n(mr
+.br
+.nr mr (17
+unclosed para: \n(mr
+.br
+.nr mr (18)
+(18): \n(mr
+.br
+.nr mr ( 25 - 6 )
+( 25 - 6 ): \n(mr
+.br
+.nr mr 11+( 3*3 )
+11+( 3*3 ): \n(mr
+.br
+.nr mr 3+(3*(5==5*2)*4)+(3*5)/2
+3+(3*(5==5*2)*4)+(3*5)/2: \n(mr
+.br
diff --git a/regress/usr.bin/mandoc/roff/nr/eval.out_ascii b/regress/usr.bin/mandoc/roff/nr/eval.out_ascii
new file mode 100644
index 00000000000..b2edcc52532
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/nr/eval.out_ascii
@@ -0,0 +1,33 @@
+NR-EVAL(1) OpenBSD Reference Manual NR-EVAL(1)
+
+
+
+NNAAMMEE
+ nr-eval - numeric expressions in assignments to number registers
+
+DDEESSCCRRIIPPTTIIOONN
+ 1: 1
+ nonumber: 1
+ 3X: 3
+ 4+: 3
+ 2+3: 5
+ 1+1:: 5
+ 10-3: 7
+ 4*2: 8
+ 27/3: 9
+ 58%16: 10
+ 11<?20: 11
+ 30<?12: 12
+ 7>?13: 13
+ 14>?6: 14
+ 2+3*3: 15
+ para at eol: 15
+ unclosed para: 17
+ (18): 18
+ ( 25 - 6 ): 19
+ 11+( 3*3 ): 20
+ 3+(3*(5==5*2)*4)+(3*5)/2: 21
+
+
+
+OpenBSD April 7, 2014 NR-EVAL(1)
diff --git a/share/man/man7/roff.7 b/share/man/man7/roff.7
index 79123050938..39019cd2195 100644
--- a/share/man/man7/roff.7
+++ b/share/man/man7/roff.7
@@ -1,4 +1,4 @@
-.\" $OpenBSD: roff.7,v 1.31 2014/04/05 20:33:38 schwarze Exp $
+.\" $OpenBSD: roff.7,v 1.32 2014/04/07 15:05:12 schwarze Exp $
.\"
.\" Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
.\" Copyright (c) 2010, 2011, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -15,7 +15,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: April 5 2014 $
+.Dd $Mdocdate: April 7 2014 $
.Dt ROFF 7
.Os
.Sh NAME
@@ -749,18 +749,12 @@ or
.Pq troff mode ,
COND evaluates to false.
.It
-If COND starts with a digit, optionally prefixed by a minus sign,
-it is evaluated as a numerical expression of the form
-.Ar number operator number ,
-where
-.Ar operator
-is one of
-.Sq < ,
-.Sq <= ,
-.Sq = ,
-.Sq >= ,
-or
-.Sq > .
+If COND starts with a parenthesis or with an optionally signed
+integer number, it is evaluated according to the rules of
+.Sx Numerical expressions
+explained below.
+It evaluates to true if the the result is positive,
+or to false if the result is zero or negative.
.It
Otherwise, the first character of COND is regarded as a delimiter
and COND evaluates to true if the string extending from its first
@@ -902,11 +896,13 @@ A register is an arbitrary string value that defines some sort of state,
which influences parsing and/or formatting.
Its syntax is as follows:
.Pp
-.D1 Pf \. Cm \&nr Ar name Oo +|- Oc Ns Ar value
+.D1 Pf \. Cm \&nr Ar name Oo +|- Oc Ns Ar expression
.Pp
-The
-.Ar value
-may, at the moment, only be an integer.
+For the syntax of
+.Ar expression ,
+see
+.Sx Numerical expressions
+below.
If it is prefixed by a sign, the register will be
incremented or decremented instead of assigned to.
.Pp
@@ -1019,6 +1015,73 @@ Begin a table, which formats input in aligned rows and columns.
See
.Xr tbl 7
for a description of the tbl language.
+.Ss Numerical expressions
+The
+.Sx \&nr ,
+.Sx \&if ,
+and
+.Sx \&ie
+requests accept integer numerical expressions as arguments.
+These are always evaluated using the C
+.Vt int
+type; integer overflow works the same way as in the C language.
+Numbers consist of an arbitrary number of digits
+.Sq 0
+to
+.Sq 9
+prefixed by an optional sign
+.Sq +
+or
+.Sq - .
+.Pp
+The following binary operators are implemented.
+Unless otherwise stated, they behave as in the C language:
+.Pp
+.Bl -tag -width 2n -compact
+.It Ic +
+addition
+.It Ic -
+subtraction
+.It Ic *
+multiplication
+.It Ic /
+division
+.It Ic %
+remainder of division
+.It Ic <
+less than
+.It Ic >
+greater than
+.It Ic ==
+equal to
+.It Ic =
+equal to, same effect as
+.Ic ==
+(this differs from C)
+.It Ic <=
+less than or equal to
+.It Ic >=
+greater than or equal to
+.It Ic <>
+not equal to (corresponds to C
+.Ic != ;
+this one is of limited portability, it is supported by Heirloom roff,
+but not by groff)
+.It Ic &
+logical and (corresponds to C
+.Ic && )
+.It Ic \&:
+logical or (corresponds to C
+.Ic \&|| )
+.It Ic <?
+minimum (not available in C)
+.It Ic >?
+maximum (not available in C)
+.El
+.Pp
+There is no concept of precendence; evaluation proceeds from left to right,
+except when subexpressions are enclosed in parantheses.
+Inside parentheses, whitespace is ignored.
.Sh ESCAPE SEQUENCE REFERENCE
The
.Xr mandoc 1
diff --git a/usr.bin/mandoc/roff.c b/usr.bin/mandoc/roff.c
index 8e4e8727a2d..dcb7945800b 100644
--- a/usr.bin/mandoc/roff.c
+++ b/usr.bin/mandoc/roff.c
@@ -1,4 +1,4 @@
-/* $Id: roff.c,v 1.76 2014/04/05 20:33:38 schwarze Exp $ */
+/* $Id: roff.c,v 1.77 2014/04/07 15:05:12 schwarze Exp $ */
/*
* Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -177,6 +177,8 @@ static enum rofferr roff_cond_text(ROFF_ARGS);
static enum rofferr roff_cond_sub(ROFF_ARGS);
static enum rofferr roff_ds(ROFF_ARGS);
static int roff_evalcond(const char *, int *);
+static int roff_evalnum(const char *, int *, int *, int);
+static int roff_evalpar(const char *, int *, int *);
static int roff_evalstrcond(const char *, int *);
static void roff_free1(struct roff *);
static void roff_freereg(struct roffreg *);
@@ -1103,6 +1105,12 @@ roff_cond_text(ROFF_ARGS)
return(rr ? ROFF_CONT : ROFF_IGN);
}
+/*
+ * Parse a single signed integer number. Stop at the first non-digit.
+ * If there is at least one digit, return success and advance the
+ * parse point, else return failure and let the parse point unchanged.
+ * Ignore overflows, treat them just like the C language.
+ */
static int
roff_getnum(const char *v, int *pos, int *res)
{
@@ -1114,7 +1122,7 @@ roff_getnum(const char *v, int *pos, int *res)
p++;
for (*res = 0; isdigit((unsigned char)v[p]); p++)
- *res += 10 * *res + v[p] - '0';
+ *res = 10 * *res + v[p] - '0';
if (p == *pos + n)
return 0;
@@ -1125,34 +1133,6 @@ roff_getnum(const char *v, int *pos, int *res)
return 1;
}
-static int
-roff_getop(const char *v, int *pos, char *res)
-{
- int e;
-
- *res = v[*pos];
- e = v[*pos + 1] == '=';
-
- switch (*res) {
- case '=':
- break;
- case '>':
- if (e)
- *res = 'g';
- break;
- case '<':
- if (e)
- *res = 'l';
- break;
- default:
- return(0);
- }
-
- *pos += 1 + e;
-
- return(*res);
-}
-
/*
* Evaluate a string comparison condition.
* The first character is the delimiter.
@@ -1196,11 +1176,14 @@ out:
return(match);
}
+/*
+ * Evaluate an optionally negated single character, numerical,
+ * or string condition.
+ */
static int
roff_evalcond(const char *v, int *pos)
{
- int wanttrue, lh, rh;
- char op;
+ int wanttrue, number;
if ('!' == v[*pos]) {
wanttrue = 0;
@@ -1229,27 +1212,10 @@ roff_evalcond(const char *v, int *pos)
break;
}
- if (!roff_getnum(v, pos, &lh))
+ if (roff_evalnum(v, pos, &number, 0))
+ return((number > 0) == wanttrue);
+ else
return(roff_evalstrcond(v, pos) == wanttrue);
- if (!roff_getop(v, pos, &op))
- return((lh > 0) == wanttrue);
- if (!roff_getnum(v, pos, &rh))
- return(0);
-
- switch (op) {
- case 'g':
- return((lh >= rh) == wanttrue);
- case 'l':
- return((lh <= rh) == wanttrue);
- case '=':
- return((lh == rh) == wanttrue);
- case '>':
- return((lh > rh) == wanttrue);
- case '<':
- return((lh < rh) == wanttrue);
- default:
- return(0);
- }
}
/* ARGSUSED */
@@ -1367,6 +1333,194 @@ roff_ds(ROFF_ARGS)
return(ROFF_IGN);
}
+/*
+ * Parse a single operator, one or two characters long.
+ * If the operator is recognized, return success and advance the
+ * parse point, else return failure and let the parse point unchanged.
+ */
+static int
+roff_getop(const char *v, int *pos, char *res)
+{
+
+ *res = v[*pos];
+
+ switch (*res) {
+ case ('+'):
+ /* FALLTHROUGH */
+ case ('-'):
+ /* FALLTHROUGH */
+ case ('*'):
+ /* FALLTHROUGH */
+ case ('/'):
+ /* FALLTHROUGH */
+ case ('%'):
+ /* FALLTHROUGH */
+ case ('&'):
+ /* FALLTHROUGH */
+ case (':'):
+ break;
+ case '<':
+ switch (v[*pos + 1]) {
+ case ('='):
+ *res = 'l';
+ (*pos)++;
+ break;
+ case ('>'):
+ *res = '!';
+ (*pos)++;
+ break;
+ case ('?'):
+ *res = 'i';
+ (*pos)++;
+ break;
+ default:
+ break;
+ }
+ break;
+ case '>':
+ switch (v[*pos + 1]) {
+ case ('='):
+ *res = 'g';
+ (*pos)++;
+ break;
+ case ('?'):
+ *res = 'a';
+ (*pos)++;
+ break;
+ default:
+ break;
+ }
+ break;
+ case '=':
+ if ('=' == v[*pos + 1])
+ (*pos)++;
+ break;
+ default:
+ return(0);
+ }
+ (*pos)++;
+
+ return(*res);
+}
+
+/*
+ * Evaluate either a parenthesized numeric expression
+ * or a single signed integer number.
+ */
+static int
+roff_evalpar(const char *v, int *pos, int *res)
+{
+
+ if ('(' != v[*pos])
+ return(roff_getnum(v, pos, res));
+
+ (*pos)++;
+ if ( ! roff_evalnum(v, pos, res, 1))
+ return(0);
+
+ /* If the trailing parenthesis is missing, ignore the error. */
+ if (')' == v[*pos])
+ (*pos)++;
+
+ return(1);
+}
+
+/*
+ * Evaluate a complete numeric expression.
+ * Proceed left to right, there is no concept of precedence.
+ */
+static int
+roff_evalnum(const char *v, int *pos, int *res, int skipwhite)
+{
+ int mypos, operand2;
+ char operator;
+
+ if (NULL == pos) {
+ mypos = 0;
+ pos = &mypos;
+ }
+
+ if (skipwhite)
+ while (isspace((unsigned char)v[*pos]))
+ (*pos)++;
+
+ if ( ! roff_evalpar(v, pos, res))
+ return(0);
+
+ while (1) {
+ if (skipwhite)
+ while (isspace((unsigned char)v[*pos]))
+ (*pos)++;
+
+ if ( ! roff_getop(v, pos, &operator))
+ break;
+
+ if (skipwhite)
+ while (isspace((unsigned char)v[*pos]))
+ (*pos)++;
+
+ if ( ! roff_evalpar(v, pos, &operand2))
+ return(0);
+
+ if (skipwhite)
+ while (isspace((unsigned char)v[*pos]))
+ (*pos)++;
+
+ switch (operator) {
+ case ('+'):
+ *res += operand2;
+ break;
+ case ('-'):
+ *res -= operand2;
+ break;
+ case ('*'):
+ *res *= operand2;
+ break;
+ case ('/'):
+ *res /= operand2;
+ break;
+ case ('%'):
+ *res %= operand2;
+ break;
+ case ('<'):
+ *res = *res < operand2;
+ break;
+ case ('>'):
+ *res = *res > operand2;
+ break;
+ case ('l'):
+ *res = *res <= operand2;
+ break;
+ case ('g'):
+ *res = *res >= operand2;
+ break;
+ case ('='):
+ *res = *res == operand2;
+ break;
+ case ('!'):
+ *res = *res != operand2;
+ break;
+ case ('&'):
+ *res = *res && operand2;
+ break;
+ case (':'):
+ *res = *res || operand2;
+ break;
+ case ('i'):
+ if (operand2 < *res)
+ *res = operand2;
+ break;
+ case ('a'):
+ if (operand2 > *res)
+ *res = operand2;
+ break;
+ default:
+ abort();
+ }
+ }
+ return(1);
+}
+
void
roff_setreg(struct roff *r, const char *name, int val, char sign)
{
@@ -1476,13 +1630,11 @@ roff_freereg(struct roffreg *reg)
}
}
-/* ARGSUSED */
static enum rofferr
roff_nr(ROFF_ARGS)
{
const char *key;
char *val;
- size_t sz;
int iv;
char sign;
@@ -1493,10 +1645,8 @@ roff_nr(ROFF_ARGS)
if ('+' == sign || '-' == sign)
val++;
- sz = strspn(val, "0123456789");
- iv = sz ? mandoc_strntoi(val, sz, 10) : 0;
-
- roff_setreg(r, key, iv, sign);
+ if (roff_evalnum(val, NULL, &iv, 0))
+ roff_setreg(r, key, iv, sign);
return(ROFF_IGN);
}