aboutsummaryrefslogtreecommitdiff
path: root/bin/mi5.awk
blob: c05955ff7c74a87a3efde878615d1f87d169d3e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Crude m4 preprocessor
BEGIN { mac = 0 }

# Print an m4 opener as the first byte
NR == 1 { printf "`" }

# Blocks
NF == 1 && $1 == "<%" && !mac {
    mac = 1
    printf "'"
    next
}
NF == 1 && $1 == "%>" && mac {
    mac = 0
    printf "`"
    next
}

# If processing macros, strip leading and trailing whitespace and skip blank
# lines
mac {
    sub(/^ */, "")
    sub(/ *$/, "")
}
mac && !NF { next }

# Inlines
mac {
    print $0 "dnl"
}
!mac {

    # Don't let apostrophes close the comment
    gsub(/'/, "''`")

    # Don't let $ signs confound expansion
    gsub(/\$/, "$'`")

    # Replace m5 opener with m4 closer
    gsub(/<% */, "'")

    # Replace m5 closer with m4 opener
    gsub(/ *%>/, "`")
    print
}

# Print an m4 closer and newline deleter as the last bytes
END {
    print "'dnl"
}