commit b8b9d983c84dc75713d56eea31dc352da17b9070
parent d4e2067d3decf44a915df56475a681b70a623b53
Author: FRIGN <dev@frign.de>
Date: Thu, 29 Jan 2015 21:52:44 +0100
Add unescape() to libutil
formerly known as resolveescapes(), it is of central use to numerous
programs.
This drops a lot of LOC.
Diffstat:
M | Makefile | | | 3 | ++- |
M | cut.c | | | 36 | +----------------------------------- |
A | libutil/unescape.c | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | paste.c | | | 38 | +------------------------------------- |
M | tr.c | | | 46 | +--------------------------------------------- |
M | util.h | | | 1 | + |
6 files changed, 56 insertions(+), 118 deletions(-)
diff --git a/Makefile b/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)
diff --git a/cut.c b/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;
diff --git a/libutil/unescape.c b/libutil/unescape.c
@@ -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;
+}
diff --git a/paste.c b/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 */
diff --git a/tr.c b/tr.c
@@ -71,50 +71,6 @@ rstrmatch(Rune *r, char *s, size_t n)
}
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))
{
Rune *rstr;
@@ -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++) {
diff --git a/util.h b/util.h
@@ -56,3 +56,4 @@ char *humansize(double);
mode_t parsemode(const char *, mode_t, mode_t);
void putword(const char *);
void recurse(const char *, void (*)(const char *));
+size_t unescape(char *);