diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2003-09-30 18:33:36 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2003-09-30 18:33:36 +0000 |
commit | e6543e9ee25b6a30182de6cc78d812f81727b1a9 (patch) | |
tree | ced2def7fc946926c980b197a06a3fa460df4e52 /usr.bin/dc | |
parent | 88a72857d93404b78081f9247cb1d1ef76e030c5 (diff) |
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.
Diffstat (limited to 'usr.bin/dc')
-rw-r--r-- | usr.bin/dc/dc.1 | 7 | ||||
-rw-r--r-- | usr.bin/dc/inout.c | 35 |
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; |