aboutsummaryrefslogtreecommitdiff
path: root/libbb/process_escape_sequence.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/process_escape_sequence.c')
-rw-r--r--libbb/process_escape_sequence.c44
1 files changed, 30 insertions, 14 deletions
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c
index 82cbe10dc..dd6e076b0 100644
--- a/libbb/process_escape_sequence.c
+++ b/libbb/process_escape_sequence.c
@@ -31,14 +31,13 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
unsigned num_digits;
unsigned r;
unsigned n;
- unsigned d;
unsigned base;
num_digits = n = 0;
base = 8;
q = *ptr;
-#ifdef WANT_HEX_ESCAPES
+#if WANT_HEX_ESCAPES
if (*q == 'x') {
++q;
base = 16;
@@ -50,20 +49,21 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
* \02 works, \2 does not (prints \ and 2).
* We treat \2 as a valid octal escape sequence. */
do {
- d = (unsigned char)(*q) - '0';
-#ifdef WANT_HEX_ESCAPES
- if (d >= 10) {
- d = (unsigned char)(_tolower(*q)) - 'a' + 10;
- }
+#if !WANT_HEX_ESCAPES
+ unsigned d = (unsigned char)(*q) - '0';
+#else
+ unsigned d = (unsigned char)_tolower(*q) - '0';
+ if (d >= 10)
+ d += ('0' - 'a' + 10);
#endif
-
if (d >= base) {
-#ifdef WANT_HEX_ESCAPES
- if ((base == 16) && (!--num_digits)) {
-/* return '\\'; */
- --q;
+ if (WANT_HEX_ESCAPES && base == 16) {
+ --num_digits;
+ if (num_digits == 0) {
+ /* \x<bad_char> */
+ --q; /* go back to x */
+ }
}
-#endif
break;
}
@@ -85,7 +85,9 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
}
} while (*++p);
/* p points to found escape char or NUL,
- * advance it and find what it translates to */
+ * advance it and find what it translates to.
+ * Note that unrecognized sequence \z returns '\'
+ * and leaves ptr pointing to z. */
p += sizeof(charmap) / 2;
n = *p;
}
@@ -94,3 +96,17 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
return (char) n;
}
+
+char* FAST_FUNC strcpy_and_process_escape_sequences(char *dst, const char *src)
+{
+ while (1) {
+ char c, c1;
+ c = c1 = *src++;
+ if (c1 == '\\')
+ c1 = bb_process_escape_sequence(&src);
+ *dst = c1;
+ if (c == '\0')
+ return dst;
+ dst++;
+ }
+}