3 # xxdi.pl - perl implementation of 'xxd -i' mode
5 # Copyright 2013 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
6 # Copyright 2013 Linux Foundation
8 # Released under the GPLv2.
10 # Implements the "basic" functionality of 'xxd -i' in perl to keep build
11 # systems from having to build/install/rely on vim-core, which not all
12 # distros want to do. But everyone has perl, so use it instead.
25 open($fh, '<:raw', $ARGV[0]) || die("Unable to open $ARGV[0]: $!\n");
30 $indata = readline $fh;
35 my $len_data = length($indata);
36 my $num_digits_per_line = 12;
40 # Use the variable name of the file we read from, converting '/' and '.
41 # to '_', or, if this is stdin, just use "stdin" as the name.
44 $var_name =~ s/\//_
/g
;
45 $var_name =~ s/\./_/g;
50 $outdata .= "unsigned char $var_name\[] = {";
52 # trailing ',' is acceptable, so instead of duplicating the logic for
53 # just the last character, live with the extra ','.
54 for (my $key= 0; $key < $len_data; $key++) {
55 if ($key % $num_digits_per_line == 0) {
58 $outdata .= sprintf("0x%.2x, ", ord(substr($indata, $key, 1)));
61 $outdata .= "\n};\nunsigned int $var_name\_len = $len_data;\n";
64 print {*STDOUT
} $outdata;