Home | History | Annotate | Line # | Download | only in testdir
      1 # fmt - format
      2 #    input:  text
      3 #    output: text formatted into lines of <= 72 characters
      4 
      5 BEGIN {
      6         maxlen = 72
      7 }
      8 
      9 /^[ \t]/ { printline(); print; next }      # verbatim
     10 ###/^ +/ { printline();  }			# whitespace == break
     11 
     12 /./  { for (i = 1; i <= NF; i++) addword($i); next }
     13 
     14 /^$/ { printline(); print "" }
     15 END  { printline() }
     16 
     17 function addword(w) {
     18     print "adding [", w, "] ", length(w), length(line), maxlen
     19     if (length(line) + length(w) > maxlen)
     20         printline()
     21     if (length(w) > 2 && ( w ~ /[\.!]["?)]?$/ || w ~ /[?!]"?$/) &&
     22 		w !~ /^(Mr|Dr|Ms|Mrs|vs|Ph.D)\.$/)
     23         w = w " "
     24     line = line " " w
     25 }
     26 
     27 function printline() {
     28     if (length(line) > 0) {
     29         sub(/ +$/, "", line)
     30         print substr(line, 2)   # removes leading blank
     31         line = ""
     32     }
     33 }
     34