File: extract

package info (click to toggle)
less 668-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,988 kB
  • sloc: ansic: 24,976; perl: 884; sh: 399; makefile: 123; python: 33
file content (78 lines) | stat: -rwxr-xr-x 2,096 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env perl
use strict;
use Getopt::Std;

# Extract all text files from an lt file.
my $usage = <<_EOF_;
usage: extract [-ckfv] [lt-file]...
       -c = print text file to stdout
       -f = overwrite existing text file
       -k = print keystrokes to stdout
       -v = verbose
_EOF_

my %opt;

exit(main() ? 0 : 1);
sub main() {
    die $usage if not getopts('cfkv', \%opt);
    foreach my $in_filename (@ARGV) {
        my $in;
        if (not open $in, '<', $in_filename) {
            print STDERR "ERROR: cannot open $in_filename: $!\n";
            return 0;
        }
        return 0 if not read_ltfile($in, $in_filename);
        close $in;
    }
    return 1;
}

sub read_ltfile {
    my ($in, $in_filename) = @_;
    my $is_lt_file = 0;
    while (<$in>) {
        if (/^!lesstest!/) {
            $is_lt_file = 1;
        } else {
            if (not $is_lt_file) {
                print STDERR "WARNING: skipping $in_filename: not an lt file\n";
                return 0;
            }
            if (/^F\s*"([^"]+)"\s*(\d+)/) {
                return 0 if not create_text_file($1, $2, $in);
            } elsif (/^\+([0-9a-fA-F]+)/) {
                print "$1\n" if $opt{k};
            }
        }
    }
    return 1;
}

sub create_text_file {
    my ($out_filename, $filelen, $in) = @_;
    my $out;
    if ($opt{k}) {
        $out = undef;
    } elsif ($opt{c}) {
        $out = \*STDOUT;
    } else {
        if (-f $out_filename and not $opt{f}) {
            print STDERR "WARNING: skipping existing file $out_filename (use -f to overwrite)\n";
            return 0;
        }
        if (not open($out, '>', $out_filename)) {
            print STDERR "ERROR: cannot create $out_filename: $!\n";
            return 0;
        }
    }
    my $buf;
    my $nread = read $in, $buf, $filelen;
    print STDERR "$out_filename: read $nread/$filelen\n" if $opt{v};
    print STDERR "WARNING: read only $nread of $filelen bytes\n" if not $nread or $nread != $filelen;
    if (not $opt{k}) {
        print $out $buf;
        close $out unless $opt{c};
    }
    return 1;
}