Looking at xxd, I noticed that the -b[inary] flag wasn't there,
Having some facility to print binary is nice, but since printf
doesn't have anything in built for it implementing it isn't hard,
but it looks _ugly_. Which is why I decided not to do that in this
patch.

xxd also runs on average about 5 times slower than vim xxd, this is
because of read reading 16 bytes at a time, also not hard to fix, but
very hard to fix cleanly.

xxd has a -d flag to do decimal address lables that I added in this patch.

Toybox xxd intentionally caps itself at 1 file per invocation, "Why?": Vim xxd 
is
a bloated pile of garbage with things like EBCDIC support. They added a 
"improvement" where if you gave 2 files to it, it would rewrite the contents
of the second with the output of the first because it was made for operating
systems where file redirection wasn't a built in thing. As someone who's lost
data because of this, I'm glad there isn't support for this in toybox xxd.

But even though we do loopfiles() over the arguments, There isn't any actual way
to take advantage of that code since we cap it at one file. Removing the cap 
doesn't
break anything that wouldn't already be broken, so I removed the ">1" in the 
option
string in this patch.

-   Oliver Webb <[email protected]>

P.S. As someone who once did a cleanup pass on a vim xxd fork a while back,
a reason to not do colorized output is that you are outputting ANSI codes
for _every byte, twice_. And unless you have code to optimize that down, your
files can take 3/4 times longer to print out via colorized xxd since every
line goes from 68 bytes to 420:

Colored xxd: 
0001d590: 306d 201b 5b31 3b33 376d 3030 1b5b 306d  0m .[1;37m00.[0m
0001d5a0: 1b5b 313b 3337 6d30 301b 5b30 6d20 1b5b  .[1;37m00.[0m .[
0001d5b0: 313b 3337 6d30 301b 5b30 6d1b 5b31 3b33  1;37m00.[0m.[1;3
Normal xxd:
000159a0: 3030 2030 3030 3020 3030 3030 2030 3030  00 0000 0000 000
000159b0: 3020 3030 3030 2030 3030 3020 3030 3030  0 0000 0000 0000
000159c0: 2030 3030 3020 202e 2e2e 2e2e 2e2e 2e2e   0000  .........
From cd0c223e33cee1762a61fcc4356273ed467e0816 Mon Sep 17 00:00:00 2001
From: Oliver Webb <[email protected]>
Date: Sat, 20 Apr 2024 21:10:29 -0500
Subject: [PATCH] xxd -d Decimal Address Label, Don't cap at one file (Writing
 to the second file is what "normal" xxd does, but we use loopfiles anyways
 so...)

---
 toys/other/xxd.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/toys/other/xxd.c b/toys/other/xxd.c
index 2afa5298..828024a2 100644
--- a/toys/other/xxd.c
+++ b/toys/other/xxd.c
@@ -10,18 +10,19 @@
  * xxd -p "plain" output:
  *   "4c696e75782076657273696f6e20342e392e302d342d616d643634202864"
 
-USE_XXD(NEWTOY(xxd, ">1c#<0>256l#o#g#<0=2eiprs#[!rs][!re]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_XXD(NEWTOY(xxd, "c#<0>256l#o#g#<0=2ediprs#[!rs][!re]", TOYFLAG_USR|TOYFLAG_BIN))
 
 config XXD
   bool "xxd"
   default y
   help
-    usage: xxd [-eipr] [-cglos N] [file]
+    usage: xxd [-edipr] [-cglos N] [file...]
 
     Hexdump a file to stdout. If no file is listed, copy from stdin.
     Filename "-" is a synonym for stdin.
 
     -c N	Show N bytes per line (default 16)
+    -d    Decimal adress lable (instead of hex)
     -e	Little-endian
     -g N	Group bytes by adding a ' ' every N bytes (default 2)
     -i	Output include file (CSV hex bytes, plus C header/footer if not stdin)
@@ -52,7 +53,7 @@ static void do_xxd(int fd, char *name)
   }
 
   while (0<(len = readall(fd, toybuf, (limit && limit-pos<c) ? limit-pos : c))){
-    if (!FLAG(p)) printf("%08llx: ", TT.o + pos);
+    if (!FLAG(p)) printf(!FLAG(d) ? "%08llx: " : "08ll: ", TT.o + pos);
     pos += len;
     space = 2*TT.c;
     space += TT.g ? (TT.c+TT.g-1)/TT.g+1 : 2;
-- 
2.44.0

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to