[Toybox] [PATCH] POSIX's unexpand command

Oliver Webb aquahobbyist at proton.me
Fri Feb 23 18:27:07 PST 2024


Browsing through list archives from 2020, I found a mention of the unexpand command (in POSIX)

>From http://lists.landley.net/pipermail/toybox-landley.net/2020-May/019792.html:

unexpand "converts spaces to tabs". Haven't gotten around to it yet. :)

This commands behavior is so simple (s/  /\t/g) that it can be knocked out in a couple hours,
The below patch is the command in 60 lines of code, and some tests for it.

Since the command only looks for 2 characters (' ' and '\t'), no UTF safety checking is required,
unexpand doesn't parse backspaces either.

The only problem is...

The GNU man page doesn't say if spaces are supposed to be processed beyond the beginning of lines.
Since it specifies -a (Spaces -> tabs after the start of lines), and puts the option to disable that
behavior under "--first-only" (while noting that it OVERRIDES -a). You would THINK it'd not process
spaces like that, and the "--first-only" option serves the same purpose as grep -G (None at all, but might
make option parsing a bit simpler for scripts).

POSIX also doesn't make this apparent while also specifying -a, but in a much more verbose way because POSIX

--first-only behavior is nice (the only reason I can find that would make this 
command more useful than sed 's/  /\t/g'), but long options are cumbersome.

FAIL: unexpand -a behavior default
echo -ne '  123  123\n' | "/sbin/unexpand" -t 2
[...]
-       123      123
+       123       123

Now it's converting spaces to tabs, while leaving trailing spaces?

The man page does not document that:
       -a, --all
              convert all blanks, instead of just initial blanks

       --first-only
              convert only leading sequences of blanks (overrides -a)

       -t, --tabs=N
              have tabs N characters apart instead of 8 (enables -a)

Also... why does -t enable -a, that makes doing testing on a terminal where tabs are displayed
as 8 spaces a lot harder (have to switch on -t to make sure anything is being CONVERTED,
but if you do that it will switch on another, completely unrelated option)...

-   Oliver Webb <aquahobbyist at proton.me>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-A-unexpand-implementation.patch
Type: text/x-patch
Size: 2551 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20240224/4606f536/attachment.bin>


More information about the Toybox mailing list