Add octal sequence support to tr(1)

This commit is contained in:
FRIGN 2015-01-24 22:43:46 +01:00
parent 27996f2b86
commit eb57becb38
2 changed files with 17 additions and 3 deletions

2
tr.1
View File

@ -73,4 +73,4 @@ The
.Nm .Nm
utility is compliant with the utility is compliant with the
.St -p1003.1-2008 .St -p1003.1-2008
except from octal sequences and equivalence classes. specification except from equivalence classes.

18
tr.c
View File

@ -73,7 +73,7 @@ rstrmatch(Rune *r, char *s, size_t n)
static size_t static size_t
resolveescapes(Rune *r, size_t len) 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++) { for (i = 0; i < len - 1; i++) {
if (r[i] != '\\') if (r[i] != '\\')
@ -89,7 +89,21 @@ resolveescapes(Rune *r, size_t len)
case 'r': r[i] = '\r'; off++; break; case 'r': r[i] = '\r'; off++; break;
case 't': r[i] = '\t'; off++; break; case 't': r[i] = '\t'; off++; break;
case 'v': r[i] = '\v'; 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++) for (m = i + 1; m <= len - off; m++)