* patch: (read "\x1b") => #\esc
@ 2003-11-10 22:41 Paul Jarc
2003-11-10 22:48 ` Paul Jarc
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Paul Jarc @ 2003-11-10 22:41 UTC (permalink / raw)
[-- Attachment #1: Type: TEXT/PLAIN, Size: 453 bytes --]
This patch fixes a failure to detect EOF, and also provides "\xNN" in
strings, where NN are exactly two hexadecimal digits. This way, your
source code can be safe to print to a tty even if the strings it
defines would not be. If this is acceptable, I'd also like to change
print.c to use this when (write)ing strings containing control
characters.
* read.c (scm_lreadr): detect EOF after backslash, and
interpret \xNN hexadecimal sequences.
paul
[-- Attachment #2: read-string-hex.patch --]
[-- Type: text/x-patch, Size: 1761 bytes --]
Index: guile-core/libguile/read.c
===================================================================
RCS file: /cvsroot/guile/guile/guile-core/libguile/read.c,v
retrieving revision 1.91
diff -u -r1.91 read.c
--- guile-core/libguile/read.c 4 Jun 2003 16:36:03 -0000 1.91
+++ guile-core/libguile/read.c 10 Nov 2003 22:28:07 -0000
@@ -489,7 +489,7 @@
while ('"' != (c = scm_getc (port)))
{
if (c == EOF)
- scm_input_error (FUNC_NAME, port, "end of file in string constant", SCM_EOL);
+ str_eof: scm_input_error (FUNC_NAME, port, "end of file in string constant", SCM_EOL);
while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
scm_grow_tok_buf (tok_buf);
@@ -497,6 +497,8 @@
if (c == '\\')
switch (c = scm_getc (port))
{
+ case EOF:
+ goto str_eof;
case '\n':
continue;
case '0':
@@ -520,6 +522,30 @@
case 'v':
c = '\v';
break;
+ case 'x':
+ {
+ int a, b, a_09 = 0, b_09 = 0, a_AF = 0, b_AF = 0, a_af = 0,
+ b_af = 0;
+ a = scm_getc (port);
+ if (a == EOF) goto str_eof;
+ b = scm_getc (port);
+ if (b == EOF) goto str_eof;
+ if ('0' <= a && a <= '9') a_09 = 1;
+ else if ('A' <= a && a <= 'F') a_AF = 1;
+ else if ('a' <= a && a <= 'f') a_af = 1;
+ if ('0' <= b && b <= '9') b_09 = 1;
+ else if ('A' <= b && b <= 'F') b_AF = 1;
+ else if ('a' <= b && b <= 'f') b_af = 1;
+ if ((a_09 || a_AF || a_af) && (b_09 || b_AF || b_af))
+ c = (a_09? a - '0': a_AF? a - 'A' + 10: a - 'a' + 10) * 16
+ + (b_09? b - '0': b_AF? b - 'A' + 10: b - 'a' + 10);
+ else
+ {
+ scm_ungetc (b, port);
+ scm_ungetc (a, port);
+ }
+ break;
+ }
}
SCM_STRING_CHARS (*tok_buf)[j] = c;
++j;
[-- Attachment #3: Type: text/plain, Size: 142 bytes --]
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: patch: (read "\x1b") => #\esc
2003-11-10 22:41 patch: (read "\x1b") => #\esc Paul Jarc
@ 2003-11-10 22:48 ` Paul Jarc
2003-11-10 23:18 ` Kevin Ryde
2003-11-13 20:10 ` Marius Vollmer
2 siblings, 0 replies; 11+ messages in thread
From: Paul Jarc @ 2003-11-10 22:48 UTC (permalink / raw)
Er, the subject is misleading. Make that:
(string-ref (with-input-from-string "\"x1b\"" read) 0) => #\eof
Well, you get the idea.
paul
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: patch: (read "\x1b") => #\esc
2003-11-10 22:41 patch: (read "\x1b") => #\esc Paul Jarc
2003-11-10 22:48 ` Paul Jarc
@ 2003-11-10 23:18 ` Kevin Ryde
2003-11-11 2:45 ` Paul Jarc
2003-11-13 20:10 ` Marius Vollmer
2 siblings, 1 reply; 11+ messages in thread
From: Kevin Ryde @ 2003-11-10 23:18 UTC (permalink / raw)
prj@po.cwru.edu (Paul Jarc) writes:
>
> + case EOF:
> + goto str_eof;
Currently that gets noticed next time around the loop does it?
(But not expecting scm_getc to return EOF a second time is probably a
good thing.)
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: patch: (read "\x1b") => #\esc
2003-11-10 22:41 patch: (read "\x1b") => #\esc Paul Jarc
2003-11-10 22:48 ` Paul Jarc
2003-11-10 23:18 ` Kevin Ryde
@ 2003-11-13 20:10 ` Marius Vollmer
2003-11-18 20:04 ` Paul Jarc
2003-11-25 17:12 ` Paul Jarc
2 siblings, 2 replies; 11+ messages in thread
From: Marius Vollmer @ 2003-11-13 20:10 UTC (permalink / raw)
prj@po.cwru.edu (Paul Jarc) writes:
> This patch fixes a failure to detect EOF, and also provides "\xNN" in
> strings, where NN are exactly two hexadecimal digits.
Nice!
What about this change, tho: instead of ignoring a \x sequence that
does not have two hexadecmal digits, we could signal an error.
Likewise, we could signal errors for all unrecognized \ sequences.
I have applied your patch unchanged as a start.
--
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3 331E FAF8 226A D5D4 E405
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: patch: (read "\x1b") => #\esc
2003-11-13 20:10 ` Marius Vollmer
@ 2003-11-18 20:04 ` Paul Jarc
2003-11-19 0:24 ` Paul Jarc
2003-11-29 23:43 ` Marius Vollmer
2003-11-25 17:12 ` Paul Jarc
1 sibling, 2 replies; 11+ messages in thread
From: Paul Jarc @ 2003-11-18 20:04 UTC (permalink / raw)
Cc: guile-devel
Marius Vollmer <mvo@zagadka.de> wrote:
> What about this change, tho: instead of ignoring a \x sequence that
> does not have two hexadecmal digits, we could signal an error.
> Likewise, we could signal errors for all unrecognized \ sequences.
Will do; I was thinking that too. Meanwhile, here's the patch for
writing.
* print.c (scm_iprin1): use \xNN hexadecimal sequences when
writing control characters in strings.
paul
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: patch: (read "\x1b") => #\esc
2003-11-18 20:04 ` Paul Jarc
@ 2003-11-19 0:24 ` Paul Jarc
2003-11-29 23:43 ` Marius Vollmer
1 sibling, 0 replies; 11+ messages in thread
From: Paul Jarc @ 2003-11-19 0:24 UTC (permalink / raw)
Cc: guile-devel
[-- Attachment #1: Type: TEXT/PLAIN, Size: 66 bytes --]
I wrote:
> Meanwhile, here's the patch for writing.
Oops.
paul
[-- Attachment #2: write-string-hex.patch --]
[-- Type: text/x-patch, Size: 1222 bytes --]
Index: guile-core/libguile/print.c
===================================================================
RCS file: /cvsroot/guile/guile/guile-core/libguile/print.c,v
retrieving revision 1.155
diff -u -r1.155 print.c
--- guile-core/libguile/print.c 11 Oct 2003 00:57:25 -0000 1.155
+++ guile-core/libguile/print.c 18 Nov 2003 19:57:56 -0000
@@ -538,16 +538,24 @@
scm_putc ('"', port);
for (i = 0; i < SCM_STRING_LENGTH (exp); ++i)
- switch (SCM_STRING_CHARS (exp)[i])
- {
- case '"':
- case '\\':
- scm_putc ('\\', port);
- default:
- scm_putc (SCM_STRING_CHARS (exp)[i], port);
- }
+ {
+ unsigned char ch = SCM_STRING_CHARS (exp)[i];
+ if ((ch < 32 && ch != '\n') || (127 <= ch && ch < 148))
+ {
+ static char const hex[]="0123456789abcdef";
+ scm_putc ('\\', port);
+ scm_putc ('x', port);
+ scm_putc (hex [ch / 16], port);
+ scm_putc (hex [ch % 16], port);
+ }
+ else
+ {
+ if (ch == '"' || ch == '\\')
+ scm_putc ('\\', port);
+ scm_putc (ch, port);
+ }
+ }
scm_putc ('"', port);
- break;
}
else
scm_lfwrite (SCM_STRING_CHARS (exp), SCM_STRING_LENGTH (exp), port);
[-- Attachment #3: Type: text/plain, Size: 142 bytes --]
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: patch: (read "\x1b") => #\esc
2003-11-18 20:04 ` Paul Jarc
2003-11-19 0:24 ` Paul Jarc
@ 2003-11-29 23:43 ` Marius Vollmer
1 sibling, 0 replies; 11+ messages in thread
From: Marius Vollmer @ 2003-11-29 23:43 UTC (permalink / raw)
prj@po.cwru.edu (Paul Jarc) writes:
> Meanwhile, here's the patch for writing.
Thanks, applied!
--
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3 331E FAF8 226A D5D4 E405
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: patch: (read "\x1b") => #\esc
2003-11-13 20:10 ` Marius Vollmer
2003-11-18 20:04 ` Paul Jarc
@ 2003-11-25 17:12 ` Paul Jarc
2003-11-30 1:05 ` Marius Vollmer
1 sibling, 1 reply; 11+ messages in thread
From: Paul Jarc @ 2003-11-25 17:12 UTC (permalink / raw)
Cc: guile-devel
[-- Attachment #1: Type: TEXT/PLAIN, Size: 366 bytes --]
Marius Vollmer <mvo@zagadka.de> wrote:
> What about this change, tho: instead of ignoring a \x sequence that
> does not have two hexadecmal digits, we could signal an error.
> Likewise, we could signal errors for all unrecognized \ sequences.
Here it is.
* read.c (scm_lreadr): Signal an error for invalid escape
sequences in strings. Code cleanups too.
paul
[-- Attachment #2: read-string-error.patch --]
[-- Type: text/x-patch, Size: 1775 bytes --]
Index: guile-core/libguile/read.c
===================================================================
RCS file: /cvsroot/guile/guile/guile-core/libguile/read.c,v
retrieving revision 1.92
diff -u -r1.92 read.c
--- guile-core/libguile/read.c 16 Nov 2003 21:01:57 -0000 1.92
+++ guile-core/libguile/read.c 25 Nov 2003 17:06:09 -0000
@@ -524,28 +524,27 @@
break;
case 'x':
{
- int a, b, a_09 = 0, b_09 = 0, a_AF = 0, b_AF = 0, a_af = 0,
- b_af = 0;
+ int a, b;
a = scm_getc (port);
if (a == EOF) goto str_eof;
b = scm_getc (port);
if (b == EOF) goto str_eof;
- if ('0' <= a && a <= '9') a_09 = 1;
- else if ('A' <= a && a <= 'F') a_AF = 1;
- else if ('a' <= a && a <= 'f') a_af = 1;
- if ('0' <= b && b <= '9') b_09 = 1;
- else if ('A' <= b && b <= 'F') b_AF = 1;
- else if ('a' <= b && b <= 'f') b_af = 1;
- if ((a_09 || a_AF || a_af) && (b_09 || b_AF || b_af))
- c = (a_09? a - '0': a_AF? a - 'A' + 10: a - 'a' + 10) * 16
- + (b_09? b - '0': b_AF? b - 'A' + 10: b - 'a' + 10);
- else
- {
- scm_ungetc (b, port);
- scm_ungetc (a, port);
- }
+ if ('0' <= a && a <= '9') a -= '0';
+ else if ('A' <= a && a <= 'F') a = a - 'A' + 10;
+ else if ('a' <= a && a <= 'f') a = a - 'a' + 10;
+ else goto bad_escaped;
+ if ('0' <= b && b <= '9') b -= '0';
+ else if ('A' <= b && b <= 'F') b = b - 'A' + 10;
+ else if ('a' <= b && b <= 'f') b = b - 'a' + 10;
+ else goto bad_escaped;
+ c = a * 16 + b;
break;
}
+ default:
+ bad_escaped:
+ scm_input_error(FUNC_NAME, port,
+ "illegal character in escape sequence: ~S",
+ scm_list_1 (SCM_MAKE_CHAR (c)));
}
SCM_STRING_CHARS (*tok_buf)[j] = c;
++j;
[-- Attachment #3: Type: text/plain, Size: 142 bytes --]
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2003-11-30 1:05 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-10 22:41 patch: (read "\x1b") => #\esc Paul Jarc
2003-11-10 22:48 ` Paul Jarc
2003-11-10 23:18 ` Kevin Ryde
2003-11-11 2:45 ` Paul Jarc
2003-11-11 21:48 ` Kevin Ryde
2003-11-13 20:10 ` Marius Vollmer
2003-11-18 20:04 ` Paul Jarc
2003-11-19 0:24 ` Paul Jarc
2003-11-29 23:43 ` Marius Vollmer
2003-11-25 17:12 ` Paul Jarc
2003-11-30 1:05 ` Marius Vollmer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).