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