summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2003-09-30 18:33:35 +0000
committerotto <otto@openbsd.org>2003-09-30 18:33:35 +0000
commitadedf6af41fa03f818e2d14b17df2d0b005edaf2 (patch)
tree12c043c1f1422cec92e5288738e5a6bd8337f346
parentFlush stdout after P operator. Improves interaction with bc(1). (diff)
downloadwireguard-openbsd-adedf6af41fa03f818e2d14b17df2d0b005edaf2.tar.xz
wireguard-openbsd-adedf6af41fa03f818e2d14b17df2d0b005edaf2.zip
Teach dc(1) how to read strings with unbalanced braces by introducing
backslash as an escape char. This is needed for bc(1), which is required by Posix to handle strings with brackets in them.
-rw-r--r--usr.bin/dc/dc.17
-rw-r--r--usr.bin/dc/inout.c35
2 files changed, 27 insertions, 15 deletions
diff --git a/usr.bin/dc/dc.1 b/usr.bin/dc/dc.1
index 32643abc890..f9d719e9a4f 100644
--- a/usr.bin/dc/dc.1
+++ b/usr.bin/dc/dc.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: dc.1,v 1.4 2003/09/22 14:50:32 otto Exp $
+.\" $OpenBSD: dc.1,v 1.5 2003/09/30 18:33:35 otto Exp $
.\"
.\" Copyright (C) Caldera International Inc. 2001-2002.
.\" All rights reserved.
@@ -189,7 +189,10 @@ If the top of the stack is a string, replace it with the integer 0.
Puts the bracketed
.Tn ASCII
string onto the top of the stack.
-The brackets may be nested.
+If the string includes brackets, these must be properly balanced.
+The backslash character \e\ may be used as an escape character, making it
+possible to include unbalanced brackets in strings.
+To include a backslash into a string, use a double backslash.
.It Xo
.Cm < Ns Va x
.Cm > Ns Va x
diff --git a/usr.bin/dc/inout.c b/usr.bin/dc/inout.c
index 8326bb910d7..9c4bb0c8da2 100644
--- a/usr.bin/dc/inout.c
+++ b/usr.bin/dc/inout.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: inout.c,v 1.4 2003/09/28 19:29:32 otto Exp $ */
+/* $OpenBSD: inout.c,v 1.5 2003/09/30 18:33:35 otto Exp $ */
/*
* Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
@@ -17,7 +17,7 @@
*/
#ifndef lint
-static const char rcsid[] = "$OpenBSD: inout.c,v 1.4 2003/09/28 19:29:32 otto Exp $";
+static const char rcsid[] = "$OpenBSD: inout.c,v 1.5 2003/09/30 18:33:35 otto Exp $";
#endif /* not lint */
#include <ssl/ssl.h>
@@ -216,25 +216,34 @@ read_string(struct source *src)
{
int count, i, sz, new_sz, ch;
char *p;
+ bool escape;
+ escape = false;
count = 1;
i = 0;
sz = 15;
p = bmalloc(sz + 1);
while ((ch = (*src->vtable->readchar)(src)) != EOF) {
- if (ch == '[')
- count++;
- else if (ch == ']')
- count--;
- if (count == 0)
- break;
- if (i == sz) {
- new_sz = sz * 2;
- p = brealloc(p, new_sz + 1);
- sz = new_sz;
+ if (!escape) {
+ if (ch == '[')
+ count++;
+ else if (ch == ']')
+ count--;
+ if (count == 0)
+ break;
+ }
+ if (ch == '\\' && !escape)
+ escape = true;
+ else {
+ escape = false;
+ if (i == sz) {
+ new_sz = sz * 2;
+ p = brealloc(p, new_sz + 1);
+ sz = new_sz;
+ }
+ p[i++] = ch;
}
- p[i++] = ch;
}
p[i] = '\0';
return p;