aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-06-02 23:11:34 +1200
committerTom Ryder <tom@sanctum.geek.nz>2017-06-02 23:11:34 +1200
commite697d4d5ba0219c4ba955664d01b987f69854c03 (patch)
treed3aac415308a7910c0e39d53cf2106dee6000f68
parentMerge branch 'master' into port/bsd/freebsd (diff)
parentTidier implementation for mi5 sh (diff)
downloaddotfiles-e697d4d5ba0219c4ba955664d01b987f69854c03.tar.gz
dotfiles-e697d4d5ba0219c4ba955664d01b987f69854c03.zip
Merge branch 'master' into port/bsd/freebsd
-rw-r--r--IDEAS.markdown2
-rw-r--r--Makefile20
-rw-r--r--bin/mi5.awk55
-rw-r--r--man/man1/mi5.1df37
4 files changed, 70 insertions, 44 deletions
diff --git a/IDEAS.markdown b/IDEAS.markdown
index 61f1049d..3b0f1c75 100644
--- a/IDEAS.markdown
+++ b/IDEAS.markdown
@@ -12,5 +12,3 @@ Ideas
processes and mkfifo(1).
* Write something like hcat(1df) or tcat(1df) that includes filename headings
for each concatenated file.
-* mi5(1df) could be made to handle comment delimiters and $1 $2 expansions
- without too much pain (substr/index counting)
diff --git a/Makefile b/Makefile
index 8f278325..5bb29685 100644
--- a/Makefile
+++ b/Makefile
@@ -197,6 +197,15 @@ BINS = bin/ap \
bin/xrbg \
bin/xrq
+BINS_MI5 = bin/chn.sh \
+ bin/edda.sh \
+ bin/pst.sh \
+ bin/rndl.sh \
+ bin/swr.sh \
+ bin/tlcs.sh \
+ bin/try.sh \
+ bin/urlc.sh
+
GAMES = games/aaf \
games/acq \
games/aesth \
@@ -269,14 +278,7 @@ clean distclean:
.m4.sh:
m4 < $< > $@
-bin/chn.sh: include/mktd.m4
-bin/edda.sh: include/mktd.m4
-bin/pst.sh: include/mktd.m4
-bin/rndl.sh: include/mktd.m4
-bin/swr.sh: include/mktd.m4
-bin/tlcs.sh: include/mktd.m4
-bin/try.sh: include/mktd.m4
-bin/urlc.sh: include/mktd.m4
+$(BINS_MI5): include/mktd.m4
git/gitconfig: git/gitconfig.m4
m4 \
@@ -498,7 +500,7 @@ check: check-bin \
check-bash:
sh check/bash.sh
-check-bin:
+check-bin: $(BINS_MI5)
sh check/bin.sh
check-games:
diff --git a/bin/mi5.awk b/bin/mi5.awk
index 32bac6e9..1ddc5a2f 100644
--- a/bin/mi5.awk
+++ b/bin/mi5.awk
@@ -16,32 +16,51 @@ NF == 1 && $1 == "%>" && mac {
next
}
-# If processing macros, strip leading and trailing whitespace and skip blank
-# lines
-mac {
+# If in a block, print each line with any content on it after stripping leading
+# and trailing whitespace
+mac && NF {
sub(/^ */, "")
sub(/ *$/, "")
-}
-mac && !NF { next }
-
-# Inlines
-mac {
print $0 "dnl"
}
+
+# If not in a block, look for inlines to process
!mac {
- # Don't let apostrophes close the comment
- gsub(/'/, "''`")
+ # We'll empty one variable into another
+ src = $0
+ dst = ""
+
+ # As long as there's a pair of opening and closing tags
+ while (src ~ /<%.*%>/) {
+
+ # Read up to opening tag into seg, shift from src
+ ind = index(src, "<%")
+ seg = substr(src, 1, ind - 1)
+ src = substr(src, ind)
- # Replace m5 opener with m4 closer
- gsub(/<% */, "'")
+ # Escape quote closer and add to dst
+ gsub(/'/, "''`", seg)
+ dst = dst seg
- # Replace m5 closer with m4 opener
- gsub(/ *%>/, "`")
- print
+ # Read up to closing tag into seg, shift from src
+ ind = index(src, "%>")
+ seg = substr(src, 1, ind + 1)
+ src = substr(src, ind + 2)
+
+ # Translate tags to quote open and close and add to dst
+ sub(/^<% */ , "'", seg)
+ sub(/ *%>$/ , "`", seg)
+ dst = dst seg
+ }
+
+ # Escape quote closers in whatever's left
+ gsub(/'/, "''`", src)
+
+ # Tack that onto the end, and print it
+ dst = dst src
+ print dst
}
# Print an m4 closer and newline deleter as the last bytes
-END {
- print "'dnl"
-}
+END { print "'dnl" }
diff --git a/man/man1/mi5.1df b/man/man1/mi5.1df
index 7fb41078..04f964c1 100644
--- a/man/man1/mi5.1df
+++ b/man/man1/mi5.1df
@@ -10,7 +10,8 @@ FILE > out.m4
FILE1 FILE2 > out.m4
.br
prog |
-.B mi5 > out.m4
+.B mi5
+> out.m4
.br
.SH DESCRIPTION
.B mi5
@@ -19,21 +20,23 @@ and predictable for its author, who wants badly to like m4 but doesn't. It's
primarily intended for situations where the majority of a file is simple static
text, and only a few simple macros need to be defined and expanded, which
covers almost every usage case for the author. It's written to work with any
-POSIX m4.
+POSIX awk and to generate output for any POSIX m4.
.P
mi5 inverts m4's usual approach by approaching most of the file as if it were
part of an m4 quote, with <% and %> as the delimiters to specify markers in
-which macro expansion should occur. This makes m4 work in a way reminiscent of
-templating libraries or languages like PHP.
+which macro expansion should occur. This is therefore a way to shoehorn m4 into
+working in a way reminiscent of templating libraries or languages like PHP.
.P
Macros can be expanded as blocks:
.P
<%
- define(`FOO', `bar')
- define(`BAZ', include(`include/quux.inc')
- ?>
+
+ define(`FOO', `bar')
+ define(`BAZ', include(`include/quux.inc')
+
+ %>
.P
-For this format, "dnl" macros to delete newlines for each declaration are
+For this format, `dnl' macros to delete newlines for each declaration are
inserted for you. Blank lines are skipped, and leading and trailing spaces are
ignored. The above code therefore produces no actual output, as it only has two
define calls.
@@ -43,15 +46,19 @@ For inline expansion, the syntax is similar, but the behaviour slightly differen
The value of the FOO macro is <% FOO %>.
.P
Spaces immediately after the opening delimiter and before the closing delimiter
-are ignored, but spaces produced within the macro are preserved.
+are ignored, but spaces produced within the macro are preserved. `dnl` macros
+are not inserted for inline blocks.
.P
-Ideally, you do macro definition in an mi5 block at the top of your file, and
-very simple macro expansion in an mi5 inline.
+Ideally, you do your complex macro definition in a block at the top of your
+file, and your simple macro expansion of those results in an inline.
.SH CAVEATS
-Only very simple macro expansions work in inline calls at the moment. This can
-be fixed by the author tokenizing the line properly, which he'll do Real Soon
-Now (TM). Specifically, quote delimiters do not work.
+The <% delimiters %> are hardcoded for now.
+.P
+Inline expansions cannot span multiple lines. Use blocks for that.
+.P
+Doesn't cope at all with `changequote'. If you need that, you should probably
+write raw m4.
.SH SEE ALSO
-bp(1df), xargs(1)
+m4(1)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>