Add unescape() to libutil
formerly known as resolveescapes(), it is of central use to numerous programs. This drops a lot of LOC.
This commit is contained in:
parent
d4e2067d3d
commit
b8b9d983c8
3
Makefile
3
Makefile
|
@ -54,7 +54,8 @@ LIBUTILSRC =\
|
|||
libutil/strcasestr.c\
|
||||
libutil/strlcat.c\
|
||||
libutil/strlcpy.c\
|
||||
libutil/strsep.c
|
||||
libutil/strsep.c\
|
||||
libutil/unescape.c
|
||||
|
||||
LIB = $(LIBUTF) $(LIBUTIL)
|
||||
|
||||
|
|
36
cut.c
36
cut.c
|
@ -139,40 +139,6 @@ cut(FILE *fp)
|
|||
}
|
||||
}
|
||||
|
||||
static size_t
|
||||
resolveescapes(char *s, size_t len)
|
||||
{
|
||||
size_t i, off, m;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (s[i] != '\\')
|
||||
continue;
|
||||
off = 0;
|
||||
|
||||
switch (s[i + 1]) {
|
||||
case '\\': s[i] = '\\'; off++; break;
|
||||
case 'a': s[i] = '\a'; off++; break;
|
||||
case 'b': s[i] = '\b'; off++; break;
|
||||
case 'f': s[i] = '\f'; off++; break;
|
||||
case 'n': s[i] = '\n'; off++; break;
|
||||
case 'r': s[i] = '\r'; off++; break;
|
||||
case 't': s[i] = '\t'; off++; break;
|
||||
case 'v': s[i] = '\v'; off++; break;
|
||||
case '\0':
|
||||
eprintf("cut: null escape sequence in delimiter\n");
|
||||
default:
|
||||
eprintf("cut: invalid escape sequence '\\%c' in "
|
||||
"delimiter\n", s[i + 1]);
|
||||
}
|
||||
|
||||
for (m = i + 1; m <= len - off; m++)
|
||||
s[m] = s[m + off];
|
||||
len -= off;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
|
@ -197,7 +163,7 @@ main(int argc, char *argv[])
|
|||
delim = EARGF(usage());
|
||||
if (!*delim)
|
||||
eprintf("cut: empty delimiter\n");
|
||||
delimlen = resolveescapes(delim, strlen(delim));
|
||||
delimlen = unescape(delim);
|
||||
break;
|
||||
case 'n':
|
||||
nflag = 1;
|
||||
|
|
50
libutil/unescape.c
Normal file
50
libutil/unescape.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <string.h>
|
||||
|
||||
#include "../util.h"
|
||||
|
||||
size_t
|
||||
unescape(char *s)
|
||||
{
|
||||
size_t len, i, off, m, factor, q;
|
||||
|
||||
len = strlen(s);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (s[i] != '\\')
|
||||
continue;
|
||||
off = 0;
|
||||
|
||||
switch (s[i + 1]) {
|
||||
case '\\': s[i] = '\\'; off++; break;
|
||||
case 'a': s[i] = '\a'; off++; break;
|
||||
case 'b': s[i] = '\b'; off++; break;
|
||||
case 'f': s[i] = '\f'; off++; break;
|
||||
case 'n': s[i] = '\n'; off++; break;
|
||||
case 'r': s[i] = '\r'; off++; break;
|
||||
case 't': s[i] = '\t'; off++; break;
|
||||
case 'v': s[i] = '\v'; off++; break;
|
||||
case '\0':
|
||||
eprintf("%s: null escape sequence\n", argv0);
|
||||
default:
|
||||
/* "\O[OO]" octal escape */
|
||||
for (m = i + 1; m < i + 1 + 3 && m < len; m++)
|
||||
if (s[m] < '0' || s[m] > '7')
|
||||
break;
|
||||
if (m == i + 1)
|
||||
eprintf("%s: invalid escape sequence '\\%c'\n", argv0, s[i + 1]);
|
||||
off += m - i - 1;
|
||||
for (--m, q = 0, factor = 1; m > i; m--) {
|
||||
q += (s[m] - '0') * factor;
|
||||
factor *= 8;
|
||||
}
|
||||
s[i] = q;
|
||||
}
|
||||
|
||||
for (m = i + 1; m <= len - off; m++)
|
||||
s[m] = s[m + off];
|
||||
len -= off;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
38
paste.c
38
paste.c
|
@ -13,42 +13,6 @@ struct fdescr {
|
|||
const char *name;
|
||||
};
|
||||
|
||||
static size_t
|
||||
resolveescapes(char *s)
|
||||
{
|
||||
size_t len, i, off, m;
|
||||
|
||||
len = strlen(s);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (s[i] != '\\')
|
||||
continue;
|
||||
off = 0;
|
||||
|
||||
switch (s[i + 1]) {
|
||||
case '\\': s[i] = '\\'; off++; break;
|
||||
case 'a': s[i] = '\a'; off++; break;
|
||||
case 'b': s[i] = '\b'; off++; break;
|
||||
case 'f': s[i] = '\f'; off++; break;
|
||||
case 'n': s[i] = '\n'; off++; break;
|
||||
case 'r': s[i] = '\r'; off++; break;
|
||||
case 't': s[i] = '\t'; off++; break;
|
||||
case 'v': s[i] = '\v'; off++; break;
|
||||
case '\0':
|
||||
eprintf("paste: null escape sequence in delimiter\n");
|
||||
default:
|
||||
eprintf("paste: invalid escape sequence '\\%c' in "
|
||||
"delimiter\n", s[i + 1]);
|
||||
}
|
||||
|
||||
for (m = i + 1; m <= len - off; m++)
|
||||
s[m] = s[m + off];
|
||||
len -= off;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static void
|
||||
sequential(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen)
|
||||
{
|
||||
|
@ -145,7 +109,7 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
|
||||
/* populate delimiters */
|
||||
resolveescapes(adelim);
|
||||
unescape(adelim);
|
||||
len = chartorunearr(adelim, &delim);
|
||||
|
||||
/* populate file list */
|
||||
|
|
46
tr.c
46
tr.c
|
@ -70,50 +70,6 @@ rstrmatch(Rune *r, char *s, size_t n)
|
|||
return 1;
|
||||
}
|
||||
|
||||
static size_t
|
||||
resolveescapes(Rune *r, size_t len)
|
||||
{
|
||||
size_t i, off, m, factor, q;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (r[i] != '\\')
|
||||
continue;
|
||||
off = 0;
|
||||
|
||||
switch (r[i + 1]) {
|
||||
case '\\': r[i] = '\\'; off++; break;
|
||||
case 'a': r[i] = '\a'; off++; break;
|
||||
case 'b': r[i] = '\b'; off++; break;
|
||||
case 'f': r[i] = '\f'; off++; break;
|
||||
case 'n': r[i] = '\n'; off++; break;
|
||||
case 'r': r[i] = '\r'; off++; break;
|
||||
case 't': r[i] = '\t'; off++; break;
|
||||
case 'v': r[i] = '\v'; off++; break;
|
||||
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)
|
||||
eprintf("tr: invalid escape sequence '\\%c'\n", r[i + 1]);
|
||||
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++)
|
||||
r[m] = r[m + off];
|
||||
len -= off;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static size_t
|
||||
makeset(char *str, struct range **set, int (**check)(wint_t))
|
||||
{
|
||||
|
@ -123,8 +79,8 @@ makeset(char *str, struct range **set, int (**check)(wint_t))
|
|||
int factor, base;
|
||||
|
||||
/* rstr defines at most len ranges */
|
||||
unescape(str);
|
||||
len = chartorunearr(str, &rstr);
|
||||
len = resolveescapes(rstr, len);
|
||||
*set = emalloc(len * sizeof(**set));
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user