aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/roff.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2014-04-07 15:07:13 +0000
committerIngo Schwarze <schwarze@openbsd.org>2014-04-07 15:07:13 +0000
commit0f4d7e63b2cdb7406eb497a9d061fe740845a0bc (patch)
tree5e21b8877d700dec0db6f97b6432910dce831e8a /roff.c
parentf7ae141bdbc537700c2e9f197c05fefe73126d5c (diff)
downloadmandoc-0f4d7e63b2cdb7406eb497a9d061fe740845a0bc.tar.gz
mandoc-0f4d7e63b2cdb7406eb497a9d061fe740845a0bc.tar.zst
mandoc-0f4d7e63b2cdb7406eb497a9d061fe740845a0bc.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().
Diffstat (limited to 'roff.c')
-rw-r--r--roff.c266
1 files changed, 208 insertions, 58 deletions
diff --git a/roff.c b/roff.c
index 1316bfa9..ca8d7f1f 100644
--- a/roff.c
+++ b/roff.c
@@ -1,4 +1,4 @@
-/* $Id: roff.c,v 1.203 2014/04/05 20:34:57 schwarze Exp $ */
+/* $Id: roff.c,v 1.204 2014/04/07 15:07:13 schwarze Exp $ */
/*
* Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -181,6 +181,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 *);
@@ -1107,6 +1109,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)
{
@@ -1118,7 +1126,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;
@@ -1129,34 +1137,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.
@@ -1200,11 +1180,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;
@@ -1233,27 +1216,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 */
@@ -1371,6 +1337,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)
{
@@ -1480,13 +1634,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;
@@ -1497,10 +1649,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);
}