| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use File::Basename; | ||
| 6 | use Getopt::Long; | ||
| 7 | use Parse::Win32Registry; | ||
| 8 | |||
| 9 | binmode(STDOUT, ':utf8'); | ||
| 10 | |||
| 11 | Getopt::Long::Configure('bundling'); | ||
| 12 | |||
| 13 | GetOptions('values|v' => \my $show_values); | ||
| 14 | |||
| 15 | my $filename = shift or die usage(); | ||
| 16 | my $initial_key_path = shift; | ||
| 17 | |||
| 18 | my $registry = Parse::Win32Registry->new($filename) | ||
| 19 | or die "'$filename' is not a registry file\n"; | ||
| 20 | my $root_key = $registry->get_root_key | ||
| 21 | or die "Could not get root key of '$filename'\n"; | ||
| 22 | |||
| 23 | if (defined($initial_key_path)) { | ||
| 24 | $root_key = $root_key->get_subkey($initial_key_path); | ||
| 25 | if (!defined($root_key)) { | ||
| 26 | die "Could not locate the key '$initial_key_path' in '$filename'\n"; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | traverse($root_key); | ||
| 31 | |||
| 32 | sub traverse { | ||
| 33 | my $key = shift; | ||
| 34 | my @siblings = @_; | ||
| 35 | # @siblings tracks the number of remaining keys at each level of depth | ||
| 36 | # $siblings[0] = count of remaining sibling keys at level 0 | ||
| 37 | # $siblings[1] = count of remaining sibling keys at level 1 | ||
| 38 | # etc. | ||
| 39 | |||
| 40 | if (@siblings) { | ||
| 41 | foreach my $remaining (@siblings[0..$#siblings-1]) { | ||
| 42 | print $remaining > 0 ? "| " : " "; | ||
| 43 | } | ||
| 44 | print $siblings[-1] > 0 ? "+-" : "`-"; | ||
| 45 | } | ||
| 46 | |||
| 47 | print $key->get_name; | ||
| 48 | if (defined($key->get_timestamp)) { | ||
| 49 | print " [", $key->get_timestamp_as_string, "]" | ||
| 50 | } | ||
| 51 | print "\n"; | ||
| 52 | |||
| 53 | # initialize the count of remaining sibling keys for this depth | ||
| 54 | push @siblings, scalar $key->get_list_of_subkeys; | ||
| 55 | |||
| 56 | if ($show_values) { | ||
| 57 | foreach my $value ($key->get_list_of_values) { | ||
| 58 | foreach my $remaining (@siblings) { | ||
| 59 | print $remaining > 0 ? "| " : " "; | ||
| 60 | } | ||
| 61 | print $value->as_string, "\n"; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | foreach my $subkey ($key->get_list_of_subkeys) { | ||
| 66 | $siblings[-1]--; | ||
| 67 | traverse($subkey, @siblings); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub usage { | ||
| 72 | my $script_name = basename $0; | ||
| 73 | return <<USAGE; | ||
| 74 | $script_name for Parse::Win32Registry $Parse::Win32Registry::VERSION | ||
| 75 | |||
| 76 | Displays the keys and values of a registry file as an indented tree. | ||
| 77 | |||
| 78 | $script_name <filename> [subkey] [-v] | ||
| 79 | -v or --values display values | ||
| 80 | USAGE | ||
| 81 | } |