3 ms·
M4 has some well-known uses like Sendmail and GNU Autoconf, but I don’t think this can be counted in M4’s favor: For operating in a standard Unix-like environm
by teddyh 7y ago
M4 has some well-known uses like Sendmail and GNU Autoconf, but I don’t think this can be counted in M4’s favor: For operating in a standard Unix-like environment, there is really no good alternative to m4 for generic text macro expansion. You can try to force cpp – the C preprocessor – into this, but it is not suited to not being part of a C compiler pipeline. For better or for worse, m4 is it in the world of Unix and shell tools, even though its syntax is quite annoying to use.
Of course, if you operate within a language ecosystem, you can probably find a nice templating system there. Python, for instance, has Jinja2. Or, if your needs are simple, you can use something more basic, like sed(1).
- downerending 7y agoI've looked at m4 before, and want to love it, but it just seems a bit too clumsy to use. And unfortunately, IIRC, unlike sed it's not ubiquitously available. Not really crazy about Jinja2 either, but at this point it seems like it has a lot more mindshare and maintenance.
- froh 7y agoM4 as part of posix should be as widely available as sed, no?
- downerending 7y agoIn principle, yes. In practice, it seems like it's often not installed on the random hosts I encounter. This isn't a fatal flaw, but it is a small advantage in favor of sed and awk and probably even perl, which are essentially ubiquitous these days.
- PaulHoule 7y agoLast time I used M4 it was for a multi language system where I needed to substitute in PHP, shell scripts, apache conf files, etc.
- Blackthorn 7y agoMustache / Handlebars is pretty widely available throughout languages these days too and is pretty basic. Doesn't help you at the default posix level, but there are even command line versions IIRC
- chubot 7y agoPlain shell is pretty good for templating, and it's the most available language on Unix. You have here docs for long literals, and you can choose to use $var expansion or not with EOF or 'EOF' (yes it's a confusing syntax). You have loops and conditionals, and functions that take parameters to modularize it (e.g. a function for each table row). Sequential composition "concatenates" stdout. At the end you redirect it all like this, which is inconvenient in other languages: top-level-function > out.html ---- For example, I generate this page with shell: http://www.oilshell.org/releases.html http://www.oilshell.org/releases.html And it's separated into 3-4 functions here: https://github.com/oilshell/oil/blob/master/devtools/release.sh#L809 https://github.com/oilshell/oil/blob/master/devtools/release... ---- Shell falls down when you have to generate complex logic as with tables [1], but m4 also falls down there. I actually experimented with many different methods in order to help me design the Oil language. This is generated with awk, and it has some advantages over shell: https://www.oilshell.org/release/0.8.pre3/test/spec.wwz/osh.html https://www.oilshell.org/release/0.8.pre3/test/spec.wwz/osh.... These two are generated with Python: https://www.oilshell.org/release/0.8.pre3/test/spec.wwz/alias.html https://www.oilshell.org/release/0.8.pre3/test/spec.wwz/alia... https://www.oilshell.org/release/0.8.pre3/test/wild.wwz/ https://www.oilshell.org/release/0.8.pre3/test/wild.wwz/ So I hope Oil will do better than shell, awk, and Python. ----- Shell needs to be better for escaping. Template languages like Jinja do better there. I put m4 in the "70's-style macro processing bucket" which is generally sloppy about escaping. I use Python for escaping here, but I do it with O(1) processes and not one process for every tiny fragment that needs to be escaped! Git Log in HTML: A Harder Problem and A Safe Solution http://www.oilshell.org/blog/2017/09/29.html http://www.oilshell.org/blog/2017/09/29.html