diff options
author | Petr Štetiar <ynezz@true.cz> | 2022-08-30 08:31:42 +0200 |
---|---|---|
committer | Petr Štetiar <ynezz@true.cz> | 2022-09-16 18:50:46 +0200 |
commit | a352e30b8f23a38f60289d2d091b4b57f0f56d19 (patch) | |
tree | 4034d5527c468bef4b45073666a83a5107c1f57a /scripts | |
parent | b3fa0241e2faaf4080f4903c0547949ebcddbc6b (diff) | |
download | upstream-a352e30b8f23a38f60289d2d091b4b57f0f56d19.tar.gz upstream-a352e30b8f23a38f60289d2d091b4b57f0f56d19.tar.bz2 upstream-a352e30b8f23a38f60289d2d091b4b57f0f56d19.zip |
scripts: add xxdi.pl
xxdi.pl is a Perl script that implements vim's 'xxd -i' mode so that
packages do not have to use all of vim just to get this functionality.
References: #10555
Source: https://github.com/gregkh/xxdi/blob/97a6bd5cee05d1b15851981ec38ef5a460ddfcb1/xxdi.pl
Signed-off-by: Petr Štetiar <ynezz@true.cz>
(cherry picked from commit 2117d04a3aaad3394c0afec799d9c43f8a09c2cf)
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/xxdi.pl | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/scripts/xxdi.pl b/scripts/xxdi.pl new file mode 100755 index 0000000000..acc974c4b3 --- /dev/null +++ b/scripts/xxdi.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +# +# xxdi.pl - perl implementation of 'xxd -i' mode +# +# Copyright 2013 Greg Kroah-Hartman <gregkh@linuxfoundation.org> +# Copyright 2013 Linux Foundation +# +# Released under the GPLv2. +# +# Implements the "basic" functionality of 'xxd -i' in perl to keep build +# systems from having to build/install/rely on vim-core, which not all +# distros want to do. But everyone has perl, so use it instead. +# + +use strict; +use warnings; +use File::Slurp qw(slurp); + +my $indata = slurp(@ARGV ? $ARGV[0] : \*STDIN); +my $len_data = length($indata); +my $num_digits_per_line = 12; +my $var_name; +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"; +} + +$outdata .= "unsigned char $var_name\[] = {"; + +# 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 .= sprintf("0x%.2x, ", ord(substr($indata, $key, 1))); +} + +$outdata .= "\n};\nunsigned int $var_name\_len = $len_data;\n"; + +binmode STDOUT; +print {*STDOUT} $outdata; + |