# HG changeset patch # User Jonathan Clairembault # Date 1353659093 -3600 # Node ID 8c10cf7bace0b77f4b9bbbee4ce21c48623db62a # Parent 3417db95f24bd59b3bbf6cfe10ab8aaaeb51b38f expand: handle backspace. diff -r 3417db95f24b -r 8c10cf7bace0 scripts/test/expand.test --- a/scripts/test/expand.test Fri Nov 23 00:06:28 2012 +0100 +++ b/scripts/test/expand.test Fri Nov 23 09:24:53 2012 +0100 @@ -11,6 +11,7 @@ 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 -r 3417db95f24b -r 8c10cf7bace0 toys/posix/expand.c --- a/toys/posix/expand.c Fri Nov 23 00:06:28 2012 +0100 +++ b/toys/posix/expand.c Fri Nov 23 09:24:53 2012 +0100 @@ -1,7 +1,5 @@ /* expand.c - expands tabs to space * - * FIXME: handle backspace. - * * Copyright 2012 Jonathan Clairembault * * See http://http://pubs.opengroup.org/onlinepubs/9699919799/nframe.html @@ -116,7 +114,10 @@ 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);