summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorOtto Moerbeek <otto@cvs.openbsd.org>2003-10-22 12:15:21 +0000
committerOtto Moerbeek <otto@cvs.openbsd.org>2003-10-22 12:15:21 +0000
commita253b3ed201890a2b95faf5a2af6e77fec76e30b (patch)
treec850dcaa3b8948044f11a3bf542d7cd98b7c4f21 /usr.bin
parentf49ecb567de10a6bb8d3d8459d78e41d7f1ec617 (diff)
Implement extended comparison operators, to allow for an if ... else construct
in bc(1).
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/dc/bcode.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/usr.bin/dc/bcode.c b/usr.bin/dc/bcode.c
index fc9ccbff6f2..f68e354cedf 100644
--- a/usr.bin/dc/bcode.c
+++ b/usr.bin/dc/bcode.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bcode.c,v 1.10 2003/10/18 20:33:48 otto Exp $ */
+/* $OpenBSD: bcode.c,v 1.11 2003/10/22 12:15:20 otto Exp $ */
/*
* Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
@@ -17,7 +17,7 @@
*/
#ifndef lint
-static const char rcsid[] = "$OpenBSD: bcode.c,v 1.10 2003/10/18 20:33:48 otto Exp $";
+static const char rcsid[] = "$OpenBSD: bcode.c,v 1.11 2003/10/22 12:15:20 otto Exp $";
#endif /* not lint */
#include <ssl/ssl.h>
@@ -36,6 +36,8 @@ BIGNUM zero;
#define MAX_ARRAY_INDEX 2048
#define MAX_RECURSION 100
+#define NO_ELSE -2 /* -1 is EOF */
+
struct bmachine {
struct stack stack;
u_int scale;
@@ -1237,19 +1239,23 @@ not_greater(void)
static void
compare(enum bcode_compare type)
{
- int index;
+ int index, elseindex;
struct number *a, *b;
u_int scale;
int cmp;
bool ok;
struct value *v;
+ elseindex = NO_ELSE;
index = readch();
+ if (readch() == 'e')
+ elseindex = readch();
+ else
+ unreadch();
a = pop_number();
- if (a == NULL) {
+ if (a == NULL)
return;
- }
b = pop_number();
if (b == NULL) {
push_number(a);
@@ -1289,10 +1295,15 @@ compare(enum bcode_compare type)
break;
}
- if (ok) {
+ if (!ok && elseindex != NO_ELSE)
+ index = elseindex;
+
+ if (index < 0 || index > UCHAR_MAX)
+ warnx("internal error: reg num = %d", index);
+ else if (ok || (!ok && elseindex != NO_ELSE)) {
v = stack_tos(&bmachine.reg[index]);
if (v == NULL)
- warn("stack empty");
+ warnx("register '%c' (0%o) is empty", index, index);
else {
switch(v->type) {
case BCODE_NONE: