#!/bin/sh # # This is a shell archive. To extract its contents, # execute this file with /bin/sh to create the file(s): # # ww # # This shell archive created: Tue Oct 12 16:24:42 EDT 1999 # echo "Extracting file ww" sed -e 's/^X//' <<\SHAR_EOF > ww X#!/bin/sh X# X# ww (wordwrap) Wrap long lines without splitting words X# X# X# Usage: ww [-abhmstH] [-c chars] [-f chars] [-i n] [-l length] [files] X# X# X# Options: X# X# -a output all lines, even usually non-printing ones X# -b break each output line at the end of each input line X# -c treat lines beginning with flag characters "chars" as comment lines X# -f don't wrap lines beginning with flag characters "chars" X# -h don't wrap mail or news header lines X# -i indent first line of each output paragraph by n spaces X# -l specifies maximum output line length (default = 70) X# -m process as mail/news message/article (==> -h -f'>}:') X# -s process file in sections marked with "-w" and "+w" X# -t input lines indented with tabs or blanks also start new paragraphs X# -H add rudimentary HTML formatting to output X# X# X# Notes: X# X# -a The (-a) option causes WW to output all lines from the input file, X# even those that would normally not be included such as "-w", "+w", X# and comment lines. This allows a source file to be re-wrapped X# while retaining its formatting lines so that the resulting file X# can continue being used as a source file. X# X# -h Mail/News header lines are all non-blank lines before the first X# blank line. X# X# -c Multiple characters may be specified with (-c). Only the first X# character position of each input line is inspected for the X# presence of one of these comment flag characters. X# X# -f Multiple characters may be specified with (-f). Only the first X# character position of each input line is inspected for the X# presence of one of these no-wrap flag characters. X# X# -m In addition to implying the arguments "-h -f'>}:'", (-m) will X# also cause WW to attempt not to wrap lines in a signature X# block at the end of the message. The convention for indicating X# such a block is that it is preceded by a line consisting of two X# dashes and a blank ("-- "). Unfortunately, this convention is X# followed much less than it once was, and there is no other X# widely used consistent way of flagging signature blocks. X# X# -l If a maximum output line length of zero (0) is explicitly X# specified, WW will use one less than the current terminal or X# window width as reported by "/usr/5bin/tput cols". X# X# -s This option will cause WW to recognize lines consisting of "-w" X# and "+w" as turning wrapping off and on respectively. "-W" and X# "+W" are also allowed. These strings must be the only characters X# on the flagging lines, which are not printed themselves. Wrapping X# is initially turned on; i. e., an implied "+w" precedes the first X# line of the file. X# X# -t Using (-t) will cause WW to recognize any line starting with X# tabs or blanks (whitespace) as starting a new paragraph. Blank X# lines always mark the start of new paragraphs, regardless of X# whether (-t) is used. X# X# All paragraphs in the output are set off by at least one blank X# line. If a new paragraph is started due to beginning-of-line X# whitespace and the immediately previous output line was not blank, X# WW will print a blank line before starting the new paragraph. X# X# -i If (-i) is not used, all paragraphs will be started flush left, X# regardless of whether they are started due to blank lines or X# whitespace. If an indentation is specified, the first line of each X# paragraph will be indented using the requested number of blanks. X# X# -H If (-H) is specified, WW will add basic HTML formatting tags to X# the output: X# X# o and will be added to the front of the file, X# and and will be added to the end. A comment X# tag will also be included at the front that lists the WW X# options that were used to create the file. X# X# o If wrap/no-wrap sections are used (-s), a
 tag will be
X#               output when wrapping is turned off with "-w", and a 
X# tag will be output when wrapping is turned on with "+w". X# X# o If (-f) is used to cause lines flagged with certain characters X# to not be wrapped, a
tag will be appended to each such X# line. X# X# o If a mail/news header is being recognized (-h, -m), it will X# be enclosed in
 tags.  Likewise, a 
 tag will
X#               be output before a signature block indicated by a "-- " line
X#               if (-m) is used.
X#
X#             o Each paragraph will be preceded by a "

" tag and followed X# by a "

