M4 Troubles…

In my previous post I wrote about mostly M4 templates. Well, it turns out I’ve run into some issues.

Chapter I: The Problem

I ran my blog script to convert my markdown into a nice HTML webpage, but then something happened. Nothing. Nothing happened. The script just sat there, stuck. This was odd because the script usually runs in under a second, so something had to have gone wrong…

Chapter II: The Quick Fix

I turned on tracing in my head.m4 by adding traceon to the start of the file then I ran the script again. I knew instantly what had gone wrong… M4 was spitting out the same HTML over and over and over again: It was expanding the macros I had written in my post! Creating an infinite loop.

As a quick fix in the post I replaced all occurrences of my actual macros with ones that were slightly different. It worked! …but then M4 gave another error:

m4: unexpected end of input, unclosed parenthesis:

Chapter III: The Root of All Evil

In my search for answers I came across this post: http://silas.net.br/tech/devel/m4.html. While the particular issue in on the webpage wasn’t my issue, it gave me a hint as to what had gone wrong. M4 must have been trying to expand or evaluate something else in my post’s text. Eventually I figured out that M4 was trying to run all the define() and include() statements I had written in my post. Needless to say, writing a post about M4 while using M4 for my templates was causing some issues…

As a crappy bandaid I added a couple of seds to my script:

article_html=$(echo "$article_html" |  sed -E -e "s/define\(/\'!define\(!\"/g" -e "s/include\(/\'!include\(!\"/g")

Because I only wrote define and include in my M4 code snippets for the post, this worked just fine. (Hopefully it works for this post too).

I may have to rethink using M4 completely because I won’t lie I have no idea what I’m doing - it seems like I’m bound to run into this issue or a similar one in the future. Maybe Timmy was wrong; maybe I should just echo a bunch of HTML in my script instead, surely it can’t be that messy. We’ll have to see what I come up with.

Chapter IV: Going Forward

For now, consider my previous post to be non-cannon in the /bin/sh blog series (if you can really call it a series - there hasn’t even been a part II yet!).