+/*
+ * 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);
+}
+