summaryrefslogtreecommitdiffstats
path: root/lib/parse-patch
diff options
context:
space:
mode:
Diffstat (limited to 'lib/parse-patch')
-rwxr-xr-xlib/parse-patch105
1 files changed, 105 insertions, 0 deletions
diff --git a/lib/parse-patch b/lib/parse-patch
new file mode 100755
index 0000000..b4dded2
--- /dev/null
+++ b/lib/parse-patch
@@ -0,0 +1,105 @@
+#!/usr/bin/perl -w
+
+# Extract or update a section from a combined patch + documentation +
+# meta information file.
+
+use FileHandle;
+use Getopt::Long;
+use strict;
+
+my $select;
+my $update;
+
+if (!GetOptions("s|select=s" => \$select,
+ "u|update=s" => \$update) ||
+ (!defined $select && !defined $update)) {
+ print STDERR "USAGE: $0 {-s|-u} section file [< replacement]\n";
+ exit 1;
+}
+
+foreach my $arg (@ARGV) {
+ my $fh;
+
+ if ($arg =~ /\.gz$/) {
+ $fh = new FileHandle("gzip -cd $arg |");
+ } elsif ($arg =~ /\.bz2$/) {
+ $fh = new FileHandle("bzip2 -cd $arg |");
+ } else {
+ $fh = new FileHandle("< $arg");
+ }
+
+ unless ($fh) {
+ print STDERR "$arg: $!\n";
+ next;
+ }
+
+ if (defined $select) {
+ my $section = "head";
+ my $newline = "";
+ while (<$fh>) {
+ if (/^%(.*)/) {
+ last if $section eq $select;
+ $section = $1;
+ next;
+ }
+ if ($section eq $select) {
+ print $newline;
+ if ($_ eq "\n") {
+ $newline = $_;
+ } else {
+ $newline="";
+ print;
+ }
+ }
+ }
+ } elsif (defined $update) {
+ my $fh2;
+ if ($arg =~ /\.gz$/) {
+ $fh2 = new FileHandle("| gzip -c > $arg.parse");
+ } elsif ($arg =~ /\.bz2$/) {
+ $fh2 = new FileHandle("| bzip2 -c > $arg.parse");
+ } else {
+ $fh2 = new FileHandle("$arg.parse", O_CREAT|O_WRONLY);
+ }
+ unless ($fh2) {
+ die "$arg.parse: $!\n";
+ }
+
+ # Copy things before updated section
+ while (<$fh>) {
+ if (/^%(.*)/ && $1 eq $update) {
+ last;
+ }
+ print $fh2 $_;
+ }
+ # Create/replace updated section
+ print $fh2 "%$update\n";
+ while (<STDIN>) {
+ print $fh2 $_;
+ }
+ print $fh2 "\n";
+ # Skip obsolete section
+ while (<$fh>) {
+ if (/^%(.*)/) {
+ print $fh2 $_;
+ last;
+ }
+ }
+ # Copy things after updated section
+ while (<$fh>) {
+ print $fh2 $_;
+ }
+ unless (close $fh2) {
+ die "$arg.patch: $!\n";
+ }
+
+ unlink "$arg~";
+ unless (rename $arg, "$arg~") {
+ die "Failed to rename $arg to $arg~: $!\n";
+ }
+ unless (rename "$arg.parse", $arg) {
+ die "Failed to rename $arg.parse to $arg: $!\n";
+ }
+ }
+ close $fh;
+}