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.
19 my $var_name = "stdin";
20 my $full_output = (@ARGV > 0 && $ARGV[0] eq '-i') ?
shift @ARGV : undef;
28 open($fh, '<:raw', $var_name) || die("xxdi.pl: Unable to open $var_name: $!\n");
29 } elsif (! -t STDIN
) {
33 die "usage: xxdi.pl [-i] [infile]\n";
36 $indata = readline $fh;
41 my $len_data = length($indata);
42 my $num_digits_per_line = 12;
45 # Use the variable name of the file we read from, converting '/' and '.
46 # to '_', or, if this is stdin, just use "stdin" as the name.
47 $var_name =~ s/\//_
/g
;
48 $var_name =~ s/\./_/g;
49 $var_name = "__$var_name" if $var_name =~ /^\d/;
51 $outdata = "unsigned char $var_name\[] = { " if $full_output;
53 for (my $key= 0; $key < $len_data; $key++) {
54 if ($key % $num_digits_per_line == 0) {
55 $outdata = substr($outdata, 0, -1)."\n ";
57 $outdata .= sprintf("0x%.2x, ", ord(substr($indata, $key, 1)));
60 $outdata = substr($outdata, 0, -2);
63 $outdata .= "};\nunsigned int $var_name\_len = $len_data;\n" if $full_output;