File: zapchain

package info (click to toggle)
adzapper 20080508-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 380 kB
  • ctags: 30
  • sloc: perl: 3,565; sh: 124; makefile: 48
file content (100 lines) | stat: -rwxr-xr-x 1,618 bytes parent folder | download | duplicates (5)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl
#
# Chain multiple redirectors together.
#	- Cameron Simpson <cs@zip.com.au> 
#

use strict vars;

use IO::File;
use IPC::Open2;

die "Usage: $0 redirectors...\n" if ! @ARGV;

## Note that the ZAP_CHAINING variable is obsolete these days.
## - Cameron Simpson <cs@zip.com.au> 17jul2001
##
##$::Chaining = ( length $ENV{ZAP_CHAINING}
##	      ? $ENV{ZAP_CHAINING} eq 'FULL'
##		? 2
##		: 1
##	      : 0
##	      );
$::Chaining = 0;

my @sub=();
my $nsubs=0;

for my $sub (@ARGV)
{ 
  ++$nsubs;

  my $rd = "RD$nsubs";
  my $wr = "WR$nsubs";
  my $pid = open2($rd, $wr, $sub);
  die "$0: can't open2($sub): $!\n" if ! defined $pid;

  autoflush $wr 1;

  push(@sub,[$sub,$pid,$rd,$wr]);
}

autoflush STDOUT 1;

my @words;
my $o_;
my $redir;
my($sub,$pid,$rd,$wr);

while (defined($_=<STDIN>))
{ chomp;

  @words = split;
  $o_ = $_;

  # pass through every redirector
  for my $s (@sub)
  { ($sub,$pid,$rd,$wr)=@$s;

    print $wr $_, "\n";

    $redir=<$rd>;
    die "$0: unexpected EOF from [$sub]" if ! defined $redir;
    chomp($redir);

    if (length($redir))
    # redirected
    { my @nwords=split(/\s+/,$redir);

      if (@nwords == 1)
      # plain URL
      { $words[0]=$nwords[0];
      }
      else
      # full redirector input line
      {
	if (@nwords != 4)
	{ warn "$0: @words -> @nwords";
	}

	@words=@nwords;
      }

      $_="@words";
    }
  }

  if ($::Chaining == 0)
  # pure redirector
  { print STDOUT (($_ eq $o_) ? '' : $words[0]), "\n";
  }
  elsif ($::Chaining == 1)
  # print new URL;
  { print STDOUT $words[0], "\n";
  }
  else
  { print STDOUT $_, "\n";
  }
}

exit 0;