aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scripts/test/expand.test1
-rw-r--r--toys/posix/expand.c7
2 files changed, 5 insertions, 3 deletions
diff --git a/scripts/test/expand.test b/scripts/test/expand.test
index 73374562..bb0ca083 100644
--- a/scripts/test/expand.test
+++ b/scripts/test/expand.test
@@ -11,6 +11,7 @@ testing "expand default" "expand input" " foo bar\n" "\tfoo\tbar\n" "
testing "expand default stdin" "expand" " foo bar\n" "" "\tfoo\tbar\n"
testing "expand single" "expand -t 2 input" " foo bar\n" "\tfoo\tbar\n" ""
testing "expand tablist" "expand -t 5,10,12 input" " foo bar foo\n" "\tfoo\tbar\tfoo\n" ""
+testing "expand backspace" "expand input" "foobarf bar\n" "foobarfoo\b\b\tbar\n" ""
# advanced tests
diff --git a/toys/posix/expand.c b/toys/posix/expand.c
index 739c326d..bbe6fdf1 100644
--- a/toys/posix/expand.c
+++ b/toys/posix/expand.c
@@ -1,7 +1,5 @@
/* expand.c - expands tabs to space
*
- * FIXME: handle backspace.
- *
* Copyright 2012 Jonathan Clairembault <jonathan at clairembault dot fr>
*
* See http://http://pubs.opengroup.org/onlinepubs/9699919799/nframe.html
@@ -116,7 +114,10 @@ static void expand_file(int fd, char *name)
wrlinei += count;
} else { /* copy input to output */
wrbuf[wrbufi++] = rdbuf[rdbufi];
- wrlinei += 1;
+ if (rdbuf[rdbufi] == '\b') /* go back one column on backspace */
+ wrlinei -= !!wrlinei; /* do not go below zero */
+ else
+ wrlinei += 1;
/* flush expand buffer and reset tablist at newline */
if (rdbuf[rdbufi] == '\n') {
writeall(STDOUT_FILENO, wrbuf, wrbufi);