hush in arm nommu environment

Denis Vlasenko vda.linux at googlemail.com
Mon Aug 6 14:07:26 UTC 2007


On Monday 06 August 2007 10:35, Per Hallsmark wrote:
> well... sometimes one can answer oneself... :)
> 
> --- busybox-1.6.1.orig/libbb/lineedit.c 2007-06-30 17:06:40.000000000 +0200
> +++ busybox-1.6.1/libbb/lineedit.c      2007-08-06 11:23:57.000000000 +0200
> @@ -165,10 +165,12 @@
> 
>          if (cmdedit_x >= num) {
>                  cmdedit_x -= num;
> +#if 0
>                  if (num <= 4) {
>                          printf("\b\b\b\b" + (4-num));
>                          return;
>                  }
> +#endif
>                  printf("\033[%uD", num);
>                  return;
>          }
> 
> 
> With above patch backspace works ok.
> 
> The \b\b... printf looks rather mystic... guess the author
> perhaps wanted something like this instead?
> 
> switch (num) {
>    case 4:
>      printf("\b");
>      /* fall trough */
>    case 3:
>      printf("\b");
>      /* fall trough */
>    case 2:
>      printf("\b");
>      /* fall trough */
>    case 1:
>      printf("\b");
>      return;
>    default:
> }
> 
> but I wonder what the benefits are...

printf("\b\b\b\b" + (4-num));

does exactly the same in 4 times less code.
If num==4, it does printf("\b\b\b\b" + 0) => printf("\b\b\b\b")
If num==3, it does printf("\b\b\b\b" + 1) => printf("\b\b\b")
If num==2, it does printf("\b\b\b\b" + 2) => printf("\b\b")
If num==1, it does printf("\b\b\b\b" + 3) => printf("\b")
etc

The benefits are that we send less bytes than with printf("\033[%uD", num);
It's done primarily for the most common case: single [Backspace] key.
It's silly to send ESC [ 1 D instead of single backspace char.

Now, I wonder why \b for you does not do what it should (that is,
going back one char). Can you compile and run this test program?

int main(int argc, char **argv) {
        printf("987654321\b\b\b\b" + argc);
        fflush(0);
        sleep(3);
        puts("");
}

When I run it, I see this:

# ./a.out
87654321   <---- cursor is under '4'
# ./a.out 1
7654321    <---- cursor is under '4'
# ./a.out 1 1
654321     <---- cursor is under '4'
# ./a.out 1 1 | hexdump -vC
00000000  36 35 34 33 32 31 08 08  08 08 0a                 |654321.....|
0000000b

IOW, both printf("str" + n) and '\b' work right for me.

What do you see
--
vda



More information about the busybox mailing list