summaryrefslogtreecommitdiffstats
path: root/scripts/xxdi.pl
diff options
context:
space:
mode:
authorPetr Štetiar <ynezz@true.cz>2022-08-30 08:34:26 +0200
committerPetr Štetiar <ynezz@true.cz>2022-09-06 08:04:53 +0200
commit06e01e817ec6643a35beb9e6946689e9cc7d020a (patch)
tree7efd2931896e0428209a2830be31dc045381b133 /scripts/xxdi.pl
parent8b278a76d90e3724815a5fde32be59f7796be1d8 (diff)
downloadopenwrt-06e01e817ec6643a35beb9e6946689e9cc7d020a.tar.gz
openwrt-06e01e817ec6643a35beb9e6946689e9cc7d020a.tar.bz2
openwrt-06e01e817ec6643a35beb9e6946689e9cc7d020a.zip
scripts: xxdi.pl: add xxd -i compat mode
So it can serve as a standalone drop in replacement for xxd utility used currently mostly in U-Boot packages with `xxd -i` mode which outputs C include file style, with aim for byte to byte identical output, so the eventual difference in the generated output is easily spottable. Fixes: #10555 Signed-off-by: Petr Štetiar <ynezz@true.cz> Signed-off-by: Jo-Philipp Wich <jo@mein.io> [perl-fu]
Diffstat (limited to 'scripts/xxdi.pl')
-rwxr-xr-xscripts/xxdi.pl37
1 files changed, 19 insertions, 18 deletions
diff --git a/scripts/xxdi.pl b/scripts/xxdi.pl
index 1f960902be..f7bb3c2f9c 100755
--- a/scripts/xxdi.pl
+++ b/scripts/xxdi.pl
@@ -16,15 +16,21 @@ use strict;
use warnings;
my $indata;
+my $var_name = "stdin";
+my $full_output = (@ARGV > 0 && $ARGV[0] eq '-i') ? shift @ARGV : undef;
{
local $/;
my $fh;
if (@ARGV) {
- open($fh, '<:raw', $ARGV[0]) || die("Unable to open $ARGV[0]: $!\n");
- } else {
+ $var_name = $ARGV[0];
+ open($fh, '<:raw', $var_name) || die("xxdi.pl: Unable to open $var_name: $!\n");
+ } elsif (! -t STDIN) {
$fh = \*STDIN;
+ undef $full_output;
+ } else {
+ die "usage: xxdi.pl [-i] [infile]\n";
}
$indata = readline $fh;
@@ -34,32 +40,27 @@ my $indata;
my $len_data = length($indata);
my $num_digits_per_line = 12;
-my $var_name;
-my $outdata;
+my $outdata = "";
# Use the variable name of the file we read from, converting '/' and '.
# to '_', or, if this is stdin, just use "stdin" as the name.
-if (@ARGV) {
- $var_name = $ARGV[0];
- $var_name =~ s/\//_/g;
- $var_name =~ s/\./_/g;
-} else {
- $var_name = "stdin";
-}
+$var_name =~ s/\//_/g;
+$var_name =~ s/\./_/g;
+$var_name = "__$var_name" if $var_name =~ /^\d/;
-$outdata .= "unsigned char $var_name\[] = {";
+$outdata = "unsigned char $var_name\[] = { " if $full_output;
-# trailing ',' is acceptable, so instead of duplicating the logic for
-# just the last character, live with the extra ','.
for (my $key= 0; $key < $len_data; $key++) {
if ($key % $num_digits_per_line == 0) {
- $outdata .= "\n\t";
+ $outdata = substr($outdata, 0, -1)."\n ";
}
$outdata .= sprintf("0x%.2x, ", ord(substr($indata, $key, 1)));
}
-$outdata .= "\n};\nunsigned int $var_name\_len = $len_data;\n";
+$outdata = substr($outdata, 0, -2);
+$outdata .= "\n";
-binmode STDOUT;
-print {*STDOUT} $outdata;
+$outdata .= "};\nunsigned int $var_name\_len = $len_data;\n" if $full_output;
+binmode STDOUT;
+print $outdata;