commit eb57becb38526404c5bcea20bcb498a9ddb0a218
parent 27996f2b86ada4de79190fb1fff49b48091ef72a
Author: FRIGN <dev@frign.de>
Date: Sat, 24 Jan 2015 22:43:46 +0100
Add octal sequence support to tr(1)
Diffstat:
M | tr.1 | | | 2 | +- |
M | tr.c | | | 18 | ++++++++++++++++-- |
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/tr.1 b/tr.1
@@ -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
@@ -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] != '\\')
@@ -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++)