home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!news.cerf.net!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Subject: Re: What do *you* use to format mail?
- Message-ID: <1992Dec17.235350.24904@netlabs.com>
- Sender: news@netlabs.com
- Nntp-Posting-Host: scalpel.netlabs.com
- Organization: NetLabs, Inc.
- References: <1992Dec13.091156.19462@nwnexus.WA.COM> <nrp.724506949@reading> <1992Dec16.170902.21889@nwnexus.WA.COM>
- Date: Thu, 17 Dec 1992 23:53:50 GMT
- Lines: 68
-
- In article <1992Dec16.170902.21889@nwnexus.WA.COM> osiris@halcyon.halcyon.com (David Ruggiero) writes:
- : If fmt(1) could do all that I wanted, I wouldn't have posted (see the original
- : for a statement of the problem). Besides, on my system [Ultrix], fmt(1) is so
- : brain dead that it doesn't even accept a line-length option, so it's useless
- : for the task I have laid out. What I want to do, I expect, takes a short
- : perl or awk script, and I figure *someone* out there must have one.
-
- #!/usr/bin/perl
-
- # Usage: reform [-lNUM] [-rNUM] [-iNUM] [-sNUM] [files]
-
- # Set default values for left margin, right margin, indent
- # and paragraph spacing.
-
- $l = 0;
- $r = 0;
- $i = 0;
- $s = 1;
-
- # Process any switches.
-
- while ($ARGV[0] =~ /^-/) {
- $_ = shift;
- /^-(l|r|i|s)(\d+)/ && (eval "\$$1 = \$2", next);
- die "Unrecognized switch: $_\n";
- }
-
- # Calculate format strings.
-
- $r = $l + 65 unless $r;
- $r -= $l; # make $r relative to $l
- die "Margins too close\n" if $l + $i >= $r;
-
- $LEFT = ' ' x $l;
- $INDENT = ' ' x $i;
- $RIGHT1 = '^' . '<' x ($r - 1 - $i);
- $RIGHT2 = '^' . '<' x ($r - 1);
- $SPACING = "\n" x $s;
-
- # Define a format at run time.
-
- $form = <<"End of Format Definition";
- format STDOUT =
- $LEFT$INDENT$RIGHT1
- \$_
- $LEFT$RIGHT2~~
- \$_
- $SPACING.
- End of Format Definition
-
- print $form if $debugging;
- eval $form;
-
- # Set paragraph mode on input.
-
- $/ = '';
-
- # For each paragraph...
-
- while (<>) {
- s/\s+/ /g; # Canonicalize white space.
- s/ $//; # Trim final space.
- s/([Ia-z0-9][)'"]*[.!?][)'"]*) /$1 /g; # Fix sentence ends.
- write; # Spit out new paragraph.
- }
-
- Larry Wall
- lwall@netlabs.com
-