aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2020-04-13 19:45:09 -0500
committerRob Landley <rob@landley.net>2020-04-13 19:45:09 -0500
commit71ae0e1617218820f405bbf79d5d5dc89d5772ee (patch)
treeb21f46c1a3258b4d5564748374d4291902406e9b /lib
parent64aa0f9c06c4215f64f6385d0afe38f02effd888 (diff)
downloadtoybox-71ae0e1617218820f405bbf79d5d5dc89d5772ee.tar.gz
Add unescape2(), migrate some unescape() users over.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c23
-rw-r--r--lib/lib.h1
2 files changed, 24 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 364ce07e..4fa358c0 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -436,6 +436,29 @@ int unescape(char c)
return (idx == -1) ? 0 : to[idx];
}
+// parse next character advancing pointer. echo requires leading 0 in octal esc
+int unescape2(char **c, int echo)
+{
+ int idx = *((*c)++), i, off;
+
+ if (idx != '\\' || !**c) return idx;
+ if (**c == 'c') return 31&*(++*c);
+ for (i = 0; i<4; i++) {
+ if (sscanf(*c, (char *[]){"0%3o%n"+!echo, "x%2x%n", "u%4x%n", "U%6x%n"}[i],
+ &idx, &off))
+ {
+ *c += off;
+
+ return idx;
+ }
+ }
+
+ if (-1 == (idx = stridx("\\abeEfnrtv'\"?", **c))) return '\\';
+ ++*c;
+
+ return "\\\a\b\033\033\f\n\r\t\v'\"?"[idx];
+}
+
// If string ends with suffix return pointer to start of suffix in string,
// else NULL
char *strend(char *str, char *suffix)
diff --git a/lib/lib.h b/lib/lib.h
index 3200dc32..60c800ca 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -234,6 +234,7 @@ char *strlower(char *s);
char *strafter(char *haystack, char *needle);
char *chomp(char *s);
int unescape(char c);
+int unescape2(char **c, int echo);
char *strend(char *str, char *suffix);
int strstart(char **a, char *b);
int strcasestart(char **a, char *b);