[hackers] [sbase] Add octal sequence support to tr(1) || FRIGN

From: <git_AT_suckless.org>
Date: Tue, 24 Mar 2015 23:53:15 +0100 (CET)

commit eb57becb38526404c5bcea20bcb498a9ddb0a218
Author: FRIGN <dev_AT_frign.de>
Date: Sat Jan 24 22:43:46 2015 +0100

    Add octal sequence support to tr(1)

diff --git a/tr.1 b/tr.1
index 1f96dc5..29ea1f1 100644
--- a/tr.1
+++ b/tr.1
_AT_@ -73,4 +73,4 @@ The
 .Nm
 utility is compliant with the
 .St -p1003.1-2008
-except from octal sequences and equivalence classes.
+specification except from equivalence classes.
diff --git a/tr.c b/tr.c
index cefb918..e2e6b71 100644
--- a/tr.c
+++ b/tr.c
_AT_@ -73,7 +73,7 @@ rstrmatch(Rune *r, char *s, size_t n)
 static size_t
 resolveescapes(Rune *r, size_t len)
 {
- size_t i, off, m;
+ size_t i, off, m, factor, q;
 
         for (i = 0; i < len - 1; i++) {
                 if (r[i] != '\\')
_AT_@ -89,7 +89,21 @@ resolveescapes(Rune *r, size_t len)
                 case 'r': r[i] = '\r'; off++; break;
                 case 't': r[i] = '\t'; off++; break;
                 case 'v': r[i] = '\v'; off++; break;
- default: continue;
+ case '\0':
+ eprintf("tr: null escape sequence\n");
+ default:
+ /* "\O[OO]" octal escape */
+ for (m = i + 1; m < i + 1 + 3 && m < len; m++)
+ if (r[m] < '0' || r[m] > '7')
+ break;
+ if (m == i + 1)
+ continue;
+ off += m - i - 1;
+ for (--m, q = 0, factor = 1; m > i; m--) {
+ q += (r[m] - '0') * factor;
+ factor *= 8;
+ }
+ r[i] = q;
                 }
 
                 for (m = i + 1; m <= len - off; m++)
Received on Tue Mar 24 2015 - 23:53:15 CET

This archive was generated by hypermail 2.3.0 : Wed Mar 25 2015 - 00:02:43 CET