" tag on a separate line. X# X# Other WW attempts to recognize end-of-sentence and other punctuation X# marks and append the correct number of blanks. An array holds X# common titles such as "Dr." and "Ms." that should have only one X# blank following the period ("."). X# X# X# SCCS ID: @(#) ww 1.13 10/12/99 16:23:48 X# X# @(#) Author: Walt Hultgren - walt@rmy.emory.edu X# X# @(#) (c) Copyright 1996 - 1999 by Walt Hultgren. All rights reserved. X# X# X X XUSAGE="usage: ww [-abhmstH] [-c chars] [-f chars] [-i n] [-l length] [files]" X X X# X# Parse command line X# X Xset -- `getopt 'abc:f:hi:l:mstH' $*` X Xif [ $? -ne 0 ] Xthen X echo "$USAGE" 1>&2 X exit 1 Xfi X X XALLOUT='0' # (-a) XFILL='1' # (-b) XSKIPCH='' # (-c) XFLAGCH='' # (-f) XHEADER='0' # (-h) XINDENT='0' # (-i) XLENG='70' # (-l) XMAIL='0' # (-m) XSECT='0' # (-s) XWHTSPC='0' # (-t) XHTML='0' # (-H) X Xfor ARG in "$@" Xdo X case $ARG in X -a ) ALLOUT="1" ; shift ;; X -b ) FILL="0" ; shift ;; X -c ) SKIPCH="$2" ; shift 2 ;; X -f ) FLAGCH="$2" ; shift 2 ;; X -h ) HEADER="1" ; shift ;; X -i ) INDENT="$2" ; shift 2 ;; X -l ) LENG="$2" ; shift 2 ;; X -m ) MAIL="1" ; shift ;; X -s ) SECT="1" ; shift ;; X -t ) WHTSPC="1" ; shift ;; X -H ) HTML="1" ; shift ;; X -- ) shift ; break ;; X esac Xdone X X X# X# Set wrap margin to one less than window/terminal length if (-l0) specified X# X Xif [ "$LENG" = "0" ] Xthen X LENG=`/usr/5bin/tput cols` X LENG=`expr $LENG - 1` Xfi X X X# X# Set up arguments to initialize nawk variables on the command line X# based on whether the OS is SunOS 4.x or 5.x. X# X X#OS_VER=`uname -r | cut -c1` X Xcase `uname -r` in X X 5* ) PREVARS="-v fill=$FILL -v flagch=$FLAGCH -v header=$HEADER" X PREVARS="$PREVARS -v indent=$INDENT -v maxleng=$LENG -v html=$HTML" X PREVARS="$PREVARS -v mail=$MAIL -v whtspc=$WHTSPC -v sect=$SECT" X PREVARS="$PREVARS -v skipch=$SKIPCH -v allout=$ALLOUT" X POSTVARS="" X ;; X X * ) PREVARS="" X POSTVARS="fill=$FILL skipch=$SKIPCH flagch=$FLAGCH header=$HEADER" X POSTVARS="$POSTVARS indent=$INDENT maxleng=$LENG html=$HTML" X POSTVARS="$POSTVARS mail=$MAIL whtspc=$WHTSPC sect=$SECT" X POSTVARS="$POSTVARS allout=$ALLOUT" X ;; Xesac X X X# X# Process file(s) X# X Xnawk $PREVARS ' X X BEGIN { X X opt = ( fill == 0 ? "b" : "" ) \ X ( header ? "h" : "" ) \ X ( mail ? "m" : "" ) \ X ( sect ? "s" : "" ) \ X ( whtspc ? "t" : "" ) \ X ( html ? "H" : "" ) X if ( opt != "" ) opt = " -" opt X if ( skipch != "" ) opt = opt " -c" skipch X if ( flagch != "" ) opt = opt " -f" flagch X if ( indent != 0 ) opt = opt " -i" indent X opt = opt " -l" maxleng X X if ( mail != 0 ) X { X header = 1 X if ( flagch == "" ) flagch = ">}:" X } X X if ( length( skipch ) > 0 ) skipch_re = "^[" skipch "]" X else skipch_re = "" X X if ( length( flagch ) > 0 ) flagch_re = "^[" flagch "]" X else flagch_re = "" X X line = "" X last_line = "" X line_leng = 0 X num_blanks = 0 X sig = 0 X sectwrap = 1 X X title[1] = "DR." X title[2] = "DRS." X title[3] = "MR." X title[4] = "MRS." X title[5] = "MS." X title[6] = "ST." # "Saint" X X num_titles = 6 X X locase = "abcdefghijklmnopqrstuvwxyz" X upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" X X if ( html == 1 ) X { X bop_string = "

" X X for ( i = 1; i <= indent; ++i ) X bop_string = bop_string " " X X print "" X print "" X print "" X print "" X X if ( header == 1 ) print "

"
X          }
X        else
X          {
X            bop_string = ""
X
X            for ( i = 1; i <= indent; ++i )
X                bop_string = bop_string " "
X          }
X
X        bol_string = bop_string
X
X        }
X
X
X    #
X    #   Skip lines beginning with comment flag character regular expression
X    #
X
X    skipch_re != "" && $0 ~ skipch_re {
X
X        if ( allout == 1 )
X          {
X            line_out()
X            print $0
X          }
X
X        next
X
X        }
X
X
X    #
X    #   Do not wrap mail/news header lines if (-h) or (-m) specified
X    #
X
X    header != 0 {
X
X        if ( $0 ~ /^$/ )
X          {
X            header = 0
X            if ( html == 1 ) print "
" X } X X print $0 X X last_line = $0 X next X X } X X X # X # Do not wrap lines in signature block if mail/news (-m) specified X # X X mail != 0 && $0 == "-- " { X X line_out() X if ( html == 1 ) print "
"
X
X        sig = 1
X
X        }
X
X    sig != 0 {
X
X        print $0
X
X        last_line = $0
X        next
X
X        }
X
X
X    #
X    #   Do not wrap lines beginning with flag character regular expression
X    #
X
X    flagch_re != "" && $0 ~ flagch_re {
X
X        line_out()
X        print $0 ( html ? "
" : "" ) X X last_line = $0 X next X X } X X X # X # If wrapping in sections, check for start/stop lines X # X X sect != 0 && $0 ~/^\+[Ww]$/ { X X line_out() X if ( allout == 1 ) print $0 X if ( html == 1 ) print "
" X X sectwrap = 1 X next X X } X X sect != 0 && $0 ~/^\-[Ww]$/ { X X line_out() X if ( allout == 1 ) print $0 X if ( html == 1 ) print "
"
X
X        sectwrap = 0
X        next
X
X        }
X
X
X    #
X    #   Do not wrap line if currently in a no-wrap section
X    #
X
X    sectwrap == 0 {
X
X        print $0
X
X        last_line = $0
X        next
X
X        }
X
X
X    #
X    #   Check for a blank line that marks the end of the current paragraph
X    #
X
X    NF == 0 {
X
X        line_out()   # flush last line of current paragraph
X
X        if ( html == 1 && last_line != "" ) print "

" X X print $0 # print this blank line X X # Set beginning-of-line blanks for start of next paragraph if X # indentation (-i) specified X X bol_string = bop_string X X last_line = $0 X next X X } X X X # X # Check for the beginning of a paragraph due to whitespace if (-t) X # specified. Print a blank line if previous line was not empty. X # X X whtspc != 0 && $0 ~ /^[ \t]/ { X X line_out() # flush last line of paragraph just ended X X if ( html == 1 && NR!= 1 ) print "

" X X if ( last_line != "" ) X { X print "" X last_line = "" X } X X bol_string = bop_string X X } X X X # X # Process current input line X # X X { X for ( iwd = 1; iwd <= split( $0, arr ); ++iwd ) X app_wd( iwd, arr[iwd] ) X X if ( fill == 0 ) line_out() X X } X X X END { X X line_out() # flush final output line if any X X if ( html == 1 ) X { X if ( last_line != "" ) print "

" X if ( sectwrap == 0 ) print "
" X X print "" X print "" X print "" X } X X } X X X# X# Function to append a word to the current output line X# X Xfunction app_wd( iword, word, xstr, xuc, xleng, i ) { X X # X # Prepend blank(s) to word if needed X # X X if ( line_leng > 0 ) X { X if ( num_blanks == 2 ) xstr = " " word # not first of line X else X if ( num_blanks == 1 ) xstr = " " word X else xstr = word X } X else X { X xstr = bol_string word # beginning of line X bol_string = "" X } X X X # X # Check current output line length and flush line if this word X # will make line too long X # X X xleng = length( xstr ) X X if ( ( line_leng + xleng ) > maxleng ) X { X line_out() X X xstr = word X xleng = length( word ) X } X X X # X # Append word to line X # X X line = line xstr X X line_leng += xleng X X X # X # Set how many blanks should follow this word X # X X if ( word ~ "[.]$" ) X { X if ( word ~ "^[A-Z][.]$" ) X { X num_blanks = 1 # it is an initial X } X else X { X xuc = upshift( word ) X num_blanks = 2 X X for ( i = 1; i <= num_titles; ++i ) # check for title X if ( xuc == title[i] ) num_blanks = 1 # it is a title X } X } X else X { X if ( iword == NF && word ~ "-$" && word !~ "--$" ) X { X num_blanks = 0 # hyphenated word at end of line X } X else X { X if ( word ~ "[:;?!]$" ) # end of sentence or some other X { # punctuation mark requiring 2 X num_blanks = 2 # blanks X } X else X { X num_blanks = 1 # none of the above, use 1 blank X } X } X } X X } X X X# X# Function to print current output line if it contains any text. Blank X# lines are handled elsewhere. X# X Xfunction line_out() { X X if ( line_leng > 0 ) X { X print line X last_line = line X } X X line = "" X line_leng = 0 X X } X X X# X# Function to force letters of a string to upper case X# X Xfunction upshift( str, xstr, xc, i, j ) { X X xstr = "" X X for ( i = 1; i <= length( str ); ++i ) X { X xc = substr( str, i, 1 ) X X if ( ( j = index( locase, xc ) ) ) xstr = xstr substr( upcase, j, 1 ) X else xstr = xstr xc X } X X return xstr X X } X X ' $POSTVARS $* SHAR_EOF if [ `wc -c < ww` -ne 15065 ] then echo "Lengths do not match -- Bad Copy of ww" fi echo "Done." exit 0