#!/bin/sh -- # A comment mentioning perl, indented for bash's sake. eval 'exec perl -S $0 ${1+"$@"}' if 0; chop($Program = `basename $0`); $join = 0; $trim = 0; $Usage = <<"END"; $Program: XXX Usage: $Program Options: Notes: END LOOP: while (@ARGV) { $_ = shift; CASE: { /^-j$/ && ($join = 1, last CASE); /^(-t|-trim)$/ && ($trim = 1, last CASE); /^-d$/ && ($Debug = 1, last CASE); /^--$/ && (last LOOP); # -- means end of switches /^-(-.*)$/ && (unshift(@ARGV, $1), last CASE); /^(-h|-help)$/ && ((print STDERR $Usage), exit 0, last CASE); if ( /^-(..+)$/ ) { # split bundled switches: local($y, $x) = ($1, ''); foreach $x (reverse(split(//, $y))) { unshift(@ARGV,"-$x") }; last CASE; } /^-/ && ((print STDERR "Invalid arg: $_\n$Usage"), exit 1, last CASE); unshift(@ARGV,$_); last LOOP; } } $cnt = 0; while(<>) { chop; push(@Line, $_); $beg = $_; $beg =~ s/\S.*$//; push(@Beg, length($beg) + 1); $end = $_; $end =~ s/\s*$//; push(@End, length($end) - 1); } $last_beg = -1; $last_end = -1; $| = 1; for ($i=0; $i < @Line; $i++) { $this_beg = $Beg[$i]; $this_end = $End[$i]; $this_line = $Line[$i]; if ( $this_line =~ /^\s*$/ ) { print "\n"; next; } $overlap = 0; if ( $last_beg <= $this_beg && $this_beg <= $last_end ) { $overlap = 1; } if ( $last_beg <= $this_end && $this_end <= $last_end ) { $overlap = 1; } if ( $overlap || $this_beg < $last_beg ) { if ( $join && $overlap ) { $tmp = $Line[$i]; $tmp =~ s/^\s*/ /; $tmp =~ s/^\s*// if $trim; print $tmp; } else { print "\n"; $tmp = $Line[$i]; $tmp =~ s/^\s*// if $trim; print $tmp; } } else { $tmp = substr($Line[$i], $last_end + 1); $tmp =~ s/^\s*// if $trim; print $tmp; } $last_beg = $this_beg; $last_end = $this_end; } print "\n"; exit 0; sub trim { local($x) = @_; $x =~ s/^\s*//; $x =~ s/\s*$//; return $x; }